Пример #1
0
        public void Test_ctor()
        {
            var msg = new DHTMessage(MessageType.Query, QueryType.Ping, null);

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
        }
Пример #2
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);
        }
Пример #3
0
        private void OnRecvMessage(IPEndPoint ipinfo, byte[] data)
        {
            try {
                DHTMessage msg = DHTMessage.ParseBuffer(data);
                if (msg == null)
                {
                    return;
                }

                if (msg.IsSimilarTo(fClientVer))
                {
                    fLogger.WriteDebug(">>>> Received a message from a similar client from {0}", ipinfo);
                }

                switch (msg.Type)
                {
                case MessageType.Response:
                    OnRecvResponseX(ipinfo, msg as DHTResponseMessage);
                    break;

                case MessageType.Query:
                    OnRecvQueryX(ipinfo, msg as DHTQueryMessage);
                    break;

                case MessageType.Error:
                    OnRecvErrorX(ipinfo, msg as DHTErrorMessage);
                    break;
                }
            } catch (Exception ex) {
                fLogger.WriteError("DHTClient.OnRecvMessage(): ", ex);
            }
        }
Пример #4
0
        private void OnRecvAnnouncePeerQuery(IPEndPoint ipinfo, BDictionary data)
        {
            var t    = data.Get <BString>("t");
            var args = data.Get <BDictionary>("a");

            var id          = args.Get <BString>("id");
            var infoHash    = args.Get <BString>("info_hash");
            var impliedPort = args.Get <BNumber>("implied_port");
            int port        = (impliedPort != null && impliedPort.Value == 1) ? ipinfo.Port : (int)args.Get <BNumber>("port").Value;

            fLogger.WriteDebug("Receive `announce_peer` query from {0} [{1}] for {2}", ipinfo.ToString(), id.Value.ToHexString(), infoHash.Value.ToHexString());

            fRoutingTable.UpdateNode(new DHTNode(id.Value, ipinfo));

            if (!Algorithms.ArraysEqual(infoHash.Value, fSearchInfoHash))
            {
                // skip response for another infohash query
                return;
            }

            // receive `announce_peer` query for our infohash
            var nodesList = fRoutingTable.FindNodes(infoHash.Value);

            Send(ipinfo, DHTMessage.CreateAnnouncePeerResponse(t, fLocalID, nodesList));
        }
Пример #5
0
        private void OnRecvAnnouncePeerQuery(IPEndPoint ipinfo, BDictionary data)
        {
            var t    = data.Get <BString>("t");
            var args = data.Get <BDictionary>("a");

            var id          = args.Get <BString>("id");
            var infoHash    = new DHTId(args.Get <BString>("info_hash"));
            var impliedPort = args.Get <BNumber>("implied_port");
            int port        = (impliedPort != null && impliedPort.Value == 1) ? ipinfo.Port : (int)args.Get <BNumber>("port").Value;

            var remoteNode = new DHTNode(id.Value, ipinfo);

            UpdateRoutingTable(remoteNode);

#if DEBUG_DHT_INTERNALS
            fLogger.WriteDebug("Receive `announce_peer` query from {0} for {1}", remoteNode.ToString(), infoHash.ToString());
#endif

            // skip response for another infohash query
            if (infoHash == fSearchInfoHash)
            {
                // receive `announce_peer` query for our infohash
                var nodesList = fRoutingTable.GetClosest(infoHash.Data);
                Send(ipinfo, DHTMessage.CreateAnnouncePeerResponse(t, fLocalID, nodesList));

                if (PeersFound != null)
                {
                    PeersFound(this, new PeersFoundEventArgs(new List <IPEndPoint>()
                    {
                        ipinfo
                    }));
                }
            }
        }
Пример #6
0
        protected override void OnRecvMessage(IPEndPoint ipinfo, byte[] data)
        {
            try {
                DHTMessage msg = DHTMessage.ParseBuffer(data);
                if (msg == null)
                {
                    return;
                }

                if (msg.IsSimilarTo(fClientVer))
                {
                    // Received a message from a similar client
                    // many false positives, client version is not unique
                }

                switch (msg.Type)
                {
                case MessageType.Response:
                    OnRecvResponseX(ipinfo, msg as DHTResponseMessage);
                    break;

                case MessageType.Query:
                    OnRecvQueryX(ipinfo, msg as DHTQueryMessage);
                    break;

                case MessageType.Error:
                    OnRecvErrorX(ipinfo, msg as DHTErrorMessage);
                    break;
                }
            } catch (Exception ex) {
                fLogger.WriteError("OnRecvMessage()", ex);
            }
        }
Пример #7
0
        internal void SendGetPeersQuery(IPEndPoint address, byte[] infoHash)
        {
            var transactionID = DHTHelper.GetTransactionId();
            var msg           = DHTMessage.CreateGetPeersQuery(transactionID, fLocalID, infoHash);

            SetTransaction(transactionID, msg);
            Send(address, msg);
        }
Пример #8
0
        public void Test_GetMessageType()
        {
            Assert.AreEqual(MessageType.Response, DHTMessage.GetMessageType("r"));
            Assert.AreEqual(MessageType.Query, DHTMessage.GetMessageType("q"));
            Assert.AreEqual(MessageType.Error, DHTMessage.GetMessageType("e"));

            Assert.AreEqual(MessageType.Unknown, DHTMessage.GetMessageType("xyz"));
            Assert.AreEqual(MessageType.Unknown, DHTMessage.GetMessageType(null));
        }
Пример #9
0
        public void Test_ParseBuffer_GetPeersResponse2()
        {
            var msg = DHTMessage.ParseBuffer("d1:rd2:id20:abcdefghij01234567895:nodes9:def456...5:token8:aoeusnthe1:ti0e1:y1:re");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            // TODO: test contents
        }
Пример #10
0
        public void Test_ParseBuffer_PingResponse()
        {
            var msg = DHTMessage.ParseBuffer("d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            // TODO: test contents
        }
Пример #11
0
        public void Test_ParseBuffer_FindNodeResponse()
        {
            var msg = DHTMessage.ParseBuffer("d1:rd2:id20:0123456789abcdefghij5:nodes9:def456...e1:t2:aa1:y1:re");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            // TODO: test contents
        }
Пример #12
0
        public void Test_ParseBuffer_FindNodeQuery()
        {
            var msg = DHTMessage.ParseBuffer("d1:ad2:id20:abcdefghij01234567896:target20:mnopqrstuvwxyz123456e1:q9:find_node1:t2:aa1:y1:qe");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            Assert.AreEqual(QueryType.FindNode, msg.QueryType);
            // TODO: test contents
        }
Пример #13
0
        public void Test_ParseBuffer_PingQuery()
        {
            var msg = DHTMessage.ParseBuffer("d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            Assert.AreEqual(QueryType.Ping, msg.QueryType);
            // TODO: test contents
        }
Пример #14
0
        public void Test_GetQueryType()
        {
            Assert.AreEqual(QueryType.Ping, DHTMessage.GetQueryType("ping"));
            Assert.AreEqual(QueryType.FindNode, DHTMessage.GetQueryType("find_node"));
            Assert.AreEqual(QueryType.GetPeers, DHTMessage.GetQueryType("get_peers"));
            Assert.AreEqual(QueryType.AnnouncePeer, DHTMessage.GetQueryType("announce_peer"));

            Assert.AreEqual(QueryType.None, DHTMessage.GetQueryType("xyz"));
            Assert.AreEqual(QueryType.None, DHTMessage.GetQueryType(null));
        }
Пример #15
0
        public void Test_ParseBuffer_GetPeersQuery()
        {
            var msg = DHTMessage.ParseBuffer("d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz123456e1:q9:get_peers1:t2:aa1:y1:qe");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            Assert.AreEqual(QueryType.GetPeers, msg.QueryType);
            // TODO: test contents
        }
Пример #16
0
        public void Test_CreateFindNodeResponse()
        {
            var        tid    = DHTHelper.GetTransactionId();
            var        nodeId = DHTHelper.GetRandomID();
            DHTMessage msg    = DHTMessage.CreateFindNodeResponse(tid, nodeId, new List <DHTNode>());

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Response, msg.Type);
            // TODO: test contents
        }
Пример #17
0
        internal void SendPingQuery(IPEndPoint address)
        {
            fLogger.WriteDebug("Send peer ping {0}", address);

            var transactionID = DHTHelper.GetTransactionId();
            var msg           = DHTMessage.CreatePingQuery(transactionID, fLocalID);

            SetTransaction(transactionID, msg);
            Send(address, msg);
        }
Пример #18
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);
        }
Пример #19
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
        }
Пример #20
0
        public void Test_ParseBuffer_AnnouncePeerQuery()
        {
            var msg = DHTMessage.ParseBuffer("d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz1234564:porti6881e5:token8:aoeusnthe1:q13:announce_peer1:t2:aa1:y1:qe");

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(string.Empty, msg.ClientVer);
            Assert.AreEqual(QueryType.AnnouncePeer, msg.QueryType);
            // TODO: test contents
        }
Пример #21
0
        public void Test_CreatePingQuery()
        {
            var        tid    = DHTHelper.GetTransactionId();
            var        nodeId = DHTHelper.GetRandomID();
            DHTMessage msg    = DHTMessage.CreatePingQuery(tid, nodeId);

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Query, msg.Type);
            Assert.AreEqual(QueryType.Ping, msg.QueryType);
            // TODO: test contents
        }
Пример #22
0
        internal void SendFindNodeQuery(IPEndPoint address, byte[] data)
        {
            var transactionID = DHTHelper.GetTransactionId();

            byte[] nid = (data == null) ? fLocalID : DHTHelper.GetNeighbor(data, fLocalID);

            var msg = DHTMessage.CreateFindNodeQuery(transactionID, nid);

            SetTransaction(transactionID, msg);
            Send(address, msg);
        }
Пример #23
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);
        }
Пример #24
0
        public void Test_ParseBuffer_Empty()
        {
            byte[] buffer = null;
            var    msg    = DHTMessage.ParseBuffer(buffer);

            Assert.IsNull(msg);

            buffer = new byte[] {};
            msg    = DHTMessage.ParseBuffer(buffer);
            Assert.IsNull(msg);
        }
Пример #25
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
        }
Пример #26
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);
        }
Пример #27
0
        private void SendGetPeersQuery(IPEndPoint address, byte[] infoHash)
        {
            var transactionID = DHTHelper.GetTransactionId();

            byte[] nid = fLocalID;

            BDictionary sendData = DHTMessage.CreateGetPeersQuery(transactionID, nid, infoHash);

            SetTransaction(transactionID, new DHTMessage(MsgType.query, QueryType.get_peers, sendData));
            Send(address, sendData);
        }
Пример #28
0
        private void SendFindNodeQuery(IPEndPoint address, byte[] data)
        {
            var transactionID = DHTHelper.GetTransactionId();

            byte[] nid = (data == null) ? fLocalID : DHTHelper.GetNeighbor(data, fLocalID);

            BDictionary sendData = DHTMessage.CreateFindNodeQuery(transactionID, nid);

            SetTransaction(transactionID, new DHTMessage(MsgType.query, QueryType.find_node, sendData));
            Send(address, sendData);
        }
Пример #29
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
        }
 public void SetQuery(BString transactionId, DHTMessage message)
 {
     try {
         if (transactionId != null && transactionId.Length == 2)
         {
             int tid = BitConverter.ToUInt16(transactionId.Value, 0);
             fTransactions[tid] = message;
         }
     } catch (Exception ex) {
         fLogger.WriteError("SetQuery()", ex);
     }
 }