source: trunk/libtransmission/torrent.h @ 13670

Last change on this file since 13670 was 13670, checked in by jordan, 9 years ago

(trunk, libT) #5168 'make libtransmission's public funcs nonblocking when possible' -- remove tr_torrentRef() and tr_torrentUnref() as discussed in https://trac.transmissionbt.com/ticket/5168#comment:8

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