1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: peer-io.c 6453 2008-08-08 22:44:32Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <string.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <unistd.h> |
---|
17 | |
---|
18 | #ifdef WIN32 |
---|
19 | #include <winsock2.h> |
---|
20 | #else |
---|
21 | #include <netinet/in.h> /* struct in_addr */ |
---|
22 | #include <arpa/inet.h> /* inet_ntoa */ |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <event.h> |
---|
26 | |
---|
27 | #include "transmission.h" |
---|
28 | #include "crypto.h" |
---|
29 | #include "net.h" |
---|
30 | #include "peer-io.h" |
---|
31 | #include "ratecontrol.h" |
---|
32 | #include "trevent.h" |
---|
33 | #include "utils.h" |
---|
34 | |
---|
35 | #define IO_TIMEOUT_SECS 8 |
---|
36 | |
---|
37 | /* the size of a typical request message */ |
---|
38 | #define TR_RDBUF ((1024*16) + 13) |
---|
39 | |
---|
40 | /** |
---|
41 | *** |
---|
42 | **/ |
---|
43 | |
---|
44 | struct tr_peerIo |
---|
45 | { |
---|
46 | unsigned int isEncrypted : 1; |
---|
47 | unsigned int isIncoming : 1; |
---|
48 | unsigned int peerIdIsSet : 1; |
---|
49 | unsigned int extendedProtocolSupported : 1; |
---|
50 | unsigned int fastPeersSupported : 1; |
---|
51 | |
---|
52 | uint8_t encryptionMode; |
---|
53 | uint8_t timeout; |
---|
54 | uint16_t port; |
---|
55 | int socket; |
---|
56 | |
---|
57 | uint8_t peerId[20]; |
---|
58 | time_t timeCreated; |
---|
59 | |
---|
60 | struct tr_handle * handle; |
---|
61 | |
---|
62 | struct in_addr in_addr; |
---|
63 | struct bufferevent * bufev; |
---|
64 | |
---|
65 | tr_can_read_cb canRead; |
---|
66 | tr_did_write_cb didWrite; |
---|
67 | tr_net_error_cb gotError; |
---|
68 | void * userData; |
---|
69 | |
---|
70 | tr_crypto * crypto; |
---|
71 | }; |
---|
72 | |
---|
73 | /** |
---|
74 | *** |
---|
75 | **/ |
---|
76 | |
---|
77 | static void |
---|
78 | didWriteWrapper( struct bufferevent * e, void * userData ) |
---|
79 | { |
---|
80 | tr_peerIo * c = (tr_peerIo *) userData; |
---|
81 | if( c->didWrite ) |
---|
82 | c->didWrite( e, c->userData ); |
---|
83 | } |
---|
84 | |
---|
85 | static void |
---|
86 | canReadWrapper( struct bufferevent * e, void * userData ) |
---|
87 | { |
---|
88 | int done = 0; |
---|
89 | tr_peerIo * c = userData; |
---|
90 | tr_handle * handle = c->handle; |
---|
91 | |
---|
92 | if( c->canRead == NULL ) |
---|
93 | return; |
---|
94 | |
---|
95 | tr_globalLock( handle ); |
---|
96 | |
---|
97 | while( !done ) |
---|
98 | { |
---|
99 | const int ret = c->canRead( e, c->userData ); |
---|
100 | |
---|
101 | switch( ret ) |
---|
102 | { |
---|
103 | case READ_AGAIN: |
---|
104 | if( EVBUFFER_LENGTH( e->input ) ) |
---|
105 | continue; |
---|
106 | case READ_MORE: |
---|
107 | case READ_DONE: |
---|
108 | done = 1; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | tr_globalUnlock( handle ); |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | gotErrorWrapper( struct bufferevent * e, short what, void * userData ) |
---|
117 | { |
---|
118 | tr_peerIo * c = userData; |
---|
119 | if( c->gotError ) |
---|
120 | c->gotError( e, what, c->userData ); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | *** |
---|
125 | **/ |
---|
126 | |
---|
127 | void bufferevent_setwatermark(struct bufferevent *, short, size_t, size_t); |
---|
128 | |
---|
129 | static tr_peerIo* |
---|
130 | tr_peerIoNew( struct tr_handle * handle, |
---|
131 | const struct in_addr * in_addr, |
---|
132 | uint16_t port, |
---|
133 | const uint8_t * torrentHash, |
---|
134 | int isIncoming, |
---|
135 | int socket ) |
---|
136 | { |
---|
137 | tr_peerIo * c; |
---|
138 | |
---|
139 | if( socket >= 0 ) |
---|
140 | tr_netSetTOS( socket, handle->peerSocketTOS ); |
---|
141 | |
---|
142 | c = tr_new0( tr_peerIo, 1 ); |
---|
143 | c->crypto = tr_cryptoNew( torrentHash, isIncoming ); |
---|
144 | c->handle = handle; |
---|
145 | c->in_addr = *in_addr; |
---|
146 | c->port = port; |
---|
147 | c->socket = socket; |
---|
148 | c->isIncoming = isIncoming ? 1 : 0; |
---|
149 | c->timeout = IO_TIMEOUT_SECS; |
---|
150 | c->timeCreated = time( NULL ); |
---|
151 | c->bufev = bufferevent_new( c->socket, |
---|
152 | canReadWrapper, |
---|
153 | didWriteWrapper, |
---|
154 | gotErrorWrapper, |
---|
155 | c ); |
---|
156 | bufferevent_settimeout( c->bufev, c->timeout, c->timeout ); |
---|
157 | bufferevent_enable( c->bufev, EV_READ|EV_WRITE ); |
---|
158 | bufferevent_setwatermark( c->bufev, EV_READ, 0, TR_RDBUF ); |
---|
159 | |
---|
160 | return c; |
---|
161 | } |
---|
162 | |
---|
163 | tr_peerIo* |
---|
164 | tr_peerIoNewIncoming( struct tr_handle * handle, |
---|
165 | const struct in_addr * in_addr, |
---|
166 | uint16_t port, |
---|
167 | int socket ) |
---|
168 | { |
---|
169 | assert( handle ); |
---|
170 | assert( in_addr ); |
---|
171 | assert( socket >= 0 ); |
---|
172 | |
---|
173 | return tr_peerIoNew( handle, in_addr, port, |
---|
174 | NULL, 1, |
---|
175 | socket ); |
---|
176 | } |
---|
177 | |
---|
178 | tr_peerIo* |
---|
179 | tr_peerIoNewOutgoing( struct tr_handle * handle, |
---|
180 | const struct in_addr * in_addr, |
---|
181 | int port, |
---|
182 | const uint8_t * torrentHash ) |
---|
183 | { |
---|
184 | int socket; |
---|
185 | |
---|
186 | assert( handle ); |
---|
187 | assert( in_addr ); |
---|
188 | assert( port >= 0 ); |
---|
189 | assert( torrentHash ); |
---|
190 | |
---|
191 | socket = tr_netOpenTCP( in_addr, port, 0 ); |
---|
192 | |
---|
193 | return socket < 0 |
---|
194 | ? NULL |
---|
195 | : tr_peerIoNew( handle, in_addr, port, torrentHash, 0, socket ); |
---|
196 | } |
---|
197 | |
---|
198 | static void |
---|
199 | io_dtor( void * vio ) |
---|
200 | { |
---|
201 | tr_peerIo * io = vio; |
---|
202 | |
---|
203 | bufferevent_free( io->bufev ); |
---|
204 | tr_netClose( io->socket ); |
---|
205 | tr_cryptoFree( io->crypto ); |
---|
206 | tr_free( io ); |
---|
207 | } |
---|
208 | |
---|
209 | void |
---|
210 | tr_peerIoFree( tr_peerIo * io ) |
---|
211 | { |
---|
212 | if( io ) |
---|
213 | { |
---|
214 | io->canRead = NULL; |
---|
215 | io->didWrite = NULL; |
---|
216 | io->gotError = NULL; |
---|
217 | tr_runInEventThread( io->handle, io_dtor, io ); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | tr_handle* |
---|
222 | tr_peerIoGetHandle( tr_peerIo * io ) |
---|
223 | { |
---|
224 | assert( io ); |
---|
225 | assert( io->handle ); |
---|
226 | |
---|
227 | return io->handle; |
---|
228 | } |
---|
229 | |
---|
230 | const struct in_addr* |
---|
231 | tr_peerIoGetAddress( const tr_peerIo * io, uint16_t * port ) |
---|
232 | { |
---|
233 | assert( io ); |
---|
234 | |
---|
235 | if( port ) |
---|
236 | *port = io->port; |
---|
237 | |
---|
238 | return &io->in_addr; |
---|
239 | } |
---|
240 | |
---|
241 | const char* |
---|
242 | tr_peerIoAddrStr( const struct in_addr * addr, uint16_t port ) |
---|
243 | { |
---|
244 | static char buf[512]; |
---|
245 | tr_snprintf( buf, sizeof(buf), "%s:%u", inet_ntoa( *addr ), ntohs( port ) ); |
---|
246 | return buf; |
---|
247 | } |
---|
248 | |
---|
249 | const char* |
---|
250 | tr_peerIoGetAddrStr( const tr_peerIo * io ) |
---|
251 | { |
---|
252 | return tr_peerIoAddrStr( &io->in_addr, io->port ); |
---|
253 | } |
---|
254 | |
---|
255 | void |
---|
256 | tr_peerIoTryRead( tr_peerIo * io ) |
---|
257 | { |
---|
258 | if( EVBUFFER_LENGTH( io->bufev->input ) ) |
---|
259 | canReadWrapper( io->bufev, io ); |
---|
260 | } |
---|
261 | |
---|
262 | void |
---|
263 | tr_peerIoSetIOFuncs( tr_peerIo * io, |
---|
264 | tr_can_read_cb readcb, |
---|
265 | tr_did_write_cb writecb, |
---|
266 | tr_net_error_cb errcb, |
---|
267 | void * userData ) |
---|
268 | { |
---|
269 | io->canRead = readcb; |
---|
270 | io->didWrite = writecb; |
---|
271 | io->gotError = errcb; |
---|
272 | io->userData = userData; |
---|
273 | |
---|
274 | tr_peerIoTryRead( io ); |
---|
275 | } |
---|
276 | |
---|
277 | int |
---|
278 | tr_peerIoIsIncoming( const tr_peerIo * c ) |
---|
279 | { |
---|
280 | return c->isIncoming ? 1 : 0; |
---|
281 | } |
---|
282 | |
---|
283 | int |
---|
284 | tr_peerIoReconnect( tr_peerIo * io ) |
---|
285 | { |
---|
286 | assert( !tr_peerIoIsIncoming( io ) ); |
---|
287 | |
---|
288 | if( io->socket >= 0 ) |
---|
289 | tr_netClose( io->socket ); |
---|
290 | |
---|
291 | io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 ); |
---|
292 | |
---|
293 | if( io->socket >= 0 ) |
---|
294 | { |
---|
295 | tr_netSetTOS( io->socket, io->handle->peerSocketTOS ); |
---|
296 | |
---|
297 | bufferevent_free( io->bufev ); |
---|
298 | |
---|
299 | io->bufev = bufferevent_new( io->socket, |
---|
300 | canReadWrapper, |
---|
301 | didWriteWrapper, |
---|
302 | gotErrorWrapper, |
---|
303 | io ); |
---|
304 | bufferevent_settimeout( io->bufev, io->timeout, io->timeout ); |
---|
305 | bufferevent_enable( io->bufev, EV_READ|EV_WRITE ); |
---|
306 | bufferevent_setwatermark( io->bufev, EV_READ, 0, TR_RDBUF ); |
---|
307 | |
---|
308 | return 0; |
---|
309 | } |
---|
310 | |
---|
311 | return -1; |
---|
312 | } |
---|
313 | |
---|
314 | void |
---|
315 | tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs ) |
---|
316 | { |
---|
317 | io->timeout = secs; |
---|
318 | bufferevent_settimeout( io->bufev, io->timeout, io->timeout ); |
---|
319 | bufferevent_enable( io->bufev, EV_READ|EV_WRITE ); |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | *** |
---|
324 | **/ |
---|
325 | |
---|
326 | void |
---|
327 | tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
328 | const uint8_t * hash ) |
---|
329 | { |
---|
330 | assert( io ); |
---|
331 | |
---|
332 | tr_cryptoSetTorrentHash( io->crypto, hash ); |
---|
333 | } |
---|
334 | |
---|
335 | const uint8_t* |
---|
336 | tr_peerIoGetTorrentHash( tr_peerIo * io ) |
---|
337 | { |
---|
338 | assert( io ); |
---|
339 | assert( io->crypto ); |
---|
340 | |
---|
341 | return tr_cryptoGetTorrentHash( io->crypto ); |
---|
342 | } |
---|
343 | |
---|
344 | int |
---|
345 | tr_peerIoHasTorrentHash( const tr_peerIo * io ) |
---|
346 | { |
---|
347 | assert( io ); |
---|
348 | assert( io->crypto ); |
---|
349 | |
---|
350 | return tr_cryptoHasTorrentHash( io->crypto ); |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | *** |
---|
355 | **/ |
---|
356 | |
---|
357 | void |
---|
358 | tr_peerIoSetPeersId( tr_peerIo * io, |
---|
359 | const uint8_t * peer_id ) |
---|
360 | { |
---|
361 | assert( io ); |
---|
362 | |
---|
363 | if(( io->peerIdIsSet = peer_id != NULL )) |
---|
364 | memcpy( io->peerId, peer_id, 20 ); |
---|
365 | else |
---|
366 | memset( io->peerId, 0, 20 ); |
---|
367 | } |
---|
368 | |
---|
369 | const uint8_t* |
---|
370 | tr_peerIoGetPeersId( const tr_peerIo * io ) |
---|
371 | { |
---|
372 | assert( io ); |
---|
373 | assert( io->peerIdIsSet ); |
---|
374 | |
---|
375 | return io->peerId; |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | *** |
---|
380 | **/ |
---|
381 | |
---|
382 | void |
---|
383 | tr_peerIoEnableLTEP( tr_peerIo * io, int flag ) |
---|
384 | { |
---|
385 | assert( io ); |
---|
386 | assert( flag==0 || flag==1 ); |
---|
387 | |
---|
388 | io->extendedProtocolSupported = flag; |
---|
389 | } |
---|
390 | |
---|
391 | void |
---|
392 | tr_peerIoEnableFEXT( tr_peerIo * io, int flag ) |
---|
393 | { |
---|
394 | assert( io ); |
---|
395 | assert( flag==0 || flag==1 ); |
---|
396 | |
---|
397 | io->fastPeersSupported = flag; |
---|
398 | } |
---|
399 | |
---|
400 | int |
---|
401 | tr_peerIoSupportsLTEP( const tr_peerIo * io ) |
---|
402 | { |
---|
403 | assert( io ); |
---|
404 | |
---|
405 | return io->extendedProtocolSupported; |
---|
406 | } |
---|
407 | |
---|
408 | int |
---|
409 | tr_peerIoSupportsFEXT( const tr_peerIo * io ) |
---|
410 | { |
---|
411 | assert( io ); |
---|
412 | |
---|
413 | return io->fastPeersSupported; |
---|
414 | } |
---|
415 | /** |
---|
416 | *** |
---|
417 | **/ |
---|
418 | |
---|
419 | size_t |
---|
420 | tr_peerIoWriteBytesWaiting( const tr_peerIo * io ) |
---|
421 | { |
---|
422 | return EVBUFFER_LENGTH( EVBUFFER_OUTPUT( io->bufev ) ); |
---|
423 | } |
---|
424 | |
---|
425 | void |
---|
426 | tr_peerIoWrite( tr_peerIo * io, |
---|
427 | const void * writeme, |
---|
428 | int writeme_len ) |
---|
429 | { |
---|
430 | assert( tr_amInEventThread( io->handle ) ); |
---|
431 | bufferevent_write( io->bufev, writeme, writeme_len ); |
---|
432 | } |
---|
433 | |
---|
434 | void |
---|
435 | tr_peerIoWriteBuf( tr_peerIo * io, |
---|
436 | struct evbuffer * buf ) |
---|
437 | { |
---|
438 | const size_t n = EVBUFFER_LENGTH( buf ); |
---|
439 | tr_peerIoWrite( io, EVBUFFER_DATA(buf), n ); |
---|
440 | evbuffer_drain( buf, n ); |
---|
441 | } |
---|
442 | |
---|
443 | /** |
---|
444 | *** |
---|
445 | **/ |
---|
446 | |
---|
447 | tr_crypto* |
---|
448 | tr_peerIoGetCrypto( tr_peerIo * c ) |
---|
449 | { |
---|
450 | return c->crypto; |
---|
451 | } |
---|
452 | |
---|
453 | void |
---|
454 | tr_peerIoSetEncryption( tr_peerIo * io, |
---|
455 | int encryptionMode ) |
---|
456 | { |
---|
457 | assert( io ); |
---|
458 | assert( encryptionMode==PEER_ENCRYPTION_NONE || encryptionMode==PEER_ENCRYPTION_RC4 ); |
---|
459 | |
---|
460 | io->encryptionMode = encryptionMode; |
---|
461 | } |
---|
462 | |
---|
463 | int |
---|
464 | tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
465 | { |
---|
466 | return io!=NULL && io->encryptionMode==PEER_ENCRYPTION_RC4; |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | *** |
---|
471 | **/ |
---|
472 | |
---|
473 | void |
---|
474 | tr_peerIoWriteBytes( tr_peerIo * io, |
---|
475 | struct evbuffer * outbuf, |
---|
476 | const void * bytes, |
---|
477 | size_t byteCount ) |
---|
478 | { |
---|
479 | uint8_t * tmp; |
---|
480 | |
---|
481 | switch( io->encryptionMode ) |
---|
482 | { |
---|
483 | case PEER_ENCRYPTION_NONE: |
---|
484 | evbuffer_add( outbuf, bytes, byteCount ); |
---|
485 | break; |
---|
486 | |
---|
487 | case PEER_ENCRYPTION_RC4: |
---|
488 | tmp = tr_new( uint8_t, byteCount ); |
---|
489 | tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp ); |
---|
490 | evbuffer_add( outbuf, tmp, byteCount ); |
---|
491 | tr_free( tmp ); |
---|
492 | break; |
---|
493 | |
---|
494 | default: |
---|
495 | assert( 0 ); |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | void |
---|
500 | tr_peerIoWriteUint8( tr_peerIo * io, |
---|
501 | struct evbuffer * outbuf, |
---|
502 | uint8_t writeme ) |
---|
503 | { |
---|
504 | tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof(uint8_t) ); |
---|
505 | } |
---|
506 | |
---|
507 | void |
---|
508 | tr_peerIoWriteUint16( tr_peerIo * io, |
---|
509 | struct evbuffer * outbuf, |
---|
510 | uint16_t writeme ) |
---|
511 | { |
---|
512 | uint16_t tmp = htons( writeme ); |
---|
513 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint16_t) ); |
---|
514 | } |
---|
515 | |
---|
516 | void |
---|
517 | tr_peerIoWriteUint32( tr_peerIo * io, |
---|
518 | struct evbuffer * outbuf, |
---|
519 | uint32_t writeme ) |
---|
520 | { |
---|
521 | uint32_t tmp = htonl( writeme ); |
---|
522 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint32_t) ); |
---|
523 | } |
---|
524 | |
---|
525 | /*** |
---|
526 | **** |
---|
527 | ***/ |
---|
528 | |
---|
529 | void |
---|
530 | tr_peerIoReadBytes( tr_peerIo * io, |
---|
531 | struct evbuffer * inbuf, |
---|
532 | void * bytes, |
---|
533 | size_t byteCount ) |
---|
534 | { |
---|
535 | assert( EVBUFFER_LENGTH( inbuf ) >= byteCount ); |
---|
536 | |
---|
537 | switch( io->encryptionMode ) |
---|
538 | { |
---|
539 | case PEER_ENCRYPTION_NONE: |
---|
540 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
541 | break; |
---|
542 | |
---|
543 | case PEER_ENCRYPTION_RC4: |
---|
544 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
545 | tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes ); |
---|
546 | break; |
---|
547 | |
---|
548 | default: |
---|
549 | assert( 0 ); |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | void |
---|
554 | tr_peerIoReadUint8( tr_peerIo * io, |
---|
555 | struct evbuffer * inbuf, |
---|
556 | uint8_t * setme ) |
---|
557 | { |
---|
558 | tr_peerIoReadBytes( io, inbuf, setme, sizeof(uint8_t) ); |
---|
559 | } |
---|
560 | |
---|
561 | void |
---|
562 | tr_peerIoReadUint16( tr_peerIo * io, |
---|
563 | struct evbuffer * inbuf, |
---|
564 | uint16_t * setme ) |
---|
565 | { |
---|
566 | uint16_t tmp; |
---|
567 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof(uint16_t) ); |
---|
568 | *setme = ntohs( tmp ); |
---|
569 | } |
---|
570 | |
---|
571 | void |
---|
572 | tr_peerIoReadUint32( tr_peerIo * io, |
---|
573 | struct evbuffer * inbuf, |
---|
574 | uint32_t * setme ) |
---|
575 | { |
---|
576 | uint32_t tmp; |
---|
577 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof(uint32_t) ); |
---|
578 | *setme = ntohl( tmp ); |
---|
579 | } |
---|
580 | |
---|
581 | void |
---|
582 | tr_peerIoDrain( tr_peerIo * io, |
---|
583 | struct evbuffer * inbuf, |
---|
584 | size_t byteCount ) |
---|
585 | { |
---|
586 | uint8_t * tmp = tr_new( uint8_t, byteCount ); |
---|
587 | tr_peerIoReadBytes( io, inbuf, tmp, byteCount ); |
---|
588 | tr_free( tmp ); |
---|
589 | } |
---|
590 | |
---|
591 | int |
---|
592 | tr_peerIoGetAge( const tr_peerIo * io ) |
---|
593 | { |
---|
594 | return time( NULL ) - io->timeCreated; |
---|
595 | } |
---|