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 7574 2009-01-02 04:47:37Z 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 | struct tr_peerIo |
---|
82 | { |
---|
83 | tr_bool isEncrypted; |
---|
84 | tr_bool isIncoming; |
---|
85 | tr_bool peerIdIsSet; |
---|
86 | tr_bool extendedProtocolSupported; |
---|
87 | tr_bool fastExtensionSupported; |
---|
88 | |
---|
89 | int magicNumber; |
---|
90 | |
---|
91 | uint8_t encryptionMode; |
---|
92 | tr_port port; |
---|
93 | int socket; |
---|
94 | |
---|
95 | uint8_t peerId[SHA_DIGEST_LENGTH]; |
---|
96 | time_t timeCreated; |
---|
97 | |
---|
98 | tr_session * session; |
---|
99 | |
---|
100 | tr_address addr; |
---|
101 | |
---|
102 | tr_can_read_cb canRead; |
---|
103 | tr_did_write_cb didWrite; |
---|
104 | tr_net_error_cb gotError; |
---|
105 | void * userData; |
---|
106 | |
---|
107 | tr_bandwidth * bandwidth; |
---|
108 | tr_crypto * crypto; |
---|
109 | |
---|
110 | struct evbuffer * inbuf; |
---|
111 | struct evbuffer * outbuf; |
---|
112 | struct __tr_list outbuf_datatypes; /* struct tr_datatype */ |
---|
113 | |
---|
114 | struct event event_read; |
---|
115 | struct event event_write; |
---|
116 | }; |
---|
117 | |
---|
118 | /*** |
---|
119 | **** |
---|
120 | ***/ |
---|
121 | |
---|
122 | static void |
---|
123 | didWriteWrapper( tr_peerIo * io, size_t bytes_transferred ) |
---|
124 | { |
---|
125 | while( bytes_transferred ) |
---|
126 | { |
---|
127 | struct tr_datatype * next = __tr_list_entry( io->outbuf_datatypes.next, |
---|
128 | struct tr_datatype, head ); |
---|
129 | const size_t payload = MIN( next->length, bytes_transferred ); |
---|
130 | const size_t overhead = getPacketOverhead( payload ); |
---|
131 | |
---|
132 | tr_bandwidthUsed( io->bandwidth, TR_UP, payload, next->isPieceData ); |
---|
133 | |
---|
134 | if( overhead > 0 ) |
---|
135 | tr_bandwidthUsed( io->bandwidth, TR_UP, overhead, FALSE ); |
---|
136 | |
---|
137 | if( io->didWrite ) |
---|
138 | io->didWrite( io, payload, next->isPieceData, io->userData ); |
---|
139 | |
---|
140 | bytes_transferred -= payload; |
---|
141 | next->length -= payload; |
---|
142 | if( !next->length ) { |
---|
143 | __tr_list_remove( io->outbuf_datatypes.next ); |
---|
144 | tr_free( next ); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | static void |
---|
150 | canReadWrapper( tr_peerIo * io ) |
---|
151 | { |
---|
152 | tr_bool done = 0; |
---|
153 | tr_bool err = 0; |
---|
154 | tr_session * session = io->session; |
---|
155 | |
---|
156 | dbgmsg( io, "canRead" ); |
---|
157 | |
---|
158 | /* try to consume the input buffer */ |
---|
159 | if( io->canRead ) |
---|
160 | { |
---|
161 | tr_globalLock( session ); |
---|
162 | |
---|
163 | while( !done && !err ) |
---|
164 | { |
---|
165 | size_t piece = 0; |
---|
166 | const size_t oldLen = EVBUFFER_LENGTH( io->inbuf ); |
---|
167 | const int ret = io->canRead( io, io->userData, &piece ); |
---|
168 | |
---|
169 | const size_t used = oldLen - EVBUFFER_LENGTH( io->inbuf ); |
---|
170 | |
---|
171 | assert( tr_isPeerIo( io ) ); |
---|
172 | |
---|
173 | if( io->bandwidth != NULL ) |
---|
174 | { |
---|
175 | if( piece ) |
---|
176 | tr_bandwidthUsed( io->bandwidth, TR_DOWN, piece, TRUE ); |
---|
177 | |
---|
178 | if( used != piece ) |
---|
179 | tr_bandwidthUsed( io->bandwidth, TR_DOWN, used - piece, FALSE ); |
---|
180 | } |
---|
181 | |
---|
182 | switch( ret ) |
---|
183 | { |
---|
184 | case READ_NOW: |
---|
185 | if( EVBUFFER_LENGTH( io->inbuf ) ) |
---|
186 | continue; |
---|
187 | done = 1; |
---|
188 | break; |
---|
189 | |
---|
190 | case READ_LATER: |
---|
191 | done = 1; |
---|
192 | break; |
---|
193 | |
---|
194 | case READ_ERR: |
---|
195 | err = 1; |
---|
196 | break; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | tr_globalUnlock( session ); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | #define _isBool(b) (((b)==0 || (b)==1)) |
---|
205 | |
---|
206 | tr_bool |
---|
207 | tr_isPeerIo( const tr_peerIo * io ) |
---|
208 | { |
---|
209 | return ( io != NULL ) |
---|
210 | && ( io->magicNumber == MAGIC_NUMBER ) |
---|
211 | && ( tr_isAddress( &io->addr ) ) |
---|
212 | && ( _isBool( io->isEncrypted ) ) |
---|
213 | && ( _isBool( io->isIncoming ) ) |
---|
214 | && ( _isBool( io->peerIdIsSet ) ) |
---|
215 | && ( _isBool( io->extendedProtocolSupported ) ) |
---|
216 | && ( _isBool( io->fastExtensionSupported ) ); |
---|
217 | } |
---|
218 | |
---|
219 | static void |
---|
220 | event_read_cb( int fd, short event UNUSED, void * vio ) |
---|
221 | { |
---|
222 | int res; |
---|
223 | int e; |
---|
224 | tr_peerIo * io = vio; |
---|
225 | |
---|
226 | /* Limit the input buffer to 256K, so it doesn't grow too large */ |
---|
227 | size_t howmuch; |
---|
228 | const tr_direction dir = TR_DOWN; |
---|
229 | const size_t max = 256 * 1024; |
---|
230 | const size_t curlen = EVBUFFER_LENGTH( io->inbuf ); |
---|
231 | |
---|
232 | howmuch = curlen >= max ? 0 : max - curlen; |
---|
233 | howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch ); |
---|
234 | |
---|
235 | assert( tr_isPeerIo( io ) ); |
---|
236 | |
---|
237 | dbgmsg( io, "libevent says this peer is ready to read" ); |
---|
238 | |
---|
239 | /* if we don't have any bandwidth left, stop reading */ |
---|
240 | if( howmuch < 1 ) { |
---|
241 | tr_peerIoSetEnabled( io, dir, FALSE ); |
---|
242 | return; |
---|
243 | } |
---|
244 | |
---|
245 | errno = 0; |
---|
246 | res = evbuffer_read( io->inbuf, fd, howmuch ); |
---|
247 | e = errno; |
---|
248 | |
---|
249 | if( res > 0 ) |
---|
250 | { |
---|
251 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
252 | |
---|
253 | /* Invoke the user callback - must always be called last */ |
---|
254 | canReadWrapper( io ); |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | short what = EVBUFFER_READ; |
---|
259 | |
---|
260 | if( res == 0 ) /* EOF */ |
---|
261 | what |= EVBUFFER_EOF; |
---|
262 | else if( res == -1 ) { |
---|
263 | if( e == EAGAIN || e == EINTR ) { |
---|
264 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
265 | return; |
---|
266 | } |
---|
267 | what |= EVBUFFER_ERROR; |
---|
268 | } |
---|
269 | |
---|
270 | dbgmsg( io, "event_read_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
271 | |
---|
272 | if( io->gotError != NULL ) |
---|
273 | io->gotError( io, what, io->userData ); |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | static int |
---|
278 | tr_evbuffer_write( tr_peerIo * io, int fd, size_t howmuch ) |
---|
279 | { |
---|
280 | struct evbuffer * buffer = io->outbuf; |
---|
281 | int n = MIN( EVBUFFER_LENGTH( buffer ), howmuch ); |
---|
282 | int e; |
---|
283 | |
---|
284 | errno = 0; |
---|
285 | #ifdef WIN32 |
---|
286 | n = send(fd, buffer->buffer, n, 0 ); |
---|
287 | #else |
---|
288 | n = write(fd, buffer->buffer, n ); |
---|
289 | #endif |
---|
290 | e = errno; |
---|
291 | dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?strerror(e):"") ); |
---|
292 | |
---|
293 | if( n == -1 ) |
---|
294 | return -1; |
---|
295 | if (n == 0) |
---|
296 | return 0; |
---|
297 | evbuffer_drain( buffer, n ); |
---|
298 | |
---|
299 | return n; |
---|
300 | } |
---|
301 | |
---|
302 | static void |
---|
303 | event_write_cb( int fd, short event UNUSED, void * vio ) |
---|
304 | { |
---|
305 | int res = 0; |
---|
306 | int e; |
---|
307 | short what = EVBUFFER_WRITE; |
---|
308 | tr_peerIo * io = vio; |
---|
309 | size_t howmuch; |
---|
310 | const tr_direction dir = TR_UP; |
---|
311 | |
---|
312 | assert( tr_isPeerIo( io ) ); |
---|
313 | |
---|
314 | dbgmsg( io, "libevent says this peer is ready to write" ); |
---|
315 | |
---|
316 | /* Write as much as possible, since the socket is non-blocking, write() will |
---|
317 | * return if it can't write any more data without blocking */ |
---|
318 | howmuch = tr_bandwidthClamp( io->bandwidth, dir, EVBUFFER_LENGTH( io->outbuf ) ); |
---|
319 | |
---|
320 | /* if we don't have any bandwidth left, stop writing */ |
---|
321 | if( howmuch < 1 ) { |
---|
322 | tr_peerIoSetEnabled( io, dir, FALSE ); |
---|
323 | return; |
---|
324 | } |
---|
325 | |
---|
326 | errno = 0; |
---|
327 | res = tr_evbuffer_write( io, fd, howmuch ); |
---|
328 | e = errno; |
---|
329 | |
---|
330 | if (res == -1) { |
---|
331 | #ifndef WIN32 |
---|
332 | /*todo. evbuffer uses WriteFile when WIN32 is set. WIN32 system calls do not |
---|
333 | * *set errno. thus this error checking is not portable*/ |
---|
334 | if (e == EAGAIN || e == EINTR || e == EINPROGRESS) |
---|
335 | goto reschedule; |
---|
336 | /* error case */ |
---|
337 | what |= EVBUFFER_ERROR; |
---|
338 | |
---|
339 | #else |
---|
340 | goto reschedule; |
---|
341 | #endif |
---|
342 | |
---|
343 | } else if (res == 0) { |
---|
344 | /* eof case */ |
---|
345 | what |= EVBUFFER_EOF; |
---|
346 | } |
---|
347 | if (res <= 0) |
---|
348 | goto error; |
---|
349 | |
---|
350 | if( EVBUFFER_LENGTH( io->outbuf ) ) |
---|
351 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
352 | |
---|
353 | didWriteWrapper( io, res ); |
---|
354 | return; |
---|
355 | |
---|
356 | reschedule: |
---|
357 | if( EVBUFFER_LENGTH( io->outbuf ) ) |
---|
358 | tr_peerIoSetEnabled( io, dir, TRUE ); |
---|
359 | return; |
---|
360 | |
---|
361 | error: |
---|
362 | |
---|
363 | dbgmsg( io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
364 | |
---|
365 | if( io->gotError != NULL ) |
---|
366 | io->gotError( io, what, io->userData ); |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | *** |
---|
371 | **/ |
---|
372 | |
---|
373 | static tr_peerIo* |
---|
374 | tr_peerIoNew( tr_session * session, |
---|
375 | const tr_address * addr, |
---|
376 | tr_port port, |
---|
377 | const uint8_t * torrentHash, |
---|
378 | int isIncoming, |
---|
379 | int socket ) |
---|
380 | { |
---|
381 | tr_peerIo * io; |
---|
382 | |
---|
383 | if( socket >= 0 ) |
---|
384 | tr_netSetTOS( socket, session->peerSocketTOS ); |
---|
385 | |
---|
386 | io = tr_new0( tr_peerIo, 1 ); |
---|
387 | io->magicNumber = MAGIC_NUMBER; |
---|
388 | io->crypto = tr_cryptoNew( torrentHash, isIncoming ); |
---|
389 | io->session = session; |
---|
390 | io->addr = *addr; |
---|
391 | io->port = port; |
---|
392 | io->socket = socket; |
---|
393 | io->isIncoming = isIncoming != 0; |
---|
394 | io->timeCreated = time( NULL ); |
---|
395 | io->inbuf = evbuffer_new( ); |
---|
396 | io->outbuf = evbuffer_new( ); |
---|
397 | |
---|
398 | event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io ); |
---|
399 | event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io ); |
---|
400 | |
---|
401 | __tr_list_init( &io->outbuf_datatypes ); |
---|
402 | |
---|
403 | tr_peerIoSetBandwidth( io, session->bandwidth ); |
---|
404 | |
---|
405 | return io; |
---|
406 | } |
---|
407 | |
---|
408 | tr_peerIo* |
---|
409 | tr_peerIoNewIncoming( tr_session * session, |
---|
410 | const tr_address * addr, |
---|
411 | tr_port port, |
---|
412 | int socket ) |
---|
413 | { |
---|
414 | assert( session ); |
---|
415 | assert( tr_isAddress( addr ) ); |
---|
416 | assert( socket >= 0 ); |
---|
417 | |
---|
418 | return tr_peerIoNew( session, addr, port, NULL, 1, socket ); |
---|
419 | } |
---|
420 | |
---|
421 | tr_peerIo* |
---|
422 | tr_peerIoNewOutgoing( tr_session * session, |
---|
423 | const tr_address * addr, |
---|
424 | tr_port port, |
---|
425 | const uint8_t * torrentHash ) |
---|
426 | { |
---|
427 | int socket; |
---|
428 | |
---|
429 | assert( session ); |
---|
430 | assert( tr_isAddress( addr ) ); |
---|
431 | assert( torrentHash ); |
---|
432 | |
---|
433 | socket = tr_netOpenTCP( session, addr, port ); |
---|
434 | |
---|
435 | return socket < 0 |
---|
436 | ? NULL |
---|
437 | : tr_peerIoNew( session, addr, port, torrentHash, 0, socket ); |
---|
438 | } |
---|
439 | |
---|
440 | static void |
---|
441 | trDatatypeFree( void * data ) |
---|
442 | { |
---|
443 | struct tr_datatype * dt = __tr_list_entry( data, struct tr_datatype, head ); |
---|
444 | tr_free(dt); |
---|
445 | } |
---|
446 | |
---|
447 | static void |
---|
448 | io_dtor( void * vio ) |
---|
449 | { |
---|
450 | tr_peerIo * io = vio; |
---|
451 | |
---|
452 | event_del( &io->event_read ); |
---|
453 | event_del( &io->event_write ); |
---|
454 | tr_peerIoSetBandwidth( io, NULL ); |
---|
455 | evbuffer_free( io->outbuf ); |
---|
456 | evbuffer_free( io->inbuf ); |
---|
457 | tr_netClose( io->socket ); |
---|
458 | tr_cryptoFree( io->crypto ); |
---|
459 | __tr_list_destroy( &io->outbuf_datatypes, trDatatypeFree ); |
---|
460 | |
---|
461 | io->magicNumber = 0xDEAD; |
---|
462 | tr_free( io ); |
---|
463 | } |
---|
464 | |
---|
465 | void |
---|
466 | tr_peerIoFree( tr_peerIo * io ) |
---|
467 | { |
---|
468 | if( io ) |
---|
469 | { |
---|
470 | io->canRead = NULL; |
---|
471 | io->didWrite = NULL; |
---|
472 | io->gotError = NULL; |
---|
473 | tr_runInEventThread( io->session, io_dtor, io ); |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | tr_session* |
---|
478 | tr_peerIoGetSession( tr_peerIo * io ) |
---|
479 | { |
---|
480 | assert( tr_isPeerIo( io ) ); |
---|
481 | assert( io->session ); |
---|
482 | |
---|
483 | return io->session; |
---|
484 | } |
---|
485 | |
---|
486 | const tr_address* |
---|
487 | tr_peerIoGetAddress( const tr_peerIo * io, |
---|
488 | tr_port * port ) |
---|
489 | { |
---|
490 | assert( tr_isPeerIo( io ) ); |
---|
491 | |
---|
492 | if( port ) |
---|
493 | *port = io->port; |
---|
494 | |
---|
495 | return &io->addr; |
---|
496 | } |
---|
497 | |
---|
498 | const char* |
---|
499 | tr_peerIoAddrStr( const tr_address * addr, tr_port port ) |
---|
500 | { |
---|
501 | static char buf[512]; |
---|
502 | |
---|
503 | if( addr->type == TR_AF_INET ) |
---|
504 | tr_snprintf( buf, sizeof( buf ), "%s:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); |
---|
505 | else |
---|
506 | tr_snprintf( buf, sizeof( buf ), "[%s]:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); |
---|
507 | return buf; |
---|
508 | } |
---|
509 | |
---|
510 | const char* |
---|
511 | tr_peerIoGetAddrStr( const tr_peerIo * io ) |
---|
512 | { |
---|
513 | return tr_peerIoAddrStr( &io->addr, io->port ); |
---|
514 | } |
---|
515 | |
---|
516 | void |
---|
517 | tr_peerIoSetIOFuncs( tr_peerIo * io, |
---|
518 | tr_can_read_cb readcb, |
---|
519 | tr_did_write_cb writecb, |
---|
520 | tr_net_error_cb errcb, |
---|
521 | void * userData ) |
---|
522 | { |
---|
523 | io->canRead = readcb; |
---|
524 | io->didWrite = writecb; |
---|
525 | io->gotError = errcb; |
---|
526 | io->userData = userData; |
---|
527 | } |
---|
528 | |
---|
529 | tr_bool |
---|
530 | tr_peerIoIsIncoming( const tr_peerIo * c ) |
---|
531 | { |
---|
532 | return c->isIncoming != 0; |
---|
533 | } |
---|
534 | |
---|
535 | int |
---|
536 | tr_peerIoReconnect( tr_peerIo * io ) |
---|
537 | { |
---|
538 | assert( !tr_peerIoIsIncoming( io ) ); |
---|
539 | |
---|
540 | if( io->socket >= 0 ) |
---|
541 | tr_netClose( io->socket ); |
---|
542 | |
---|
543 | io->socket = tr_netOpenTCP( io->session, &io->addr, io->port ); |
---|
544 | |
---|
545 | if( io->socket >= 0 ) |
---|
546 | { |
---|
547 | tr_bandwidth * bandwidth = io->bandwidth; |
---|
548 | tr_peerIoSetBandwidth( io, NULL ); |
---|
549 | tr_netSetTOS( io->socket, io->session->peerSocketTOS ); |
---|
550 | tr_peerIoSetBandwidth( io, bandwidth ); |
---|
551 | return 0; |
---|
552 | } |
---|
553 | |
---|
554 | return -1; |
---|
555 | } |
---|
556 | |
---|
557 | /** |
---|
558 | *** |
---|
559 | **/ |
---|
560 | |
---|
561 | void |
---|
562 | tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
563 | const uint8_t * hash ) |
---|
564 | { |
---|
565 | assert( tr_isPeerIo( io ) ); |
---|
566 | |
---|
567 | tr_cryptoSetTorrentHash( io->crypto, hash ); |
---|
568 | } |
---|
569 | |
---|
570 | const uint8_t* |
---|
571 | tr_peerIoGetTorrentHash( tr_peerIo * io ) |
---|
572 | { |
---|
573 | assert( tr_isPeerIo( io ) ); |
---|
574 | assert( io->crypto ); |
---|
575 | |
---|
576 | return tr_cryptoGetTorrentHash( io->crypto ); |
---|
577 | } |
---|
578 | |
---|
579 | int |
---|
580 | tr_peerIoHasTorrentHash( const tr_peerIo * io ) |
---|
581 | { |
---|
582 | assert( tr_isPeerIo( io ) ); |
---|
583 | assert( io->crypto ); |
---|
584 | |
---|
585 | return tr_cryptoHasTorrentHash( io->crypto ); |
---|
586 | } |
---|
587 | |
---|
588 | /** |
---|
589 | *** |
---|
590 | **/ |
---|
591 | |
---|
592 | void |
---|
593 | tr_peerIoSetPeersId( tr_peerIo * io, |
---|
594 | const uint8_t * peer_id ) |
---|
595 | { |
---|
596 | assert( tr_isPeerIo( io ) ); |
---|
597 | |
---|
598 | if( ( io->peerIdIsSet = peer_id != NULL ) ) |
---|
599 | memcpy( io->peerId, peer_id, 20 ); |
---|
600 | else |
---|
601 | memset( io->peerId, 0, 20 ); |
---|
602 | } |
---|
603 | |
---|
604 | const uint8_t* |
---|
605 | tr_peerIoGetPeersId( const tr_peerIo * io ) |
---|
606 | { |
---|
607 | assert( tr_isPeerIo( io ) ); |
---|
608 | assert( io->peerIdIsSet ); |
---|
609 | |
---|
610 | return io->peerId; |
---|
611 | } |
---|
612 | |
---|
613 | /** |
---|
614 | *** |
---|
615 | **/ |
---|
616 | |
---|
617 | void |
---|
618 | tr_peerIoEnableFEXT( tr_peerIo * io, |
---|
619 | tr_bool flag ) |
---|
620 | { |
---|
621 | assert( tr_isPeerIo( io ) ); |
---|
622 | assert( _isBool( flag ) ); |
---|
623 | |
---|
624 | dbgmsg( io, "setting FEXT support flag to %d", (flag!=0) ); |
---|
625 | io->fastExtensionSupported = flag; |
---|
626 | } |
---|
627 | |
---|
628 | tr_bool |
---|
629 | tr_peerIoSupportsFEXT( const tr_peerIo * io ) |
---|
630 | { |
---|
631 | assert( tr_isPeerIo( io ) ); |
---|
632 | |
---|
633 | return io->fastExtensionSupported; |
---|
634 | } |
---|
635 | |
---|
636 | /** |
---|
637 | *** |
---|
638 | **/ |
---|
639 | |
---|
640 | void |
---|
641 | tr_peerIoEnableLTEP( tr_peerIo * io, |
---|
642 | tr_bool flag ) |
---|
643 | { |
---|
644 | assert( tr_isPeerIo( io ) ); |
---|
645 | assert( _isBool( flag ) ); |
---|
646 | |
---|
647 | dbgmsg( io, "setting LTEP support flag to %d", (flag!=0) ); |
---|
648 | io->extendedProtocolSupported = flag; |
---|
649 | } |
---|
650 | |
---|
651 | tr_bool |
---|
652 | tr_peerIoSupportsLTEP( const tr_peerIo * io ) |
---|
653 | { |
---|
654 | assert( tr_isPeerIo( io ) ); |
---|
655 | |
---|
656 | return io->extendedProtocolSupported; |
---|
657 | } |
---|
658 | |
---|
659 | /** |
---|
660 | *** |
---|
661 | **/ |
---|
662 | |
---|
663 | static size_t |
---|
664 | getDesiredOutputBufferSize( const tr_peerIo * io ) |
---|
665 | { |
---|
666 | /* this is all kind of arbitrary, but what seems to work well is |
---|
667 | * being large enough to hold the next 20 seconds' worth of input, |
---|
668 | * or a few blocks, whichever is bigger. |
---|
669 | * It's okay to tweak this as needed */ |
---|
670 | const double maxBlockSize = 16 * 1024; /* 16 KiB is from BT spec */ |
---|
671 | const double currentSpeed = tr_bandwidthGetPieceSpeed( io->bandwidth, TR_UP ); |
---|
672 | const double period = 20; /* arbitrary */ |
---|
673 | const double numBlocks = 5.5; /* the 5 is arbitrary; the .5 is to leave room for messages */ |
---|
674 | return MAX( maxBlockSize*numBlocks, currentSpeed*1024*period ); |
---|
675 | } |
---|
676 | |
---|
677 | size_t |
---|
678 | tr_peerIoGetWriteBufferSpace( const tr_peerIo * io ) |
---|
679 | { |
---|
680 | const size_t desiredLen = getDesiredOutputBufferSize( io ); |
---|
681 | const size_t currentLen = EVBUFFER_LENGTH( io->outbuf ); |
---|
682 | size_t freeSpace = 0; |
---|
683 | |
---|
684 | if( desiredLen > currentLen ) |
---|
685 | freeSpace = desiredLen - currentLen; |
---|
686 | |
---|
687 | return freeSpace; |
---|
688 | } |
---|
689 | |
---|
690 | void |
---|
691 | tr_peerIoSetBandwidth( tr_peerIo * io, |
---|
692 | tr_bandwidth * bandwidth ) |
---|
693 | { |
---|
694 | assert( tr_isPeerIo( io ) ); |
---|
695 | |
---|
696 | if( io->bandwidth ) |
---|
697 | tr_bandwidthRemovePeer( io->bandwidth, io ); |
---|
698 | |
---|
699 | io->bandwidth = bandwidth; |
---|
700 | |
---|
701 | if( io->bandwidth ) |
---|
702 | tr_bandwidthAddPeer( io->bandwidth, io ); |
---|
703 | } |
---|
704 | |
---|
705 | /** |
---|
706 | *** |
---|
707 | **/ |
---|
708 | |
---|
709 | tr_crypto* |
---|
710 | tr_peerIoGetCrypto( tr_peerIo * c ) |
---|
711 | { |
---|
712 | return c->crypto; |
---|
713 | } |
---|
714 | |
---|
715 | void |
---|
716 | tr_peerIoSetEncryption( tr_peerIo * io, |
---|
717 | int encryptionMode ) |
---|
718 | { |
---|
719 | assert( tr_isPeerIo( io ) ); |
---|
720 | assert( encryptionMode == PEER_ENCRYPTION_NONE |
---|
721 | || encryptionMode == PEER_ENCRYPTION_RC4 ); |
---|
722 | |
---|
723 | io->encryptionMode = encryptionMode; |
---|
724 | } |
---|
725 | |
---|
726 | int |
---|
727 | tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
728 | { |
---|
729 | return io != NULL && io->encryptionMode == PEER_ENCRYPTION_RC4; |
---|
730 | } |
---|
731 | |
---|
732 | /** |
---|
733 | *** |
---|
734 | **/ |
---|
735 | |
---|
736 | void |
---|
737 | tr_peerIoWrite( tr_peerIo * io, |
---|
738 | const void * writeme, |
---|
739 | size_t writemeLen, |
---|
740 | int isPieceData ) |
---|
741 | { |
---|
742 | struct tr_datatype * datatype; |
---|
743 | |
---|
744 | assert( tr_amInEventThread( io->session ) ); |
---|
745 | dbgmsg( io, "adding %zu bytes into io->output", writemeLen ); |
---|
746 | |
---|
747 | datatype = tr_new( struct tr_datatype, 1 ); |
---|
748 | datatype->isPieceData = isPieceData != 0; |
---|
749 | datatype->length = writemeLen; |
---|
750 | |
---|
751 | __tr_list_init( &datatype->head ); |
---|
752 | __tr_list_append( &io->outbuf_datatypes, &datatype->head ); |
---|
753 | |
---|
754 | evbuffer_add( io->outbuf, writeme, writemeLen ); |
---|
755 | } |
---|
756 | |
---|
757 | void |
---|
758 | tr_peerIoWriteBuf( tr_peerIo * io, |
---|
759 | struct evbuffer * buf, |
---|
760 | int isPieceData ) |
---|
761 | { |
---|
762 | const size_t n = EVBUFFER_LENGTH( buf ); |
---|
763 | |
---|
764 | tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData ); |
---|
765 | evbuffer_drain( buf, n ); |
---|
766 | } |
---|
767 | |
---|
768 | /** |
---|
769 | *** |
---|
770 | **/ |
---|
771 | |
---|
772 | void |
---|
773 | tr_peerIoWriteBytes( tr_peerIo * io, |
---|
774 | struct evbuffer * outbuf, |
---|
775 | const void * bytes, |
---|
776 | size_t byteCount ) |
---|
777 | { |
---|
778 | uint8_t tmp[MAX_STACK_ARRAY_SIZE]; |
---|
779 | |
---|
780 | switch( io->encryptionMode ) |
---|
781 | { |
---|
782 | case PEER_ENCRYPTION_NONE: |
---|
783 | evbuffer_add( outbuf, bytes, byteCount ); |
---|
784 | break; |
---|
785 | |
---|
786 | case PEER_ENCRYPTION_RC4: |
---|
787 | evbuffer_expand( outbuf, byteCount ); |
---|
788 | while( byteCount > 0 ) { |
---|
789 | const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); |
---|
790 | tr_cryptoEncrypt( io->crypto, thisPass, bytes, tmp ); |
---|
791 | evbuffer_add( outbuf, tmp, thisPass ); |
---|
792 | bytes += thisPass; |
---|
793 | byteCount -= thisPass; |
---|
794 | } |
---|
795 | break; |
---|
796 | |
---|
797 | default: |
---|
798 | assert( 0 ); |
---|
799 | } |
---|
800 | } |
---|
801 | |
---|
802 | void |
---|
803 | tr_peerIoWriteUint8( tr_peerIo * io, |
---|
804 | struct evbuffer * outbuf, |
---|
805 | uint8_t writeme ) |
---|
806 | { |
---|
807 | tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) ); |
---|
808 | } |
---|
809 | |
---|
810 | void |
---|
811 | tr_peerIoWriteUint16( tr_peerIo * io, |
---|
812 | struct evbuffer * outbuf, |
---|
813 | uint16_t writeme ) |
---|
814 | { |
---|
815 | uint16_t tmp = htons( writeme ); |
---|
816 | |
---|
817 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) ); |
---|
818 | } |
---|
819 | |
---|
820 | void |
---|
821 | tr_peerIoWriteUint32( tr_peerIo * io, |
---|
822 | struct evbuffer * outbuf, |
---|
823 | uint32_t writeme ) |
---|
824 | { |
---|
825 | uint32_t tmp = htonl( writeme ); |
---|
826 | |
---|
827 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) ); |
---|
828 | } |
---|
829 | |
---|
830 | /*** |
---|
831 | **** |
---|
832 | ***/ |
---|
833 | |
---|
834 | void |
---|
835 | tr_peerIoReadBytes( tr_peerIo * io, |
---|
836 | struct evbuffer * inbuf, |
---|
837 | void * bytes, |
---|
838 | size_t byteCount ) |
---|
839 | { |
---|
840 | assert( EVBUFFER_LENGTH( inbuf ) >= byteCount ); |
---|
841 | |
---|
842 | switch( io->encryptionMode ) |
---|
843 | { |
---|
844 | case PEER_ENCRYPTION_NONE: |
---|
845 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
846 | break; |
---|
847 | |
---|
848 | case PEER_ENCRYPTION_RC4: |
---|
849 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
850 | tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes ); |
---|
851 | break; |
---|
852 | |
---|
853 | default: |
---|
854 | assert( 0 ); |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | void |
---|
859 | tr_peerIoReadUint8( tr_peerIo * io, |
---|
860 | struct evbuffer * inbuf, |
---|
861 | uint8_t * setme ) |
---|
862 | { |
---|
863 | tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); |
---|
864 | } |
---|
865 | |
---|
866 | void |
---|
867 | tr_peerIoReadUint16( tr_peerIo * io, |
---|
868 | struct evbuffer * inbuf, |
---|
869 | uint16_t * setme ) |
---|
870 | { |
---|
871 | uint16_t tmp; |
---|
872 | |
---|
873 | assert( tr_isPeerIo( io ) ); |
---|
874 | |
---|
875 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) ); |
---|
876 | *setme = ntohs( tmp ); |
---|
877 | } |
---|
878 | |
---|
879 | void |
---|
880 | tr_peerIoReadUint32( tr_peerIo * io, |
---|
881 | struct evbuffer * inbuf, |
---|
882 | uint32_t * setme ) |
---|
883 | { |
---|
884 | uint32_t tmp; |
---|
885 | |
---|
886 | assert( tr_isPeerIo( io ) ); |
---|
887 | |
---|
888 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) ); |
---|
889 | *setme = ntohl( tmp ); |
---|
890 | } |
---|
891 | |
---|
892 | void |
---|
893 | tr_peerIoDrain( tr_peerIo * io, |
---|
894 | struct evbuffer * inbuf, |
---|
895 | size_t byteCount ) |
---|
896 | { |
---|
897 | uint8_t tmp[MAX_STACK_ARRAY_SIZE]; |
---|
898 | |
---|
899 | assert( tr_isPeerIo( io ) ); |
---|
900 | |
---|
901 | while( byteCount > 0 ) |
---|
902 | { |
---|
903 | const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); |
---|
904 | tr_peerIoReadBytes( io, inbuf, tmp, thisPass ); |
---|
905 | byteCount -= thisPass; |
---|
906 | } |
---|
907 | } |
---|
908 | |
---|
909 | int |
---|
910 | tr_peerIoGetAge( const tr_peerIo * io ) |
---|
911 | { |
---|
912 | return time( NULL ) - io->timeCreated; |
---|
913 | } |
---|
914 | |
---|
915 | /*** |
---|
916 | **** |
---|
917 | ***/ |
---|
918 | |
---|
919 | static int |
---|
920 | tr_peerIoTryRead( tr_peerIo * io, size_t howmuch ) |
---|
921 | { |
---|
922 | int res = 0; |
---|
923 | |
---|
924 | if(( howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch ))) |
---|
925 | { |
---|
926 | int e; |
---|
927 | errno = 0; |
---|
928 | res = evbuffer_read( io->inbuf, io->socket, howmuch ); |
---|
929 | e = errno; |
---|
930 | |
---|
931 | dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") ); |
---|
932 | |
---|
933 | if( EVBUFFER_LENGTH( io->inbuf ) ) |
---|
934 | canReadWrapper( io ); |
---|
935 | |
---|
936 | if( ( res <= 0 ) && ( io->gotError ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) ) |
---|
937 | { |
---|
938 | short what = EVBUFFER_READ | EVBUFFER_ERROR; |
---|
939 | if( res == 0 ) |
---|
940 | what |= EVBUFFER_EOF; |
---|
941 | dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) ); |
---|
942 | io->gotError( io, what, io->userData ); |
---|
943 | } |
---|
944 | } |
---|
945 | |
---|
946 | return res; |
---|
947 | } |
---|
948 | |
---|
949 | static int |
---|
950 | tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch ) |
---|
951 | { |
---|
952 | int n; |
---|
953 | |
---|
954 | if(( howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch ))) |
---|
955 | { |
---|
956 | int e; |
---|
957 | errno = 0; |
---|
958 | n = tr_evbuffer_write( io, io->socket, (int)howmuch ); |
---|
959 | e = errno; |
---|
960 | |
---|
961 | if( n > 0 ) |
---|
962 | didWriteWrapper( io, n ); |
---|
963 | |
---|
964 | if( ( n < 0 ) && ( io->gotError ) && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) ) |
---|
965 | { |
---|
966 | const short what = EVBUFFER_WRITE | EVBUFFER_ERROR; |
---|
967 | dbgmsg( io, "tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)", n, what, e, strerror( e ) ); |
---|
968 | io->gotError( io, what, io->userData ); |
---|
969 | } |
---|
970 | } |
---|
971 | |
---|
972 | return n; |
---|
973 | } |
---|
974 | |
---|
975 | int |
---|
976 | tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit ) |
---|
977 | { |
---|
978 | int ret; |
---|
979 | |
---|
980 | assert( tr_isPeerIo( io ) ); |
---|
981 | assert( tr_isDirection( dir ) ); |
---|
982 | |
---|
983 | if( dir==TR_DOWN ) |
---|
984 | ret = tr_peerIoTryRead( io, limit ); |
---|
985 | else |
---|
986 | ret = tr_peerIoTryWrite( io, limit ); |
---|
987 | |
---|
988 | return ret; |
---|
989 | } |
---|
990 | |
---|
991 | struct evbuffer * |
---|
992 | tr_peerIoGetReadBuffer( tr_peerIo * io ) |
---|
993 | { |
---|
994 | assert( tr_isPeerIo( io ) ); |
---|
995 | |
---|
996 | return io->inbuf; |
---|
997 | } |
---|
998 | |
---|
999 | tr_bool |
---|
1000 | tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir ) |
---|
1001 | { |
---|
1002 | assert( tr_isPeerIo( io ) ); |
---|
1003 | assert( tr_isDirection( dir ) ); |
---|
1004 | |
---|
1005 | return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0; |
---|
1006 | } |
---|
1007 | |
---|
1008 | /*** |
---|
1009 | **** |
---|
1010 | ****/ |
---|
1011 | |
---|
1012 | static void |
---|
1013 | event_enable( tr_peerIo * io, short event ) |
---|
1014 | { |
---|
1015 | assert( tr_isPeerIo( io ) ); |
---|
1016 | |
---|
1017 | if( event & EV_READ ) |
---|
1018 | event_add( &io->event_read, NULL ); |
---|
1019 | |
---|
1020 | if( event & EV_WRITE ) |
---|
1021 | event_add( &io->event_write, NULL ); |
---|
1022 | } |
---|
1023 | |
---|
1024 | static void |
---|
1025 | event_disable( struct tr_peerIo * io, short event ) |
---|
1026 | { |
---|
1027 | assert( tr_isPeerIo( io ) ); |
---|
1028 | |
---|
1029 | if( event & EV_READ ) |
---|
1030 | event_del( &io->event_read ); |
---|
1031 | |
---|
1032 | if( event & EV_WRITE ) |
---|
1033 | event_del( &io->event_write ); |
---|
1034 | } |
---|
1035 | |
---|
1036 | |
---|
1037 | void |
---|
1038 | tr_peerIoSetEnabled( tr_peerIo * io, |
---|
1039 | tr_direction dir, |
---|
1040 | tr_bool isEnabled ) |
---|
1041 | { |
---|
1042 | short event; |
---|
1043 | |
---|
1044 | assert( tr_isPeerIo( io ) ); |
---|
1045 | assert( tr_isDirection( dir ) ); |
---|
1046 | |
---|
1047 | event = dir == TR_UP ? EV_WRITE : EV_READ; |
---|
1048 | |
---|
1049 | if( isEnabled ) |
---|
1050 | event_enable( io, event ); |
---|
1051 | else |
---|
1052 | event_disable( io, event ); |
---|
1053 | } |
---|