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