예제 #1
0
        private void HandleAnnounceResponse(IPEndPoint endpoint, TrackerGetUdpEntry entry, byte[] data)
        {
            if (data.Length < 20)
            {
                context.CallPacketIgnored(endpoint, data.Length);
                return;
            }

            if (Bytes.Equals(TrackerGetUdpProtocol.Announce, data, 0, 4) == false)
            {
                context.CallPacketIgnored(endpoint, data.Length);
                return;
            }

            List <NetworkAddress> peers = new List <NetworkAddress>();

            int      intervalInSeconds = Bytes.ReadInt32(data, 8);
            TimeSpan interval          = TimeSpan.FromSeconds(intervalInSeconds);

            int leechers = Bytes.ReadInt32(data, 12);
            int seeders  = Bytes.ReadInt32(data, 16);

            for (int i = 20; i + 6 <= data.Length; i += 6)
            {
                int           port    = Bytes.ReadUInt16(data, i + 4);
                StringBuilder address = new StringBuilder();

                address.Append(data[i].ToString());
                address.Append('.');
                address.Append(data[i + 1].ToString());
                address.Append('.');
                address.Append(data[i + 2].ToString());
                address.Append('.');
                address.Append(data[i + 3].ToString());

                if (port > 0)
                {
                    peers.Add(new NetworkAddress(address.ToString(), port));
                }
            }

            collection.Remove(entry.Transaction);
            entry.Callback.Invoke(interval);
            context.CallAnnounced(entry.Address, entry.Request.Hash, interval, seeders, leechers, peers.ToArray());
        }
예제 #2
0
        private TcpSocketReceiveCallback OnReceived(TrackerGetHttpEntry entry)
        {
            return(received =>
            {
                if (received.Status == SocketStatus.OK)
                {
                    context.Queue.Add(() =>
                    {
                        byte[] data = Bytes.Copy(entry.Buffer.Data, entry.Buffer.Offset, received.Count);
                        string text = Encoding.ASCII.GetString(data);

                        context.CallPacketReceived(entry.Endpoint, data.Length);

                        if (text.StartsWith(TrackerGetHttpProtocol.ResponseHeader) == false)
                        {
                            context.CallPacketIgnored(entry.Endpoint, data.Length);
                            return;
                        }

                        int counter = 0, position = 0;
                        bool r = false, n = false;

                        for (int i = 0; i < data.Length; i++)
                        {
                            if (data[i] == '\r')
                            {
                                r = true;
                                counter++;
                                continue;
                            }

                            if (data[i] == '\n')
                            {
                                n = true;
                                counter++;
                                continue;
                            }

                            if (counter == 4 && r && n)
                            {
                                position = i;
                                break;
                            }

                            if (counter == 2 && !(r && n))
                            {
                                position = i;
                                break;
                            }

                            counter = 0;
                        }

                        if (position == 0)
                        {
                            context.CallPacketIgnored(entry.Endpoint, data.Length);
                            return;
                        }

                        BencodedValue decoded = Bencoder.Decode(data, position);
                        if (decoded.Dictionary == null)
                        {
                            context.CallPacketIgnored(entry.Endpoint, data.Length);
                            return;
                        }

                        string failure = decoded.Find("failure reason", x => x?.Text?.GetString());
                        if (failure != null)
                        {
                            context.CallFailed(entry.Address, entry.Request.Hash, failure);
                            return;
                        }

                        int?interval = decoded.Find("interval", x => x?.ToInt32());
                        BencodedValue peers = decoded.Find("peers", x => x);

                        if (interval != null && peers.Text != null && peers.Text.Length % 6 == 0)
                        {
                            List <NetworkAddress> result = new List <NetworkAddress>(peers.Text.Length / 6);
                            byte[] bytes = peers.Data.GetBytes();

                            for (int i = 0; i < bytes.Length; i += 6)
                            {
                                int port = Bytes.ReadUInt16(bytes, i + 4);
                                StringBuilder address = new StringBuilder();

                                address.Append(bytes[i].ToString());
                                address.Append('.');
                                address.Append(bytes[i + 1].ToString());
                                address.Append('.');
                                address.Append(bytes[i + 2].ToString());
                                address.Append('.');
                                address.Append(bytes[i + 3].ToString());

                                if (port > 0)
                                {
                                    result.Add(new NetworkAddress(address.ToString(), port));
                                }
                            }

                            collection.Remove(entry.Socket);
                            entry.Callback.Invoke(TimeSpan.FromSeconds(interval.Value));
                            context.CallAnnounced(entry.Address, entry.Request.Hash, TimeSpan.FromSeconds(interval.Value), result.ToArray());
                        }
                    });
                }
            });
        }