Exemplo n.º 1
0
        public void ExtractInfoHash()
        {
            Bencoder   bc  = new Bencoder();
            MnlMessage msg = new MnlMessage
                             .Builder(ParseTest.ConvertHexStringToByteArray("64313a6164323a696432303a7200ee612a3d17f22d721a8019317eb27c7039c9393a696e666f5f6861736832303af6ef92f7cfa792c050fef70c405ded104771553d363a6e6f7365656469316565313a71393a6765745f7065657273313a74343a9b9427a4313a76343a5554ab14313a79313a7165"))
                             .Build();

            MnlMessage myMsg = new MnlMessage
                               .Builder(ParseTest.ConvertHexStringToByteArray("64313a74323a3032313a79313a71313a71393a6765745f7065657273313a6164323a696432303a09dc0cea8478d7f285ef674c6a2f931ae3b603f4393a696e666f5f6861736832303a3f4f40163040223f3f063f3a562f603e3f543f3f6565"))
                               .Build();



            Dictionary <string, object> decoded   = (Dictionary <string, object>)bc.DecodeElement(ParseTest.ConvertHexStringToByteArray("64313a6164323a696432303ab2dfb21aa9b31c00cdca99bcd9232a7facbc76e2393a696e666f5f6861736832303aa44f7b0766a4e53ebfc0cf79fa9c82778b88d94765313a71393a6765745f7065657273313a74343a67707588313a79313a7165"));
            Dictionary <string, object> decodedMy = (Dictionary <string, object>)bc.DecodeElement(ParseTest.ConvertHexStringToByteArray("64313a74323a3034313a79313a71313a71393a6765745f7065657273313a6164323a696432303ae36f5d8ef6928691d9696e02d025e3b8f1a08ca2393a696e666f5f6861736832303a139f69de76a07790ec3ad31cbc3bd5d9058716006565"));
            var infoHash = Encoding.UTF8.GetBytes((string)((Dictionary <string, object>)decoded["a"])["info_hash"]);

            var bytes = ParseTest.ConvertHexStringToByteArray("3F4F40163040223F3F063F3A562F603E3F543F3F");

            //CollectionAssert.AreEqual(infoHash, bytes);

            SoapHexBinary hex = new SoapHexBinary(infoHash);

            Debug.WriteLine($"Info hash: {hex.ToString()} ");
        }
Exemplo n.º 2
0
        private void ListenIncoming(Node ownerNode)
        {
            while (!_stopped)
            {
                try {
                    IPEndPoint incomingIpEndPoint = new IPEndPoint(IPAddress.Any, EndPoint.Port);

                    byte[] rawMsg = _udpClient.Receive(ref incomingIpEndPoint);
                    if (!IsLegitMnlDhtMessage(rawMsg))
                    {
                        continue;
                    }

                    Message.MnlMessage incomingMsg = null;
                    try {
                        incomingMsg = new Message.MnlMessage.Builder(rawMsg).Build();
                    } catch (Bittorrent.InvalidFieldException ife) {
                        MnlMessage errMessage = new MnlMessage.Builder()
                                                .SetTranId(GenerateTranId())
                                                .SetType(MessageType.Error)
                                                .SetErrorCode(ErrorType.ProtocolError)
                                                .SetErrorMessage(ife.Message)
                                                .Build();

                        Debug.WriteLine($"Sending incorrect field message {ife.Message}");
                        SendMessage(incomingIpEndPoint, errMessage);
                        continue;
                    } catch (MalformedPacketException mpe) {
                        MnlMessage errMessage = new MnlMessage.Builder()
                                                .SetTranId(GenerateTranId())
                                                .SetType(MessageType.Error)
                                                .SetErrorCode(ErrorType.ProtocolError)
                                                .SetErrorMessage(mpe.Message)
                                                .Build();

                        Debug.WriteLine($"Sending malformed packet message {mpe.Message}");
                        SendMessage(incomingIpEndPoint, errMessage);
                        continue;
                    }

                    ulong msgIdx = GetMessageId(incomingIpEndPoint.Address, BitConverter.ToUInt16(Encoding.ASCII.GetBytes(incomingMsg.TranId.PadLeft(2, '\0')), 0));
                    if (_messages.ContainsKey(msgIdx))
                    {
                        PendingMnlMessage rqMsg;
                        if (_messages.TryRemove(msgIdx, out rqMsg))
                        {
                            if (incomingMsg.Type == MessageType.Error)
                            {
                                Debug.WriteLine($"Recieved error {incomingMsg.ErrorMessage}");
                                switch (incomingMsg.ErrorCode)
                                {
                                case ErrorType.ProtocolError:
                                case ErrorType.MethodUnknown:
                                    Debug.WriteLine($"!!!Error {incomingMsg.ErrorMessage}");
                                    break;

                                default:
                                    continue;
                                }
                            }

                            if (rqMsg.ValidateId)
                            {
                                if (rqMsg.RecipientId != incomingMsg.OriginatorId)
                                {
                                    Debug.WriteLine($"Node {incomingIpEndPoint} id mismatch!");
                                    OnMaliciousMessage?.Invoke(incomingIpEndPoint, rqMsg.RecipientId, rqMsg.SentMessage);
                                    continue;
                                }
                            }

                            rqMsg.SentMessage.SetResponse(incomingMsg);
                        }
                    }
                    else
                    {
                        if (incomingMsg.Type == MessageType.Query)
                        {
                            ownerNode.ProcessQuery(incomingMsg, incomingIpEndPoint);
                        }
                    }
                } catch (SocketException se) {
                    if (se.SocketErrorCode != SocketError.Interrupted)
                    {
                        throw;
                    }
                }
            }
        }