1 | /* |
---|
2 | * This file Copyright (C) 2007-2010 Mnemosyne LLC |
---|
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-io.c 9973 2010-01-20 18:48:52Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <limits.h> /* INT_MAX */ |
---|
16 | #include <string.h> |
---|
17 | #include <stdio.h> |
---|
18 | #include <unistd.h> |
---|
19 | |
---|
20 | #ifdef WIN32 |
---|
21 | #include <winsock2.h> |
---|
22 | #else |
---|
23 | #include <arpa/inet.h> /* inet_ntoa */ |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <event.h> |
---|
27 | |
---|
28 | #include "transmission.h" |
---|
29 | #include "session.h" |
---|
30 | #include "bandwidth.h" |
---|
31 | #include "crypto.h" |
---|
32 | #include "list.h" |
---|
33 | #include "net.h" |
---|
34 | #include "peer-io.h" |
---|
35 | #include "platform.h" /* MAX_STACK_ARRAY_SIZE */ |
---|
36 | #include "trevent.h" /* tr_runInEventThread() */ |
---|
37 | #include "utils.h" |
---|
38 | |
---|
39 | #define MAGIC_NUMBER 206745 |
---|
40 | |
---|
41 | static size_t |
---|
42 | guessPacketOverhead( size_t d ) |
---|
43 | { |
---|
44 | /** |
---|
45 | * http://sd.wareonearth.com/~phil/net/overhead/ |
---|
46 | * |
---|
47 | * TCP over Ethernet: |
---|
48 | * Assuming no header compression (e.g. not PPP) |
---|
49 | * Add 20 IPv4 header or 40 IPv6 header (no options) |
---|
50 | * Add 20 TCP header |
---|
51 | * Add 12 bytes optional TCP timestamps |
---|
52 | * Max TCP Payload data rates over ethernet are thus: |
---|
53 | * (1500-40)/(38+1500) = 94.9285 % IPv4, minimal headers |
---|
54 | * (1500-52)/(38+1500) = 94.1482 % IPv4, TCP timestamps |
---|
55 | * (1500-52)/(42+1500) = 93.9040 % 802.1q, IPv4, TCP timestamps |
---|
56 | * (1500-60)/(38+1500) = 93.6281 % IPv6, minimal headers |
---|
57 | * (1500-72)/(38+1500) = 92.8479 % IPv6, TCP timestamps |
---|
58 | * (1500-72)/(42+1500) = 92.6070 % 802.1q, IPv6, ICP timestamps |
---|
59 | */ |
---|
60 | const double assumed_payload_data_rate = 94.0; |
---|
61 | |
---|
62 | return (size_t)( d * ( 100.0 / assumed_payload_data_rate ) - d ); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | *** |
---|
67 | **/ |
---|
68 | |
---|
69 | #define dbgmsg( io, ... ) \ |
---|
70 | do { \ |
---|
71 | if( tr_deepLoggingIsActive( ) ) \ |
---|
72 | tr_deepLog( __FILE__, __LINE__, tr_peerIoGetAddrStr( io ), __VA_ARGS__ ); \ |
---|
73 | } while( 0 ) |
---|
74 | |
---|
75 | struct tr_datatype |
---|
76 | { |
---|
77 | tr_bool isPieceData; |
---|
78 | size_t length; |
---|
79 | struct __tr_list head; |
---|
80 | }; |
---|
81 | |
---|
82 | /*** |
---|
83 | **** |
---|
84 | ***/ |
---|
85 | |
---|
86 | static void |
---|
87 | didWriteWrapper( tr_peerIo * io, size_t bytes_transferred ) |
---|
88 | { |
---|
89 | while( bytes_transferred && tr_isPeerIo( io ) ) |
---|
90 | { |
---|
91 | struct tr_datatype * next = __tr_list_entry( io->outbuf_datatypes.next, struct tr_datatype, head ); |
---|
92 | const size_t payload = MIN( next->length, bytes_transferred ); |
---|
93 | const size_t overhead = guessPacketOverhead( payload ); |
---|
94 | |
---|
95 | tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData ); |
---|
96 | |
---|
97 | if( overhead > 0 ) |
---|
98 | tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE ); |
---|
99 | |
---|
100 | if( io->didWrite ) |
---|
101 | io->didWrite( io, payload, next->isPieceData, io->userData ); |
---|
102 | |
---|
103 | if( tr_isPeerIo( io ) ) |
---|
104 | { |
---|
105 | bytes_transferred -= payload; |
---|
106 | next->length -= payload; |
---|
107 | if( !next->length ) { |
---|
108 | __tr_list_remove( io->outbuf_datatypes.next ); |
---|
109 | tr_free( next ); |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | canReadWrapper( tr_peerIo * io ) |
---|
117 | { |
---|
118 | tr_bool err = 0; |
---|
119 | tr_bool done = 0; |
---|
120 | tr_session * session; |
---|
121 | |
---|
122 | dbgmsg( io, "canRead" ); |
---|
123 | |
---|
124 | assert( tr_isPeerIo( io ) ); |
---|
125 | assert( tr_isSession( io->session ) ); |
---|
126 | tr_peerIoRef( io ); |
---|
127 | |
---|
128 | session = io->session; |
---|
129 | |
---|
130 | /* try to consume the input buffer */ |
---|
131 | if( io->canRead ) |
---|
132 | { |
---|
133 | tr_sessionLock( session ); |
---|
134 | |
---|
135 | while( !done && !err ) |
---|
136 | { |
---|
137 | size_t piece = 0; |
---|
138 | const size_t oldLen = EVBUFFER_LENGTH( io->inbuf ); |
---|
139 | const int ret = io->canRead( io, io->userData, &piece ); |
---|
140 | |
---|
141 | const size_t used = oldLen - EVBUFFER_LENGTH( io->inbuf ); |
---|
142 | |
---|
143 | assert( tr_isPeerIo( io ) ); |
---|
144 | |
---|
145 | if( piece ) |
---|
146 | tr_bandwidthUsed( &io->bandwidth, TR_DOWN, piece, TRUE ); |
---|
147 | |
---|
148 | if( used != piece ) |
---|
149 | tr_bandwidthUsed( &io->bandwidth, TR_DOWN, used - piece, FALSE ); |
---|
150 | |
---|
151 | switch( ret ) |
---|
152 | { |
---|
153 | case READ_NOW: |
---|
154 | if( EVBUFFER_LENGTH( io->inbuf ) ) |
---|
155 | continue; |
---|
156 | done = 1; |
---|
157 | break; |
---|
158 | |
---|
159 | case READ_LATER: |
---|
160 | done = 1; |
---|
161 | break; |
---|
162 | |
---|
163 | case READ_ERR: |
---|
164 | err = 1; |
---|
165 | break; |
---|
166 | } |
---|
167 | |
---|
168 | assert( tr_isPeerIo( io ) ); |
---|
169 | } |
---|
170 | |
---|
171 | tr_sessionUnlock( session ); |
---|
172 | } |
---|
173 | |
---|
174 | /* keep the iobuf's excess capacity from growing too large */ |
---|
175 | if( EVBUFFER_LENGTH( io->inbuf ) == 0 ) { |
---|
176 | evbuffer_free( io->inbuf ); |
---|
177 | io->inbuf = evbuffer_new( ); |
---|
178 | } |
---|
179 | |
---|
180 | assert( tr_isPeerIo( io ) ); |
---|
181 | tr_peerIoUnref( io ); |
---|
182 | } |
---|
183 | |
---|
184 | tr_bool |
---|
185 | tr_isPeerIo( const tr_peerIo * io ) |
---|
186 | { |
---|
187 | return ( io != NULL ) |
---|
188 | && ( io->magicNumber == MAGIC_NUMBER ) |
---|
189 | && ( io->refCount >= 0 ) |
---|
190 | && ( tr_isBandwidth( &io->bandwidth ) ) |
---|
191 | && ( tr_isAddress( &io->addr ) ); |
---|
192 | } |
---|
193 | |
---|
194 | static void |
---|
195 | event_read_cb( int fd, short event UNUSED, void * vio ) |
---|
196 | { |
---|
197 | int res; |
---|
198 | int e; |
---|
199 | tr_peerIo * io = vio; |
---|
200 | |
---|
201 | /* Limit the input buffer to 256K, so it doesn't grow too large */ |
---|
202 | size_t howmuch; |
---|
203 | const tr_direction dir = TR_DOWN; |
---|
204 | const size_t max = 256 * 1024; |
---|
205 | size_t curlen; |
---|
206 | |
---|
207 | assert( tr_isPeerIo( io ) ); |
---|
208 | |
---|
209 | io->hasFinishedConnecting = TRUE; |
---|
210 | io->pendingEvents &= ~EV_READ; |
---|
211 | |
---|
212 | curlen = EVBUFFER_LENGTH( io->inbuf ); |
---|
213 | howmuch = curlen >= max ? 0 : max - curlen; |
---|
214 | howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch ); |
---|
215 | |
---|
216 | dbgmsg( io, "libevent says this peer is ready to read" ); |
---|
217 | |
---|
218 | /* if we don't have any bandwidth left, stop reading */ |
---|
219 | if( howmuch < 1 ) { |
---|
220 | tr_peerIoSetEnabled( io, dir, FALSE ); |
---|
221 | return; |
---|
222 | } |
---|
223 | |
---|
224 | errno = 0; |
---|
225 | res = evbuffer_read( io->inbuf, fd, howmuch ); |
---|
226 | e = errno; |
---|
227 | |
---|
228 | if( res > 0 ) |
---|
229 | { |
---|
230 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
231 | |
---|
232 | /* Invoke the user callback - must always be called last */ |
---|
233 | canReadWrapper( io ); |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | short what = EVBUFFER_READ; |
---|
238 | |
---|
239 | if( res == 0 ) /* EOF */ |
---|
240 | what |= EVBUFFER_EOF; |
---|
241 | else if( res == -1 ) { |
---|
242 | if( e == EAGAIN || e == EINTR ) { |
---|
243 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
244 | return; |
---|
245 | } |
---|
246 | what |= EVBUFFER_ERROR; |
---|
247 | } |
---|
248 | |
---|
249 | dbgmsg( io, "event_read_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
250 | |
---|
251 | if( io->gotError != NULL ) |
---|
252 | io->gotError( io, what, io->userData ); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | static int |
---|
257 | tr_evbuffer_write( tr_peerIo * io, int fd, size_t howmuch ) |
---|
258 | { |
---|
259 | int e; |
---|
260 | int n; |
---|
261 | struct evbuffer * buffer = io->outbuf; |
---|
262 | |
---|
263 | howmuch = MIN( EVBUFFER_LENGTH( buffer ), howmuch ); |
---|
264 | |
---|
265 | errno = 0; |
---|
266 | #ifdef WIN32 |
---|
267 | n = (int) send(fd, buffer->buffer, howmuch, 0 ); |
---|
268 | #else |
---|
269 | n = (int) write(fd, buffer->buffer, howmuch ); |
---|
270 | #endif |
---|
271 | e = errno; |
---|
272 | dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?strerror(e):"") ); |
---|
273 | |
---|
274 | if( n > 0 ) |
---|
275 | evbuffer_drain( buffer, n ); |
---|
276 | |
---|
277 | /* keep the iobuf's excess capacity from growing too large */ |
---|
278 | if( EVBUFFER_LENGTH( io->outbuf ) == 0 ) { |
---|
279 | evbuffer_free( io->outbuf ); |
---|
280 | io->outbuf = evbuffer_new( ); |
---|
281 | } |
---|
282 | |
---|
283 | return n; |
---|
284 | } |
---|
285 | |
---|
286 | static void |
---|
287 | event_write_cb( int fd, short event UNUSED, void * vio ) |
---|
288 | { |
---|
289 | int res = 0; |
---|
290 | int e; |
---|
291 | short what = EVBUFFER_WRITE; |
---|
292 | tr_peerIo * io = vio; |
---|
293 | size_t howmuch; |
---|
294 | const tr_direction dir = TR_UP; |
---|
295 | |
---|
296 | assert( tr_isPeerIo( io ) ); |
---|
297 | |
---|
298 | io->hasFinishedConnecting = TRUE; |
---|
299 | io->pendingEvents &= ~EV_WRITE; |
---|
300 | |
---|
301 | dbgmsg( io, "libevent says this peer is ready to write" ); |
---|
302 | |
---|
303 | /* Write as much as possible, since the socket is non-blocking, write() will |
---|
304 | * return if it can't write any more data without blocking */ |
---|
305 | howmuch = tr_bandwidthClamp( &io->bandwidth, dir, EVBUFFER_LENGTH( io->outbuf ) ); |
---|
306 | |
---|
307 | /* if we don't have any bandwidth left, stop writing */ |
---|
308 | if( howmuch < 1 ) { |
---|
309 | tr_peerIoSetEnabled( io, dir, FALSE ); |
---|
310 | return; |
---|
311 | } |
---|
312 | |
---|
313 | errno = 0; |
---|
314 | res = tr_evbuffer_write( io, fd, howmuch ); |
---|
315 | e = errno; |
---|
316 | |
---|
317 | if (res == -1) { |
---|
318 | #ifndef WIN32 |
---|
319 | /*todo. evbuffer uses WriteFile when WIN32 is set. WIN32 system calls do not |
---|
320 | * *set errno. thus this error checking is not portable*/ |
---|
321 | if (e == EAGAIN || e == EINTR || e == EINPROGRESS) |
---|
322 | goto reschedule; |
---|
323 | /* error case */ |
---|
324 | what |= EVBUFFER_ERROR; |
---|
325 | |
---|
326 | #else |
---|
327 | goto reschedule; |
---|
328 | #endif |
---|
329 | |
---|
330 | } else if (res == 0) { |
---|
331 | /* eof case */ |
---|
332 | what |= EVBUFFER_EOF; |
---|
333 | } |
---|
334 | if (res <= 0) |
---|
335 | goto error; |
---|
336 | |
---|
337 | if( EVBUFFER_LENGTH( io->outbuf ) ) |
---|
338 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
339 | |
---|
340 | didWriteWrapper( io, res ); |
---|
341 | return; |
---|
342 | |
---|
343 | reschedule: |
---|
344 | if( EVBUFFER_LENGTH( io->outbuf ) ) |
---|
345 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
346 | return; |
---|
347 | |
---|
348 | error: |
---|
349 | |
---|
350 | dbgmsg( io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
351 | |
---|
352 | if( io->gotError != NULL ) |
---|
353 | io->gotError( io, what, io->userData ); |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | *** |
---|
358 | **/ |
---|
359 | |
---|
360 | static tr_peerIo* |
---|
361 | tr_peerIoNew( tr_session * session, |
---|
362 | tr_bandwidth * parent, |
---|
363 | const tr_address * addr, |
---|
364 | tr_port port, |
---|
365 | const uint8_t * torrentHash, |
---|
366 | tr_bool isIncoming, |
---|
367 | tr_bool isSeed, |
---|
368 | int socket ) |
---|
369 | { |
---|
370 | tr_peerIo * io; |
---|
371 | |
---|
372 | assert( session != NULL ); |
---|
373 | assert( session->events != NULL ); |
---|
374 | assert( tr_isBool( isIncoming ) ); |
---|
375 | assert( tr_isBool( isSeed ) ); |
---|
376 | assert( tr_amInEventThread( session ) ); |
---|
377 | |
---|
378 | if( socket >= 0 ) |
---|
379 | tr_netSetTOS( socket, session->peerSocketTOS ); |
---|
380 | |
---|
381 | io = tr_new0( tr_peerIo, 1 ); |
---|
382 | io->magicNumber = MAGIC_NUMBER; |
---|
383 | io->refCount = 1; |
---|
384 | io->crypto = tr_cryptoNew( torrentHash, isIncoming ); |
---|
385 | io->session = session; |
---|
386 | io->addr = *addr; |
---|
387 | io->isSeed = isSeed; |
---|
388 | io->port = port; |
---|
389 | io->socket = socket; |
---|
390 | io->isIncoming = isIncoming != 0; |
---|
391 | io->hasFinishedConnecting = FALSE; |
---|
392 | io->timeCreated = tr_time( ); |
---|
393 | io->inbuf = evbuffer_new( ); |
---|
394 | io->outbuf = evbuffer_new( ); |
---|
395 | tr_bandwidthConstruct( &io->bandwidth, session, parent ); |
---|
396 | tr_bandwidthSetPeer( &io->bandwidth, io ); |
---|
397 | dbgmsg( io, "bandwidth is %p; its parent is %p", &io->bandwidth, parent ); |
---|
398 | |
---|
399 | event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io ); |
---|
400 | event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io ); |
---|
401 | |
---|
402 | __tr_list_init( &io->outbuf_datatypes ); |
---|
403 | |
---|
404 | return io; |
---|
405 | } |
---|
406 | |
---|
407 | tr_peerIo* |
---|
408 | tr_peerIoNewIncoming( tr_session * session, |
---|
409 | tr_bandwidth * parent, |
---|
410 | const tr_address * addr, |
---|
411 | tr_port port, |
---|
412 | int fd ) |
---|
413 | { |
---|
414 | assert( session ); |
---|
415 | assert( tr_isAddress( addr ) ); |
---|
416 | assert( fd >= 0 ); |
---|
417 | |
---|
418 | return tr_peerIoNew( session, parent, addr, port, NULL, TRUE, FALSE, fd ); |
---|
419 | } |
---|
420 | |
---|
421 | tr_peerIo* |
---|
422 | tr_peerIoNewOutgoing( tr_session * session, |
---|
423 | tr_bandwidth * parent, |
---|
424 | const tr_address * addr, |
---|
425 | tr_port port, |
---|
426 | const uint8_t * torrentHash, |
---|
427 | tr_bool isSeed ) |
---|
428 | { |
---|
429 | int fd; |
---|
430 | |
---|
431 | assert( session ); |
---|
432 | assert( tr_isAddress( addr ) ); |
---|
433 | assert( torrentHash ); |
---|
434 | |
---|
435 | fd = tr_netOpenPeerSocket( session, addr, port, isSeed ); |
---|
436 | dbgmsg( NULL, "tr_netOpenPeerSocket returned fd %d", fd ); |
---|
437 | |
---|
438 | return fd < 0 ? NULL |
---|
439 | : tr_peerIoNew( session, parent, addr, port, torrentHash, FALSE, isSeed, fd ); |
---|
440 | } |
---|
441 | |
---|
442 | /*** |
---|
443 | **** |
---|
444 | ***/ |
---|
445 | |
---|
446 | static void |
---|
447 | event_enable( tr_peerIo * io, short event ) |
---|
448 | { |
---|
449 | assert( tr_amInEventThread( io->session ) ); |
---|
450 | assert( io->session != NULL ); |
---|
451 | assert( io->session->events != NULL ); |
---|
452 | assert( event_initialized( &io->event_read ) ); |
---|
453 | assert( event_initialized( &io->event_write ) ); |
---|
454 | |
---|
455 | if( ( event & EV_READ ) && ! ( io->pendingEvents & EV_READ ) ) |
---|
456 | { |
---|
457 | dbgmsg( io, "enabling libevent ready-to-read polling" ); |
---|
458 | event_add( &io->event_read, NULL ); |
---|
459 | io->pendingEvents |= EV_READ; |
---|
460 | } |
---|
461 | |
---|
462 | if( ( event & EV_WRITE ) && ! ( io->pendingEvents & EV_WRITE ) ) |
---|
463 | { |
---|
464 | dbgmsg( io, "enabling libevent ready-to-write polling" ); |
---|
465 | event_add( &io->event_write, NULL ); |
---|
466 | io->pendingEvents |= EV_WRITE; |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | static void |
---|
471 | event_disable( struct tr_peerIo * io, short event ) |
---|
472 | { |
---|
473 | assert( tr_amInEventThread( io->session ) ); |
---|
474 | assert( io->session != NULL ); |
---|
475 | assert( io->session->events != NULL ); |
---|
476 | assert( event_initialized( &io->event_read ) ); |
---|
477 | assert( event_initialized( &io->event_write ) ); |
---|
478 | |
---|
479 | if( ( event & EV_READ ) && ( io->pendingEvents & EV_READ ) ) |
---|
480 | { |
---|
481 | dbgmsg( io, "disabling libevent ready-to-read polling" ); |
---|
482 | event_del( &io->event_read ); |
---|
483 | io->pendingEvents &= ~EV_READ; |
---|
484 | } |
---|
485 | |
---|
486 | if( ( event & EV_WRITE ) && ( io->pendingEvents & EV_WRITE ) ) |
---|
487 | { |
---|
488 | dbgmsg( io, "disabling libevent ready-to-write polling" ); |
---|
489 | event_del( &io->event_write ); |
---|
490 | io->pendingEvents &= ~EV_WRITE; |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | void |
---|
495 | tr_peerIoSetEnabled( tr_peerIo * io, |
---|
496 | tr_direction dir, |
---|
497 | tr_bool isEnabled ) |
---|
498 | { |
---|
499 | const short event = dir == TR_UP ? EV_WRITE : EV_READ; |
---|
500 | |
---|
501 | assert( tr_isPeerIo( io ) ); |
---|
502 | assert( tr_isDirection( dir ) ); |
---|
503 | assert( tr_amInEventThread( io->session ) ); |
---|
504 | assert( io->session->events != NULL ); |
---|
505 | |
---|
506 | if( isEnabled ) |
---|
507 | event_enable( io, event ); |
---|
508 | else |
---|
509 | event_disable( io, event ); |
---|
510 | } |
---|
511 | |
---|
512 | /*** |
---|
513 | **** |
---|
514 | ***/ |
---|
515 | |
---|
516 | static void |
---|
517 | trDatatypeFree( void * data ) |
---|
518 | { |
---|
519 | struct tr_datatype * dt = __tr_list_entry( data, struct tr_datatype, head ); |
---|
520 | tr_free(dt); |
---|
521 | } |
---|
522 | |
---|
523 | static void |
---|
524 | io_dtor( void * vio ) |
---|
525 | { |
---|
526 | tr_peerIo * io = vio; |
---|
527 | |
---|
528 | assert( tr_isPeerIo( io ) ); |
---|
529 | assert( tr_amInEventThread( io->session ) ); |
---|
530 | assert( io->session->events != NULL ); |
---|
531 | |
---|
532 | dbgmsg( io, "in tr_peerIo destructor" ); |
---|
533 | event_disable( io, EV_READ | EV_WRITE ); |
---|
534 | tr_bandwidthDestruct( &io->bandwidth ); |
---|
535 | evbuffer_free( io->outbuf ); |
---|
536 | evbuffer_free( io->inbuf ); |
---|
537 | tr_netClose( io->session, io->socket ); |
---|
538 | tr_cryptoFree( io->crypto ); |
---|
539 | __tr_list_destroy( &io->outbuf_datatypes, trDatatypeFree ); |
---|
540 | |
---|
541 | memset( io, ~0, sizeof( tr_peerIo ) ); |
---|
542 | tr_free( io ); |
---|
543 | } |
---|
544 | |
---|
545 | static void |
---|
546 | tr_peerIoFree( tr_peerIo * io ) |
---|
547 | { |
---|
548 | if( io ) |
---|
549 | { |
---|
550 | dbgmsg( io, "in tr_peerIoFree" ); |
---|
551 | io->canRead = NULL; |
---|
552 | io->didWrite = NULL; |
---|
553 | io->gotError = NULL; |
---|
554 | tr_runInEventThread( io->session, io_dtor, io ); |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | void |
---|
559 | tr_peerIoRefImpl( const char * file, int line, tr_peerIo * io ) |
---|
560 | { |
---|
561 | assert( tr_isPeerIo( io ) ); |
---|
562 | |
---|
563 | dbgmsg( io, "%s:%d is incrementing the IO's refcount from %d to %d", |
---|
564 | file, line, io->refCount, io->refCount+1 ); |
---|
565 | |
---|
566 | ++io->refCount; |
---|
567 | } |
---|
568 | |
---|
569 | void |
---|
570 | tr_peerIoUnrefImpl( const char * file, int line, tr_peerIo * io ) |
---|
571 | { |
---|
572 | assert( tr_isPeerIo( io ) ); |
---|
573 | |
---|
574 | dbgmsg( io, "%s:%d is decrementing the IO's refcount from %d to %d", |
---|
575 | file, line, io->refCount, io->refCount-1 ); |
---|
576 | |
---|
577 | if( !--io->refCount ) |
---|
578 | tr_peerIoFree( io ); |
---|
579 | } |
---|
580 | |
---|
581 | const tr_address* |
---|
582 | tr_peerIoGetAddress( const tr_peerIo * io, tr_port * port ) |
---|
583 | { |
---|
584 | assert( tr_isPeerIo( io ) ); |
---|
585 | |
---|
586 | if( port ) |
---|
587 | *port = io->port; |
---|
588 | |
---|
589 | return &io->addr; |
---|
590 | } |
---|
591 | |
---|
592 | const char* |
---|
593 | tr_peerIoAddrStr( const tr_address * addr, tr_port port ) |
---|
594 | { |
---|
595 | static char buf[512]; |
---|
596 | |
---|
597 | if( addr->type == TR_AF_INET ) |
---|
598 | tr_snprintf( buf, sizeof( buf ), "%s:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); |
---|
599 | else |
---|
600 | tr_snprintf( buf, sizeof( buf ), "[%s]:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); |
---|
601 | return buf; |
---|
602 | } |
---|
603 | |
---|
604 | void |
---|
605 | tr_peerIoSetIOFuncs( tr_peerIo * io, |
---|
606 | tr_can_read_cb readcb, |
---|
607 | tr_did_write_cb writecb, |
---|
608 | tr_net_error_cb errcb, |
---|
609 | void * userData ) |
---|
610 | { |
---|
611 | io->canRead = readcb; |
---|
612 | io->didWrite = writecb; |
---|
613 | io->gotError = errcb; |
---|
614 | io->userData = userData; |
---|
615 | } |
---|
616 | |
---|
617 | void |
---|
618 | tr_peerIoClear( tr_peerIo * io ) |
---|
619 | { |
---|
620 | tr_peerIoSetIOFuncs( io, NULL, NULL, NULL, NULL ); |
---|
621 | tr_peerIoSetEnabled( io, TR_UP, FALSE ); |
---|
622 | tr_peerIoSetEnabled( io, TR_DOWN, FALSE ); |
---|
623 | } |
---|
624 | |
---|
625 | int |
---|
626 | tr_peerIoReconnect( tr_peerIo * io ) |
---|
627 | { |
---|
628 | int pendingEvents; |
---|
629 | tr_session * session; |
---|
630 | |
---|
631 | assert( tr_isPeerIo( io ) ); |
---|
632 | assert( !tr_peerIoIsIncoming( io ) ); |
---|
633 | |
---|
634 | session = tr_peerIoGetSession( io ); |
---|
635 | |
---|
636 | pendingEvents = io->pendingEvents; |
---|
637 | event_disable( io, EV_READ | EV_WRITE ); |
---|
638 | |
---|
639 | if( io->socket >= 0 ) |
---|
640 | tr_netClose( session, io->socket ); |
---|
641 | |
---|
642 | io->socket = tr_netOpenPeerSocket( session, &io->addr, io->port, io->isSeed ); |
---|
643 | event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io ); |
---|
644 | event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io ); |
---|
645 | event_enable( io, pendingEvents ); |
---|
646 | |
---|
647 | if( io->socket >= 0 ) |
---|
648 | { |
---|
649 | tr_netSetTOS( io->socket, session->peerSocketTOS ); |
---|
650 | return 0; |
---|
651 | } |
---|
652 | |
---|
653 | return -1; |
---|
654 | } |
---|
655 | |
---|
656 | /** |
---|
657 | *** |
---|
658 | **/ |
---|
659 | |
---|
660 | void |
---|
661 | tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
662 | const uint8_t * hash ) |
---|
663 | { |
---|
664 | assert( tr_isPeerIo( io ) ); |
---|
665 | |
---|
666 | tr_cryptoSetTorrentHash( io->crypto, hash ); |
---|
667 | } |
---|
668 | |
---|
669 | const uint8_t* |
---|
670 | tr_peerIoGetTorrentHash( tr_peerIo * io ) |
---|
671 | { |
---|
672 | assert( tr_isPeerIo( io ) ); |
---|
673 | assert( io->crypto ); |
---|
674 | |
---|
675 | return tr_cryptoGetTorrentHash( io->crypto ); |
---|
676 | } |
---|
677 | |
---|
678 | int |
---|
679 | tr_peerIoHasTorrentHash( const tr_peerIo * io ) |
---|
680 | { |
---|
681 | assert( tr_isPeerIo( io ) ); |
---|
682 | assert( io->crypto ); |
---|
683 | |
---|
684 | return tr_cryptoHasTorrentHash( io->crypto ); |
---|
685 | } |
---|
686 | |
---|
687 | /** |
---|
688 | *** |
---|
689 | **/ |
---|
690 | |
---|
691 | void |
---|
692 | tr_peerIoSetPeersId( tr_peerIo * io, |
---|
693 | const uint8_t * peer_id ) |
---|
694 | { |
---|
695 | assert( tr_isPeerIo( io ) ); |
---|
696 | |
---|
697 | if( ( io->peerIdIsSet = peer_id != NULL ) ) |
---|
698 | memcpy( io->peerId, peer_id, 20 ); |
---|
699 | else |
---|
700 | memset( io->peerId, 0, 20 ); |
---|
701 | } |
---|
702 | |
---|
703 | /** |
---|
704 | *** |
---|
705 | **/ |
---|
706 | |
---|
707 | void |
---|
708 | tr_peerIoEnableFEXT( tr_peerIo * io, |
---|
709 | tr_bool flag ) |
---|
710 | { |
---|
711 | assert( tr_isPeerIo( io ) ); |
---|
712 | assert( tr_isBool( flag ) ); |
---|
713 | |
---|
714 | dbgmsg( io, "setting FEXT support flag to %d", (flag!=0) ); |
---|
715 | io->fastExtensionSupported = flag; |
---|
716 | } |
---|
717 | |
---|
718 | void |
---|
719 | tr_peerIoEnableLTEP( tr_peerIo * io, |
---|
720 | tr_bool flag ) |
---|
721 | { |
---|
722 | assert( tr_isPeerIo( io ) ); |
---|
723 | assert( tr_isBool( flag ) ); |
---|
724 | |
---|
725 | dbgmsg( io, "setting LTEP support flag to %d", (flag!=0) ); |
---|
726 | io->extendedProtocolSupported = flag; |
---|
727 | } |
---|
728 | |
---|
729 | void |
---|
730 | tr_peerIoEnableDHT( tr_peerIo * io, tr_bool flag ) |
---|
731 | { |
---|
732 | assert( tr_isPeerIo( io ) ); |
---|
733 | assert( tr_isBool( flag ) ); |
---|
734 | |
---|
735 | dbgmsg( io, "setting DHT support flag to %d", (flag!=0) ); |
---|
736 | io->dhtSupported = flag; |
---|
737 | } |
---|
738 | |
---|
739 | /** |
---|
740 | *** |
---|
741 | **/ |
---|
742 | |
---|
743 | static size_t |
---|
744 | getDesiredOutputBufferSize( const tr_peerIo * io, uint64_t now ) |
---|
745 | { |
---|
746 | /* this is all kind of arbitrary, but what seems to work well is |
---|
747 | * being large enough to hold the next 20 seconds' worth of input, |
---|
748 | * or a few blocks, whichever is bigger. |
---|
749 | * It's okay to tweak this as needed */ |
---|
750 | const double maxBlockSize = 16 * 1024; /* 16 KiB is from BT spec */ |
---|
751 | const double currentSpeed = tr_bandwidthGetPieceSpeed( &io->bandwidth, now, TR_UP ); |
---|
752 | const double period = 15; /* arbitrary */ |
---|
753 | const double numBlocks = 3.5; /* the 3 is arbitrary; the .5 is to leave room for messages */ |
---|
754 | return MAX( maxBlockSize*numBlocks, currentSpeed*1024*period ); |
---|
755 | } |
---|
756 | |
---|
757 | size_t |
---|
758 | tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now ) |
---|
759 | { |
---|
760 | const size_t desiredLen = getDesiredOutputBufferSize( io, now ); |
---|
761 | const size_t currentLen = EVBUFFER_LENGTH( io->outbuf ); |
---|
762 | size_t freeSpace = 0; |
---|
763 | |
---|
764 | if( desiredLen > currentLen ) |
---|
765 | freeSpace = desiredLen - currentLen; |
---|
766 | |
---|
767 | return freeSpace; |
---|
768 | } |
---|
769 | |
---|
770 | /** |
---|
771 | *** |
---|
772 | **/ |
---|
773 | |
---|
774 | void |
---|
775 | tr_peerIoSetEncryption( tr_peerIo * io, |
---|
776 | int encryptionMode ) |
---|
777 | { |
---|
778 | assert( tr_isPeerIo( io ) ); |
---|
779 | assert( encryptionMode == PEER_ENCRYPTION_NONE |
---|
780 | || encryptionMode == PEER_ENCRYPTION_RC4 ); |
---|
781 | |
---|
782 | io->encryptionMode = encryptionMode; |
---|
783 | } |
---|
784 | |
---|
785 | /** |
---|
786 | *** |
---|
787 | **/ |
---|
788 | |
---|
789 | void |
---|
790 | tr_peerIoWrite( tr_peerIo * io, |
---|
791 | const void * bytes, |
---|
792 | size_t byteCount, |
---|
793 | tr_bool isPieceData ) |
---|
794 | { |
---|
795 | /* FIXME(libevent2): this implementation snould be moved to tr_peerIoWriteBuf. This function should be implemented as evbuffer_new() + evbuffer_add_reference() + a call to tr_peerIoWriteBuf() + evbuffer_free() */ |
---|
796 | struct tr_datatype * datatype; |
---|
797 | |
---|
798 | assert( tr_amInEventThread( io->session ) ); |
---|
799 | dbgmsg( io, "adding %zu bytes into io->output", byteCount ); |
---|
800 | |
---|
801 | datatype = tr_new( struct tr_datatype, 1 ); |
---|
802 | datatype->isPieceData = isPieceData != 0; |
---|
803 | datatype->length = byteCount; |
---|
804 | |
---|
805 | __tr_list_init( &datatype->head ); |
---|
806 | __tr_list_append( &io->outbuf_datatypes, &datatype->head ); |
---|
807 | |
---|
808 | switch( io->encryptionMode ) |
---|
809 | { |
---|
810 | case PEER_ENCRYPTION_RC4: |
---|
811 | { |
---|
812 | /* FIXME(libevent2): use evbuffer_reserve_space() and evbuffer_commit_space() instead of tmp */ |
---|
813 | uint8_t tmp[MAX_STACK_ARRAY_SIZE]; |
---|
814 | const uint8_t * walk = bytes; |
---|
815 | evbuffer_expand( io->outbuf, byteCount ); |
---|
816 | while( byteCount > 0 ) |
---|
817 | { |
---|
818 | const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); |
---|
819 | tr_cryptoEncrypt( io->crypto, thisPass, walk, tmp ); |
---|
820 | evbuffer_add( io->outbuf, tmp, thisPass ); |
---|
821 | walk += thisPass; |
---|
822 | byteCount -= thisPass; |
---|
823 | } |
---|
824 | break; |
---|
825 | } |
---|
826 | |
---|
827 | case PEER_ENCRYPTION_NONE: |
---|
828 | evbuffer_add( io->outbuf, bytes, byteCount ); |
---|
829 | break; |
---|
830 | |
---|
831 | default: |
---|
832 | assert( 0 ); |
---|
833 | break; |
---|
834 | } |
---|
835 | } |
---|
836 | |
---|
837 | void |
---|
838 | tr_peerIoWriteBuf( tr_peerIo * io, |
---|
839 | struct evbuffer * buf, |
---|
840 | tr_bool isPieceData ) |
---|
841 | { |
---|
842 | /* FIXME(libevent2): loop through calls to evbuffer_get_contiguous_space() + evbuffer_drain() */ |
---|
843 | const size_t n = EVBUFFER_LENGTH( buf ); |
---|
844 | tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData ); |
---|
845 | evbuffer_drain( buf, n ); |
---|
846 | } |
---|
847 | |
---|
848 | /*** |
---|
849 | **** |
---|
850 | ***/ |
---|
851 | |
---|
852 | void |
---|
853 | tr_peerIoReadBytes( tr_peerIo * io, |
---|
854 | struct evbuffer * inbuf, |
---|
855 | void * bytes, |
---|
856 | size_t byteCount ) |
---|
857 | { |
---|
858 | assert( tr_isPeerIo( io ) ); |
---|
859 | /* FIXME(libevent2): use evbuffer_get_length() */ |
---|
860 | assert( EVBUFFER_LENGTH( inbuf ) >= byteCount ); |
---|
861 | |
---|
862 | switch( io->encryptionMode ) |
---|
863 | { |
---|
864 | case PEER_ENCRYPTION_NONE: |
---|
865 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
866 | break; |
---|
867 | |
---|
868 | case PEER_ENCRYPTION_RC4: |
---|
869 | /* FIXME(libevent2): loop through calls to evbuffer_get_contiguous_space() + evbuffer_drain() */ |
---|
870 | tr_cryptoDecrypt( io->crypto, byteCount, EVBUFFER_DATA(inbuf), bytes ); |
---|
871 | evbuffer_drain(inbuf, byteCount ); |
---|
872 | break; |
---|
873 | |
---|
874 | default: |
---|
875 | assert( 0 ); |
---|
876 | } |
---|
877 | } |
---|
878 | |
---|
879 | void |
---|
880 | tr_peerIoDrain( tr_peerIo * io, |
---|
881 | struct evbuffer * inbuf, |
---|
882 | size_t byteCount ) |
---|
883 | { |
---|
884 | uint8_t tmp[MAX_STACK_ARRAY_SIZE]; |
---|
885 | |
---|
886 | while( byteCount > 0 ) |
---|
887 | { |
---|
888 | const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); |
---|
889 | tr_peerIoReadBytes( io, inbuf, tmp, thisPass ); |
---|
890 | byteCount -= thisPass; |
---|
891 | } |
---|
892 | } |
---|
893 | |
---|
894 | /*** |
---|
895 | **** |
---|
896 | ***/ |
---|
897 | |
---|
898 | static int |
---|
899 | tr_peerIoTryRead( tr_peerIo * io, size_t howmuch ) |
---|
900 | { |
---|
901 | int res = 0; |
---|
902 | |
---|
903 | if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch ))) |
---|
904 | { |
---|
905 | int e; |
---|
906 | errno = 0; |
---|
907 | res = evbuffer_read( io->inbuf, io->socket, howmuch ); |
---|
908 | e = errno; |
---|
909 | |
---|
910 | dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") ); |
---|
911 | |
---|
912 | if( EVBUFFER_LENGTH( io->inbuf ) ) |
---|
913 | canReadWrapper( io ); |
---|
914 | |
---|
915 | if( ( res <= 0 ) && ( io->gotError ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) ) |
---|
916 | { |
---|
917 | short what = EVBUFFER_READ | EVBUFFER_ERROR; |
---|
918 | if( res == 0 ) |
---|
919 | what |= EVBUFFER_EOF; |
---|
920 | dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
921 | io->gotError( io, what, io->userData ); |
---|
922 | } |
---|
923 | } |
---|
924 | |
---|
925 | return res; |
---|
926 | } |
---|
927 | |
---|
928 | static int |
---|
929 | tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch ) |
---|
930 | { |
---|
931 | int n = 0; |
---|
932 | |
---|
933 | if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_UP, howmuch ))) |
---|
934 | { |
---|
935 | int e; |
---|
936 | errno = 0; |
---|
937 | n = tr_evbuffer_write( io, io->socket, howmuch ); |
---|
938 | e = errno; |
---|
939 | |
---|
940 | if( n > 0 ) |
---|
941 | didWriteWrapper( io, n ); |
---|
942 | |
---|
943 | if( ( n < 0 ) && ( io->gotError ) && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) ) |
---|
944 | { |
---|
945 | const short what = EVBUFFER_WRITE | EVBUFFER_ERROR; |
---|
946 | dbgmsg( io, "tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)", n, what, e, strerror( e ) ); |
---|
947 | io->gotError( io, what, io->userData ); |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | return n; |
---|
952 | } |
---|
953 | |
---|
954 | int |
---|
955 | tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit ) |
---|
956 | { |
---|
957 | int bytesUsed = 0; |
---|
958 | |
---|
959 | assert( tr_isPeerIo( io ) ); |
---|
960 | assert( tr_isDirection( dir ) ); |
---|
961 | |
---|
962 | if( io->hasFinishedConnecting ) |
---|
963 | { |
---|
964 | if( dir == TR_DOWN ) |
---|
965 | bytesUsed = tr_peerIoTryRead( io, limit ); |
---|
966 | else |
---|
967 | bytesUsed = tr_peerIoTryWrite( io, limit ); |
---|
968 | } |
---|
969 | |
---|
970 | dbgmsg( io, "flushing peer-io, hasFinishedConnecting %d, direction %d, limit %zu, bytesUsed %d", (int)io->hasFinishedConnecting, (int)dir, limit, bytesUsed ); |
---|
971 | return bytesUsed; |
---|
972 | } |
---|
973 | |
---|
974 | int |
---|
975 | tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io ) |
---|
976 | { |
---|
977 | size_t byteCount = 0; |
---|
978 | struct __tr_list * walk; |
---|
979 | struct __tr_list * fencepost = &io->outbuf_datatypes; |
---|
980 | |
---|
981 | /* count up how many bytes are used by non-piece-data messages |
---|
982 | at the front of our outbound queue */ |
---|
983 | for( walk=fencepost->next; walk!=fencepost; walk=walk->next ) { |
---|
984 | struct tr_datatype * d = __tr_list_entry( walk, struct tr_datatype, head ); |
---|
985 | if( d->isPieceData ) |
---|
986 | break; |
---|
987 | byteCount += d->length; |
---|
988 | } |
---|
989 | |
---|
990 | return tr_peerIoFlush( io, TR_UP, byteCount ); |
---|
991 | } |
---|