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 2995 2007-09-09 01:52:14Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <string.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <unistd.h> |
---|
17 | #include <arpa/inet.h> |
---|
18 | #include <event.h> |
---|
19 | #include "transmission.h" |
---|
20 | #include "crypto.h" |
---|
21 | #include "net.h" |
---|
22 | #include "peer-io.h" |
---|
23 | #include "ratecontrol.h" |
---|
24 | #include "trevent.h" |
---|
25 | #include "utils.h" |
---|
26 | |
---|
27 | /** |
---|
28 | *** |
---|
29 | **/ |
---|
30 | |
---|
31 | struct tr_peerIo |
---|
32 | { |
---|
33 | struct tr_handle * handle; |
---|
34 | |
---|
35 | struct in_addr in_addr; |
---|
36 | int port; |
---|
37 | int socket; |
---|
38 | int extensions; |
---|
39 | int encryptionMode; |
---|
40 | struct bufferevent * bufev; |
---|
41 | uint8_t peerId[20]; |
---|
42 | |
---|
43 | unsigned int isEncrypted : 1; |
---|
44 | unsigned int isIncoming : 1; |
---|
45 | unsigned int peerIdIsSet : 1; |
---|
46 | |
---|
47 | tr_ratecontrol * rateToPeer; |
---|
48 | tr_ratecontrol * rateToClient; |
---|
49 | |
---|
50 | tr_can_read_cb canRead; |
---|
51 | tr_did_write_cb didWrite; |
---|
52 | tr_net_error_cb gotError; |
---|
53 | void * userData; |
---|
54 | |
---|
55 | tr_crypto * crypto; |
---|
56 | }; |
---|
57 | |
---|
58 | /** |
---|
59 | *** |
---|
60 | **/ |
---|
61 | |
---|
62 | static void |
---|
63 | didWriteWrapper( struct bufferevent * e, void * userData ) |
---|
64 | { |
---|
65 | tr_peerIo * c = (tr_peerIo *) userData; |
---|
66 | if( c->didWrite != NULL ) |
---|
67 | (*c->didWrite)( e, c->userData ); |
---|
68 | } |
---|
69 | |
---|
70 | static void |
---|
71 | canReadWrapper( struct bufferevent * e, void * userData ) |
---|
72 | { |
---|
73 | tr_peerIo * c = (tr_peerIo *) userData; |
---|
74 | |
---|
75 | if( c->canRead == NULL ) |
---|
76 | return; |
---|
77 | |
---|
78 | for( ;; ) { |
---|
79 | const int ret = (*c->canRead)( e, c->userData ); |
---|
80 | switch( ret ) { |
---|
81 | case READ_AGAIN: if( EVBUFFER_LENGTH( e->input ) ) continue; /* note fall-through */ |
---|
82 | case READ_MORE: //fprintf( stderr, "waiting for bytes from peer...\n" ); |
---|
83 | tr_peerIoSetIOMode( c, EV_READ, 0 ); return; break; |
---|
84 | case READ_DONE: return; fprintf( stderr, "READ_DONE\n"); break; |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | static void |
---|
90 | gotErrorWrapper( struct bufferevent * e, short what, void * userData ) |
---|
91 | { |
---|
92 | tr_peerIo * c = (tr_peerIo *) userData; |
---|
93 | assert( c->gotError != NULL ); |
---|
94 | (*c->gotError)( e, what, c->userData ); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | *** |
---|
99 | **/ |
---|
100 | |
---|
101 | static tr_peerIo* |
---|
102 | tr_peerIoNew( struct tr_handle * handle, |
---|
103 | struct in_addr * in_addr, |
---|
104 | const uint8_t * torrentHash, |
---|
105 | int isIncoming, |
---|
106 | int socket ) |
---|
107 | { |
---|
108 | tr_peerIo * c; |
---|
109 | c = tr_new0( tr_peerIo, 1 ); |
---|
110 | c->crypto = tr_cryptoNew( torrentHash, isIncoming ); |
---|
111 | c->handle = handle; |
---|
112 | c->in_addr = *in_addr; |
---|
113 | c->socket = socket; |
---|
114 | c->rateToPeer = tr_rcInit( ); |
---|
115 | c->rateToClient = tr_rcInit( ); |
---|
116 | c->isIncoming = isIncoming ? 1 : 0; |
---|
117 | fprintf( stderr, "io %p rates: peer %p client %p\n", c, c->rateToPeer, c->rateToClient ); |
---|
118 | c->bufev = bufferevent_new( c->socket, |
---|
119 | canReadWrapper, |
---|
120 | didWriteWrapper, |
---|
121 | gotErrorWrapper, |
---|
122 | c ); |
---|
123 | bufferevent_enable( c->bufev, EV_READ|EV_WRITE ); |
---|
124 | return c; |
---|
125 | } |
---|
126 | |
---|
127 | tr_peerIo* |
---|
128 | tr_peerIoNewIncoming( struct tr_handle * handle, |
---|
129 | struct in_addr * in_addr, |
---|
130 | int socket ) |
---|
131 | { |
---|
132 | tr_peerIo * c; |
---|
133 | |
---|
134 | assert( handle != NULL ); |
---|
135 | assert( in_addr != NULL ); |
---|
136 | assert( socket >= 0 ); |
---|
137 | |
---|
138 | c = tr_peerIoNew( handle, in_addr, NULL, 1, socket ); |
---|
139 | c->port = -1; |
---|
140 | return c; |
---|
141 | } |
---|
142 | |
---|
143 | tr_peerIo* |
---|
144 | tr_peerIoNewOutgoing( struct tr_handle * handle, |
---|
145 | struct in_addr * in_addr, |
---|
146 | int port, |
---|
147 | const uint8_t * torrentHash ) |
---|
148 | { |
---|
149 | tr_peerIo * c; |
---|
150 | |
---|
151 | assert( handle != NULL ); |
---|
152 | assert( in_addr != NULL ); |
---|
153 | assert( port >= 0 ); |
---|
154 | assert( torrentHash != NULL ); |
---|
155 | |
---|
156 | c = tr_peerIoNew( handle, in_addr, torrentHash, 0, |
---|
157 | tr_netOpenTCP( in_addr, port, 0 ) ); |
---|
158 | c->port = port; |
---|
159 | return c; |
---|
160 | } |
---|
161 | |
---|
162 | void |
---|
163 | tr_peerIoFree( tr_peerIo * c ) |
---|
164 | { |
---|
165 | if( c != NULL ) |
---|
166 | { |
---|
167 | bufferevent_free( c->bufev ); |
---|
168 | fprintf( stderr, "io %p destroying rate to client %p to peer %p\n", c, c->rateToClient, c->rateToPeer ); |
---|
169 | tr_rcClose( c->rateToClient ); |
---|
170 | tr_rcClose( c->rateToPeer ); |
---|
171 | tr_netClose( c->socket ); |
---|
172 | tr_cryptoFree( c->crypto ); |
---|
173 | tr_free( c ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | tr_handle* |
---|
178 | tr_peerIoGetHandle( tr_peerIo * io ) |
---|
179 | { |
---|
180 | assert( io != NULL ); |
---|
181 | assert( io->handle != NULL ); |
---|
182 | |
---|
183 | return io->handle; |
---|
184 | } |
---|
185 | |
---|
186 | const struct in_addr* |
---|
187 | tr_peerIoGetAddress( const tr_peerIo * io, uint16_t * port ) |
---|
188 | { |
---|
189 | assert( io != NULL ); |
---|
190 | |
---|
191 | if( port != NULL ) |
---|
192 | *port = io->port; |
---|
193 | |
---|
194 | return &io->in_addr; |
---|
195 | } |
---|
196 | |
---|
197 | void |
---|
198 | tr_peerIoSetIOFuncs( tr_peerIo * io, |
---|
199 | tr_can_read_cb readcb, |
---|
200 | tr_did_write_cb writecb, |
---|
201 | tr_net_error_cb errcb, |
---|
202 | void * userData ) |
---|
203 | { |
---|
204 | io->canRead = readcb; |
---|
205 | io->didWrite = writecb; |
---|
206 | io->gotError = errcb; |
---|
207 | io->userData = userData; |
---|
208 | |
---|
209 | if( EVBUFFER_LENGTH( io->bufev->input ) ) |
---|
210 | canReadWrapper( io->bufev, io ); |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | tr_peerIoSetIOMode( tr_peerIo * c, short enable, short disable ) |
---|
215 | { |
---|
216 | tr_setBufferEventMode( c->handle, c->bufev, enable, disable ); |
---|
217 | } |
---|
218 | |
---|
219 | int |
---|
220 | tr_peerIoIsIncoming( const tr_peerIo * c ) |
---|
221 | { |
---|
222 | return c->isIncoming ? 1 : 0; |
---|
223 | } |
---|
224 | |
---|
225 | int |
---|
226 | tr_peerIoReconnect( tr_peerIo * io ) |
---|
227 | { |
---|
228 | assert( !tr_peerIoIsIncoming( io ) ); |
---|
229 | |
---|
230 | fprintf( stderr, "tr_peerIoReconnect: io %p\n", io ); |
---|
231 | |
---|
232 | if( io->socket >= 0 ) |
---|
233 | tr_netClose( io->socket ); |
---|
234 | |
---|
235 | io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 ); |
---|
236 | |
---|
237 | fprintf( stderr, "tr_peerIoReconnect: io->socket is %d\n", io->socket ); |
---|
238 | |
---|
239 | if( io->socket >= 0 ) |
---|
240 | { |
---|
241 | bufferevent_free( io->bufev ); |
---|
242 | |
---|
243 | io->bufev = bufferevent_new( io->socket, |
---|
244 | canReadWrapper, |
---|
245 | didWriteWrapper, |
---|
246 | gotErrorWrapper, |
---|
247 | io ); |
---|
248 | bufferevent_enable( io->bufev, EV_READ|EV_WRITE ); |
---|
249 | |
---|
250 | return 0; |
---|
251 | } |
---|
252 | |
---|
253 | return -1; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | *** |
---|
258 | **/ |
---|
259 | |
---|
260 | void |
---|
261 | tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
262 | const uint8_t * hash ) |
---|
263 | { |
---|
264 | assert( io != NULL ); |
---|
265 | |
---|
266 | tr_cryptoSetTorrentHash( io->crypto, hash ); |
---|
267 | } |
---|
268 | |
---|
269 | const uint8_t* |
---|
270 | tr_peerIoGetTorrentHash( tr_peerIo * io ) |
---|
271 | { |
---|
272 | assert( io != NULL ); |
---|
273 | assert( io->crypto != NULL ); |
---|
274 | |
---|
275 | return tr_cryptoGetTorrentHash( io->crypto ); |
---|
276 | } |
---|
277 | |
---|
278 | int |
---|
279 | tr_peerIoHasTorrentHash( const tr_peerIo * io ) |
---|
280 | { |
---|
281 | assert( io != NULL ); |
---|
282 | assert( io->crypto != NULL ); |
---|
283 | |
---|
284 | return tr_cryptoHasTorrentHash( io->crypto ); |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | *** |
---|
289 | **/ |
---|
290 | |
---|
291 | void |
---|
292 | tr_peerIoSetPeersId( tr_peerIo * io, |
---|
293 | const uint8_t * peer_id ) |
---|
294 | { |
---|
295 | assert( io != NULL ); |
---|
296 | |
---|
297 | if(( io->peerIdIsSet = peer_id != NULL )) |
---|
298 | memcpy( io->peerId, peer_id, 20 ); |
---|
299 | else |
---|
300 | memset( io->peerId, 0, 20 ); |
---|
301 | } |
---|
302 | |
---|
303 | const uint8_t* |
---|
304 | tr_peerIoGetPeersId( const tr_peerIo * io ) |
---|
305 | { |
---|
306 | assert( io != NULL ); |
---|
307 | assert( io->peerIdIsSet ); |
---|
308 | |
---|
309 | return io->peerId; |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | *** |
---|
314 | **/ |
---|
315 | |
---|
316 | void |
---|
317 | tr_peerIoSetExtension( tr_peerIo * io, |
---|
318 | int extensions ) |
---|
319 | { |
---|
320 | assert( io != NULL ); |
---|
321 | assert( ( extensions == LT_EXTENSIONS_NONE ) |
---|
322 | || ( extensions == LT_EXTENSIONS_LTEP ) |
---|
323 | || ( extensions == LT_EXTENSIONS_AZMP ) ); |
---|
324 | |
---|
325 | io->extensions = extensions; |
---|
326 | } |
---|
327 | |
---|
328 | int |
---|
329 | tr_peerIoGetExtension( const tr_peerIo * io ) |
---|
330 | { |
---|
331 | assert( io != NULL ); |
---|
332 | |
---|
333 | return io->extensions; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | *** |
---|
338 | **/ |
---|
339 | |
---|
340 | void |
---|
341 | tr_peerIoWrite( tr_peerIo * io, |
---|
342 | const void * writeme, |
---|
343 | int writeme_len ) |
---|
344 | { |
---|
345 | tr_bufferevent_write( io->handle, io->bufev, writeme, writeme_len ); |
---|
346 | tr_rcTransferred( io->rateToPeer, writeme_len ); |
---|
347 | } |
---|
348 | |
---|
349 | void |
---|
350 | tr_peerIoWriteBuf( tr_peerIo * io, |
---|
351 | const struct evbuffer * buf ) |
---|
352 | { |
---|
353 | tr_peerIoWrite( io, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf) ); |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | *** |
---|
358 | **/ |
---|
359 | |
---|
360 | tr_crypto* |
---|
361 | tr_peerIoGetCrypto( tr_peerIo * c ) |
---|
362 | { |
---|
363 | return c->crypto; |
---|
364 | } |
---|
365 | |
---|
366 | void |
---|
367 | tr_peerIoSetEncryption( tr_peerIo * io, |
---|
368 | int encryptionMode ) |
---|
369 | { |
---|
370 | assert( io != NULL ); |
---|
371 | assert( encryptionMode==PEER_ENCRYPTION_PLAINTEXT || encryptionMode==PEER_ENCRYPTION_RC4 ); |
---|
372 | |
---|
373 | io->encryptionMode = encryptionMode; |
---|
374 | } |
---|
375 | |
---|
376 | int |
---|
377 | tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
378 | { |
---|
379 | return io!=NULL && io->encryptionMode==PEER_ENCRYPTION_RC4; |
---|
380 | } |
---|
381 | |
---|
382 | void |
---|
383 | tr_peerIoWriteBytes( tr_peerIo * io, |
---|
384 | struct evbuffer * outbuf, |
---|
385 | const void * bytes, |
---|
386 | int byteCount ) |
---|
387 | { |
---|
388 | uint8_t * tmp; |
---|
389 | |
---|
390 | switch( io->encryptionMode ) |
---|
391 | { |
---|
392 | case PEER_ENCRYPTION_PLAINTEXT: |
---|
393 | fprintf( stderr, "writing %d plaintext bytes to outbuf...\n", byteCount ); |
---|
394 | evbuffer_add( outbuf, bytes, byteCount ); |
---|
395 | break; |
---|
396 | |
---|
397 | case PEER_ENCRYPTION_RC4: |
---|
398 | fprintf( stderr, "encrypting and writing %d bytes to outbuf...\n", byteCount ); |
---|
399 | tmp = tr_new( uint8_t, byteCount ); |
---|
400 | tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp ); |
---|
401 | tr_bufferevent_write( io->handle, io->bufev, tmp, byteCount ); |
---|
402 | tr_free( tmp ); |
---|
403 | break; |
---|
404 | |
---|
405 | default: |
---|
406 | assert( 0 ); |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | void |
---|
411 | tr_peerIoWriteUint16( tr_peerIo * io, |
---|
412 | struct evbuffer * outbuf, |
---|
413 | uint16_t writeme ) |
---|
414 | { |
---|
415 | uint16_t tmp = htons( writeme ); |
---|
416 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint16_t) ); |
---|
417 | } |
---|
418 | |
---|
419 | void |
---|
420 | tr_peerIoWriteUint32( tr_peerIo * io, |
---|
421 | struct evbuffer * outbuf, |
---|
422 | uint32_t writeme ) |
---|
423 | { |
---|
424 | uint32_t tmp = htonl( writeme ); |
---|
425 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint32_t) ); |
---|
426 | } |
---|
427 | |
---|
428 | void |
---|
429 | tr_peerIoReadBytes( tr_peerIo * io, |
---|
430 | struct evbuffer * inbuf, |
---|
431 | void * bytes, |
---|
432 | int byteCount ) |
---|
433 | { |
---|
434 | assert( (int)EVBUFFER_LENGTH( inbuf ) >= byteCount ); |
---|
435 | |
---|
436 | switch( io->encryptionMode ) |
---|
437 | { |
---|
438 | case PEER_ENCRYPTION_PLAINTEXT: |
---|
439 | fprintf( stderr, "reading %d plaintext bytes from inbuf...\n", byteCount ); |
---|
440 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
441 | tr_rcTransferred( io->rateToClient, byteCount ); |
---|
442 | break; |
---|
443 | |
---|
444 | case PEER_ENCRYPTION_RC4: |
---|
445 | fprintf( stderr, "reading AND DECRYPTING %d bytes from inbuf...\n", byteCount ); |
---|
446 | evbuffer_remove( inbuf, bytes, byteCount ); |
---|
447 | tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes ); |
---|
448 | tr_rcTransferred( io->rateToClient, byteCount ); |
---|
449 | break; |
---|
450 | |
---|
451 | default: |
---|
452 | assert( 0 ); |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | void |
---|
457 | tr_peerIoReadUint16( tr_peerIo * io, |
---|
458 | struct evbuffer * inbuf, |
---|
459 | uint16_t * setme ) |
---|
460 | { |
---|
461 | uint16_t tmp; |
---|
462 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof(uint16_t) ); |
---|
463 | *setme = ntohs( tmp ); |
---|
464 | } |
---|
465 | |
---|
466 | void |
---|
467 | tr_peerIoReadUint32( tr_peerIo * io, |
---|
468 | struct evbuffer * inbuf, |
---|
469 | uint32_t * setme ) |
---|
470 | { |
---|
471 | uint32_t tmp; |
---|
472 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof(uint32_t) ); |
---|
473 | *setme = ntohl( tmp ); |
---|
474 | } |
---|
475 | |
---|
476 | void |
---|
477 | tr_peerIoDrain( tr_peerIo * io, |
---|
478 | struct evbuffer * inbuf, |
---|
479 | int byteCount ) |
---|
480 | { |
---|
481 | uint8_t * tmp = tr_new( uint8_t, byteCount ); |
---|
482 | tr_peerIoReadBytes( io, inbuf, tmp, byteCount ); |
---|
483 | tr_free( tmp ); |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | *** |
---|
488 | **/ |
---|
489 | |
---|
490 | float |
---|
491 | tr_peerIoGetRateToClient( const tr_peerIo * io ) |
---|
492 | { |
---|
493 | return io==NULL ? 0.0f : tr_rcRate( io->rateToClient ); |
---|
494 | |
---|
495 | } |
---|
496 | |
---|
497 | float |
---|
498 | tr_peerIoGetRateToPeer( const tr_peerIo * io ) |
---|
499 | { |
---|
500 | return io==NULL ? 0.0f : tr_rcRate( io->rateToPeer ); |
---|
501 | } |
---|
502 | |
---|