source: trunk/libtransmission/internal.h @ 2374

Last change on this file since 2374 was 2374, checked in by charles, 15 years ago

re-randomize peer_id each time we start a torrent. (ticket #257)

  • Property svn:keywords set to Date Rev Author Id
File size: 6.9 KB
Line 
1/******************************************************************************
2 * $Id: internal.h 2374 2007-07-16 11:19:44Z charles $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef TR_INTERNAL_H
26#define TR_INTERNAL_H 1
27
28/* Standard headers used here and there.
29   That is probably ugly to put them all here, but it is sooo
30   convenient */
31#if ( defined( __unix__ ) || defined( unix ) ) && !defined( USG )
32#include <sys/param.h>
33#endif
34#include <stdio.h>
35#ifdef SYS_BEOS
36/* BeOS doesn't declare vasprintf in its headers, but actually
37 * implements it */
38int vasprintf( char **, const char *, va_list );
39#endif
40#include <stdlib.h>
41#include <string.h>
42#include <errno.h>
43#include <time.h>
44#ifndef __AMIGAOS4__
45#include <sys/resource.h>
46#endif
47#include <assert.h>
48#ifdef SYS_BEOS
49#  define socklen_t uint32_t
50#endif
51#ifdef BEOS_NETSERVER
52#  define in_port_t uint16_t
53#else
54#  include <arpa/inet.h>
55#endif
56
57#define TR_NAME                 "Transmission"
58
59#ifndef INADDR_NONE
60#define INADDR_NONE             0xffffffff
61#endif
62
63#ifdef __GNUC__
64#  define UNUSED __attribute__((unused))
65#  define PRINTF( fmt, args ) __attribute__((format (printf, fmt, args)))
66#else
67#  define UNUSED
68#  define PRINTF( fmt, args )
69#endif
70
71/* We use OpenSSL whenever possible, since it is likely to be more
72   optimized and it is ok to use it with a MIT-licensed application.
73   Otherwise, we use the included implementation by vi@nwr.jp. */
74#ifdef HAVE_OPENSSL
75#  undef SHA_DIGEST_LENGTH
76#  include <openssl/sha.h>
77#else
78#  include "sha1.h"
79#  define SHA1(p,i,h) \
80   { \
81     sha1_state_s pms; \
82     sha1_init( &pms ); \
83     sha1_update( &pms, (sha1_byte_t *) p, i ); \
84     sha1_finish( &pms, (sha1_byte_t *) h ); \
85   }
86#endif
87
88/* Convenient macros to perform uint32_t endian conversions with
89   char pointers */
90#define TR_NTOHL(p,a) (a) = tr_ntohl((p))
91#define TR_HTONL(a,p) tr_htonl((a), ( uint8_t * )(p))
92static inline uint32_t tr_ntohl( uint8_t * p )
93{
94        uint32_t u;
95        memcpy( &u, p, sizeof( uint32_t ) );
96        return ntohl( u );
97}
98static inline void tr_htonl( uint32_t a, uint8_t * p )
99{
100        uint32_t u;
101        u = htonl( a );
102        memcpy ( p, &u, sizeof( uint32_t ) );
103}
104
105/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
106   case, define those here since we will use them */
107#ifndef MAX
108#define MAX(a,b) ((a)>(b)?(a):(b))
109#endif
110#ifndef MIN
111#define MIN(a,b) ((a)>(b)?(b):(a))
112#endif
113
114#define TR_MAX_PEER_COUNT 60
115
116typedef struct tr_completion_s tr_completion_t;
117typedef struct tr_shared_s tr_shared_t;
118typedef struct tr_bitfield_s tr_bitfield_t;
119
120typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
121
122#include "platform.h"
123#include "tracker.h"
124#include "peer.h"
125#include "inout.h"
126#include "ratecontrol.h"
127
128#ifndef TRUE
129#define TRUE 1
130#endif
131#ifndef FALSE
132#define FALSE 0
133#endif
134
135void tr_peerIdNew ( char* buf, int buflen );
136
137void tr_torrentResetTransferStats( tr_torrent_t * );
138
139int tr_torrentAddCompact( tr_torrent_t * tor, int from,
140                           uint8_t * buf, int count );
141int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer );
142
143void tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has );
144
145void tr_torrentReaderLock    ( const tr_torrent_t * );
146void tr_torrentReaderUnlock  ( const tr_torrent_t * );
147void tr_torrentWriterLock    ( tr_torrent_t * );
148void tr_torrentWriterUnlock  ( tr_torrent_t * );
149
150typedef enum
151{
152    TR_RUN_CHECKING           = (1<<0), /* checking files' checksums */
153    TR_RUN_RUNNING            = (1<<1), /* seeding or leeching */
154    TR_RUN_STOPPING           = (1<<2), /* stopping */
155    TR_RUN_STOPPING_NET_WAIT  = (1<<3), /* waiting on network -- we're
156                                           telling tracker we've stopped */
157    TR_RUN_STOPPED            = (1<<4)  /* stopped */
158}
159run_status_t;
160
161#define TR_ID_LEN               20
162#define TR_KEY_LEN              20
163
164struct tr_torrent_s
165{
166    tr_handle_t * handle;
167    tr_info_t info;
168
169    int                customUploadLimit;
170    int                customDownloadLimit;
171    tr_ratecontrol_t * upload;
172    tr_ratecontrol_t * download;
173    tr_ratecontrol_t * swarmspeed;
174
175    int               error;
176    char              errorString[128];
177    int               hasChangedState;
178
179    char              peer_id[TR_ID_LEN+1];
180    char            * key;
181    uint8_t         * azId;
182    int               publicPort;
183
184    /* An escaped string used to include the hash in HTTP queries */
185    char              escapedHashString[3*SHA_DIGEST_LENGTH+1];
186
187    /* Where to download */
188    char            * destination;
189   
190    /* How many bytes we ask for per request */
191    int               blockSize;
192    int               blockCount;
193   
194    tr_completion_t * completion;
195
196    volatile char     dieFlag;
197    volatile char     recheckFlag;
198    run_status_t      runStatus;
199    cp_status_t       cpStatus;
200    tr_thread_t       thread;
201    tr_rwlock_t       lock;
202
203    tr_tracker_t    * tracker;
204    tr_io_t         * io;
205    uint64_t          startDate;
206    uint64_t          stopDate;
207    int               ioLoaded;
208
209    int               peerCount;
210    tr_peer_t       * peers[TR_MAX_PEER_COUNT];
211
212    uint64_t          downloadedCur;
213    uint64_t          downloadedPrev;
214    uint64_t          uploadedCur;
215    uint64_t          uploadedPrev;
216    uint64_t          activityDate;
217
218    uint8_t           pexDisabled;
219
220    tr_stat_t         stats[2];
221    int               statCur;
222
223    tr_torrent_t    * next;
224};
225
226#include "utils.h"
227#include "completion.h"
228
229struct tr_handle_s
230{
231    int                torrentCount;
232    tr_torrent_t     * torrentList;
233
234    char             * tag;
235    int                isPortSet;
236
237    tr_ratecontrol_t * upload;
238    tr_ratecontrol_t * download;
239
240    tr_shared_t      * shared;
241
242    char           key[TR_KEY_LEN+1];
243
244    tr_handle_status_t stats[2];
245    int                statCur;
246#define TR_AZ_ID_LEN            20
247    uint8_t        azId[TR_AZ_ID_LEN];
248};
249
250#endif
Note: See TracBrowser for help on using the repository browser.