1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
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 11945 2011-02-18 00:41:06Z jordan $ |
---|
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 <event2/buffer.h> |
---|
27 | #include <event2/event.h> |
---|
28 | |
---|
29 | #include "transmission.h" |
---|
30 | #include "bandwidth.h" |
---|
31 | #include "list.h" /* tr_list */ |
---|
32 | #include "net.h" /* tr_address */ |
---|
33 | #include "utils.h" /* tr_time() */ |
---|
34 | |
---|
35 | struct evbuffer; |
---|
36 | struct tr_bandwidth; |
---|
37 | struct tr_crypto; |
---|
38 | struct tr_peerIo; |
---|
39 | |
---|
40 | /** |
---|
41 | * @addtogroup networked_io Networked IO |
---|
42 | * @{ |
---|
43 | */ |
---|
44 | |
---|
45 | typedef enum |
---|
46 | { |
---|
47 | READ_NOW, |
---|
48 | READ_LATER, |
---|
49 | READ_ERR |
---|
50 | } |
---|
51 | ReadState; |
---|
52 | |
---|
53 | typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io, |
---|
54 | void * user_data, |
---|
55 | size_t * setme_piece_byte_count ); |
---|
56 | |
---|
57 | typedef void ( *tr_did_write_cb )( struct tr_peerIo * io, |
---|
58 | size_t bytesWritten, |
---|
59 | int wasPieceData, |
---|
60 | void * userData ); |
---|
61 | |
---|
62 | typedef void ( *tr_net_error_cb )( struct tr_peerIo * io, |
---|
63 | short what, |
---|
64 | void * userData ); |
---|
65 | |
---|
66 | typedef struct tr_peerIo |
---|
67 | { |
---|
68 | tr_bool isEncrypted; |
---|
69 | tr_bool isIncoming; |
---|
70 | tr_bool peerIdIsSet; |
---|
71 | tr_bool extendedProtocolSupported; |
---|
72 | tr_bool fastExtensionSupported; |
---|
73 | tr_bool dhtSupported; |
---|
74 | tr_bool utpSupported; |
---|
75 | |
---|
76 | tr_priority_t priority; |
---|
77 | |
---|
78 | short int pendingEvents; |
---|
79 | |
---|
80 | int magicNumber; |
---|
81 | |
---|
82 | uint32_t encryptionMode; |
---|
83 | tr_bool isSeed; |
---|
84 | |
---|
85 | tr_port port; |
---|
86 | int socket; |
---|
87 | struct UTPSocket *utp_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 | tr_bool isSeed, |
---|
125 | tr_bool utp ); |
---|
126 | |
---|
127 | |
---|
128 | tr_peerIo* tr_peerIoNewIncoming( tr_session * session, |
---|
129 | struct tr_bandwidth * parent, |
---|
130 | const struct tr_address * addr, |
---|
131 | tr_port port, |
---|
132 | int socket, |
---|
133 | struct UTPSocket * utp_socket ); |
---|
134 | |
---|
135 | void tr_peerIoRefImpl ( const char * file, |
---|
136 | int line, |
---|
137 | tr_peerIo * io ); |
---|
138 | |
---|
139 | #define tr_peerIoRef(io) tr_peerIoRefImpl( __FILE__, __LINE__, (io) ); |
---|
140 | |
---|
141 | void tr_peerIoUnrefImpl ( const char * file, |
---|
142 | int line, |
---|
143 | tr_peerIo * io ); |
---|
144 | |
---|
145 | #define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) ); |
---|
146 | |
---|
147 | tr_bool tr_isPeerIo ( const tr_peerIo * io ); |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | *** |
---|
152 | **/ |
---|
153 | |
---|
154 | static inline void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag ) |
---|
155 | { |
---|
156 | io->fastExtensionSupported = flag; |
---|
157 | } |
---|
158 | static inline tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io ) |
---|
159 | { |
---|
160 | return io->fastExtensionSupported; |
---|
161 | } |
---|
162 | |
---|
163 | static inline void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag ) |
---|
164 | { |
---|
165 | io->extendedProtocolSupported = flag; |
---|
166 | } |
---|
167 | static inline tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io ) |
---|
168 | { |
---|
169 | return io->extendedProtocolSupported; |
---|
170 | } |
---|
171 | |
---|
172 | static inline void tr_peerIoEnableDHT( tr_peerIo * io, tr_bool flag ) |
---|
173 | { |
---|
174 | io->dhtSupported = flag; |
---|
175 | } |
---|
176 | static inline tr_bool tr_peerIoSupportsDHT( const tr_peerIo * io ) |
---|
177 | { |
---|
178 | return io->dhtSupported; |
---|
179 | } |
---|
180 | |
---|
181 | static inline tr_bool tr_peerIoSupportsUTP( const tr_peerIo * io ) |
---|
182 | { |
---|
183 | return io->dhtSupported; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | *** |
---|
188 | **/ |
---|
189 | |
---|
190 | static inline tr_session* tr_peerIoGetSession ( tr_peerIo * io ) |
---|
191 | { |
---|
192 | assert( tr_isPeerIo( io ) ); |
---|
193 | assert( io->session ); |
---|
194 | |
---|
195 | return io->session; |
---|
196 | } |
---|
197 | |
---|
198 | const char* tr_peerIoAddrStr( const struct tr_address * addr, |
---|
199 | tr_port port ); |
---|
200 | |
---|
201 | const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); |
---|
202 | |
---|
203 | const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io, |
---|
204 | tr_port * port ); |
---|
205 | |
---|
206 | const uint8_t* tr_peerIoGetTorrentHash( tr_peerIo * io ); |
---|
207 | |
---|
208 | int tr_peerIoHasTorrentHash( const tr_peerIo * io ); |
---|
209 | |
---|
210 | void tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
211 | const uint8_t * hash ); |
---|
212 | |
---|
213 | int tr_peerIoReconnect( tr_peerIo * io ); |
---|
214 | |
---|
215 | static inline tr_bool tr_peerIoIsIncoming( const tr_peerIo * io ) |
---|
216 | { |
---|
217 | return io->isIncoming; |
---|
218 | } |
---|
219 | |
---|
220 | static inline int tr_peerIoGetAge( const tr_peerIo * io ) |
---|
221 | { |
---|
222 | return tr_time() - io->timeCreated; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | *** |
---|
228 | **/ |
---|
229 | |
---|
230 | void tr_peerIoSetPeersId( tr_peerIo * io, |
---|
231 | const uint8_t * peer_id ); |
---|
232 | |
---|
233 | static inline const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io ) |
---|
234 | { |
---|
235 | assert( tr_isPeerIo( io ) ); |
---|
236 | assert( io->peerIdIsSet ); |
---|
237 | |
---|
238 | return io->peerId; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | *** |
---|
243 | **/ |
---|
244 | |
---|
245 | void tr_peerIoSetIOFuncs ( tr_peerIo * io, |
---|
246 | tr_can_read_cb readcb, |
---|
247 | tr_did_write_cb writecb, |
---|
248 | tr_net_error_cb errcb, |
---|
249 | void * user_data ); |
---|
250 | |
---|
251 | void tr_peerIoClear ( tr_peerIo * io ); |
---|
252 | |
---|
253 | /** |
---|
254 | *** |
---|
255 | **/ |
---|
256 | |
---|
257 | void tr_peerIoWriteBytes ( tr_peerIo * io, |
---|
258 | const void * writeme, |
---|
259 | size_t writemeLen, |
---|
260 | tr_bool isPieceData ); |
---|
261 | |
---|
262 | void tr_peerIoWriteBuf ( tr_peerIo * io, |
---|
263 | struct evbuffer * buf, |
---|
264 | tr_bool isPieceData ); |
---|
265 | |
---|
266 | /** |
---|
267 | *** |
---|
268 | **/ |
---|
269 | |
---|
270 | static inline struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io ) |
---|
271 | { |
---|
272 | return io->crypto; |
---|
273 | } |
---|
274 | |
---|
275 | typedef enum |
---|
276 | { |
---|
277 | /* these match the values in MSE's crypto_select */ |
---|
278 | PEER_ENCRYPTION_NONE = ( 1 << 0 ), |
---|
279 | PEER_ENCRYPTION_RC4 = ( 1 << 1 ) |
---|
280 | } |
---|
281 | EncryptionMode; |
---|
282 | |
---|
283 | void tr_peerIoSetEncryption( tr_peerIo * io, uint32_t encryptionMode ); |
---|
284 | |
---|
285 | static inline tr_bool |
---|
286 | tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
287 | { |
---|
288 | return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 ); |
---|
289 | } |
---|
290 | |
---|
291 | void evbuffer_add_uint8 ( struct evbuffer * outbuf, uint8_t byte ); |
---|
292 | void evbuffer_add_uint16( struct evbuffer * outbuf, uint16_t hs ); |
---|
293 | void evbuffer_add_uint32( struct evbuffer * outbuf, uint32_t hl ); |
---|
294 | |
---|
295 | void tr_peerIoReadBytes( tr_peerIo * io, |
---|
296 | struct evbuffer * inbuf, |
---|
297 | void * bytes, |
---|
298 | size_t byteCount ); |
---|
299 | |
---|
300 | static inline void tr_peerIoReadUint8( tr_peerIo * io, |
---|
301 | struct evbuffer * inbuf, |
---|
302 | uint8_t * setme ) |
---|
303 | { |
---|
304 | tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); |
---|
305 | } |
---|
306 | |
---|
307 | void tr_peerIoReadUint16( tr_peerIo * io, |
---|
308 | struct evbuffer * inbuf, |
---|
309 | uint16_t * setme ); |
---|
310 | |
---|
311 | void tr_peerIoReadUint32( tr_peerIo * io, |
---|
312 | struct evbuffer * inbuf, |
---|
313 | uint32_t * setme ); |
---|
314 | |
---|
315 | void tr_peerIoDrain( tr_peerIo * io, |
---|
316 | struct evbuffer * inbuf, |
---|
317 | size_t byteCount ); |
---|
318 | |
---|
319 | /** |
---|
320 | *** |
---|
321 | **/ |
---|
322 | |
---|
323 | size_t tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now ); |
---|
324 | |
---|
325 | static inline void tr_peerIoSetParent( tr_peerIo * io, |
---|
326 | struct tr_bandwidth * parent ) |
---|
327 | { |
---|
328 | assert( tr_isPeerIo( io ) ); |
---|
329 | |
---|
330 | tr_bandwidthSetParent( &io->bandwidth, parent ); |
---|
331 | } |
---|
332 | |
---|
333 | void tr_peerIoBandwidthUsed( tr_peerIo * io, |
---|
334 | tr_direction direction, |
---|
335 | size_t byteCount, |
---|
336 | int isPieceData ); |
---|
337 | |
---|
338 | static inline tr_bool |
---|
339 | tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir ) |
---|
340 | { |
---|
341 | assert( tr_isPeerIo( io ) ); |
---|
342 | |
---|
343 | return tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0; |
---|
344 | } |
---|
345 | |
---|
346 | static inline unsigned int |
---|
347 | tr_peerIoGetPieceSpeed_Bps( 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_Bps( &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 | int tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io ); |
---|
368 | |
---|
369 | /** |
---|
370 | *** |
---|
371 | **/ |
---|
372 | |
---|
373 | static inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ) |
---|
374 | { |
---|
375 | return io->inbuf; |
---|
376 | } |
---|
377 | |
---|
378 | /* @} */ |
---|
379 | |
---|
380 | #endif |
---|