Ejemplo n.º 1
0
 public void FixtureSetup()
 {
     rig = TestRig.CreateMultiFile();
     id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < id.BitField.Length; i += 2)
         id.BitField[i] = true;
 }
Ejemplo n.º 2
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (_engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            var id = new PeerId(e.Peer, e.TorrentManager) {Connection = e.Connection};

            Logger.Log(id.Connection, "ListenManager - ConnectionReceived");

            if (id.Connection.IsIncoming)
            {
                var skeys = new List<InfoHash>();

                ClientEngine.MainLoop.QueueWait(delegate
                                                    {
                                                        for (var i = 0; i < _engine.Torrents.Count; i++)
                                                            skeys.Add(_engine.Torrents[i].InfoHash);
                                                    });

                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, _endCheckEncryptionCallback,
                                                      id, skeys.ToArray());
            }
            else
            {
                ClientEngine.MainLoop.Queue(() => _engine.ConnectionManager.ProcessFreshConnection(id));
            }
        }
Ejemplo n.º 3
0
 internal PeerConnectionEventArgs( TorrentManager manager, PeerId id, Direction direction, String message )
     : base(manager)
 {
     _peerConnectionId = id;
     _connectionDirection = direction;
     _message = message;
 }
Ejemplo n.º 4
0
 public virtual void Unchoke(PeerId id)
 {
     id.AmChoking = false;
     id.TorrentManager.UploadingTo++;
     id.Enqueue(new UnchokeMessage());
     id.LastUnchoked = DateTime.Now;
 }
Ejemplo n.º 5
0
        public PeerExchangePeersAdded(TorrentManager manager, int count, int total, PeerId id)
            : base(manager, count, total)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            _id = id;
        }
Ejemplo n.º 6
0
        internal static IAsyncResult BeginCheckEncryption(PeerId id, int bytesToReceive, AsyncCallback callback, object state, InfoHash[] sKeys)
        {
            EncryptorAsyncResult result = new EncryptorAsyncResult(id, callback, state);
            result.SKeys = sKeys;

            IConnection c = id.Connection;
            ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromSeconds(10), delegate {
                if (id.Encryptor == null || id.Decryptor == null)
                    id.CloseConnection();
                return false;
            });

            try
            {
                // If the connection is incoming, receive the handshake before
                // trying to decide what encryption to use
                if (id.Connection.IsIncoming)
                {
                    result.Buffer = new byte[bytesToReceive];
                    NetworkIO.EnqueueReceive(c, result.Buffer, 0, result.Buffer.Length, null, null, null, HandshakeReceivedCallback, result);
                }
                else
                {
                    EncryptionTypes usable = CheckRC4(id);
                    bool hasPlainText = Toolbox.HasEncryption(usable, EncryptionTypes.PlainText);
                    bool hasRC4 = Toolbox.HasEncryption(usable, EncryptionTypes.RC4Full) || Toolbox.HasEncryption(usable, EncryptionTypes.RC4Header);
                    if (id.Engine.Settings.PreferEncryption)
                    {
                        if (hasRC4)
                        {
                            result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable);
                            result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result);
                        }
                        else
                        {
                            result.Complete();
                        }
                    }
                    else
                    {
                        if (hasPlainText)
                        {
                            result.Complete();
                        }
                        else
                        {
                            result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable);
                            result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Complete(ex);
            }
            return result;
        }
Ejemplo n.º 7
0
 public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     // Invert 'bitfield' and AND it with the peers bitfield
     // Any pieces which are 'true' in the bitfield will not be downloaded
     temp.From(peerBitfield).NAnd(bitfield);
     if (temp.AllFalse)
         return null;
     return base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex);
 }
Ejemplo n.º 8
0
 internal Block(Piece piece, int startOffset, int requestLength)
 {
     this.requestedOff = null;
     this.piece = piece;
     this.received = false;
     this.requested = false;
     this.requestLength = requestLength;
     this.startOffset = startOffset;
     this.written = false;
 }
Ejemplo n.º 9
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            if (peerBitfield.AllFalse)
                return null;

            if (count > 1)
                return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            int midpoint = random.Next(startIndex, endIndex);
            return base.PickPiece(id, peerBitfield, otherPeers, count, midpoint, endIndex) ??
                   base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, midpoint);
        }
Ejemplo n.º 10
0
        public void Choke(PeerId peer)
        {
            //Choke the supplied peer

            if (peer.AmChoking)
                //We're already choking this peer, nothing to do
                return;

            peer.AmChoking = true;
            _owningTorrent.UploadingTo--;
            RejectPendingRequests(peer);
            peer.EnqueueAt(new ChokeMessage(), 0);
            Logger.Log(peer.Connection, "Choking");
            //			Send2Log("Choking: " + PeerToChoke.Location);
        }
Ejemplo n.º 11
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            if (peerBitfield.AllFalse)
                return null;

            if (count > 1)
                return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            GenerateRarestFirst(peerBitfield, otherPeers);

            while (rarest.Count > 0)
            {
                BitField current = rarest.Pop();
                MessageBundle bundle = base.PickPiece(id, current, otherPeers, count, startIndex, endIndex);
                spares.Push(current);

                if (bundle != null)
                    return bundle;
            }

            return null;
        }
Ejemplo n.º 12
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            PickPieceId.Add(id);
            BitField clone = new BitField(peerBitfield.Length);
            clone.Or(peerBitfield);
            PickPieceBitfield.Add(clone);
            PickPiecePeers.Add(otherPeers);
            PickPieceStartIndex.Add(startIndex);
            PickPieceEndIndex.Add(endIndex);
            PickPieceCount.Add(count);

            for (int i = startIndex; i < endIndex; i++)
            {
                if (PickedPieces.Contains(i))
                    continue;
                PickedPieces.Add(i);
                if (ReturnNoPiece)
                    return null;
                else
                    return new MessageBundle();
            }
            return null;
        }
Ejemplo n.º 13
0
 public RequestMessage PickPiece(PeerId peer, List<PeerId> otherPeers)
 {
     MessageBundle bundle = PickPiece(peer, otherPeers, 1);
     return bundle == null ? null : (RequestMessage)bundle.Messages[0];
 }
Ejemplo n.º 14
0
 public override bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece)
 {
     piece = null;
     return false;
 }
Ejemplo n.º 15
0
 public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     return null;
 }
Ejemplo n.º 16
0
 public override void CancelRequests(PeerId peer)
 {
 }
Ejemplo n.º 17
0
 public override void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
 }
Ejemplo n.º 18
0
 public virtual RequestMessage ContinueExistingRequest(PeerId peer)
 {
     CheckOverriden();
     return picker.ContinueExistingRequest(peer);
 }
Ejemplo n.º 19
0
 public virtual void CancelRequests(PeerId peer)
 {
     CheckOverriden();
     picker.CancelRequests(peer);
 }
Ejemplo n.º 20
0
 public virtual void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
     CheckOverriden();
     picker.CancelRequest(peer, piece, startOffset, length);
 }
Ejemplo n.º 21
0
        public void Setup()
        {
            bitfield = new BitField(40).SetAll(true)
                                       .Set(4, false)
                                       .Set(6, false)
                                       .Set(24, false)
                                       .Set(36, false);
            picker = new EndGamePicker();
            pieces = new List<Piece>(new Piece[] {
                new Piece(4, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(6, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(24, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(36, rig.Torrent.PieceLength, rig.Torrent.Size)
            });

            id = new PeerId(new Peer("peerid", new Uri("tcp://weburl.com")), rig.Manager);
            id.IsChoking = false;
            id.BitField.SetAll(false);

            other = new PeerId(new Peer("other", new Uri("tcp://other.com")), rig.Manager);
            other.IsChoking = false;
            other.BitField.SetAll(false);
        }
Ejemplo n.º 22
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;
            int i;
            for (i = 0; i < 1000; i++)
            {
                try
                {
                    listener = new HttpListener();
                    listener.Prefixes.Add(string.Format(listenerURL, i));
                    listener.Start();
                    break;
                }
                catch
                {

                }
            }
            listener.BeginGetContext(GotContext, null);
            rig = TestRig.CreateMultiFile();
            connection = new HttpConnection(new Uri(string.Format(listenerURL, i)));
            connection.Manager = rig.Manager;

            id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection = connection;
            id.IsChoking = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces);
        }
Ejemplo n.º 23
0
 public void FixtureSetup()
 {
     rig = TestRig.CreateMultiFile();
     id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     id.BitField.SetAll(true);
 }
Ejemplo n.º 24
0
        public void SingleFileTorrent()
        {
            rig.Dispose();
            rig = TestRig.CreateSingleFile();
            string url = rig.Torrent.GetRightHttpSeeds[0];
            connection = new HttpConnection(new Uri (url));
            connection.Manager = rig.Manager;

            id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection = connection;
            id.IsChoking = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces);
            RecieveFirst();
            Assert.AreEqual(url, requestedUrl[0]);
        }
Ejemplo n.º 25
0
 public virtual void Choke(PeerId id)
 {
     id.AmChoking = true;
     id.TorrentManager.UploadingTo--;
     id.Enqueue(new ChokeMessage());
 }
Ejemplo n.º 26
0
 public MessageBundle PickPiece(PeerId peer, List<PeerId> otherPeers, int count)
 {
     return PickPiece(peer, peer.BitField, otherPeers, count, 0, peer.BitField.Length);
 }
Ejemplo n.º 27
0
        public void Setup(bool metadataMode, string metadataPath)
        {
            pair = new ConnectionPair(55432);
            rig = TestRig.CreateSingleFile(1024 * 1024 * 1024, 32768, metadataMode);
            rig.MetadataPath = metadataPath;
            rig.RecreateManager();

            rig.Manager.HashChecked = true;
            rig.Manager.Start();
            rig.AddConnection(pair.Outgoing);

            var connection = pair.Incoming;
            PeerId id = new PeerId(new Peer("", connection.Uri), rig.Manager);
            id.Connection = connection;
            byte[] data;

            EncryptorFactory.EndCheckEncryption(EncryptorFactory.BeginCheckEncryption(id, 68, null, null, new InfoHash[] { id.TorrentManager.InfoHash }), out data);
            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
        }
Ejemplo n.º 28
0
 public virtual MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     CheckOverriden();
     return picker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);
 }
Ejemplo n.º 29
0
 public virtual void Setup()
 {
     // Yes, this is horrible. Deal with it.
     rig = TestRig.CreateMultiFile();
     peers = new List<PeerId>();
     picker = new IgnoringPicker(rig.Manager.Bitfield, new StandardPicker());
     picker.Initialise(rig.Manager.Bitfield, rig.Manager.Torrent.Files, new List<Piece>());
     peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < 20; i++)
     {
         PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager);
         p.SupportsFastPeer = true;
         peers.Add(p);
     }
 }
Ejemplo n.º 30
0
 public virtual bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece)
 {
     CheckOverriden();
     return picker.ValidatePiece(peer, pieceIndex, startOffset, length, out piece);
 }