/**
         * @param port the local port to bind to
         * @param local_tas an IEnumerable object which gives the list of local
         * IPs.  This is consulted every time LocalTAs is accessed, so it can
         * change as new interfaces are added
         * @param ta_auth the TAAuthorizer for packets incoming
         */
        public UdpEdgeListener(int port, IEnumerable local_config_ips, TAAuthorizer ta_auth)
        {
            _s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, Int32.MaxValue);
            _s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, Int32.MaxValue);
            ipep = new IPEndPoint(IPAddress.Any, port);
            _s.Bind(ipep);
            _port = port = ((IPEndPoint)(_s.LocalEndPoint)).Port;

            /**
             * We get all the IPAddresses for this computer
             */
            if (local_config_ips == null)
            {
                _tas = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Udp, _port);
            }
            else
            {
                _tas = TransportAddressFactory.Create(TransportAddress.TAType.Udp, _port, local_config_ips);
            }
            _nat_hist = null;
            _nat_tas  = new NatTAs(_tas, _nat_hist);
            _ta_auth  = ta_auth;
            if (_ta_auth == null)
            {
                //Always authorize in this case:
                _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
            }
            //We start out expecting around 30 edges with
            //a load factor of 0.15 (to make edge lookup fast)
            _id_ht        = new Hashtable(30, 0.15f);
            _remote_id_ht = new Hashtable();
            _sync         = new object();
            _running      = 0;
            _isstarted    = 0;
            ///@todo, we need a system for using the cryographic RNG
            _rand                     = new Random();
            _send_handler             = this;
            _listen_finished_event    = new ManualResetEvent(false);
            _listen_thread            = new Thread(ListenThread);
            _send_thread              = new Thread(SendThread);
            _send_thread.IsBackground = true;
            _send_queue               = new LFBlockingQueue <UdpMessage>();
        }
Example #2
0
        public void TestPortPrediction()
        {
            Edge e = new FakeEdge(TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:80"),
                                  TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:1080"));
            NatHistory h = null;

            h = h + new NewEdgePoint(DateTime.UtcNow, e);
            h = h + new LocalMappingChangePoint(DateTime.UtcNow, e,
                                                TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:80"));
            NatHandler nh = new PublicNatHandler();

            Assert.IsTrue(nh.IsMyType(h), "PublicNatHandler");
            IList tas = nh.TargetTAs(h);

            Assert.IsTrue(tas.Contains(
                              TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:80")
                              ), "ConeNatHandler.TargetTAs");

            nh = new ConeNatHandler();
            Assert.IsTrue(nh.IsMyType(h), "ConeNatHandler");
            tas = nh.TargetTAs(h);
            //foreach(object ta in tas) { Console.Error.WriteLine(ta); }
            Assert.IsTrue(tas.Contains(
                              TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:80")
                              ), "ConeNatHandler.TargetTAs");

            /*
             * Now, let's try Port prediction:
             */
            int local_port = 80;
            int port       = local_port;

            h = null;
            while (port < 86)
            {
                e = new FakeEdge(TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:"
                                                                        + local_port.ToString()),
                                 TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:1081"));
                h = h + new NewEdgePoint(DateTime.UtcNow, e);

                h = h + new LocalMappingChangePoint(DateTime.UtcNow, e,
                                                    TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:"
                                                                                           + port.ToString()
                                                                                           ));
                port = port + 1;
            }
            nh = new SymmetricNatHandler();
            Assert.IsTrue(nh.IsMyType(h), "SymmetricNatHandler");
            tas = nh.TargetTAs(h);
            //foreach(object ta in tas) { Console.Error.WriteLine(ta); }
            Assert.IsTrue(tas.Contains(
                              TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:86")
                              ), "SymmetricNatHandler.TargetTAs");
            nh = new LinuxNatHandler();
            Assert.IsTrue(nh.IsMyType(h), "LinuxNatHandler");
            tas = nh.TargetTAs(h);
            //foreach(object ta in tas) { Console.Error.WriteLine(ta); }
            Assert.IsTrue(tas.Contains(
                              TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:86")
                              ), "LinuxNatHandler.TargetTAs");
            Assert.IsTrue(tas.Contains(
                              TransportAddressFactory.CreateInstance("brunet.udp://128.128.128.128:80")
                              ), "LinuxNatHandler.TargetTAs");
        }
Example #3
0
 public NatHistory(NatHistory nh, NatDataPoint ndp) : base(nh, ndp)
 {
 }
Example #4
0
 /**
  * @param local_config_tas the list of TAs to use as last resort
  * @param NatHistory history information learned from talking to peers (may be null)
  */
 public NatTAs(IEnumerable local_config_tas, NatHistory hist)
 {
     _hist         = hist;
     _local_config = local_config_tas;
 }
Example #5
0
 /**
  * @param local_config_tas the list of TAs to use as last resort
  * @param NatHistory history information learned from talking to peers (may be null)
  */
 public NatTAs(IEnumerable local_config_tas, NatHistory hist)
 {
     _nat_type     = string.Empty;
     _hist         = hist;
     _local_config = local_config_tas;
 }