28
#include "transmission.h"
29
#include "bandwidth.h"
30
#include "list.h" /* __tr_list */
31
#include "net.h" /* tr_address */
25
typedef struct tr_peerIo tr_peerIo;
26
typedef struct tr_extensions tr_extensions;
33
tr_peerIoNewOutgoing( struct tr_handle * handle,
34
const struct in_addr * addr,
36
const uint8_t * torrentHash );
39
tr_peerIoNewIncoming( struct tr_handle * handle,
40
const struct in_addr * addr,
44
void tr_peerIoFree ( tr_peerIo * io );
46
tr_handle* tr_peerIoGetHandle( tr_peerIo * io );
52
void tr_peerIoEnableLTEP( tr_peerIo * io, int flag );
53
void tr_peerIoEnableFEXT( tr_peerIo * io, int flag );
55
int tr_peerIoSupportsLTEP( const tr_peerIo * io );
56
int tr_peerIoSupportsFEXT( const tr_peerIo * io );
63
tr_peerIoAddrStr( const struct in_addr * addr, uint16_t port );
66
tr_peerIoGetAddrStr( const tr_peerIo * io );
69
tr_peerIoGetAddress( const tr_peerIo * io, uint16_t * port );
72
tr_peerIoGetTorrentHash( tr_peerIo * io );
74
int tr_peerIoHasTorrentHash( const tr_peerIo * io );
76
void tr_peerIoSetTorrentHash( tr_peerIo * io,
77
const uint8_t * hash );
79
int tr_peerIoReconnect( tr_peerIo * io );
81
int tr_peerIoIsIncoming( const tr_peerIo * io );
83
void tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs );
85
int tr_peerIoGetAge( const tr_peerIo * io );
92
void tr_peerIoSetPeersId( tr_peerIo * io,
93
const uint8_t * peer_id );
96
tr_peerIoGetPeersId( const tr_peerIo * io );
102
typedef enum { READ_MORE, READ_AGAIN, READ_DONE } ReadState;
103
typedef ReadState (*tr_can_read_cb)(struct bufferevent*, void* user_data);
104
typedef void (*tr_did_write_cb)(struct bufferevent *, void *);
105
typedef void (*tr_net_error_cb)(struct bufferevent *, short what, void *);
107
void tr_peerIoSetIOFuncs( tr_peerIo * io,
108
tr_can_read_cb readcb,
109
tr_did_write_cb writecb,
110
tr_net_error_cb errcb,
113
size_t tr_peerIoWriteBytesWaiting( const tr_peerIo * io );
115
void tr_peerIoTryRead( tr_peerIo * io );
117
void tr_peerIoWrite( tr_peerIo * io,
118
const void * writeme,
121
void tr_peerIoWriteBuf( tr_peerIo * io,
122
struct evbuffer * buf );
129
struct tr_crypto* tr_peerIoGetCrypto( tr_peerIo * io );
46
typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io,
48
size_t * setme_piece_byte_count );
50
typedef void ( *tr_did_write_cb )( struct tr_peerIo * io,
55
typedef void ( *tr_net_error_cb )( struct tr_peerIo * io,
59
typedef struct tr_peerIo
64
tr_bool extendedProtocolSupported;
65
tr_bool fastExtensionSupported;
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;
72
tr_priority_t priority;
78
uint8_t encryptionMode;
85
uint8_t peerId[SHA_DIGEST_LENGTH];
92
tr_can_read_cb canRead;
93
tr_did_write_cb didWrite;
94
tr_net_error_cb gotError;
97
struct tr_bandwidth bandwidth;
98
struct tr_crypto * crypto;
100
struct evbuffer * inbuf;
101
struct evbuffer * outbuf;
102
struct __tr_list outbuf_datatypes; /* struct tr_datatype */
104
struct event event_read;
105
struct event event_write;
113
tr_peerIo* tr_peerIoNewOutgoing( tr_session * session,
114
struct tr_bandwidth * parent,
115
const struct tr_address * addr,
117
const uint8_t * torrentHash );
119
tr_peerIo* tr_peerIoNewIncoming( tr_session * session,
120
struct tr_bandwidth * parent,
121
const struct tr_address * addr,
125
void tr_peerIoRefImpl ( const char * file,
129
#define tr_peerIoRef(io) tr_peerIoRefImpl( __FILE__, __LINE__, (io) );
131
void tr_peerIoUnrefImpl ( const char * file,
135
#define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) );
137
tr_bool tr_isPeerIo ( const tr_peerIo * io );
144
void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag );
146
static TR_INLINE tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io )
148
assert( tr_isPeerIo( io ) );
150
return io->extendedProtocolSupported;
153
void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag );
155
static TR_INLINE tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io )
157
assert( tr_isPeerIo( io ) );
159
return io->fastExtensionSupported;
166
static TR_INLINE tr_session* tr_peerIoGetSession ( tr_peerIo * io )
168
assert( tr_isPeerIo( io ) );
169
assert( io->session );
174
const char* tr_peerIoAddrStr( const struct tr_address * addr,
177
static TR_INLINE const char* tr_peerIoGetAddrStr( const tr_peerIo * io )
179
return tr_isPeerIo( io ) ? tr_peerIoAddrStr( &io->addr, io->port ) : "error";
182
const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io,
185
const uint8_t* tr_peerIoGetTorrentHash( tr_peerIo * io );
187
int tr_peerIoHasTorrentHash( const tr_peerIo * io );
189
void tr_peerIoSetTorrentHash( tr_peerIo * io,
190
const uint8_t * hash );
192
int tr_peerIoReconnect( tr_peerIo * io );
194
static TR_INLINE tr_bool tr_peerIoIsIncoming( const tr_peerIo * io )
196
return io->isIncoming;
199
static TR_INLINE int tr_peerIoGetAge( const tr_peerIo * io )
201
return time( NULL ) - io->timeCreated;
209
void tr_peerIoSetPeersId( tr_peerIo * io,
210
const uint8_t * peer_id );
212
static TR_INLINE const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io )
214
assert( tr_isPeerIo( io ) );
215
assert( io->peerIdIsSet );
224
void tr_peerIoSetIOFuncs ( tr_peerIo * io,
225
tr_can_read_cb readcb,
226
tr_did_write_cb writecb,
227
tr_net_error_cb errcb,
230
void tr_peerIoClear ( tr_peerIo * io );
236
void tr_peerIoWrite ( tr_peerIo * io,
237
const void * writeme,
239
tr_bool isPieceData );
241
void tr_peerIoWriteBuf ( tr_peerIo * io,
242
struct evbuffer * buf,
243
tr_bool isPieceData );
249
static TR_INLINE struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
133
256
/* these match the values in MSE's crypto_select */
134
PEER_ENCRYPTION_NONE = (1<<0),
135
PEER_ENCRYPTION_RC4 = (1<<1)
257
PEER_ENCRYPTION_NONE = ( 1 << 0 ),
258
PEER_ENCRYPTION_RC4 = ( 1 << 1 )
139
void tr_peerIoSetEncryption( tr_peerIo * io,
140
int encryptionMode );
142
int tr_peerIoIsEncrypted( const tr_peerIo * io );
144
void tr_peerIoWriteBytes ( tr_peerIo * io,
145
struct evbuffer * outbuf,
149
void tr_peerIoWriteUint8 ( tr_peerIo * io,
150
struct evbuffer * outbuf,
153
void tr_peerIoWriteUint16 ( tr_peerIo * io,
154
struct evbuffer * outbuf,
157
void tr_peerIoWriteUint32 ( tr_peerIo * io,
158
struct evbuffer * outbuf,
161
void tr_peerIoReadBytes ( tr_peerIo * io,
162
struct evbuffer * inbuf,
166
void tr_peerIoReadUint8 ( tr_peerIo * io,
167
struct evbuffer * inbuf,
170
void tr_peerIoReadUint16 ( tr_peerIo * io,
171
struct evbuffer * inbuf,
174
void tr_peerIoReadUint32 ( tr_peerIo * io,
175
struct evbuffer * inbuf,
178
void tr_peerIoDrain ( tr_peerIo * io,
179
struct evbuffer * inbuf,
262
void tr_peerIoSetEncryption( tr_peerIo * io,
263
int encryptionMode );
265
static TR_INLINE tr_bool tr_peerIoIsEncrypted( const tr_peerIo * io )
267
return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 );
270
static TR_INLINE void tr_peerIoWriteBytes( tr_peerIo * io UNUSED,
271
struct evbuffer * outbuf,
275
evbuffer_add( outbuf, bytes, byteCount );
278
static TR_INLINE void tr_peerIoWriteUint8( tr_peerIo * io,
279
struct evbuffer * outbuf,
282
tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
285
static TR_INLINE void tr_peerIoWriteUint16( tr_peerIo * io,
286
struct evbuffer * outbuf,
289
const uint16_t tmp = htons( writeme );
290
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
293
static TR_INLINE void tr_peerIoWriteUint32( tr_peerIo * io,
294
struct evbuffer * outbuf,
297
const uint32_t tmp = htonl( writeme );
298
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
301
void tr_peerIoReadBytes( tr_peerIo * io,
302
struct evbuffer * inbuf,
306
static TR_INLINE void tr_peerIoReadUint8( tr_peerIo * io,
307
struct evbuffer * inbuf,
310
tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) );
313
static TR_INLINE void tr_peerIoReadUint16( tr_peerIo * io,
314
struct evbuffer * inbuf,
318
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
319
*setme = ntohs( tmp );
322
static TR_INLINE void tr_peerIoReadUint32( tr_peerIo * io,
323
struct evbuffer * inbuf,
327
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
328
*setme = ntohl( tmp );
331
void tr_peerIoDrain( tr_peerIo * io,
332
struct evbuffer * inbuf,
339
size_t tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now );
341
static TR_INLINE void tr_peerIoSetParent( tr_peerIo * io,
342
struct tr_bandwidth * parent )
344
assert( tr_isPeerIo( io ) );
346
tr_bandwidthSetParent( &io->bandwidth, parent );
349
void tr_peerIoBandwidthUsed( tr_peerIo * io,
350
tr_direction direction,
354
static TR_INLINE tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io,
357
assert( tr_isPeerIo( io ) );
359
return !io->hasFinishedConnecting
360
|| ( tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0 );
363
static TR_INLINE double tr_peerIoGetPieceSpeed( const tr_peerIo * io, uint64_t now, tr_direction dir )
365
assert( tr_isPeerIo( io ) );
366
assert( tr_isDirection( dir ) );
368
return tr_bandwidthGetPieceSpeed( &io->bandwidth, now, dir );
375
void tr_peerIoSetEnabled( tr_peerIo * io,
379
int tr_peerIoFlush( tr_peerIo * io,
383
int tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io );
389
static TR_INLINE struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io )