CreateInstance() public static method

public static CreateInstance ( TransportAddress t, IPAddress host, int port ) : TransportAddress
t TransportAddress
host System.Net.IPAddress
port int
return TransportAddress
コード例 #1
0
        public void Test()
        {
            TAAuthorizer     a1 = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
            TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:45");

            Assert.IsTrue(a1.IsNotDenied(ta), "constant allow");
            TAAuthorizer a2 = new ConstantAuthorizer(TAAuthorizer.Decision.Deny);

            Assert.IsFalse(a2.IsNotDenied(ta), "constant deny");

            IPAddress    network = IPAddress.Parse("10.128.0.0");
            TAAuthorizer a3      = new NetmaskTAAuthorizer(network, 9,
                                                           TAAuthorizer.Decision.Deny,
                                                           TAAuthorizer.Decision.None);
            TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.255.255.255:80");

            Assert.AreEqual(a3.Authorize(ta2), TAAuthorizer.Decision.Deny, "Netmask Deny");
            TransportAddress ta3 = TransportAddressFactory.CreateInstance("brunet.udp://10.1.255.255:80");

            Assert.AreEqual(a3.Authorize(ta3), TAAuthorizer.Decision.None, "Netmask None");
            //Here is the series:
            //If Netmask doesn't say no, constant says yes:
            TAAuthorizer[] my_auths = new TAAuthorizer[] { a3, a1 };
            TAAuthorizer   a4       = new SeriesTAAuthorizer(my_auths);

            Assert.AreEqual(a4.Authorize(ta2), TAAuthorizer.Decision.Deny, "Series Deny");
            Assert.AreEqual(a4.Authorize(ta3), TAAuthorizer.Decision.Allow, "Series Allow");
        }
コード例 #2
0
        /** return the base TransportAddress and the path associated with it
         */
        public static TransportAddress SplitPath(TransportAddress ta, out string path)
        {
            string tas = ta.ToString();
            // Need to be careful of the case ta:////ta:9/
            int  init_idx = tas.IndexOf("://") + 3;
            int  idx      = init_idx;
            int  pos      = 0;
            bool next     = false;

            for (; idx < tas.Length; idx++)
            {
                if (tas[idx] == '/')
                {
                    if (!next)
                    {
                        pos = idx;
                    }
                }
                else
                {
                    next = false;
                }
            }

            if (pos > 0)
            {
                path = tas.Substring(pos);
                return(TransportAddressFactory.CreateInstance(tas.Substring(0, pos)));
            }
            else
            {
                path = "/";
                return(ta);
            }
        }
コード例 #3
0
ファイル: TcpEdge.cs プロジェクト: pcbing/brunet
 /**
  * The send_cb is the method which actually does the
  * sending (which is in UdpEdgeListener).
  */
 public TcpEdge(IEdgeSendHandler send_cb,
                bool is_in, Socket s) : base(send_cb, is_in)
 {
     //This will update both the end point and the remote TA
     Socket     = s;
     _localta   = TransportAddressFactory.CreateInstance(TAType, (IPEndPoint)s.LocalEndPoint);
     _remoteta  = TransportAddressFactory.CreateInstance(TAType, (IPEndPoint)s.RemoteEndPoint);
     _buffer    = new byte[MAX_PACKET + 2]; //+2 to include the size of the packet.
     _written   = 0;
     _flush_ptr = 0;
 }
コード例 #4
0
 /**
  * The send_cb is the method which actually does the
  * sending (which is in UdpEdgeListener).
  */
 public UdpEdge(IEdgeSendHandler send_cb,
                bool is_in,
                System.Net.IPEndPoint remote_end_point,
                System.Net.IPEndPoint local_end_point,
                int id, int remoteid) : base(send_cb, is_in)
 {
     //This will update both the end point and the remote TA
     this.End  = remote_end_point;
     _localta  = TransportAddressFactory.CreateInstance(TAType, (IPEndPoint)local_end_point);
     _id       = id;
     _remoteid = remoteid;
 }
コード例 #5
0
        /// <summary>Converts a list of TAs as strings into TA objects.</summary>
        protected void UpdateRemoteTAs(IList tas_as_str)
        {
            var tas = new List <TransportAddress>(tas_as_str.Count);

            foreach (string ta in tas_as_str)
            {
                try {
                    tas.Add(TransportAddressFactory.CreateInstance(ta));
                } catch (Exception e) {
                    ProtocolLog.WriteIf(ProtocolLog.Exceptions, "Unexpected exception: " + e);
                }
            }
            UpdateRemoteTAs(tas);
        }
コード例 #6
0
        /** Join a path to the end of a TransportAddress
         */
        public static TransportAddress JoinPath(TransportAddress ta, string path)
        {
            Uri    orig_u = ta.Uri;
            string s      = orig_u.ToString();

            if (s[s.Length - 1] == '/')
            {
                s = s.Substring(0, s.Length - 1);
            }
            if (path[0] == '/')
            {
                path = path.Substring(1);
            }
            return(TransportAddressFactory.CreateInstance(String.Format("{0}/{1}", s, path)));
        }
コード例 #7
0
 public FunctionEdgeListener(int id, double loss_prob, TAAuthorizer ta_auth)
 {
     _listener_id = id;
     _ploss_prob  = loss_prob;
     if (ta_auth == null)
     {
         _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
     }
     else
     {
         _ta_auth = ta_auth;
     }
     _tas = new ArrayList();
     _tas.Add(TransportAddressFactory.CreateInstance("brunet.function://localhost:" +
                                                     _listener_id.ToString()));
     _queue        = new BC.LFBlockingQueue <FQEntry>();
     _queue_thread = new Thread(new ThreadStart(StartQueueProcessing));
 }
コード例 #8
0
 public SimulationEdgeListener(int id, double loss_prob, TAAuthorizer ta_auth, bool use_delay)
 {
     _edges       = new Dictionary <Edge, Edge>();
     _use_delay   = use_delay;
     _listener_id = id;
     _ploss_prob  = loss_prob;
     if (ta_auth == null)
     {
         _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
     }
     else
     {
         _ta_auth = ta_auth;
     }
     _tas = new ArrayList();
     _tas.Add(TransportAddressFactory.CreateInstance("b.s://" + _listener_id));
     _rand = new Random();
 }
コード例 #9
0
        public void Test()
        {
            TransportAddress tas = TransportAddressFactory.CreateInstance("b.s://234580");

            Assert.AreEqual(tas.ToString(), "b.s://234580", "Simulation string");
            Assert.AreEqual((tas as SimulationTransportAddress).ID, 234580, "Simulation id");
            Assert.AreEqual(TransportAddress.TAType.S, tas.TransportAddressType, "Simulation ta type");
            TransportAddress ta1 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000");

            Assert.AreEqual(ta1.ToString(), "brunet.udp://10.5.144.69:5000", "Testing TA parsing");

            TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000");

            Assert.AreEqual(ta1, ta2, "Testing TA Equals");



            string           StrLocalHost = Dns.GetHostName();
            IPHostEntry      IPEntry      = Dns.GetHostEntry(StrLocalHost);
            TransportAddress local_ta     = TransportAddressFactory.CreateInstance("brunet.udp://" + IPEntry.AddressList[0].ToString() +
                                                                                   ":" + 5000);
            IEnumerable locals = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Udp, 5000);

            bool match = false;

            foreach (TransportAddress test_ta1 in locals)
            {
                //Console.WriteLine("test_ta: {0}", test_ta1);
                if (test_ta1.Equals(local_ta))
                {
                    match = true;
                }
            }
            Assert.AreEqual(match, true, "testing local TA matches");
            //testing function TA
            TransportAddress func_ta  = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000");
            TransportAddress func_ta2 = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000");

            Assert.AreEqual(func_ta, func_ta2, "equality of instances");
            Assert.IsTrue(func_ta == func_ta2, "reference equality, test of caching");
            Assert.AreEqual(func_ta.ToString(), "brunet.function://localhost:3000", "Testing function TA parsing");
        }
コード例 #10
0
 public SimulationEdgeListener(int id, double loss_prob, TAAuthorizer ta_auth, bool use_delay)
 {
     _edges       = new Hashtable();
     _use_delay   = use_delay;
     _sync        = new object();
     _ba          = new BufferAllocator(Int16.MaxValue);
     _listener_id = id;
     _ploss_prob  = loss_prob;
     if (ta_auth == null)
     {
         _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
     }
     else
     {
         _ta_auth = ta_auth;
     }
     _tas = new ArrayList();
     _tas.Add(TransportAddressFactory.CreateInstance("b.s://" + _listener_id));
     _rand = new Random();
 }
コード例 #11
0
            protected void HandleReads(SocketState ss)
            {
                ArrayList readsocks   = ss.ReadSocks;
                Socket    listen_sock = ss.ListenSock;

                for (int i = 0; i < readsocks.Count; i++)
                {
                    Socket s = (Socket)readsocks[i];
                    //See if this is a new socket
                    if (s == listen_sock)
                    {
                        TcpEdge e     = null;
                        Socket  new_s = null;
                        try {
                            new_s = listen_sock.Accept();
                            IPEndPoint rep = (IPEndPoint)new_s.RemoteEndPoint;
                            new_s.LingerState = new LingerOption(true, 0);
                            TransportAddress rta =
                                TransportAddressFactory.CreateInstance(TransportAddress.TAType.Tcp, rep);
                            if (ss.TAA.Authorize(rta) == TAAuthorizer.Decision.Deny)
                            {
                                //No thank you Dr. Evil
                                Console.Error.WriteLine("Denying: {0}", rta);
                                new_s.Close();
                            }
                            else
                            {
                                //This edge looks clean
                                TcpEdgeListener.SetSocketOpts(s);
                                e = new TcpEdge(TEL, true, new_s);
                                ss.AddEdge(e);
                                //Handle closes in the select thread:
                                CloseAction ca = new CloseAction(e, TEL.ActionQueue);
                                e.CloseEvent += ca.CloseHandler;
                                TEL.SendEdgeEvent(e);
                            }
                        }
                        catch (Exception) {
                            //Looks like this Accept has failed.  Do nothing
                            //Console.Error.WriteLine("New incoming edge ({0}) failed: {1}", new_s, sx);
                            //Make sure the edge is closed
                            if (e != null)
                            {
                                TEL.RequestClose(e);
                                //Go ahead and forget about this socket.
                                CloseAction ca = new CloseAction(e, null);
                                ca.Start(ss);
                            }
                            else if (new_s != null)
                            {
                                //This should not be able to throw an exception:
                                new_s.Close();
                            }
                        }
                    }
                    else
                    {
                        ReceiveState rs = ss.GetReceiveState(s);
                        if (rs != null && !rs.Receive())
                        {
                            TEL.RequestClose(rs.Edge);
                            //Go ahead and forget about this socket.
                            CloseAction ca = new CloseAction(rs.Edge, null);
                            ca.Start(ss);
                        }
                    }
                }
            }
コード例 #12
0
        /**
         * This reads a packet from buf which came from end, with
         * the given ids
         */
        protected void HandleDataPacket(int remoteid, int localid,
                                        MemBlock packet, EndPoint end, object state)
        {
            bool read_packet = true;
            bool is_new_edge = false;
            //It is threadsafe to read from Hashtable
            UdpEdge edge = (UdpEdge)_id_ht[localid];

            if (localid == 0)
            {
                //This is a potentially a new incoming edge
                is_new_edge = true;

                //Check to see if it is a dup:
                UdpEdge e_dup = (UdpEdge)_remote_id_ht[remoteid];
                if (e_dup != null)
                {
                    //Lets check to see if this is a true dup:
                    if (e_dup.End.Equals(end))
                    {
                        //Same id from the same endpoint, looks like a dup...
                        is_new_edge = false;
                        //Reuse the existing edge:
                        edge = e_dup;
                    }
                    else
                    {
                        //This is just a coincidence.
                    }
                }
                if (is_new_edge)
                {
                    TransportAddress rta = TransportAddressFactory.CreateInstance(this.TAType, (IPEndPoint)end);
                    if (_ta_auth.Authorize(rta) == TAAuthorizer.Decision.Deny)
                    {
                        //This is bad news... Ignore it...
                        ///@todo perhaps we should send a control message... I don't know
                        is_new_edge = false;
                        read_packet = false;
                        if (ProtocolLog.UdpEdge.Enabled)
                        {
                            ProtocolLog.Write(ProtocolLog.UdpEdge, String.Format(
                                                  "Denying: {0}", rta));
                        }
                    }
                    else
                    {
                        //We need to assign it a local ID:
                        lock ( _id_ht ) {
                            /*
                             * Now we need to lock the table so that it cannot
                             * be written to by anyone else while we work
                             */
                            do
                            {
                                localid = _rand.Next();
                                //Make sure not to use negative ids
                                if (localid < 0)
                                {
                                    localid = ~localid;
                                }
                            } while(_id_ht.Contains(localid) || localid == 0);

                            /*
                             * We copy the endpoint because (I think) .Net
                             * overwrites it each time.  Since making new
                             * edges is rare, this is better than allocating
                             * a new endpoint each time
                             */
                            IPEndPoint this_end = (IPEndPoint)end;
                            IPEndPoint my_end   = new IPEndPoint(this_end.Address,
                                                                 this_end.Port);
                            edge = new UdpEdge(_send_handler, true, my_end,
                                               _local_ep, localid, remoteid);
                            _id_ht[localid]         = edge;
                            _remote_id_ht[remoteid] = edge;
                        }
                    }
                }
            }
            else if (edge == null)
            {
                /*
                 * This is the case where the Edge is not a new edge,
                 * but we don't know about it.  It is probably an old edge
                 * that we have closed.  We can ignore this packet
                 */
                read_packet = false;
                //Send a control packet
                SendControlPacket(end, remoteid, localid, ControlCode.EdgeClosed, state);
            }
            else if (edge.RemoteID == 0)
            {
                /* This is the response to our edge creation */
                edge.RemoteID = remoteid;
            }
            else if (edge.RemoteID != remoteid)
            {
                /*
                 * This could happen as a result of packet loss or duplication
                 * on the first packet.  We should ignore any packet that
                 * does not have both ids matching.
                 */
                read_packet = false;
                //Tell the other guy to close this ignored edge
                SendControlPacket(end, remoteid, localid, ControlCode.EdgeClosed, state);
                edge = null;
            }
            if ((edge != null) && !edge.End.Equals(end))
            {
                //This happens when a NAT mapping changes
                if (ProtocolLog.UdpEdge.Enabled)
                {
                    ProtocolLog.Write(ProtocolLog.UdpEdge, String.Format(
                                          "Remote NAT Mapping changed on Edge: {0}\n{1} -> {2}",
                                          edge, edge.End, end));
                }
                //Actually update:
                TransportAddress rta = TransportAddressFactory.CreateInstance(this.TAType, (IPEndPoint)end);
                if (_ta_auth.Authorize(rta) != TAAuthorizer.Decision.Deny)
                {
                    IPEndPoint this_end = (IPEndPoint)end;
                    edge.End = new IPEndPoint(this_end.Address, this_end.Port);
                    NatDataPoint dp = new RemoteMappingChangePoint(DateTime.UtcNow, edge);
                    Interlocked.Exchange <NatHistory>(ref _nat_hist, _nat_hist + dp);
                    Interlocked.Exchange <IEnumerable>(ref _nat_tas, new NatTAs(_tas, _nat_hist));
                    //Tell the other guy:
                    SendControlPacket(end, remoteid, localid, ControlCode.EdgeDataAnnounce, state);
                }
                else
                {
                    /*
                     * Looks like the new TA is no longer authorized.
                     */
                    SendControlPacket(end, remoteid, localid, ControlCode.EdgeClosed, state);
                    RequestClose(edge);
                    CloseHandler(edge, null);
                }
            }
            if (is_new_edge)
            {
                try {
                    NatDataPoint dp = new NewEdgePoint(DateTime.UtcNow, edge);
                    Interlocked.Exchange <NatHistory>(ref _nat_hist, _nat_hist + dp);
                    Interlocked.Exchange <IEnumerable>(ref _nat_tas, new NatTAs(_tas, _nat_hist));
                    edge.CloseEvent += this.CloseHandler;
                    //If we make it here, the edge wasn't closed, go ahead and process it.
                    SendEdgeEvent(edge);
                    // Stun support
                    SendControlPacket(end, remoteid, localid, ControlCode.EdgeDataAnnounce, state);
                }
                catch {
                    //Make sure this edge is closed and we are done with it.
                    RequestClose(edge);
                    CloseHandler(edge, null);
                    read_packet = false;
                    //This was a new edge, so the other node has our id as zero, send
                    //with that localid:
                    SendControlPacket(end, remoteid, 0, ControlCode.EdgeClosed, state);
                }
            }
            if (read_packet)
            {
                //We have the edge, now tell the edge to announce the packet:
                try {
                    edge.ReceivedPacketEvent(packet);
                }
                catch (EdgeClosedException) {
                    SendControlPacket(end, remoteid, localid, ControlCode.EdgeClosed, state);
                    //Make sure we record that this edge has been closed
                    CloseHandler(edge, null);
                }
            }
        }
コード例 #13
0
        /**
         * This handles lightweight control messages that may be sent
         * by UDP
         */
        protected void HandleControlPacket(int remoteid, int n_localid,
                                           MemBlock buffer, object state)
        {
            int     local_id = ~n_localid;
            UdpEdge e        = _id_ht[local_id] as UdpEdge;

            if (e == null)
            {
                return;
            }

            if (e.RemoteID == 0)
            {
                try {
                    e.RemoteID = remoteid;
                } catch {
                    return;
                }
            }

            if (e.RemoteID != remoteid)
            {
                return;
            }

            try {
                ControlCode code = (ControlCode)NumberSerializer.ReadInt(buffer, 0);
                if (ProtocolLog.UdpEdge.Enabled)
                {
                    ProtocolLog.Write(ProtocolLog.UdpEdge, String.Format(
                                          "Got control {1} from: {0}", e, code));
                }
                if (code == ControlCode.EdgeClosed)
                {
                    //The edge has been closed on the other side
                    RequestClose(e);
                    CloseHandler(e, null);
                }
                else if (code == ControlCode.EdgeDataAnnounce)
                {
                    //our NAT mapping may have changed:
                    IDictionary info =
                        (IDictionary)AdrConverter.Deserialize(buffer.Slice(4));
                    string our_local_ta = (string)info["RemoteTA"]; //his remote is our local
                    if (our_local_ta != null)
                    {
                        //Update our list:
                        TransportAddress new_ta = TransportAddressFactory.CreateInstance(our_local_ta);
                        TransportAddress old_ta = e.PeerViewOfLocalTA;
                        if (!new_ta.Equals(old_ta))
                        {
                            if (ProtocolLog.UdpEdge.Enabled)
                            {
                                ProtocolLog.Write(ProtocolLog.UdpEdge, String.Format(
                                                      "Local NAT Mapping changed on Edge: {0}\n{1} => {2}",
                                                      e, old_ta, new_ta));
                            }
                            //Looks like matters have changed:
                            this.UpdateLocalTAs(e, new_ta);

                            /**
                             * @todo, maybe we should ping the other edges sharing this
                             * EndPoint, but we need to be careful not to do some O(E^2)
                             * operation, which could easily happen if each EdgeDataAnnounce
                             * triggered E packets to be sent
                             */
                        }
                    }
                }
                else if (code == ControlCode.Null)
                {
                    //Do nothing in this case
                }
            }
            catch (Exception x) {
                //This could happen if this is some control message we don't understand
                if (ProtocolLog.Exceptions.Enabled)
                {
                    ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString());
                }
            }
        }
コード例 #14
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");
        }
コード例 #15
0
        /*
         * Given an IEnumerable of NatDataPoints, return a list of
         * ports from most likely to least likely to be the
         * next port used by the NAT
         *
         * @return an empty list if this is not our type
         */
        protected ArrayList PredictPorts(IEnumerable ndps)
        {
            ArrayList all_diffs = new ArrayList();
            //Get an increasing subset of the ports:
            int  prev             = Int32.MinValue;
            int  most_recent_port = -1;
            uint sum            = 0;
            uint sum2           = 0;
            bool got_extra_data = false;

            TransportAddress.TAType t = TransportAddress.TAType.Unknown;
            string host = String.Empty;

            foreach (NatDataPoint ndp in ndps)
            {
                if (false == (ndp is EdgeClosePoint))
                {
                    //Ignore closing events for prediction, they'll screw up the port prediction
                    TransportAddress ta = ndp.PeerViewOfLocalTA;
                    if (ta != null)
                    {
                        int port = ((IPTransportAddress)ta).Port;
//          Console.Error.WriteLine("port: {0}", port);
                        if (!got_extra_data)
                        {
                            t                = ta.TransportAddressType;
                            host             = ((IPTransportAddress)ta).Host;
                            most_recent_port = port;
                            got_extra_data   = true;
                        }
                        if (prev > port)
                        {
                            uint diff = (uint)(prev - port); //Clearly diff is always non-neg
                            all_diffs.Add(diff);
                            sum  += diff;
                            sum2 += diff * diff;
                        }
                        prev = port;
                    }
                }
            }

            /**
             * Now look at the mean and variance of the diffs
             */
            ArrayList prediction = new ArrayList();

            if (all_diffs.Count > 1)
            {
                double n    = (double)all_diffs.Count;
                double sd   = (double)sum;
                double mean = sd / n;
                double s2   = ((double)sum2) - sd * sd / n;
                s2 = s2 / (double)(all_diffs.Count - 1);
                double stddev = Math.Sqrt(s2);
                //Console.Error.WriteLine("stddev: {0}", stddev);
                if (stddev < MAX_STD_DEV)
                {
                    try {
                        double max_delta = mean + SAFETY * stddev;
                        if (max_delta < mean + 0.001)
                        {
                            //This means the stddev is very small, just go up one above the
                            //mean:
                            max_delta = mean + 1.001;
                        }
                        int delta = (int)(mean - SAFETY * stddev);
                        while (delta < max_delta)
                        {
                            if (delta > 0)
                            {
                                int pred_port = most_recent_port + delta;
                                prediction.Add(TransportAddressFactory.CreateInstance(t, host, pred_port));
                            }
                            else
                            {
                                //Increment the max by one just to keep a constant width:
                                max_delta += 1.001; //Giving a little extra to make sure we get 1
                            }
                            delta++;
                        }
                    }
                    catch {
                        //Just ignore any bad transport addresses.
                    }
                }
                else
                {
                    //The standard deviation is too wide to make a meaningful prediction
                }
            }
            return(prediction);
        }
コード例 #16
0
ファイル: TransportAddress.cs プロジェクト: pcbing/brunet
        public void Test()
        {
            TransportAddress tas = TransportAddressFactory.CreateInstance("b.s://234580");

            Assert.AreEqual(tas.ToString(), "b.s://234580", "Simulation string");
            Assert.AreEqual((tas as SimulationTransportAddress).ID, 234580, "Simulation id");
            Assert.AreEqual(TransportAddress.TAType.S, tas.TransportAddressType, "Simulation ta type");
            TransportAddress ta1 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000");

            Assert.AreEqual(ta1.ToString(), "brunet.udp://10.5.144.69:5000", "Testing TA parsing");

            TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000");

            Assert.AreEqual(ta1, ta2, "Testing TA Equals");

            string           ta_string = "brunet.tunnel://UBU72YLHU5C3SY7JMYMJRTKK4D5BGW22/FE4QWASN+FE4QWASM";
            TransportAddress ta        = TransportAddressFactory.CreateInstance("brunet.tunnel://UBU72YLHU5C3SY7JMYMJRTKK4D5BGW22/FE4QWASN+FE4QWASM");

            Assert.AreEqual(ta.ToString(), ta_string, "testing tunnel TA parsing");
            //Console.WriteLine(ta);

            TunnelTransportAddress tun_ta = (TunnelTransportAddress)TransportAddressFactory.CreateInstance("brunet.tunnel://OIHZCNNUAXTLLARQIOBNCUWXYNAS62LO/CADSL6GV+CADSL6GU");

            ArrayList fwd = new ArrayList();

            fwd.Add(new AHAddress(Base32.Decode("CADSL6GVVBM6V442CETP4JTEAWACLC5A")));
            fwd.Add(new AHAddress(Base32.Decode("CADSL6GUVBM6V442CETP4JTEAWACLC5A")));

            TunnelTransportAddress test_ta = new TunnelTransportAddress(tun_ta.Target, fwd);

            Assert.AreEqual(tun_ta, test_ta, "testing tunnel TA compression enhancements");
            //Console.WriteLine(tun_ta.ToString());
            //Console.WriteLine(test_ta.ToString());
            Assert.AreEqual(tun_ta.ToString(), test_ta.ToString(), "testing tunnel TA compression enhancements (toString)");

            Assert.AreEqual(tun_ta.ContainsForwarder(new AHAddress(Base32.Decode("CADSL6GVVBM6V442CETP4JTEAWACLC5A"))), true,
                            "testing tunnel TA contains forwarder (1)");

            Assert.AreEqual(tun_ta.ContainsForwarder(new AHAddress(Base32.Decode("CADSL6GUVBM6V442CETP4JTEAWACLC5A"))), true,
                            "testing tunnel TA contains forwarder (2)");



            string           StrLocalHost = Dns.GetHostName();
            IPHostEntry      IPEntry      = Dns.GetHostEntry(StrLocalHost);
            TransportAddress local_ta     = TransportAddressFactory.CreateInstance("brunet.udp://" + IPEntry.AddressList[0].ToString() +
                                                                                   ":" + 5000);
            IEnumerable locals = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Udp, 5000);

            bool match = false;

            foreach (TransportAddress test_ta1 in locals)
            {
                //Console.WriteLine("test_ta: {0}", test_ta1);
                if (test_ta1.Equals(local_ta))
                {
                    match = true;
                }
            }
            Assert.AreEqual(match, true, "testing local TA matches");
            //testing function TA
            TransportAddress func_ta  = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000");
            TransportAddress func_ta2 = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000");

            Assert.AreEqual(func_ta, func_ta2, "equality of instances");
            Assert.IsTrue(func_ta == func_ta2, "reference equality, test of caching");
            Assert.AreEqual(func_ta.ToString(), "brunet.function://localhost:3000", "Testing function TA parsing");
        }