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