1 | /* |
---|
2 | * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: peer-msgs.c 3839 2007-11-16 20:40:03Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <ctype.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <stdio.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <string.h> |
---|
19 | #include <libgen.h> /* basename */ |
---|
20 | |
---|
21 | #include <netinet/in.h> /* struct in_addr */ |
---|
22 | |
---|
23 | #include <event.h> |
---|
24 | |
---|
25 | #include "transmission.h" |
---|
26 | #include "bencode.h" |
---|
27 | #include "completion.h" |
---|
28 | #include "inout.h" |
---|
29 | #include "list.h" |
---|
30 | #include "peer-io.h" |
---|
31 | #include "peer-mgr.h" |
---|
32 | #include "peer-mgr-private.h" |
---|
33 | #include "peer-msgs.h" |
---|
34 | #include "ratecontrol.h" |
---|
35 | #include "trevent.h" |
---|
36 | #include "utils.h" |
---|
37 | |
---|
38 | /** |
---|
39 | *** |
---|
40 | **/ |
---|
41 | |
---|
42 | enum |
---|
43 | { |
---|
44 | BT_CHOKE = 0, |
---|
45 | BT_UNCHOKE = 1, |
---|
46 | BT_INTERESTED = 2, |
---|
47 | BT_NOT_INTERESTED = 3, |
---|
48 | BT_HAVE = 4, |
---|
49 | BT_BITFIELD = 5, |
---|
50 | BT_REQUEST = 6, |
---|
51 | BT_PIECE = 7, |
---|
52 | BT_CANCEL = 8, |
---|
53 | BT_PORT = 9, |
---|
54 | BT_SUGGEST = 13, |
---|
55 | BT_HAVE_ALL = 14, |
---|
56 | BT_HAVE_NONE = 15, |
---|
57 | BT_REJECT = 16, |
---|
58 | BT_ALLOWED_FAST = 17, |
---|
59 | BT_LTEP = 20, |
---|
60 | |
---|
61 | LTEP_HANDSHAKE = 0, |
---|
62 | |
---|
63 | OUR_LTEP_PEX = 1, |
---|
64 | |
---|
65 | MAX_REQUEST_BYTE_COUNT = (16 * 1024), /* drop requests who want too much */ |
---|
66 | |
---|
67 | /* idle seconds before we send a keepalive */ |
---|
68 | KEEPALIVE_INTERVAL_SECS = 90, |
---|
69 | |
---|
70 | PEX_INTERVAL = (60 * 1000), /* msec between calls to sendPex() */ |
---|
71 | PEER_PULSE_INTERVAL = (100), /* msec between calls to pulse() */ |
---|
72 | RATE_PULSE_INTERVAL = (333), /* msec between calls to ratePulse() */ |
---|
73 | |
---|
74 | /* Fast Peers Extension constants */ |
---|
75 | MAX_FAST_ALLOWED_COUNT = 10, /* max. number of pieces we fast-allow to another peer */ |
---|
76 | MAX_FAST_ALLOWED_THRESHOLD = 10, /* max threshold for allowing fast-pieces requests */ |
---|
77 | |
---|
78 | }; |
---|
79 | |
---|
80 | enum |
---|
81 | { |
---|
82 | AWAITING_BT_LENGTH, |
---|
83 | AWAITING_BT_ID, |
---|
84 | AWAITING_BT_MESSAGE, |
---|
85 | AWAITING_BT_PIECE |
---|
86 | }; |
---|
87 | |
---|
88 | struct peer_request |
---|
89 | { |
---|
90 | uint32_t index; |
---|
91 | uint32_t offset; |
---|
92 | uint32_t length; |
---|
93 | time_t time_requested; |
---|
94 | }; |
---|
95 | |
---|
96 | static int |
---|
97 | compareRequest( const void * va, const void * vb ) |
---|
98 | { |
---|
99 | int i; |
---|
100 | const struct peer_request * a = va; |
---|
101 | const struct peer_request * b = vb; |
---|
102 | if(( i = tr_compareUint32( a->index, b->index ))) return i; |
---|
103 | if(( i = tr_compareUint32( a->offset, b->offset ))) return i; |
---|
104 | if(( i = tr_compareUint32( a->length, b->length ))) return i; |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | /* this is raw, unchanged data from the peer regarding |
---|
109 | * the current message that it's sending us. */ |
---|
110 | struct tr_incoming |
---|
111 | { |
---|
112 | uint32_t length; /* includes the +1 for id length */ |
---|
113 | uint8_t id; |
---|
114 | struct peer_request blockReq; /* metadata for incoming blocks */ |
---|
115 | struct evbuffer * block; /* piece data for incoming blocks */ |
---|
116 | }; |
---|
117 | |
---|
118 | struct tr_peermsgs |
---|
119 | { |
---|
120 | tr_peer * info; |
---|
121 | |
---|
122 | tr_handle * handle; |
---|
123 | tr_torrent * torrent; |
---|
124 | tr_peerIo * io; |
---|
125 | |
---|
126 | tr_publisher_t * publisher; |
---|
127 | |
---|
128 | struct evbuffer * outBlock; /* buffer of all the current piece message */ |
---|
129 | struct evbuffer * outMessages; /* buffer of all the non-piece messages */ |
---|
130 | tr_list * peerAskedFor; |
---|
131 | tr_list * peerAskedForFast; |
---|
132 | tr_list * clientAskedFor; |
---|
133 | tr_list * clientWillAskFor; |
---|
134 | |
---|
135 | tr_timer * rateTimer; |
---|
136 | tr_timer * pulseTimer; |
---|
137 | tr_timer * pexTimer; |
---|
138 | |
---|
139 | time_t lastReqAddedAt; |
---|
140 | time_t clientSentPexAt; |
---|
141 | time_t clientSentAnythingAt; |
---|
142 | |
---|
143 | unsigned int notListening : 1; |
---|
144 | unsigned int peerSupportsPex : 1; |
---|
145 | unsigned int clientSentLtepHandshake : 1; |
---|
146 | unsigned int peerSentLtepHandshake : 1; |
---|
147 | |
---|
148 | tr_bitfield * clientAllowedPieces; |
---|
149 | tr_bitfield * peerAllowedPieces; |
---|
150 | |
---|
151 | tr_bitfield * clientSuggestedPieces; |
---|
152 | |
---|
153 | uint8_t state; |
---|
154 | uint8_t ut_pex_id; |
---|
155 | uint16_t pexCount; |
---|
156 | uint32_t maxActiveRequests; |
---|
157 | uint32_t minActiveRequests; |
---|
158 | |
---|
159 | struct tr_incoming incoming; |
---|
160 | |
---|
161 | tr_pex * pex; |
---|
162 | }; |
---|
163 | |
---|
164 | /** |
---|
165 | *** |
---|
166 | **/ |
---|
167 | |
---|
168 | static void |
---|
169 | myDebug( const char * file, int line, |
---|
170 | const struct tr_peermsgs * msgs, |
---|
171 | const char * fmt, ... ) |
---|
172 | { |
---|
173 | FILE * fp = tr_getLog( ); |
---|
174 | if( fp != NULL ) |
---|
175 | { |
---|
176 | va_list args; |
---|
177 | char timestr[64]; |
---|
178 | struct evbuffer * buf = evbuffer_new( ); |
---|
179 | char * myfile = tr_strdup( file ); |
---|
180 | |
---|
181 | evbuffer_add_printf( buf, "[%s] %s - %s [%s]: ", |
---|
182 | tr_getLogTimeStr( timestr, sizeof(timestr) ), |
---|
183 | msgs->torrent->info.name, |
---|
184 | tr_peerIoGetAddrStr( msgs->io ), |
---|
185 | msgs->info->client ); |
---|
186 | va_start( args, fmt ); |
---|
187 | evbuffer_add_vprintf( buf, fmt, args ); |
---|
188 | va_end( args ); |
---|
189 | evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line ); |
---|
190 | fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp ); |
---|
191 | |
---|
192 | tr_free( myfile ); |
---|
193 | evbuffer_free( buf ); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | #define dbgmsg(msgs, fmt...) myDebug(__FILE__, __LINE__, msgs, ##fmt ) |
---|
198 | |
---|
199 | /** |
---|
200 | *** |
---|
201 | **/ |
---|
202 | |
---|
203 | static void |
---|
204 | protocolSendRequest( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
205 | { |
---|
206 | tr_peerIo * io = msgs->io; |
---|
207 | struct evbuffer * out = msgs->outMessages; |
---|
208 | |
---|
209 | dbgmsg( msgs, "requesting %u:%u->%u", req->index, req->offset, req->length ); |
---|
210 | tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 3*sizeof(uint32_t) ); |
---|
211 | tr_peerIoWriteUint8 ( io, out, BT_REQUEST ); |
---|
212 | tr_peerIoWriteUint32( io, out, req->index ); |
---|
213 | tr_peerIoWriteUint32( io, out, req->offset ); |
---|
214 | tr_peerIoWriteUint32( io, out, req->length ); |
---|
215 | } |
---|
216 | |
---|
217 | static void |
---|
218 | protocolSendCancel( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
219 | { |
---|
220 | tr_peerIo * io = msgs->io; |
---|
221 | struct evbuffer * out = msgs->outMessages; |
---|
222 | |
---|
223 | dbgmsg( msgs, "cancelling %u:%u->%u", req->index, req->offset, req->length ); |
---|
224 | tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 3*sizeof(uint32_t) ); |
---|
225 | tr_peerIoWriteUint8 ( io, out, BT_CANCEL ); |
---|
226 | tr_peerIoWriteUint32( io, out, req->index ); |
---|
227 | tr_peerIoWriteUint32( io, out, req->offset ); |
---|
228 | tr_peerIoWriteUint32( io, out, req->length ); |
---|
229 | } |
---|
230 | |
---|
231 | static void |
---|
232 | protocolSendHave( tr_peermsgs * msgs, uint32_t index ) |
---|
233 | { |
---|
234 | tr_peerIo * io = msgs->io; |
---|
235 | struct evbuffer * out = msgs->outMessages; |
---|
236 | |
---|
237 | dbgmsg( msgs, "sending Have %u", index ); |
---|
238 | tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + sizeof(uint32_t) ); |
---|
239 | tr_peerIoWriteUint8 ( io, out, BT_HAVE ); |
---|
240 | tr_peerIoWriteUint32( io, out, index ); |
---|
241 | } |
---|
242 | |
---|
243 | static void |
---|
244 | protocolSendChoke( tr_peermsgs * msgs, int choke ) |
---|
245 | { |
---|
246 | tr_peerIo * io = msgs->io; |
---|
247 | struct evbuffer * out = msgs->outMessages; |
---|
248 | |
---|
249 | dbgmsg( msgs, "sending %s", (choke ? "Choke" : "Unchoke") ); |
---|
250 | tr_peerIoWriteUint32( io, out, sizeof(uint8_t) ); |
---|
251 | tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE ); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | *** EVENTS |
---|
256 | **/ |
---|
257 | |
---|
258 | static const tr_peermsgs_event blankEvent = { 0, 0, 0, 0, 0.0f }; |
---|
259 | |
---|
260 | static void |
---|
261 | publish( tr_peermsgs * msgs, tr_peermsgs_event * e ) |
---|
262 | { |
---|
263 | tr_publisherPublish( msgs->publisher, msgs->info, e ); |
---|
264 | } |
---|
265 | |
---|
266 | static void |
---|
267 | fireGotAssertError( tr_peermsgs * msgs ) |
---|
268 | { |
---|
269 | tr_peermsgs_event e = blankEvent; |
---|
270 | e.eventType = TR_PEERMSG_GOT_ASSERT_ERROR; |
---|
271 | publish( msgs, &e ); |
---|
272 | } |
---|
273 | |
---|
274 | static void |
---|
275 | fireGotError( tr_peermsgs * msgs ) |
---|
276 | { |
---|
277 | tr_peermsgs_event e = blankEvent; |
---|
278 | e.eventType = TR_PEERMSG_GOT_ERROR; |
---|
279 | publish( msgs, &e ); |
---|
280 | } |
---|
281 | |
---|
282 | static void |
---|
283 | fireNeedReq( tr_peermsgs * msgs ) |
---|
284 | { |
---|
285 | tr_peermsgs_event e = blankEvent; |
---|
286 | e.eventType = TR_PEERMSG_NEED_REQ; |
---|
287 | publish( msgs, &e ); |
---|
288 | } |
---|
289 | |
---|
290 | static void |
---|
291 | firePeerProgress( tr_peermsgs * msgs ) |
---|
292 | { |
---|
293 | tr_peermsgs_event e = blankEvent; |
---|
294 | e.eventType = TR_PEERMSG_PEER_PROGRESS; |
---|
295 | e.progress = msgs->info->progress; |
---|
296 | publish( msgs, &e ); |
---|
297 | } |
---|
298 | |
---|
299 | static void |
---|
300 | fireClientHave( tr_peermsgs * msgs, uint32_t pieceIndex ) |
---|
301 | { |
---|
302 | tr_peermsgs_event e = blankEvent; |
---|
303 | e.eventType = TR_PEERMSG_CLIENT_HAVE; |
---|
304 | e.pieceIndex = pieceIndex; |
---|
305 | publish( msgs, &e ); |
---|
306 | } |
---|
307 | |
---|
308 | static void |
---|
309 | fireGotBlock( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
310 | { |
---|
311 | tr_peermsgs_event e = blankEvent; |
---|
312 | e.eventType = TR_PEERMSG_CLIENT_BLOCK; |
---|
313 | e.pieceIndex = req->index; |
---|
314 | e.offset = req->offset; |
---|
315 | e.length = req->length; |
---|
316 | publish( msgs, &e ); |
---|
317 | } |
---|
318 | |
---|
319 | static void |
---|
320 | fireCancelledReq( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
321 | { |
---|
322 | tr_peermsgs_event e = blankEvent; |
---|
323 | e.eventType = TR_PEERMSG_CANCEL; |
---|
324 | e.pieceIndex = req->index; |
---|
325 | e.offset = req->offset; |
---|
326 | e.length = req->length; |
---|
327 | publish( msgs, &e ); |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | *** INTEREST |
---|
332 | **/ |
---|
333 | |
---|
334 | static int |
---|
335 | isPieceInteresting( const tr_peermsgs * peer, |
---|
336 | int piece ) |
---|
337 | { |
---|
338 | const tr_torrent * torrent = peer->torrent; |
---|
339 | if( torrent->info.pieces[piece].dnd ) /* we don't want it */ |
---|
340 | return FALSE; |
---|
341 | if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we have it */ |
---|
342 | return FALSE; |
---|
343 | if( !tr_bitfieldHas( peer->info->have, piece ) ) /* peer doesn't have it */ |
---|
344 | return FALSE; |
---|
345 | if( tr_bitfieldHas( peer->info->banned, piece ) ) /* peer is banned */ |
---|
346 | return FALSE; |
---|
347 | return TRUE; |
---|
348 | } |
---|
349 | |
---|
350 | /* "interested" means we'll ask for piece data if they unchoke us */ |
---|
351 | static int |
---|
352 | isPeerInteresting( const tr_peermsgs * msgs ) |
---|
353 | { |
---|
354 | int i; |
---|
355 | const tr_torrent * torrent; |
---|
356 | const tr_bitfield * bitfield; |
---|
357 | const int clientIsSeed = tr_torrentIsSeed( msgs->torrent ); |
---|
358 | |
---|
359 | if( clientIsSeed ) |
---|
360 | return FALSE; |
---|
361 | |
---|
362 | torrent = msgs->torrent; |
---|
363 | bitfield = tr_cpPieceBitfield( torrent->completion ); |
---|
364 | |
---|
365 | if( !msgs->info->have ) |
---|
366 | return TRUE; |
---|
367 | |
---|
368 | assert( bitfield->len == msgs->info->have->len ); |
---|
369 | for( i=0; i<torrent->info.pieceCount; ++i ) |
---|
370 | if( isPieceInteresting( msgs, i ) ) |
---|
371 | return TRUE; |
---|
372 | |
---|
373 | return FALSE; |
---|
374 | } |
---|
375 | |
---|
376 | static void |
---|
377 | sendInterest( tr_peermsgs * msgs, int weAreInterested ) |
---|
378 | { |
---|
379 | assert( msgs != NULL ); |
---|
380 | assert( weAreInterested==0 || weAreInterested==1 ); |
---|
381 | |
---|
382 | msgs->info->clientIsInterested = weAreInterested; |
---|
383 | dbgmsg( msgs, "Sending %s", |
---|
384 | weAreInterested ? "Interested" : "Not Interested"); |
---|
385 | |
---|
386 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) ); |
---|
387 | tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, |
---|
388 | weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED ); |
---|
389 | } |
---|
390 | |
---|
391 | static void |
---|
392 | updateInterest( tr_peermsgs * msgs ) |
---|
393 | { |
---|
394 | const int i = isPeerInteresting( msgs ); |
---|
395 | if( i != msgs->info->clientIsInterested ) |
---|
396 | sendInterest( msgs, i ); |
---|
397 | if( i ) |
---|
398 | fireNeedReq( msgs ); |
---|
399 | } |
---|
400 | |
---|
401 | #define MIN_CHOKE_PERIOD_SEC 10 |
---|
402 | |
---|
403 | static void |
---|
404 | cancelAllRequestsToClientExceptFast( tr_peermsgs * msgs ) |
---|
405 | { |
---|
406 | tr_list_free( &msgs->peerAskedFor, tr_free ); |
---|
407 | } |
---|
408 | |
---|
409 | void |
---|
410 | tr_peerMsgsSetChoke( tr_peermsgs * msgs, int choke ) |
---|
411 | { |
---|
412 | const time_t fibrillationTime = time(NULL) - MIN_CHOKE_PERIOD_SEC; |
---|
413 | |
---|
414 | assert( msgs != NULL ); |
---|
415 | assert( msgs->info != NULL ); |
---|
416 | assert( choke==0 || choke==1 ); |
---|
417 | |
---|
418 | if( msgs->info->chokeChangedAt > fibrillationTime ) |
---|
419 | { |
---|
420 | dbgmsg( msgs, "Not changing choke to %d to avoid fibrillation", choke ); |
---|
421 | } |
---|
422 | else if( msgs->info->peerIsChoked != choke ) |
---|
423 | { |
---|
424 | msgs->info->peerIsChoked = choke; |
---|
425 | if( choke ) |
---|
426 | cancelAllRequestsToClientExceptFast( msgs ); |
---|
427 | protocolSendChoke( msgs, choke ); |
---|
428 | msgs->info->chokeChangedAt = time( NULL ); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | *** |
---|
434 | **/ |
---|
435 | |
---|
436 | void |
---|
437 | tr_peerMsgsHave( tr_peermsgs * msgs, |
---|
438 | uint32_t index ) |
---|
439 | { |
---|
440 | protocolSendHave( msgs, index ); |
---|
441 | |
---|
442 | /* since we have more pieces now, we might not be interested in this peer */ |
---|
443 | updateInterest( msgs ); |
---|
444 | } |
---|
445 | #if 0 |
---|
446 | static void |
---|
447 | sendFastSuggest( tr_peermsgs * msgs, |
---|
448 | uint32_t pieceIndex ) |
---|
449 | { |
---|
450 | assert( msgs != NULL ); |
---|
451 | |
---|
452 | if( tr_peerIoSupportsFEXT( msgs->io ) ) |
---|
453 | { |
---|
454 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) ); |
---|
455 | tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_SUGGEST ); |
---|
456 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex ); |
---|
457 | } |
---|
458 | } |
---|
459 | #endif |
---|
460 | static void |
---|
461 | sendFastHave( tr_peermsgs * msgs, int all ) |
---|
462 | { |
---|
463 | assert( msgs != NULL ); |
---|
464 | |
---|
465 | if( tr_peerIoSupportsFEXT( msgs->io ) ) |
---|
466 | { |
---|
467 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) ); |
---|
468 | tr_peerIoWriteUint8( msgs->io, msgs->outMessages, ( all ? BT_HAVE_ALL |
---|
469 | : BT_HAVE_NONE ) ); |
---|
470 | updateInterest( msgs ); |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | static void |
---|
475 | sendFastReject( tr_peermsgs * msgs, |
---|
476 | uint32_t pieceIndex, |
---|
477 | uint32_t offset, |
---|
478 | uint32_t length ) |
---|
479 | { |
---|
480 | assert( msgs != NULL ); |
---|
481 | |
---|
482 | if( tr_peerIoSupportsFEXT( msgs->io ) ) |
---|
483 | { |
---|
484 | const uint32_t len = sizeof(uint8_t) + 3 * sizeof(uint32_t); |
---|
485 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len ); |
---|
486 | tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_REJECT ); |
---|
487 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex ); |
---|
488 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, offset ); |
---|
489 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, length ); |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | static void |
---|
494 | sendFastAllowed( tr_peermsgs * msgs, |
---|
495 | uint32_t pieceIndex) |
---|
496 | { |
---|
497 | assert( msgs != NULL ); |
---|
498 | |
---|
499 | if( tr_peerIoSupportsFEXT( msgs->io ) ) |
---|
500 | { |
---|
501 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) ); |
---|
502 | tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_ALLOWED_FAST ); |
---|
503 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex ); |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | |
---|
508 | |
---|
509 | static void |
---|
510 | sendFastAllowedSet( tr_peermsgs * msgs ) |
---|
511 | { |
---|
512 | int i = 0; |
---|
513 | while (i <= msgs->torrent->info.pieceCount ) |
---|
514 | { |
---|
515 | if ( tr_bitfieldHas( msgs->peerAllowedPieces, i) ) |
---|
516 | sendFastAllowed( msgs, i ); |
---|
517 | i++; |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | |
---|
522 | /** |
---|
523 | *** |
---|
524 | **/ |
---|
525 | |
---|
526 | static int |
---|
527 | reqIsValid( const tr_peermsgs * msgs, |
---|
528 | uint32_t index, |
---|
529 | uint32_t offset, |
---|
530 | uint32_t length ) |
---|
531 | { |
---|
532 | const tr_torrent * tor = msgs->torrent; |
---|
533 | |
---|
534 | if( index >= (uint32_t) tor->info.pieceCount ) |
---|
535 | return FALSE; |
---|
536 | if ( (int)offset >= tr_torPieceCountBytes( tor, (int)index ) ) |
---|
537 | return FALSE; |
---|
538 | if( length > MAX_REQUEST_BYTE_COUNT ) |
---|
539 | return FALSE; |
---|
540 | if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize ) |
---|
541 | return FALSE; |
---|
542 | |
---|
543 | return TRUE; |
---|
544 | } |
---|
545 | |
---|
546 | static int |
---|
547 | requestIsValid( const tr_peermsgs * msgs, const struct peer_request * req ) |
---|
548 | { |
---|
549 | return reqIsValid( msgs, req->index, req->offset, req->length ); |
---|
550 | } |
---|
551 | |
---|
552 | static void |
---|
553 | pumpRequestQueue( tr_peermsgs * msgs ) |
---|
554 | { |
---|
555 | const int max = msgs->maxActiveRequests; |
---|
556 | const int min = msgs->minActiveRequests; |
---|
557 | int count = tr_list_size( msgs->clientAskedFor ); |
---|
558 | int sent = 0; |
---|
559 | |
---|
560 | if( count > min ) |
---|
561 | return; |
---|
562 | if( msgs->info->clientIsChoked ) |
---|
563 | return; |
---|
564 | |
---|
565 | while( ( count < max ) && ( msgs->clientWillAskFor != NULL ) ) |
---|
566 | { |
---|
567 | struct peer_request * r = tr_list_pop_front( &msgs->clientWillAskFor ); |
---|
568 | protocolSendRequest( msgs, r ); |
---|
569 | r->time_requested = msgs->lastReqAddedAt = time( NULL ); |
---|
570 | tr_list_append( &msgs->clientAskedFor, r ); |
---|
571 | ++count; |
---|
572 | ++sent; |
---|
573 | } |
---|
574 | |
---|
575 | if( sent ) |
---|
576 | dbgmsg( msgs, "pump sent %d requests, now have %d active and %d queued", |
---|
577 | sent, |
---|
578 | tr_list_size(msgs->clientAskedFor), |
---|
579 | tr_list_size(msgs->clientWillAskFor) ); |
---|
580 | |
---|
581 | if( count < max ) |
---|
582 | fireNeedReq( msgs ); |
---|
583 | } |
---|
584 | |
---|
585 | static int |
---|
586 | pulse( void * vmsgs ); |
---|
587 | |
---|
588 | int |
---|
589 | tr_peerMsgsAddRequest( tr_peermsgs * msgs, |
---|
590 | uint32_t index, |
---|
591 | uint32_t offset, |
---|
592 | uint32_t length ) |
---|
593 | { |
---|
594 | const int req_max = msgs->maxActiveRequests; |
---|
595 | struct peer_request tmp, *req; |
---|
596 | |
---|
597 | assert( msgs != NULL ); |
---|
598 | assert( msgs->torrent != NULL ); |
---|
599 | assert( reqIsValid( msgs, index, offset, length ) ); |
---|
600 | |
---|
601 | /** |
---|
602 | *** Reasons to decline the request |
---|
603 | **/ |
---|
604 | |
---|
605 | /* don't send requests to choked clients */ |
---|
606 | if( msgs->info->clientIsChoked ) { |
---|
607 | dbgmsg( msgs, "declining request because they're choking us" ); |
---|
608 | return TR_ADDREQ_CLIENT_CHOKED; |
---|
609 | } |
---|
610 | |
---|
611 | /* peer doesn't have this piece */ |
---|
612 | if( !tr_bitfieldHas( msgs->info->have, index ) ) |
---|
613 | return TR_ADDREQ_MISSING; |
---|
614 | |
---|
615 | /* peer's queue is full */ |
---|
616 | if( tr_list_size( msgs->clientWillAskFor ) >= req_max ) { |
---|
617 | dbgmsg( msgs, "declining request because we're full" ); |
---|
618 | return TR_ADDREQ_FULL; |
---|
619 | } |
---|
620 | |
---|
621 | /* have we already asked for this piece? */ |
---|
622 | tmp.index = index; |
---|
623 | tmp.offset = offset; |
---|
624 | tmp.length = length; |
---|
625 | if( tr_list_find( msgs->clientAskedFor, &tmp, compareRequest ) ) { |
---|
626 | dbgmsg( msgs, "declining because it's a duplicate" ); |
---|
627 | return TR_ADDREQ_DUPLICATE; |
---|
628 | } |
---|
629 | if( tr_list_find( msgs->clientWillAskFor, &tmp, compareRequest ) ) { |
---|
630 | dbgmsg( msgs, "declining because it's a duplicate" ); |
---|
631 | return TR_ADDREQ_DUPLICATE; |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | *** Accept this request |
---|
636 | **/ |
---|
637 | |
---|
638 | dbgmsg( msgs, "added req for piece %d, offset %d", (int)index, (int)offset ); |
---|
639 | req = tr_new0( struct peer_request, 1 ); |
---|
640 | *req = tmp; |
---|
641 | tr_list_append( &msgs->clientWillAskFor, req ); |
---|
642 | pulse( msgs ); |
---|
643 | return TR_ADDREQ_OK; |
---|
644 | } |
---|
645 | |
---|
646 | static void |
---|
647 | cancelAllRequestsToPeer( tr_peermsgs * msgs ) |
---|
648 | { |
---|
649 | struct peer_request * req; |
---|
650 | |
---|
651 | while(( req = tr_list_pop_front( &msgs->clientWillAskFor ) )) |
---|
652 | { |
---|
653 | fireCancelledReq( msgs, req ); |
---|
654 | tr_free( req ); |
---|
655 | } |
---|
656 | |
---|
657 | while(( req = tr_list_pop_front( &msgs->clientAskedFor ) )) |
---|
658 | { |
---|
659 | fireCancelledReq( msgs, req ); |
---|
660 | protocolSendCancel( msgs, req ); |
---|
661 | tr_free( req ); |
---|
662 | } |
---|
663 | } |
---|
664 | |
---|
665 | void |
---|
666 | tr_peerMsgsCancel( tr_peermsgs * msgs, |
---|
667 | uint32_t pieceIndex, |
---|
668 | uint32_t offset, |
---|
669 | uint32_t length ) |
---|
670 | { |
---|
671 | struct peer_request *req, tmp; |
---|
672 | |
---|
673 | assert( msgs != NULL ); |
---|
674 | assert( length > 0 ); |
---|
675 | |
---|
676 | /* have we asked the peer for this piece? */ |
---|
677 | tmp.index = pieceIndex; |
---|
678 | tmp.offset = offset; |
---|
679 | tmp.length = length; |
---|
680 | |
---|
681 | /* if it's only in the queue and hasn't been sent yet, free it */ |
---|
682 | if(( req = tr_list_remove( &msgs->clientWillAskFor, &tmp, compareRequest ) )) |
---|
683 | { |
---|
684 | fireCancelledReq( msgs, req ); |
---|
685 | tr_free( req ); |
---|
686 | } |
---|
687 | |
---|
688 | /* if it's already been sent, send a cancel message too */ |
---|
689 | if(( req = tr_list_remove( &msgs->clientAskedFor, &tmp, compareRequest ) )) |
---|
690 | { |
---|
691 | protocolSendCancel( msgs, req ); |
---|
692 | fireCancelledReq( msgs, req ); |
---|
693 | tr_free( req ); |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | /** |
---|
698 | *** |
---|
699 | **/ |
---|
700 | |
---|
701 | static void |
---|
702 | sendLtepHandshake( tr_peermsgs * msgs ) |
---|
703 | { |
---|
704 | benc_val_t val, *m; |
---|
705 | char * buf; |
---|
706 | int len; |
---|
707 | int pex; |
---|
708 | const char * v = TR_NAME " " USERAGENT_PREFIX; |
---|
709 | const int port = tr_getPublicPort( msgs->handle ); |
---|
710 | struct evbuffer * outbuf; |
---|
711 | |
---|
712 | if( msgs->clientSentLtepHandshake ) |
---|
713 | return; |
---|
714 | |
---|
715 | outbuf = evbuffer_new( ); |
---|
716 | dbgmsg( msgs, "sending an ltep handshake" ); |
---|
717 | msgs->clientSentLtepHandshake = 1; |
---|
718 | |
---|
719 | /* decide if we want to advertise pex support */ |
---|
720 | if( !tr_torrentIsPexEnabled( msgs->torrent ) ) |
---|
721 | pex = 0; |
---|
722 | else if( msgs->peerSentLtepHandshake ) |
---|
723 | pex = msgs->peerSupportsPex ? 1 : 0; |
---|
724 | else |
---|
725 | pex = 1; |
---|
726 | |
---|
727 | tr_bencInit( &val, TYPE_DICT ); |
---|
728 | tr_bencDictReserve( &val, 4 ); |
---|
729 | tr_bencInitInt( tr_bencDictAdd( &val, "e" ), 1 ); |
---|
730 | m = tr_bencDictAdd( &val, "m" ); |
---|
731 | tr_bencInit( m, TYPE_DICT ); |
---|
732 | if( pex ) { |
---|
733 | tr_bencDictReserve( m, 1 ); |
---|
734 | tr_bencInitInt( tr_bencDictAdd( m, "ut_pex" ), OUR_LTEP_PEX ); |
---|
735 | } |
---|
736 | if( port > 0 ) |
---|
737 | tr_bencInitInt( tr_bencDictAdd( &val, "p" ), port ); |
---|
738 | tr_bencInitStr( tr_bencDictAdd( &val, "v" ), v, 0, 1 ); |
---|
739 | buf = tr_bencSave( &val, &len ); |
---|
740 | |
---|
741 | tr_peerIoWriteUint32( msgs->io, outbuf, 2*sizeof(uint8_t) + len ); |
---|
742 | tr_peerIoWriteUint8 ( msgs->io, outbuf, BT_LTEP ); |
---|
743 | tr_peerIoWriteUint8 ( msgs->io, outbuf, LTEP_HANDSHAKE ); |
---|
744 | tr_peerIoWriteBytes ( msgs->io, outbuf, buf, len ); |
---|
745 | |
---|
746 | tr_peerIoWriteBuf( msgs->io, outbuf ); |
---|
747 | |
---|
748 | #if 0 |
---|
749 | dbgmsg( msgs, "here is the ltep handshake we sent:" ); |
---|
750 | tr_bencPrint( &val ); |
---|
751 | dbgmsg( msgs, "here is the ltep handshake we read [%s]:", tr_bencSave( &val, NULL ) ); |
---|
752 | #endif |
---|
753 | |
---|
754 | /* cleanup */ |
---|
755 | tr_bencFree( &val ); |
---|
756 | tr_free( buf ); |
---|
757 | evbuffer_free( outbuf ); |
---|
758 | } |
---|
759 | |
---|
760 | static void |
---|
761 | parseLtepHandshake( tr_peermsgs * msgs, int len, struct evbuffer * inbuf ) |
---|
762 | { |
---|
763 | benc_val_t val, * sub; |
---|
764 | uint8_t * tmp = tr_new( uint8_t, len ); |
---|
765 | |
---|
766 | tr_peerIoReadBytes( msgs->io, inbuf, tmp, len ); |
---|
767 | msgs->peerSentLtepHandshake = 1; |
---|
768 | |
---|
769 | if( tr_bencLoad( tmp, len, &val, NULL ) || val.type!=TYPE_DICT ) { |
---|
770 | dbgmsg( msgs, "GET extended-handshake, couldn't get dictionary" ); |
---|
771 | tr_free( tmp ); |
---|
772 | return; |
---|
773 | } |
---|
774 | |
---|
775 | #if 0 |
---|
776 | dbgmsg( msgs, "here is the ltep handshake we read:" ); |
---|
777 | tr_bencPrint( &val ); |
---|
778 | dbgmsg( msgs, "here is the ltep handshake we read [%s]:", tr_bencSave( &val, NULL ) ); |
---|
779 | #endif |
---|
780 | |
---|
781 | /* does the peer prefer encrypted connections? */ |
---|
782 | sub = tr_bencDictFind( &val, "e" ); |
---|
783 | if( tr_bencIsInt( sub ) ) |
---|
784 | msgs->info->encryption_preference = sub->val.i |
---|
785 | ? ENCRYPTION_PREFERENCE_YES |
---|
786 | : ENCRYPTION_PREFERENCE_NO; |
---|
787 | |
---|
788 | /* check supported messages for utorrent pex */ |
---|
789 | sub = tr_bencDictFind( &val, "m" ); |
---|
790 | if( tr_bencIsDict( sub ) ) { |
---|
791 | sub = tr_bencDictFind( sub, "ut_pex" ); |
---|
792 | if( tr_bencIsInt( sub ) ) { |
---|
793 | msgs->peerSupportsPex = 1; |
---|
794 | msgs->ut_pex_id = (uint8_t) sub->val.i; |
---|
795 | dbgmsg( msgs, "msgs->ut_pex is %d", (int)msgs->ut_pex_id ); |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | /* get peer's listening port */ |
---|
800 | sub = tr_bencDictFind( &val, "p" ); |
---|
801 | if( tr_bencIsInt( sub ) ) { |
---|
802 | msgs->info->port = htons( (uint16_t)sub->val.i ); |
---|
803 | dbgmsg( msgs, "msgs->port is now %hu", msgs->info->port ); |
---|
804 | } |
---|
805 | |
---|
806 | tr_bencFree( &val ); |
---|
807 | tr_free( tmp ); |
---|
808 | } |
---|
809 | |
---|
810 | static void |
---|
811 | parseUtPex( tr_peermsgs * msgs, int msglen, struct evbuffer * inbuf ) |
---|
812 | { |
---|
813 | benc_val_t val, * sub; |
---|
814 | uint8_t * tmp; |
---|
815 | |
---|
816 | if( !tr_torrentIsPexEnabled( msgs->torrent ) ) /* no sharing! */ |
---|
817 | return; |
---|
818 | |
---|
819 | tmp = tr_new( uint8_t, msglen ); |
---|
820 | tr_peerIoReadBytes( msgs->io, inbuf, tmp, msglen ); |
---|
821 | |
---|
822 | if( tr_bencLoad( tmp, msglen, &val, NULL ) || !tr_bencIsDict( &val ) ) { |
---|
823 | dbgmsg( msgs, "GET can't read extended-pex dictionary" ); |
---|
824 | tr_free( tmp ); |
---|
825 | return; |
---|
826 | } |
---|
827 | |
---|
828 | sub = tr_bencDictFind( &val, "added" ); |
---|
829 | if( tr_bencIsStr(sub) && ((sub->val.s.i % 6) == 0)) { |
---|
830 | const int n = sub->val.s.i / 6 ; |
---|
831 | dbgmsg( msgs, "got %d peers from uT pex", n ); |
---|
832 | tr_peerMgrAddPeers( msgs->handle->peerMgr, |
---|
833 | msgs->torrent->info.hash, |
---|
834 | TR_PEER_FROM_PEX, |
---|
835 | (uint8_t*)sub->val.s.s, n ); |
---|
836 | } |
---|
837 | |
---|
838 | tr_bencFree( &val ); |
---|
839 | tr_free( tmp ); |
---|
840 | } |
---|
841 | |
---|
842 | static void |
---|
843 | sendPex( tr_peermsgs * msgs ); |
---|
844 | |
---|
845 | static void |
---|
846 | parseLtep( tr_peermsgs * msgs, int msglen, struct evbuffer * inbuf ) |
---|
847 | { |
---|
848 | uint8_t ltep_msgid; |
---|
849 | |
---|
850 | tr_peerIoReadUint8( msgs->io, inbuf, <ep_msgid ); |
---|
851 | msglen--; |
---|
852 | |
---|
853 | if( ltep_msgid == LTEP_HANDSHAKE ) |
---|
854 | { |
---|
855 | dbgmsg( msgs, "got ltep handshake" ); |
---|
856 | parseLtepHandshake( msgs, msglen, inbuf ); |
---|
857 | if( tr_peerIoSupportsLTEP( msgs->io ) ) |
---|
858 | { |
---|
859 | sendLtepHandshake( msgs ); |
---|
860 | sendPex( msgs ); |
---|
861 | } |
---|
862 | } |
---|
863 | else if( ltep_msgid == msgs->ut_pex_id ) |
---|
864 | { |
---|
865 | dbgmsg( msgs, "got ut pex" ); |
---|
866 | msgs->peerSupportsPex = 1; |
---|
867 | parseUtPex( msgs, msglen, inbuf ); |
---|
868 | } |
---|
869 | else |
---|
870 | { |
---|
871 | dbgmsg( msgs, "skipping unknown ltep message (%d)", (int)ltep_msgid ); |
---|
872 | evbuffer_drain( inbuf, msglen ); |
---|
873 | } |
---|
874 | } |
---|
875 | |
---|
876 | static int |
---|
877 | readBtLength( tr_peermsgs * msgs, struct evbuffer * inbuf ) |
---|
878 | { |
---|
879 | uint32_t len; |
---|
880 | |
---|
881 | if( EVBUFFER_LENGTH(inbuf) < sizeof(len) ) |
---|
882 | return READ_MORE; |
---|
883 | |
---|
884 | tr_peerIoReadUint32( msgs->io, inbuf, &len ); |
---|
885 | |
---|
886 | if( len == 0 ) /* peer sent us a keepalive message */ |
---|
887 | dbgmsg( msgs, "got KeepAlive" ); |
---|
888 | else { |
---|
889 | msgs->incoming.length = len; |
---|
890 | msgs->state = AWAITING_BT_ID; |
---|
891 | } |
---|
892 | dbgmsg( msgs, "readBtLength: got a length of %d, msgs->state is now %d", (int)len, (int)msgs->state ); |
---|
893 | |
---|
894 | return READ_AGAIN; |
---|
895 | } |
---|
896 | |
---|
897 | static int |
---|
898 | readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf ); |
---|
899 | |
---|
900 | static int |
---|
901 | readBtId( tr_peermsgs * msgs, struct evbuffer * inbuf ) |
---|
902 | { |
---|
903 | uint8_t id; |
---|
904 | |
---|
905 | if( EVBUFFER_LENGTH(inbuf) < sizeof(uint8_t) ) |
---|
906 | return READ_MORE; |
---|
907 | |
---|
908 | tr_peerIoReadUint8( msgs->io, inbuf, &id ); |
---|
909 | msgs->incoming.id = id; |
---|
910 | |
---|
911 | if( id==BT_PIECE ) |
---|
912 | { |
---|
913 | msgs->state = AWAITING_BT_PIECE; |
---|
914 | return READ_AGAIN; |
---|
915 | } |
---|
916 | else if( msgs->incoming.length != 1 ) |
---|
917 | { |
---|
918 | msgs->state = AWAITING_BT_MESSAGE; |
---|
919 | return READ_AGAIN; |
---|
920 | } |
---|
921 | else return readBtMessage( msgs, inbuf ); |
---|
922 | } |
---|
923 | |
---|
924 | static void |
---|
925 | updatePeerProgress( tr_peermsgs * msgs ) |
---|
926 | { |
---|
927 | msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; |
---|
928 | dbgmsg( msgs, "peer progress is %f", msgs->info->progress ); |
---|
929 | updateInterest( msgs ); |
---|
930 | firePeerProgress( msgs ); |
---|
931 | } |
---|
932 | |
---|
933 | static int |
---|
934 | clientCanSendFastBlock( const tr_peermsgs * msgs UNUSED ) |
---|
935 | { |
---|
936 | /* We can't send a fast piece if a peer has more than MAX_FAST_ALLOWED_THRESHOLD pieces */ |
---|
937 | if( tr_bitfieldCountTrueBits( msgs->info->have ) > MAX_FAST_ALLOWED_THRESHOLD ) |
---|
938 | return FALSE; |
---|
939 | |
---|
940 | /* ...or if we don't have ourself enough pieces */ |
---|
941 | if( tr_bitfieldCountTrueBits( tr_cpPieceBitfield( msgs->torrent->completion ) ) < MAX_FAST_ALLOWED_THRESHOLD ) |
---|
942 | return FALSE; |
---|
943 | |
---|
944 | /* Maybe a bandwidth limit ? */ |
---|
945 | return TRUE; |
---|
946 | } |
---|
947 | |
---|
948 | static void |
---|
949 | peerMadeRequest( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
950 | { |
---|
951 | const int reqIsValid = requestIsValid( msgs, req ); |
---|
952 | const int clientHasPiece = reqIsValid && tr_cpPieceIsComplete( msgs->torrent->completion, req->index ); |
---|
953 | const int peerIsChoked = msgs->info->peerIsChoked; |
---|
954 | const int peerIsFast = tr_peerIoSupportsFEXT( msgs->io ); |
---|
955 | const int pieceIsFast = reqIsValid && tr_bitfieldHas( msgs->peerAllowedPieces, req->index ); |
---|
956 | const int canSendFast = clientCanSendFastBlock( msgs ); |
---|
957 | |
---|
958 | if( !reqIsValid ) /* bad request */ |
---|
959 | { |
---|
960 | dbgmsg( msgs, "rejecting an invalid request." ); |
---|
961 | sendFastReject( msgs, req->index, req->offset, req->length ); |
---|
962 | } |
---|
963 | else if( !clientHasPiece ) /* we don't have it */ |
---|
964 | { |
---|
965 | dbgmsg( msgs, "rejecting request for a piece we don't have." ); |
---|
966 | sendFastReject( msgs, req->index, req->offset, req->length ); |
---|
967 | } |
---|
968 | else if( peerIsChoked && !peerIsFast ) /* doesn't he know he's choked? */ |
---|
969 | { |
---|
970 | tr_peerMsgsSetChoke( msgs, 1 ); |
---|
971 | sendFastReject( msgs, req->index, req->offset, req->length ); |
---|
972 | } |
---|
973 | else if( peerIsChoked && peerIsFast && ( !pieceIsFast || !canSendFast ) ) |
---|
974 | { |
---|
975 | sendFastReject( msgs, req->index, req->offset, req->length ); |
---|
976 | } |
---|
977 | else /* YAY */ |
---|
978 | { |
---|
979 | struct peer_request * tmp = tr_new( struct peer_request, 1 ); |
---|
980 | *tmp = *req; |
---|
981 | if( peerIsFast && pieceIsFast ) |
---|
982 | tr_list_append( &msgs->peerAskedForFast, tmp ); |
---|
983 | else |
---|
984 | tr_list_append( &msgs->peerAskedFor, tmp ); |
---|
985 | } |
---|
986 | } |
---|
987 | |
---|
988 | static int |
---|
989 | messageLengthIsCorrect( const tr_peermsgs * msg, uint8_t id, uint32_t len ) |
---|
990 | { |
---|
991 | switch( id ) |
---|
992 | { |
---|
993 | case BT_CHOKE: |
---|
994 | case BT_UNCHOKE: |
---|
995 | case BT_INTERESTED: |
---|
996 | case BT_NOT_INTERESTED: |
---|
997 | case BT_HAVE_ALL: |
---|
998 | case BT_HAVE_NONE: |
---|
999 | return len==1; |
---|
1000 | |
---|
1001 | case BT_HAVE: |
---|
1002 | case BT_SUGGEST: |
---|
1003 | case BT_ALLOWED_FAST: |
---|
1004 | return len==5; |
---|
1005 | |
---|
1006 | case BT_BITFIELD: |
---|
1007 | return len == (msg->torrent->info.pieceCount+7u)/8u + 1u; |
---|
1008 | |
---|
1009 | case BT_REQUEST: |
---|
1010 | case BT_CANCEL: |
---|
1011 | case BT_REJECT: |
---|
1012 | return len==13; |
---|
1013 | |
---|
1014 | case BT_PIECE: |
---|
1015 | return len>9 && len<=16393; |
---|
1016 | |
---|
1017 | case BT_PORT: |
---|
1018 | return len==3; |
---|
1019 | |
---|
1020 | case BT_LTEP: |
---|
1021 | return len >= 2; |
---|
1022 | |
---|
1023 | default: |
---|
1024 | return FALSE; |
---|
1025 | } |
---|
1026 | } |
---|
1027 | |
---|
1028 | static int |
---|
1029 | clientGotBlock( tr_peermsgs * msgs, const uint8_t * block, const struct peer_request * req ); |
---|
1030 | |
---|
1031 | static int |
---|
1032 | readBtPiece( tr_peermsgs * msgs, struct evbuffer * inbuf ) |
---|
1033 | { |
---|
1034 | struct peer_request * req = &msgs->incoming.blockReq; |
---|
1035 | dbgmsg( msgs, "In readBtPiece" ); |
---|
1036 | |
---|
1037 | if( !req->length ) |
---|
1038 | { |
---|
1039 | if( EVBUFFER_LENGTH( inbuf ) < 8 ) |
---|
1040 | return READ_MORE; |
---|
1041 | |
---|
1042 | tr_peerIoReadUint32( msgs->io, inbuf, &req->index ); |
---|
1043 | tr_peerIoReadUint32( msgs->io, inbuf, &req->offset ); |
---|
1044 | req->length = msgs->incoming.length - 9; |
---|
1045 | dbgmsg( msgs, "got incoming block header %u:%u->%u", req->index, req->offset, req->length ); |
---|
1046 | return READ_AGAIN; |
---|
1047 | } |
---|
1048 | else |
---|
1049 | { |
---|
1050 | int err; |
---|
1051 | |
---|
1052 | /* read in another chunk of data */ |
---|
1053 | const size_t nLeft = req->length - EVBUFFER_LENGTH(msgs->incoming.block); |
---|
1054 | size_t n = MIN( nLeft, EVBUFFER_LENGTH(inbuf) ); |
---|
1055 | uint8_t * buf = tr_new( uint8_t, n ); |
---|
1056 | tr_peerIoReadBytes( msgs->io, inbuf, buf, n ); |
---|
1057 | evbuffer_add( msgs->incoming.block, buf, n ); |
---|
1058 | tr_free( buf ); |
---|
1059 | dbgmsg( msgs, "got %d bytes for block %u:%u->%u ... %d remain", |
---|
1060 | (int)n, req->index, req->offset, req->length, |
---|
1061 | (int)( req->length - EVBUFFER_LENGTH(msgs->incoming.block) ) ); |
---|
1062 | if( EVBUFFER_LENGTH(msgs->incoming.block) < req->length ) |
---|
1063 | return READ_MORE; |
---|
1064 | |
---|
1065 | /* we've got the whole block ... process it */ |
---|
1066 | err = clientGotBlock( msgs, EVBUFFER_DATA(msgs->incoming.block), req ); |
---|
1067 | |
---|
1068 | /* cleanup */ |
---|
1069 | evbuffer_drain( msgs->incoming.block, EVBUFFER_LENGTH(msgs->incoming.block) ); |
---|
1070 | req->length = 0; |
---|
1071 | msgs->state = AWAITING_BT_LENGTH; |
---|
1072 | if( !err ) |
---|
1073 | return READ_AGAIN; |
---|
1074 | else { |
---|
1075 | fireGotAssertError( msgs ); |
---|
1076 | return READ_DONE; |
---|
1077 | } |
---|
1078 | } |
---|
1079 | } |
---|
1080 | |
---|
1081 | static int |
---|
1082 | readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf ) |
---|
1083 | { |
---|
1084 | uint32_t ui32; |
---|
1085 | uint32_t msglen = msgs->incoming.length; |
---|
1086 | const uint8_t id = msgs->incoming.id; |
---|
1087 | const size_t startBufLen = EVBUFFER_LENGTH( inbuf ); |
---|
1088 | |
---|
1089 | dbgmsg( msgs, "in readBtMessage" ); |
---|
1090 | |
---|
1091 | --msglen; // id length |
---|
1092 | |
---|
1093 | if( EVBUFFER_LENGTH(inbuf) < msglen ) { |
---|
1094 | dbgmsg( msgs, " too short!!! " ); |
---|
1095 | return READ_MORE; |
---|
1096 | } |
---|
1097 | |
---|
1098 | dbgmsg( msgs, "got BT id %d, len %d, buffer size is %d", (int)id, (int)msglen, (int)EVBUFFER_LENGTH(inbuf) ); |
---|
1099 | |
---|
1100 | if( !messageLengthIsCorrect( msgs, id, msglen+1 ) ) |
---|
1101 | { |
---|
1102 | dbgmsg( msgs, "bad packet - BT message #%d with a length of %d", (int)id, (int)msglen ); |
---|
1103 | fireGotError( msgs ); |
---|
1104 | return READ_DONE; |
---|
1105 | } |
---|
1106 | |
---|
1107 | switch( id ) |
---|
1108 | { |
---|
1109 | case BT_CHOKE: |
---|
1110 | dbgmsg( msgs, "got Choke" ); |
---|
1111 | msgs->info->clientIsChoked = 1; |
---|
1112 | cancelAllRequestsToPeer( msgs ); |
---|
1113 | cancelAllRequestsToClientExceptFast( msgs ); |
---|
1114 | break; |
---|
1115 | |
---|
1116 | case BT_UNCHOKE: |
---|
1117 | dbgmsg( msgs, "got Unchoke" ); |
---|
1118 | msgs->info->clientIsChoked = 0; |
---|
1119 | fireNeedReq( msgs ); |
---|
1120 | break; |
---|
1121 | |
---|
1122 | case BT_INTERESTED: |
---|
1123 | dbgmsg( msgs, "got Interested" ); |
---|
1124 | msgs->info->peerIsInterested = 1; |
---|
1125 | tr_peerMsgsSetChoke( msgs, 0 ); |
---|
1126 | break; |
---|
1127 | |
---|
1128 | case BT_NOT_INTERESTED: |
---|
1129 | dbgmsg( msgs, "got Not Interested" ); |
---|
1130 | msgs->info->peerIsInterested = 0; |
---|
1131 | break; |
---|
1132 | |
---|
1133 | case BT_HAVE: |
---|
1134 | tr_peerIoReadUint32( msgs->io, inbuf, &ui32 ); |
---|
1135 | dbgmsg( msgs, "got Have: %u", ui32 ); |
---|
1136 | tr_bitfieldAdd( msgs->info->have, ui32 ); |
---|
1137 | /* If this is a fast-allowed piece for this peer, mark it as normal now */ |
---|
1138 | if( msgs->clientAllowedPieces != NULL && tr_bitfieldHas( msgs->clientAllowedPieces, ui32 ) ) |
---|
1139 | tr_bitfieldRem( msgs->clientAllowedPieces, ui32 ); |
---|
1140 | updatePeerProgress( msgs ); |
---|
1141 | tr_rcTransferred( msgs->torrent->swarmspeed, msgs->torrent->info.pieceSize ); |
---|
1142 | break; |
---|
1143 | |
---|
1144 | case BT_BITFIELD: { |
---|
1145 | const int clientIsSeed = tr_torrentIsSeed( msgs->torrent ); |
---|
1146 | dbgmsg( msgs, "got a bitfield" ); |
---|
1147 | tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen ); |
---|
1148 | updatePeerProgress( msgs ); |
---|
1149 | tr_peerMsgsSetChoke( msgs, !clientIsSeed || (msgs->info->progress<1.0) ); |
---|
1150 | fireNeedReq( msgs ); |
---|
1151 | break; |
---|
1152 | } |
---|
1153 | |
---|
1154 | case BT_REQUEST: { |
---|
1155 | struct peer_request req; |
---|
1156 | tr_peerIoReadUint32( msgs->io, inbuf, &req.index ); |
---|
1157 | tr_peerIoReadUint32( msgs->io, inbuf, &req.offset ); |
---|
1158 | tr_peerIoReadUint32( msgs->io, inbuf, &req.length ); |
---|
1159 | dbgmsg( msgs, "got Request: %u:%u->%u", req.index, req.offset, req.length ); |
---|
1160 | peerMadeRequest( msgs, &req ); |
---|
1161 | break; |
---|
1162 | } |
---|
1163 | |
---|
1164 | case BT_CANCEL: { |
---|
1165 | struct peer_request req; |
---|
1166 | tr_peerIoReadUint32( msgs->io, inbuf, &req.index ); |
---|
1167 | tr_peerIoReadUint32( msgs->io, inbuf, &req.offset ); |
---|
1168 | tr_peerIoReadUint32( msgs->io, inbuf, &req.length ); |
---|
1169 | dbgmsg( msgs, "got a Cancel %u:%u->%u", req.index, req.offset, req.length ); |
---|
1170 | tr_free( tr_list_remove( &msgs->peerAskedForFast, &req, compareRequest ) ); |
---|
1171 | tr_free( tr_list_remove( &msgs->peerAskedFor, &req, compareRequest ) ); |
---|
1172 | break; |
---|
1173 | } |
---|
1174 | |
---|
1175 | case BT_PIECE: |
---|
1176 | assert( 0 ); /* should be handled elsewhere! */ |
---|
1177 | break; |
---|
1178 | |
---|
1179 | case BT_PORT: |
---|
1180 | dbgmsg( msgs, "Got a BT_PORT" ); |
---|
1181 | tr_peerIoReadUint16( msgs->io, inbuf, &msgs->info->port ); |
---|
1182 | break; |
---|
1183 | |
---|
1184 | case BT_SUGGEST: { |
---|
1185 | dbgmsg( msgs, "Got a BT_SUGGEST" ); |
---|
1186 | tr_peerIoReadUint32( msgs->io, inbuf, &ui32 ); |
---|
1187 | const tr_bitfield * bt = tr_cpPieceBitfield( msgs->torrent->completion ); |
---|
1188 | if( tr_bitfieldHas( bt, ui32 ) ) |
---|
1189 | tr_bitfieldAdd( msgs->clientSuggestedPieces, ui32 ); |
---|
1190 | break; |
---|
1191 | } |
---|
1192 | |
---|
1193 | case BT_HAVE_ALL: |
---|
1194 | dbgmsg( msgs, "Got a BT_HAVE_ALL" ); |
---|
1195 | tr_bitfieldAddRange( msgs->info->have, 0, msgs->torrent->info.pieceCount ); |
---|
1196 | updatePeerProgress( msgs ); |
---|
1197 | break; |
---|
1198 | |
---|
1199 | |
---|
1200 | case BT_HAVE_NONE: |
---|
1201 | dbgmsg( msgs, "Got a BT_HAVE_NONE" ); |
---|
1202 | tr_bitfieldClear( msgs->info->have ); |
---|
1203 | updatePeerProgress( msgs ); |
---|
1204 | break; |
---|
1205 | |
---|
1206 | case BT_REJECT: { |
---|
1207 | struct peer_request req; |
---|
1208 | dbgmsg( msgs, "Got a BT_REJECT" ); |
---|
1209 | tr_peerIoReadUint32( msgs->io, inbuf, &req.index ); |
---|
1210 | tr_peerIoReadUint32( msgs->io, inbuf, &req.offset ); |
---|
1211 | tr_peerIoReadUint32( msgs->io, inbuf, &req.length ); |
---|
1212 | tr_free( tr_list_remove( &msgs->clientAskedFor, &req, compareRequest ) ); |
---|
1213 | break; |
---|
1214 | } |
---|
1215 | |
---|
1216 | case BT_ALLOWED_FAST: { |
---|
1217 | dbgmsg( msgs, "Got a BT_ALLOWED_FAST" ); |
---|
1218 | tr_peerIoReadUint32( msgs->io, inbuf, &ui32 ); |
---|
1219 | tr_bitfieldAdd( msgs->clientAllowedPieces, ui32 ); |
---|
1220 | break; |
---|
1221 | } |
---|
1222 | |
---|
1223 | case BT_LTEP: |
---|
1224 | dbgmsg( msgs, "Got a BT_LTEP" ); |
---|
1225 | parseLtep( msgs, msglen, inbuf ); |
---|
1226 | break; |
---|
1227 | |
---|
1228 | default: |
---|
1229 | dbgmsg( msgs, "peer sent us an UNKNOWN: %d", (int)id ); |
---|
1230 | tr_peerIoDrain( msgs->io, inbuf, msglen ); |
---|
1231 | break; |
---|
1232 | } |
---|
1233 | |
---|
1234 | dbgmsg( msgs, "startBufLen was %d, msglen was %d, current inbuf len is %d", (int)startBufLen, (int)(msglen+1), (int)EVBUFFER_LENGTH(inbuf) ); |
---|
1235 | |
---|
1236 | assert( msglen + 1 == msgs->incoming.length ); |
---|
1237 | assert( EVBUFFER_LENGTH(inbuf) == startBufLen - msglen ); |
---|
1238 | |
---|
1239 | msgs->state = AWAITING_BT_LENGTH; |
---|
1240 | return READ_AGAIN; |
---|
1241 | } |
---|
1242 | |
---|
1243 | static void |
---|
1244 | clientGotBytes( tr_peermsgs * msgs, uint32_t byteCount ) |
---|
1245 | { |
---|
1246 | tr_torrent * tor = msgs->torrent; |
---|
1247 | tor->activityDate = tr_date( ); |
---|
1248 | tor->downloadedCur += byteCount; |
---|
1249 | msgs->info->pieceDataActivityDate = time( NULL ); |
---|
1250 | tr_rcTransferred( msgs->info->rcToClient, byteCount ); |
---|
1251 | tr_rcTransferred( tor->download, byteCount ); |
---|
1252 | tr_rcTransferred( tor->handle->download, byteCount ); |
---|
1253 | } |
---|
1254 | |
---|
1255 | static void |
---|
1256 | peerGotBytes( tr_peermsgs * msgs, uint32_t byteCount ) |
---|
1257 | { |
---|
1258 | tr_torrent * tor = msgs->torrent; |
---|
1259 | tor->activityDate = tr_date( ); |
---|
1260 | tor->uploadedCur += byteCount; |
---|
1261 | msgs->info->pieceDataActivityDate = time( NULL ); |
---|
1262 | tr_rcTransferred( msgs->info->rcToPeer, byteCount ); |
---|
1263 | tr_rcTransferred( tor->upload, byteCount ); |
---|
1264 | tr_rcTransferred( tor->handle->upload, byteCount ); |
---|
1265 | } |
---|
1266 | |
---|
1267 | static int |
---|
1268 | canDownload( const tr_peermsgs * msgs ) |
---|
1269 | { |
---|
1270 | tr_torrent * tor = msgs->torrent; |
---|
1271 | |
---|
1272 | if( tor->downloadLimitMode == TR_SPEEDLIMIT_GLOBAL ) |
---|
1273 | return !tor->handle->useDownloadLimit || tr_rcCanTransfer( tor->handle->download ); |
---|
1274 | |
---|
1275 | if( tor->downloadLimitMode == TR_SPEEDLIMIT_SINGLE ) |
---|
1276 | return tr_rcCanTransfer( tor->download ); |
---|
1277 | |
---|
1278 | return TRUE; |
---|
1279 | } |
---|
1280 | |
---|
1281 | static void |
---|
1282 | reassignBytesToCorrupt( tr_peermsgs * msgs, uint32_t byteCount ) |
---|
1283 | { |
---|
1284 | tr_torrent * tor = msgs->torrent; |
---|
1285 | |
---|
1286 | /* increment the `corrupt' field */ |
---|
1287 | tor->corruptCur += byteCount; |
---|
1288 | |
---|
1289 | /* decrement the `downloaded' field */ |
---|
1290 | if( tor->downloadedCur >= byteCount ) |
---|
1291 | tor->downloadedCur -= byteCount; |
---|
1292 | else |
---|
1293 | tor->downloadedCur = 0; |
---|
1294 | } |
---|
1295 | |
---|
1296 | |
---|
1297 | static void |
---|
1298 | gotBadPiece( tr_peermsgs * msgs, uint32_t pieceIndex ) |
---|
1299 | { |
---|
1300 | const uint32_t byteCount = tr_torPieceCountBytes( msgs->torrent, (int)pieceIndex ); |
---|
1301 | reassignBytesToCorrupt( msgs, byteCount ); |
---|
1302 | } |
---|
1303 | |
---|
1304 | static void |
---|
1305 | clientGotUnwantedBlock( tr_peermsgs * msgs, const struct peer_request * req ) |
---|
1306 | { |
---|
1307 | reassignBytesToCorrupt( msgs, req->length ); |
---|
1308 | } |
---|
1309 | |
---|
1310 | static void |
---|
1311 | addPeerToBlamefield( tr_peermsgs * msgs, uint32_t index ) |
---|
1312 | { |
---|
1313 | if( !msgs->info->blame ) |
---|
1314 | msgs->info->blame = tr_bitfieldNew( msgs->torrent->info.pieceCount ); |
---|
1315 | tr_bitfieldAdd( msgs->info->blame, index ); |
---|
1316 | } |
---|
1317 | |
---|
1318 | static int |
---|
1319 | clientGotBlock( tr_peermsgs * msgs, const uint8_t * data, const struct peer_request * req ) |
---|
1320 | { |
---|
1321 | int i; |
---|
1322 | tr_torrent * tor = msgs->torrent; |
---|
1323 | const int block = _tr_block( tor, req->index, req->offset ); |
---|
1324 | struct peer_request *myreq; |
---|
1325 | |
---|
1326 | assert( msgs != NULL ); |
---|
1327 | assert( req != NULL ); |
---|
1328 | |
---|
1329 | if( req->length != (uint32_t)tr_torBlockCountBytes( msgs->torrent, block ) ) |
---|
1330 | { |
---|
1331 | dbgmsg( msgs, "wrong block size -- expected %u, got %d", |
---|
1332 | tr_torBlockCountBytes( msgs->torrent, block ), req->length ); |
---|
1333 | return TR_ERROR_ASSERT; |
---|
1334 | } |
---|
1335 | |
---|
1336 | /* save the block */ |
---|
1337 | dbgmsg( msgs, "got block %u:%u->%u", req->index, req->offset, req->length ); |
---|
1338 | |
---|
1339 | /** |
---|
1340 | *** Remove the block from our `we asked for this' list |
---|
1341 | **/ |
---|
1342 | |
---|
1343 | myreq = tr_list_remove( &msgs->clientAskedFor, req, compareRequest ); |
---|
1344 | if( myreq == NULL ) { |
---|
1345 | clientGotUnwantedBlock( msgs, req ); |
---|
1346 | dbgmsg( msgs, "we didn't ask for this message..." ); |
---|
1347 | return 0; |
---|
1348 | } |
---|
1349 | |
---|
1350 | dbgmsg( msgs, "got block %u:%u->%u (turnaround time %d secs)", |
---|
1351 | myreq->index, myreq->offset, myreq->length, |
---|
1352 | (int)(time(NULL) - myreq->time_requested) ); |
---|
1353 | dbgmsg( msgs, "peer has %d more blocks we've asked for", |
---|
1354 | tr_list_size(msgs->clientAskedFor)); |
---|
1355 | |
---|
1356 | tr_free( myreq ); |
---|
1357 | myreq = NULL; |
---|
1358 | |
---|
1359 | /** |
---|
1360 | *** Error checks |
---|
1361 | **/ |
---|
1362 | |
---|
1363 | if( tr_cpBlockIsComplete( tor->completion, block ) ) { |
---|
1364 | dbgmsg( msgs, "we have this block already..." ); |
---|
1365 | clientGotUnwantedBlock( msgs, req ); |
---|
1366 | return 0; |
---|
1367 | } |
---|
1368 | |
---|
1369 | /** |
---|
1370 | *** Save the block |
---|
1371 | **/ |
---|
1372 | |
---|
1373 | msgs->info->peerSentPieceDataAt = time( NULL ); |
---|
1374 | clientGotBytes( msgs, req->length ); |
---|
1375 | i = tr_ioWrite( tor, req->index, req->offset, req->length, data ); |
---|
1376 | if( i ) |
---|
1377 | return 0; |
---|
1378 | |
---|
1379 | tr_cpBlockAdd( tor->completion, block ); |
---|
1380 | |
---|
1381 | addPeerToBlamefield( msgs, req->index ); |
---|
1382 | |
---|
1383 | fireGotBlock( msgs, req ); |
---|
1384 | |
---|
1385 | /** |
---|
1386 | *** Handle if this was the last block in the piece |
---|
1387 | **/ |
---|
1388 | |
---|
1389 | if( tr_cpPieceIsComplete( tor->completion, req->index ) ) |
---|
1390 | { |
---|
1391 | if( tr_ioHash( tor, req->index ) ) |
---|
1392 | { |
---|
1393 | gotBadPiece( msgs, req->index ); |
---|
1394 | return 0; |
---|
1395 | } |
---|
1396 | |
---|
1397 | fireClientHave( msgs, req->index ); |
---|
1398 | } |
---|
1399 | |
---|
1400 | return 0; |
---|
1401 | } |
---|
1402 | |
---|
1403 | static ReadState |
---|
1404 | canRead( struct bufferevent * evin, void * vmsgs ) |
---|
1405 | { |
---|
1406 | ReadState ret; |
---|
1407 | tr_peermsgs * msgs = (tr_peermsgs *) vmsgs; |
---|
1408 | struct evbuffer * inbuf = EVBUFFER_INPUT ( evin ); |
---|
1409 | |
---|
1410 | dbgmsg( msgs, "canRead, state is %d", msgs->state ); |
---|
1411 | if( !canDownload( msgs ) ) |
---|
1412 | { |
---|
1413 | dbgmsg( msgs, "oh, but canDownload failed" ); |
---|
1414 | msgs->notListening = 1; |
---|
1415 | ret = READ_DONE; |
---|
1416 | } |
---|
1417 | else switch( msgs->state ) |
---|
1418 | { |
---|
1419 | case AWAITING_BT_LENGTH: ret = readBtLength ( msgs, inbuf ); break; |
---|
1420 | case AWAITING_BT_ID: ret = readBtId ( msgs, inbuf ); break; |
---|
1421 | case AWAITING_BT_MESSAGE: ret = readBtMessage ( msgs, inbuf ); break; |
---|
1422 | case AWAITING_BT_PIECE: ret = readBtPiece ( msgs, inbuf ); break; |
---|
1423 | default: assert( 0 ); |
---|
1424 | } |
---|
1425 | |
---|
1426 | return ret; |
---|
1427 | } |
---|
1428 | |
---|
1429 | static void |
---|
1430 | sendKeepalive( tr_peermsgs * msgs ) |
---|
1431 | { |
---|
1432 | dbgmsg( msgs, "sending a keepalive message" ); |
---|
1433 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 0 ); |
---|
1434 | } |
---|
1435 | |
---|
1436 | /** |
---|
1437 | *** |
---|
1438 | **/ |
---|
1439 | |
---|
1440 | static int |
---|
1441 | canWrite( const tr_peermsgs * msgs ) |
---|
1442 | { |
---|
1443 | /* don't let our outbuffer get too large */ |
---|
1444 | if( tr_peerIoWriteBytesWaiting( msgs->io ) > 4096 ) |
---|
1445 | return FALSE; |
---|
1446 | |
---|
1447 | return TRUE; |
---|
1448 | } |
---|
1449 | |
---|
1450 | static size_t |
---|
1451 | getUploadMax( const tr_peermsgs * msgs ) |
---|
1452 | { |
---|
1453 | static const size_t maxval = ~0; |
---|
1454 | const tr_torrent * tor = msgs->torrent; |
---|
1455 | |
---|
1456 | if( !canWrite( msgs ) ) |
---|
1457 | return 0; |
---|
1458 | |
---|
1459 | if( tor->uploadLimitMode == TR_SPEEDLIMIT_GLOBAL ) |
---|
1460 | return tor->handle->useUploadLimit |
---|
1461 | ? tr_rcBytesLeft( tor->handle->upload ) : maxval; |
---|
1462 | |
---|
1463 | if( tor->uploadLimitMode == TR_SPEEDLIMIT_SINGLE ) |
---|
1464 | return tr_rcBytesLeft( tor->upload ); |
---|
1465 | |
---|
1466 | return maxval; |
---|
1467 | } |
---|
1468 | |
---|
1469 | static int |
---|
1470 | ratePulse( void * vmsgs ) |
---|
1471 | { |
---|
1472 | tr_peermsgs * msgs = (tr_peermsgs *) vmsgs; |
---|
1473 | msgs->info->rateToClient = tr_rcRate( msgs->info->rcToClient ); |
---|
1474 | msgs->info->rateToPeer = tr_rcRate( msgs->info->rcToPeer ); |
---|
1475 | msgs->maxActiveRequests = MIN( 8 + (int)(msgs->info->rateToClient/5), 100 ); |
---|
1476 | msgs->minActiveRequests = msgs->maxActiveRequests / 3; |
---|
1477 | return TRUE; |
---|
1478 | } |
---|
1479 | |
---|
1480 | static struct peer_request* |
---|
1481 | popNextRequest( tr_peermsgs * msgs ) |
---|
1482 | { |
---|
1483 | struct peer_request * ret; |
---|
1484 | ret = tr_list_pop_front( &msgs->peerAskedForFast ); |
---|
1485 | if( !ret ) |
---|
1486 | ret = tr_list_pop_front( &msgs->peerAskedFor); |
---|
1487 | return ret; |
---|
1488 | } |
---|
1489 | |
---|
1490 | static int |
---|
1491 | pulse( void * vmsgs ) |
---|
1492 | { |
---|
1493 | const time_t now = time( NULL ); |
---|
1494 | tr_peermsgs * msgs = vmsgs; |
---|
1495 | struct peer_request * r; |
---|
1496 | size_t len; |
---|
1497 | |
---|
1498 | /* if we froze out a downloaded block because of speed limits, |
---|
1499 | start listening to the peer again */ |
---|
1500 | if( msgs->notListening && canDownload( msgs ) ) |
---|
1501 | { |
---|
1502 | msgs->notListening = 0; |
---|
1503 | tr_peerIoTryRead( msgs->io ); |
---|
1504 | } |
---|
1505 | |
---|
1506 | pumpRequestQueue( msgs ); |
---|
1507 | |
---|
1508 | if( !canWrite( msgs ) ) |
---|
1509 | { |
---|
1510 | } |
---|
1511 | else if(( len = EVBUFFER_LENGTH( msgs->outBlock ) )) |
---|
1512 | { |
---|
1513 | const size_t uploadMax = getUploadMax( msgs ); |
---|
1514 | const size_t outlen = MIN( len, uploadMax ); |
---|
1515 | tr_peerIoWrite( msgs->io, EVBUFFER_DATA( msgs->outBlock ), outlen ); |
---|
1516 | evbuffer_drain( msgs->outBlock, outlen ); |
---|
1517 | msgs->clientSentAnythingAt = now; |
---|
1518 | peerGotBytes( msgs, outlen ); |
---|
1519 | len -= outlen; |
---|
1520 | dbgmsg( msgs, "wrote %d bytes; %d left in block", (int)outlen, (int)len ); |
---|
1521 | fflush( stdout ); |
---|
1522 | } |
---|
1523 | else if(( len = EVBUFFER_LENGTH( msgs->outMessages ) )) |
---|
1524 | { |
---|
1525 | tr_peerIoWriteBuf( msgs->io, msgs->outMessages ); |
---|
1526 | msgs->clientSentAnythingAt = now; |
---|
1527 | } |
---|
1528 | else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS ) |
---|
1529 | { |
---|
1530 | sendKeepalive( msgs ); |
---|
1531 | } |
---|
1532 | |
---|
1533 | if( !EVBUFFER_LENGTH( msgs->outBlock ) |
---|
1534 | && (( r = popNextRequest( msgs ))) |
---|
1535 | && requestIsValid( msgs, r ) |
---|
1536 | && tr_cpPieceIsComplete( msgs->torrent->completion, r->index ) ) |
---|
1537 | { |
---|
1538 | uint8_t * buf = tr_new( uint8_t, r->length ); |
---|
1539 | |
---|
1540 | if( !tr_ioRead( msgs->torrent, r->index, r->offset, r->length, buf ) ) |
---|
1541 | { |
---|
1542 | tr_peerIo * io = msgs->io; |
---|
1543 | struct evbuffer * out = msgs->outBlock; |
---|
1544 | |
---|
1545 | dbgmsg( msgs, "sending block %u:%u->%u", r->index, r->offset, r->length ); |
---|
1546 | tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 2*sizeof(uint32_t) + r->length ); |
---|
1547 | tr_peerIoWriteUint8 ( io, out, BT_PIECE ); |
---|
1548 | tr_peerIoWriteUint32( io, out, r->index ); |
---|
1549 | tr_peerIoWriteUint32( io, out, r->offset ); |
---|
1550 | tr_peerIoWriteBytes ( io, out, buf, r->length ); |
---|
1551 | } |
---|
1552 | |
---|
1553 | tr_free( buf ); |
---|
1554 | tr_free( r ); |
---|
1555 | |
---|
1556 | pulse( msgs ); /* start sending it right away */ |
---|
1557 | } |
---|
1558 | |
---|
1559 | return TRUE; /* loop forever */ |
---|
1560 | } |
---|
1561 | |
---|
1562 | static void |
---|
1563 | didWrite( struct bufferevent * evin UNUSED, void * vmsgs ) |
---|
1564 | { |
---|
1565 | pulse( vmsgs ); |
---|
1566 | } |
---|
1567 | |
---|
1568 | static void |
---|
1569 | gotError( struct bufferevent * evbuf UNUSED, short what, void * vmsgs ) |
---|
1570 | { |
---|
1571 | dbgmsg( vmsgs, "libevent got an error! what=%d, errno=%d (%s)", |
---|
1572 | (int)what, errno, strerror(errno) ); |
---|
1573 | fireGotError( vmsgs ); |
---|
1574 | } |
---|
1575 | |
---|
1576 | static void |
---|
1577 | sendBitfield( tr_peermsgs * msgs ) |
---|
1578 | { |
---|
1579 | const tr_bitfield * bitfield = tr_cpPieceBitfield( msgs->torrent->completion ); |
---|
1580 | struct evbuffer * out = msgs->outMessages; |
---|
1581 | |
---|
1582 | dbgmsg( msgs, "sending peer a bitfield message" ); |
---|
1583 | tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->len ); |
---|
1584 | tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD ); |
---|
1585 | tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->len ); |
---|
1586 | } |
---|
1587 | |
---|
1588 | /** |
---|
1589 | *** |
---|
1590 | **/ |
---|
1591 | |
---|
1592 | /* some peers give us error messages if we send |
---|
1593 | more than this many peers in a single pex message */ |
---|
1594 | #define MAX_PEX_DIFFS 200 |
---|
1595 | |
---|
1596 | typedef struct |
---|
1597 | { |
---|
1598 | tr_pex * added; |
---|
1599 | tr_pex * dropped; |
---|
1600 | tr_pex * elements; |
---|
1601 | int addedCount; |
---|
1602 | int droppedCount; |
---|
1603 | int elementCount; |
---|
1604 | int diffCount; |
---|
1605 | } |
---|
1606 | PexDiffs; |
---|
1607 | |
---|
1608 | static void |
---|
1609 | pexAddedCb( void * vpex, void * userData ) |
---|
1610 | { |
---|
1611 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
1612 | tr_pex * pex = (tr_pex *) vpex; |
---|
1613 | if( diffs->diffCount < MAX_PEX_DIFFS ) |
---|
1614 | { |
---|
1615 | diffs->diffCount++; |
---|
1616 | diffs->added[diffs->addedCount++] = *pex; |
---|
1617 | diffs->elements[diffs->elementCount++] = *pex; |
---|
1618 | } |
---|
1619 | } |
---|
1620 | |
---|
1621 | static void |
---|
1622 | pexRemovedCb( void * vpex, void * userData ) |
---|
1623 | { |
---|
1624 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
1625 | tr_pex * pex = (tr_pex *) vpex; |
---|
1626 | if( diffs->diffCount < MAX_PEX_DIFFS ) |
---|
1627 | { |
---|
1628 | diffs->diffCount++; |
---|
1629 | diffs->dropped[diffs->droppedCount++] = *pex; |
---|
1630 | } |
---|
1631 | } |
---|
1632 | |
---|
1633 | static void |
---|
1634 | pexElementCb( void * vpex, void * userData ) |
---|
1635 | { |
---|
1636 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
1637 | tr_pex * pex = (tr_pex *) vpex; |
---|
1638 | if( diffs->diffCount < MAX_PEX_DIFFS ) |
---|
1639 | { |
---|
1640 | diffs->diffCount++; |
---|
1641 | diffs->elements[diffs->elementCount++] = *pex; |
---|
1642 | } |
---|
1643 | } |
---|
1644 | |
---|
1645 | static void |
---|
1646 | sendPex( tr_peermsgs * msgs ) |
---|
1647 | { |
---|
1648 | if( msgs->peerSupportsPex && tr_torrentIsPexEnabled( msgs->torrent ) ) |
---|
1649 | { |
---|
1650 | int i; |
---|
1651 | tr_pex * newPex = NULL; |
---|
1652 | const int newCount = tr_peerMgrGetPeers( msgs->handle->peerMgr, msgs->torrent->info.hash, &newPex ); |
---|
1653 | PexDiffs diffs; |
---|
1654 | benc_val_t val, *added, *dropped, *flags; |
---|
1655 | uint8_t *tmp, *walk; |
---|
1656 | char * benc; |
---|
1657 | int bencLen; |
---|
1658 | |
---|
1659 | /* build the diffs */ |
---|
1660 | diffs.added = tr_new( tr_pex, newCount ); |
---|
1661 | diffs.addedCount = 0; |
---|
1662 | diffs.dropped = tr_new( tr_pex, msgs->pexCount ); |
---|
1663 | diffs.droppedCount = 0; |
---|
1664 | diffs.elements = tr_new( tr_pex, newCount + msgs->pexCount ); |
---|
1665 | diffs.elementCount = 0; |
---|
1666 | diffs.diffCount = 0; |
---|
1667 | tr_set_compare( msgs->pex, msgs->pexCount, |
---|
1668 | newPex, newCount, |
---|
1669 | tr_pexCompare, sizeof(tr_pex), |
---|
1670 | pexRemovedCb, pexAddedCb, pexElementCb, &diffs ); |
---|
1671 | dbgmsg( msgs, "pex: old peer count %d, new peer count %d, added %d, removed %d", msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount ); |
---|
1672 | |
---|
1673 | /* update peer */ |
---|
1674 | tr_free( msgs->pex ); |
---|
1675 | msgs->pex = diffs.elements; |
---|
1676 | msgs->pexCount = diffs.elementCount; |
---|
1677 | |
---|
1678 | /* build the pex payload */ |
---|
1679 | tr_bencInit( &val, TYPE_DICT ); |
---|
1680 | tr_bencDictReserve( &val, 3 ); |
---|
1681 | |
---|
1682 | /* "added" */ |
---|
1683 | added = tr_bencDictAdd( &val, "added" ); |
---|
1684 | tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); |
---|
1685 | for( i=0; i<diffs.addedCount; ++i ) { |
---|
1686 | memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4; |
---|
1687 | memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; |
---|
1688 | } |
---|
1689 | assert( ( walk - tmp ) == diffs.addedCount * 6 ); |
---|
1690 | tr_bencInitStr( added, tmp, walk-tmp, FALSE ); |
---|
1691 | |
---|
1692 | /* "added.f" */ |
---|
1693 | flags = tr_bencDictAdd( &val, "added.f" ); |
---|
1694 | tmp = walk = tr_new( uint8_t, diffs.addedCount ); |
---|
1695 | for( i=0; i<diffs.addedCount; ++i ) |
---|
1696 | *walk++ = diffs.added[i].flags; |
---|
1697 | assert( ( walk - tmp ) == diffs.addedCount ); |
---|
1698 | tr_bencInitStr( flags, tmp, walk-tmp, FALSE ); |
---|
1699 | |
---|
1700 | /* "dropped" */ |
---|
1701 | dropped = tr_bencDictAdd( &val, "dropped" ); |
---|
1702 | tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); |
---|
1703 | for( i=0; i<diffs.droppedCount; ++i ) { |
---|
1704 | memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4; |
---|
1705 | memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; |
---|
1706 | } |
---|
1707 | assert( ( walk - tmp ) == diffs.droppedCount * 6 ); |
---|
1708 | tr_bencInitStr( dropped, tmp, walk-tmp, FALSE ); |
---|
1709 | |
---|
1710 | /* write the pex message */ |
---|
1711 | benc = tr_bencSave( &val, &bencLen ); |
---|
1712 | tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 2*sizeof(uint8_t) + bencLen ); |
---|
1713 | tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_LTEP ); |
---|
1714 | tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, OUR_LTEP_PEX ); |
---|
1715 | tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, benc, bencLen ); |
---|
1716 | |
---|
1717 | /* cleanup */ |
---|
1718 | tr_free( benc ); |
---|
1719 | tr_bencFree( &val ); |
---|
1720 | tr_free( diffs.added ); |
---|
1721 | tr_free( diffs.dropped ); |
---|
1722 | tr_free( newPex ); |
---|
1723 | |
---|
1724 | msgs->clientSentPexAt = time( NULL ); |
---|
1725 | } |
---|
1726 | } |
---|
1727 | |
---|
1728 | static int |
---|
1729 | pexPulse( void * vpeer ) |
---|
1730 | { |
---|
1731 | sendPex( vpeer ); |
---|
1732 | return TRUE; |
---|
1733 | } |
---|
1734 | |
---|
1735 | /** |
---|
1736 | *** |
---|
1737 | **/ |
---|
1738 | |
---|
1739 | tr_peermsgs* |
---|
1740 | tr_peerMsgsNew( struct tr_torrent * torrent, |
---|
1741 | struct tr_peer * info, |
---|
1742 | tr_delivery_func func, |
---|
1743 | void * userData, |
---|
1744 | tr_publisher_tag * setme ) |
---|
1745 | { |
---|
1746 | tr_peermsgs * m; |
---|
1747 | |
---|
1748 | assert( info != NULL ); |
---|
1749 | assert( info->io != NULL ); |
---|
1750 | |
---|
1751 | m = tr_new0( tr_peermsgs, 1 ); |
---|
1752 | m->publisher = tr_publisherNew( ); |
---|
1753 | m->info = info; |
---|
1754 | m->handle = torrent->handle; |
---|
1755 | m->torrent = torrent; |
---|
1756 | m->io = info->io; |
---|
1757 | m->info->clientIsChoked = 1; |
---|
1758 | m->info->peerIsChoked = 1; |
---|
1759 | m->info->clientIsInterested = 0; |
---|
1760 | m->info->peerIsInterested = 0; |
---|
1761 | m->info->have = tr_bitfieldNew( torrent->info.pieceCount ); |
---|
1762 | m->state = AWAITING_BT_LENGTH; |
---|
1763 | m->pulseTimer = tr_timerNew( m->handle, pulse, m, PEER_PULSE_INTERVAL ); |
---|
1764 | m->rateTimer = tr_timerNew( m->handle, ratePulse, m, RATE_PULSE_INTERVAL ); |
---|
1765 | m->pexTimer = tr_timerNew( m->handle, pexPulse, m, PEX_INTERVAL ); |
---|
1766 | m->outMessages = evbuffer_new( ); |
---|
1767 | m->incoming.block = evbuffer_new( ); |
---|
1768 | m->outBlock = evbuffer_new( ); |
---|
1769 | m->peerAllowedPieces = NULL; |
---|
1770 | m->clientAllowedPieces = NULL; |
---|
1771 | m->clientSuggestedPieces = NULL; |
---|
1772 | *setme = tr_publisherSubscribe( m->publisher, func, userData ); |
---|
1773 | |
---|
1774 | if ( tr_peerIoSupportsFEXT( m->io ) ) |
---|
1775 | { |
---|
1776 | /* This peer is fastpeer-enabled, generate its allowed set |
---|
1777 | * (before registering our callbacks) */ |
---|
1778 | if ( !m->peerAllowedPieces ) { |
---|
1779 | const struct in_addr *peerAddr = tr_peerIoGetAddress( m->io, NULL ); |
---|
1780 | |
---|
1781 | m->peerAllowedPieces = tr_peerMgrGenerateAllowedSet( MAX_FAST_ALLOWED_COUNT, |
---|
1782 | m->torrent->info.pieceCount, |
---|
1783 | m->torrent->info.hash, |
---|
1784 | peerAddr ); |
---|
1785 | } |
---|
1786 | m->clientAllowedPieces = tr_bitfieldNew( m->torrent->info.pieceCount ); |
---|
1787 | |
---|
1788 | m->clientSuggestedPieces = tr_bitfieldNew( m->torrent->info.pieceCount ); |
---|
1789 | } |
---|
1790 | |
---|
1791 | tr_peerIoSetTimeoutSecs( m->io, 150 ); /* timeout after N seconds of inactivity */ |
---|
1792 | tr_peerIoSetIOFuncs( m->io, canRead, didWrite, gotError, m ); |
---|
1793 | ratePulse( m ); |
---|
1794 | |
---|
1795 | if ( tr_peerIoSupportsLTEP( m->io ) ) |
---|
1796 | sendLtepHandshake( m ); |
---|
1797 | |
---|
1798 | if ( !tr_peerIoSupportsFEXT( m->io ) ) |
---|
1799 | sendBitfield( m ); |
---|
1800 | else { |
---|
1801 | /* This peer is fastpeer-enabled, send it have-all or have-none if appropriate */ |
---|
1802 | uint32_t peerProgress; |
---|
1803 | float completion = tr_cpPercentComplete( m->torrent->completion ); |
---|
1804 | if ( completion == 0.0f ) { |
---|
1805 | sendFastHave( m, 0 ); |
---|
1806 | } else if ( completion == 1.0f ) { |
---|
1807 | sendFastHave( m, 1 ); |
---|
1808 | } else { |
---|
1809 | sendBitfield( m ); |
---|
1810 | } |
---|
1811 | peerProgress = m->torrent->info.pieceCount * m->info->progress; |
---|
1812 | |
---|
1813 | if ( peerProgress < MAX_FAST_ALLOWED_COUNT ) |
---|
1814 | sendFastAllowedSet( m ); |
---|
1815 | } |
---|
1816 | |
---|
1817 | return m; |
---|
1818 | } |
---|
1819 | |
---|
1820 | void |
---|
1821 | tr_peerMsgsFree( tr_peermsgs* msgs ) |
---|
1822 | { |
---|
1823 | if( msgs != NULL ) |
---|
1824 | { |
---|
1825 | tr_timerFree( &msgs->pulseTimer ); |
---|
1826 | tr_timerFree( &msgs->rateTimer ); |
---|
1827 | tr_timerFree( &msgs->pexTimer ); |
---|
1828 | tr_publisherFree( &msgs->publisher ); |
---|
1829 | tr_list_free( &msgs->clientWillAskFor, tr_free ); |
---|
1830 | tr_list_free( &msgs->clientAskedFor, tr_free ); |
---|
1831 | tr_list_free( &msgs->peerAskedForFast, tr_free ); |
---|
1832 | tr_list_free( &msgs->peerAskedFor, tr_free ); |
---|
1833 | tr_bitfieldFree( msgs->peerAllowedPieces ); |
---|
1834 | tr_bitfieldFree( msgs->clientAllowedPieces ); |
---|
1835 | tr_bitfieldFree( msgs->clientSuggestedPieces ); |
---|
1836 | evbuffer_free( msgs->incoming.block ); |
---|
1837 | evbuffer_free( msgs->outMessages ); |
---|
1838 | evbuffer_free( msgs->outBlock ); |
---|
1839 | tr_free( msgs->pex ); |
---|
1840 | msgs->pexCount = 0; |
---|
1841 | tr_free( msgs ); |
---|
1842 | } |
---|
1843 | } |
---|
1844 | |
---|
1845 | tr_publisher_tag |
---|
1846 | tr_peerMsgsSubscribe( tr_peermsgs * peer, |
---|
1847 | tr_delivery_func func, |
---|
1848 | void * userData ) |
---|
1849 | { |
---|
1850 | return tr_publisherSubscribe( peer->publisher, func, userData ); |
---|
1851 | } |
---|
1852 | |
---|
1853 | void |
---|
1854 | tr_peerMsgsUnsubscribe( tr_peermsgs * peer, |
---|
1855 | tr_publisher_tag tag ) |
---|
1856 | { |
---|
1857 | tr_publisherUnsubscribe( peer->publisher, tag ); |
---|
1858 | } |
---|
1859 | |
---|
1860 | int |
---|
1861 | tr_peerMsgsIsPieceFastAllowed( const tr_peermsgs * peer, |
---|
1862 | uint32_t index ) |
---|
1863 | { |
---|
1864 | return tr_bitfieldHas( peer->clientAllowedPieces, index ); |
---|
1865 | } |
---|
1866 | |
---|
1867 | int |
---|
1868 | tr_peerMsgsIsPieceSuggested( const tr_peermsgs * peer, |
---|
1869 | uint32_t index ) |
---|
1870 | { |
---|
1871 | return tr_bitfieldHas( peer->clientSuggestedPieces, index ); |
---|
1872 | } |
---|
1873 | |
---|