Exemplo n.º 1
0
        /**
         * Compares two STUN Attributes. Attributeas are considered equal when their
         * type, length, and all data are the same.
         *
         * @param obj the object to compare this attribute with.
         * @return true if the attributes are equal and false otherwise.
         */
        public override bool Equals(Object obj)
        {
            if (!(obj is AddressAttribute) ||
                obj == null)
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }

            AddressAttribute att = (AddressAttribute)obj;

            if (att.GetAttributeType() != GetAttributeType() ||
                att.GetDataLength() != GetDataLength()
                //compare data
                || att.GetFamily() != GetFamily() ||
                (att.GetAddress() != null &&
                 !address.Equals(att.GetAddress()))
                )
            {
                return(false);
            }

            //addresses
            if (att.GetAddress() == null && GetAddress() == null)
            {
                return(true);
            }

            return(true);
        }
        /**
         * Compares this object against the specified object. The result is true if
         * and only if the argument is not null and it represents the same address
         * as this object.
         *
         * Two instances of InetSocketAddress represent the same address if both the
         * InetAddresses (or hostnames if it is unresolved) and port numbers are
         * equal. If both addresses are unresolved, then the hostname & the port
         * number are compared.
         *
         * @param obj the object to compare against.
         * @return true if the objects are the same; false otherwise.
         */
        public override bool Equals(object obj)
        {
            if (obj == null ||
                !(obj is NetAccessPointDescriptor))
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }

            return(stunAddr.Equals(((NetAccessPointDescriptor)obj).stunAddr));
        }
        /**
         * Implements the discovery process itself (see class description).
         * @return a StunDiscoveryReport containing details about the network
         * configuration of the host where the class is executed.
         * @throws StunException ILLEGAL_STATE if the discoverer has not been started
         * NETWORK_ERROR or ILLEGAL_ARGUMENT if a failure occurs while executing
         * the discovery algorithm
         */
        virtual public StunDiscoveryReport determineAddress()
        {
            checkStarted();
            StunDiscoveryReport report = new StunDiscoveryReport();
            StunMessageEvent    evt    = doTestI(serverAddress);

            if (evt == null)
            {
                //UDP Blocked
                report.SetNatType(StunDiscoveryReport.UDP_BLOCKING_FIREWALL);
                return(report);
            }
            else
            {
                StunAddress mappedAddress = ((MappedAddressAttribute)evt.GetMessage().
                                             GetAttribute(Attribute.MAPPED_ADDRESS)).GetAddress();

                StunAddress backupServerAddress = ((ChangedAddressAttribute)evt.GetMessage().
                                                   GetAttribute(Attribute.CHANGED_ADDRESS)).GetAddress();
                report.SetPublicAddress(mappedAddress);
                if (mappedAddress.Equals(apDescriptor.GetAddress()))
                {
                    evt = doTestII(serverAddress);
                    if (evt == null)
                    {
                        //Sym UDP Firewall
                        report.SetNatType(StunDiscoveryReport.SYMMETRIC_UDP_FIREWALL);
                        return(report);
                    }
                    else
                    {
                        //open internet
                        report.SetNatType(StunDiscoveryReport.OPEN_INTERNET);
                        return(report);
                    }
                }
                else
                {
                    evt = doTestII(serverAddress);
                    if (evt == null)
                    {
                        evt = doTestI(backupServerAddress);
                        if (evt == null)
                        {
                            // System.oout.println("Failed to receive a response from backup stun server!");
                            return(report);
                        }
                        StunAddress mappedAddress2 = ((MappedAddressAttribute)evt.GetMessage().
                                                      GetAttribute(Attribute.MAPPED_ADDRESS)).GetAddress();
                        if (mappedAddress.Equals(mappedAddress2))
                        {
                            evt = doTestIII(serverAddress);
                            if (evt == null)
                            {
                                //port restricted cone
                                report.SetNatType(StunDiscoveryReport.PORT_RESTRICTED_CONE_NAT);
                                return(report);
                            }
                            else
                            {
                                //restricted cone
                                report.SetNatType(StunDiscoveryReport.RESTRICTED_CONE_NAT);
                                return(report);
                            }
                        }
                        else
                        {
                            //Symmetric NAT
                            report.SetNatType(StunDiscoveryReport.SYMMETRIC_NAT);
                            return(report);
                        }
                    }
                    else
                    {
                        //full cone
                        report.SetNatType(StunDiscoveryReport.FULL_CONE_NAT);
                        return(report);
                    }
                }
            }
        }