예제 #1
0
        /**
         * Creates a listening point from the following address and attempts to
         * discover how it is mapped so that using inside the application is possible.
         * @param address the [address]:[port] pair where ther request should be
         * sent from.
         * @return a StunAddress object containing the mapped address or null if
         * discovery failed.
         * @throws StunException if something fails along the way.
         */
        public virtual StunAddress GetMappingFor(StunAddress address)
        {
            NetAccessPointDescriptor apDesc = new NetAccessPointDescriptor(address);

            stunStack.InstallNetAccessPoint(apDesc);

            requestSender = new BlockingRequestSender(stunProvider, apDesc);
            StunMessageEvent evt = null;

            try
            {
                evt = requestSender.SendRequestAndWaitForResponse(
                    MessageFactory.CreateBindingRequest(), serverAddress);
            }
            finally
            {
                //free the port to allow the application to use it.
                stunStack.RemoveNetAccessPoint(apDesc);
            }

            if (evt != null)
            {
                Response res = (Response)evt.GetMessage();
                MappedAddressAttribute maAtt =
                    (MappedAddressAttribute)
                    res.GetAttribute(Attribute.MAPPED_ADDRESS);
                if (maAtt != null)
                {
                    StunAddress sa = maAtt.GetAddress();
                    return(sa);
                }
            }

            return(null);
        }
예제 #2
0
        /**
         * Creates a listening point for the specified socket and attempts to
         * discover how its local address is NAT mapped.
         * @param socket the socket whose address needs to be resolved.
         * @return a StunAddress object containing the mapped address or null if
         * discovery failed.
         * @throws StunException if something fails along the way.
         */

        public virtual StunAddress GetMappingFor(MyUdpClient socket)
        {
            NetAccessPointDescriptor apDesc = stunStack.InstallNetAccessPoint(socket);

            requestSender = new BlockingRequestSender(stunProvider, apDesc);
            StunMessageEvent evt = null;

            try
            {
                evt = requestSender.SendRequestAndWaitForResponse(
                    MessageFactory.CreateBindingRequest(), serverAddress);
            }
            finally
            {
                stunStack.RemoveNetAccessPoint(apDesc);
            }

            if (evt != null)
            {
                Response res = (Response)evt.GetMessage();
                MappedAddressAttribute maAtt =
                    (MappedAddressAttribute)
                    res.GetAttribute(Attribute.MAPPED_ADDRESS);
                if (maAtt != null)
                {
                    return(maAtt.GetAddress());
                }
            }

            return(null);
        }