source: trunk/libtransmission/torrent.h @ 10332

Last change on this file since 10332 was 10332, checked in by charles, 12 years ago

(trunk) #2993 "'Downloaded' much greater than 'Have' or 'verified'" -- add new congestion-based throttle for 2.0

  • Property svn:keywords set to Date Rev Author Id
File size: 13.1 KB
Line 
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 10332 2010-03-08 04:29:58Z 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 "history.h" /* tr_recentHistory */
22#include "session.h" /* tr_sessionLock(), tr_sessionUnlock() */
23#include "utils.h" /* TR_GNUC_PRINTF */
24
25struct tr_bandwidth;
26struct tr_torrent_tiers;
27struct tr_magnet_info;
28
29/**
30***  Package-visible ctor API
31**/
32
33void        tr_ctorSetSave( tr_ctor * ctor,
34                            tr_bool   saveMetadataInOurTorrentsDir );
35
36int         tr_ctorGetSave( const tr_ctor * ctor );
37
38void        tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor );
39
40void        tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor );
41
42/**
43***
44**/
45
46/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
47void        tr_torrentInitFileDLs( tr_torrent *      tor,
48                                   tr_file_index_t * files,
49                                   tr_file_index_t   fileCount,
50                                   tr_bool           do_download );
51
52void        tr_torrentRecheckCompleteness( tr_torrent * );
53
54void        tr_torrentSetHasPiece( tr_torrent *     tor,
55                                   tr_piece_index_t pieceIndex,
56                                   tr_bool          has );
57
58void        tr_torrentChangeMyPort( tr_torrent * session );
59
60tr_torrent* tr_torrentFindFromHashString( tr_session * session,
61                                          const char * hashString );
62
63tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
64                                              const uint8_t * hash );
65
66tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
67                                              tr_direction       direction );
68
69
70
71#define tr_block( a, b ) _tr_block( tor, a, b )
72tr_block_index_t _tr_block( const tr_torrent * tor,
73                            tr_piece_index_t   index,
74                            uint32_t           offset );
75
76tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
77                                       tr_piece_index_t   index,
78                                       uint32_t           offset,
79                                       uint32_t           length );
80
81uint64_t         tr_pieceOffset( const tr_torrent * tor,
82                                 tr_piece_index_t   index,
83                                 uint32_t           offset,
84                                 uint32_t           length );
85
86void             tr_torrentInitFilePriority( tr_torrent       * tor,
87                                             tr_file_index_t    fileIndex,
88                                             tr_priority_t      priority );
89
90int              tr_torrentCountUncheckedPieces( const tr_torrent * );
91
92tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
93                                          tr_file_index_t     file );
94
95void             tr_torrentSetPieceChecked( tr_torrent       * tor,
96                                            tr_piece_index_t   piece,
97                                            tr_bool            isChecked );
98
99void             tr_torrentSetFileChecked( tr_torrent       * tor,
100                                           tr_file_index_t    file,
101                                           tr_bool            isChecked );
102
103void             tr_torrentUncheck( tr_torrent * tor );
104
105int              tr_torrentPromoteTracker( tr_torrent   * tor,
106                                           int            trackerIndex );
107
108time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
109                                      size_t            * setmeCount );
110
111tr_torrent*      tr_torrentNext( tr_session  * session,
112                                 tr_torrent  * current );
113
114void             tr_torrentCheckSeedRatio( tr_torrent * tor );
115
116/** save a torrent's .resume file if it's changed since the last time it was saved */
117void             tr_torrentSave( tr_torrent * tor );
118
119void             tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 );
120
121
122
123typedef enum
124{
125    TR_VERIFY_NONE,
126    TR_VERIFY_WAIT,
127    TR_VERIFY_NOW
128}
129tr_verify_state;
130
131void             tr_torrentSetVerifyState( tr_torrent      * tor,
132                                           tr_verify_state   state );
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
147    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
148
149    /* Used when the torrent has been created with a magnet link
150     * and we're in the process of downloading the metainfo from
151     * other peers */
152    struct tr_incomplete_metadata  * incompleteMetadata;
153
154    /* If the initiator of the connection receives a handshake in which the
155     * peer_id does not match the expected peerid, then the initiator is
156     * expected to drop the connection. Note that the initiator presumably
157     * received the peer information from the tracker, which includes the
158     * peer_id that was registered by the peer. The peer_id from the tracker
159     * and in the handshake are expected to match.
160     */
161    uint8_t * peer_id;
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    int infoDictOffset;
174
175    /* Where the files are now.
176     * This pointer will be equal to downloadDir or incompleteDir */
177    const char * currentDir;
178
179    tr_recentHistory       * blocksSentToClient;
180    tr_recentHistory       * blocksSentToPeer;
181
182    tr_recentHistory       * cancelsSentToClient;
183    tr_recentHistory       * cancelsSentToPeer;
184
185    /* How many bytes we ask for per request */
186    uint32_t                   blockSize;
187    tr_block_index_t           blockCount;
188
189    uint32_t                   lastBlockSize;
190    uint32_t                   lastPieceSize;
191
192    uint32_t                   blockCountInPiece;
193    uint32_t                   blockCountInLastPiece;
194
195    struct tr_completion       completion;
196
197    struct tr_bitfield         checkedPieces;
198    tr_completeness            completeness;
199
200    struct tr_torrent_tiers  * tiers;
201    struct tr_publisher_tag  * tiersSubscription;
202
203    time_t                     dhtAnnounceAt;
204    time_t                     dhtAnnounce6At;
205    tr_bool                    dhtAnnounceInProgress;
206    tr_bool                    dhtAnnounce6InProgress;
207
208    uint64_t                   downloadedCur;
209    uint64_t                   downloadedPrev;
210    uint64_t                   uploadedCur;
211    uint64_t                   uploadedPrev;
212    uint64_t                   corruptCur;
213    uint64_t                   corruptPrev;
214
215    uint64_t                   etaDLSpeedCalculatedAt;
216    double                     etaDLSpeed;
217    uint64_t                   etaULSpeedCalculatedAt;
218    double                     etaULSpeed;
219
220    time_t                     addedDate;
221    time_t                     activityDate;
222    time_t                     doneDate;
223    time_t                     startDate;
224    time_t                     anyDate;
225
226    tr_torrent_metadata_func  * metadata_func;
227    void                      * metadata_func_user_data;
228
229    tr_torrent_completeness_func  * completeness_func;
230    void                          *  completeness_func_user_data;
231
232    tr_torrent_ratio_limit_hit_func  * ratio_limit_hit_func;
233    void                             * ratio_limit_hit_func_user_data;
234
235    tr_bool                    isRunning;
236    tr_bool                    isDeleting;
237    tr_bool                    needsSeedRatioCheck;
238    tr_bool                    startAfterVerify;
239    tr_bool                    isDirty;
240
241    uint16_t                   maxConnectedPeers;
242
243    tr_verify_state            verifyState;
244
245    time_t                     lastStatTime;
246    tr_stat                    stats;
247
248    tr_torrent *               next;
249
250    int                        uniqueId;
251
252    struct tr_bandwidth      * bandwidth;
253
254    struct tr_torrent_peers  * torrentPeers;
255
256    double                     desiredRatio;
257    tr_ratiolimit              ratioLimitMode;
258
259    uint64_t                   preVerifyTotal;
260};
261
262/* get the index of this piece's first block */
263static inline tr_block_index_t
264tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
265{
266    return piece * tor->blockCountInPiece;
267}
268
269/* what piece index is this block in? */
270static inline tr_piece_index_t
271tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
272{
273    return block / tor->blockCountInPiece;
274}
275
276/* how many blocks are in this piece? */
277static inline uint32_t
278tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
279{
280    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
281                                             : tor->blockCountInPiece;
282}
283
284/* how many bytes are in this piece? */
285static inline uint32_t
286tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
287{
288    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
289                                             : tor->info.pieceSize;
290}
291
292/* how many bytes are in this block? */
293static inline uint32_t
294tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
295{
296    return block == tor->blockCount - 1 ? tor->lastBlockSize
297                                        : tor->blockSize;
298}
299
300static inline void tr_torrentLock( const tr_torrent * tor )
301{
302    tr_sessionLock( tor->session );
303}
304
305static inline void tr_torrentUnlock( const tr_torrent * tor )
306{
307    tr_sessionUnlock( tor->session );
308}
309
310static inline tr_bool
311tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
312{
313    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
314}
315
316static inline tr_bool
317tr_torrentIsSeed( const tr_torrent * tor )
318{
319    return tor->completeness != TR_LEECH;
320}
321
322static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
323{
324    return ( tor != NULL ) && tor->info.isPrivate;
325}
326
327static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
328{
329    return ( tor != NULL )
330        && ( tor->session->isPexEnabled )
331        && ( !tr_torrentIsPrivate( tor ) );
332}
333
334static inline tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
335{
336    return ( tor != NULL )
337        && ( tr_sessionAllowsDHT( tor->session ) )
338        && ( !tr_torrentIsPrivate( tor ) );
339}
340
341static inline tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor,
342                                                   tr_piece_index_t    i )
343{
344    return tr_bitfieldHasFast( &tor->checkedPieces, i );
345}
346
347/***
348****
349***/
350
351enum
352{
353    TORRENT_DOWNLOAD_CONGESTION_HISTORY_SEC = 120,
354
355    TORRENT_MAGIC_NUMBER = 95549
356};
357
358static inline tr_bool tr_isTorrent( const tr_torrent * tor )
359{
360    return ( tor != NULL )
361        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
362        && ( tr_isSession( tor->session ) );
363}
364
365/* set a flag indicating that the torrent's .resume file
366 * needs to be saved when the torrent is closed */
367static inline
368void tr_torrentSetDirty( tr_torrent * tor )
369{
370    assert( tr_isTorrent( tor ) );
371
372    tor->isDirty = TRUE;
373}
374
375static inline
376const char * tr_torrentName( const tr_torrent * tor )
377{
378    assert( tr_isTorrent( tor ) );
379
380    return tor->info.name;
381}
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 */
402tr_bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo,
403                             const char ** base, char ** subpath );
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
416#endif
Note: See TracBrowser for help on using the repository browser.