source: trunk/libtransmission/torrent.h @ 10635

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

(trunk) #3060 "Local Peer Discovery" -- in the code, rename LDS as LPD for Local Peer Discovery

  • 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 10635 2010-05-08 08:42:45Z 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 "session.h" /* tr_sessionLock(), tr_sessionUnlock() */
22#include "utils.h" /* TR_GNUC_PRINTF */
23
24struct tr_bandwidth;
25struct tr_torrent_tiers;
26struct tr_magnet_info;
27
28/**
29***  Package-visible ctor API
30**/
31
32void        tr_ctorSetSave( tr_ctor * ctor,
33                            tr_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                                   tr_file_index_t * files,
48                                   tr_file_index_t   fileCount,
49                                   tr_bool           do_download );
50
51void        tr_torrentRecheckCompleteness( tr_torrent * );
52
53void        tr_torrentSetHasPiece( tr_torrent *     tor,
54                                   tr_piece_index_t pieceIndex,
55                                   tr_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
65tr_bool     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
75tr_bool          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_torrentInitFilePriority( tr_torrent       * tor,
86                                             tr_file_index_t    fileIndex,
87                                             tr_priority_t      priority );
88
89int              tr_torrentCountUncheckedPieces( const tr_torrent * );
90
91tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
92                                          tr_file_index_t     file );
93
94void             tr_torrentSetPieceChecked( tr_torrent       * tor,
95                                            tr_piece_index_t   piece,
96                                            tr_bool            isChecked );
97
98void             tr_torrentSetFileChecked( tr_torrent       * tor,
99                                           tr_file_index_t    file,
100                                           tr_bool            isChecked );
101
102void             tr_torrentUncheck( tr_torrent * tor );
103
104int              tr_torrentPromoteTracker( tr_torrent   * tor,
105                                           int            trackerIndex );
106
107time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
108                                      size_t            * setmeCount );
109
110tr_torrent*      tr_torrentNext( tr_session  * session,
111                                 tr_torrent  * current );
112
113void             tr_torrentCheckSeedRatio( tr_torrent * tor );
114
115/** save a torrent's .resume file if it's changed since the last time it was saved */
116void             tr_torrentSave( tr_torrent * tor );
117
118void             tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 );
119
120
121
122typedef enum
123{
124    TR_VERIFY_NONE,
125    TR_VERIFY_WAIT,
126    TR_VERIFY_NOW
127}
128tr_verify_state;
129
130void             tr_torrentSetVerifyState( tr_torrent      * tor,
131                                           tr_verify_state   state );
132
133tr_torrent_activity tr_torrentGetActivity( tr_torrent * tor );
134
135struct tr_incomplete_metadata;
136
137/** @brief Torrent object */
138struct tr_torrent
139{
140    tr_session *             session;
141    tr_info                  info;
142
143    int                      magicNumber;
144
145    tr_stat_errtype          error;
146    char                     errorString[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;
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    int infoDictOffset;
175
176    /* Where the files are now.
177     * This pointer will be equal to downloadDir or incompleteDir */
178    const char * currentDir;
179
180    /* How many bytes we ask for per request */
181    uint32_t                   blockSize;
182    tr_block_index_t           blockCount;
183
184    uint32_t                   lastBlockSize;
185    uint32_t                   lastPieceSize;
186
187    uint32_t                   blockCountInPiece;
188    uint32_t                   blockCountInLastPiece;
189
190    struct tr_completion       completion;
191
192    struct tr_bitfield         checkedPieces;
193    tr_completeness            completeness;
194
195    struct tr_torrent_tiers  * tiers;
196    struct tr_publisher_tag  * tiersSubscription;
197
198    time_t                     dhtAnnounceAt;
199    time_t                     dhtAnnounce6At;
200    tr_bool                    dhtAnnounceInProgress;
201    tr_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    double                     etaDLSpeed;
214    uint64_t                   etaULSpeedCalculatedAt;
215    double                     etaULSpeed;
216
217    time_t                     addedDate;
218    time_t                     activityDate;
219    time_t                     doneDate;
220    time_t                     startDate;
221    time_t                     anyDate;
222
223    tr_torrent_metadata_func  * metadata_func;
224    void                      * metadata_func_user_data;
225
226    tr_torrent_completeness_func  * completeness_func;
227    void                          *  completeness_func_user_data;
228
229    tr_torrent_ratio_limit_hit_func  * ratio_limit_hit_func;
230    void                             * ratio_limit_hit_func_user_data;
231
232    tr_bool                    isRunning;
233    tr_bool                    isDeleting;
234    tr_bool                    startAfterVerify;
235    tr_bool                    isDirty;
236
237    uint16_t                   maxConnectedPeers;
238
239    tr_verify_state            verifyState;
240
241    time_t                     lastStatTime;
242    tr_stat                    stats;
243
244    tr_torrent *               next;
245
246    int                        uniqueId;
247
248    struct tr_bandwidth      * bandwidth;
249
250    struct tr_torrent_peers  * torrentPeers;
251
252    double                     desiredRatio;
253    tr_ratiolimit              ratioLimitMode;
254
255    uint64_t                   preVerifyTotal;
256};
257
258/* get the index of this piece's first block */
259static inline tr_block_index_t
260tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
261{
262    return piece * tor->blockCountInPiece;
263}
264
265/* what piece index is this block in? */
266static inline tr_piece_index_t
267tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
268{
269    return block / tor->blockCountInPiece;
270}
271
272/* how many blocks are in this piece? */
273static inline uint32_t
274tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
275{
276    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
277                                             : tor->blockCountInPiece;
278}
279
280/* how many bytes are in this piece? */
281static inline uint32_t
282tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
283{
284    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
285                                             : tor->info.pieceSize;
286}
287
288/* how many bytes are in this block? */
289static inline uint32_t
290tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
291{
292    return block == tor->blockCount - 1 ? tor->lastBlockSize
293                                        : tor->blockSize;
294}
295
296static inline void tr_torrentLock( const tr_torrent * tor )
297{
298    tr_sessionLock( tor->session );
299}
300
301static inline void tr_torrentUnlock( const tr_torrent * tor )
302{
303    tr_sessionUnlock( tor->session );
304}
305
306static inline tr_bool
307tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
308{
309    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
310}
311
312static inline tr_bool
313tr_torrentIsSeed( const tr_torrent * tor )
314{
315    return tor->completeness != TR_LEECH;
316}
317
318static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
319{
320    return ( tor != NULL ) && tor->info.isPrivate;
321}
322
323static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
324{
325    return ( tor != NULL )
326        && ( tor->session->isPexEnabled )
327        && ( !tr_torrentIsPrivate( tor ) );
328}
329
330static inline tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
331{
332    return ( tor != NULL )
333        && ( tr_sessionAllowsDHT( tor->session ) )
334        && ( !tr_torrentIsPrivate( tor ) );
335}
336
337static inline tr_bool tr_torrentAllowsLPD( const tr_torrent * tor )
338{
339    return ( tor != NULL )
340        && ( tr_sessionAllowsLPD( tor->session ) )
341        && ( !tr_torrentIsPrivate( tor ) );
342}
343
344static inline tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor,
345                                                   tr_piece_index_t    i )
346{
347    return tr_bitfieldHasFast( &tor->checkedPieces, i );
348}
349
350/***
351****
352***/
353
354enum
355{
356    TORRENT_MAGIC_NUMBER = 95549
357};
358
359static inline tr_bool tr_isTorrent( const tr_torrent * tor )
360{
361    return ( tor != NULL )
362        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
363        && ( tr_isSession( tor->session ) );
364}
365
366/* set a flag indicating that the torrent's .resume file
367 * needs to be saved when the torrent is closed */
368static inline
369void tr_torrentSetDirty( tr_torrent * tor )
370{
371    assert( tr_isTorrent( tor ) );
372
373    tor->isDirty = TRUE;
374}
375
376static inline
377const char * tr_torrentName( const tr_torrent * tor )
378{
379    assert( tr_isTorrent( tor ) );
380
381    return tor->info.name;
382}
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 */
403tr_bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo,
404                             const char ** base, char ** subpath );
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
417#endif
Note: See TracBrowser for help on using the repository browser.