source: trunk/libtransmission/torrent.h @ 12615

Last change on this file since 12615 was 12615, checked in by jordan, 10 years ago

Add a callback to be invoked when the queue starts a torrent.

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