//---------- main

        /**
         * Runs the discoverer and shows a message dialog with the returned report.
         * @param args args[0] - stun server address, args[1] - port. in the case of
         * no args - defaults are provided.
         * @throws java.lang.Exception if an exception occurrs during the discovery
         * process.
         */
#if false
        public static void main(String[] args)
        {
            StunAddress localAddr  = null;
            StunAddress serverAddr = null;

            if (args.Length == 4)
            {
                localAddr  = new StunAddress(args[2], Integer.valueOf(args[3]).intValue());
                serverAddr = new StunAddress(args[0],
                                             Integer.valueOf(args[1]).intValue());
            }
            else
            {
                localAddr  = new StunAddress(InetAddress.getLocalHost(), 5678);
                serverAddr = new StunAddress("stun01bak.sipphone.com.", 3479);
            }
            NetworkConfigurationDiscoveryProcess addressDiscovery =
                new NetworkConfigurationDiscoveryProcess(localAddr, serverAddr);

            addressDiscovery.start();
            StunDiscoveryReport report = addressDiscovery.determineAddress();

            System.oout.println(report);
//        javax.swing.JOptionPane.showMessageDialog(
//                null,
//                report.toString(),
//                "Stun Discovery Process",
//                javax.swing.JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
Ejemplo n.º 2
0
        /**
         * Compares this object with obj. Two reports are considered equal if and
         * only if both have the same nat type and their public addresses are
         * equal or are both null.
         * @param obj the object to compare against.
         * @return true if the two objects are equal and false otherwise.
         */
        public override bool Equals(Object obj)
        {
            if (obj == null ||
                !(obj is StunDiscoveryReport))
            {
                return(false);
            }

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

            StunDiscoveryReport target = (StunDiscoveryReport)obj;

            return(target.GetNatType() == GetNatType() &&
                   (GetPublicAddress() == null && target.GetPublicAddress() == null ||
                    target.GetPublicAddress().Equals(GetPublicAddress())));
        }
        /**
         * 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
         */
        public virtual 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;
                    }
                }
            }
        }
        /**
         * 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);
                    }
                }
            }
        }