1 | /* |
---|
2 | * This file Copyright (C) 2009-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: torrent.h 14532 2015-05-31 22:13:31Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef __TRANSMISSION__ |
---|
11 | #error only libtransmission should #include this header. |
---|
12 | #endif |
---|
13 | |
---|
14 | #ifndef TR_TORRENT_H |
---|
15 | #define TR_TORRENT_H 1 |
---|
16 | |
---|
17 | #include "bandwidth.h" /* tr_bandwidth */ |
---|
18 | #include "completion.h" /* tr_completion */ |
---|
19 | #include "session.h" /* tr_sessionLock (), tr_sessionUnlock () */ |
---|
20 | #include "utils.h" /* TR_GNUC_PRINTF */ |
---|
21 | |
---|
22 | struct tr_torrent_tiers; |
---|
23 | struct tr_magnet_info; |
---|
24 | |
---|
25 | /** |
---|
26 | *** Package-visible ctor API |
---|
27 | **/ |
---|
28 | |
---|
29 | void tr_torrentFree (tr_torrent * tor); |
---|
30 | |
---|
31 | void tr_ctorSetSave (tr_ctor * ctor, |
---|
32 | bool saveMetadataInOurTorrentsDir); |
---|
33 | |
---|
34 | bool tr_ctorGetSave (const tr_ctor * ctor); |
---|
35 | |
---|
36 | void tr_ctorInitTorrentPriorities (const tr_ctor * ctor, tr_torrent * tor); |
---|
37 | |
---|
38 | void tr_ctorInitTorrentWanted (const tr_ctor * ctor, tr_torrent * tor); |
---|
39 | |
---|
40 | /** |
---|
41 | *** |
---|
42 | **/ |
---|
43 | |
---|
44 | /* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */ |
---|
45 | void tr_torrentInitFileDLs (tr_torrent * tor, |
---|
46 | const tr_file_index_t * files, |
---|
47 | tr_file_index_t fileCount, |
---|
48 | bool do_download); |
---|
49 | |
---|
50 | void tr_torrentRecheckCompleteness (tr_torrent *); |
---|
51 | |
---|
52 | void tr_torrentSetHasPiece (tr_torrent * tor, |
---|
53 | tr_piece_index_t pieceIndex, |
---|
54 | bool has); |
---|
55 | |
---|
56 | void tr_torrentChangeMyPort (tr_torrent * session); |
---|
57 | |
---|
58 | tr_torrent* tr_torrentFindFromHashString (tr_session * session, |
---|
59 | const char * hashString); |
---|
60 | |
---|
61 | tr_torrent* tr_torrentFindFromObfuscatedHash (tr_session * session, |
---|
62 | const uint8_t * hash); |
---|
63 | |
---|
64 | bool tr_torrentIsPieceTransferAllowed (const tr_torrent * torrent, |
---|
65 | tr_direction direction); |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | #define tr_block(a, b) _tr_block (tor, a, b) |
---|
70 | tr_block_index_t _tr_block (const tr_torrent * tor, |
---|
71 | tr_piece_index_t index, |
---|
72 | uint32_t offset); |
---|
73 | |
---|
74 | bool tr_torrentReqIsValid (const tr_torrent * tor, |
---|
75 | tr_piece_index_t index, |
---|
76 | uint32_t offset, |
---|
77 | uint32_t length); |
---|
78 | |
---|
79 | uint64_t tr_pieceOffset (const tr_torrent * tor, |
---|
80 | tr_piece_index_t index, |
---|
81 | uint32_t offset, |
---|
82 | uint32_t length); |
---|
83 | |
---|
84 | void tr_torrentGetBlockLocation (const tr_torrent * tor, |
---|
85 | tr_block_index_t block, |
---|
86 | tr_piece_index_t * piece, |
---|
87 | uint32_t * offset, |
---|
88 | uint32_t * length); |
---|
89 | |
---|
90 | void tr_torGetFileBlockRange (const tr_torrent * tor, |
---|
91 | const tr_file_index_t file, |
---|
92 | tr_block_index_t * first, |
---|
93 | tr_block_index_t * last); |
---|
94 | |
---|
95 | void tr_torGetPieceBlockRange (const tr_torrent * tor, |
---|
96 | const tr_piece_index_t piece, |
---|
97 | tr_block_index_t * first, |
---|
98 | tr_block_index_t * last); |
---|
99 | |
---|
100 | void tr_torrentInitFilePriority (tr_torrent * tor, |
---|
101 | tr_file_index_t fileIndex, |
---|
102 | tr_priority_t priority); |
---|
103 | |
---|
104 | void tr_torrentSetPieceChecked (tr_torrent * tor, |
---|
105 | tr_piece_index_t piece); |
---|
106 | |
---|
107 | void tr_torrentSetChecked (tr_torrent * tor, time_t when); |
---|
108 | |
---|
109 | void tr_torrentCheckSeedLimit (tr_torrent * tor); |
---|
110 | |
---|
111 | /** save a torrent's .resume file if it's changed since the last time it was saved */ |
---|
112 | void tr_torrentSave (tr_torrent * tor); |
---|
113 | |
---|
114 | void tr_torrentSetLocalError (tr_torrent * tor, const char * fmt, ...) TR_GNUC_PRINTF (2, 3); |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | typedef enum |
---|
119 | { |
---|
120 | TR_VERIFY_NONE, |
---|
121 | TR_VERIFY_WAIT, |
---|
122 | TR_VERIFY_NOW |
---|
123 | } |
---|
124 | tr_verify_state; |
---|
125 | |
---|
126 | void tr_torrentSetVerifyState (tr_torrent * tor, |
---|
127 | tr_verify_state state); |
---|
128 | |
---|
129 | tr_torrent_activity tr_torrentGetActivity (const tr_torrent * tor); |
---|
130 | |
---|
131 | struct tr_incomplete_metadata; |
---|
132 | |
---|
133 | /** @brief Torrent object */ |
---|
134 | struct tr_torrent |
---|
135 | { |
---|
136 | tr_session * session; |
---|
137 | tr_info info; |
---|
138 | |
---|
139 | int magicNumber; |
---|
140 | |
---|
141 | tr_stat_errtype error; |
---|
142 | char errorString[128]; |
---|
143 | char errorTracker[128]; |
---|
144 | |
---|
145 | uint8_t obfuscatedHash[SHA_DIGEST_LENGTH]; |
---|
146 | |
---|
147 | /* Used when the torrent has been created with a magnet link |
---|
148 | * and we're in the process of downloading the metainfo from |
---|
149 | * other peers */ |
---|
150 | struct tr_incomplete_metadata * incompleteMetadata; |
---|
151 | |
---|
152 | /* If the initiator of the connection receives a handshake in which the |
---|
153 | * peer_id does not match the expected peerid, then the initiator is |
---|
154 | * expected to drop the connection. Note that the initiator presumably |
---|
155 | * received the peer information from the tracker, which includes the |
---|
156 | * peer_id that was registered by the peer. The peer_id from the tracker |
---|
157 | * and in the handshake are expected to match. |
---|
158 | */ |
---|
159 | unsigned char peer_id[PEER_ID_LEN+1]; |
---|
160 | |
---|
161 | time_t peer_id_creation_time; |
---|
162 | |
---|
163 | /* Where the files will be when it's complete */ |
---|
164 | char * downloadDir; |
---|
165 | |
---|
166 | /* Where the files are when the torrent is incomplete */ |
---|
167 | char * incompleteDir; |
---|
168 | |
---|
169 | /* Length, in bytes, of the "info" dict in the .torrent file. */ |
---|
170 | int infoDictLength; |
---|
171 | |
---|
172 | /* Offset, in bytes, of the beginning of the "info" dict in the .torrent file. |
---|
173 | * |
---|
174 | * Used by the torrent-magnet code for serving metainfo to peers. |
---|
175 | * This field is lazy-generated and might not be initialized yet. */ |
---|
176 | int infoDictOffset; |
---|
177 | |
---|
178 | /* Where the files are now. |
---|
179 | * This pointer will be equal to downloadDir or incompleteDir */ |
---|
180 | const char * currentDir; |
---|
181 | |
---|
182 | /* How many bytes we ask for per request */ |
---|
183 | uint32_t blockSize; |
---|
184 | tr_block_index_t blockCount; |
---|
185 | |
---|
186 | uint32_t lastBlockSize; |
---|
187 | uint32_t lastPieceSize; |
---|
188 | |
---|
189 | uint16_t blockCountInPiece; |
---|
190 | uint16_t blockCountInLastPiece; |
---|
191 | |
---|
192 | struct tr_completion completion; |
---|
193 | |
---|
194 | tr_completeness completeness; |
---|
195 | |
---|
196 | struct tr_torrent_tiers * tiers; |
---|
197 | |
---|
198 | time_t dhtAnnounceAt; |
---|
199 | time_t dhtAnnounce6At; |
---|
200 | bool dhtAnnounceInProgress; |
---|
201 | bool dhtAnnounce6InProgress; |
---|
202 | |
---|
203 | time_t lpdAnnounceAt; |
---|
204 | |
---|
205 | uint64_t downloadedCur; |
---|
206 | uint64_t downloadedPrev; |
---|
207 | uint64_t uploadedCur; |
---|
208 | uint64_t uploadedPrev; |
---|
209 | uint64_t corruptCur; |
---|
210 | uint64_t corruptPrev; |
---|
211 | |
---|
212 | uint64_t etaDLSpeedCalculatedAt; |
---|
213 | unsigned int etaDLSpeed_Bps; |
---|
214 | uint64_t etaULSpeedCalculatedAt; |
---|
215 | unsigned int etaULSpeed_Bps; |
---|
216 | |
---|
217 | time_t addedDate; |
---|
218 | time_t activityDate; |
---|
219 | time_t doneDate; |
---|
220 | time_t startDate; |
---|
221 | time_t anyDate; |
---|
222 | |
---|
223 | int secondsDownloading; |
---|
224 | int secondsSeeding; |
---|
225 | |
---|
226 | int queuePosition; |
---|
227 | |
---|
228 | tr_torrent_metadata_func metadata_func; |
---|
229 | void * metadata_func_user_data; |
---|
230 | |
---|
231 | tr_torrent_completeness_func completeness_func; |
---|
232 | void * completeness_func_user_data; |
---|
233 | |
---|
234 | tr_torrent_ratio_limit_hit_func ratio_limit_hit_func; |
---|
235 | void * ratio_limit_hit_func_user_data; |
---|
236 | |
---|
237 | tr_torrent_idle_limit_hit_func idle_limit_hit_func; |
---|
238 | void * idle_limit_hit_func_user_data; |
---|
239 | |
---|
240 | void * queue_started_user_data; |
---|
241 | void (* queue_started_callback)(tr_torrent *, void * queue_started_user_data); |
---|
242 | |
---|
243 | bool isRunning; |
---|
244 | bool isStopping; |
---|
245 | bool isDeleting; |
---|
246 | bool startAfterVerify; |
---|
247 | bool isDirty; |
---|
248 | bool isQueued; |
---|
249 | |
---|
250 | bool magnetVerify; |
---|
251 | |
---|
252 | bool infoDictOffsetIsCached; |
---|
253 | |
---|
254 | uint16_t maxConnectedPeers; |
---|
255 | |
---|
256 | tr_verify_state verifyState; |
---|
257 | |
---|
258 | time_t lastStatTime; |
---|
259 | tr_stat stats; |
---|
260 | |
---|
261 | tr_torrent * next; |
---|
262 | |
---|
263 | int uniqueId; |
---|
264 | |
---|
265 | struct tr_bandwidth bandwidth; |
---|
266 | |
---|
267 | struct tr_swarm * swarm; |
---|
268 | |
---|
269 | float desiredRatio; |
---|
270 | tr_ratiolimit ratioLimitMode; |
---|
271 | |
---|
272 | uint16_t idleLimitMinutes; |
---|
273 | tr_idlelimit idleLimitMode; |
---|
274 | bool finishedSeedingByIdle; |
---|
275 | }; |
---|
276 | |
---|
277 | static inline tr_torrent* |
---|
278 | tr_torrentNext (tr_session * session, tr_torrent * current) |
---|
279 | { |
---|
280 | return current ? current->next : session->torrentList; |
---|
281 | } |
---|
282 | |
---|
283 | /* what piece index is this block in? */ |
---|
284 | static inline tr_piece_index_t |
---|
285 | tr_torBlockPiece (const tr_torrent * tor, const tr_block_index_t block) |
---|
286 | { |
---|
287 | return block / tor->blockCountInPiece; |
---|
288 | } |
---|
289 | |
---|
290 | /* how many bytes are in this piece? */ |
---|
291 | static inline uint32_t |
---|
292 | tr_torPieceCountBytes (const tr_torrent * tor, const tr_piece_index_t piece) |
---|
293 | { |
---|
294 | return piece + 1 == tor->info.pieceCount ? tor->lastPieceSize |
---|
295 | : tor->info.pieceSize; |
---|
296 | } |
---|
297 | |
---|
298 | /* how many bytes are in this block? */ |
---|
299 | static inline uint32_t |
---|
300 | tr_torBlockCountBytes (const tr_torrent * tor, const tr_block_index_t block) |
---|
301 | { |
---|
302 | return block + 1 == tor->blockCount ? tor->lastBlockSize |
---|
303 | : tor->blockSize; |
---|
304 | } |
---|
305 | |
---|
306 | static inline void tr_torrentLock (const tr_torrent * tor) |
---|
307 | { |
---|
308 | tr_sessionLock (tor->session); |
---|
309 | } |
---|
310 | static inline bool tr_torrentIsLocked (const tr_torrent * tor) |
---|
311 | { |
---|
312 | return tr_sessionIsLocked (tor->session); |
---|
313 | } |
---|
314 | static inline void tr_torrentUnlock (const tr_torrent * tor) |
---|
315 | { |
---|
316 | tr_sessionUnlock (tor->session); |
---|
317 | } |
---|
318 | |
---|
319 | static inline bool |
---|
320 | tr_torrentExists (const tr_session * session, const uint8_t * torrentHash) |
---|
321 | { |
---|
322 | return tr_torrentFindFromHash ((tr_session*)session, torrentHash) != NULL; |
---|
323 | } |
---|
324 | |
---|
325 | static inline tr_completeness |
---|
326 | tr_torrentGetCompleteness (const tr_torrent * tor) |
---|
327 | { |
---|
328 | return tor->completeness; |
---|
329 | } |
---|
330 | |
---|
331 | static inline bool |
---|
332 | tr_torrentIsSeed (const tr_torrent * tor) |
---|
333 | { |
---|
334 | return tr_torrentGetCompleteness(tor) != TR_LEECH; |
---|
335 | } |
---|
336 | |
---|
337 | static inline bool tr_torrentIsPrivate (const tr_torrent * tor) |
---|
338 | { |
---|
339 | return (tor != NULL) && tor->info.isPrivate; |
---|
340 | } |
---|
341 | |
---|
342 | static inline bool tr_torrentAllowsPex (const tr_torrent * tor) |
---|
343 | { |
---|
344 | return (tor != NULL) |
---|
345 | && (tor->session->isPexEnabled) |
---|
346 | && (!tr_torrentIsPrivate (tor)); |
---|
347 | } |
---|
348 | |
---|
349 | static inline bool tr_torrentAllowsDHT (const tr_torrent * tor) |
---|
350 | { |
---|
351 | return (tor != NULL) |
---|
352 | && (tr_sessionAllowsDHT (tor->session)) |
---|
353 | && (!tr_torrentIsPrivate (tor)); |
---|
354 | } |
---|
355 | |
---|
356 | static inline bool tr_torrentAllowsLPD (const tr_torrent * tor) |
---|
357 | { |
---|
358 | return (tor != NULL) |
---|
359 | && (tr_sessionAllowsLPD (tor->session)) |
---|
360 | && (!tr_torrentIsPrivate (tor)); |
---|
361 | } |
---|
362 | |
---|
363 | /*** |
---|
364 | **** |
---|
365 | ***/ |
---|
366 | |
---|
367 | enum |
---|
368 | { |
---|
369 | TORRENT_MAGIC_NUMBER = 95549 |
---|
370 | }; |
---|
371 | |
---|
372 | static inline bool tr_isTorrent (const tr_torrent * tor) |
---|
373 | { |
---|
374 | return (tor != NULL) |
---|
375 | && (tor->magicNumber == TORRENT_MAGIC_NUMBER) |
---|
376 | && (tr_isSession (tor->session)); |
---|
377 | } |
---|
378 | |
---|
379 | /* set a flag indicating that the torrent's .resume file |
---|
380 | * needs to be saved when the torrent is closed */ |
---|
381 | static inline |
---|
382 | void tr_torrentSetDirty (tr_torrent * tor) |
---|
383 | { |
---|
384 | assert (tr_isTorrent (tor)); |
---|
385 | |
---|
386 | tor->isDirty = true; |
---|
387 | } |
---|
388 | |
---|
389 | uint32_t tr_getBlockSize (uint32_t pieceSize); |
---|
390 | |
---|
391 | /** |
---|
392 | * Tell the tr_torrent that it's gotten a block |
---|
393 | */ |
---|
394 | void tr_torrentGotBlock (tr_torrent * tor, tr_block_index_t blockIndex); |
---|
395 | |
---|
396 | |
---|
397 | |
---|
398 | /** |
---|
399 | * @brief Like tr_torrentFindFile (), but splits the filename into base and subpath; |
---|
400 | * |
---|
401 | * If the file is found, "tr_buildPath (base, subpath, NULL)" |
---|
402 | * will generate the complete filename. |
---|
403 | * |
---|
404 | * @return true if the file is found, false otherwise. |
---|
405 | * |
---|
406 | * @param base if the torrent is found, this will be either |
---|
407 | * tor->downloadDir or tor->incompleteDir |
---|
408 | * @param subpath on success, this pointer is assigned a newly-allocated |
---|
409 | * string holding the second half of the filename. |
---|
410 | */ |
---|
411 | bool tr_torrentFindFile2 (const tr_torrent *, tr_file_index_t fileNo, |
---|
412 | const char ** base, char ** subpath, time_t * mtime); |
---|
413 | |
---|
414 | |
---|
415 | /* Returns a newly-allocated version of the tr_file.name string |
---|
416 | * that's been modified to denote that it's not a complete file yet. |
---|
417 | * In the current implementation this is done by appending ".part" |
---|
418 | * a la Firefox. */ |
---|
419 | char* tr_torrentBuildPartial (const tr_torrent *, tr_file_index_t fileNo); |
---|
420 | |
---|
421 | /* for when the info dict has been fundamentally changed wrt files, |
---|
422 | * piece size, etc. such as in BEP 9 where peers exchange metadata */ |
---|
423 | void tr_torrentGotNewInfoDict (tr_torrent * tor); |
---|
424 | |
---|
425 | void tr_torrentSetSpeedLimit_Bps (tr_torrent *, tr_direction, unsigned int Bps); |
---|
426 | unsigned int tr_torrentGetSpeedLimit_Bps (const tr_torrent *, tr_direction); |
---|
427 | |
---|
428 | /** |
---|
429 | * @return true if this piece needs to be tested |
---|
430 | */ |
---|
431 | bool tr_torrentPieceNeedsCheck (const tr_torrent * tor, tr_piece_index_t pieceIndex); |
---|
432 | |
---|
433 | /** |
---|
434 | * @brief Test a piece against its info dict checksum |
---|
435 | * @return true if the piece's passes the checksum test |
---|
436 | */ |
---|
437 | bool tr_torrentCheckPiece (tr_torrent * tor, tr_piece_index_t pieceIndex); |
---|
438 | |
---|
439 | time_t tr_torrentGetFileMTime (const tr_torrent * tor, tr_file_index_t i); |
---|
440 | |
---|
441 | uint64_t tr_torrentGetCurrentSizeOnDisk (const tr_torrent * tor); |
---|
442 | |
---|
443 | bool tr_torrentIsStalled (const tr_torrent * tor); |
---|
444 | |
---|
445 | const unsigned char * tr_torrentGetPeerId (tr_torrent * tor); |
---|
446 | |
---|
447 | static inline uint64_t |
---|
448 | tr_torrentGetLeftUntilDone (const tr_torrent * tor) |
---|
449 | { |
---|
450 | return tr_cpLeftUntilDone (&tor->completion); |
---|
451 | } |
---|
452 | |
---|
453 | static inline bool |
---|
454 | tr_torrentHasAll (const tr_torrent * tor) |
---|
455 | { |
---|
456 | return tr_cpHasAll (&tor->completion); |
---|
457 | } |
---|
458 | |
---|
459 | static inline bool |
---|
460 | tr_torrentHasNone (const tr_torrent * tor) |
---|
461 | { |
---|
462 | return tr_cpHasNone (&tor->completion); |
---|
463 | } |
---|
464 | |
---|
465 | static inline bool |
---|
466 | tr_torrentPieceIsComplete (const tr_torrent * tor, tr_piece_index_t i) |
---|
467 | { |
---|
468 | return tr_cpPieceIsComplete (&tor->completion, i); |
---|
469 | } |
---|
470 | |
---|
471 | static inline bool |
---|
472 | tr_torrentBlockIsComplete (const tr_torrent * tor, tr_block_index_t i) |
---|
473 | { |
---|
474 | return tr_cpBlockIsComplete (&tor->completion, i); |
---|
475 | } |
---|
476 | |
---|
477 | static inline size_t |
---|
478 | tr_torrentMissingBlocksInPiece (const tr_torrent * tor, tr_piece_index_t i) |
---|
479 | { |
---|
480 | return tr_cpMissingBlocksInPiece (&tor->completion, i); |
---|
481 | } |
---|
482 | |
---|
483 | static inline size_t |
---|
484 | tr_torrentMissingBytesInPiece (const tr_torrent * tor, tr_piece_index_t i) |
---|
485 | { |
---|
486 | return tr_cpMissingBytesInPiece (&tor->completion, i); |
---|
487 | } |
---|
488 | |
---|
489 | static inline void * |
---|
490 | tr_torrentCreatePieceBitfield (const tr_torrent * tor, size_t * byte_count) |
---|
491 | { |
---|
492 | return tr_cpCreatePieceBitfield (&tor->completion, byte_count); |
---|
493 | } |
---|
494 | |
---|
495 | static inline uint64_t |
---|
496 | tr_torrentHaveTotal (const tr_torrent * tor) |
---|
497 | { |
---|
498 | return tr_cpHaveTotal (&tor->completion); |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | static inline bool |
---|
503 | tr_torrentIsQueued (const tr_torrent * tor) |
---|
504 | { |
---|
505 | return tor->isQueued; |
---|
506 | } |
---|
507 | |
---|
508 | static inline tr_direction |
---|
509 | tr_torrentGetQueueDirection (const tr_torrent * tor) |
---|
510 | { |
---|
511 | return tr_torrentIsSeed (tor) ? TR_UP : TR_DOWN; |
---|
512 | } |
---|
513 | |
---|
514 | #endif |
---|