Ejemplo n.º 1
0
 public DHTClient(IPEndPoint localEndPoint, IDHTPeersHolder peersHolder, string clientVer) : base(localEndPoint)
 {
     fPeersHolder  = peersHolder;
     fLocalID      = peersHolder.ClientNodeId;
     fClientVer    = clientVer;
     fLogger       = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "DHTClient");
     fRoutingTable = new DHTRoutingTable(KTableSize);
     fTransactions = new DHTTransactions();
 }
Ejemplo n.º 2
0
        public void Test_UpdateNode()
        {
            var table = new DHTRoutingTable(10);

            table.UpdateNode(null);

            var node = new DHTNode(DHTHelper.GetRandomID(), new IPEndPoint(IPAddress.Any, 0));

            table.UpdateNode(node);
        }
        public void Test_UpdateNode_DDoS_Protection()
        {
            var table = new DHTRoutingTable(10);

            // do not use nodes with ports below 1024
            var node   = new DHTNode(DHTId.CreateRandom(), new IPEndPoint(IPAddress.Any, 111));
            var result = table.UpdateNode(node);

            Assert.IsFalse(result);
        }
        public void Test_ctor()
        {
            var table = new DHTRoutingTable(10);

            Assert.IsNotNull(table);

            Assert.AreEqual(0, table.Count);

            table.Clear();

            var randId = DHTId.CreateRandom();
            var nodes  = table.GetClosest(randId.Data);
        }
Ejemplo n.º 5
0
        public void Test_ctor()
        {
            var table = new DHTRoutingTable(10);

            Assert.IsNotNull(table);

            Assert.AreEqual(0, table.Count);
            Assert.AreEqual(false, table.IsFull);

            table.Clear();

            var randId = DHTHelper.GetRandomID();
            var nodes  = table.FindNodes(randId);
        }
Ejemplo n.º 6
0
        public void Test_FindNodes()
        {
            var table = new DHTRoutingTable(10);

            var randId = DHTHelper.GetRandomID();
            var nodes  = table.FindNodes(randId);

            Assert.IsNotNull(nodes);
            Assert.AreEqual(0, nodes.Count);

            var node = new DHTNode(randId, new IPEndPoint(IPAddress.Any, 0));

            table.UpdateNode(node);
            nodes = table.FindNodes(randId);
            Assert.IsNotNull(nodes);
            Assert.AreEqual(1, nodes.Count);
        }
Ejemplo n.º 7
0
        public void Test_FindNode()
        {
            var table = new DHTRoutingTable(10);

            var randId       = DHTHelper.GetRandomID();
            var nodeEndPoint = new IPEndPoint(IPAddress.Any, 0);
            var node         = new DHTNode(randId, nodeEndPoint);

            table.UpdateNode(node);

            var node2 = table.FindNode(nodeEndPoint);

            Assert.IsNotNull(node2);

            node2 = table.FindNode(null);
            Assert.IsNull(node2);
        }
        public void Test_FindNodes()
        {
            var table = new DHTRoutingTable(10);

            var randId = DHTId.CreateRandom();
            var nodes  = table.GetClosest(randId.Data);

            Assert.IsNotNull(nodes);
            Assert.AreEqual(0, nodes.Count);

            var node = new DHTNode(randId, new IPEndPoint(IPAddress.Any, 6881));

            table.UpdateNode(node);
            nodes = table.GetClosest(randId.Data);
            Assert.IsNotNull(nodes);
            Assert.AreEqual(1, nodes.Count);
        }
Ejemplo n.º 9
0
        public DHTClient(IPAddress addr, int port, IDHTPeersHolder peersHolder, string clientVer)
        {
            fBuffer       = new byte[65535];
            fClientVer    = clientVer;
            fDefaultIP    = new IPEndPoint(IPLoopbackAddress, 0);
            fLocalID      = DHTHelper.GetRandomID();
            fLocalIP      = new IPEndPoint(addr, port);
            fLogger       = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "DHTClient");
            fPeersHolder  = peersHolder;
            fRoutingTable = new DHTRoutingTable(KTableSize);
            fSearchRunned = false;
            fTransactions = new Dictionary <int, DHTMessage>();

            fSocket = new Socket(IPAddressFamily, SocketType.Dgram, ProtocolType.Udp);

            // FIXME: unsupported?
            #if !__MonoCS__
            fSocket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
            #if !IP6
            const long IOC_IN            = 0x80000000;
            const long IOC_VENDOR        = 0x18000000;
            const long SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
            byte[]     optionInValue     = { Convert.ToByte(false) };
            byte[]     optionOutValue    = new byte[4];
            fSocket.IOControl((IOControlCode)SIO_UDP_CONNRESET, optionInValue, optionOutValue);
            #else
            #endif
            #endif

            #if !IP6
            fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            fSocket.Ttl = 255;
            #else
            fSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
            #endif

            fSocket.Bind(fLocalIP);
        }
Ejemplo n.º 10
0
        public void Test_ComputeRouteDistance()
        {
            var bytes1  = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
            var bytes2  = new byte[] { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77 };
            var result1 = DHTRoutingTable.ComputeRouteDistance(bytes1, bytes2);

            Assert.AreEqual("6666666666666666666666666666666666666666", result1.ToString());

            bytes1 = new byte[] { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
            bytes2 = new byte[] { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99 };
            var result2 = DHTRoutingTable.ComputeRouteDistance(bytes1, bytes2);

            Assert.AreEqual("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", result2.ToString());

            // result1 is less than result2, result1 is first

            var list = new SortedList <DHTId, DHTNode>(8);

            list.Add(result2, null);
            list.Add(result1, null);

            Assert.AreEqual(result1, list.Keys[0]);
            Assert.AreEqual(result2, list.Keys[1]);
        }