1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: torrent.h 10664 2010-05-19 19:02:25Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_TORRENT_H |
---|
18 | #define TR_TORRENT_H 1 |
---|
19 | |
---|
20 | #include "completion.h" /* tr_completion */ |
---|
21 | #include "session.h" /* tr_sessionLock(), tr_sessionUnlock() */ |
---|
22 | #include "utils.h" /* TR_GNUC_PRINTF */ |
---|
23 | |
---|
24 | struct tr_bandwidth; |
---|
25 | struct tr_torrent_tiers; |
---|
26 | struct tr_magnet_info; |
---|
27 | |
---|
28 | /** |
---|
29 | *** Package-visible ctor API |
---|
30 | **/ |
---|
31 | |
---|
32 | void tr_ctorSetSave( tr_ctor * ctor, |
---|
33 | tr_bool saveMetadataInOurTorrentsDir ); |
---|
34 | |
---|
35 | int tr_ctorGetSave( const tr_ctor * ctor ); |
---|
36 | |
---|
37 | void tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor ); |
---|
38 | |
---|
39 | void tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor ); |
---|
40 | |
---|
41 | /** |
---|
42 | *** |
---|
43 | **/ |
---|
44 | |
---|
45 | /* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */ |
---|
46 | void tr_torrentInitFileDLs( tr_torrent * tor, |
---|
47 | const tr_file_index_t * files, |
---|
48 | tr_file_index_t fileCount, |
---|
49 | tr_bool do_download ); |
---|
50 | |
---|
51 | void tr_torrentRecheckCompleteness( tr_torrent * ); |
---|
52 | |
---|
53 | void tr_torrentSetHasPiece( tr_torrent * tor, |
---|
54 | tr_piece_index_t pieceIndex, |
---|
55 | tr_bool has ); |
---|
56 | |
---|
57 | void tr_torrentChangeMyPort( tr_torrent * session ); |
---|
58 | |
---|
59 | tr_torrent* tr_torrentFindFromHashString( tr_session * session, |
---|
60 | const char * hashString ); |
---|
61 | |
---|
62 | tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session * session, |
---|
63 | const uint8_t * hash ); |
---|
64 | |
---|
65 | tr_bool tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent, |
---|
66 | tr_direction direction ); |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | #define tr_block( a, b ) _tr_block( tor, a, b ) |
---|
71 | tr_block_index_t _tr_block( const tr_torrent * tor, |
---|
72 | tr_piece_index_t index, |
---|
73 | uint32_t offset ); |
---|
74 | |
---|
75 | tr_bool tr_torrentReqIsValid( const tr_torrent * tor, |
---|
76 | tr_piece_index_t index, |
---|
77 | uint32_t offset, |
---|
78 | uint32_t length ); |
---|
79 | |
---|
80 | uint64_t tr_pieceOffset( const tr_torrent * tor, |
---|
81 | tr_piece_index_t index, |
---|
82 | uint32_t offset, |
---|
83 | uint32_t length ); |
---|
84 | |
---|
85 | void tr_torrentInitFilePriority( tr_torrent * tor, |
---|
86 | tr_file_index_t fileIndex, |
---|
87 | tr_priority_t priority ); |
---|
88 | |
---|
89 | int tr_torrentCountUncheckedPieces( const tr_torrent * ); |
---|
90 | |
---|
91 | tr_bool tr_torrentIsFileChecked( const tr_torrent * tor, |
---|
92 | tr_file_index_t file ); |
---|
93 | |
---|
94 | void tr_torrentSetPieceChecked( tr_torrent * tor, |
---|
95 | tr_piece_index_t piece, |
---|
96 | tr_bool isChecked ); |
---|
97 | |
---|
98 | void tr_torrentSetFileChecked( tr_torrent * tor, |
---|
99 | tr_file_index_t file, |
---|
100 | tr_bool isChecked ); |
---|
101 | |
---|
102 | void tr_torrentUncheck( tr_torrent * tor ); |
---|
103 | |
---|
104 | time_t* tr_torrentGetMTimes( const tr_torrent * tor, |
---|
105 | size_t * setmeCount ); |
---|
106 | |
---|
107 | tr_torrent* tr_torrentNext( tr_session * session, |
---|
108 | tr_torrent * current ); |
---|
109 | |
---|
110 | void tr_torrentCheckSeedRatio( tr_torrent * tor ); |
---|
111 | |
---|
112 | /** save a torrent's .resume file if it's changed since the last time it was saved */ |
---|
113 | void tr_torrentSave( tr_torrent * tor ); |
---|
114 | |
---|
115 | void tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 ); |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | typedef enum |
---|
120 | { |
---|
121 | TR_VERIFY_NONE, |
---|
122 | TR_VERIFY_WAIT, |
---|
123 | TR_VERIFY_NOW |
---|
124 | } |
---|
125 | tr_verify_state; |
---|
126 | |
---|
127 | void tr_torrentSetVerifyState( tr_torrent * tor, |
---|
128 | tr_verify_state state ); |
---|
129 | |
---|
130 | tr_torrent_activity tr_torrentGetActivity( tr_torrent * tor ); |
---|
131 | |
---|
132 | struct tr_incomplete_metadata; |
---|
133 | |
---|
134 | /** @brief Torrent object */ |
---|
135 | struct tr_torrent |
---|
136 | { |
---|
137 | tr_session * session; |
---|
138 | tr_info info; |
---|
139 | |
---|
140 | int magicNumber; |
---|
141 | |
---|
142 | tr_stat_errtype error; |
---|
143 | char errorString[128]; |
---|
144 | char errorTracker[128]; |
---|
145 | |
---|
146 | uint8_t obfuscatedHash[SHA_DIGEST_LENGTH]; |
---|
147 | |
---|
148 | /* Used when the torrent has been created with a magnet link |
---|
149 | * and we're in the process of downloading the metainfo from |
---|
150 | * other peers */ |
---|
151 | struct tr_incomplete_metadata * incompleteMetadata; |
---|
152 | |
---|
153 | /* If the initiator of the connection receives a handshake in which the |
---|
154 | * peer_id does not match the expected peerid, then the initiator is |
---|
155 | * expected to drop the connection. Note that the initiator presumably |
---|
156 | * received the peer information from the tracker, which includes the |
---|
157 | * peer_id that was registered by the peer. The peer_id from the tracker |
---|
158 | * and in the handshake are expected to match. |
---|
159 | */ |
---|
160 | uint8_t * peer_id; |
---|
161 | |
---|
162 | /* Where the files will be when it's complete */ |
---|
163 | char * downloadDir; |
---|
164 | |
---|
165 | /* Where the files are when the torrent is incomplete */ |
---|
166 | char * incompleteDir; |
---|
167 | |
---|
168 | /* Length, in bytes, of the "info" dict in the .torrent file */ |
---|
169 | int infoDictLength; |
---|
170 | |
---|
171 | /* Offset, in bytes, of the beginning of the "info" dict in the .torrent file */ |
---|
172 | int infoDictOffset; |
---|
173 | |
---|
174 | /* Where the files are now. |
---|
175 | * This pointer will be equal to downloadDir or incompleteDir */ |
---|
176 | const char * currentDir; |
---|
177 | |
---|
178 | /* How many bytes we ask for per request */ |
---|
179 | uint32_t blockSize; |
---|
180 | tr_block_index_t blockCount; |
---|
181 | |
---|
182 | uint32_t lastBlockSize; |
---|
183 | uint32_t lastPieceSize; |
---|
184 | |
---|
185 | uint32_t blockCountInPiece; |
---|
186 | uint32_t blockCountInLastPiece; |
---|
187 | |
---|
188 | struct tr_completion completion; |
---|
189 | |
---|
190 | struct tr_bitfield checkedPieces; |
---|
191 | tr_completeness completeness; |
---|
192 | |
---|
193 | struct tr_torrent_tiers * tiers; |
---|
194 | struct tr_publisher_tag * tiersSubscription; |
---|
195 | |
---|
196 | time_t dhtAnnounceAt; |
---|
197 | time_t dhtAnnounce6At; |
---|
198 | tr_bool dhtAnnounceInProgress; |
---|
199 | tr_bool dhtAnnounce6InProgress; |
---|
200 | |
---|
201 | time_t lpdAnnounceAt; |
---|
202 | |
---|
203 | uint64_t downloadedCur; |
---|
204 | uint64_t downloadedPrev; |
---|
205 | uint64_t uploadedCur; |
---|
206 | uint64_t uploadedPrev; |
---|
207 | uint64_t corruptCur; |
---|
208 | uint64_t corruptPrev; |
---|
209 | |
---|
210 | uint64_t etaDLSpeedCalculatedAt; |
---|
211 | double etaDLSpeed; |
---|
212 | uint64_t etaULSpeedCalculatedAt; |
---|
213 | double etaULSpeed; |
---|
214 | |
---|
215 | time_t addedDate; |
---|
216 | time_t activityDate; |
---|
217 | time_t doneDate; |
---|
218 | time_t startDate; |
---|
219 | time_t anyDate; |
---|
220 | |
---|
221 | tr_torrent_metadata_func * metadata_func; |
---|
222 | void * metadata_func_user_data; |
---|
223 | |
---|
224 | tr_torrent_completeness_func * completeness_func; |
---|
225 | void * completeness_func_user_data; |
---|
226 | |
---|
227 | tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func; |
---|
228 | void * ratio_limit_hit_func_user_data; |
---|
229 | |
---|
230 | tr_bool isRunning; |
---|
231 | tr_bool isDeleting; |
---|
232 | tr_bool startAfterVerify; |
---|
233 | tr_bool isDirty; |
---|
234 | |
---|
235 | uint16_t maxConnectedPeers; |
---|
236 | |
---|
237 | tr_verify_state verifyState; |
---|
238 | |
---|
239 | time_t lastStatTime; |
---|
240 | tr_stat stats; |
---|
241 | |
---|
242 | tr_torrent * next; |
---|
243 | |
---|
244 | int uniqueId; |
---|
245 | |
---|
246 | struct tr_bandwidth * bandwidth; |
---|
247 | |
---|
248 | struct tr_torrent_peers * torrentPeers; |
---|
249 | |
---|
250 | double desiredRatio; |
---|
251 | tr_ratiolimit ratioLimitMode; |
---|
252 | |
---|
253 | uint64_t preVerifyTotal; |
---|
254 | }; |
---|
255 | |
---|
256 | /* get the index of this piece's first block */ |
---|
257 | static inline tr_block_index_t |
---|
258 | tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
259 | { |
---|
260 | return piece * tor->blockCountInPiece; |
---|
261 | } |
---|
262 | |
---|
263 | /* what piece index is this block in? */ |
---|
264 | static inline tr_piece_index_t |
---|
265 | tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block ) |
---|
266 | { |
---|
267 | return block / tor->blockCountInPiece; |
---|
268 | } |
---|
269 | |
---|
270 | /* how many blocks are in this piece? */ |
---|
271 | static inline uint32_t |
---|
272 | tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
273 | { |
---|
274 | return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece |
---|
275 | : tor->blockCountInPiece; |
---|
276 | } |
---|
277 | |
---|
278 | /* how many bytes are in this piece? */ |
---|
279 | static inline uint32_t |
---|
280 | tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
281 | { |
---|
282 | return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize |
---|
283 | : tor->info.pieceSize; |
---|
284 | } |
---|
285 | |
---|
286 | /* how many bytes are in this block? */ |
---|
287 | static inline uint32_t |
---|
288 | tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block ) |
---|
289 | { |
---|
290 | return block == tor->blockCount - 1 ? tor->lastBlockSize |
---|
291 | : tor->blockSize; |
---|
292 | } |
---|
293 | |
---|
294 | static inline void tr_torrentLock( const tr_torrent * tor ) |
---|
295 | { |
---|
296 | tr_sessionLock( tor->session ); |
---|
297 | } |
---|
298 | |
---|
299 | static inline void tr_torrentUnlock( const tr_torrent * tor ) |
---|
300 | { |
---|
301 | tr_sessionUnlock( tor->session ); |
---|
302 | } |
---|
303 | |
---|
304 | static inline tr_bool |
---|
305 | tr_torrentExists( const tr_session * session, const uint8_t * torrentHash ) |
---|
306 | { |
---|
307 | return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL; |
---|
308 | } |
---|
309 | |
---|
310 | static inline tr_bool |
---|
311 | tr_torrentIsSeed( const tr_torrent * tor ) |
---|
312 | { |
---|
313 | return tor->completeness != TR_LEECH; |
---|
314 | } |
---|
315 | |
---|
316 | static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor ) |
---|
317 | { |
---|
318 | return ( tor != NULL ) && tor->info.isPrivate; |
---|
319 | } |
---|
320 | |
---|
321 | static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor ) |
---|
322 | { |
---|
323 | return ( tor != NULL ) |
---|
324 | && ( tor->session->isPexEnabled ) |
---|
325 | && ( !tr_torrentIsPrivate( tor ) ); |
---|
326 | } |
---|
327 | |
---|
328 | static inline tr_bool tr_torrentAllowsDHT( const tr_torrent * tor ) |
---|
329 | { |
---|
330 | return ( tor != NULL ) |
---|
331 | && ( tr_sessionAllowsDHT( tor->session ) ) |
---|
332 | && ( !tr_torrentIsPrivate( tor ) ); |
---|
333 | } |
---|
334 | |
---|
335 | static inline tr_bool tr_torrentAllowsLPD( const tr_torrent * tor ) |
---|
336 | { |
---|
337 | return ( tor != NULL ) |
---|
338 | && ( tr_sessionAllowsLPD( tor->session ) ) |
---|
339 | && ( !tr_torrentIsPrivate( tor ) ); |
---|
340 | } |
---|
341 | |
---|
342 | static inline tr_bool tr_torrentIsPieceChecked( const tr_torrent * tor, |
---|
343 | tr_piece_index_t i ) |
---|
344 | { |
---|
345 | return tr_bitfieldHasFast( &tor->checkedPieces, i ); |
---|
346 | } |
---|
347 | |
---|
348 | /*** |
---|
349 | **** |
---|
350 | ***/ |
---|
351 | |
---|
352 | enum |
---|
353 | { |
---|
354 | TORRENT_MAGIC_NUMBER = 95549 |
---|
355 | }; |
---|
356 | |
---|
357 | static inline tr_bool tr_isTorrent( const tr_torrent * tor ) |
---|
358 | { |
---|
359 | return ( tor != NULL ) |
---|
360 | && ( tor->magicNumber == TORRENT_MAGIC_NUMBER ) |
---|
361 | && ( tr_isSession( tor->session ) ); |
---|
362 | } |
---|
363 | |
---|
364 | /* set a flag indicating that the torrent's .resume file |
---|
365 | * needs to be saved when the torrent is closed */ |
---|
366 | static inline |
---|
367 | void tr_torrentSetDirty( tr_torrent * tor ) |
---|
368 | { |
---|
369 | assert( tr_isTorrent( tor ) ); |
---|
370 | |
---|
371 | tor->isDirty = TRUE; |
---|
372 | } |
---|
373 | |
---|
374 | static inline |
---|
375 | const char * tr_torrentName( const tr_torrent * tor ) |
---|
376 | { |
---|
377 | assert( tr_isTorrent( tor ) ); |
---|
378 | |
---|
379 | return tor->info.name; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Tell the tr_torrent that one of its files has become complete |
---|
384 | */ |
---|
385 | void tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNo ); |
---|
386 | |
---|
387 | |
---|
388 | /** |
---|
389 | * @brief Like tr_torrentFindFile(), but splits the filename into base and subpath; |
---|
390 | * |
---|
391 | * If the file is found, "tr_buildPath( base, subpath, NULL )" |
---|
392 | * will generate the complete filename. |
---|
393 | * |
---|
394 | * @return true if the file is found, false otherwise. |
---|
395 | * |
---|
396 | * @param base if the torrent is found, this will be either |
---|
397 | * tor->downloadDir or tor->incompleteDir |
---|
398 | * @param subpath on success, this pointer is assigned a newly-allocated |
---|
399 | * string holding the second half of the filename. |
---|
400 | */ |
---|
401 | tr_bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo, |
---|
402 | const char ** base, char ** subpath ); |
---|
403 | |
---|
404 | |
---|
405 | /* Returns a newly-allocated version of the tr_file.name string |
---|
406 | * that's been modified to denote that it's not a complete file yet. |
---|
407 | * In the current implementation this is done by appending ".part" |
---|
408 | * a la Firefox. */ |
---|
409 | char* tr_torrentBuildPartial( const tr_torrent *, tr_file_index_t fileNo ); |
---|
410 | |
---|
411 | /* for when the info dict has been fundamentally changed wrt files, |
---|
412 | * piece size, etc. such as in BEP 9 where peers exchange metadata */ |
---|
413 | void tr_torrentGotNewInfoDict( tr_torrent * tor ); |
---|
414 | |
---|
415 | #endif |
---|