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