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