1 | /****************************************************************************** |
---|
2 | * $Id: torrent.h 7576 2009-01-02 06:28:22Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef __TRANSMISSION__ |
---|
26 | #error only libtransmission should #include this header. |
---|
27 | #endif |
---|
28 | |
---|
29 | #include "utils.h" /* tr_bitfield */ |
---|
30 | |
---|
31 | #ifndef TR_TORRENT_H |
---|
32 | #define TR_TORRENT_H 1 |
---|
33 | |
---|
34 | struct tr_bandwidth; |
---|
35 | struct tr_ratecontrol; |
---|
36 | |
---|
37 | /** |
---|
38 | *** Package-visible ctor API |
---|
39 | **/ |
---|
40 | |
---|
41 | void tr_ctorSetSave( tr_ctor * ctor, |
---|
42 | tr_bool saveMetadataInOurTorrentsDir ); |
---|
43 | |
---|
44 | int tr_ctorGetSave( const tr_ctor * ctor ); |
---|
45 | |
---|
46 | /** |
---|
47 | *** |
---|
48 | **/ |
---|
49 | |
---|
50 | /* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */ |
---|
51 | void tr_torrentInitFileDLs( tr_torrent * tor, |
---|
52 | tr_file_index_t * files, |
---|
53 | tr_file_index_t fileCount, |
---|
54 | tr_bool do_download ); |
---|
55 | |
---|
56 | tr_bool tr_torrentIsPrivate( const tr_torrent * ); |
---|
57 | |
---|
58 | void tr_torrentRecheckCompleteness( tr_torrent * ); |
---|
59 | |
---|
60 | void tr_torrentResetTransferStats( tr_torrent * ); |
---|
61 | |
---|
62 | void tr_torrentSetHasPiece( tr_torrent * tor, |
---|
63 | tr_piece_index_t pieceIndex, |
---|
64 | tr_bool has ); |
---|
65 | |
---|
66 | extern inline void |
---|
67 | tr_torrentLock( const tr_torrent * session ); |
---|
68 | |
---|
69 | extern inline void |
---|
70 | tr_torrentUnlock( const tr_torrent * session ); |
---|
71 | |
---|
72 | tr_bool tr_torrentIsSeed( const tr_torrent * session ); |
---|
73 | |
---|
74 | void tr_torrentChangeMyPort( tr_torrent * session ); |
---|
75 | |
---|
76 | extern inline tr_bool |
---|
77 | tr_torrentExists( const tr_session * session, |
---|
78 | const uint8_t * hash ); |
---|
79 | |
---|
80 | tr_torrent* tr_torrentFindFromId( tr_session * session, |
---|
81 | int id ); |
---|
82 | |
---|
83 | tr_torrent* tr_torrentFindFromHash( tr_session * session, |
---|
84 | const uint8_t * hash ); |
---|
85 | |
---|
86 | tr_torrent* tr_torrentFindFromHashString( tr_session * session, |
---|
87 | const char * hashString ); |
---|
88 | |
---|
89 | tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session * session, |
---|
90 | const uint8_t * hash ); |
---|
91 | |
---|
92 | tr_bool tr_torrentAllowsPex( const tr_torrent * ); |
---|
93 | |
---|
94 | tr_bool tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent, |
---|
95 | tr_direction direction ); |
---|
96 | |
---|
97 | |
---|
98 | #define tr_block( a, b ) _tr_block( tor, a, b ) |
---|
99 | tr_block_index_t _tr_block( const tr_torrent * tor, |
---|
100 | tr_piece_index_t index, |
---|
101 | uint32_t offset ); |
---|
102 | |
---|
103 | tr_bool tr_torrentReqIsValid( const tr_torrent * tor, |
---|
104 | tr_piece_index_t index, |
---|
105 | uint32_t offset, |
---|
106 | uint32_t length ); |
---|
107 | |
---|
108 | uint64_t tr_pieceOffset( const tr_torrent * tor, |
---|
109 | tr_piece_index_t index, |
---|
110 | uint32_t offset, |
---|
111 | uint32_t length ); |
---|
112 | |
---|
113 | void tr_torrentInitFilePriority( tr_torrent * tor, |
---|
114 | tr_file_index_t fileIndex, |
---|
115 | tr_priority_t priority ); |
---|
116 | |
---|
117 | |
---|
118 | int tr_torrentCountUncheckedPieces( const tr_torrent * ); |
---|
119 | |
---|
120 | tr_bool tr_torrentIsPieceChecked( const tr_torrent * tor, |
---|
121 | tr_piece_index_t piece ); |
---|
122 | |
---|
123 | tr_bool tr_torrentIsFileChecked( const tr_torrent * tor, |
---|
124 | tr_file_index_t file ); |
---|
125 | |
---|
126 | void tr_torrentSetPieceChecked( tr_torrent * tor, |
---|
127 | tr_piece_index_t piece, |
---|
128 | tr_bool isChecked ); |
---|
129 | |
---|
130 | void tr_torrentSetFileChecked( tr_torrent * tor, |
---|
131 | tr_file_index_t file, |
---|
132 | tr_bool isChecked ); |
---|
133 | |
---|
134 | void tr_torrentUncheck( tr_torrent * tor ); |
---|
135 | |
---|
136 | int tr_torrentPromoteTracker( tr_torrent * tor, |
---|
137 | int trackerIndex ); |
---|
138 | |
---|
139 | time_t* tr_torrentGetMTimes( const tr_torrent * tor, |
---|
140 | size_t * setmeCount ); |
---|
141 | |
---|
142 | typedef enum |
---|
143 | { |
---|
144 | TR_VERIFY_NONE, |
---|
145 | TR_VERIFY_WAIT, |
---|
146 | TR_VERIFY_NOW |
---|
147 | } |
---|
148 | tr_verify_state; |
---|
149 | |
---|
150 | struct tr_torrent |
---|
151 | { |
---|
152 | tr_session * session; |
---|
153 | tr_info info; |
---|
154 | |
---|
155 | tr_speedlimit speedLimitMode[2]; |
---|
156 | |
---|
157 | struct tr_ratecontrol * swarmSpeed; |
---|
158 | |
---|
159 | int error; |
---|
160 | char errorString[128]; |
---|
161 | |
---|
162 | uint8_t obfuscatedHash[SHA_DIGEST_LENGTH]; |
---|
163 | |
---|
164 | /* If the initiator of the connection receives a handshake in which the |
---|
165 | * peer_id does not match the expected peerid, then the initiator is |
---|
166 | * expected to drop the connection. Note that the initiator presumably |
---|
167 | * received the peer information from the tracker, which includes the |
---|
168 | * peer_id that was registered by the peer. The peer_id from the tracker |
---|
169 | * and in the handshake are expected to match. |
---|
170 | */ |
---|
171 | uint8_t * peer_id; |
---|
172 | |
---|
173 | /* Where to download */ |
---|
174 | char * downloadDir; |
---|
175 | |
---|
176 | /* How many bytes we ask for per request */ |
---|
177 | uint32_t blockSize; |
---|
178 | tr_block_index_t blockCount; |
---|
179 | |
---|
180 | uint32_t lastBlockSize; |
---|
181 | uint32_t lastPieceSize; |
---|
182 | |
---|
183 | uint32_t blockCountInPiece; |
---|
184 | uint32_t blockCountInLastPiece; |
---|
185 | |
---|
186 | struct tr_completion * completion; |
---|
187 | |
---|
188 | struct tr_bitfield checkedPieces; |
---|
189 | tr_completeness completeness; |
---|
190 | |
---|
191 | struct tr_tracker * tracker; |
---|
192 | struct tr_publisher_tag * trackerSubscription; |
---|
193 | |
---|
194 | uint64_t downloadedCur; |
---|
195 | uint64_t downloadedPrev; |
---|
196 | uint64_t uploadedCur; |
---|
197 | uint64_t uploadedPrev; |
---|
198 | uint64_t corruptCur; |
---|
199 | uint64_t corruptPrev; |
---|
200 | |
---|
201 | time_t addedDate; |
---|
202 | time_t activityDate; |
---|
203 | time_t doneDate; |
---|
204 | time_t startDate; |
---|
205 | |
---|
206 | tr_torrent_completeness_func * completeness_func; |
---|
207 | void * completeness_func_user_data; |
---|
208 | |
---|
209 | tr_bool isRunning; |
---|
210 | tr_bool isDeleting; |
---|
211 | |
---|
212 | uint16_t maxConnectedPeers; |
---|
213 | |
---|
214 | tr_verify_state verifyState; |
---|
215 | |
---|
216 | time_t lastStatTime; |
---|
217 | tr_stat stats; |
---|
218 | |
---|
219 | tr_torrent * next; |
---|
220 | |
---|
221 | int uniqueId; |
---|
222 | |
---|
223 | struct tr_bandwidth * bandwidth; |
---|
224 | }; |
---|
225 | |
---|
226 | /* get the index of this piece's first block */ |
---|
227 | static inline tr_block_index_t |
---|
228 | tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
229 | { |
---|
230 | return piece * tor->blockCountInPiece; |
---|
231 | } |
---|
232 | |
---|
233 | /* what piece index is this block in? */ |
---|
234 | static inline tr_piece_index_t |
---|
235 | tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block ) |
---|
236 | { |
---|
237 | return block / tor->blockCountInPiece; |
---|
238 | } |
---|
239 | |
---|
240 | /* how many blocks are in this piece? */ |
---|
241 | static inline uint32_t |
---|
242 | tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
243 | { |
---|
244 | return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece |
---|
245 | : tor->blockCountInPiece; |
---|
246 | } |
---|
247 | |
---|
248 | /* how many bytes are in this piece? */ |
---|
249 | static inline uint32_t |
---|
250 | tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece ) |
---|
251 | { |
---|
252 | return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize |
---|
253 | : tor->info.pieceSize; |
---|
254 | } |
---|
255 | |
---|
256 | /* how many bytes are in this block? */ |
---|
257 | static inline uint32_t |
---|
258 | tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block ) |
---|
259 | { |
---|
260 | return block == tor->blockCount - 1 ? tor->lastBlockSize |
---|
261 | : tor->blockSize; |
---|
262 | } |
---|
263 | |
---|
264 | #endif |
---|