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