~ubuntu-branches/ubuntu/precise/transmission/precise

« back to all changes in this revision

Viewing changes to libtransmission/peer-io.h

  • Committer: Bazaar Package Importer
  • Author(s): Leo Costela
  • Date: 2009-05-17 19:39:51 UTC
  • mto: (1.3.4 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: james.westby@ubuntu.com-20090517193951-k8x15sqoxzf7cuyx
ImportĀ upstreamĀ versionĀ 1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
 
2
 * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
3
3
 *
4
4
 * This file is licensed by the GPL version 2.  Works owned by the
5
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. 
 
6
 * so that the bulk of its code can remain under the MIT license.
7
7
 * This exemption does not extend to derived works not owned by
8
8
 * the Transmission project.
9
9
 *
10
 
 * $Id: peer-io.h 4608 2008-01-11 02:09:20Z charles $
 
10
 * $Id: peer-io.h 8265 2009-04-21 16:18:51Z charles $
11
11
 */
12
12
 
 
13
#ifndef __TRANSMISSION__
 
14
#error only libtransmission should #include this header.
 
15
#endif
 
16
 
13
17
#ifndef TR_PEER_IO_H
14
18
#define TR_PEER_IO_H
15
19
 
17
21
***
18
22
**/
19
23
 
20
 
struct in_addr;
 
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
 
21
33
struct evbuffer;
22
 
struct bufferevent;
23
 
struct tr_handle;
 
34
struct tr_bandwidth;
24
35
struct tr_crypto;
25
 
typedef struct tr_peerIo tr_peerIo;
26
 
typedef struct tr_extensions tr_extensions;
27
 
 
28
 
/**
29
 
***
30
 
**/
31
 
 
32
 
tr_peerIo*
33
 
      tr_peerIoNewOutgoing( struct tr_handle     * handle,
34
 
                            const struct in_addr * addr,
35
 
                            int                    port,
36
 
                            const  uint8_t       * torrentHash );
37
 
 
38
 
tr_peerIo*
39
 
      tr_peerIoNewIncoming( struct tr_handle     * handle,
40
 
                            const struct in_addr * addr,
41
 
                            uint16_t               port,
42
 
                            int                    socket );
43
 
 
44
 
void  tr_peerIoFree      ( tr_peerIo  * io );
45
 
 
46
 
tr_handle* tr_peerIoGetHandle( tr_peerIo * io );
47
 
 
48
 
/**
49
 
***
50
 
**/
51
 
 
52
 
void  tr_peerIoEnableLTEP( tr_peerIo * io, int flag );
53
 
void  tr_peerIoEnableFEXT( tr_peerIo * io, int flag );
54
 
 
55
 
int   tr_peerIoSupportsLTEP( const tr_peerIo * io );
56
 
int   tr_peerIoSupportsFEXT( const tr_peerIo * io );
57
 
 
58
 
/**
59
 
***
60
 
**/
61
 
 
62
 
const char*
63
 
       tr_peerIoAddrStr( const struct in_addr * addr, uint16_t port );
64
 
 
65
 
const char*
66
 
      tr_peerIoGetAddrStr( const tr_peerIo * io );
67
 
 
68
 
const struct in_addr*
69
 
      tr_peerIoGetAddress( const tr_peerIo * io, uint16_t * port );
70
 
 
71
 
const uint8_t*
72
 
      tr_peerIoGetTorrentHash( tr_peerIo * io );
73
 
 
74
 
int   tr_peerIoHasTorrentHash( const tr_peerIo * io );
75
 
 
76
 
void  tr_peerIoSetTorrentHash( tr_peerIo      * io,
77
 
                               const uint8_t  * hash );
78
 
 
79
 
int   tr_peerIoReconnect( tr_peerIo * io );
80
 
 
81
 
int   tr_peerIoIsIncoming( const tr_peerIo * io );
82
 
 
83
 
void  tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs );
84
 
 
85
 
int   tr_peerIoGetAge( const tr_peerIo * io );
86
 
 
87
 
 
88
 
/**
89
 
***
90
 
**/
91
 
 
92
 
void  tr_peerIoSetPeersId( tr_peerIo      * io,
93
 
                           const uint8_t  * peer_id );
94
 
 
95
 
const uint8_t*
96
 
      tr_peerIoGetPeersId( const tr_peerIo * io );
97
 
 
98
 
/**
99
 
***
100
 
**/
101
 
 
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 *);
106
 
 
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,
111
 
                           void             * user_data );
112
 
 
113
 
size_t tr_peerIoWriteBytesWaiting( const tr_peerIo * io );
114
 
 
115
 
void tr_peerIoTryRead( tr_peerIo * io );
116
 
 
117
 
void tr_peerIoWrite( tr_peerIo   * io,
118
 
                     const void  * writeme,
119
 
                     int           writeme_len );
120
 
 
121
 
void tr_peerIoWriteBuf( tr_peerIo       * io,
122
 
                        struct evbuffer * buf );
123
 
 
124
 
 
125
 
/**
126
 
***
127
 
**/
128
 
 
129
 
struct tr_crypto* tr_peerIoGetCrypto( tr_peerIo * io );
 
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
    tr_priority_t         priority;
 
73
 
 
74
    int                   pendingEvents;
 
75
 
 
76
    int                   magicNumber;
 
77
 
 
78
    uint8_t               encryptionMode;
 
79
 
 
80
    tr_port               port;
 
81
    int                   socket;
 
82
 
 
83
    int                   refCount;
 
84
 
 
85
    uint8_t               peerId[SHA_DIGEST_LENGTH];
 
86
    time_t                timeCreated;
 
87
 
 
88
    tr_session          * session;
 
89
 
 
90
    tr_address            addr;
 
91
 
 
92
    tr_can_read_cb        canRead;
 
93
    tr_did_write_cb       didWrite;
 
94
    tr_net_error_cb       gotError;
 
95
    void *                userData;
 
96
 
 
97
    struct tr_bandwidth   bandwidth;
 
98
    struct tr_crypto    * crypto;
 
99
 
 
100
    struct evbuffer     * inbuf;
 
101
    struct evbuffer     * outbuf;
 
102
    struct __tr_list      outbuf_datatypes; /* struct tr_datatype */
 
103
 
 
104
    struct event          event_read;
 
105
    struct event          event_write;
 
106
}
 
107
tr_peerIo;
 
108
 
 
109
/**
 
110
***
 
111
**/
 
112
 
 
113
tr_peerIo*  tr_peerIoNewOutgoing( tr_session              * session,
 
114
                                  struct tr_bandwidth     * parent,
 
115
                                  const struct tr_address * addr,
 
116
                                  tr_port                   port,
 
117
                                  const  uint8_t          * torrentHash );
 
118
 
 
119
tr_peerIo*  tr_peerIoNewIncoming( tr_session              * session,
 
120
                                  struct tr_bandwidth     * parent,
 
121
                                  const struct tr_address * addr,
 
122
                                  tr_port                   port,
 
123
                                  int                       socket );
 
124
 
 
125
void tr_peerIoRefImpl           ( const char              * file,
 
126
                                  int                       line,
 
127
                                  tr_peerIo               * io );
 
128
 
 
129
#define tr_peerIoRef(io) tr_peerIoRefImpl( __FILE__, __LINE__, (io) );
 
130
 
 
131
void tr_peerIoUnrefImpl         ( const char              * file,
 
132
                                  int                       line,
 
133
                                  tr_peerIo               * io );
 
134
 
 
135
#define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) );
 
136
 
 
137
tr_bool     tr_isPeerIo         ( const tr_peerIo         * io );
 
138
 
 
139
 
 
140
/**
 
141
***
 
142
**/
 
143
 
 
144
void        tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag );
 
145
 
 
146
static TR_INLINE tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io )
 
147
{
 
148
    assert( tr_isPeerIo( io ) );
 
149
 
 
150
    return io->extendedProtocolSupported;
 
151
}
 
152
 
 
153
void        tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag );
 
154
 
 
155
static TR_INLINE tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io )
 
156
{
 
157
    assert( tr_isPeerIo( io ) );
 
158
 
 
159
    return io->fastExtensionSupported;
 
160
}
 
161
 
 
162
/**
 
163
***
 
164
**/
 
165
 
 
166
static TR_INLINE tr_session* tr_peerIoGetSession ( tr_peerIo * io )
 
167
{
 
168
    assert( tr_isPeerIo( io ) );
 
169
    assert( io->session );
 
170
 
 
171
    return io->session;
 
172
}
 
173
 
 
174
const char* tr_peerIoAddrStr( const struct tr_address * addr,
 
175
                              tr_port                   port );
 
176
 
 
177
static TR_INLINE const char* tr_peerIoGetAddrStr( const tr_peerIo * io )
 
178
{
 
179
    return tr_isPeerIo( io ) ? tr_peerIoAddrStr( &io->addr, io->port ) : "error";
 
180
}
 
181
 
 
182
const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io,
 
183
                                               tr_port         * port );
 
184
 
 
185
const uint8_t*       tr_peerIoGetTorrentHash( tr_peerIo * io );
 
186
 
 
187
int                  tr_peerIoHasTorrentHash( const tr_peerIo * io );
 
188
 
 
189
void                 tr_peerIoSetTorrentHash( tr_peerIo *     io,
 
190
                                              const uint8_t * hash );
 
191
 
 
192
int                  tr_peerIoReconnect( tr_peerIo * io );
 
193
 
 
194
static TR_INLINE tr_bool tr_peerIoIsIncoming( const tr_peerIo * io )
 
195
{
 
196
    return io->isIncoming;
 
197
}
 
198
 
 
199
static TR_INLINE int    tr_peerIoGetAge( const tr_peerIo * io )
 
200
{
 
201
    return time( NULL ) - io->timeCreated;
 
202
}
 
203
 
 
204
 
 
205
/**
 
206
***
 
207
**/
 
208
 
 
209
void                 tr_peerIoSetPeersId( tr_peerIo *     io,
 
210
                                          const uint8_t * peer_id );
 
211
 
 
212
static TR_INLINE const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io )
 
213
{
 
214
    assert( tr_isPeerIo( io ) );
 
215
    assert( io->peerIdIsSet );
 
216
 
 
217
    return io->peerId;
 
218
}
 
219
 
 
220
/**
 
221
***
 
222
**/
 
223
 
 
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,
 
228
                                   void             * user_data );
 
229
 
 
230
void    tr_peerIoClear           ( tr_peerIo        * io );
 
231
 
 
232
/**
 
233
***
 
234
**/
 
235
 
 
236
void    tr_peerIoWrite          ( tr_peerIo         * io,
 
237
                                  const void        * writeme,
 
238
                                  size_t              writemeLen,
 
239
                                  tr_bool             isPieceData );
 
240
 
 
241
void    tr_peerIoWriteBuf       ( tr_peerIo         * io,
 
242
                                  struct evbuffer   * buf,
 
243
                                  tr_bool             isPieceData );
 
244
 
 
245
/**
 
246
***
 
247
**/
 
248
 
 
249
static TR_INLINE struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
 
250
{
 
251
    return io->crypto;
 
252
}
130
253
 
131
254
typedef enum
132
255
{
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 )
136
259
}
137
260
EncryptionMode;
138
261
 
139
 
void tr_peerIoSetEncryption( tr_peerIo       * io,
140
 
                              int              encryptionMode );
141
 
 
142
 
int  tr_peerIoIsEncrypted( const tr_peerIo * io );
143
 
 
144
 
void tr_peerIoWriteBytes  ( tr_peerIo        * io,
145
 
                            struct evbuffer  * outbuf,
146
 
                            const void       * bytes,
147
 
                            size_t             byteCount );
148
 
 
149
 
void tr_peerIoWriteUint8  ( tr_peerIo        * io,
150
 
                            struct evbuffer  * outbuf,
151
 
                            uint8_t            writeme );
152
 
 
153
 
void tr_peerIoWriteUint16 ( tr_peerIo        * io,
154
 
                            struct evbuffer  * outbuf,
155
 
                            uint16_t           writeme );
156
 
 
157
 
void tr_peerIoWriteUint32 ( tr_peerIo        * io,
158
 
                            struct evbuffer  * outbuf,
159
 
                            uint32_t           writeme );
160
 
 
161
 
void tr_peerIoReadBytes   ( tr_peerIo        * io,
162
 
                            struct evbuffer  * inbuf,
163
 
                            void             * bytes,
164
 
                            size_t             byteCount );
165
 
 
166
 
void tr_peerIoReadUint8   ( tr_peerIo        * io,
167
 
                            struct evbuffer  * inbuf,
168
 
                            uint8_t          * setme );
169
 
 
170
 
void tr_peerIoReadUint16  ( tr_peerIo        * io,
171
 
                            struct evbuffer  * inbuf,
172
 
                            uint16_t         * setme );
173
 
 
174
 
void tr_peerIoReadUint32  ( tr_peerIo        * io,
175
 
                            struct evbuffer  * inbuf,
176
 
                            uint32_t         * setme );
177
 
 
178
 
void tr_peerIoDrain       ( tr_peerIo        * io,
179
 
                            struct evbuffer  * inbuf,
180
 
                            size_t             byteCount );
 
262
void      tr_peerIoSetEncryption( tr_peerIo * io,
 
263
                                  int         encryptionMode );
 
264
 
 
265
static TR_INLINE tr_bool tr_peerIoIsEncrypted( const tr_peerIo * io )
 
266
{
 
267
    return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 );
 
268
}
 
269
 
 
270
static TR_INLINE void tr_peerIoWriteBytes( tr_peerIo        * io UNUSED,
 
271
                                           struct evbuffer  * outbuf,
 
272
                                           const void       * bytes,
 
273
                                           size_t             byteCount )
 
274
{
 
275
    evbuffer_add( outbuf, bytes, byteCount );
 
276
}
 
277
 
 
278
static TR_INLINE void  tr_peerIoWriteUint8( tr_peerIo        * io,
 
279
                                            struct evbuffer  * outbuf,
 
280
                                            uint8_t            writeme )
 
281
{
 
282
    tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
 
283
}
 
284
 
 
285
static TR_INLINE void tr_peerIoWriteUint16( tr_peerIo        * io,
 
286
                                            struct evbuffer  * outbuf,
 
287
                                            uint16_t           writeme )
 
288
{
 
289
    const uint16_t tmp = htons( writeme );
 
290
    tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
 
291
}
 
292
 
 
293
static TR_INLINE void tr_peerIoWriteUint32( tr_peerIo        * io,
 
294
                                            struct evbuffer  * outbuf,
 
295
                                            uint32_t           writeme )
 
296
{
 
297
    const uint32_t tmp = htonl( writeme );
 
298
    tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
 
299
}
 
300
 
 
301
void tr_peerIoReadBytes( tr_peerIo        * io,
 
302
                         struct evbuffer  * inbuf,
 
303
                         void             * bytes,
 
304
                         size_t             byteCount );
 
305
 
 
306
static TR_INLINE void tr_peerIoReadUint8( tr_peerIo        * io,
 
307
                                          struct evbuffer  * inbuf,
 
308
                                          uint8_t          * setme )
 
309
{
 
310
    tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) );
 
311
}
 
312
 
 
313
static TR_INLINE void tr_peerIoReadUint16( tr_peerIo        * io,
 
314
                                           struct evbuffer  * inbuf,
 
315
                                           uint16_t         * setme )
 
316
{
 
317
    uint16_t tmp;
 
318
    tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
 
319
    *setme = ntohs( tmp );
 
320
}
 
321
 
 
322
static TR_INLINE void tr_peerIoReadUint32( tr_peerIo        * io,
 
323
                                           struct evbuffer  * inbuf,
 
324
                                           uint32_t         * setme )
 
325
{
 
326
    uint32_t tmp;
 
327
    tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
 
328
    *setme = ntohl( tmp );
 
329
}
 
330
 
 
331
void      tr_peerIoDrain( tr_peerIo        * io,
 
332
                          struct evbuffer  * inbuf,
 
333
                          size_t             byteCount );
 
334
 
 
335
/**
 
336
***
 
337
**/
 
338
 
 
339
size_t    tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now );
 
340
 
 
341
static TR_INLINE void tr_peerIoSetParent( tr_peerIo            * io,
 
342
                                          struct tr_bandwidth  * parent )
 
343
{
 
344
    assert( tr_isPeerIo( io ) );
 
345
 
 
346
    tr_bandwidthSetParent( &io->bandwidth, parent );
 
347
}
 
348
 
 
349
void      tr_peerIoBandwidthUsed( tr_peerIo           * io,
 
350
                                  tr_direction          direction,
 
351
                                  size_t                byteCount,
 
352
                                  int                   isPieceData );
 
353
 
 
354
static TR_INLINE tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo  * io,
 
355
                                                    tr_direction       dir )
 
356
{
 
357
    assert( tr_isPeerIo( io ) );
 
358
 
 
359
    return !io->hasFinishedConnecting
 
360
        || ( tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0 );
 
361
}
 
362
 
 
363
static TR_INLINE double tr_peerIoGetPieceSpeed( const tr_peerIo * io, uint64_t now, tr_direction dir )
 
364
{
 
365
    assert( tr_isPeerIo( io ) );
 
366
    assert( tr_isDirection( dir ) );
 
367
 
 
368
    return tr_bandwidthGetPieceSpeed( &io->bandwidth, now, dir );
 
369
}
 
370
 
 
371
/**
 
372
***
 
373
**/
 
374
 
 
375
void      tr_peerIoSetEnabled( tr_peerIo    * io,
 
376
                               tr_direction   dir,
 
377
                               tr_bool        isEnabled );
 
378
                       
 
379
int       tr_peerIoFlush( tr_peerIo     * io,
 
380
                          tr_direction    dir,
 
381
                          size_t          byteLimit );
 
382
 
 
383
int       tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io );
 
384
 
 
385
/**
 
386
***
 
387
**/
 
388
 
 
389
static TR_INLINE struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io )
 
390
{
 
391
    return io->inbuf;
 
392
}
181
393
 
182
394
 
183
395
#endif