source: trunk/libtransmission/torrent.h @ 7632

Last change on this file since 7632 was 7632, checked in by charles, 13 years ago

(trunk libT) update the copyrights on some files that no longer have old code in them

  • Property svn:keywords set to Date Rev Author Id
File size: 8.7 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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 7632 2009-01-07 06:53:29Z 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 "ratecontrol.h" /* tr_ratecontrol */
22#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
23#include "utils.h" /* tr_bitfield */
24
25struct tr_bandwidth;
26struct tr_ratecontrol;
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
37/**
38***
39**/
40
41/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
42void        tr_torrentInitFileDLs( tr_torrent *      tor,
43                                   tr_file_index_t * files,
44                                   tr_file_index_t   fileCount,
45                                   tr_bool           do_download );
46
47void        tr_torrentRecheckCompleteness( tr_torrent * );
48
49void        tr_torrentResetTransferStats( tr_torrent * );
50
51void        tr_torrentSetHasPiece( tr_torrent *     tor,
52                                   tr_piece_index_t pieceIndex,
53                                   tr_bool          has );
54
55void        tr_torrentChangeMyPort( tr_torrent * session );
56
57tr_torrent* tr_torrentFindFromId( tr_session * session,
58                                  int          id );
59
60tr_torrent* tr_torrentFindFromHash( tr_session *    session,
61                                    const uint8_t * hash );
62
63tr_torrent* tr_torrentFindFromHashString( tr_session * session,
64                                          const char * hashString );
65
66tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
67                                              const uint8_t * hash );
68
69tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
70                                              tr_direction       direction );
71
72
73#define tr_block( a, b ) _tr_block( tor, a, b )
74tr_block_index_t _tr_block( const tr_torrent * tor,
75                            tr_piece_index_t   index,
76                            uint32_t           offset );
77
78tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
79                                       tr_piece_index_t   index,
80                                       uint32_t           offset,
81                                       uint32_t           length );
82
83uint64_t         tr_pieceOffset( const tr_torrent * tor,
84                                 tr_piece_index_t   index,
85                                 uint32_t           offset,
86                                 uint32_t           length );
87
88void             tr_torrentInitFilePriority( tr_torrent       * tor,
89                                             tr_file_index_t    fileIndex,
90                                             tr_priority_t      priority );
91
92
93int              tr_torrentCountUncheckedPieces( const tr_torrent * );
94
95tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
96                                          tr_file_index_t     file );
97
98void             tr_torrentSetPieceChecked( tr_torrent       * tor,
99                                            tr_piece_index_t   piece,
100                                            tr_bool            isChecked );
101
102void             tr_torrentSetFileChecked( tr_torrent       * tor,
103                                           tr_file_index_t    file,
104                                           tr_bool            isChecked );
105
106void             tr_torrentUncheck( tr_torrent * tor );
107
108int              tr_torrentPromoteTracker( tr_torrent   * tor,
109                                           int            trackerIndex );
110
111time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
112                                      size_t            * setmeCount );
113
114typedef enum
115{
116    TR_VERIFY_NONE,
117    TR_VERIFY_WAIT,
118    TR_VERIFY_NOW
119}
120tr_verify_state;
121
122struct tr_torrent
123{
124    tr_session *             session;
125    tr_info                  info;
126
127    tr_speedlimit            speedLimitMode[2];
128
129    struct tr_ratecontrol    swarmSpeed;
130
131    int                      error;
132    char                     errorString[128];
133
134    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
135
136    /* If the initiator of the connection receives a handshake in which the
137     * peer_id does not match the expected peerid, then the initiator is
138     * expected to drop the connection. Note that the initiator presumably
139     * received the peer information from the tracker, which includes the
140     * peer_id that was registered by the peer. The peer_id from the tracker
141     * and in the handshake are expected to match.
142     */
143    uint8_t * peer_id;
144
145    /* Where to download */
146    char * downloadDir;
147
148    /* How many bytes we ask for per request */
149    uint32_t                   blockSize;
150    tr_block_index_t           blockCount;
151
152    uint32_t                   lastBlockSize;
153    uint32_t                   lastPieceSize;
154
155    uint32_t                   blockCountInPiece;
156    uint32_t                   blockCountInLastPiece;
157
158    struct tr_completion       completion;
159
160    struct tr_bitfield         checkedPieces;
161    tr_completeness            completeness;
162
163    struct tr_tracker *        tracker;
164    struct tr_publisher_tag *  trackerSubscription;
165
166    uint64_t                   downloadedCur;
167    uint64_t                   downloadedPrev;
168    uint64_t                   uploadedCur;
169    uint64_t                   uploadedPrev;
170    uint64_t                   corruptCur;
171    uint64_t                   corruptPrev;
172
173    time_t                     addedDate;
174    time_t                     activityDate;
175    time_t                     doneDate;
176    time_t                     startDate;
177
178    tr_torrent_completeness_func *   completeness_func;
179    void *                     completeness_func_user_data;
180
181    tr_bool                    isRunning;
182    tr_bool                    isDeleting;
183
184    uint16_t                   maxConnectedPeers;
185
186    tr_verify_state            verifyState;
187
188    time_t                     lastStatTime;
189    tr_stat                    stats;
190
191    tr_torrent *               next;
192
193    int                        uniqueId;
194
195    struct tr_bandwidth      * bandwidth;
196};
197
198/* get the index of this piece's first block */
199static inline tr_block_index_t
200tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
201{
202    return piece * tor->blockCountInPiece;
203}
204
205/* what piece index is this block in? */
206static inline tr_piece_index_t
207tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
208{
209    return block / tor->blockCountInPiece;
210}
211
212/* how many blocks are in this piece? */
213static inline uint32_t
214tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
215{
216    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
217                                             : tor->blockCountInPiece;
218}
219
220/* how many bytes are in this piece? */
221static inline uint32_t
222tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
223{
224    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
225                                             : tor->info.pieceSize;
226}
227
228/* how many bytes are in this block? */
229static inline uint32_t
230tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
231{
232    return block == tor->blockCount - 1 ? tor->lastBlockSize
233                                        : tor->blockSize;
234}
235
236static inline void tr_torrentLock( const tr_torrent * tor )
237{
238    tr_globalLock( tor->session );
239}
240
241static inline void tr_torrentUnlock( const tr_torrent * tor )
242{
243    tr_globalUnlock( tor->session );
244}
245
246static inline tr_bool
247tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
248{
249    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
250}
251
252static inline tr_bool
253tr_torrentIsSeed( const tr_torrent * tor )
254{
255    return tor->completeness != TR_LEECH;
256}
257
258static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
259{
260    return ( tor != NULL ) && tor->info.isPrivate;
261}
262
263static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
264{
265    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
266}
267
268static inline tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
269{
270    return tr_bitfieldHas( &tor->checkedPieces, i );
271}
272
273#endif
Note: See TracBrowser for help on using the repository browser.