1 | /****************************************************************************** |
---|
2 | * $Id: internal.h 2462 2007-07-23 03:00:20Z 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 */ |
---|
38 | int 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 | #define TR_MAX_PEER_COUNT 60 |
---|
89 | |
---|
90 | typedef struct tr_completion_s tr_completion_t; |
---|
91 | typedef struct tr_shared_s tr_shared_t; |
---|
92 | |
---|
93 | typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t; |
---|
94 | |
---|
95 | #include "platform.h" |
---|
96 | #include "tracker.h" |
---|
97 | #include "peer.h" |
---|
98 | #include "inout.h" |
---|
99 | #include "ratecontrol.h" |
---|
100 | #include "utils.h" |
---|
101 | |
---|
102 | #ifndef TRUE |
---|
103 | #define TRUE 1 |
---|
104 | #endif |
---|
105 | #ifndef FALSE |
---|
106 | #define FALSE 0 |
---|
107 | #endif |
---|
108 | |
---|
109 | void tr_peerIdNew ( char* buf, int buflen ); |
---|
110 | |
---|
111 | void tr_torrentResetTransferStats( tr_torrent_t * ); |
---|
112 | |
---|
113 | int tr_torrentAddCompact( tr_torrent_t * tor, int from, |
---|
114 | uint8_t * buf, int count ); |
---|
115 | int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer ); |
---|
116 | |
---|
117 | void tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has ); |
---|
118 | |
---|
119 | void tr_torrentReaderLock ( const tr_torrent_t * ); |
---|
120 | void tr_torrentReaderUnlock ( const tr_torrent_t * ); |
---|
121 | void tr_torrentWriterLock ( tr_torrent_t * ); |
---|
122 | void tr_torrentWriterUnlock ( tr_torrent_t * ); |
---|
123 | |
---|
124 | #define tr_blockPiece(a) _tr_blockPiece(tor,a) |
---|
125 | int _tr_blockPiece( const tr_torrent_t * tor, int block ); |
---|
126 | |
---|
127 | #define tr_blockSize(a) _tr_blockSize(tor,a) |
---|
128 | int _tr_blockSize( const tr_torrent_t * tor, int block ); |
---|
129 | |
---|
130 | #define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a) |
---|
131 | int _tr_blockPosInPiece( const tr_torrent_t * tor, int block ); |
---|
132 | |
---|
133 | #define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a) |
---|
134 | int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece ); |
---|
135 | |
---|
136 | #define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a) |
---|
137 | int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece ); |
---|
138 | |
---|
139 | #define tr_pieceSize(a) _tr_pieceSize(tor,a) |
---|
140 | int _tr_pieceSize( const tr_torrent_t * tor, int piece ); |
---|
141 | |
---|
142 | #define tr_block(a,b) _tr_block(tor,a,b) |
---|
143 | int _tr_block( const tr_torrent_t * tor, int index, int begin ); |
---|
144 | |
---|
145 | |
---|
146 | typedef enum |
---|
147 | { |
---|
148 | TR_RUN_CHECKING = (1<<0), /* checking files' checksums */ |
---|
149 | TR_RUN_RUNNING = (1<<1), /* seeding or leeching */ |
---|
150 | TR_RUN_STOPPING = (1<<2), /* stopping */ |
---|
151 | TR_RUN_STOPPING_NET_WAIT = (1<<3), /* waiting on network -- we're |
---|
152 | telling tracker we've stopped */ |
---|
153 | TR_RUN_STOPPED = (1<<4) /* stopped */ |
---|
154 | } |
---|
155 | run_status_t; |
---|
156 | |
---|
157 | #define TR_ID_LEN 20 |
---|
158 | #define TR_KEY_LEN 20 |
---|
159 | |
---|
160 | struct tr_torrent_s |
---|
161 | { |
---|
162 | tr_handle_t * handle; |
---|
163 | tr_info_t info; |
---|
164 | |
---|
165 | tr_speedlimit_t uploadLimitMode; |
---|
166 | tr_speedlimit_t downloadLimitMode; |
---|
167 | tr_ratecontrol_t * upload; |
---|
168 | tr_ratecontrol_t * download; |
---|
169 | tr_ratecontrol_t * swarmspeed; |
---|
170 | |
---|
171 | int error; |
---|
172 | char errorString[128]; |
---|
173 | int hasChangedState; |
---|
174 | |
---|
175 | char peer_id[TR_ID_LEN+1]; |
---|
176 | char * key; |
---|
177 | uint8_t * azId; |
---|
178 | int publicPort; |
---|
179 | |
---|
180 | /* An escaped string used to include the hash in HTTP queries */ |
---|
181 | char escapedHashString[3*SHA_DIGEST_LENGTH+1]; |
---|
182 | |
---|
183 | /* Where to download */ |
---|
184 | char * destination; |
---|
185 | |
---|
186 | /* How many bytes we ask for per request */ |
---|
187 | int blockSize; |
---|
188 | int blockCount; |
---|
189 | |
---|
190 | tr_completion_t * completion; |
---|
191 | |
---|
192 | volatile char dieFlag; |
---|
193 | tr_bitfield_t * uncheckedPieces; |
---|
194 | run_status_t runStatus; |
---|
195 | cp_status_t cpStatus; |
---|
196 | tr_thread_t thread; |
---|
197 | tr_rwlock_t lock; |
---|
198 | |
---|
199 | tr_tracker_t * tracker; |
---|
200 | tr_io_t * io; |
---|
201 | uint64_t startDate; |
---|
202 | uint64_t stopDate; |
---|
203 | char ioLoaded; |
---|
204 | char fastResumeDirty; |
---|
205 | |
---|
206 | int peerCount; |
---|
207 | tr_peer_t * peers[TR_MAX_PEER_COUNT]; |
---|
208 | |
---|
209 | uint64_t downloadedCur; |
---|
210 | uint64_t downloadedPrev; |
---|
211 | uint64_t uploadedCur; |
---|
212 | uint64_t uploadedPrev; |
---|
213 | uint64_t activityDate; |
---|
214 | |
---|
215 | uint8_t pexDisabled; |
---|
216 | |
---|
217 | int8_t statCur; |
---|
218 | tr_stat_t stats[2]; |
---|
219 | |
---|
220 | tr_torrent_t * next; |
---|
221 | }; |
---|
222 | |
---|
223 | #include "utils.h" |
---|
224 | #include "completion.h" |
---|
225 | |
---|
226 | struct tr_handle_s |
---|
227 | { |
---|
228 | int torrentCount; |
---|
229 | tr_torrent_t * torrentList; |
---|
230 | |
---|
231 | char * tag; |
---|
232 | int isPortSet; |
---|
233 | |
---|
234 | char useUploadLimit; |
---|
235 | tr_ratecontrol_t * upload; |
---|
236 | char useDownloadLimit; |
---|
237 | tr_ratecontrol_t * download; |
---|
238 | |
---|
239 | tr_shared_t * shared; |
---|
240 | |
---|
241 | char key[TR_KEY_LEN+1]; |
---|
242 | |
---|
243 | tr_handle_status_t stats[2]; |
---|
244 | int statCur; |
---|
245 | #define TR_AZ_ID_LEN 20 |
---|
246 | uint8_t azId[TR_AZ_ID_LEN]; |
---|
247 | }; |
---|
248 | |
---|
249 | #endif |
---|