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