source: trunk/libtransmission/internal.h @ 1534

Last change on this file since 1534 was 1534, checked in by joshe, 15 years ago

Do bounds checking on bitfields.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.0 KB
Line 
1/******************************************************************************
2 * $Id: internal.h 1534 2007-03-05 23:03:38Z joshe $
3 *
4 * Copyright (c) 2005-2006 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#include <stdarg.h>
36#ifdef SYS_BEOS
37/* BeOS doesn't declare vasprintf in its headers, but actually
38 * implements it */
39int vasprintf( char **, const char *, va_list );
40#endif
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <errno.h>
45#include <limits.h>
46#include <signal.h>
47#include <time.h>
48#include <sys/time.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#ifndef __AMIGAOS4__
52#include <sys/resource.h>
53#endif
54#include <netdb.h>
55#include <sys/socket.h>
56#include <netinet/in.h>
57#include <fcntl.h>
58#include <assert.h>
59#ifdef SYS_BEOS
60#  define socklen_t uint32_t
61#endif
62#ifdef BEOS_NETSERVER
63#  define in_port_t uint16_t
64#else
65#  include <arpa/inet.h>
66#endif
67
68#ifndef INADDR_NONE
69#define INADDR_NONE             0xffffffff
70#endif
71
72#ifdef __GNUC__
73#  define UNUSED __attribute__((unused))
74#  define PRINTF( fmt, args ) __attribute__((format (printf, fmt, args)))
75#else
76#  define UNUSED
77#  define PRINTF( fmt, args )
78#endif
79
80/* We use OpenSSL whenever possible, since it is likely to be more
81   optimized and it is ok to use it with a MIT-licensed application.
82   Otherwise, we use the included implementation by vi@nwr.jp. */
83#ifdef HAVE_OPENSSL
84#  undef SHA_DIGEST_LENGTH
85#  include <openssl/sha.h>
86#else
87#  include "sha1.h"
88#  define SHA1(p,i,h) \
89   { \
90     sha1_state_s pms; \
91     sha1_init( &pms ); \
92     sha1_update( &pms, (sha1_byte_t *) p, i ); \
93     sha1_finish( &pms, (sha1_byte_t *) h ); \
94   }
95#endif
96
97/* Convenient macros to perform uint32_t endian conversions with
98   char pointers */
99#define TR_NTOHL(p,a) (a) = tr_ntohl((p))
100#define TR_HTONL(a,p) tr_htonl((a), (p))
101static inline uint32_t tr_ntohl( uint8_t * p )
102{
103        uint32_t u;
104        memcpy( &u, p, sizeof( uint32_t ) );
105        return ntohl( u );
106}
107static inline void tr_htonl( uint32_t a, uint8_t * p )
108{
109        uint32_t u;
110        u = htonl( a );
111        memcpy ( p, &u, sizeof( uint32_t ) );
112}
113
114/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
115   case, define those here since we will use them */
116#ifndef MAX
117#define MAX(a,b) ((a)>(b)?(a):(b))
118#endif
119#ifndef MIN
120#define MIN(a,b) ((a)>(b)?(b):(a))
121#endif
122
123#define TR_MAX_PEER_COUNT 60
124
125typedef struct tr_completion_s tr_completion_t;
126typedef struct tr_shared_s tr_shared_t;
127typedef struct tr_bitfield_s tr_bitfield_t;
128
129typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
130
131#include "platform.h"
132#include "bencode.h"
133#include "metainfo.h"
134#include "tracker.h"
135#include "fdlimit.h"
136#include "peer.h"
137#include "net.h"
138#include "inout.h"
139#include "ratecontrol.h"
140#include "clients.h"
141#include "choking.h"
142#include "natpmp.h"
143#include "upnp.h"
144#include "http.h"
145#include "xml.h"
146
147void tr_torrentAddCompact( tr_torrent_t * tor, uint8_t * buf, int count );
148void tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer );
149
150struct tr_torrent_s
151{
152    tr_handle_t * handle;
153    tr_info_t info;
154
155    int                customUploadLimit;
156    int                customDownloadLimit;
157    tr_ratecontrol_t * upload;
158    tr_ratecontrol_t * download;
159    tr_ratecontrol_t * swarmspeed;
160
161    int               status;
162    int               error;
163    char              errorString[128];
164    int               finished;
165
166    char            * id;
167    char            * key;
168    int               publicPort;
169
170    /* An escaped string used to include the hash in HTTP queries */
171    char              escapedHashString[3*SHA_DIGEST_LENGTH+1];
172
173    /* Where to download */
174    char            * destination;
175   
176    /* How many bytes we ask for per request */
177    int               blockSize;
178    int               blockCount;
179   
180    tr_completion_t * completion;
181
182    volatile char     die;
183    tr_thread_t       thread;
184    tr_lock_t         lock;
185    tr_cond_t         cond;
186
187    tr_tracker_t    * tracker;
188    tr_io_t         * io;
189    uint64_t          stopDate;
190
191    int               peerCount;
192    tr_peer_t       * peers[TR_MAX_PEER_COUNT];
193
194    uint64_t          date;
195    uint64_t          downloadedCur;
196    uint64_t          downloadedPrev;
197    uint64_t          uploadedCur;
198    uint64_t          uploadedPrev;
199
200    tr_stat_t         stats[2];
201    int               statCur;
202
203    tr_torrent_t    * prev;
204    tr_torrent_t    * next;
205};
206
207#include "utils.h"
208#include "completion.h"
209
210struct tr_handle_s
211{
212    int            torrentCount;
213    tr_torrent_t * torrentList;
214
215    int            isPortSet;
216    int            uploadLimit;
217    int            downloadLimit;
218    tr_shared_t  * shared;
219
220    char           id[21];
221    char           key[21];
222
223    tr_handle_status_t stats[2];
224    int                statCur;
225};
226
227#endif
Note: See TracBrowser for help on using the repository browser.