1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 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 7579 2009-01-02 17:46:22Z 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 "list.h" /* __tr_list */ |
---|
30 | #include "net.h" /* tr_address */ |
---|
31 | |
---|
32 | struct evbuffer; |
---|
33 | struct tr_bandwidth; |
---|
34 | struct tr_crypto; |
---|
35 | struct tr_peerIo; |
---|
36 | |
---|
37 | typedef enum |
---|
38 | { |
---|
39 | READ_NOW, |
---|
40 | READ_LATER, |
---|
41 | READ_ERR |
---|
42 | } |
---|
43 | ReadState; |
---|
44 | |
---|
45 | typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io, |
---|
46 | void * user_data, |
---|
47 | size_t * setme_piece_byte_count ); |
---|
48 | |
---|
49 | typedef void ( *tr_did_write_cb )( struct tr_peerIo * io, |
---|
50 | size_t bytesWritten, |
---|
51 | int wasPieceData, |
---|
52 | void * userData ); |
---|
53 | |
---|
54 | typedef void ( *tr_net_error_cb )( struct tr_peerIo * io, |
---|
55 | short what, |
---|
56 | void * userData ); |
---|
57 | |
---|
58 | typedef struct tr_peerIo |
---|
59 | { |
---|
60 | tr_bool isEncrypted; |
---|
61 | tr_bool isIncoming; |
---|
62 | tr_bool peerIdIsSet; |
---|
63 | tr_bool extendedProtocolSupported; |
---|
64 | tr_bool fastExtensionSupported; |
---|
65 | |
---|
66 | int magicNumber; |
---|
67 | |
---|
68 | uint8_t encryptionMode; |
---|
69 | tr_port port; |
---|
70 | int socket; |
---|
71 | |
---|
72 | uint8_t peerId[SHA_DIGEST_LENGTH]; |
---|
73 | time_t timeCreated; |
---|
74 | |
---|
75 | tr_session * session; |
---|
76 | |
---|
77 | tr_address addr; |
---|
78 | |
---|
79 | tr_can_read_cb canRead; |
---|
80 | tr_did_write_cb didWrite; |
---|
81 | tr_net_error_cb gotError; |
---|
82 | void * userData; |
---|
83 | |
---|
84 | struct tr_bandwidth * bandwidth; |
---|
85 | struct tr_crypto * crypto; |
---|
86 | |
---|
87 | struct evbuffer * inbuf; |
---|
88 | struct evbuffer * outbuf; |
---|
89 | struct __tr_list outbuf_datatypes; /* struct tr_datatype */ |
---|
90 | |
---|
91 | struct event event_read; |
---|
92 | struct event event_write; |
---|
93 | } |
---|
94 | tr_peerIo; |
---|
95 | |
---|
96 | /** |
---|
97 | *** |
---|
98 | **/ |
---|
99 | |
---|
100 | tr_peerIo* tr_peerIoNewOutgoing( tr_session * session, |
---|
101 | const struct tr_address * addr, |
---|
102 | tr_port port, |
---|
103 | const uint8_t * torrentHash ); |
---|
104 | |
---|
105 | tr_peerIo* tr_peerIoNewIncoming( tr_session * session, |
---|
106 | const struct tr_address * addr, |
---|
107 | tr_port port, |
---|
108 | int socket ); |
---|
109 | |
---|
110 | void tr_peerIoFree ( tr_peerIo * io ); |
---|
111 | |
---|
112 | tr_bool tr_isPeerIo ( const tr_peerIo * io ); |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | *** |
---|
117 | **/ |
---|
118 | |
---|
119 | void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag ); |
---|
120 | |
---|
121 | tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io ); |
---|
122 | |
---|
123 | void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag ); |
---|
124 | |
---|
125 | tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io ); |
---|
126 | |
---|
127 | /** |
---|
128 | *** |
---|
129 | **/ |
---|
130 | |
---|
131 | tr_session* tr_peerIoGetSession ( tr_peerIo * io ); |
---|
132 | |
---|
133 | const char* tr_peerIoAddrStr( const struct tr_address * addr, |
---|
134 | tr_port port ); |
---|
135 | |
---|
136 | const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); |
---|
137 | |
---|
138 | const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io, |
---|
139 | tr_port * port ); |
---|
140 | |
---|
141 | const uint8_t* tr_peerIoGetTorrentHash( tr_peerIo * io ); |
---|
142 | |
---|
143 | int tr_peerIoHasTorrentHash( const tr_peerIo * io ); |
---|
144 | |
---|
145 | void tr_peerIoSetTorrentHash( tr_peerIo * io, |
---|
146 | const uint8_t * hash ); |
---|
147 | |
---|
148 | int tr_peerIoReconnect( tr_peerIo * io ); |
---|
149 | |
---|
150 | tr_bool tr_peerIoIsIncoming( const tr_peerIo * io ); |
---|
151 | |
---|
152 | static inline int tr_peerIoGetAge( const tr_peerIo * io ) |
---|
153 | { |
---|
154 | return time( NULL ) - io->timeCreated; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | *** |
---|
160 | **/ |
---|
161 | |
---|
162 | void tr_peerIoSetPeersId( tr_peerIo * io, |
---|
163 | const uint8_t * peer_id ); |
---|
164 | |
---|
165 | const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io ); |
---|
166 | |
---|
167 | /** |
---|
168 | *** |
---|
169 | **/ |
---|
170 | |
---|
171 | void tr_peerIoSetIOFuncs ( tr_peerIo * io, |
---|
172 | tr_can_read_cb readcb, |
---|
173 | tr_did_write_cb writecb, |
---|
174 | tr_net_error_cb errcb, |
---|
175 | void * user_data ); |
---|
176 | |
---|
177 | /** |
---|
178 | *** |
---|
179 | **/ |
---|
180 | |
---|
181 | void tr_peerIoWrite ( tr_peerIo * io, |
---|
182 | const void * writeme, |
---|
183 | size_t writemeLen, |
---|
184 | int isPieceData ); |
---|
185 | |
---|
186 | void tr_peerIoWriteBuf ( tr_peerIo * io, |
---|
187 | struct evbuffer * buf, |
---|
188 | int isPieceData ); |
---|
189 | |
---|
190 | /** |
---|
191 | *** |
---|
192 | **/ |
---|
193 | |
---|
194 | struct tr_crypto* tr_peerIoGetCrypto( tr_peerIo * io ); |
---|
195 | |
---|
196 | typedef enum |
---|
197 | { |
---|
198 | /* these match the values in MSE's crypto_select */ |
---|
199 | PEER_ENCRYPTION_NONE = ( 1 << 0 ), |
---|
200 | PEER_ENCRYPTION_RC4 = ( 1 << 1 ) |
---|
201 | } |
---|
202 | EncryptionMode; |
---|
203 | |
---|
204 | void tr_peerIoSetEncryption( tr_peerIo * io, |
---|
205 | int encryptionMode ); |
---|
206 | |
---|
207 | int tr_peerIoIsEncrypted( const tr_peerIo * io ); |
---|
208 | |
---|
209 | void tr_peerIoWriteBytes( tr_peerIo * io, |
---|
210 | struct evbuffer * outbuf, |
---|
211 | const void * bytes, |
---|
212 | size_t byteCount ); |
---|
213 | |
---|
214 | static inline void tr_peerIoWriteUint8( tr_peerIo * io, |
---|
215 | struct evbuffer * outbuf, |
---|
216 | uint8_t writeme ) |
---|
217 | { |
---|
218 | tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) ); |
---|
219 | } |
---|
220 | |
---|
221 | static inline void tr_peerIoWriteUint16( tr_peerIo * io, |
---|
222 | struct evbuffer * outbuf, |
---|
223 | uint16_t writeme ) |
---|
224 | { |
---|
225 | const uint16_t tmp = htons( writeme ); |
---|
226 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) ); |
---|
227 | } |
---|
228 | |
---|
229 | static inline void tr_peerIoWriteUint32( tr_peerIo * io, |
---|
230 | struct evbuffer * outbuf, |
---|
231 | uint32_t writeme ) |
---|
232 | { |
---|
233 | const uint32_t tmp = htonl( writeme ); |
---|
234 | tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) ); |
---|
235 | } |
---|
236 | |
---|
237 | void tr_peerIoReadBytes( tr_peerIo * io, |
---|
238 | struct evbuffer * inbuf, |
---|
239 | void * bytes, |
---|
240 | size_t byteCount ); |
---|
241 | |
---|
242 | static inline void tr_peerIoReadUint8( tr_peerIo * io, |
---|
243 | struct evbuffer * inbuf, |
---|
244 | uint8_t * setme ) |
---|
245 | { |
---|
246 | tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); |
---|
247 | } |
---|
248 | |
---|
249 | static inline void tr_peerIoReadUint16( tr_peerIo * io, |
---|
250 | struct evbuffer * inbuf, |
---|
251 | uint16_t * setme ) |
---|
252 | { |
---|
253 | uint16_t tmp; |
---|
254 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) ); |
---|
255 | *setme = ntohs( tmp ); |
---|
256 | } |
---|
257 | |
---|
258 | static inline void tr_peerIoReadUint32( tr_peerIo * io, |
---|
259 | struct evbuffer * inbuf, |
---|
260 | uint32_t * setme ) |
---|
261 | { |
---|
262 | uint32_t tmp; |
---|
263 | tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) ); |
---|
264 | *setme = ntohl( tmp ); |
---|
265 | } |
---|
266 | |
---|
267 | void tr_peerIoDrain( tr_peerIo * io, |
---|
268 | struct evbuffer * inbuf, |
---|
269 | size_t byteCount ); |
---|
270 | |
---|
271 | /** |
---|
272 | *** |
---|
273 | **/ |
---|
274 | |
---|
275 | size_t tr_peerIoGetWriteBufferSpace( const tr_peerIo * io ); |
---|
276 | |
---|
277 | void tr_peerIoSetBandwidth( tr_peerIo * io, |
---|
278 | struct tr_bandwidth * bandwidth ); |
---|
279 | |
---|
280 | void tr_peerIoBandwidthUsed( tr_peerIo * io, |
---|
281 | tr_direction direction, |
---|
282 | size_t byteCount, |
---|
283 | int isPieceData ); |
---|
284 | |
---|
285 | tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io, |
---|
286 | tr_direction dir ); |
---|
287 | |
---|
288 | /** |
---|
289 | *** |
---|
290 | **/ |
---|
291 | |
---|
292 | void tr_peerIoSetEnabled( tr_peerIo * io, |
---|
293 | tr_direction dir, |
---|
294 | tr_bool isEnabled ); |
---|
295 | |
---|
296 | int tr_peerIoFlush( tr_peerIo * io, |
---|
297 | tr_direction dir, |
---|
298 | size_t byteLimit ); |
---|
299 | |
---|
300 | /** |
---|
301 | *** |
---|
302 | **/ |
---|
303 | |
---|
304 | static inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ) |
---|
305 | { |
---|
306 | assert( tr_isPeerIo( io ) ); |
---|
307 | |
---|
308 | return io->inbuf; |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | #endif |
---|