Exemple #1
0
        /// <summary>
        /// Creates new peer information.
        /// </summary>
        /// <param name="id">The peer ID.</param>
        /// <param name="endPoint">The peer IP end-point.</param>
        public PeerInfo(PeerID id, IPEndPoint endPoint)
        {
            if (id.ID == null)
            {
                throw new ArgumentException("The peer ID is invalid.", "id");
            }
            else if (endPoint == null)
            {
                throw new ArgumentNullException("endPoint");
            }

            this.id       = id;
            this.endPoint = endPoint;
        }
Exemple #2
0
        private bool HandleHandshake(Packet packet)
        {
            if (packet.Length != 68)
            {
                Log.LogWarning("[Peer][{0}] Invalid handshake received with {1} bytes (should have been 68).", endPoint, packet.Length);
                return(false);
            }

            int protocolNameLength = packet.ReadByte();

            if (packet.RemainingBytes < (protocolNameLength + 40))
            {
                Log.LogWarning("[Peer][{0}] Invalid handshake received with {1} bytes (protocol name length: {2}).", endPoint, packet.Length, protocolNameLength);
                return(false);
            }

            string protocolName = packet.ReadString(protocolNameLength);

            if (!string.Equals(protocolName, ProtocolName))
            {
                Log.LogWarning("[Peer][{0}] Handshake protocol is not supported: {1}", endPoint, protocolName);
                return(false);
            }

            // Skip 8 bytes of flags
            packet.Skip(8);

            byte[] hashBytes = packet.ReadBytes(20);
            byte[] idBytes   = packet.ReadBytes(20);

            var infoHash = new InfoHash(hashBytes);
            var peerID   = new PeerID(idBytes);

            if (torrent != null && !torrent.InfoHash.Equals(infoHash))
            {
                Log.LogWarning("[Peer][{0}] Handshake with invalid info hash: {1}", endPoint, infoHash);
                return(false);
            }
            else if (torrent != null && peerID.Equals(torrent.PeerID))
            {
                peer.IsSelf = true;
                Log.LogDebug("[Peer][{0}] Handshake with ourself. Closing connection.", endPoint);
                return(false);
            }
            else if (torrent == null)
            {
                var foundTorrent = TorrentRegistry.FindTorrentByInfoHash(infoHash);
                if (foundTorrent == null)
                {
                    Log.LogWarning("[Peer][{0}] Handshake with unknown info hash: {1}", endPoint, infoHash);
                    return(false);
                }
                else if (foundTorrent.IsStoppedOrStopping)
                {
                    Log.LogWarning("[Peer][{0}] Handshake received for a torrent that is stopped or currently stopping: {1}", endPoint, infoHash);
                    return(false);
                }

                torrent = foundTorrent;
                if (peerID.Equals(torrent.PeerID))
                {
                    Log.LogDebug("[Peer][{0}] Handshake with ourself. Closing connection.", endPoint);
                    return(false);
                }

                peer = torrent.OnPeerHandshaked(peerID, this);
            }

            if (peer.ID.IsNone)
            {
                // If the peer had no ID prior, then we set it now and register the ID with the torrent
                peer.ID = peerID;
                torrent.RegisterPeerWithID(peerID, peer);
            }
            else if (!peerID.Equals(peer.ID))
            {
                Log.LogWarning("[Peer][{0}] Handshake with invalid peer ID: {1}   !==   {2}", endPoint, peerID, peer.ID);
                return(false);
            }

            this.infoHash       = infoHash;
            this.peerID         = peerID;
            isHandshakeReceived = true;

            Log.LogDebug("[Peer][{0}] A peer handshaked with us with info hash [{1}] and peer ID [{2}].", endPoint, infoHash, peerID);

            SendHandshake();
            SendBitField();
            OnHandshaked();
            return(true);
        }