1 | /* |
---|
2 | * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.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.h 8903 2009-08-12 14:40:32Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_PEER_IO_H |
---|
18 | #define TR_PEER_IO_H |
---|
19 | |
---|
20 | /** |
---|
21 | *** |
---|
22 | **/ |
---|
23 | |
---|
24 | #include <assert.h> |
---|
25 | |
---|
26 | #include <event.h> |
---|
27 | |
---|
28 | #include "transmission.h" |
---|
29 | #include "bandwidth.h" |
---|
30 | #include "list.h" /* __tr_list */ |
---|
31 | #include "net.h" /* tr_address */ |
---|
32 | |
---|
33 | struct evbuffer; |
---|
34 | struct tr_bandwidth; |
---|
35 | struct tr_crypto; |
---|
36 | struct tr_peerIo; |
---|
37 | |
---|
38 | /** |
---|
39 | * @addtogroup networked_io Networked IO |
---|
40 | * @{ |
---|
41 | */ |
---|
42 | |
---|
43 | typedef enum |
---|
44 | { |
---|
45 | READ_NOW, |
---|
46 | READ_LATER, |
---|
47 | READ_ERR |
---|
48 | } |
---|
49 | ReadState; |
---|
50 | |
---|
51 | typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io, |
---|
52 | void * user_data, |
---|
53 | size_t * setme_piece_byte_count ); |
---|
54 | |
---|
55 | typedef void ( *tr_did_write_cb )( struct tr_peerIo * io, |
---|
56 | size_t bytesWritten, |
---|
57 | int wasPieceData, |
---|
58 | void * userData ); |
---|
59 | |
---|
60 | typedef void ( *tr_net_error_cb )( struct tr_peerIo * io, |
---|
61 | short what, |
---|
62 | void * userData ); |
---|
63 | |
---|
64 | typedef struct tr_peerIo |
---|
65 | { |
---|
66 | tr_bool isEncrypted; |
---|
67 | tr_bool isIncoming; |
---|
68 | tr_bool peerIdIsSet; |
---|
69 | tr_bool extendedProtocolSupported; |
---|
70 | tr_bool fastExtensionSupported; |
---|
71 | tr_bool dhtSupported; |
---|
72 | |
---|
73 | /* we create the socket in a nonblocking way, so this flag is initially |
---|
74 | * false and then set to true when libevent says that the socket is ready |
---|
75 | * for reading or writing */ |
---|
76 | tr_bool hasFinishedConnecting; |
---|
77 | |
---|
78 | tr_priority_t priority; |
---|
79 | |
---|
80 | int pendingEvents; |
---|
81 | |
---|
82 | int magicNumber; |
---|
83 | |
---|
84 | uint8_t encryptionMode; |
---|
85 | |
---|
86 | tr_port port; |
---|
87 | int socket; |
---|
88 | |
---|
89 | int refCount; |
---|
90 | |
---|
91 | uint8_t peerId[SHA_DIGEST_LENGTH]; |
---|
92 | time_t timeCreated; |
---|
93 | |
---|
94 | tr_session * session; |
---|
95 | |
---|
96 | tr_address addr; |
---|
97 | |
---|
98 | tr_can_read_cb canRead; |
---|
99 | tr_did_write_cb didWrite; |
---|
100 | tr_net_error_cb gotError; |
---|
101 | void * userData; |
---|
102 | |
---|
103 | struct tr_bandwidth bandwidth; |
---|
104 | struct tr_crypto * crypto; |
---|
105 | |
---|
106 | struct evbuffer * inbuf; |
---|
107 | struct evbuffer * outbuf; |
---|
108 | struct __tr_list outbuf_datatypes; /* struct tr_datatype */ |
---|
109 | |
---|
110 | struct event event_read; |
---|
111 | struct event event_write; |
---|
112 | } |
---|
113 | tr_peerIo; |
---|
114 | |
---|
115 | /** |
---|
116 | *** |
---|
117 | **/ |
---|
118 | |
---|
119 | tr_peerIo* tr_peerIoNewOutgoing( tr_session * session, |
---|
120 | struct tr_bandwidth * parent, |
---|
121 | const struct tr_address * addr, |
---|
122 | tr_port port, |
---|
123 | const uint8_t * torrentHash ); |
---|
124 | |
---|
125 | tr_peerIo* tr_peerIoNewIncoming( tr_session * session, |
---|
126 | struct tr_bandwidth * parent, |
---|
127 | const struct tr_address * addr, |
---|
128 | tr_port port, |
---|
129 | int socket ); |
---|
130 | |
---|
131 | void tr_peerIoRefImpl ( const char * file, |
---|
132 | int line, |
---|
133 | tr_peerIo * io ); |
---|
134 | |
---|
135 | #define tr_peerIoRef(io) tr_peerIoRefImpl( __FILE__, __LINE__, (io) ); |
---|
136 | |
---|
137 | void tr_peerIoUnrefImpl ( const char * file, |
---|
138 | int line, |
---|
139 | tr_peerIo * io ); |
---|
140 | |
---|
141 | #define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) ); |
---|
142 | |
---|
143 | tr_bool tr_isPeerIo ( const tr_peerIo * io ); |
---|
144 | |
---|
145 | |
---|
146 | /** |
---|
147 | *** |
---|
148 | **/ |
---|
149 | |
---|
150 | void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag ); |
---|
151 | |
---|
152 | static TR_INLINE tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io ) |
---|
153 | { |
---|
154 | assert( tr_isPeerIo( io ) ); |
---|
155 | |
---|
156 | return io->extendedProtocolSupported; |
---|
157 | } |
---|
158 | |
---|
159 | void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag ); |
---|
160 | |
---|
161 | static TR_INLINE tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io ) |
---|
162 | { |
---|
163 | assert( tr_isPeerIo( io ) ); |
---|
164 | |
---|
165 | return io->fastExtensionSupported; |
---|
166 | } |
---|
167 | |
---|
168 | void tr_peerIoEnableDHT( tr_peerIo * io, tr_bool flag ); |
---|
169 | |
---|
170 | static TR_INLINE tr_bool tr_peerIoSupportsDHT( const tr_peerIo * io ) |
---|
171 | { |
---|
172 | assert( tr_isPeerIo( io ) ); |
---|
173 | |
---|
174 | return io->dhtSupported; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | *** |
---|
179 | **/ |
---|
180 | |
---|
181 | static TR_INLINE tr_session* tr_peerIoGetSession ( tr_peerIo * io ) |
---|
182 | { |
---|
183 | assert( tr_isPeerIo( io ) ); |
---|
184 | assert( io->session ); |
---|
185 | |
---|
186 | return io->session; |
---|
187 | } |
---|
188 | |
---|
189 | const char* tr_peerIoAddrStr( const struct tr_address * addr, |
---|
190 | tr_port port ); |
---|
191 | |
---|
192 | static TR_INLINE const char* tr_peerIoGetAddrStr( const tr_peerIo * io ) |
---|
193 | { |
---|
194 | return tr_isPeerIo( io ) ? tr_peerIoAddrStr( &io->addr, io->port ) : "error"; |
---|
195 | } |
---|
196 | |
---|
197 | const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io, |
---|
198 | tr_port * port ); |
---|
199 | |
---|
200 | const uint8_t* tr_peerIoGetTorrentHash( tr_peerIo * io ); |
---|
201 | |
---|
202 | int tr_peerIoHasTorrentHash( const tr_peerIo * io ); |
---|
203 | |
---|
204 | void tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
205 | const uint8_t * hash ); |
---|
206 | |
---|
207 | int tr_peerIoReconnect( tr_peerIo * io ); |
---|
208 | |
---|
209 | static TR_INLINE tr_bool tr_peerIoIsIncoming( const tr_peerIo * io ) |
---|
210 | { |
---|
211 | return io->isIncoming; |
---|
212 | } |
---|
213 | |
---|
214 | static TR_INLINE int tr_peerIoGetAge( const tr_peerIo * io ) |
---|
215 | { |
---|
216 | return time( NULL ) - io->timeCreated; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | /** |
---|
221 | *** |
---|
222 | **/ |
---|
223 | |
---|
224 | void tr_peerIoSetPeersId( tr_peerIo * io, |
---|
225 | const uint8_t * peer_id ); |
---|
226 | |
---|
227 | static TR_INLINE const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io ) |
---|
228 | { |
---|
229 | assert( tr_isPeerIo( io ) ); |
---|
230 | assert( io->peerIdIsSet ); |
---|
231 | |
---|
232 | return io->peerId; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | *** |
---|
237 | **/ |
---|
238 | |
---|
239 | void tr_peerIoSetIOFuncs ( tr_peerIo * io, |
---|
240 | tr_can_read_cb readcb, |
---|
241 | tr_did_write_cb writecb, |
---|
242 | tr_net_error_cb errcb, |
---|
243 | void * user_data ); |
---|
244 | |
---|
245 | void tr_peerIoClear ( tr_peerIo * io ); |
---|
246 | |
---|
247 | /** |
---|
248 | *** |
---|
249 | **/ |
---|
250 | |
---|
251 | void tr_peerIoWrite ( tr_peerIo * io, |
---|
252 | const void * writeme, |
---|
253 | size_t writemeLen, |
---|
254 | tr_bool isPieceData ); |
---|
255 | |
---|
256 | void tr_peerIoWriteBuf ( tr_peerIo * io, |
---|
257 | struct evbuffer * buf, |
---|
258 | tr_bool isPieceData ); |
---|
259 | |
---|
260 | /** |
---|
261 | *** |
---|
262 | **/ |
---|
263 | |
---|
264 | static TR_INLINE struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io ) |
---|
265 | { |
---|
266 | return io->crypto; |
---|
267 | } |
---|
268 | |
---|
269 | typedef enum |
---|
270 | { |
---|
271 | /* these match the values in MSE's crypto_select */ |
---|
272 | PEER_ENCRYPTION_NONE = ( 1 << 0 ), |
---|
273 | PEER_ENCRYPTION_RC4 = ( 1 << 1 ) |
---|
274 | } |
---|
275 | EncryptionMode; |
---|
276 | |
---|
277 | void tr_peerIoSetEncryption( tr_peerIo * io, |
---|
278 | int encryptionMode ); |
---|
279 | |
---|
280 | static TR_INLINE tr_bool tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
281 | { |
---|
282 | return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 ); |
---|
283 | } |
---|
284 | |
---|
285 | static TR_INLINE void tr_peerIoWriteBytes( tr_peerIo * io UNUSED, |
---|
286 | struct evbuffer * outbuf, |
---|
287 | const void * bytes, |
---|
288 | size_t byteCount ) |
---|
289 | { |
---|
290 | evbuffer_add( outbuf, bytes, byteCount ); |
---|
291 | } |
---|
292 | |
---|
293 | static TR_INLINE void tr_peerIoWriteUint8( tr_peerIo * io, |
---|
294 | struct evbuffer * outbuf, |
---|
295 | uint8_t writeme ) |
---|
296 | { |
---|
297 | tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) ); |
---|
298 | } |
---|
299 | |
---|
300 | static TR_INLINE void tr_peerIoWriteUint16( tr_peerIo * io, |
---|
301 | struct evbuffer * outbuf, |
---|
302 | uint16_t writeme ) |
---|
303 | { |
---|
304 | const uint16_t tmp = htons( writeme ); |
---|
305 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) ); |
---|
306 | } |
---|
307 | |
---|
308 | static TR_INLINE void tr_peerIoWriteUint32( tr_peerIo * io, |
---|
309 | struct evbuffer * outbuf, |
---|
310 | uint32_t writeme ) |
---|
311 | { |
---|
312 | const uint32_t tmp = htonl( writeme ); |
---|
313 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) ); |
---|
314 | } |
---|
315 | |
---|
316 | void tr_peerIoReadBytes( tr_peerIo * io, |
---|
317 | struct evbuffer * inbuf, |
---|
318 | void * bytes, |
---|
319 | size_t byteCount ); |
---|
320 | |
---|
321 | static TR_INLINE void tr_peerIoReadUint8( tr_peerIo * io, |
---|
322 | struct evbuffer * inbuf, |
---|
323 | uint8_t * setme ) |
---|
324 | { |
---|
325 | tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); |
---|
326 | } |
---|
327 | |
---|
328 | static TR_INLINE void tr_peerIoReadUint16( tr_peerIo * io, |
---|
329 | struct evbuffer * inbuf, |
---|
330 | uint16_t * setme ) |
---|
331 | { |
---|
332 | uint16_t tmp; |
---|
333 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) ); |
---|
334 | *setme = ntohs( tmp ); |
---|
335 | } |
---|
336 | |
---|
337 | static TR_INLINE void tr_peerIoReadUint32( tr_peerIo * io, |
---|
338 | struct evbuffer * inbuf, |
---|
339 | uint32_t * setme ) |
---|
340 | { |
---|
341 | uint32_t tmp; |
---|
342 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) ); |
---|
343 | *setme = ntohl( tmp ); |
---|
344 | } |
---|
345 | |
---|
346 | void tr_peerIoDrain( tr_peerIo * io, |
---|
347 | struct evbuffer * inbuf, |
---|
348 | size_t byteCount ); |
---|
349 | |
---|
350 | /** |
---|
351 | *** |
---|
352 | **/ |
---|
353 | |
---|
354 | size_t tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now ); |
---|
355 | |
---|
356 | static TR_INLINE void tr_peerIoSetParent( tr_peerIo * io, |
---|
357 | struct tr_bandwidth * parent ) |
---|
358 | { |
---|
359 | assert( tr_isPeerIo( io ) ); |
---|
360 | |
---|
361 | tr_bandwidthSetParent( &io->bandwidth, parent ); |
---|
362 | } |
---|
363 | |
---|
364 | void tr_peerIoBandwidthUsed( tr_peerIo * io, |
---|
365 | tr_direction direction, |
---|
366 | size_t byteCount, |
---|
367 | int isPieceData ); |
---|
368 | |
---|
369 | static TR_INLINE tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io, |
---|
370 | tr_direction dir ) |
---|
371 | { |
---|
372 | assert( tr_isPeerIo( io ) ); |
---|
373 | |
---|
374 | return !io->hasFinishedConnecting |
---|
375 | || ( tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0 ); |
---|
376 | } |
---|
377 | |
---|
378 | static TR_INLINE double tr_peerIoGetPieceSpeed( const tr_peerIo * io, uint64_t now, tr_direction dir ) |
---|
379 | { |
---|
380 | assert( tr_isPeerIo( io ) ); |
---|
381 | assert( tr_isDirection( dir ) ); |
---|
382 | |
---|
383 | return tr_bandwidthGetPieceSpeed( &io->bandwidth, now, dir ); |
---|
384 | } |
---|
385 | |
---|
386 | /** |
---|
387 | *** |
---|
388 | **/ |
---|
389 | |
---|
390 | void tr_peerIoSetEnabled( tr_peerIo * io, |
---|
391 | tr_direction dir, |
---|
392 | tr_bool isEnabled ); |
---|
393 | |
---|
394 | int tr_peerIoFlush( tr_peerIo * io, |
---|
395 | tr_direction dir, |
---|
396 | size_t byteLimit ); |
---|
397 | |
---|
398 | int tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io ); |
---|
399 | |
---|
400 | /** |
---|
401 | *** |
---|
402 | **/ |
---|
403 | |
---|
404 | static TR_INLINE struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ) |
---|
405 | { |
---|
406 | return io->inbuf; |
---|
407 | } |
---|
408 | |
---|
409 | /* @} */ |
---|
410 | |
---|
411 | #endif |
---|