예제 #1
0
        internal void SendAnnouncePeerQuery(DHTNode remoteNode, DHTId infoHash, byte implied_port, int port, BString token)
        {
            if (remoteNode == null || token == null || token.Length == 0)
            {
                return;
            }

            long nowTicks = DateTime.UtcNow.Ticks;

            if (nowTicks - remoteNode.LastAnnouncementTime < AnnounceLife.Ticks)
            {
                return;
            }
            remoteNode.LastAnnouncementTime = nowTicks;

#if DEBUG_DHT_INTERNALS
            fLogger.WriteDebug("Send announce peer query {0}, {1}, {2}", remoteNode.EndPoint, implied_port, port);
#endif

            // https://www.bittorrent.org/beps/bep_0005.html
            // If implied_port (0/1) is present and non-zero, the port argument should be ignored
            // and the source port of the UDP packet should be used as the peer's port instead.

            var transactionID = DHTTransactions.GetNextId();
            var msg           = DHTMessage.CreateAnnouncePeerQuery(transactionID, fLocalID, infoHash, implied_port, port, token);
            fTransactions.SetQuery(transactionID, msg);
            Send(remoteNode.EndPoint, msg);
        }
예제 #2
0
        public void Test_GetTransactionId()
        {
            var tid = DHTTransactions.GetNextId();

            Assert.IsNotNull(tid);
            Assert.AreEqual(2, tid.Length);
        }
예제 #3
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();
 }
예제 #4
0
        public void Test_CreateAnnouncePeerResponse()
        {
            var        tid    = DHTTransactions.GetNextId();
            var        nodeId = DHTId.CreateRandom();
            DHTMessage msg    = DHTMessage.CreateAnnouncePeerResponse(tid, nodeId, new List <DHTNode>());

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            // TODO: test contents
        }
예제 #5
0
        internal void SendFindNodeQuery(IPEndPoint address, DHTId target, bool neighbor = true)
        {
            var   transactionID = DHTTransactions.GetNextId();
            DHTId targetId      = (target == null) ? fLocalID : ((neighbor) ? DHTNode.GetNeighbor(target.Data, fLocalID.Data) : target);

            var msg = DHTMessage.CreateFindNodeQuery(transactionID, fLocalID, targetId);

            fTransactions.SetQuery(transactionID, msg);
            Send(address, msg);
        }
예제 #6
0
        public void Test_CreateFindNodeQuery()
        {
            var        tid    = DHTTransactions.GetNextId();
            var        nodeId = DHTId.CreateRandom();
            DHTMessage msg    = DHTMessage.CreateFindNodeQuery(tid, nodeId, nodeId);

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(QueryType.FindNode, msg.QueryType);
            // TODO: test contents
        }
예제 #7
0
        internal void SendGetPeersQuery(IPEndPoint address, DHTId infoHash)
        {
#if DEBUG_DHT_INTERNALS
            fLogger.WriteDebug("Send get peers query {0}", address);
#endif

            var transactionID = DHTTransactions.GetNextId();
            var msg           = DHTMessage.CreateGetPeersQuery(transactionID, fLocalID, infoHash);
            fTransactions.SetQuery(transactionID, msg);
            Send(address, msg);
        }
예제 #8
0
        internal void SendPingQuery(IPEndPoint address, bool async = true)
        {
#if DEBUG_DHT_INTERNALS
            fLogger.WriteDebug("Send peer ping {0}", address);
#endif

            var transactionID = DHTTransactions.GetNextId();
            var msg           = DHTMessage.CreatePingQuery(transactionID, fLocalID);
            fTransactions.SetQuery(transactionID, msg);
            Send(address, msg, async);
        }
예제 #9
0
        public void Test_CreateAnnouncePeerQuery()
        {
            var        tid      = DHTTransactions.GetNextId();
            var        nodeId   = DHTId.CreateRandom();
            var        infoHash = DHTId.CreateRandom();
            DHTMessage msg      = DHTMessage.CreateAnnouncePeerQuery(tid, nodeId, infoHash, 1, 1, new BString("XX"));

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(QueryType.AnnouncePeer, msg.QueryType);
            // TODO: test contents
        }
예제 #10
0
        public void Test_DHTTransactions_class()
        {
            var instance = new DHTTransactions();

            Assert.IsNotNull(instance);

            var tid = DHTTransactions.GetNextId();
            var msg = new DHTMessage(MessageType.Query, QueryType.Ping, null);

            instance.SetQuery(tid, msg);
            Assert.AreEqual(QueryType.Ping, instance.CheckQuery(tid));
        }
예제 #11
0
        public void Test_CreateGetPeersResponse()
        {
            var        tid      = DHTTransactions.GetNextId();
            var        nodeId   = DHTId.CreateRandom();
            var        infoHash = DHTId.CreateRandom();
            var        peers    = new List <IDHTPeer>();
            var        nodes    = new List <DHTNode>();
            DHTMessage msg      = DHTMessage.CreateGetPeersResponse(tid, nodeId, infoHash, peers, nodes);

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            // TODO: test contents
        }