1 | /****************************************************************************** |
---|
2 | * $Id: internal.h 2552 2007-07-30 15:27:52Z 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 | |
---|
29 | #ifdef SYS_BEOS |
---|
30 | /* BeOS doesn't declare vasprintf in its headers, but actually |
---|
31 | * implements it */ |
---|
32 | extern int vasprintf( char **, const char *, va_list ); |
---|
33 | #include <signal.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifndef __AMIGAOS4__ |
---|
37 | #include <sys/types.h> |
---|
38 | #include <sys/resource.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifdef SYS_BEOS |
---|
42 | # define socklen_t uint32_t |
---|
43 | #endif |
---|
44 | |
---|
45 | #ifdef BEOS_NETSERVER |
---|
46 | # define in_port_t uint16_t |
---|
47 | #else |
---|
48 | # include <arpa/inet.h> |
---|
49 | #endif |
---|
50 | |
---|
51 | #define TR_NAME "Transmission" |
---|
52 | |
---|
53 | #ifndef INADDR_NONE |
---|
54 | #define INADDR_NONE 0xffffffff |
---|
55 | #endif |
---|
56 | |
---|
57 | #ifdef __GNUC__ |
---|
58 | # define UNUSED __attribute__((unused)) |
---|
59 | # define PRINTF( fmt, args ) __attribute__((format (printf, fmt, args))) |
---|
60 | #else |
---|
61 | # define UNUSED |
---|
62 | # define PRINTF( fmt, args ) |
---|
63 | #endif |
---|
64 | |
---|
65 | /* We use OpenSSL whenever possible, since it is likely to be more |
---|
66 | optimized and it is ok to use it with a MIT-licensed application. |
---|
67 | Otherwise, we use the included implementation by vi@nwr.jp. */ |
---|
68 | #if defined(HAVE_OPENSSL) || defined(HAVE_LIBSSL) |
---|
69 | # undef SHA_DIGEST_LENGTH |
---|
70 | # include <openssl/sha.h> |
---|
71 | #else |
---|
72 | # include "sha1.h" |
---|
73 | # define SHA1(p,i,h) \ |
---|
74 | { \ |
---|
75 | sha1_state_s pms; \ |
---|
76 | sha1_init( &pms ); \ |
---|
77 | sha1_update( &pms, (sha1_byte_t *) p, i ); \ |
---|
78 | sha1_finish( &pms, (sha1_byte_t *) h ); \ |
---|
79 | } |
---|
80 | #endif |
---|
81 | |
---|
82 | #define TR_MAX_PEER_COUNT 60 |
---|
83 | |
---|
84 | typedef struct tr_completion_s tr_completion_t; |
---|
85 | typedef struct tr_shared_s tr_shared_t; |
---|
86 | |
---|
87 | typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t; |
---|
88 | |
---|
89 | #include "platform.h" |
---|
90 | #include "tracker.h" |
---|
91 | #include "peer.h" |
---|
92 | #include "inout.h" |
---|
93 | #include "ratecontrol.h" |
---|
94 | #include "utils.h" |
---|
95 | |
---|
96 | #ifndef TRUE |
---|
97 | #define TRUE 1 |
---|
98 | #endif |
---|
99 | #ifndef FALSE |
---|
100 | #define FALSE 0 |
---|
101 | #endif |
---|
102 | |
---|
103 | void tr_peerIdNew ( char* buf, int buflen ); |
---|
104 | |
---|
105 | void tr_torrentResetTransferStats( tr_torrent_t * ); |
---|
106 | |
---|
107 | int tr_torrentAddCompact( tr_torrent_t * tor, int from, |
---|
108 | uint8_t * buf, int count ); |
---|
109 | int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer ); |
---|
110 | |
---|
111 | void tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has ); |
---|
112 | |
---|
113 | void tr_torrentReaderLock ( const tr_torrent_t * ); |
---|
114 | void tr_torrentReaderUnlock ( const tr_torrent_t * ); |
---|
115 | void tr_torrentWriterLock ( tr_torrent_t * ); |
---|
116 | void tr_torrentWriterUnlock ( tr_torrent_t * ); |
---|
117 | |
---|
118 | /* get the index of this piece's first block */ |
---|
119 | #define tr_torPieceFirstBlock(tor,piece) ( (piece) * (tor)->blockCountInPiece ) |
---|
120 | |
---|
121 | /* what piece index is this block in? */ |
---|
122 | #define tr_torBlockPiece(tor,block) ( (block) / (tor)->blockCountInPiece ) |
---|
123 | |
---|
124 | /* how many blocks are in this piece? */ |
---|
125 | #define tr_torPieceCountBlocks(tor,piece) \ |
---|
126 | ( ((piece)==((tor)->info.pieceCount-1)) ? (tor)->blockCountInLastPiece : (tor)->blockCountInPiece ) |
---|
127 | |
---|
128 | /* how many bytes are in this piece? */ |
---|
129 | #define tr_torPieceCountBytes(tor,piece) \ |
---|
130 | ( ((piece)==((tor)->info.pieceCount-1)) ? (tor)->lastPieceSize : (tor)->info.pieceSize ) |
---|
131 | |
---|
132 | /* how many bytes are in this block? */ |
---|
133 | #define tr_torBlockCountBytes(tor,block) \ |
---|
134 | ( ((block)==((tor)->blockCount-1)) ? (tor)->lastBlockSize : (tor)->blockSize ) |
---|
135 | |
---|
136 | #define tr_block(a,b) _tr_block(tor,a,b) |
---|
137 | int _tr_block( const tr_torrent_t * tor, int index, int begin ); |
---|
138 | |
---|
139 | |
---|
140 | typedef enum |
---|
141 | { |
---|
142 | TR_RUN_CHECKING = (1<<0), /* checking files' checksums */ |
---|
143 | TR_RUN_RUNNING = (1<<1), /* seeding or leeching */ |
---|
144 | TR_RUN_STOPPING = (1<<2), /* stopping */ |
---|
145 | TR_RUN_STOPPING_NET_WAIT = (1<<3), /* waiting on network -- we're |
---|
146 | telling tracker we've stopped */ |
---|
147 | TR_RUN_STOPPED = (1<<4) /* stopped */ |
---|
148 | } |
---|
149 | run_status_t; |
---|
150 | |
---|
151 | #define TR_ID_LEN 20 |
---|
152 | #define TR_KEY_LEN 20 |
---|
153 | |
---|
154 | struct tr_torrent_s |
---|
155 | { |
---|
156 | tr_handle_t * handle; |
---|
157 | tr_info_t info; |
---|
158 | |
---|
159 | tr_speedlimit_t uploadLimitMode; |
---|
160 | tr_speedlimit_t downloadLimitMode; |
---|
161 | tr_ratecontrol_t * upload; |
---|
162 | tr_ratecontrol_t * download; |
---|
163 | tr_ratecontrol_t * swarmspeed; |
---|
164 | |
---|
165 | int error; |
---|
166 | char errorString[128]; |
---|
167 | int hasChangedState; |
---|
168 | |
---|
169 | char peer_id[TR_ID_LEN+1]; |
---|
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 | int lastBlockSize; |
---|
185 | int lastPieceSize; |
---|
186 | |
---|
187 | int blockCountInPiece; |
---|
188 | int blockCountInLastPiece; |
---|
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 |
---|