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