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 11935 2011-02-18 00:36:24Z jch $ |
---|
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 | |
---|
75 | tr_priority_t priority; |
---|
76 | |
---|
77 | short int pendingEvents; |
---|
78 | |
---|
79 | int magicNumber; |
---|
80 | |
---|
81 | uint32_t encryptionMode; |
---|
82 | tr_bool isSeed; |
---|
83 | |
---|
84 | tr_port port; |
---|
85 | int socket; |
---|
86 | struct UTPSocket *utp_socket; |
---|
87 | |
---|
88 | int refCount; |
---|
89 | |
---|
90 | uint8_t peerId[SHA_DIGEST_LENGTH]; |
---|
91 | time_t timeCreated; |
---|
92 | |
---|
93 | tr_session * session; |
---|
94 | |
---|
95 | tr_address addr; |
---|
96 | |
---|
97 | tr_can_read_cb canRead; |
---|
98 | tr_did_write_cb didWrite; |
---|
99 | tr_net_error_cb gotError; |
---|
100 | void * userData; |
---|
101 | |
---|
102 | struct tr_bandwidth bandwidth; |
---|
103 | struct tr_crypto * crypto; |
---|
104 | |
---|
105 | struct evbuffer * inbuf; |
---|
106 | struct evbuffer * outbuf; |
---|
107 | struct tr_list * outbuf_datatypes; /* struct tr_datatype */ |
---|
108 | |
---|
109 | struct event * event_read; |
---|
110 | struct event * event_write; |
---|
111 | } |
---|
112 | tr_peerIo; |
---|
113 | |
---|
114 | /** |
---|
115 | *** |
---|
116 | **/ |
---|
117 | |
---|
118 | tr_peerIo* tr_peerIoNewOutgoing( tr_session * session, |
---|
119 | struct tr_bandwidth * parent, |
---|
120 | const struct tr_address * addr, |
---|
121 | tr_port port, |
---|
122 | const uint8_t * torrentHash, |
---|
123 | tr_bool isSeed, |
---|
124 | tr_bool utp ); |
---|
125 | |
---|
126 | |
---|
127 | tr_peerIo* tr_peerIoNewIncoming( tr_session * session, |
---|
128 | struct tr_bandwidth * parent, |
---|
129 | const struct tr_address * addr, |
---|
130 | tr_port port, |
---|
131 | int socket, |
---|
132 | struct UTPSocket * utp_socket ); |
---|
133 | |
---|
134 | void tr_peerIoRefImpl ( const char * file, |
---|
135 | int line, |
---|
136 | tr_peerIo * io ); |
---|
137 | |
---|
138 | #define tr_peerIoRef(io) tr_peerIoRefImpl( __FILE__, __LINE__, (io) ); |
---|
139 | |
---|
140 | void tr_peerIoUnrefImpl ( const char * file, |
---|
141 | int line, |
---|
142 | tr_peerIo * io ); |
---|
143 | |
---|
144 | #define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) ); |
---|
145 | |
---|
146 | tr_bool tr_isPeerIo ( const tr_peerIo * io ); |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | *** |
---|
151 | **/ |
---|
152 | |
---|
153 | static inline void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag ) |
---|
154 | { |
---|
155 | io->fastExtensionSupported = flag; |
---|
156 | } |
---|
157 | static inline tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io ) |
---|
158 | { |
---|
159 | return io->fastExtensionSupported; |
---|
160 | } |
---|
161 | |
---|
162 | static inline void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag ) |
---|
163 | { |
---|
164 | io->extendedProtocolSupported = flag; |
---|
165 | } |
---|
166 | static inline tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io ) |
---|
167 | { |
---|
168 | return io->extendedProtocolSupported; |
---|
169 | } |
---|
170 | |
---|
171 | static inline void tr_peerIoEnableDHT( tr_peerIo * io, tr_bool flag ) |
---|
172 | { |
---|
173 | io->dhtSupported = flag; |
---|
174 | } |
---|
175 | static inline tr_bool tr_peerIoSupportsDHT( const tr_peerIo * io ) |
---|
176 | { |
---|
177 | return io->dhtSupported; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | *** |
---|
182 | **/ |
---|
183 | |
---|
184 | static inline tr_session* tr_peerIoGetSession ( tr_peerIo * io ) |
---|
185 | { |
---|
186 | assert( tr_isPeerIo( io ) ); |
---|
187 | assert( io->session ); |
---|
188 | |
---|
189 | return io->session; |
---|
190 | } |
---|
191 | |
---|
192 | const char* tr_peerIoAddrStr( const struct tr_address * addr, |
---|
193 | tr_port port ); |
---|
194 | |
---|
195 | const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); |
---|
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 inline tr_bool tr_peerIoIsIncoming( const tr_peerIo * io ) |
---|
210 | { |
---|
211 | return io->isIncoming; |
---|
212 | } |
---|
213 | |
---|
214 | static inline int tr_peerIoGetAge( const tr_peerIo * io ) |
---|
215 | { |
---|
216 | return tr_time() - 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 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_peerIoWriteBytes ( 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 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, uint32_t encryptionMode ); |
---|
278 | |
---|
279 | static inline tr_bool |
---|
280 | tr_peerIoIsEncrypted( const tr_peerIo * io ) |
---|
281 | { |
---|
282 | return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 ); |
---|
283 | } |
---|
284 | |
---|
285 | void evbuffer_add_uint8 ( struct evbuffer * outbuf, uint8_t byte ); |
---|
286 | void evbuffer_add_uint16( struct evbuffer * outbuf, uint16_t hs ); |
---|
287 | void evbuffer_add_uint32( struct evbuffer * outbuf, uint32_t hl ); |
---|
288 | |
---|
289 | void tr_peerIoReadBytes( tr_peerIo * io, |
---|
290 | struct evbuffer * inbuf, |
---|
291 | void * bytes, |
---|
292 | size_t byteCount ); |
---|
293 | |
---|
294 | static inline void tr_peerIoReadUint8( tr_peerIo * io, |
---|
295 | struct evbuffer * inbuf, |
---|
296 | uint8_t * setme ) |
---|
297 | { |
---|
298 | tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); |
---|
299 | } |
---|
300 | |
---|
301 | void tr_peerIoReadUint16( tr_peerIo * io, |
---|
302 | struct evbuffer * inbuf, |
---|
303 | uint16_t * setme ); |
---|
304 | |
---|
305 | void tr_peerIoReadUint32( tr_peerIo * io, |
---|
306 | struct evbuffer * inbuf, |
---|
307 | uint32_t * setme ); |
---|
308 | |
---|
309 | void tr_peerIoDrain( tr_peerIo * io, |
---|
310 | struct evbuffer * inbuf, |
---|
311 | size_t byteCount ); |
---|
312 | |
---|
313 | /** |
---|
314 | *** |
---|
315 | **/ |
---|
316 | |
---|
317 | size_t tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now ); |
---|
318 | |
---|
319 | static inline void tr_peerIoSetParent( tr_peerIo * io, |
---|
320 | struct tr_bandwidth * parent ) |
---|
321 | { |
---|
322 | assert( tr_isPeerIo( io ) ); |
---|
323 | |
---|
324 | tr_bandwidthSetParent( &io->bandwidth, parent ); |
---|
325 | } |
---|
326 | |
---|
327 | void tr_peerIoBandwidthUsed( tr_peerIo * io, |
---|
328 | tr_direction direction, |
---|
329 | size_t byteCount, |
---|
330 | int isPieceData ); |
---|
331 | |
---|
332 | static inline tr_bool |
---|
333 | tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir ) |
---|
334 | { |
---|
335 | assert( tr_isPeerIo( io ) ); |
---|
336 | |
---|
337 | return tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0; |
---|
338 | } |
---|
339 | |
---|
340 | static inline unsigned int |
---|
341 | tr_peerIoGetPieceSpeed_Bps( const tr_peerIo * io, uint64_t now, tr_direction dir ) |
---|
342 | { |
---|
343 | assert( tr_isPeerIo( io ) ); |
---|
344 | assert( tr_isDirection( dir ) ); |
---|
345 | |
---|
346 | return tr_bandwidthGetPieceSpeed_Bps( &io->bandwidth, now, dir ); |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | *** |
---|
351 | **/ |
---|
352 | |
---|
353 | void tr_peerIoSetEnabled( tr_peerIo * io, |
---|
354 | tr_direction dir, |
---|
355 | tr_bool isEnabled ); |
---|
356 | |
---|
357 | int tr_peerIoFlush( tr_peerIo * io, |
---|
358 | tr_direction dir, |
---|
359 | size_t byteLimit ); |
---|
360 | |
---|
361 | int tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io ); |
---|
362 | |
---|
363 | /** |
---|
364 | *** |
---|
365 | **/ |
---|
366 | |
---|
367 | static inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ) |
---|
368 | { |
---|
369 | return io->inbuf; |
---|
370 | } |
---|
371 | |
---|
372 | /* @} */ |
---|
373 | |
---|
374 | #endif |
---|