-
Notifications
You must be signed in to change notification settings - Fork 10
/
peerwiretranslator.py
237 lines (191 loc) · 8.83 KB
/
peerwiretranslator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
The PeerWireTranslator is responsible for translating a stream of bytes into
higher level messages and vice versa related to the peer wire protocol between
BitTorrent peers. The bytes are exchanged with a readerwriter and the higher
level messages are exchanged with a receiver. The creator of the
PeerWireTranslator must provide it with a readerwriter upon creation or
subsequently using the set_readerwriter() method. When it no longer needs the
PeerWireTranslator, it should call the unset_readerwriter() method so that all
references to the PeerWireTranslator can be released and it can be properly
garbage collected.
On the receiver side, when told to transmit one of the peer wire protocol
messages, the PeerWireTranslator generates a set of bytes to send to the
readerwriter. When the readerwriter presents the PeerWireTranslator with a set
of bytes that represent one of the peer wire protocol messages, the translator
notifies the receiver. It also notifies the receiver when a connection is
lost.
A receiver must implement the following methods: rx_keep_alive(), rx_choke(),
rx_unchoke(), rx_interested(), rx_not_interested, rx_bitfield(), rx_have(),
rx_request(), rx_piece() and rx_cancel() and connection_lost().
On the readerwriter side, when incoming bytes are available, the readerwriter
asks the PeerWireTranslator for a buffer to put them into and after it has
done that, it notifies the translator that bytes have been received. This
scheme allows the PeerWireTranslator to get enough bytes to translate the
next part of the message expected. It also allows an entire block of incoming
data to be placed in one buffer even if it is received over multiple socket
reads.
A readerwriter must implement set_receiver(), unset_receiver() and tx_bytes()
"""
import logging
import struct
from bitstring import BitArray
logger = logging.getLogger('bt.bttranslator')
_LENGTH_LEN = 4
_MSG_CHOKE = 0
_MSG_UNCHOKE = 1
_MSG_INTERESTED = 2
_MSG_NOT_INTERESTED = 3
_MSG_HAVE = 4
_MSG_BITFIELD = 5
_MSG_REQUEST = 6
_MSG_PIECE = 7
_MSG_CANCEL = 8
class PeerWireTranslator(object):
class _States(object):
Length, Message = range(2)
def __init__(self, receiver=None, readerwriter=None):
self._length_buf = bytearray(_LENGTH_LEN)
self._length_view = memoryview(self._length_buf)
self._length_state_setup()
if receiver is not None:
self.set_receiver(receiver)
else:
self._receiver = None
if readerwriter is not None:
self.set_readerwriter(readerwriter)
else:
self._readerwriter = None
self._rx_functions = {_MSG_CHOKE: self.rx_choke,
_MSG_UNCHOKE: self.rx_unchoke,
_MSG_INTERESTED: self.rx_interested,
_MSG_NOT_INTERESTED: self.rx_not_interested,
_MSG_HAVE: self.rx_have,
_MSG_BITFIELD: self.rx_bitfield,
_MSG_REQUEST: self.rx_request,
_MSG_PIECE: self.rx_piece,
_MSG_CANCEL: self.rx_cancel}
def _length_state_setup(self):
self._rx_state = self._States.Length
self._bytes_needed = _LENGTH_LEN
self._bytes_received = 0
self._current_buf = self._length_buf
self._current_view = self._length_view
def set_receiver(self, receiver):
self._receiver = receiver
def unset_receiver(self):
self._receiver = None
def set_readerwriter(self, readerwriter):
self._readerwriter = readerwriter
self._readerwriter.set_receiver(self)
def unset_readerwriter(self):
self._readerwriter.unset_receiver()
self._readerwriter = None
def get_rx_buffer(self):
return self._current_view[self._bytes_received:], self._bytes_needed
def rx_bytes(self, n):
self._bytes_received += n
self._bytes_needed -= n
if self._bytes_needed == 0:
if self._rx_state == self._States.Length:
(length,) = struct.unpack('>i', buffer(self._length_buf))
if length == 0:
self.rx_keep_alive()
self._length_state_setup()
else:
self._rx_state = self._States.Message
self._bytes_needed = length
self._bytes_received = 0
self._current_buf = bytearray(length)
self._current_view = memoryview(self._current_buf)
else:
(message_id,) = struct.unpack("B",
buffer(self._current_buf[0:1]))
try:
self._rx_functions[message_id]()
except KeyError:
logger.debug("Received message with invalid msg id: {}"
.format(message_id))
self._length_state_setup()
def rx_keep_alive(self):
if self._receiver is not None:
self._receiver.rx_keep_alive()
def rx_choke(self):
if self._receiver is not None:
self._receiver.rx_choke()
def rx_unchoke(self):
if self._receiver is not None:
self._receiver.rx_unchoke()
def rx_interested(self):
if self._receiver is not None:
self._receiver.rx_interested()
def rx_not_interested(self):
if self._receiver is not None:
self._receiver.rx_not_interested()
def rx_have(self):
if self._receiver is not None:
(index,) = struct.unpack(">I", buffer(self._current_buf[1:5]))
self._receiver.rx_have(index)
def rx_bitfield(self):
if self._receiver is not None:
bits = BitArray(bytes=self._current_buf[1:self._bytes_received])
self._receiver.rx_bitfield(bits)
def rx_request(self):
if self._receiver is not None:
buf = buffer(self._current_buf[1:])
index, begin, length, = struct.unpack(">3I", buf)
self._receiver.rx_request(index, begin, length)
def rx_piece(self):
if self._receiver is not None:
index, begin, = struct.unpack(">2I",
buffer(self._current_buf[1:9]))
self._receiver.rx_piece(index, begin,
buffer(self._current_buf[9:]))
def rx_cancel(self):
if self._receiver is not None:
buf = buffer(self._current_buf[1:])
index, begin, length, = struct.unpack(">3I", buf)
self._receiver.rx_cancel(index, begin, length)
def tx_keep_alive(self):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('B', 0))
def tx_choke(self):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB', 1, _MSG_CHOKE))
def tx_unchoke(self):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB', 1, _MSG_UNCHOKE))
def tx_interested(self):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB', 1, _MSG_INTERESTED))
def tx_not_interested(self):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB', 1,
_MSG_NOT_INTERESTED))
def tx_have(self, index):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IBI', 5,
_MSG_HAVE, index))
def tx_bitfield(self, bits):
if self._readerwriter is not None:
bitfield = bits.tobytes()
length = len(bitfield)
self._readerwriter.tx_bytes(struct.pack('>IB{}s'.format(length),
1+length, _MSG_BITFIELD,
bitfield))
def tx_request(self, index, begin, length):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB3I', 13, _MSG_REQUEST,
index, begin, length))
def tx_piece(self, index, begin, block):
if self._readerwriter is not None:
length = len(block)
self._readerwriter.tx_bytes(struct.pack('>IB2I{}s'.format(length),
9+length, _MSG_PIECE,
index, begin, block))
def tx_cancel(self, index, begin, length):
if self._readerwriter is not None:
self._readerwriter.tx_bytes(struct.pack('>IB3I', 13, _MSG_CANCEL,
index, begin, length))
def connection_lost(self):
if self._receiver is not None:
self._receiver.connection_lost()