2009-01-07 06:53:29 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
|
2007-12-25 05:37:32 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-12-25 05:37:32 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2009-01-07 06:53:29 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
#ifndef TR_TORRENT_H
|
|
|
|
#define TR_TORRENT_H 1
|
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
#include "completion.h" /* tr_completion */
|
2009-01-02 20:42:35 +00:00
|
|
|
#include "ratecontrol.h" /* tr_ratecontrol */
|
2009-01-02 17:01:55 +00:00
|
|
|
#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
|
|
|
|
#include "utils.h" /* tr_bitfield */
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
struct tr_bandwidth;
|
2008-11-06 02:56:51 +00:00
|
|
|
struct tr_ratecontrol;
|
2009-01-13 21:00:05 +00:00
|
|
|
struct tr_torrent_peers;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2008-10-20 16:00:14 +00:00
|
|
|
/**
|
|
|
|
*** Package-visible ctor API
|
|
|
|
**/
|
|
|
|
|
|
|
|
void tr_ctorSetSave( tr_ctor * ctor,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool saveMetadataInOurTorrentsDir );
|
2008-10-20 16:00:14 +00:00
|
|
|
|
|
|
|
int tr_ctorGetSave( const tr_ctor * ctor );
|
|
|
|
|
2009-04-02 20:43:42 +00:00
|
|
|
void tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor );
|
|
|
|
|
|
|
|
void tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor );
|
|
|
|
|
2008-10-20 16:00:14 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
|
2008-09-23 19:11:04 +00:00
|
|
|
void tr_torrentInitFileDLs( tr_torrent * tor,
|
|
|
|
tr_file_index_t * files,
|
|
|
|
tr_file_index_t fileCount,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool do_download );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void tr_torrentRecheckCompleteness( tr_torrent * );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void tr_torrentResetTransferStats( tr_torrent * );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void tr_torrentSetHasPiece( tr_torrent * tor,
|
|
|
|
tr_piece_index_t pieceIndex,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool has );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
void tr_torrentChangeMyPort( tr_torrent * session );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_torrent* tr_torrentFindFromHash( tr_session * session,
|
|
|
|
const uint8_t * hash );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_torrent* tr_torrentFindFromHashString( tr_session * session,
|
|
|
|
const char * hashString );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session * session,
|
|
|
|
const uint8_t * hash );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
|
2008-11-07 04:10:27 +00:00
|
|
|
tr_direction direction );
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
#define tr_block( a, b ) _tr_block( tor, a, b )
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t _tr_block( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t index,
|
|
|
|
uint32_t offset );
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool tr_torrentReqIsValid( const tr_torrent * tor,
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_piece_index_t index,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length );
|
|
|
|
|
|
|
|
uint64_t tr_pieceOffset( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t index,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length );
|
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
void tr_torrentInitFilePriority( tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex,
|
|
|
|
tr_priority_t priority );
|
2008-03-06 13:24:44 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
int tr_torrentCountUncheckedPieces( const tr_torrent * );
|
2007-12-25 06:37:21 +00:00
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool tr_torrentIsFileChecked( const tr_torrent * tor,
|
2008-10-26 15:39:04 +00:00
|
|
|
tr_file_index_t file );
|
2008-01-02 18:05:05 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
void tr_torrentSetPieceChecked( tr_torrent * tor,
|
|
|
|
tr_piece_index_t piece,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isChecked );
|
2008-05-05 19:51:53 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
void tr_torrentSetFileChecked( tr_torrent * tor,
|
|
|
|
tr_file_index_t file,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isChecked );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
void tr_torrentUncheck( tr_torrent * tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
int tr_torrentPromoteTracker( tr_torrent * tor,
|
|
|
|
int trackerIndex );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
time_t* tr_torrentGetMTimes( const tr_torrent * tor,
|
|
|
|
size_t * setmeCount );
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2009-01-13 16:39:19 +00:00
|
|
|
tr_torrent* tr_torrentNext( tr_session * session,
|
|
|
|
tr_torrent * current );
|
|
|
|
|
2009-03-01 13:56:22 +00:00
|
|
|
void tr_torrentCheckSeedRatio( tr_torrent * tor );
|
|
|
|
|
|
|
|
|
2009-01-13 16:39:19 +00:00
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
TR_VERIFY_NONE,
|
|
|
|
TR_VERIFY_WAIT,
|
|
|
|
TR_VERIFY_NOW
|
2007-12-25 05:37:32 +00:00
|
|
|
}
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_verify_state;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-03-26 18:06:54 +00:00
|
|
|
void tr_torrentSetVerifyState( tr_torrent * tor,
|
|
|
|
tr_verify_state state );
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
struct tr_torrent
|
|
|
|
{
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_session * session;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_info info;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-01-02 20:42:35 +00:00
|
|
|
struct tr_ratecontrol swarmSpeed;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
int magicNumber;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
int error;
|
|
|
|
char errorString[128];
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t obfuscatedHash[SHA_DIGEST_LENGTH];
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-12-04 05:27:59 +00:00
|
|
|
/* If the initiator of the connection receives a handshake in which the
|
|
|
|
* peer_id does not match the expected peerid, then the initiator is
|
|
|
|
* expected to drop the connection. Note that the initiator presumably
|
|
|
|
* received the peer information from the tracker, which includes the
|
|
|
|
* peer_id that was registered by the peer. The peer_id from the tracker
|
|
|
|
* and in the handshake are expected to match.
|
|
|
|
*/
|
|
|
|
uint8_t * peer_id;
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
/* Where to download */
|
2008-12-04 05:27:59 +00:00
|
|
|
char * downloadDir;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
/* How many bytes we ask for per request */
|
2008-03-22 18:10:59 +00:00
|
|
|
uint32_t blockSize;
|
|
|
|
tr_block_index_t blockCount;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
uint32_t lastBlockSize;
|
|
|
|
uint32_t lastPieceSize;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
uint32_t blockCountInPiece;
|
|
|
|
uint32_t blockCountInLastPiece;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
struct tr_completion completion;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-29 09:51:54 +00:00
|
|
|
struct tr_bitfield checkedPieces;
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_completeness completeness;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
struct tr_tracker * tracker;
|
|
|
|
struct tr_publisher_tag * trackerSubscription;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
time_t dhtAnnounceAt;
|
|
|
|
tr_bool dhtAnnounceInProgress;
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
uint64_t downloadedCur;
|
|
|
|
uint64_t downloadedPrev;
|
|
|
|
uint64_t uploadedCur;
|
|
|
|
uint64_t uploadedPrev;
|
|
|
|
uint64_t corruptCur;
|
|
|
|
uint64_t corruptPrev;
|
|
|
|
|
2009-07-02 01:50:07 +00:00
|
|
|
uint64_t etaSpeedCalculatedAt;
|
|
|
|
double etaSpeed;
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
time_t addedDate;
|
2008-05-23 20:04:41 +00:00
|
|
|
time_t activityDate;
|
2008-06-03 19:16:12 +00:00
|
|
|
time_t doneDate;
|
|
|
|
time_t startDate;
|
2009-03-26 18:06:54 +00:00
|
|
|
time_t anyDate;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrent_completeness_func * completeness_func;
|
|
|
|
void * completeness_func_user_data;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
|
|
|
|
void * ratio_limit_hit_func_user_data;
|
|
|
|
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isRunning;
|
|
|
|
tr_bool isDeleting;
|
2009-04-09 14:10:31 +00:00
|
|
|
tr_bool needsSeedRatioCheck;
|
2009-05-13 15:54:04 +00:00
|
|
|
tr_bool startAfterVerify;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
|
|
|
uint16_t maxConnectedPeers;
|
|
|
|
|
2008-03-31 19:34:20 +00:00
|
|
|
tr_verify_state verifyState;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
|
|
|
time_t lastStatTime;
|
2008-03-31 19:34:20 +00:00
|
|
|
tr_stat stats;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * next;
|
2008-05-12 00:41:55 +00:00
|
|
|
|
|
|
|
int uniqueId;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2009-06-16 19:31:17 +00:00
|
|
|
struct tr_bandwidth * bandwidth;
|
|
|
|
|
2009-01-13 21:00:05 +00:00
|
|
|
struct tr_torrent_peers * torrentPeers;
|
2009-02-13 18:23:56 +00:00
|
|
|
|
|
|
|
double desiredRatio;
|
|
|
|
tr_ratiolimit ratioLimitMode;
|
2007-12-25 05:37:32 +00:00
|
|
|
};
|
|
|
|
|
2009-01-02 06:28:22 +00:00
|
|
|
/* get the index of this piece's first block */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_block_index_t
|
2009-01-02 06:28:22 +00:00
|
|
|
tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
|
|
|
|
{
|
|
|
|
return piece * tor->blockCountInPiece;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* what piece index is this block in? */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_piece_index_t
|
2009-01-02 06:28:22 +00:00
|
|
|
tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
|
|
|
|
{
|
|
|
|
return block / tor->blockCountInPiece;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how many blocks are in this piece? */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE uint32_t
|
2009-01-02 06:28:22 +00:00
|
|
|
tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
|
|
|
|
{
|
|
|
|
return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
|
|
|
|
: tor->blockCountInPiece;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how many bytes are in this piece? */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE uint32_t
|
2009-01-02 06:28:22 +00:00
|
|
|
tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
|
|
|
|
{
|
|
|
|
return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
|
|
|
|
: tor->info.pieceSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how many bytes are in this block? */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE uint32_t
|
2009-01-02 06:28:22 +00:00
|
|
|
tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
|
|
|
|
{
|
|
|
|
return block == tor->blockCount - 1 ? tor->lastBlockSize
|
|
|
|
: tor->blockSize;
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
|
|
|
tr_globalLock( tor->session );
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
|
|
|
tr_globalUnlock( tor->session );
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_bool
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_torrentExists( const tr_session * session, const uint8_t * torrentHash )
|
|
|
|
{
|
|
|
|
return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_bool
|
2009-01-03 00:25:27 +00:00
|
|
|
tr_torrentIsSeed( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
return tor->completeness != TR_LEECH;
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
|
2009-01-03 00:25:27 +00:00
|
|
|
{
|
|
|
|
return ( tor != NULL ) && tor->info.isPrivate;
|
|
|
|
}
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
|
2009-01-03 00:25:27 +00:00
|
|
|
{
|
2009-06-21 08:57:26 +00:00
|
|
|
return ( tor != NULL )
|
|
|
|
&& ( tor->session->isPexEnabled )
|
|
|
|
&& ( !tr_torrentIsPrivate( tor ) );
|
2009-01-03 00:25:27 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
static TR_INLINE tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
|
|
|
|
{
|
2009-06-21 08:57:26 +00:00
|
|
|
return ( tor != NULL )
|
|
|
|
&& ( tr_sessionAllowsDHT( tor->session ) )
|
|
|
|
&& ( !tr_torrentIsPrivate( tor ) );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 08:57:26 +00:00
|
|
|
static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t i )
|
2009-01-03 00:25:27 +00:00
|
|
|
{
|
2009-04-11 03:24:36 +00:00
|
|
|
return tr_bitfieldHasFast( &tor->checkedPieces, i );
|
2009-01-03 00:25:27 +00:00
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TORRENT_MAGIC_NUMBER = 95549
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline tr_bool tr_isTorrent( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
return ( tor != NULL )
|
|
|
|
&& ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
|
|
|
|
&& ( tr_isSession( tor->session ) );
|
|
|
|
}
|
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
#endif
|