2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-03-23 08:28:01 +00:00
|
|
|
* Copyright (c) 2005-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "bencode.h"
|
|
|
|
#include "clients.h" /* for tr_clientForId() */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "completion.h"
|
|
|
|
#include "inout.h"
|
2007-07-09 04:37:16 +00:00
|
|
|
#include "list.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "peer.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "peertree.h"
|
|
|
|
#include "ratecontrol.h"
|
2007-08-16 20:00:06 +00:00
|
|
|
#include "trcompat.h" /* for strlcpy */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
*****/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** The "SWIFT" system is described by Karthik Tamilmani,
|
|
|
|
*** Vinay Pai, and Alexander Mohr of Stony Brook University
|
|
|
|
*** in their paper "SWIFT: A System With Incentives For Trading"
|
|
|
|
*** http://citeseer.ist.psu.edu/tamilmani04swift.html
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use SWIFT?
|
|
|
|
*/
|
|
|
|
static const int SWIFT_ENABLED = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For every byte the peer uploads to us,
|
|
|
|
* allow them to download this many bytes from us
|
|
|
|
*/
|
2007-06-29 05:45:17 +00:00
|
|
|
static const double SWIFT_REPAYMENT_RATIO = 1.33;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow new peers to download this many bytes from
|
|
|
|
* us when getting started. This can prevent gridlock
|
|
|
|
* with other peers using tit-for-tat algorithms
|
|
|
|
*/
|
|
|
|
static const int SWIFT_INITIAL_CREDIT = 64 * 1024; /* 64 KiB */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We expend a fraction of our torrent's total upload speed
|
2007-06-22 04:30:39 +00:00
|
|
|
* on largesse by uniformly distributing free credit to
|
2007-06-21 14:47:26 +00:00
|
|
|
* all of our peers. This too helps prevent gridlock.
|
|
|
|
*/
|
2007-06-26 18:45:03 +00:00
|
|
|
static const double SWIFT_LARGESSE = 0.10; /* 10% of our UL */
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How frequently to extend largesse-based credit
|
|
|
|
*/
|
|
|
|
static const int SWIFT_REFRESH_INTERVAL_SEC = 5;
|
|
|
|
|
|
|
|
/*****
|
|
|
|
******
|
|
|
|
*****/
|
|
|
|
|
2007-06-29 06:03:54 +00:00
|
|
|
#define PERCENT_PEER_WANTED 25 /* Percent before we start relax peers min activeness */
|
2007-05-24 20:09:32 +00:00
|
|
|
#define MIN_UPLOAD_IDLE 60000 /* In high peer situations we wait only 1 min
|
|
|
|
until dropping peers for idling */
|
|
|
|
#define MAX_UPLOAD_IDLE 240000 /* In low peer situations we wait the
|
|
|
|
4 mins until dropping peers for idling */
|
|
|
|
#define MIN_KEEP_ALIVE 180000 /* In high peer situations we wait only 3 min
|
|
|
|
without a keep-alive */
|
|
|
|
#define MAX_KEEP_ALIVE 360000 /* In low peer situations we wait the
|
|
|
|
6 mins without a keep-alive */
|
|
|
|
#define MIN_CON_TIMEOUT 8000 /* Time to timeout connecting to peer,
|
|
|
|
during low peer situations */
|
|
|
|
#define MAX_CON_TIMEOUT 30000 /* Time to timeout connecting to peer,
|
|
|
|
during high peer situations */
|
2007-03-23 08:28:01 +00:00
|
|
|
#define PEX_PEER_CUTOFF 50 /* only try to add new peers from pex if
|
|
|
|
we have fewer existing peers than this */
|
|
|
|
#define PEX_INTERVAL 60 /* don't send pex messages more frequently
|
|
|
|
than PEX_INTERVAL +
|
|
|
|
rand( PEX_INTERVAL / 10 ) seconds */
|
2007-08-15 19:44:13 +00:00
|
|
|
|
|
|
|
/* uncomment this to disable support for the extended messaging bit */
|
|
|
|
/* #define DISABLE_EXTMSGS */
|
|
|
|
/* uncomment this to disable support for the azureus protocol bit */
|
|
|
|
/* #define DISABLE_AZPROTO */
|
|
|
|
|
|
|
|
#define HANDSHAKE_NAME "\023BitTorrent protocol"
|
|
|
|
#define HANDSHAKE_NAME_LEN 20
|
|
|
|
#define HANDSHAKE_FLAGS_OFF HANDSHAKE_NAME_LEN
|
|
|
|
#define HANDSHAKE_FLAGS_LEN 8
|
|
|
|
#define HANDSHAKE_HASH_OFF ( HANDSHAKE_FLAGS_OFF + HANDSHAKE_FLAGS_LEN )
|
|
|
|
#define HANDSHAKE_PEERID_OFF ( HANDSHAKE_HASH_OFF + SHA_DIGEST_LENGTH )
|
|
|
|
#define HANDSHAKE_SIZE ( HANDSHAKE_PEERID_OFF + TR_ID_LEN )
|
|
|
|
|
2007-08-15 21:26:24 +00:00
|
|
|
/* these macros test and set the bit indicating extended messaging support */
|
2007-08-15 19:44:13 +00:00
|
|
|
#ifdef DISABLE_EXTMSGS
|
|
|
|
#define HANDSHAKE_HAS_EXTMSGS( bits ) ( 0 )
|
|
|
|
#define HANDSHAKE_SET_EXTMSGS( bits ) ( (void)0 )
|
|
|
|
#else
|
|
|
|
#define HANDSHAKE_HAS_EXTMSGS( bits ) ( (bits)[5] & 0x10 )
|
|
|
|
#define HANDSHAKE_SET_EXTMSGS( bits ) ( (bits)[5] |= 0x10 )
|
|
|
|
#endif
|
|
|
|
|
2007-08-15 21:26:24 +00:00
|
|
|
/* these macros test and set the bit indicating azureus protocol support */
|
2007-08-15 19:44:13 +00:00
|
|
|
#ifdef DISABLE_AZPROTO
|
|
|
|
#define HANDSHAKE_HAS_AZPROTO( bits ) ( 0 )
|
|
|
|
#define HANDSHAKE_SET_AZPROTO( bits ) ( (void)0 )
|
|
|
|
#else
|
|
|
|
#define HANDSHAKE_HAS_AZPROTO( bits ) ( (bits)[0] & 0x80 )
|
|
|
|
#define HANDSHAKE_SET_AZPROTO( bits ) ( (bits)[0] |= 0x80 )
|
|
|
|
#endif
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-08-15 21:26:24 +00:00
|
|
|
/* these macros are to be used if both extended messaging and the
|
|
|
|
azureus protocol is supported, they indicate which protocol is preferred */
|
2007-08-26 16:16:41 +00:00
|
|
|
#ifdef DISABLE_EXTENSION_NEGOTIATION
|
2007-08-15 21:26:24 +00:00
|
|
|
#define HANDSHAKE_GET_EXTPREF( bits ) ( 0 )
|
|
|
|
#define HANDSHAKE_SET_EXTPREF( bits, val ) ( (void)0 )
|
2007-08-26 16:16:41 +00:00
|
|
|
#else
|
|
|
|
#define HANDSHAKE_GET_EXTPREF( bits ) ( (bits)[5] & 0x03 )
|
|
|
|
#define HANDSHAKE_SET_EXTPREF( bits, val ) ( (bits)[5] |= 0x03 & (val) )
|
2007-08-15 21:26:24 +00:00
|
|
|
#endif
|
2007-08-16 18:34:44 +00:00
|
|
|
#define HANDSHAKE_EXTPREF_FORCE_EXT ( 0x0 )
|
|
|
|
#define HANDSHAKE_EXTPREF_WANT_EXT ( 0x1 )
|
|
|
|
#define HANDSHAKE_EXTPREF_WANT_AZ ( 0x2 )
|
|
|
|
#define HANDSHAKE_EXTPREF_FORCE_AZ ( 0x3 )
|
2007-08-15 21:26:24 +00:00
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
#define PEER_MSG_CHOKE 0
|
|
|
|
#define PEER_MSG_UNCHOKE 1
|
|
|
|
#define PEER_MSG_INTERESTED 2
|
|
|
|
#define PEER_MSG_UNINTERESTED 3
|
|
|
|
#define PEER_MSG_HAVE 4
|
|
|
|
#define PEER_MSG_BITFIELD 5
|
|
|
|
#define PEER_MSG_REQUEST 6
|
|
|
|
#define PEER_MSG_PIECE 7
|
|
|
|
#define PEER_MSG_CANCEL 8
|
|
|
|
#define PEER_MSG_PORT 9
|
|
|
|
#define PEER_MSG_EXTENDED 20
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
typedef struct tr_request_s
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int begin;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
} tr_request_t;
|
|
|
|
|
|
|
|
struct tr_peer_s
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
struct in_addr addr;
|
2007-07-30 18:04:10 +00:00
|
|
|
tr_port_t port; /* peer's listening port, 0 if not known */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
#define PEER_STATUS_IDLE 1 /* Need to connect */
|
|
|
|
#define PEER_STATUS_CONNECTING 2 /* Trying to send handshake */
|
2007-03-23 08:28:01 +00:00
|
|
|
#define PEER_STATUS_HANDSHAKE 3 /* Waiting for peer's handshake */
|
|
|
|
#define PEER_STATUS_AZ_GIVER 4 /* Sending Azureus handshake */
|
|
|
|
#define PEER_STATUS_AZ_RECEIVER 5 /* Receiving Azureus handshake */
|
|
|
|
#define PEER_STATUS_CONNECTED 6 /* Got peer's handshake */
|
2007-03-05 23:03:38 +00:00
|
|
|
int status;
|
|
|
|
int socket;
|
2007-03-23 08:28:01 +00:00
|
|
|
char from;
|
|
|
|
char private;
|
|
|
|
char azproto; /* azureus peer protocol is being used */
|
2007-03-05 23:03:38 +00:00
|
|
|
uint64_t date;
|
|
|
|
uint64_t keepAlive;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
#define EXTENDED_NOT_SUPPORTED 0 /* extended messages not supported */
|
|
|
|
#define EXTENDED_SUPPORTED 1 /* waiting to send extended handshake */
|
|
|
|
#define EXTENDED_HANDSHAKE 2 /* sent extended handshake */
|
|
|
|
uint8_t extStatus;
|
|
|
|
uint8_t pexStatus; /* peer's ut_pex id, 0 if not supported */
|
|
|
|
uint64_t lastPex; /* time when last pex packet was sent */
|
|
|
|
int advertisedPort; /* listening port we last told peer */
|
|
|
|
tr_peertree_t sentPeers;
|
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
char isChokedByUs;
|
|
|
|
char isChokingUs;
|
|
|
|
char isInteresting;
|
|
|
|
char isInterested;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
int optimistic;
|
2007-06-29 05:45:17 +00:00
|
|
|
int timesChoked;
|
2007-03-05 23:03:38 +00:00
|
|
|
uint64_t lastChoke;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-28 06:28:34 +00:00
|
|
|
uint8_t id[TR_ID_LEN];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* The pieces that the peer has */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * bitfield;
|
2007-07-06 16:17:37 +00:00
|
|
|
|
|
|
|
/* blocks we've requested from this peer */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * reqfield;
|
2007-03-05 23:03:38 +00:00
|
|
|
int pieceCount;
|
|
|
|
float progress;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
int goodPcs;
|
|
|
|
int badPcs;
|
|
|
|
int banned;
|
2006-07-16 19:39:23 +00:00
|
|
|
/* The pieces that the peer is contributing to */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * blamefield;
|
2006-07-16 19:39:23 +00:00
|
|
|
/* The bad pieces that the peer has contributed to */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * banfield;
|
2007-03-05 23:03:38 +00:00
|
|
|
|
|
|
|
uint8_t * buf;
|
|
|
|
int size;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
uint8_t * outMessages;
|
|
|
|
int outMessagesSize;
|
|
|
|
int outMessagesPos;
|
2007-03-28 06:28:34 +00:00
|
|
|
uint8_t outBlock[25+16384];
|
2007-03-05 23:03:38 +00:00
|
|
|
int outBlockSize;
|
|
|
|
int outBlockLoaded;
|
|
|
|
int outBlockSending;
|
|
|
|
|
|
|
|
int inRequestCount;
|
2007-06-29 05:45:17 +00:00
|
|
|
int inRequestMax;
|
|
|
|
int inRequestAlloc;
|
|
|
|
tr_request_t * inRequests;
|
2007-03-05 23:03:38 +00:00
|
|
|
int inIndex;
|
|
|
|
int inBegin;
|
|
|
|
int inLength;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * outRequests;
|
2007-03-05 23:03:38 +00:00
|
|
|
uint64_t outDate;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ratecontrol * download;
|
|
|
|
tr_ratecontrol * upload;
|
2007-04-03 18:22:58 +00:00
|
|
|
|
|
|
|
char * client;
|
2007-08-15 23:03:50 +00:00
|
|
|
int extclient;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
int64_t credit;
|
2006-07-16 19:39:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define peer_dbg( a... ) __peer_dbg( peer, ## a )
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
static void __peer_dbg( tr_peer_t * peer, char * msg, ... )
|
|
|
|
{
|
|
|
|
char string[256];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, msg );
|
2007-03-12 00:04:11 +00:00
|
|
|
snprintf( string, sizeof string, "%08x:%04x ",
|
2007-09-09 00:45:08 +00:00
|
|
|
(uint32_t) ntohl(peer->addr.s_addr), tr_peerPort(peer) );
|
2006-07-16 19:39:23 +00:00
|
|
|
vsnprintf( &string[14], sizeof( string ) - 14, msg, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
tr_dbg( "%s", string );
|
|
|
|
}
|
|
|
|
|
2007-07-19 10:49:44 +00:00
|
|
|
/* utilities for endian conversions with char pointers */
|
|
|
|
|
|
|
|
static uint32_t tr_ntohl( const void * p )
|
|
|
|
{
|
|
|
|
uint32_t u;
|
|
|
|
memcpy( &u, p, sizeof( uint32_t ) );
|
|
|
|
return ntohl( u );
|
|
|
|
}
|
|
|
|
static void tr_htonl( uint32_t a, void * p )
|
|
|
|
{
|
|
|
|
const uint32_t u = htonl( a );
|
|
|
|
memcpy ( p, &u, sizeof( uint32_t ) );
|
|
|
|
}
|
|
|
|
|
2007-08-21 15:17:02 +00:00
|
|
|
static void tr_peerPieceIsCorrupt ( tr_peer_t *, int pieceIndex );
|
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
#include "peerext.h"
|
|
|
|
#include "peeraz.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "peermessages.h"
|
|
|
|
#include "peerutils.h"
|
|
|
|
#include "peerparse.h"
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_peerInit
|
|
|
|
***********************************************************************
|
|
|
|
* Initializes a new peer.
|
|
|
|
**********************************************************************/
|
2007-07-30 18:04:10 +00:00
|
|
|
tr_peer_t * tr_peerInit( const struct in_addr * addr, tr_port_t port,
|
2007-07-14 16:29:21 +00:00
|
|
|
int s, int from )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-06 16:17:37 +00:00
|
|
|
tr_peer_t * peer;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
assert( 0 <= from && TR_PEER_FROM__MAX > from );
|
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
peer = tr_new0( tr_peer_t, 1 );
|
|
|
|
peer->isChokedByUs = TRUE;
|
|
|
|
peer->isChokingUs = TRUE;
|
|
|
|
peer->date = tr_date();
|
|
|
|
peer->keepAlive = peer->date;
|
|
|
|
peer->download = tr_rcInit();
|
|
|
|
peer->upload = tr_rcInit();
|
2007-07-06 16:17:37 +00:00
|
|
|
peertreeInit( &peer->sentPeers );
|
|
|
|
|
2007-06-29 05:45:17 +00:00
|
|
|
peer->inRequestMax = peer->inRequestAlloc = 2;
|
|
|
|
peer->inRequests = tr_new0( tr_request_t, peer->inRequestAlloc );
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
peer->socket = s;
|
2007-07-14 16:29:21 +00:00
|
|
|
peer->addr = *addr;
|
2007-01-21 06:42:05 +00:00
|
|
|
peer->port = port;
|
2007-03-23 08:28:01 +00:00
|
|
|
peer->from = from;
|
2007-06-21 14:47:26 +00:00
|
|
|
peer->credit = SWIFT_INITIAL_CREDIT;
|
2007-01-21 06:42:05 +00:00
|
|
|
if( s >= 0 )
|
|
|
|
{
|
2007-03-23 08:28:01 +00:00
|
|
|
assert( TR_PEER_FROM_INCOMING == from );
|
2007-01-21 06:42:05 +00:00
|
|
|
peer->status = PEER_STATUS_CONNECTING;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peer->status = PEER_STATUS_IDLE;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
void tr_peerDestroy( tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-01-21 06:42:05 +00:00
|
|
|
tr_request_t * r;
|
|
|
|
int i, block;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
peertreeFree( &peer->sentPeers );
|
2007-01-21 06:42:05 +00:00
|
|
|
for( i = 0; i < peer->inRequestCount; i++ )
|
|
|
|
{
|
|
|
|
r = &peer->inRequests[i];
|
|
|
|
block = tr_block( r->index, r->begin );
|
2007-07-25 16:33:22 +00:00
|
|
|
if( tor != NULL )
|
|
|
|
tr_cpDownloaderRem( tor->completion, block );
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldFree( peer->bitfield );
|
|
|
|
tr_bitfieldFree( peer->blamefield );
|
|
|
|
tr_bitfieldFree( peer->banfield );
|
2007-07-06 16:17:37 +00:00
|
|
|
tr_bitfieldFree( peer->reqfield );
|
2007-07-09 04:37:16 +00:00
|
|
|
tr_list_foreach( peer->outRequests, tr_free );
|
2007-08-14 14:18:54 +00:00
|
|
|
tr_list_free( &peer->outRequests );
|
2007-07-06 15:51:06 +00:00
|
|
|
tr_free( peer->inRequests );
|
2007-06-21 14:47:26 +00:00
|
|
|
tr_free( peer->buf );
|
|
|
|
tr_free( peer->outMessages );
|
2006-07-16 19:39:23 +00:00
|
|
|
if( peer->status > PEER_STATUS_IDLE )
|
|
|
|
{
|
|
|
|
tr_netClose( peer->socket );
|
|
|
|
}
|
|
|
|
tr_rcClose( peer->download );
|
2006-12-18 04:56:27 +00:00
|
|
|
tr_rcClose( peer->upload );
|
2007-04-03 18:22:58 +00:00
|
|
|
free( peer->client );
|
2007-07-25 17:46:00 +00:00
|
|
|
|
|
|
|
memset( peer, '\0', sizeof(tr_peer_t) );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
free( peer );
|
|
|
|
}
|
|
|
|
|
2007-04-03 18:22:58 +00:00
|
|
|
const char *
|
|
|
|
tr_peerClient( tr_peer_t * peer )
|
|
|
|
{
|
2007-04-03 18:43:26 +00:00
|
|
|
if( PEER_STATUS_HANDSHAKE >= peer->status )
|
2007-04-03 18:43:01 +00:00
|
|
|
{
|
|
|
|
return "not connected";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( NULL == peer->client )
|
2007-04-03 18:22:58 +00:00
|
|
|
{
|
|
|
|
peer->client = tr_clientForId( peer->id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer->client;
|
|
|
|
}
|
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
void tr_peerSetPrivate( tr_peer_t * peer, int private )
|
|
|
|
{
|
2007-03-31 19:19:09 +00:00
|
|
|
if( peer->private == private )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
peer->private = private;
|
2007-03-31 19:19:09 +00:00
|
|
|
|
|
|
|
if( !private )
|
|
|
|
{
|
|
|
|
peer->lastPex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( EXTENDED_HANDSHAKE == peer->extStatus )
|
|
|
|
{
|
|
|
|
sendExtended( peer->tor, peer, EXTENDED_HANDSHAKE_ID );
|
|
|
|
}
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_peerSetTorrent( tr_peer_t * peer, tr_torrent * tor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 06:42:05 +00:00
|
|
|
peer->tor = tor;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_peerRead
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-01-21 06:42:05 +00:00
|
|
|
int tr_peerRead( tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2006-07-16 19:39:23 +00:00
|
|
|
int ret;
|
2007-05-25 19:14:42 +00:00
|
|
|
uint64_t date;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Try to read */
|
|
|
|
for( ;; )
|
|
|
|
{
|
2007-01-29 12:16:56 +00:00
|
|
|
if( tor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
int canDL;
|
|
|
|
switch( tor->downloadLimitMode ) {
|
2007-07-20 08:33:59 +00:00
|
|
|
case TR_SPEEDLIMIT_GLOBAL:
|
|
|
|
canDL = !tor->handle->useDownloadLimit ||
|
|
|
|
tr_rcCanTransfer( tor->handle->download ); break;
|
|
|
|
case TR_SPEEDLIMIT_SINGLE:
|
|
|
|
canDL = tr_rcCanTransfer( tor->download ); break;
|
|
|
|
default: /* unlimited */
|
|
|
|
canDL = TRUE;
|
2007-01-29 12:16:56 +00:00
|
|
|
}
|
2007-07-20 03:24:04 +00:00
|
|
|
if( !canDL )
|
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( peer->size < 1 )
|
|
|
|
{
|
|
|
|
peer->size = 1024;
|
|
|
|
peer->buf = malloc( peer->size );
|
|
|
|
}
|
|
|
|
else if( peer->pos >= peer->size )
|
|
|
|
{
|
|
|
|
peer->size *= 2;
|
|
|
|
peer->buf = realloc( peer->buf, peer->size );
|
|
|
|
}
|
2007-07-07 04:13:13 +00:00
|
|
|
|
|
|
|
/* Read in smallish chunks, otherwise we might read more
|
|
|
|
* than the download cap is supposed to allow us */
|
2006-07-16 19:39:23 +00:00
|
|
|
ret = tr_netRecv( peer->socket, &peer->buf[peer->pos],
|
2007-06-29 05:45:17 +00:00
|
|
|
MIN( 1024, peer->size - peer->pos ) );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( ret & TR_NET_CLOSE )
|
|
|
|
{
|
|
|
|
peer_dbg( "connection closed" );
|
2007-01-14 12:00:21 +00:00
|
|
|
return TR_ERROR;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else if( ret & TR_NET_BLOCK )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2007-05-25 19:14:42 +00:00
|
|
|
date = tr_date();
|
|
|
|
peer->date = date;
|
2006-07-16 19:39:23 +00:00
|
|
|
peer->pos += ret;
|
|
|
|
if( NULL != tor )
|
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
if( peer->isInteresting && !peer->isChokingUs )
|
2007-05-25 21:20:54 +00:00
|
|
|
{
|
|
|
|
tor->activityDate = date;
|
|
|
|
}
|
2007-05-16 02:03:18 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
if( ( ret = parseBuf( tor, peer ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
if( ( ret = parseBufHeader( peer ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
return TR_OK;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
uint64_t tr_peerDate( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return peer->date;
|
|
|
|
}
|
|
|
|
|
2006-08-14 22:42:32 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_peerAddress
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
struct in_addr * tr_peerAddress( tr_peer_t * peer )
|
|
|
|
{
|
|
|
|
return &peer->addr;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_peerHash
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-06-21 14:47:26 +00:00
|
|
|
const uint8_t * tr_peerHash( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return parseBufHash( peer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_peerPulse
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-01-21 06:42:05 +00:00
|
|
|
int tr_peerPulse( tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-01-21 06:42:05 +00:00
|
|
|
int ret, size;
|
2006-07-16 19:39:23 +00:00
|
|
|
uint8_t * p;
|
2007-05-25 19:14:42 +00:00
|
|
|
uint64_t date;
|
2007-07-25 17:46:00 +00:00
|
|
|
int isSeeding;
|
|
|
|
|
|
|
|
assert( peer != NULL );
|
|
|
|
assert( peer->tor != NULL );
|
|
|
|
|
|
|
|
isSeeding = tr_cpGetStatus( tor->completion ) != TR_CP_INCOMPLETE;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
if( ( ret = checkPeer( peer ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 06:42:05 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 12:38:50 +00:00
|
|
|
/* Connect */
|
2007-03-23 08:28:01 +00:00
|
|
|
if( PEER_STATUS_IDLE == peer->status )
|
2007-01-21 12:38:50 +00:00
|
|
|
{
|
2007-07-14 16:29:21 +00:00
|
|
|
peer->socket = tr_netOpenTCP( &peer->addr, peer->port, 0 );
|
2007-01-21 12:38:50 +00:00
|
|
|
if( peer->socket < 0 )
|
|
|
|
{
|
|
|
|
return TR_ERROR;
|
|
|
|
}
|
|
|
|
peer->status = PEER_STATUS_CONNECTING;
|
|
|
|
}
|
2007-05-16 03:05:25 +00:00
|
|
|
|
|
|
|
/* Disconnect if seeder and torrent is seeding */
|
2007-06-18 03:40:41 +00:00
|
|
|
if( ( peer->progress >= 1.0 )
|
2007-06-26 18:45:03 +00:00
|
|
|
&& ( peer->tor->cpStatus != TR_CP_INCOMPLETE ) )
|
2007-05-16 03:05:25 +00:00
|
|
|
{
|
|
|
|
return TR_ERROR;
|
|
|
|
}
|
2007-01-21 12:38:50 +00:00
|
|
|
|
|
|
|
/* Try to send handshake */
|
2007-03-23 08:28:01 +00:00
|
|
|
if( PEER_STATUS_CONNECTING == peer->status )
|
2007-01-21 12:38:50 +00:00
|
|
|
{
|
2007-08-15 19:44:13 +00:00
|
|
|
uint8_t buf[HANDSHAKE_SIZE];
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_info * inf;
|
2007-08-15 19:44:13 +00:00
|
|
|
|
|
|
|
inf = tr_torrentInfo( tor );
|
|
|
|
assert( 68 == HANDSHAKE_SIZE );
|
|
|
|
memcpy( buf, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
|
|
|
|
memset( buf + HANDSHAKE_FLAGS_OFF, 0, HANDSHAKE_FLAGS_LEN );
|
|
|
|
HANDSHAKE_SET_EXTMSGS( buf + HANDSHAKE_FLAGS_OFF );
|
|
|
|
HANDSHAKE_SET_AZPROTO( buf + HANDSHAKE_FLAGS_OFF );
|
2007-08-15 21:26:24 +00:00
|
|
|
HANDSHAKE_SET_EXTPREF( buf + HANDSHAKE_FLAGS_OFF,
|
|
|
|
HANDSHAKE_EXTPREF_WANT_EXT );
|
2007-08-15 19:44:13 +00:00
|
|
|
memcpy( buf + HANDSHAKE_HASH_OFF, inf->hash, SHA_DIGEST_LENGTH );
|
2007-08-16 20:00:06 +00:00
|
|
|
memcpy( buf + HANDSHAKE_PEERID_OFF, getPeerId(), TR_ID_LEN );
|
2007-01-21 12:38:50 +00:00
|
|
|
|
|
|
|
switch( tr_netSend( peer->socket, buf, 68 ) )
|
|
|
|
{
|
2007-08-15 19:44:13 +00:00
|
|
|
case HANDSHAKE_SIZE:
|
2007-01-21 12:38:50 +00:00
|
|
|
peer_dbg( "SEND handshake" );
|
|
|
|
peer->status = PEER_STATUS_HANDSHAKE;
|
|
|
|
break;
|
|
|
|
case TR_NET_BLOCK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
peer_dbg( "connection closed" );
|
|
|
|
return TR_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
if( peer->status < PEER_STATUS_HANDSHAKE )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 12:38:50 +00:00
|
|
|
/* Nothing more we can do till we sent the handshake */
|
|
|
|
return TR_OK;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-01-21 12:38:50 +00:00
|
|
|
/* Read incoming messages */
|
2007-01-21 06:42:05 +00:00
|
|
|
if( ( ret = tr_peerRead( peer ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 06:42:05 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-03-23 08:28:01 +00:00
|
|
|
|
|
|
|
/* Try to send Azureus handshake */
|
|
|
|
if( PEER_STATUS_AZ_GIVER == peer->status )
|
|
|
|
{
|
|
|
|
switch( sendAZHandshake( tor, peer ) )
|
|
|
|
{
|
|
|
|
case TR_NET_BLOCK:
|
|
|
|
break;
|
|
|
|
case TR_NET_CLOSE:
|
|
|
|
peer_dbg( "connection closed" );
|
|
|
|
return TR_ERROR;
|
|
|
|
default:
|
|
|
|
peer->status = PEER_STATUS_AZ_RECEIVER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
if( peer->status < PEER_STATUS_CONNECTED )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 12:38:50 +00:00
|
|
|
/* Nothing more we can do till we got the other guy's handshake */
|
2007-01-21 06:42:05 +00:00
|
|
|
return TR_OK;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
/* Try to write */
|
|
|
|
writeBegin:
|
|
|
|
|
|
|
|
/* Send all smaller messages regardless of the upload cap */
|
|
|
|
while( ( p = messagesPending( peer, &size ) ) )
|
|
|
|
{
|
|
|
|
ret = tr_netSend( peer->socket, p, size );
|
|
|
|
if( ret & TR_NET_CLOSE )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 06:42:05 +00:00
|
|
|
return TR_ERROR;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
else if( ret & TR_NET_BLOCK )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 06:42:05 +00:00
|
|
|
goto writeEnd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
messagesSent( peer, ret );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
/* Send pieces if we can */
|
|
|
|
while( ( p = blockPending( tor, peer, &size ) ) )
|
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
int canUL;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-07-20 03:24:04 +00:00
|
|
|
if( SWIFT_ENABLED && !isSeeding && (peer->credit<0) )
|
|
|
|
canUL = FALSE;
|
2007-07-20 08:33:59 +00:00
|
|
|
else switch( tor->uploadLimitMode )
|
|
|
|
{
|
|
|
|
case TR_SPEEDLIMIT_GLOBAL:
|
|
|
|
canUL = !tor->handle->useUploadLimit ||
|
|
|
|
tr_rcCanTransfer( tor->handle->upload ); break;
|
|
|
|
|
|
|
|
case TR_SPEEDLIMIT_SINGLE:
|
|
|
|
canUL = tr_rcCanTransfer( tor->upload ); break;
|
|
|
|
|
|
|
|
default: /* unlimited */
|
|
|
|
canUL = TRUE;
|
2007-07-20 03:24:04 +00:00
|
|
|
}
|
|
|
|
if( !canUL )
|
2007-05-16 02:03:18 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
ret = tr_netSend( peer->socket, p, size );
|
|
|
|
if( ret & TR_NET_CLOSE )
|
|
|
|
return TR_ERROR;
|
2007-07-07 04:13:13 +00:00
|
|
|
|
|
|
|
if( ret & TR_NET_BLOCK )
|
2007-01-21 06:42:05 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
blockSent( peer, ret );
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
if( ret > 0 )
|
|
|
|
tr_peerGotBlockFromUs( peer, ret );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-25 19:14:42 +00:00
|
|
|
date = tr_date();
|
|
|
|
peer->outDate = date;
|
2007-05-25 21:38:24 +00:00
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
if( !tr_peerIsChokedByUs( peer ) )
|
2007-05-25 21:38:24 +00:00
|
|
|
tor->activityDate = date;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
/* In case this block is done, you may have messages
|
|
|
|
pending. Send them before we start the next block */
|
|
|
|
goto writeBegin;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
writeEnd:
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
/* Ask for a block whenever possible */
|
2007-06-21 14:47:26 +00:00
|
|
|
if( !isSeeding
|
2007-07-13 22:22:46 +00:00
|
|
|
&& !peer->isInteresting
|
2007-06-18 03:40:41 +00:00
|
|
|
&& tor->peerCount > TR_MAX_PEER_COUNT - 2 )
|
2007-01-21 06:42:05 +00:00
|
|
|
{
|
|
|
|
/* This peer is no use to us, and it seems there are
|
|
|
|
more */
|
|
|
|
peer_dbg( "not interesting" );
|
|
|
|
return TR_ERROR;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
if( peer->isInteresting
|
|
|
|
&& !peer->isChokingUs
|
2007-06-18 03:40:41 +00:00
|
|
|
&& !peer->banned
|
2007-06-29 05:45:17 +00:00
|
|
|
&& peer->inRequestCount < peer->inRequestMax )
|
2007-01-21 06:42:05 +00:00
|
|
|
{
|
2007-06-29 06:24:55 +00:00
|
|
|
int poolSize = 0;
|
2007-07-24 18:59:37 +00:00
|
|
|
int endgame = FALSE;
|
|
|
|
int openSlots = peer->inRequestMax - peer->inRequestCount;
|
|
|
|
int * pool = getPreferredPieces ( tor, peer, &poolSize, &endgame );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-24 18:59:37 +00:00
|
|
|
if( !endgame )
|
2007-06-29 06:24:55 +00:00
|
|
|
{
|
2007-07-24 18:59:37 +00:00
|
|
|
/* pool is sorted from most to least desirable pieces,
|
|
|
|
so work our way through starting at beginning */
|
|
|
|
int p;
|
|
|
|
for( p=0; p<poolSize && openSlots>0; )
|
2007-07-06 16:17:37 +00:00
|
|
|
{
|
2007-07-24 18:59:37 +00:00
|
|
|
const int piece = pool[p];
|
|
|
|
const int block = tr_cpMissingBlockInPiece ( tor->completion, piece );
|
|
|
|
if( block < 0 )
|
|
|
|
++p;
|
|
|
|
else {
|
|
|
|
sendRequest( tor, peer, block );
|
|
|
|
--openSlots;
|
|
|
|
}
|
2007-06-29 06:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-24 18:59:37 +00:00
|
|
|
else
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-24 18:59:37 +00:00
|
|
|
/* During endgame we remove the constraint of not asking for
|
|
|
|
pieces we've already requested from a different peer.
|
|
|
|
So if we follow the non-endgame approach of walking through
|
|
|
|
[0..poolCount) we'll bog down asking all peers for 1, then
|
|
|
|
all peers for 2, and so on. Randomize our starting point
|
|
|
|
into "pool" to reduce such overlap */
|
|
|
|
int piecesLeft = poolSize;
|
2007-07-26 03:57:45 +00:00
|
|
|
int p = piecesLeft ? tr_rand(piecesLeft) : 0;
|
2007-07-24 18:59:37 +00:00
|
|
|
for( ; openSlots>0 && piecesLeft>0; --piecesLeft, p=(p+1)%poolSize )
|
|
|
|
{
|
|
|
|
const int piece = pool[p];
|
2007-07-28 15:43:34 +00:00
|
|
|
const int firstBlock = tr_torPieceFirstBlock( tor, piece );
|
|
|
|
const int n = tr_torPieceCountBlocks( tor, piece );
|
2007-07-24 18:59:37 +00:00
|
|
|
const int end = firstBlock + n;
|
|
|
|
int block;
|
2007-07-25 18:30:00 +00:00
|
|
|
for( block=firstBlock; openSlots>0 && block<end; ++block )
|
2007-07-24 18:59:37 +00:00
|
|
|
{
|
|
|
|
/* don't ask for it if we've already got it */
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, block ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* don't ask for it twice from the same peer */
|
|
|
|
if( tr_bitfieldHas( peer->reqfield, block ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ask peer for the piece */
|
|
|
|
if( !peer->reqfield )
|
|
|
|
peer->reqfield = tr_bitfieldNew( tor->blockCount );
|
|
|
|
tr_bitfieldAdd( peer->reqfield, block );
|
|
|
|
sendRequest( tor, peer, block );
|
|
|
|
--openSlots;
|
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-29 06:24:55 +00:00
|
|
|
tr_free( pool );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-07-25 19:01:36 +00:00
|
|
|
assert( peer->inRequestCount <= peer->inRequestAlloc );
|
2007-07-25 18:30:00 +00:00
|
|
|
assert( peer->inRequestMax <= peer->inRequestAlloc );
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
return TR_OK;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
int tr_peerIsConnected( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-30 12:46:45 +00:00
|
|
|
return peer && (peer->status == PEER_STATUS_CONNECTED);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_peerGetFrom( const tr_peer_t * peer )
|
2006-09-25 21:56:52 +00:00
|
|
|
{
|
2007-03-23 08:28:01 +00:00
|
|
|
return peer->from;
|
2006-09-25 21:56:52 +00:00
|
|
|
}
|
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
int tr_peerIsChokedByUs( const tr_peer_t * peer )
|
2007-01-21 07:27:57 +00:00
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
return peer->isChokedByUs;
|
2007-01-21 07:27:57 +00:00
|
|
|
}
|
2007-07-13 22:22:46 +00:00
|
|
|
int tr_peerIsInteresting( const tr_peer_t * peer )
|
2007-01-21 07:27:57 +00:00
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
return peer->isInteresting;
|
2007-01-21 07:27:57 +00:00
|
|
|
}
|
2007-07-13 22:22:46 +00:00
|
|
|
int tr_peerIsChokingUs( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
return peer->isChokingUs;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-06-21 14:47:26 +00:00
|
|
|
int tr_peerIsInterested( const tr_peer_t * peer )
|
2007-01-21 07:27:57 +00:00
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
return peer->isInterested;
|
2007-01-21 07:27:57 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
float tr_peerProgress( const tr_peer_t * peer )
|
2006-11-10 04:21:46 +00:00
|
|
|
{
|
|
|
|
return peer->progress;
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
int tr_peerPort( const tr_peer_t * peer )
|
2006-12-18 03:45:22 +00:00
|
|
|
{
|
|
|
|
return ntohs( peer->port );
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
int tr_peerHasPiece( const tr_peer_t * peer, int pieceIndex )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-21 14:47:26 +00:00
|
|
|
return tr_bitfieldHas( peer->bitfield, pieceIndex );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
float tr_peerDownloadRate( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return tr_rcRate( peer->download );
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
float tr_peerUploadRate( const tr_peer_t * peer )
|
2006-12-18 04:56:27 +00:00
|
|
|
{
|
|
|
|
return tr_rcRate( peer->upload );
|
|
|
|
}
|
|
|
|
|
2007-06-29 05:45:17 +00:00
|
|
|
int tr_peerTimesChoked( const tr_peer_t * peer )
|
|
|
|
{
|
|
|
|
return peer->timesChoked;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
void tr_peerChoke( tr_peer_t * peer )
|
|
|
|
{
|
|
|
|
sendChoke( peer, 1 );
|
|
|
|
peer->lastChoke = tr_date();
|
2007-06-29 05:45:17 +00:00
|
|
|
++peer->timesChoked;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerUnchoke( tr_peer_t * peer )
|
|
|
|
{
|
|
|
|
sendChoke( peer, 0 );
|
|
|
|
peer->lastChoke = tr_date();
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
uint64_t tr_peerLastChoke( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return peer->lastChoke;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerSetOptimistic( tr_peer_t * peer, int o )
|
|
|
|
{
|
|
|
|
peer->optimistic = o;
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
int tr_peerIsOptimistic( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return peer->optimistic;
|
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
static int peerIsBad( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-21 14:47:26 +00:00
|
|
|
return peer->badPcs > 4 + 2 * peer->goodPcs;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 14:47:26 +00:00
|
|
|
static int peerIsGood( const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-21 14:47:26 +00:00
|
|
|
return peer->goodPcs > 3 * peer->badPcs;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
void tr_peerBlame( tr_peer_t * peer, int piece, int success )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( !peer->blamefield || !tr_bitfieldHas( peer->blamefield, piece ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
{
|
|
|
|
peer->goodPcs++;
|
|
|
|
|
|
|
|
if( peer->banfield && peerIsGood( peer ) )
|
|
|
|
{
|
|
|
|
/* Assume the peer wasn't responsible for the bad pieces
|
|
|
|
we was banned for */
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldClear( peer->banfield );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peer->badPcs++;
|
|
|
|
|
|
|
|
/* Ban the peer for this piece */
|
|
|
|
if( !peer->banfield )
|
|
|
|
{
|
2007-03-05 23:03:38 +00:00
|
|
|
peer->banfield = tr_bitfieldNew( tor->info.pieceCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
tr_bitfieldAdd( peer->banfield, piece );
|
|
|
|
|
|
|
|
if( peerIsBad( peer ) )
|
|
|
|
{
|
|
|
|
/* Full ban */
|
|
|
|
peer_dbg( "banned (%d / %d)", peer->goodPcs, peer->badPcs );
|
2007-07-13 22:22:46 +00:00
|
|
|
peer->banned = TRUE;
|
|
|
|
peer->isInteresting = FALSE;
|
|
|
|
peer->isInterested = FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tr_bitfieldRem( peer->blamefield, piece );
|
|
|
|
}
|
2006-11-09 04:45:14 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_peerGetConnectable( const tr_torrent * tor, uint8_t ** _buf )
|
2006-11-09 04:45:14 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
2007-01-20 04:29:33 +00:00
|
|
|
uint8_t * buf;
|
2006-11-09 04:45:14 +00:00
|
|
|
tr_peer_t * peer;
|
|
|
|
int i;
|
|
|
|
|
2007-01-20 04:29:33 +00:00
|
|
|
if( tor->peerCount < 1 )
|
|
|
|
{
|
|
|
|
*_buf = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc( 6 * tor->peerCount );
|
2006-11-09 04:45:14 +00:00
|
|
|
for( i = 0; i < tor->peerCount; i++ )
|
|
|
|
{
|
|
|
|
peer = tor->peers[i];
|
|
|
|
|
2007-01-28 00:24:41 +00:00
|
|
|
/* Skip peers with no known listening port */
|
|
|
|
if( 0 == peer->port )
|
2006-11-09 04:45:14 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy( &buf[count*6], &peer->addr, 4 );
|
|
|
|
memcpy( &buf[count*6+4], &peer->port, 2 );
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( count < 1 )
|
|
|
|
{
|
2007-01-20 04:29:33 +00:00
|
|
|
free( buf ); buf = NULL;
|
2006-11-09 04:45:14 +00:00
|
|
|
}
|
2007-01-20 04:29:33 +00:00
|
|
|
*_buf = buf;
|
2006-11-09 04:45:14 +00:00
|
|
|
|
|
|
|
return count * 6;
|
|
|
|
}
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2007-08-21 15:17:02 +00:00
|
|
|
tr_peerPieceIsCorrupt( tr_peer_t * peer, int pieceIndex )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-08-21 15:17:02 +00:00
|
|
|
|
|
|
|
const uint64_t byteCount = tr_torPieceCountBytes( tor, pieceIndex );
|
|
|
|
|
|
|
|
/* increment the `corrupt' field */
|
|
|
|
tor->corruptCur += byteCount;
|
|
|
|
|
|
|
|
/* decrement the `downloaded' field */
|
|
|
|
if( tor->downloadedCur >= byteCount )
|
|
|
|
tor->downloadedCur -= byteCount;
|
|
|
|
else
|
|
|
|
tor->downloadedCur = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerSentBlockToUs( tr_peer_t * peer, int byteCount )
|
2007-06-21 14:47:26 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-07-09 23:09:00 +00:00
|
|
|
assert( byteCount >= 0 );
|
2007-07-09 23:27:57 +00:00
|
|
|
assert( byteCount <= tor->info.pieceSize );
|
2007-07-09 23:09:00 +00:00
|
|
|
|
|
|
|
tor->downloadedCur += byteCount;
|
2007-06-21 14:47:26 +00:00
|
|
|
tr_rcTransferred( peer->download, byteCount );
|
|
|
|
tr_rcTransferred( tor->download, byteCount );
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_rcTransferred( tor->handle->download, byteCount );
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
peer->credit += (int)(byteCount * SWIFT_REPAYMENT_RATIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerGotBlockFromUs ( tr_peer_t * peer, int byteCount )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-07-09 23:09:00 +00:00
|
|
|
assert( byteCount >= 0 );
|
2007-07-09 23:27:57 +00:00
|
|
|
assert( byteCount <= tor->info.pieceSize );
|
2007-07-09 23:09:00 +00:00
|
|
|
|
|
|
|
tor->uploadedCur += byteCount;
|
2007-06-21 14:47:26 +00:00
|
|
|
tr_rcTransferred( peer->upload, byteCount );
|
|
|
|
tr_rcTransferred( tor->upload, byteCount );
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_rcTransferred( tor->handle->upload, byteCount );
|
2007-06-21 14:47:26 +00:00
|
|
|
|
|
|
|
peer->credit -= byteCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSwiftPulse ( tr_torrent * tor )
|
2007-06-21 14:47:26 +00:00
|
|
|
{
|
2007-06-29 17:24:14 +00:00
|
|
|
/* Preferred # of seconds for the request queue's turnaround time.
|
|
|
|
This is just an arbitrary number. */
|
|
|
|
const int queueTimeSec = 5;
|
|
|
|
const int blockSizeKiB = tor->blockSize / 1024;
|
2007-07-09 20:26:16 +00:00
|
|
|
const int isSeeding = tr_cpGetStatus( tor->completion ) != TR_CP_INCOMPLETE;
|
2007-06-29 17:24:14 +00:00
|
|
|
int i;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-06-29 05:45:17 +00:00
|
|
|
for( i=0; i<tor->peerCount; ++i )
|
|
|
|
{
|
2007-06-29 17:24:14 +00:00
|
|
|
double outboundSpeedKiBs;
|
2007-06-29 17:29:02 +00:00
|
|
|
int size;
|
2007-06-29 05:45:17 +00:00
|
|
|
tr_peer_t * peer = tor->peers[ i ];
|
|
|
|
|
|
|
|
if( !tr_peerIsConnected( peer ) )
|
|
|
|
continue;
|
|
|
|
|
2007-06-29 17:24:14 +00:00
|
|
|
/* decide how many blocks we'll concurrently ask this peer for */
|
|
|
|
outboundSpeedKiBs = tr_rcRate(peer->upload);
|
|
|
|
size = queueTimeSec * outboundSpeedKiBs / blockSizeKiB;
|
|
|
|
if( size < 4 ) /* don't let it get TOO small */
|
|
|
|
size = 4;
|
|
|
|
size += 4; /* and always leave room to grow */
|
|
|
|
peer->inRequestMax = size;
|
2007-06-29 05:45:17 +00:00
|
|
|
if( peer->inRequestAlloc < peer->inRequestMax ) {
|
|
|
|
peer->inRequestAlloc = peer->inRequestMax;
|
|
|
|
peer->inRequests = tr_renew( tr_request_t, peer->inRequests, peer->inRequestAlloc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-09 20:26:16 +00:00
|
|
|
/* if we're not seeding, decide on how much
|
|
|
|
bandwidth to allocate for leechers */
|
|
|
|
if( !isSeeding )
|
2007-06-21 14:47:26 +00:00
|
|
|
{
|
2007-07-09 20:26:16 +00:00
|
|
|
tr_peer_t ** deadbeats = tr_new( tr_peer_t*, tor->peerCount );
|
|
|
|
int deadbeatCount = 0;
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-07-09 20:26:16 +00:00
|
|
|
for( i=0; i<tor->peerCount; ++i ) {
|
|
|
|
tr_peer_t * peer = tor->peers[ i ];
|
|
|
|
if( tr_peerIsConnected( peer ) && ( peer->credit < 0 ) )
|
|
|
|
deadbeats[deadbeatCount++] = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( deadbeatCount )
|
|
|
|
{
|
|
|
|
const double ul_KiBsec = tr_rcRate( tor->download );
|
|
|
|
const double ul_KiB = ul_KiBsec * SWIFT_REFRESH_INTERVAL_SEC;
|
|
|
|
const double ul_bytes = ul_KiB * 1024;
|
|
|
|
const double freeCreditTotal = ul_bytes * SWIFT_LARGESSE;
|
|
|
|
const int freeCreditPerPeer = (int)( freeCreditTotal / deadbeatCount );
|
|
|
|
for( i=0; i<deadbeatCount; ++i )
|
|
|
|
deadbeats[i]->credit = freeCreditPerPeer;
|
|
|
|
tr_dbg( "torrent %s has %d deadbeats, "
|
|
|
|
"who are each being granted %d bytes' credit "
|
|
|
|
"for a total of %.1f KiB, "
|
|
|
|
"%d%% of the torrent's ul speed %.1f\n",
|
|
|
|
tor->info.name, deadbeatCount, freeCreditPerPeer,
|
|
|
|
ul_KiBsec*SWIFT_LARGESSE, (int)(SWIFT_LARGESSE*100), ul_KiBsec );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( deadbeats );
|
|
|
|
}
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
2007-06-21 14:47:26 +00:00
|
|
|
}
|
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_swiftPulse( tr_handle * h )
|
2007-06-21 14:47:26 +00:00
|
|
|
{
|
|
|
|
static time_t lastPulseTime = 0;
|
|
|
|
|
2007-06-22 04:30:39 +00:00
|
|
|
if( lastPulseTime + SWIFT_REFRESH_INTERVAL_SEC <= time( NULL ) )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2007-06-22 04:30:39 +00:00
|
|
|
for( tor=h->torrentList; tor; tor=tor->next )
|
|
|
|
tr_torrentSwiftPulse( tor );
|
2007-06-21 14:47:26 +00:00
|
|
|
|
2007-06-22 04:30:39 +00:00
|
|
|
lastPulseTime = time( NULL );
|
|
|
|
}
|
2007-06-21 14:47:26 +00:00
|
|
|
}
|