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 2984 2007-09-07 17:05:56Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <ctype.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <string.h> |
---|
18 | |
---|
19 | #include <arpa/inet.h> |
---|
20 | |
---|
21 | #include <sys/types.h> /* event.h needs this */ |
---|
22 | #include <event.h> |
---|
23 | |
---|
24 | #include "transmission.h" |
---|
25 | #include "bencode.h" |
---|
26 | #include "completion.h" |
---|
27 | #include "inout.h" |
---|
28 | #include "list.h" |
---|
29 | #include "peer-io.h" |
---|
30 | #include "peer-mgr.h" |
---|
31 | #include "peer-mgr-private.h" |
---|
32 | #include "peer-msgs.h" |
---|
33 | #include "ratecontrol.h" |
---|
34 | #include "timer.h" |
---|
35 | #include "utils.h" |
---|
36 | |
---|
37 | /** |
---|
38 | *** |
---|
39 | **/ |
---|
40 | |
---|
41 | #define MINUTES_TO_MSEC(N) ((N) * 60 * 1000) |
---|
42 | |
---|
43 | /* pex attempts are made this frequently */ |
---|
44 | #define PEX_INTERVAL (MINUTES_TO_MSEC(1)) |
---|
45 | |
---|
46 | /* the most requests we'll batch up for this peer */ |
---|
47 | #define OUT_REQUESTS_MAX 10 |
---|
48 | |
---|
49 | /* when we get down to this many requests, we ask the manager for more */ |
---|
50 | #define OUT_REQUESTS_LOW 4 |
---|
51 | |
---|
52 | enum |
---|
53 | { |
---|
54 | BT_CHOKE = 0, |
---|
55 | BT_UNCHOKE = 1, |
---|
56 | BT_INTERESTED = 2, |
---|
57 | BT_NOT_INTERESTED = 3, |
---|
58 | BT_HAVE = 4, |
---|
59 | BT_BITFIELD = 5, |
---|
60 | BT_REQUEST = 6, |
---|
61 | BT_PIECE = 7, |
---|
62 | BT_CANCEL = 8, |
---|
63 | BT_PORT = 9, |
---|
64 | BT_LTEP = 20, |
---|
65 | |
---|
66 | LTEP_HANDSHAKE = 0 |
---|
67 | }; |
---|
68 | |
---|
69 | enum |
---|
70 | { |
---|
71 | AWAITING_BT_LENGTH, |
---|
72 | AWAITING_BT_MESSAGE, |
---|
73 | READING_BT_PIECE |
---|
74 | }; |
---|
75 | |
---|
76 | static const char * |
---|
77 | getStateName( int state ) |
---|
78 | { |
---|
79 | switch( state ) |
---|
80 | { |
---|
81 | case AWAITING_BT_LENGTH: return "awaiting bt length"; |
---|
82 | case AWAITING_BT_MESSAGE: return "awaiting bt message"; |
---|
83 | case READING_BT_PIECE: return "reading bt piece"; |
---|
84 | } |
---|
85 | |
---|
86 | fprintf (stderr, "PeerManager::getStateName: unhandled state %d\n", state ); |
---|
87 | abort( ); |
---|
88 | } |
---|
89 | |
---|
90 | struct peer_request |
---|
91 | { |
---|
92 | uint32_t index; |
---|
93 | uint32_t offset; |
---|
94 | uint32_t length; |
---|
95 | }; |
---|
96 | |
---|
97 | static int |
---|
98 | peer_request_compare( const void * va, const void * vb ) |
---|
99 | { |
---|
100 | struct peer_request * a = (struct peer_request*) va; |
---|
101 | struct peer_request * b = (struct peer_request*) vb; |
---|
102 | if( a->index != b->index ) return a->index - b->index; |
---|
103 | if( a->offset != b->offset ) return a->offset - b->offset; |
---|
104 | if( a->length != b->length ) return a->length - b->length; |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | struct tr_peermsgs |
---|
109 | { |
---|
110 | tr_peer * info; |
---|
111 | |
---|
112 | tr_handle * handle; |
---|
113 | tr_torrent * torrent; |
---|
114 | tr_peerIo * io; |
---|
115 | |
---|
116 | tr_publisher_t * publisher; |
---|
117 | |
---|
118 | struct evbuffer * outMessages; /* buffer of all the non-piece messages */ |
---|
119 | struct evbuffer * outBlock; /* the block we're currently sending */ |
---|
120 | struct evbuffer * inBlock; /* the block we're currently receiving */ |
---|
121 | tr_list * peerAskedFor; |
---|
122 | tr_list * clientAskedFor; |
---|
123 | |
---|
124 | tr_timer_tag pulseTag; |
---|
125 | tr_timer_tag pexTag; |
---|
126 | |
---|
127 | unsigned int notListening : 1; |
---|
128 | |
---|
129 | struct peer_request blockToUs; |
---|
130 | |
---|
131 | int state; |
---|
132 | |
---|
133 | uint32_t incomingMessageLength; |
---|
134 | |
---|
135 | uint64_t gotKeepAliveTime; |
---|
136 | |
---|
137 | uint8_t ut_pex; |
---|
138 | uint16_t listeningPort; |
---|
139 | |
---|
140 | tr_pex * pex; |
---|
141 | int pexCount; |
---|
142 | }; |
---|
143 | |
---|
144 | /** |
---|
145 | *** EVENTS |
---|
146 | **/ |
---|
147 | |
---|
148 | static const tr_peermsgs_event blankEvent = { 0, 0, NULL }; |
---|
149 | |
---|
150 | static void |
---|
151 | publishEvent( tr_peermsgs * peer, int eventType ) |
---|
152 | { |
---|
153 | tr_peermsgs_event e = blankEvent; |
---|
154 | e.eventType = eventType; |
---|
155 | tr_publisherPublish( peer->publisher, peer, &e ); |
---|
156 | } |
---|
157 | |
---|
158 | static void |
---|
159 | fireGotPex( tr_peermsgs * peer ) |
---|
160 | { |
---|
161 | publishEvent( peer, TR_PEERMSG_GOT_PEX ); |
---|
162 | } |
---|
163 | |
---|
164 | static void |
---|
165 | fireGotBitfield( tr_peermsgs * peer, const tr_bitfield * bitfield ) |
---|
166 | { |
---|
167 | tr_peermsgs_event e = blankEvent; |
---|
168 | e.eventType = TR_PEERMSG_GOT_BITFIELD; |
---|
169 | e.bitfield = bitfield; |
---|
170 | tr_publisherPublish( peer->publisher, peer, &e ); |
---|
171 | } |
---|
172 | |
---|
173 | static void |
---|
174 | fireGotHave( tr_peermsgs * peer, uint32_t pieceIndex ) |
---|
175 | { |
---|
176 | tr_peermsgs_event e = blankEvent; |
---|
177 | e.eventType = TR_PEERMSG_GOT_HAVE; |
---|
178 | e.pieceIndex = pieceIndex; |
---|
179 | tr_publisherPublish( peer->publisher, peer, &e ); |
---|
180 | } |
---|
181 | |
---|
182 | static void |
---|
183 | fireGotError( tr_peermsgs * peer ) |
---|
184 | { |
---|
185 | publishEvent( peer, TR_PEERMSG_GOT_ERROR ); |
---|
186 | } |
---|
187 | |
---|
188 | static void |
---|
189 | fireBlocksRunningLow( tr_peermsgs * peer ) |
---|
190 | { |
---|
191 | publishEvent( peer, TR_PEERMSG_BLOCKS_RUNNING_LOW ); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | *** INTEREST |
---|
196 | **/ |
---|
197 | |
---|
198 | static int |
---|
199 | isPieceInteresting( const tr_peermsgs * peer, |
---|
200 | int piece ) |
---|
201 | { |
---|
202 | const tr_torrent * torrent = peer->torrent; |
---|
203 | if( torrent->info.pieces[piece].dnd ) /* we don't want it */ |
---|
204 | return FALSE; |
---|
205 | if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we already have it */ |
---|
206 | return FALSE; |
---|
207 | if( !tr_bitfieldHas( peer->info->have, piece ) ) /* peer doesn't have it */ |
---|
208 | return FALSE; |
---|
209 | if( tr_bitfieldHas( peer->info->banned, piece ) ) /* peer is banned for it */ |
---|
210 | return FALSE; |
---|
211 | return TRUE; |
---|
212 | } |
---|
213 | |
---|
214 | static int |
---|
215 | isPeerInteresting( const tr_peermsgs * peer ) |
---|
216 | { |
---|
217 | int i; |
---|
218 | const tr_torrent * torrent = peer->torrent; |
---|
219 | const tr_bitfield * bitfield = tr_cpPieceBitfield( torrent->completion ); |
---|
220 | |
---|
221 | if( !peer->info->have ) /* We don't know what this peer has */ |
---|
222 | return FALSE; |
---|
223 | |
---|
224 | assert( bitfield->len == peer->info->have->len ); |
---|
225 | |
---|
226 | for( i=0; i<torrent->info.pieceCount; ++i ) |
---|
227 | if( isPieceInteresting( peer, i ) ) |
---|
228 | return TRUE; |
---|
229 | |
---|
230 | return FALSE; |
---|
231 | } |
---|
232 | |
---|
233 | static void |
---|
234 | sendInterest( tr_peermsgs * peer, int weAreInterested ) |
---|
235 | { |
---|
236 | const uint32_t len = sizeof(uint8_t); |
---|
237 | const uint8_t bt_msgid = weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED; |
---|
238 | |
---|
239 | fprintf( stderr, "peer %p: enqueueing an %s message\n", peer, (weAreInterested ? "interested" : "not interested") ); |
---|
240 | tr_peerIoWriteUint32( peer->io, peer->outMessages, len ); |
---|
241 | tr_peerIoWriteBytes( peer->io, peer->outMessages, &bt_msgid, 1 ); |
---|
242 | } |
---|
243 | |
---|
244 | static void |
---|
245 | updateInterest( tr_peermsgs * peer ) |
---|
246 | { |
---|
247 | const int i = isPeerInteresting( peer ); |
---|
248 | if( i != peer->info->clientIsInterested ) |
---|
249 | sendInterest( peer, i ); |
---|
250 | } |
---|
251 | |
---|
252 | void |
---|
253 | tr_peerMsgsSetChoke( tr_peermsgs * peer, int choke ) |
---|
254 | { |
---|
255 | assert( peer != NULL ); |
---|
256 | |
---|
257 | if( peer->info->peerIsChoked != !!choke ) |
---|
258 | { |
---|
259 | const uint32_t len = sizeof(uint8_t); |
---|
260 | const uint8_t bt_msgid = choke ? BT_CHOKE : BT_UNCHOKE; |
---|
261 | |
---|
262 | peer->info->peerIsChoked = choke ? 1 : 0; |
---|
263 | if( peer->info ) |
---|
264 | { |
---|
265 | tr_list_foreach( peer->peerAskedFor, tr_free ); |
---|
266 | tr_list_free( &peer->peerAskedFor ); |
---|
267 | } |
---|
268 | |
---|
269 | fprintf( stderr, "peer %p: enqueuing a %s message\n", peer, (choke ? "choke" : "unchoke") ); |
---|
270 | tr_peerIoWriteUint32( peer->io, peer->outMessages, len ); |
---|
271 | tr_peerIoWriteBytes( peer->io, peer->outMessages, &bt_msgid, 1 ); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | *** |
---|
277 | **/ |
---|
278 | |
---|
279 | int |
---|
280 | tr_peerMsgsAddRequest( tr_peermsgs * peer, |
---|
281 | uint32_t index, |
---|
282 | uint32_t offset, |
---|
283 | uint32_t length ) |
---|
284 | { |
---|
285 | int ret =-1; |
---|
286 | |
---|
287 | if( tr_list_size(peer->clientAskedFor) < OUT_REQUESTS_MAX ) |
---|
288 | { |
---|
289 | const uint8_t bt_msgid = BT_REQUEST; |
---|
290 | const uint32_t len = sizeof(uint8_t) + 3 * sizeof(uint32_t); |
---|
291 | struct peer_request * req = tr_new( struct peer_request, 1 ); |
---|
292 | |
---|
293 | tr_peerIoWriteUint32( peer->io, peer->outMessages, len ); |
---|
294 | tr_peerIoWriteBytes( peer->io, peer->outMessages, &bt_msgid, 1 ); |
---|
295 | tr_peerIoWriteUint32( peer->io, peer->outMessages, index ); |
---|
296 | tr_peerIoWriteUint32( peer->io, peer->outMessages, offset ); |
---|
297 | tr_peerIoWriteUint32( peer->io, peer->outMessages, length ); |
---|
298 | fprintf( stderr, "peer %p: requesting a block from piece %u, offset %u, length %u\n", |
---|
299 | peer, (unsigned int)index, (unsigned int)offset, (unsigned int)length ); |
---|
300 | |
---|
301 | req->index = index; |
---|
302 | req->offset = offset; |
---|
303 | req->length = length; |
---|
304 | tr_list_append( &peer->clientAskedFor, req ); |
---|
305 | |
---|
306 | ret = 0; |
---|
307 | } |
---|
308 | |
---|
309 | return ret; |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | *** |
---|
314 | **/ |
---|
315 | |
---|
316 | static void |
---|
317 | parseLtepHandshake( tr_peermsgs * peer, int len, struct evbuffer * inbuf ) |
---|
318 | { |
---|
319 | benc_val_t val, * sub; |
---|
320 | uint8_t * tmp = tr_new( uint8_t, len ); |
---|
321 | evbuffer_remove( inbuf, tmp, len ); |
---|
322 | |
---|
323 | if( tr_bencLoad( tmp, len, &val, NULL ) || val.type!=TYPE_DICT ) { |
---|
324 | fprintf( stderr, "GET extended-handshake, couldn't get dictionary\n" ); |
---|
325 | tr_free( tmp ); |
---|
326 | return; |
---|
327 | } |
---|
328 | |
---|
329 | tr_bencPrint( &val ); |
---|
330 | |
---|
331 | /* check supported messages for utorrent pex */ |
---|
332 | sub = tr_bencDictFind( &val, "m" ); |
---|
333 | if( tr_bencIsDict( sub ) ) { |
---|
334 | sub = tr_bencDictFind( sub, "ut_pex" ); |
---|
335 | if( tr_bencIsInt( sub ) ) { |
---|
336 | peer->ut_pex = (uint8_t) sub->val.i; |
---|
337 | fprintf( stderr, "peer->ut_pex is %d\n", peer->ut_pex ); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | /* get peer's client name */ |
---|
342 | sub = tr_bencDictFind( &val, "v" ); |
---|
343 | if( tr_bencIsStr( sub ) ) { |
---|
344 | int i; |
---|
345 | tr_free( peer->info->client ); |
---|
346 | fprintf( stderr, "dictionary says client is [%s]\n", sub->val.s.s ); |
---|
347 | peer->info->client = tr_strndup( sub->val.s.s, sub->val.s.i ); |
---|
348 | for( i=0; i<sub->val.s.i; ++i ) { fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] ); |
---|
349 | if( (int)peer->info->client[i]==-75 ) peer->info->client[i]='u'; } |
---|
350 | fprintf( stderr, "peer->client is now [%s]\n", peer->info->client ); |
---|
351 | } |
---|
352 | |
---|
353 | /* get peer's listening port */ |
---|
354 | sub = tr_bencDictFind( &val, "p" ); |
---|
355 | if( tr_bencIsInt( sub ) ) { |
---|
356 | peer->listeningPort = htons( (uint16_t)sub->val.i ); |
---|
357 | fprintf( stderr, "peer->port is now %hd\n", peer->listeningPort ); |
---|
358 | } |
---|
359 | |
---|
360 | tr_bencFree( &val ); |
---|
361 | tr_free( tmp ); |
---|
362 | } |
---|
363 | |
---|
364 | static void |
---|
365 | parseUtPex( tr_peermsgs * peer, int msglen, struct evbuffer * inbuf ) |
---|
366 | { |
---|
367 | benc_val_t val, * sub; |
---|
368 | uint8_t * tmp; |
---|
369 | |
---|
370 | if( !peer->info->pexEnabled ) /* no sharing! */ |
---|
371 | return; |
---|
372 | |
---|
373 | tmp = tr_new( uint8_t, msglen ); |
---|
374 | evbuffer_remove( inbuf, tmp, msglen ); |
---|
375 | |
---|
376 | if( tr_bencLoad( tmp, msglen, &val, NULL ) || !tr_bencIsDict( &val ) ) { |
---|
377 | fprintf( stderr, "GET can't read extended-pex dictionary\n" ); |
---|
378 | tr_free( tmp ); |
---|
379 | return; |
---|
380 | } |
---|
381 | |
---|
382 | sub = tr_bencDictFind( &val, "added" ); |
---|
383 | if( tr_bencIsStr(sub) && ((sub->val.s.i % 6) == 0)) { |
---|
384 | const int n = sub->val.s.i / 6 ; |
---|
385 | fprintf( stderr, "got %d peers from uT pex\n", n ); |
---|
386 | tr_peerMgrAddPeers( peer->handle->peerMgr, |
---|
387 | peer->torrent->info.hash, |
---|
388 | TR_PEER_FROM_PEX, |
---|
389 | (uint8_t*)sub->val.s.s, n ); |
---|
390 | } |
---|
391 | |
---|
392 | fireGotPex( peer ); |
---|
393 | |
---|
394 | tr_bencFree( &val ); |
---|
395 | tr_free( tmp ); |
---|
396 | } |
---|
397 | |
---|
398 | static void |
---|
399 | parseLtep( tr_peermsgs * peer, int msglen, struct evbuffer * inbuf ) |
---|
400 | { |
---|
401 | uint8_t ltep_msgid; |
---|
402 | |
---|
403 | tr_peerIoReadBytes( peer->io, inbuf, <ep_msgid, 1 ); |
---|
404 | msglen--; |
---|
405 | |
---|
406 | if( ltep_msgid == LTEP_HANDSHAKE ) |
---|
407 | { |
---|
408 | fprintf( stderr, "got ltep handshake\n" ); |
---|
409 | parseLtepHandshake( peer, msglen, inbuf ); |
---|
410 | } |
---|
411 | else if( ltep_msgid == peer->ut_pex ) |
---|
412 | { |
---|
413 | fprintf( stderr, "got ut pex\n" ); |
---|
414 | parseUtPex( peer, msglen, inbuf ); |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | fprintf( stderr, "skipping unknown ltep message (%d)\n", (int)ltep_msgid ); |
---|
419 | evbuffer_drain( inbuf, msglen ); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | static int |
---|
424 | readBtLength( tr_peermsgs * peer, struct evbuffer * inbuf ) |
---|
425 | { |
---|
426 | uint32_t len; |
---|
427 | const size_t needlen = sizeof(uint32_t); |
---|
428 | |
---|
429 | if( EVBUFFER_LENGTH(inbuf) < needlen ) |
---|
430 | return READ_MORE; |
---|
431 | |
---|
432 | tr_peerIoReadUint32( peer->io, inbuf, &len ); |
---|
433 | |
---|
434 | if( len == 0 ) { /* peer sent us a keepalive message */ |
---|
435 | fprintf( stderr, "peer sent us a keepalive message...\n" ); |
---|
436 | peer->gotKeepAliveTime = tr_date( ); |
---|
437 | } else { |
---|
438 | fprintf( stderr, "peer is sending us a message with %d bytes...\n", (int)len ); |
---|
439 | peer->incomingMessageLength = len; |
---|
440 | peer->state = AWAITING_BT_MESSAGE; |
---|
441 | } return READ_AGAIN; |
---|
442 | } |
---|
443 | |
---|
444 | static int |
---|
445 | readBtMessage( tr_peermsgs * peer, struct evbuffer * inbuf ) |
---|
446 | { |
---|
447 | uint8_t id; |
---|
448 | uint32_t ui32; |
---|
449 | size_t msglen = peer->incomingMessageLength; |
---|
450 | |
---|
451 | if( EVBUFFER_LENGTH(inbuf) < msglen ) |
---|
452 | return READ_MORE; |
---|
453 | |
---|
454 | tr_peerIoReadBytes( peer->io, inbuf, &id, 1 ); |
---|
455 | msglen--; |
---|
456 | fprintf( stderr, "got a message from the peer... " |
---|
457 | "bt id number is %d, and remaining len is %d\n", (int)id, (int)msglen ); |
---|
458 | |
---|
459 | switch( id ) |
---|
460 | { |
---|
461 | case BT_CHOKE: |
---|
462 | assert( msglen == 0 ); |
---|
463 | fprintf( stderr, "got a BT_CHOKE\n" ); |
---|
464 | peer->info->clientIsChoked = 1; |
---|
465 | tr_list_foreach( peer->peerAskedFor, tr_free ); |
---|
466 | tr_list_free( &peer->peerAskedFor ); |
---|
467 | /* FIXME: maybe choke them */ |
---|
468 | /* FIXME: unmark anything we'd requested from them... */ |
---|
469 | break; |
---|
470 | |
---|
471 | case BT_UNCHOKE: |
---|
472 | assert( msglen == 0 ); |
---|
473 | fprintf( stderr, "got a BT_UNCHOKE\n" ); |
---|
474 | peer->info->clientIsChoked = 0; |
---|
475 | /* FIXME: maybe unchoke them */ |
---|
476 | /* FIXME: maybe send them requests */ |
---|
477 | break; |
---|
478 | |
---|
479 | case BT_INTERESTED: |
---|
480 | assert( msglen == 0 ); |
---|
481 | fprintf( stderr, "got a BT_INTERESTED\n" ); |
---|
482 | peer->info->peerIsInterested = 1; |
---|
483 | /* FIXME: maybe unchoke them */ |
---|
484 | break; |
---|
485 | |
---|
486 | case BT_NOT_INTERESTED: |
---|
487 | assert( msglen == 0 ); |
---|
488 | fprintf( stderr, "got a BT_NOT_INTERESTED\n" ); |
---|
489 | peer->info->peerIsInterested = 0; |
---|
490 | /* FIXME: maybe choke them */ |
---|
491 | break; |
---|
492 | |
---|
493 | case BT_HAVE: |
---|
494 | assert( msglen == 4 ); |
---|
495 | fprintf( stderr, "got a BT_HAVE\n" ); |
---|
496 | tr_peerIoReadUint32( peer->io, inbuf, &ui32 ); |
---|
497 | tr_bitfieldAdd( peer->info->have, ui32 ); |
---|
498 | peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount; |
---|
499 | fireGotHave( peer, ui32 ); |
---|
500 | updateInterest( peer ); |
---|
501 | break; |
---|
502 | |
---|
503 | case BT_BITFIELD: |
---|
504 | assert( msglen == peer->info->have->len ); |
---|
505 | fprintf( stderr, "got a BT_BITFIELD\n" ); |
---|
506 | tr_peerIoReadBytes( peer->io, inbuf, peer->info->have->bits, msglen ); |
---|
507 | peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount; |
---|
508 | fprintf( stderr, "peer progress is %f\n", peer->info->progress ); |
---|
509 | fireGotBitfield( peer, peer->info->have ); |
---|
510 | updateInterest( peer ); |
---|
511 | /* FIXME: maybe unchoke */ |
---|
512 | break; |
---|
513 | |
---|
514 | case BT_REQUEST: { |
---|
515 | struct peer_request * req; |
---|
516 | assert( msglen == 12 ); |
---|
517 | fprintf( stderr, "got a BT_REQUEST\n" ); |
---|
518 | req = tr_new( struct peer_request, 1 ); |
---|
519 | tr_peerIoReadUint32( peer->io, inbuf, &req->index ); |
---|
520 | tr_peerIoReadUint32( peer->io, inbuf, &req->offset ); |
---|
521 | tr_peerIoReadUint32( peer->io, inbuf, &req->length ); |
---|
522 | if( !peer->info->peerIsChoked ) |
---|
523 | tr_list_append( &peer->peerAskedFor, req ); |
---|
524 | break; |
---|
525 | } |
---|
526 | |
---|
527 | case BT_CANCEL: { |
---|
528 | struct peer_request req; |
---|
529 | tr_list * node; |
---|
530 | assert( msglen == 12 ); |
---|
531 | fprintf( stderr, "got a BT_CANCEL\n" ); |
---|
532 | tr_peerIoReadUint32( peer->io, inbuf, &req.index ); |
---|
533 | tr_peerIoReadUint32( peer->io, inbuf, &req.offset ); |
---|
534 | tr_peerIoReadUint32( peer->io, inbuf, &req.length ); |
---|
535 | node = tr_list_find( peer->peerAskedFor, &req, peer_request_compare ); |
---|
536 | if( node != NULL ) { |
---|
537 | fprintf( stderr, "found the req that peer is cancelling... cancelled.\n" ); |
---|
538 | tr_list_remove_data( &peer->peerAskedFor, node->data ); |
---|
539 | } |
---|
540 | break; |
---|
541 | } |
---|
542 | |
---|
543 | case BT_PIECE: { |
---|
544 | fprintf( stderr, "got a BT_PIECE\n" ); |
---|
545 | assert( peer->blockToUs.length == 0 ); |
---|
546 | peer->state = READING_BT_PIECE; |
---|
547 | tr_peerIoReadUint32( peer->io, inbuf, &peer->blockToUs.index ); |
---|
548 | tr_peerIoReadUint32( peer->io, inbuf, &peer->blockToUs.offset ); |
---|
549 | peer->blockToUs.length = msglen - 8; |
---|
550 | assert( peer->blockToUs.length > 0 ); |
---|
551 | evbuffer_drain( peer->inBlock, ~0 ); |
---|
552 | break; |
---|
553 | } |
---|
554 | |
---|
555 | case BT_PORT: { |
---|
556 | assert( msglen == 2 ); |
---|
557 | fprintf( stderr, "got a BT_PORT\n" ); |
---|
558 | tr_peerIoReadUint16( peer->io, inbuf, &peer->listeningPort ); |
---|
559 | break; |
---|
560 | } |
---|
561 | |
---|
562 | case BT_LTEP: |
---|
563 | fprintf( stderr, "got a BT_LTEP\n" ); |
---|
564 | parseLtep( peer, msglen, inbuf ); |
---|
565 | break; |
---|
566 | |
---|
567 | default: |
---|
568 | fprintf( stderr, "got an unknown BT message type: %d\n", (int)id ); |
---|
569 | tr_peerIoDrain( peer->io, inbuf, msglen ); |
---|
570 | assert( 0 ); |
---|
571 | } |
---|
572 | |
---|
573 | peer->incomingMessageLength = -1; |
---|
574 | peer->state = AWAITING_BT_LENGTH; |
---|
575 | return READ_AGAIN; |
---|
576 | } |
---|
577 | |
---|
578 | static int |
---|
579 | canDownload( const tr_peermsgs * peer ) |
---|
580 | { |
---|
581 | tr_torrent * tor = peer->torrent; |
---|
582 | |
---|
583 | #if 0 |
---|
584 | /* FIXME: was swift worth it? did anyone notice a difference? */ |
---|
585 | if( SWIFT_ENABLED && !isSeeding && (peer->credit<0) ) |
---|
586 | return FALSE; |
---|
587 | #endif |
---|
588 | |
---|
589 | if( tor->downloadLimitMode == TR_SPEEDLIMIT_GLOBAL ) |
---|
590 | return !tor->handle->useDownloadLimit || tr_rcCanTransfer( tor->handle->download ); |
---|
591 | |
---|
592 | if( tor->downloadLimitMode == TR_SPEEDLIMIT_SINGLE ) |
---|
593 | return tr_rcCanTransfer( tor->download ); |
---|
594 | |
---|
595 | return TRUE; |
---|
596 | } |
---|
597 | |
---|
598 | static void |
---|
599 | gotBlock( tr_peermsgs * peer, int index, int offset, struct evbuffer * inbuf ) |
---|
600 | { |
---|
601 | tr_torrent * tor = peer->torrent; |
---|
602 | const size_t length = EVBUFFER_LENGTH( inbuf ); |
---|
603 | const int block = _tr_block( tor, index, offset ); |
---|
604 | struct peer_request key, *req; |
---|
605 | |
---|
606 | /* sanity clause */ |
---|
607 | if( tr_cpBlockIsComplete( tor->completion, block ) ) { |
---|
608 | tr_dbg( "have this block already..." ); |
---|
609 | return; |
---|
610 | } |
---|
611 | if( (int)length != tr_torBlockCountBytes( tor, block ) ) { |
---|
612 | tr_dbg( "block is the wrong length..." ); |
---|
613 | return; |
---|
614 | } |
---|
615 | |
---|
616 | /* remove it from our `we asked for this' list */ |
---|
617 | key.index = index; |
---|
618 | key.offset = offset; |
---|
619 | key.length = length; |
---|
620 | req = (struct peer_request*) tr_list_find( peer->clientAskedFor, &key, |
---|
621 | peer_request_compare ); |
---|
622 | if( req == NULL ) { |
---|
623 | tr_dbg( "we didn't ask the peer for this message..." ); |
---|
624 | return; |
---|
625 | } |
---|
626 | tr_list_remove_data( &peer->clientAskedFor, req ); |
---|
627 | tr_free( req ); |
---|
628 | |
---|
629 | /* write to disk */ |
---|
630 | if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf ))) |
---|
631 | return; |
---|
632 | |
---|
633 | /* make a note that this peer helped us with this piece */ |
---|
634 | if( !peer->info->blame ) |
---|
635 | peer->info->blame = tr_bitfieldNew( tor->info.pieceCount ); |
---|
636 | tr_bitfieldAdd( peer->info->blame, index ); |
---|
637 | |
---|
638 | tr_cpBlockAdd( tor->completion, block ); |
---|
639 | |
---|
640 | tor->downloadedCur += length; |
---|
641 | tr_rcTransferred( tor->download, length ); |
---|
642 | tr_rcTransferred( tor->handle->download, length ); |
---|
643 | |
---|
644 | if( tr_list_size(peer->clientAskedFor) <= OUT_REQUESTS_LOW ) |
---|
645 | fireBlocksRunningLow( peer ); |
---|
646 | } |
---|
647 | |
---|
648 | |
---|
649 | static ReadState |
---|
650 | readBtPiece( tr_peermsgs * peer, struct evbuffer * inbuf ) |
---|
651 | { |
---|
652 | assert( peer->blockToUs.length > 0 ); |
---|
653 | |
---|
654 | if( !canDownload( peer ) ) |
---|
655 | { |
---|
656 | peer->notListening = 1; |
---|
657 | tr_peerIoSetIOMode ( peer->io, 0, EV_READ ); |
---|
658 | return READ_DONE; |
---|
659 | } |
---|
660 | else |
---|
661 | { |
---|
662 | /* inbuf -> inBlock */ |
---|
663 | const uint32_t len = MIN( EVBUFFER_LENGTH(inbuf), peer->blockToUs.length ); |
---|
664 | uint8_t * tmp = tr_new( uint8_t, len ); |
---|
665 | tr_peerIoReadBytes( peer->io, inbuf, tmp, len ); |
---|
666 | evbuffer_add( peer->inBlock, tmp, len ); |
---|
667 | tr_free( tmp ); |
---|
668 | peer->blockToUs.length -= len; |
---|
669 | |
---|
670 | if( !peer->blockToUs.length ) |
---|
671 | { |
---|
672 | gotBlock( peer, peer->blockToUs.index, |
---|
673 | peer->blockToUs.offset, |
---|
674 | peer->inBlock ); |
---|
675 | evbuffer_drain( peer->outBlock, ~0 ); |
---|
676 | peer->state = AWAITING_BT_LENGTH; |
---|
677 | } |
---|
678 | |
---|
679 | return READ_AGAIN; |
---|
680 | } |
---|
681 | } |
---|
682 | |
---|
683 | static ReadState |
---|
684 | canRead( struct bufferevent * evin, void * vpeer ) |
---|
685 | { |
---|
686 | ReadState ret; |
---|
687 | tr_peermsgs * peer = (tr_peermsgs *) vpeer; |
---|
688 | struct evbuffer * inbuf = EVBUFFER_INPUT ( evin ); |
---|
689 | fprintf( stderr, "peer %p got a canRead; state is [%s]\n", peer, getStateName(peer->state) ); |
---|
690 | |
---|
691 | switch( peer->state ) |
---|
692 | { |
---|
693 | case AWAITING_BT_LENGTH: ret = readBtLength ( peer, inbuf ); break; |
---|
694 | case AWAITING_BT_MESSAGE: ret = readBtMessage ( peer, inbuf ); break; |
---|
695 | case READING_BT_PIECE: ret = readBtPiece ( peer, inbuf ); break; |
---|
696 | default: assert( 0 ); |
---|
697 | } |
---|
698 | return ret; |
---|
699 | } |
---|
700 | |
---|
701 | /** |
---|
702 | *** |
---|
703 | **/ |
---|
704 | |
---|
705 | static int |
---|
706 | canUpload( const tr_peermsgs * peer ) |
---|
707 | { |
---|
708 | const tr_torrent * tor = peer->torrent; |
---|
709 | |
---|
710 | if( tor->uploadLimitMode == TR_SPEEDLIMIT_GLOBAL ) |
---|
711 | return !tor->handle->useUploadLimit || tr_rcCanTransfer( tor->handle->upload ); |
---|
712 | |
---|
713 | if( tor->uploadLimitMode == TR_SPEEDLIMIT_SINGLE ) |
---|
714 | return tr_rcCanTransfer( tor->upload ); |
---|
715 | |
---|
716 | return TRUE; |
---|
717 | } |
---|
718 | |
---|
719 | static int |
---|
720 | pulse( void * vpeer ) |
---|
721 | { |
---|
722 | tr_peermsgs * peer = (tr_peermsgs *) vpeer; |
---|
723 | size_t len; |
---|
724 | |
---|
725 | fprintf( stderr, "peer %p pulse... notlistening %d, outblock size: %d, outmessages size %d, peerAskedFor %p\n", |
---|
726 | vpeer, |
---|
727 | (int)peer->notListening, |
---|
728 | (int)EVBUFFER_LENGTH( peer->outBlock ), |
---|
729 | (int)EVBUFFER_LENGTH( peer->outMessages ), |
---|
730 | peer->peerAskedFor ); |
---|
731 | |
---|
732 | /* if we froze out a downloaded block because of speed limits, |
---|
733 | start listening to the peer again */ |
---|
734 | if( peer->notListening ) |
---|
735 | { |
---|
736 | fprintf( stderr, "peer %p thawing out...\n", peer ); |
---|
737 | peer->notListening = 0; |
---|
738 | tr_peerIoSetIOMode ( peer->io, EV_READ, 0 ); |
---|
739 | } |
---|
740 | |
---|
741 | if(( len = EVBUFFER_LENGTH( peer->outBlock ) )) |
---|
742 | { |
---|
743 | fprintf( stderr, "peer %p needing to upload... canUpload %d\n", peer, canUpload(peer) ); |
---|
744 | if( canUpload( peer ) ) |
---|
745 | { |
---|
746 | const size_t outlen = MIN( len, 2048 ); |
---|
747 | fprintf( stderr, "peer %p writing %d bytes...\n", peer, (int)outlen ); |
---|
748 | tr_peerIoWrite( peer->io, EVBUFFER_DATA(peer->outBlock), outlen ); |
---|
749 | evbuffer_drain( peer->outBlock, outlen ); |
---|
750 | |
---|
751 | peer->torrent->uploadedCur += outlen; |
---|
752 | tr_rcTransferred( peer->torrent->upload, outlen ); |
---|
753 | tr_rcTransferred( peer->handle->upload, outlen ); |
---|
754 | } |
---|
755 | } |
---|
756 | else if(( len = EVBUFFER_LENGTH( peer->outMessages ) )) |
---|
757 | { |
---|
758 | fprintf( stderr, "peer %p pulse is writing %d bytes worth of messages...\n", peer, (int)len ); |
---|
759 | tr_peerIoWriteBuf( peer->io, peer->outMessages ); |
---|
760 | evbuffer_drain( peer->outMessages, ~0 ); |
---|
761 | } |
---|
762 | else if(( peer->peerAskedFor )) |
---|
763 | { |
---|
764 | struct peer_request * req = (struct peer_request*) peer->peerAskedFor->data; |
---|
765 | uint8_t * tmp = tr_new( uint8_t, req->length ); |
---|
766 | const uint8_t msgid = BT_PIECE; |
---|
767 | const uint32_t msglen = sizeof(uint8_t) + sizeof(uint32_t)*2 + req->length; |
---|
768 | fprintf( stderr, "peer %p starting to upload a block...\n", peer ); |
---|
769 | tr_ioRead( peer->torrent, req->index, req->offset, req->length, tmp ); |
---|
770 | tr_peerIoWriteUint32( peer->io, peer->outBlock, msglen ); |
---|
771 | tr_peerIoWriteBytes ( peer->io, peer->outBlock, &msgid, 1 ); |
---|
772 | tr_peerIoWriteUint32( peer->io, peer->outBlock, req->index ); |
---|
773 | tr_peerIoWriteUint32( peer->io, peer->outBlock, req->offset ); |
---|
774 | tr_peerIoWriteBytes ( peer->io, peer->outBlock, tmp, req->length ); |
---|
775 | tr_free( tmp ); |
---|
776 | } |
---|
777 | |
---|
778 | return TRUE; /* loop forever */ |
---|
779 | } |
---|
780 | |
---|
781 | static void |
---|
782 | didWrite( struct bufferevent * evin UNUSED, void * vpeer ) |
---|
783 | { |
---|
784 | tr_peermsgs * peer = (tr_peermsgs *) vpeer; |
---|
785 | fprintf( stderr, "peer %p got a didWrite...\n", peer ); |
---|
786 | pulse( vpeer ); |
---|
787 | } |
---|
788 | |
---|
789 | static void |
---|
790 | gotError( struct bufferevent * evbuf UNUSED, short what UNUSED, void * vpeer ) |
---|
791 | { |
---|
792 | fireGotError( (tr_peermsgs*)vpeer ); |
---|
793 | } |
---|
794 | |
---|
795 | static void |
---|
796 | sendBitfield( tr_peermsgs * peer ) |
---|
797 | { |
---|
798 | const tr_bitfield * bitfield = tr_cpPieceBitfield( peer->torrent->completion ); |
---|
799 | const uint32_t len = sizeof(uint8_t) + bitfield->len; |
---|
800 | const uint8_t bt_msgid = BT_BITFIELD; |
---|
801 | |
---|
802 | fprintf( stderr, "peer %p: enqueueing a bitfield message\n", peer ); |
---|
803 | tr_peerIoWriteUint32( peer->io, peer->outMessages, len ); |
---|
804 | tr_peerIoWriteBytes( peer->io, peer->outMessages, &bt_msgid, 1 ); |
---|
805 | tr_peerIoWriteBytes( peer->io, peer->outMessages, bitfield->bits, bitfield->len ); |
---|
806 | } |
---|
807 | |
---|
808 | /** |
---|
809 | *** |
---|
810 | **/ |
---|
811 | |
---|
812 | #define MAX_DIFFS 50 |
---|
813 | |
---|
814 | typedef struct |
---|
815 | { |
---|
816 | tr_pex * added; |
---|
817 | tr_pex * dropped; |
---|
818 | tr_pex * elements; |
---|
819 | int addedCount; |
---|
820 | int droppedCount; |
---|
821 | int elementCount; |
---|
822 | int diffCount; |
---|
823 | } |
---|
824 | PexDiffs; |
---|
825 | |
---|
826 | static void pexAddedCb( void * vpex, void * userData ) |
---|
827 | { |
---|
828 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
829 | tr_pex * pex = (tr_pex *) vpex; |
---|
830 | if( diffs->diffCount < MAX_DIFFS ) |
---|
831 | { |
---|
832 | diffs->diffCount++; |
---|
833 | diffs->added[diffs->addedCount++] = *pex; |
---|
834 | diffs->elements[diffs->elementCount++] = *pex; |
---|
835 | } |
---|
836 | } |
---|
837 | |
---|
838 | static void pexRemovedCb( void * vpex, void * userData ) |
---|
839 | { |
---|
840 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
841 | tr_pex * pex = (tr_pex *) vpex; |
---|
842 | if( diffs->diffCount < MAX_DIFFS ) |
---|
843 | { |
---|
844 | diffs->diffCount++; |
---|
845 | diffs->dropped[diffs->droppedCount++] = *pex; |
---|
846 | } |
---|
847 | } |
---|
848 | |
---|
849 | static void pexElementCb( void * vpex, void * userData ) |
---|
850 | { |
---|
851 | PexDiffs * diffs = (PexDiffs *) userData; |
---|
852 | tr_pex * pex = (tr_pex *) vpex; |
---|
853 | if( diffs->diffCount < MAX_DIFFS ) |
---|
854 | { |
---|
855 | diffs->diffCount++; |
---|
856 | diffs->elements[diffs->elementCount++] = *pex; |
---|
857 | } |
---|
858 | } |
---|
859 | |
---|
860 | static int |
---|
861 | pexPulse( void * vpeer ) |
---|
862 | { |
---|
863 | tr_peermsgs * peer = (tr_peermsgs *) vpeer; |
---|
864 | |
---|
865 | if( peer->info->pexEnabled ) |
---|
866 | { |
---|
867 | int i; |
---|
868 | tr_pex * newPex = NULL; |
---|
869 | const int newCount = tr_peerMgrGetPeers( peer->handle->peerMgr, peer->torrent->info.hash, &newPex ); |
---|
870 | PexDiffs diffs; |
---|
871 | benc_val_t val, *added, *dropped, *flags; |
---|
872 | uint8_t *tmp, *walk; |
---|
873 | char * benc; |
---|
874 | int bencLen; |
---|
875 | const uint8_t bt_msgid = BT_LTEP; |
---|
876 | const uint8_t ltep_msgid = peer->ut_pex; |
---|
877 | |
---|
878 | /* build the diffs */ |
---|
879 | diffs.added = tr_new( tr_pex, newCount ); |
---|
880 | diffs.addedCount = 0; |
---|
881 | diffs.dropped = tr_new( tr_pex, peer->pexCount ); |
---|
882 | diffs.droppedCount = 0; |
---|
883 | diffs.elements = tr_new( tr_pex, newCount + peer->pexCount ); |
---|
884 | diffs.elementCount = 0; |
---|
885 | diffs.diffCount = 0; |
---|
886 | tr_set_compare( peer->pex, peer->pexCount, |
---|
887 | newPex, newCount, |
---|
888 | tr_pexCompare, sizeof(tr_pex), |
---|
889 | pexRemovedCb, pexAddedCb, pexElementCb, &diffs ); |
---|
890 | fprintf( stderr, "pex: old peer count %d, new peer count %d, added %d, removed %d\n", peer->pexCount, newCount, diffs.addedCount, diffs.droppedCount ); |
---|
891 | |
---|
892 | /* update peer */ |
---|
893 | tr_free( peer->pex ); |
---|
894 | peer->pex = diffs.elements; |
---|
895 | peer->pexCount = diffs.elementCount; |
---|
896 | |
---|
897 | |
---|
898 | /* build the pex payload */ |
---|
899 | tr_bencInit( &val, TYPE_DICT ); |
---|
900 | tr_bencDictReserve( &val, 3 ); |
---|
901 | |
---|
902 | /* "added" */ |
---|
903 | added = tr_bencDictAdd( &val, "added" ); |
---|
904 | tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); |
---|
905 | for( i=0; i<diffs.addedCount; ++i ) { |
---|
906 | memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4; |
---|
907 | memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; |
---|
908 | } |
---|
909 | assert( ( walk - tmp ) == diffs.addedCount * 6 ); |
---|
910 | tr_bencInitStr( added, tmp, walk-tmp, FALSE ); |
---|
911 | |
---|
912 | /* "added.f" */ |
---|
913 | flags = tr_bencDictAdd( &val, "added.f" ); |
---|
914 | tmp = walk = tr_new( uint8_t, diffs.addedCount ); |
---|
915 | for( i=0; i<diffs.addedCount; ++i ) |
---|
916 | *walk++ = diffs.added[i].flags; |
---|
917 | assert( ( walk - tmp ) == diffs.addedCount ); |
---|
918 | tr_bencInitStr( flags, tmp, walk-tmp, FALSE ); |
---|
919 | |
---|
920 | /* "dropped" */ |
---|
921 | dropped = tr_bencDictAdd( &val, "dropped" ); |
---|
922 | tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); |
---|
923 | for( i=0; i<diffs.droppedCount; ++i ) { |
---|
924 | memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4; |
---|
925 | memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; |
---|
926 | } |
---|
927 | assert( ( walk - tmp ) == diffs.droppedCount * 6 ); |
---|
928 | tr_bencInitStr( dropped, tmp, walk-tmp, FALSE ); |
---|
929 | |
---|
930 | /* write the pex message */ |
---|
931 | benc = tr_bencSaveMalloc( &val, &bencLen ); |
---|
932 | tr_peerIoWriteUint32( peer->io, peer->outBlock, 1 + 1 + bencLen ); |
---|
933 | tr_peerIoWriteBytes ( peer->io, peer->outBlock, &bt_msgid, 1 ); |
---|
934 | tr_peerIoWriteBytes ( peer->io, peer->outBlock, <ep_msgid, 1 ); |
---|
935 | tr_peerIoWriteBytes ( peer->io, peer->outBlock, benc, bencLen ); |
---|
936 | |
---|
937 | /* cleanup */ |
---|
938 | tr_free( benc ); |
---|
939 | tr_bencFree( &val ); |
---|
940 | tr_free( diffs.added ); |
---|
941 | tr_free( diffs.dropped ); |
---|
942 | tr_free( newPex ); |
---|
943 | } |
---|
944 | |
---|
945 | return TRUE; |
---|
946 | } |
---|
947 | |
---|
948 | /** |
---|
949 | *** |
---|
950 | **/ |
---|
951 | |
---|
952 | tr_peermsgs* |
---|
953 | tr_peerMsgsNew( struct tr_torrent * torrent, struct tr_peer * info ) |
---|
954 | { |
---|
955 | tr_peermsgs * peer; |
---|
956 | |
---|
957 | assert( info != NULL ); |
---|
958 | assert( info->io != NULL ); |
---|
959 | |
---|
960 | peer = tr_new0( tr_peermsgs, 1 ); |
---|
961 | peer->publisher = tr_publisherNew( ); |
---|
962 | peer->info = info; |
---|
963 | peer->handle = torrent->handle; |
---|
964 | peer->torrent = torrent; |
---|
965 | peer->io = info->io; |
---|
966 | peer->info->clientIsChoked = 1; |
---|
967 | peer->info->peerIsChoked = 1; |
---|
968 | peer->info->clientIsInterested = 0; |
---|
969 | peer->info->peerIsInterested = 0; |
---|
970 | peer->info->have = tr_bitfieldNew( torrent->info.pieceCount ); |
---|
971 | peer->pulseTag = tr_timerNew( peer->handle, pulse, peer, NULL, 500 ); |
---|
972 | fprintf( stderr, "peer %p pulseTag %p\n", peer, peer->pulseTag ); |
---|
973 | peer->pexTag = tr_timerNew( peer->handle, pexPulse, peer, NULL, PEX_INTERVAL ); |
---|
974 | peer->outMessages = evbuffer_new( ); |
---|
975 | peer->outBlock = evbuffer_new( ); |
---|
976 | peer->inBlock = evbuffer_new( ); |
---|
977 | |
---|
978 | tr_peerIoSetIOFuncs( peer->io, canRead, didWrite, gotError, peer ); |
---|
979 | tr_peerIoSetIOMode( peer->io, EV_READ|EV_WRITE, 0 ); |
---|
980 | |
---|
981 | sendBitfield( peer ); |
---|
982 | |
---|
983 | return peer; |
---|
984 | } |
---|
985 | |
---|
986 | void |
---|
987 | tr_peerMsgsFree( tr_peermsgs* p ) |
---|
988 | { |
---|
989 | if( p != NULL ) |
---|
990 | { |
---|
991 | fprintf( stderr, "peer %p destroying its pulse tag\n", p ); |
---|
992 | tr_publisherFree( &p->publisher ); |
---|
993 | tr_timerFree( &p->pulseTag ); |
---|
994 | tr_timerFree( &p->pexTag ); |
---|
995 | evbuffer_free( p->outMessages ); |
---|
996 | evbuffer_free( p->outBlock ); |
---|
997 | evbuffer_free( p->inBlock ); |
---|
998 | tr_free( p ); |
---|
999 | } |
---|
1000 | } |
---|
1001 | |
---|
1002 | tr_publisher_tag |
---|
1003 | tr_peerMsgsSubscribe( tr_peermsgs * peer, |
---|
1004 | tr_delivery_func func, |
---|
1005 | void * userData ) |
---|
1006 | { |
---|
1007 | return tr_publisherSubscribe( peer->publisher, func, userData ); |
---|
1008 | } |
---|
1009 | |
---|
1010 | void |
---|
1011 | tr_peerMsgsUnsubscribe( tr_peermsgs * peer, |
---|
1012 | tr_publisher_tag tag ) |
---|
1013 | { |
---|
1014 | tr_publisherUnsubscribe( peer->publisher, tag ); |
---|
1015 | } |
---|