1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00
transmission/libtransmission/peer.c

752 lines
20 KiB
C
Raw Normal View History

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.
*****************************************************************************/
#include "transmission.h"
2007-03-23 08:28:01 +00:00
#include "peertree.h"
#define MAX_REQUEST_COUNT 32
#define OUR_REQUEST_COUNT 8 /* TODO: we should detect if we are on a
high-speed network and adapt */
#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 */
#define PEER_SUPPORTS_EXTENDED_MESSAGES( bits ) ( (bits)[5] & 0x10 )
#define PEER_SUPPORTS_AZUREUS_PROTOCOL( bits ) ( (bits)[0] & 0x80 )
#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-03-05 23:03:38 +00:00
tr_torrent_t * tor;
2007-03-05 23:03:38 +00:00
struct in_addr addr;
in_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-03-05 23:03:38 +00:00
char amChoking;
char amInterested;
char peerChoking;
char peerInterested;
2006-07-16 19:39:23 +00:00
2007-03-05 23:03:38 +00:00
int optimistic;
uint64_t lastChoke;
2006-07-16 19:39:23 +00:00
uint8_t id[TR_ID_LEN];
2006-07-16 19:39:23 +00:00
/* The pieces that the peer has */
2007-03-05 23:03:38 +00:00
tr_bitfield_t * bitfield;
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-03-05 23:03:38 +00:00
tr_bitfield_t * blamefield;
2006-07-16 19:39:23 +00:00
/* The bad pieces that the peer has contributed to */
2007-03-05 23:03:38 +00:00
tr_bitfield_t * banfield;
uint8_t * buf;
int size;
int pos;
uint8_t * outMessages;
int outMessagesSize;
int outMessagesPos;
uint8_t outBlock[25+16384];
2007-03-05 23:03:38 +00:00
int outBlockSize;
int outBlockLoaded;
int outBlockSending;
int inRequestCount;
tr_request_t inRequests[OUR_REQUEST_COUNT];
int inIndex;
int inBegin;
int inLength;
uint64_t inTotal;
int outRequestCount;
tr_request_t outRequests[MAX_REQUEST_COUNT];
uint64_t outTotal;
uint64_t outDate;
tr_ratecontrol_t * download;
tr_ratecontrol_t * upload;
char * client;
2006-07-16 19:39:23 +00:00
};
#define peer_dbg( a... ) __peer_dbg( peer, ## a )
2006-09-25 18:37:45 +00:00
static void __peer_dbg( tr_peer_t * peer, char * msg, ... ) PRINTF( 2, 3 );
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 ",
2006-07-16 19:39:23 +00:00
(uint32_t) peer->addr.s_addr, peer->port );
vsnprintf( &string[14], sizeof( string ) - 14, msg, args );
va_end( args );
tr_dbg( "%s", string );
}
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-03-23 08:28:01 +00:00
tr_peer_t * tr_peerInit( struct in_addr addr, in_port_t port, int s, int from )
2006-07-16 19:39:23 +00:00
{
tr_peer_t * peer = peerInit();
2007-03-23 08:28:01 +00:00
assert( 0 <= from && TR_PEER_FROM__MAX > from );
peer->socket = s;
peer->addr = addr;
peer->port = port;
2007-03-23 08:28:01 +00:00
peer->from = from;
if( s >= 0 )
{
2007-03-23 08:28:01 +00:00
assert( TR_PEER_FROM_INCOMING == from );
peer->status = PEER_STATUS_CONNECTING;
}
else
{
peer->status = PEER_STATUS_IDLE;
}
2006-07-16 19:39:23 +00:00
return peer;
}
void tr_peerDestroy( tr_peer_t * peer )
2006-07-16 19:39:23 +00:00
{
tr_torrent_t * tor = peer->tor;
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 );
for( i = 0; i < peer->inRequestCount; i++ )
{
r = &peer->inRequests[i];
block = tr_block( r->index, r->begin );
tr_cpDownloaderRem( tor->completion, block );
}
2007-03-05 23:03:38 +00:00
tr_bitfieldFree( peer->bitfield );
tr_bitfieldFree( peer->blamefield );
tr_bitfieldFree( peer->banfield );
2006-07-16 19:39:23 +00:00
if( peer->buf )
{
free( peer->buf );
}
if( peer->outMessages )
{
free( peer->outMessages );
}
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 );
free( peer->client );
2006-07-16 19:39:23 +00:00
free( peer );
}
const char *
tr_peerClient( tr_peer_t * peer )
{
2007-04-03 18:43:26 +00:00
if( PEER_STATUS_HANDSHAKE >= peer->status )
{
return "not connected";
}
if( NULL == peer->client )
{
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 )
{
if( peer->private == private )
{
return;
}
2007-03-23 08:28:01 +00:00
peer->private = private;
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
}
void tr_peerSetTorrent( tr_peer_t * peer, tr_torrent_t * tor )
2006-07-16 19:39:23 +00:00
{
peer->tor = tor;
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
* tr_peerRead
***********************************************************************
*
**********************************************************************/
int tr_peerRead( tr_peer_t * peer )
2006-07-16 19:39:23 +00:00
{
tr_torrent_t * tor = peer->tor;
2006-07-16 19:39:23 +00:00
int ret;
/* Try to read */
for( ;; )
{
if( tor )
2006-07-16 19:39:23 +00:00
{
if( tor->customDownloadLimit )
{
if( !tr_rcCanTransfer( tor->download ) )
{
break;
}
}
else
{
tr_lockUnlock( &tor->lock );
if( !tr_rcCanGlobalTransfer( tor->handle, 0 ) )
{
tr_lockLock( &tor->lock );
break;
}
tr_lockLock( &tor->lock );
}
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 );
}
/* Never read more than 1K each time, otherwise the rate
control is no use */
ret = tr_netRecv( peer->socket, &peer->buf[peer->pos],
MIN( 1024, peer->size - peer->pos ) );
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;
}
peer->date = tr_date();
peer->pos += ret;
if( NULL != tor )
{
tr_rcTransferred( peer->download, ret );
tr_rcTransferred( tor->download, ret );
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
}
}
}
return TR_OK;
2006-07-16 19:39:23 +00:00
}
uint64_t tr_peerDate( tr_peer_t * peer )
{
return peer->date;
}
/***********************************************************************
* tr_peerId
***********************************************************************
*
**********************************************************************/
uint8_t * tr_peerId( tr_peer_t * peer )
{
return & peer->id[0];
}
/***********************************************************************
* tr_peerAddress
***********************************************************************
*
**********************************************************************/
struct in_addr * tr_peerAddress( tr_peer_t * peer )
{
return &peer->addr;
}
2006-07-16 19:39:23 +00:00
/***********************************************************************
* tr_peerHash
***********************************************************************
*
**********************************************************************/
uint8_t * tr_peerHash( tr_peer_t * peer )
{
return parseBufHash( peer );
}
/***********************************************************************
* tr_peerPulse
***********************************************************************
*
**********************************************************************/
int tr_peerPulse( tr_peer_t * peer )
2006-07-16 19:39:23 +00:00
{
tr_torrent_t * tor = peer->tor;
int ret, size;
2006-07-16 19:39:23 +00:00
uint8_t * p;
if( ( ret = checkPeer( peer ) ) )
2006-07-16 19:39:23 +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-01-21 19:42:11 +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;
}
/* 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
{
uint8_t buf[68];
tr_info_t * inf = &tor->info;
buf[0] = 19;
memcpy( &buf[1], "BitTorrent protocol", 19 );
memset( &buf[20], 0, 8 );
2007-03-23 08:28:01 +00:00
buf[20] = 0x80; /* azureus protocol */
buf[25] = 0x10; /* extended messages */
2007-01-21 12:38:50 +00:00
memcpy( &buf[28], inf->hash, 20 );
memcpy( &buf[48], tor->id, 20 );
switch( tr_netSend( peer->socket, buf, 68 ) )
{
case 68:
peer_dbg( "SEND handshake" );
peer->status = PEER_STATUS_HANDSHAKE;
break;
case TR_NET_BLOCK:
break;
default:
peer_dbg( "connection closed" );
return TR_ERROR;
}
}
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 12:38:50 +00:00
/* Read incoming messages */
if( ( ret = tr_peerRead( peer ) ) )
2006-07-16 19:39:23 +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;
}
}
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 */
return TR_OK;
}
2006-07-16 19:39:23 +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
{
return TR_ERROR;
2006-07-16 19:39:23 +00:00
}
else if( ret & TR_NET_BLOCK )
2006-07-16 19:39:23 +00:00
{
goto writeEnd;
2006-07-16 19:39:23 +00:00
}
messagesSent( peer, ret );
}
2006-07-16 19:39:23 +00:00
/* Send pieces if we can */
while( ( p = blockPending( tor, peer, &size ) ) )
{
if( tor->customUploadLimit )
{
if( !tr_rcCanTransfer( tor->upload ) )
{
break;
}
}
else
{
tr_lockUnlock( &tor->lock );
if( !tr_rcCanGlobalTransfer( tor->handle, 1 ) )
{
tr_lockLock( &tor->lock );
break;
}
tr_lockLock( &tor->lock );
}
2006-07-16 19:39:23 +00:00
ret = tr_netSend( peer->socket, p, size );
if( ret & TR_NET_CLOSE )
2006-07-16 19:39:23 +00:00
{
return TR_ERROR;
}
else if( ret & TR_NET_BLOCK )
{
break;
}
2006-07-16 19:39:23 +00:00
blockSent( peer, ret );
tr_rcTransferred( peer->upload, ret );
tr_rcTransferred( tor->upload, ret );
2006-07-16 19:39:23 +00:00
tor->uploadedCur += ret;
peer->outTotal += ret;
peer->outDate = tr_date();
2006-07-16 19:39:23 +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:
/* Ask for a block whenever possible */
if( !tr_cpIsSeeding( tor->completion ) &&
!peer->amInterested && tor->peerCount > TR_MAX_PEER_COUNT - 2 )
{
/* 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
if( peer->amInterested && !peer->peerChoking && !peer->banned )
{
int block;
while( peer->inRequestCount < OUR_REQUEST_COUNT )
2006-07-16 19:39:23 +00:00
{
block = chooseBlock( tor, peer );
if( block < 0 )
2006-07-16 19:39:23 +00:00
{
break;
2006-07-16 19:39:23 +00:00
}
sendRequest( tor, peer, block );
2006-07-16 19:39:23 +00:00
}
}
return TR_OK;
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
* tr_peerIsConnected
***********************************************************************
*
**********************************************************************/
int tr_peerIsConnected( tr_peer_t * peer )
{
2007-03-23 08:28:01 +00:00
return PEER_STATUS_CONNECTED == peer->status;
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
* tr_peerIsIncoming
***********************************************************************
*
**********************************************************************/
2007-03-23 08:28:01 +00:00
int tr_peerIsFrom( tr_peer_t * peer )
{
2007-03-23 08:28:01 +00:00
return peer->from;
}
int tr_peerAmChoking( tr_peer_t * peer )
{
return peer->amChoking;
}
int tr_peerAmInterested( tr_peer_t * peer )
{
return peer->amInterested;
}
int tr_peerIsChoking( tr_peer_t * peer )
2006-07-16 19:39:23 +00:00
{
return peer->peerChoking;
2006-07-16 19:39:23 +00:00
}
int tr_peerIsInterested( tr_peer_t * peer )
{
return peer->peerInterested;
}
2006-07-16 19:39:23 +00:00
2006-11-10 04:21:46 +00:00
/***********************************************************************
2006-12-18 03:45:22 +00:00
* tr_peerProgress
2006-11-10 04:21:46 +00:00
***********************************************************************
*
**********************************************************************/
float tr_peerProgress( tr_peer_t * peer )
{
return peer->progress;
}
2006-12-18 03:45:22 +00:00
/***********************************************************************
* tr_peerPort
***********************************************************************
* Returns peer's listening port in host byte order
**********************************************************************/
int tr_peerPort( tr_peer_t * peer )
{
return ntohs( peer->port );
}
2006-07-16 19:39:23 +00:00
/***********************************************************************
* tr_peerBitfield
***********************************************************************
*
**********************************************************************/
2007-03-05 23:03:38 +00:00
tr_bitfield_t * tr_peerBitfield( tr_peer_t * peer )
2006-07-16 19:39:23 +00:00
{
return peer->bitfield;
}
float tr_peerDownloadRate( tr_peer_t * peer )
{
return tr_rcRate( peer->download );
}
2006-12-18 04:56:27 +00:00
float tr_peerUploadRate( tr_peer_t * peer )
{
return tr_rcRate( peer->upload );
}
2006-07-16 19:39:23 +00:00
void tr_peerChoke( tr_peer_t * peer )
{
sendChoke( peer, 1 );
peer->lastChoke = tr_date();
}
void tr_peerUnchoke( tr_peer_t * peer )
{
sendChoke( peer, 0 );
peer->lastChoke = tr_date();
}
uint64_t tr_peerLastChoke( tr_peer_t * peer )
{
return peer->lastChoke;
}
void tr_peerSetOptimistic( tr_peer_t * peer, int o )
{
peer->optimistic = o;
}
int tr_peerIsOptimistic( tr_peer_t * peer )
{
return peer->optimistic;
}
static inline int peerIsBad( tr_peer_t * peer )
{
return ( peer->badPcs > 4 + 2 * peer->goodPcs );
}
static inline int peerIsGood( tr_peer_t * peer )
{
return ( peer->goodPcs > 3 * peer->badPcs );
}
void tr_peerBlame( tr_peer_t * peer, int piece, int success )
2006-07-16 19:39:23 +00:00
{
tr_torrent_t * tor = peer->tor;
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 );
peer->banned = 1;
peer->peerInterested = 0;
}
}
tr_bitfieldRem( peer->blamefield, piece );
}
int tr_peerGetConnectable( tr_torrent_t * tor, uint8_t ** _buf )
{
int count = 0;
uint8_t * buf;
tr_peer_t * peer;
int i;
if( tor->peerCount < 1 )
{
*_buf = NULL;
return 0;
}
buf = malloc( 6 * tor->peerCount );
for( i = 0; i < tor->peerCount; i++ )
{
peer = tor->peers[i];
/* Skip peers with no known listening port */
if( 0 == peer->port )
continue;
memcpy( &buf[count*6], &peer->addr, 4 );
memcpy( &buf[count*6+4], &peer->port, 2 );
count++;
}
if( count < 1 )
{
free( buf ); buf = NULL;
}
*_buf = buf;
return count * 6;
}