Beispiel #1
0
    def rx_handshake(self, reserved, info_hash, peer_id):
        if self._state == self._States.Handshake_Initiated:
            if info_hash != self._info_hash:
                self._drop_connection()
            else:
                self._translator.unset_receiver()
                self._translator.unset_readerwriter()

                self._translator = PeerWireTranslator(self, self._protocol)
                self._state = self._States.Bitfield_Allowed

                self._translator.tx_bitfield(self._client.get_bitfield())
    def setup_class(self):
        self.translator = PeerWireTranslator()

        self.msg_keep_alive = struct.pack('B', 0)
        self.msg_choke = struct.pack('>IB', 1, _MSG_CHOKE)
        self.msg_unchoke = struct.pack('>IB', 1, _MSG_UNCHOKE)
        self.msg_interested = struct.pack('>IB', 1, _MSG_INTERESTED)
        self.msg_not_interested = struct.pack('>IB', 1, _MSG_NOT_INTERESTED)

        self.have_index = 55
        self.msg_have = struct.pack('>IBI', 5, _MSG_HAVE, self.have_index)

        self.bits = BitArray('0x123456789abcdef')
        bitsasbytes = self.bits.tobytes()
        length = len(bitsasbytes)
        self.msg_bitfield = struct.pack('>IB{}s'.format(length), 1 + length,
                                        _MSG_BITFIELD, bitsasbytes)

        self.request_index = 10
        self.request_begin = 60
        self.request_length = 255
        self.msg_request = struct.pack('>IB3I', 13, _MSG_REQUEST,
                                       self.request_index, self.request_begin,
                                       self.request_length)

        self.piece_index = 17
        self.piece_begin = 245
        self.piece_block = """Four score and seven years ago our fathers brought forth on this
        continent a new nation, conceived in liberty, and dedicated to the
        proposition that all men are created equal.
        Now we are engaged in a great civil war, testing whether that nation,
        or any nation so conceived and so dedicated, can long endure. We are
        met on a great battle-field of that war. We have come to dedicate a
        portion of that field, as a final resting place for those who here gave
        their lives that that nation might live. It is altogether fitting and
        proper that we should do this.
        But, in a larger sense, we can not dedicate, we can not consecrate, we
        can not hallow this ground. The brave men, living and dead, who
        struggled here, have consecrated it, far above our poor power to add or
        detract. The world will little note, nor long remember what we say
        here, but it can never forget what they did here. It is for us the
        living, rather, to be dedicated here to the unfinished work which they
        who fought here have thus far so nobly advanced. It is rather for us to
        be here dedicated to the great task remaining before us—that from these
        honored dead we take increased devotion to that cause for which they
        gave the last full measure of devotion—that we here highly resolve that
        these dead shall not have died in vain—that this nation, under God,
        shall have a new birth of freedom—and that government of the people, by
        the people, for the people, shall not perish from the earth."""
        length = len(self.piece_block)
        self.msg_piece = struct.pack('>IB2I{}s'.format(length), 9 + length,
                                     _MSG_PIECE, self.piece_index,
                                     self.piece_begin, self.piece_block)

        self.cancel_index = 99
        self.cancel_begin = 128
        self.cancel_length = 13
        self.msg_cancel = struct.pack('>IB3I', 13, _MSG_CANCEL,
                                      self.cancel_index, self.cancel_begin,
                                      self.cancel_length)

        self.junk = "abcdefghijklmnop"
        length = len(self.junk)
        self.msg_invalid = struct.pack('>IB{}s'.format(length), 1 + length,
                                       _MSG_INVALID, self.junk)