1
/******************************************************************************
2
* $Id: torrent.h 7365 2008-12-13 22:52:13Z charles $
4
* Copyright (c) 2005-2008 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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.
10
* $Id: torrent.h 7813 2009-01-29 18:18:24Z charles $
25
13
#ifndef __TRANSMISSION__
26
#error only libtransmission should #include this header.
14
#error only libtransmission should #include this header.
29
17
#ifndef TR_TORRENT_H
30
18
#define TR_TORRENT_H 1
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 */
32
25
struct tr_bandwidth;
33
26
struct tr_ratecontrol;
27
struct tr_torrent_peers;
36
30
*** Package-visible ctor API
39
33
void tr_ctorSetSave( tr_ctor * ctor,
40
int saveMetadataInOurTorrentsDir );
34
tr_bool saveMetadataInOurTorrentsDir );
42
36
int tr_ctorGetSave( const tr_ctor * ctor );
60
52
void tr_torrentSetHasPiece( tr_torrent * tor,
61
53
tr_piece_index_t pieceIndex,
64
void tr_torrentLock( const tr_torrent * session );
66
void tr_torrentUnlock( const tr_torrent * session );
68
int tr_torrentIsSeed( const tr_torrent * session );
70
56
void tr_torrentChangeMyPort( tr_torrent * session );
72
int tr_torrentExists( const tr_session * session,
73
const uint8_t * hash );
75
58
tr_torrent* tr_torrentFindFromId( tr_session * session,
84
67
tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session * session,
85
68
const uint8_t * hash );
87
int tr_torrentAllowsPex( const tr_torrent * );
89
70
tr_bool tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
90
71
tr_direction direction );
92
/* get the index of this piece's first block */
93
#define tr_torPieceFirstBlock( tor, piece ) ( ( piece ) *\
94
( tor )->blockCountInPiece )
96
/* what piece index is this block in? */
97
#define tr_torBlockPiece( tor, block ) ( ( block ) /\
98
( tor )->blockCountInPiece )
100
/* how many blocks are in this piece? */
101
#define tr_torPieceCountBlocks( tor, piece ) \
103
( ( tor )->info.pieceCount - \
104
1 ) ) ? ( tor )->blockCountInLastPiece : ( tor )->blockCountInPiece )
106
/* how many bytes are in this piece? */
107
#define tr_torPieceCountBytes( tor, piece ) \
109
( ( tor )->info.pieceCount - \
110
1 ) ) ? ( tor )->lastPieceSize : ( tor )->info.pieceSize )
112
/* how many bytes are in this block? */
113
#define tr_torBlockCountBytes( tor, block ) \
115
( ( tor )->blockCount - \
116
1 ) ) ? ( tor )->lastBlockSize : ( tor )->blockSize )
118
74
#define tr_block( a, b ) _tr_block( tor, a, b )
119
75
tr_block_index_t _tr_block( const tr_torrent * tor,
120
76
tr_piece_index_t index,
121
77
uint32_t offset );
123
int tr_torrentReqIsValid( const tr_torrent * tor,
79
tr_bool tr_torrentReqIsValid( const tr_torrent * tor,
124
80
tr_piece_index_t index,
126
82
uint32_t length );
138
94
int tr_torrentCountUncheckedPieces( const tr_torrent * );
140
int tr_torrentIsPieceChecked( const tr_torrent * tor,
141
tr_piece_index_t piece );
143
int tr_torrentIsFileChecked( const tr_torrent * tor,
96
tr_bool tr_torrentIsFileChecked( const tr_torrent * tor,
144
97
tr_file_index_t file );
146
99
void tr_torrentSetPieceChecked( tr_torrent * tor,
147
100
tr_piece_index_t piece,
150
103
void tr_torrentSetFileChecked( tr_torrent * tor,
151
104
tr_file_index_t file,
154
107
void tr_torrentUncheck( tr_torrent * tor );
203
162
uint32_t blockCountInPiece;
204
163
uint32_t blockCountInLastPiece;
206
struct tr_completion * completion;
165
struct tr_completion completion;
208
struct tr_bitfield * checkedPieces;
167
struct tr_bitfield checkedPieces;
209
168
tr_completeness completeness;
211
170
struct tr_tracker * tracker;
243
202
struct tr_bandwidth * bandwidth;
204
struct tr_torrent_peers * torrentPeers;
207
/* get the index of this piece's first block */
208
static TR_INLINE tr_block_index_t
209
tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
211
return piece * tor->blockCountInPiece;
214
/* what piece index is this block in? */
215
static TR_INLINE tr_piece_index_t
216
tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
218
return block / tor->blockCountInPiece;
221
/* how many blocks are in this piece? */
222
static TR_INLINE uint32_t
223
tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
225
return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
226
: tor->blockCountInPiece;
229
/* how many bytes are in this piece? */
230
static TR_INLINE uint32_t
231
tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
233
return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
234
: tor->info.pieceSize;
237
/* how many bytes are in this block? */
238
static TR_INLINE uint32_t
239
tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
241
return block == tor->blockCount - 1 ? tor->lastBlockSize
245
static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
247
tr_globalLock( tor->session );
250
static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
252
tr_globalUnlock( tor->session );
255
static TR_INLINE tr_bool
256
tr_torrentExists( const tr_session * session, const uint8_t * torrentHash )
258
return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
261
static TR_INLINE tr_bool
262
tr_torrentIsSeed( const tr_torrent * tor )
264
return tor->completeness != TR_LEECH;
267
static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
269
return ( tor != NULL ) && tor->info.isPrivate;
272
static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
274
return ( tor != NULL ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
277
static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent * tor, tr_piece_index_t i )
279
return tr_bitfieldHas( &tor->checkedPieces, i );
288
TORRENT_MAGIC_NUMBER = 95549
291
static inline tr_bool tr_isTorrent( const tr_torrent * tor )
293
return ( tor != NULL )
294
&& ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
295
&& ( tr_isSession( tor->session ) );