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