2007-09-20 16:32:01 +00:00
|
|
|
/*
|
2008-05-18 16:44:30 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-09-20 16:32:01 +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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-09-20 16:32:01 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-10-11 03:36:09 +00:00
|
|
|
#include <errno.h>
|
2008-04-19 19:39:39 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <event.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
|
|
|
#include "completion.h"
|
2008-08-16 21:06:57 +00:00
|
|
|
#include "crypto.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "inout.h"
|
2008-10-10 02:14:51 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "net.h" /* for ECONN */
|
|
|
|
#endif
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-io.h"
|
|
|
|
#include "peer-mgr.h"
|
|
|
|
#include "peer-mgr-private.h"
|
|
|
|
#include "peer-msgs.h"
|
|
|
|
#include "ratecontrol.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "stats.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2007-09-26 15:14:37 +00:00
|
|
|
BT_CHOKE = 0,
|
|
|
|
BT_UNCHOKE = 1,
|
|
|
|
BT_INTERESTED = 2,
|
|
|
|
BT_NOT_INTERESTED = 3,
|
|
|
|
BT_HAVE = 4,
|
|
|
|
BT_BITFIELD = 5,
|
|
|
|
BT_REQUEST = 6,
|
|
|
|
BT_PIECE = 7,
|
|
|
|
BT_CANCEL = 8,
|
|
|
|
BT_PORT = 9,
|
|
|
|
BT_SUGGEST = 13,
|
|
|
|
BT_HAVE_ALL = 14,
|
|
|
|
BT_HAVE_NONE = 15,
|
|
|
|
BT_REJECT = 16,
|
|
|
|
BT_ALLOWED_FAST = 17,
|
|
|
|
BT_LTEP = 20,
|
|
|
|
|
|
|
|
LTEP_HANDSHAKE = 0,
|
|
|
|
|
2007-11-22 06:48:08 +00:00
|
|
|
TR_LTEP_PEX = 1,
|
2007-09-26 15:14:37 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
MIN_CHOKE_PERIOD_SEC = ( 10 ),
|
2007-09-26 15:14:37 +00:00
|
|
|
|
2007-11-09 01:22:15 +00:00
|
|
|
/* idle seconds before we send a keepalive */
|
2008-05-31 00:16:26 +00:00
|
|
|
KEEPALIVE_INTERVAL_SECS = 100,
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
PEX_INTERVAL = ( 90 * 1000 ), /* msec between sendPex() calls */
|
|
|
|
PEER_PULSE_INTERVAL = ( 250 ), /* msec between peerPulse() calls
|
|
|
|
*/
|
|
|
|
|
|
|
|
MAX_QUEUE_SIZE = ( 100 ),
|
2007-11-20 17:29:56 +00:00
|
|
|
|
2008-03-01 14:09:18 +00:00
|
|
|
/* (fast peers) max number of pieces we fast-allow to another peer */
|
|
|
|
MAX_FAST_ALLOWED_COUNT = 10,
|
|
|
|
|
|
|
|
/* (fast peers) max threshold for allowing fast-pieces requests */
|
|
|
|
MAX_FAST_ALLOWED_THRESHOLD = 10,
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2008-03-18 17:46:29 +00:00
|
|
|
/* how long an unsent request can stay queued before it's returned
|
|
|
|
back to the peer-mgr's pool of requests */
|
2008-02-10 04:03:19 +00:00
|
|
|
QUEUED_REQUEST_TTL_SECS = 20,
|
|
|
|
|
2008-03-18 17:46:29 +00:00
|
|
|
/* how long a sent request can stay queued before it's returned
|
|
|
|
back to the peer-mgr's pool of requests */
|
2008-08-14 20:17:55 +00:00
|
|
|
SENT_REQUEST_TTL_SECS = 240,
|
2008-05-31 00:16:26 +00:00
|
|
|
|
|
|
|
/* used in lowering the outMessages queue period */
|
|
|
|
IMMEDIATE_PRIORITY_INTERVAL_SECS = 0,
|
2008-07-27 14:10:32 +00:00
|
|
|
HIGH_PRIORITY_INTERVAL_SECS = 2,
|
2008-08-16 21:06:57 +00:00
|
|
|
LOW_PRIORITY_INTERVAL_SECS = 20,
|
|
|
|
|
|
|
|
/* number of pieces to remove from the bitfield when
|
|
|
|
* lazy bitfields are turned on */
|
|
|
|
LAZY_PIECE_COUNT = 26
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
2008-03-01 14:09:18 +00:00
|
|
|
/**
|
|
|
|
*** REQUEST MANAGEMENT
|
|
|
|
**/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
AWAITING_BT_LENGTH,
|
2007-11-16 20:40:03 +00:00
|
|
|
AWAITING_BT_ID,
|
|
|
|
AWAITING_BT_MESSAGE,
|
|
|
|
AWAITING_BT_PIECE
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct peer_request
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t index;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t length;
|
|
|
|
time_t time_requested;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareRequest( const void * va,
|
|
|
|
const void * vb )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-09 01:22:15 +00:00
|
|
|
const struct peer_request * a = va;
|
|
|
|
const struct peer_request * b = vb;
|
2008-08-22 14:52:20 +00:00
|
|
|
|
|
|
|
if( a->index != b->index )
|
|
|
|
return a->index < b->index ? -1 : 1;
|
|
|
|
|
|
|
|
if( a->offset != b->offset )
|
|
|
|
return a->offset < b->offset ? -1 : 1;
|
|
|
|
|
|
|
|
if( a->length != b->length )
|
|
|
|
return a->length < b->length ? -1 : 1;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 03:41:50 +00:00
|
|
|
struct request_list
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint16_t count;
|
|
|
|
uint16_t max;
|
|
|
|
struct peer_request * requests;
|
2008-02-29 03:41:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct request_list REQUEST_LIST_INIT = { 0, 0, NULL };
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListReserve( struct request_list * list,
|
|
|
|
uint16_t max )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
|
|
|
if( list->max < max )
|
|
|
|
{
|
|
|
|
list->max = max;
|
2008-03-01 14:09:18 +00:00
|
|
|
list->requests = tr_renew( struct peer_request,
|
|
|
|
list->requests,
|
|
|
|
list->max );
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reqListClear( struct request_list * list )
|
|
|
|
{
|
|
|
|
tr_free( list->requests );
|
|
|
|
*list = REQUEST_LIST_INIT;
|
|
|
|
}
|
|
|
|
|
2008-03-18 17:46:29 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListCopy( struct request_list * dest,
|
|
|
|
const struct request_list * src )
|
2008-03-18 17:46:29 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
dest->count = dest->max = src->count;
|
2008-09-23 19:11:04 +00:00
|
|
|
dest->requests =
|
|
|
|
tr_memdup( src->requests, dest->count * sizeof( struct peer_request ) );
|
2008-03-18 17:46:29 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 03:41:50 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListRemoveOne( struct request_list * list,
|
|
|
|
int i )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( 0 <= i && i < list->count );
|
2008-02-29 03:41:50 +00:00
|
|
|
|
|
|
|
memmove( &list->requests[i],
|
2008-09-23 19:11:04 +00:00
|
|
|
&list->requests[i + 1],
|
|
|
|
sizeof( struct peer_request ) * ( --list->count - i ) );
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListAppend( struct request_list * list,
|
|
|
|
const struct peer_request * req )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
|
|
|
if( ++list->count >= list->max )
|
|
|
|
reqListReserve( list, list->max + 8 );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
list->requests[list->count - 1] = *req;
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListPop( struct request_list * list,
|
|
|
|
struct peer_request * setme )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
2008-10-02 20:55:05 +00:00
|
|
|
int success;
|
2008-02-29 03:41:50 +00:00
|
|
|
|
|
|
|
if( !list->count )
|
2008-10-02 20:55:05 +00:00
|
|
|
success = FALSE;
|
|
|
|
else {
|
2008-02-29 03:41:50 +00:00
|
|
|
*setme = list->requests[0];
|
|
|
|
reqListRemoveOne( list, 0 );
|
2008-10-02 20:55:05 +00:00
|
|
|
success = TRUE;
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
return success;
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListFind( struct request_list * list,
|
|
|
|
const struct peer_request * key )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
|
|
|
uint16_t i;
|
2008-06-09 22:53:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < list->count; ++i )
|
|
|
|
if( !compareRequest( key, list->requests + i ) )
|
2008-02-29 03:41:50 +00:00
|
|
|
return i;
|
2008-06-09 22:53:45 +00:00
|
|
|
|
2008-02-29 03:41:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
reqListRemove( struct request_list * list,
|
|
|
|
const struct peer_request * key )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
2008-10-02 20:55:05 +00:00
|
|
|
int success;
|
2008-02-29 03:41:50 +00:00
|
|
|
const int i = reqListFind( list, key );
|
|
|
|
|
|
|
|
if( i < 0 )
|
2008-10-02 20:55:05 +00:00
|
|
|
success = FALSE;
|
|
|
|
else {
|
2008-02-29 03:41:50 +00:00
|
|
|
reqListRemoveOne( list, i );
|
2008-10-02 20:55:05 +00:00
|
|
|
success = TRUE;
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
return success;
|
2008-02-29 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
2008-03-01 14:09:18 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
/* this is raw, unchanged data from the peer regarding
|
|
|
|
* the current message that it's sending us. */
|
|
|
|
struct tr_incoming
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t id;
|
|
|
|
uint32_t length; /* includes the +1 for id length */
|
|
|
|
struct peer_request blockReq; /* metadata for incoming blocks */
|
|
|
|
struct evbuffer * block; /* piece data for incoming blocks */
|
2007-11-16 20:40:03 +00:00
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_peermsgs
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
unsigned int peerSentBitfield : 1;
|
|
|
|
unsigned int peerSupportsPex : 1;
|
|
|
|
unsigned int clientSentLtepHandshake : 1;
|
|
|
|
unsigned int peerSentLtepHandshake : 1;
|
|
|
|
unsigned int sendingBlock : 1;
|
|
|
|
|
|
|
|
uint8_t state;
|
|
|
|
uint8_t ut_pex_id;
|
|
|
|
uint16_t pexCount;
|
|
|
|
uint16_t minActiveRequests;
|
|
|
|
uint16_t maxActiveRequests;
|
2008-03-01 14:09:18 +00:00
|
|
|
|
2008-07-27 14:10:32 +00:00
|
|
|
/* how long the outMessages batch should be allowed to grow before
|
|
|
|
* it's flushed -- some messages (like requests >:) should be sent
|
|
|
|
* very quickly; others aren't as urgent. */
|
2008-09-23 19:11:04 +00:00
|
|
|
int outMessagesBatchPeriod;
|
2008-07-27 14:10:32 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peer * info;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_session * session;
|
|
|
|
tr_torrent * torrent;
|
|
|
|
tr_peerIo * io;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_publisher_t * publisher;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
struct evbuffer * outBlock; /* buffer of the current piece message */
|
2008-10-08 03:58:21 +00:00
|
|
|
struct evbuffer * outMessages; /* all the non-piece messages */
|
2008-03-01 14:09:18 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
struct request_list peerAskedFor;
|
|
|
|
struct request_list peerAskedForFast;
|
|
|
|
struct request_list clientAskedFor;
|
|
|
|
struct request_list clientWillAskFor;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_timer * pexTimer;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
time_t clientSentPexAt;
|
|
|
|
time_t clientSentAnythingAt;
|
2008-05-31 00:16:26 +00:00
|
|
|
|
|
|
|
/* when we started batching the outMessages */
|
2008-09-23 19:11:04 +00:00
|
|
|
time_t outMessagesBatchedAt;
|
|
|
|
|
|
|
|
tr_bitfield * peerAllowedPieces;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
struct tr_incoming incoming;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * pex;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
myDebug( const char * file,
|
|
|
|
int line,
|
2007-10-03 16:42:43 +00:00
|
|
|
const struct tr_peermsgs * msgs,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * fmt,
|
|
|
|
... )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-20 23:33:46 +00:00
|
|
|
FILE * fp = tr_getLog( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( fp )
|
2007-09-20 23:33:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
va_list args;
|
|
|
|
char timestr[64];
|
2007-10-22 18:52:36 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base = tr_basename( file );
|
2007-10-22 18:52:36 +00:00
|
|
|
|
2007-11-01 13:47:32 +00:00
|
|
|
evbuffer_add_printf( buf, "[%s] %s - %s [%s]: ",
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getLogTimeStr( timestr, sizeof( timestr ) ),
|
2007-11-01 13:47:32 +00:00
|
|
|
msgs->torrent->info.name,
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_peerIoGetAddrStr( msgs->io ),
|
|
|
|
msgs->info->client );
|
2007-09-20 23:33:46 +00:00
|
|
|
va_start( args, fmt );
|
|
|
|
evbuffer_add_vprintf( buf, fmt, args );
|
|
|
|
va_end( args );
|
2008-10-14 03:39:16 +00:00
|
|
|
evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
|
2008-09-23 19:11:04 +00:00
|
|
|
fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
|
2007-10-22 18:52:36 +00:00
|
|
|
|
2008-10-14 03:39:16 +00:00
|
|
|
tr_free( base );
|
2007-09-20 23:33:46 +00:00
|
|
|
evbuffer_free( buf );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-10-13 22:45:05 +00:00
|
|
|
#define dbgmsg( msgs, ... ) myDebug( __FILE__, __LINE__, msgs, __VA_ARGS__ )
|
2007-10-06 18:20:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-05-31 00:16:26 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
pokeBatchPeriod( tr_peermsgs * msgs,
|
|
|
|
int interval )
|
2008-05-31 00:16:26 +00:00
|
|
|
{
|
|
|
|
if( msgs->outMessagesBatchPeriod > interval )
|
|
|
|
{
|
|
|
|
msgs->outMessagesBatchPeriod = interval;
|
|
|
|
dbgmsg( msgs, "lowering batch interval to %d seconds", interval );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
protocolSendRequest( tr_peermsgs * msgs,
|
|
|
|
const struct peer_request * req )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIo * io = msgs->io;
|
2007-10-06 18:20:52 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_peerIoWriteUint8 ( io, out, BT_REQUEST );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->index );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->offset );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->length );
|
2008-08-16 21:06:57 +00:00
|
|
|
dbgmsg( msgs, "requesting %u:%u->%u... outMessage size is now %d",
|
2008-09-23 19:11:04 +00:00
|
|
|
req->index, req->offset, req->length, (int)EVBUFFER_LENGTH( out ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
protocolSendCancel( tr_peermsgs * msgs,
|
|
|
|
const struct peer_request * req )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIo * io = msgs->io;
|
2007-10-06 18:20:52 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_peerIoWriteUint8 ( io, out, BT_CANCEL );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->index );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->offset );
|
|
|
|
tr_peerIoWriteUint32( io, out, req->length );
|
2008-08-16 21:06:57 +00:00
|
|
|
dbgmsg( msgs, "cancelling %u:%u->%u... outMessage size is now %d",
|
2008-09-23 19:11:04 +00:00
|
|
|
req->index, req->offset, req->length, (int)EVBUFFER_LENGTH( out ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
protocolSendHave( tr_peermsgs * msgs,
|
|
|
|
uint32_t index )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIo * io = msgs->io;
|
2007-10-06 18:20:52 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + sizeof( uint32_t ) );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_peerIoWriteUint8 ( io, out, BT_HAVE );
|
|
|
|
tr_peerIoWriteUint32( io, out, index );
|
2008-08-16 21:06:57 +00:00
|
|
|
dbgmsg( msgs, "sending Have %u.. outMessage size is now %d",
|
2008-09-23 19:11:04 +00:00
|
|
|
index, (int)EVBUFFER_LENGTH( out ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
protocolSendChoke( tr_peermsgs * msgs,
|
|
|
|
int choke )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIo * io = msgs->io;
|
2007-10-06 18:20:52 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE );
|
2008-08-16 21:06:57 +00:00
|
|
|
dbgmsg( msgs, "sending %s... outMessage size is now %d",
|
2008-09-23 19:11:04 +00:00
|
|
|
( choke ? "Choke" : "Unchoke" ),
|
|
|
|
(int)EVBUFFER_LENGTH( out ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** EVENTS
|
|
|
|
**/
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
static const tr_peer_event blankEvent = { 0, 0, 0, 0, 0.0f, 0 };
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
publish( tr_peermsgs * msgs,
|
|
|
|
tr_peer_event * e )
|
2007-09-20 23:07:36 +00:00
|
|
|
{
|
|
|
|
tr_publisherPublish( msgs->publisher, msgs->info, e );
|
|
|
|
}
|
|
|
|
|
2007-11-13 05:36:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
fireError( tr_peermsgs * msgs,
|
2008-10-03 04:49:06 +00:00
|
|
|
int err )
|
2007-11-13 05:36:43 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_ERROR;
|
2008-01-18 19:13:32 +00:00
|
|
|
e.err = err;
|
2007-09-20 23:07:36 +00:00
|
|
|
publish( msgs, &e );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fireNeedReq( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_NEED_REQ;
|
2007-09-20 23:07:36 +00:00
|
|
|
publish( msgs, &e );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
firePeerProgress( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_PEER_PROGRESS;
|
2007-09-20 23:07:36 +00:00
|
|
|
e.progress = msgs->info->progress;
|
|
|
|
publish( msgs, &e );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
fireGotBlock( tr_peermsgs * msgs,
|
|
|
|
const struct peer_request * req )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_CLIENT_GOT_BLOCK;
|
|
|
|
e.pieceIndex = req->index;
|
|
|
|
e.offset = req->offset;
|
|
|
|
e.length = req->length;
|
2007-09-20 23:07:36 +00:00
|
|
|
publish( msgs, &e );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
fireClientGotData( tr_peermsgs * msgs,
|
|
|
|
uint32_t length )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.length = length;
|
|
|
|
e.eventType = TR_PEER_CLIENT_GOT_DATA;
|
2007-09-20 23:07:36 +00:00
|
|
|
publish( msgs, &e );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 18:17:56 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
firePeerGotData( tr_peermsgs * msgs,
|
|
|
|
uint32_t length )
|
2008-01-05 18:17:56 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
e.length = length;
|
|
|
|
e.eventType = TR_PEER_PEER_GOT_DATA;
|
2008-01-05 18:17:56 +00:00
|
|
|
publish( msgs, &e );
|
|
|
|
}
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
static void
|
2008-10-11 04:07:50 +00:00
|
|
|
fireCancelledReq( tr_peermsgs * msgs, const struct peer_request * req )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_peer_event e = blankEvent;
|
|
|
|
e.eventType = TR_PEER_CANCEL;
|
2008-10-11 04:07:50 +00:00
|
|
|
e.pieceIndex = req->index;
|
|
|
|
e.offset = req->offset;
|
|
|
|
e.length = req->length;
|
2007-10-06 18:20:52 +00:00
|
|
|
publish( msgs, &e );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
*** INTEREST
|
|
|
|
**/
|
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
isPieceInteresting( const tr_peermsgs * peer,
|
|
|
|
tr_piece_index_t piece )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
const tr_torrent * torrent = peer->torrent;
|
2008-03-01 14:09:18 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return ( !torrent->info.pieces[piece].dnd ) /* we want it */
|
|
|
|
&& ( !tr_cpPieceIsComplete( torrent->completion, piece ) ) /* !have
|
|
|
|
*/
|
|
|
|
&& ( tr_bitfieldHas( peer->info->have, piece ) ); /* peer has it */
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
/* "interested" means we'll ask for piece data if they unchoke us */
|
2007-09-20 16:32:01 +00:00
|
|
|
static int
|
|
|
|
isPeerInteresting( const tr_peermsgs * msgs )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_piece_index_t i;
|
|
|
|
const tr_torrent * torrent;
|
2007-09-30 23:55:49 +00:00
|
|
|
const tr_bitfield * bitfield;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int clientIsSeed = tr_torrentIsSeed( msgs->torrent );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
if( clientIsSeed )
|
|
|
|
return FALSE;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-11-07 05:37:59 +00:00
|
|
|
if( !tr_torrentIsPieceTransferAllowed( msgs->torrent, TR_PEER_TO_CLIENT ) )
|
2008-11-07 04:10:27 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
torrent = msgs->torrent;
|
|
|
|
bitfield = tr_cpPieceBitfield( torrent->completion );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
if( !msgs->info->have )
|
2007-09-30 23:55:49 +00:00
|
|
|
return TRUE;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
assert( bitfield->byteCount == msgs->info->have->byteCount );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < torrent->info.pieceCount; ++i )
|
2007-09-30 23:55:49 +00:00
|
|
|
if( isPieceInteresting( msgs, i ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
sendInterest( tr_peermsgs * msgs,
|
|
|
|
int weAreInterested )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-08-16 05:11:28 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( weAreInterested == 0 || weAreInterested == 1 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
msgs->info->clientIsInterested = weAreInterested;
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "Sending %s",
|
|
|
|
weAreInterested ? "Interested" : "Not Interested" );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, out, sizeof( uint8_t ) );
|
|
|
|
tr_peerIoWriteUint8 (
|
|
|
|
msgs->io, out, weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH( out ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
updateInterest( tr_peermsgs * msgs )
|
|
|
|
{
|
|
|
|
const int i = isPeerInteresting( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
if( i != msgs->info->clientIsInterested )
|
|
|
|
sendInterest( msgs, i );
|
|
|
|
if( i )
|
|
|
|
fireNeedReq( msgs );
|
|
|
|
}
|
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
static void
|
|
|
|
cancelAllRequestsToClientExceptFast( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-02-29 03:41:50 +00:00
|
|
|
reqListClear( &msgs->peerAskedFor );
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 18:23:59 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerMsgsSetChoke( tr_peermsgs * msgs,
|
|
|
|
int choke )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-10-27 04:12:42 +00:00
|
|
|
const time_t now = time( NULL );
|
|
|
|
const time_t fibrillationTime = now - MIN_CHOKE_PERIOD_SEC;
|
2007-10-15 16:01:42 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
|
|
|
assert( msgs->info );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( choke == 0 || choke == 1 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-15 16:01:42 +00:00
|
|
|
if( msgs->info->chokeChangedAt > fibrillationTime )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "Not changing choke to %d to avoid fibrillation",
|
|
|
|
choke );
|
2007-10-15 16:01:42 +00:00
|
|
|
}
|
|
|
|
else if( msgs->info->peerIsChoked != choke )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-28 16:40:21 +00:00
|
|
|
msgs->info->peerIsChoked = choke;
|
2007-09-28 14:27:56 +00:00
|
|
|
if( choke )
|
2007-10-27 15:45:03 +00:00
|
|
|
cancelAllRequestsToClientExceptFast( msgs );
|
2007-10-06 18:20:52 +00:00
|
|
|
protocolSendChoke( msgs, choke );
|
2008-10-27 04:12:42 +00:00
|
|
|
msgs->info->chokeChangedAt = now;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMsgsHave( tr_peermsgs * msgs,
|
2007-10-06 18:20:52 +00:00
|
|
|
uint32_t index )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-06 18:20:52 +00:00
|
|
|
protocolSendHave( msgs, index );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/* since we have more pieces now, we might not be interested in this peer */
|
2007-09-20 16:32:01 +00:00
|
|
|
updateInterest( msgs );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-28 16:40:21 +00:00
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
sendFastSuggest( tr_peermsgs * msgs,
|
|
|
|
uint32_t pieceIndex )
|
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 16:06:00 +00:00
|
|
|
if( tr_peerIoSupportsFEXT( msgs->io ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, msgs->outMessages,
|
|
|
|
sizeof( uint8_t ) + sizeof( uint32_t ) );
|
2007-11-10 16:06:00 +00:00
|
|
|
tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_SUGGEST );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
|
|
|
|
}
|
2007-09-28 16:40:21 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-28 16:40:21 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
sendFastHave( tr_peermsgs * msgs,
|
|
|
|
int all )
|
2007-09-28 16:40:21 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 16:06:00 +00:00
|
|
|
if( tr_peerIoSupportsFEXT( msgs->io ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof( uint8_t ) );
|
|
|
|
tr_peerIoWriteUint8( msgs->io, msgs->outMessages,
|
|
|
|
( all ? BT_HAVE_ALL
|
|
|
|
: BT_HAVE_NONE ) );
|
2007-11-10 16:06:00 +00:00
|
|
|
updateInterest( msgs );
|
|
|
|
}
|
2007-09-28 16:40:21 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-17 02:11:29 +00:00
|
|
|
#endif
|
2007-09-28 16:40:21 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
sendFastReject( tr_peermsgs * msgs,
|
|
|
|
uint32_t pieceIndex,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
2007-10-27 15:45:03 +00:00
|
|
|
|
|
|
|
if( tr_peerIoSupportsFEXT( msgs->io ) )
|
|
|
|
{
|
2008-08-16 05:11:28 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint32_t len = sizeof( uint8_t ) + 3 * sizeof( uint32_t );
|
|
|
|
dbgmsg( msgs, "sending fast reject %u:%u->%u", pieceIndex, offset,
|
|
|
|
length );
|
2008-08-16 05:11:28 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, out, len );
|
|
|
|
tr_peerIoWriteUint8( msgs->io, out, BT_REJECT );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, out, pieceIndex );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, out, offset );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, out, length );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "outMessage size is now %d",
|
|
|
|
(int)EVBUFFER_LENGTH( out ) );
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
2007-09-28 16:40:21 +00:00
|
|
|
}
|
|
|
|
|
2008-01-06 21:56:30 +00:00
|
|
|
static tr_bitfield*
|
|
|
|
getPeerAllowedPieces( tr_peermsgs * msgs )
|
|
|
|
{
|
|
|
|
if( !msgs->peerAllowedPieces && tr_peerIoSupportsFEXT( msgs->io ) )
|
|
|
|
{
|
|
|
|
msgs->peerAllowedPieces = tr_peerMgrGenerateAllowedSet(
|
|
|
|
MAX_FAST_ALLOWED_COUNT,
|
|
|
|
msgs->torrent->info.pieceCount,
|
|
|
|
msgs->torrent->info.hash,
|
|
|
|
tr_peerIoGetAddress( msgs->io, NULL ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs->peerAllowedPieces;
|
|
|
|
}
|
2007-09-28 16:40:21 +00:00
|
|
|
|
2008-01-06 22:20:44 +00:00
|
|
|
static void
|
|
|
|
sendFastAllowed( tr_peermsgs * msgs,
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t pieceIndex )
|
2008-01-06 22:20:44 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-06 22:20:44 +00:00
|
|
|
if( tr_peerIoSupportsFEXT( msgs->io ) )
|
|
|
|
{
|
2008-08-16 05:11:28 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
|
|
|
dbgmsg( msgs, "sending fast allowed" );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, out, sizeof( uint8_t ) +
|
|
|
|
sizeof( uint32_t ) );
|
2008-08-16 05:11:28 +00:00
|
|
|
tr_peerIoWriteUint8( msgs->io, out, BT_ALLOWED_FAST );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, out, pieceIndex );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "outMessage size is now %d",
|
|
|
|
(int)EVBUFFER_LENGTH( out ) );
|
2008-01-06 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-28 16:40:21 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
sendFastAllowedSet( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i = 0;
|
2008-01-06 21:56:30 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( i <= msgs->torrent->info.pieceCount )
|
2007-09-28 16:40:21 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bitfieldHas( getPeerAllowedPieces( msgs ), i ) )
|
2007-09-28 16:40:21 +00:00
|
|
|
sendFastAllowed( msgs, i );
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 22:20:44 +00:00
|
|
|
static void
|
|
|
|
maybeSendFastAllowedSet( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bitfieldCountTrueBits( msgs->info->have ) <=
|
|
|
|
MAX_FAST_ALLOWED_THRESHOLD )
|
2008-01-06 22:20:44 +00:00
|
|
|
sendFastAllowedSet( msgs );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-25 22:30:41 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
reqIsValid( const tr_peermsgs * peer,
|
|
|
|
uint32_t index,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
2007-09-25 22:30:41 +00:00
|
|
|
{
|
2008-06-09 22:53:45 +00:00
|
|
|
return tr_torrentReqIsValid( peer->torrent, index, offset, length );
|
2007-09-25 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-10-11 04:07:50 +00:00
|
|
|
requestIsValid( const tr_peermsgs * msgs, const struct peer_request * req )
|
2008-06-09 22:53:45 +00:00
|
|
|
{
|
2008-10-11 04:07:50 +00:00
|
|
|
return reqIsValid( msgs, req->index, req->offset, req->length );
|
2007-09-25 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 17:22:51 +00:00
|
|
|
static void
|
2008-10-27 04:12:42 +00:00
|
|
|
expireOldRequests( tr_peermsgs * msgs, const time_t now )
|
2007-12-25 17:22:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
time_t oldestAllowed;
|
2008-03-18 17:46:29 +00:00
|
|
|
struct request_list tmp = REQUEST_LIST_INIT;
|
|
|
|
|
|
|
|
/* cancel requests that have been queued for too long */
|
2008-10-27 04:12:42 +00:00
|
|
|
oldestAllowed = now - QUEUED_REQUEST_TTL_SECS;
|
2008-03-18 17:46:29 +00:00
|
|
|
reqListCopy( &tmp, &msgs->clientWillAskFor );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tmp.count; ++i )
|
|
|
|
{
|
2008-03-18 17:46:29 +00:00
|
|
|
const struct peer_request * req = &tmp.requests[i];
|
|
|
|
if( req->time_requested < oldestAllowed )
|
2008-10-11 04:07:50 +00:00
|
|
|
tr_peerMsgsCancel( msgs, req->index, req->offset, req->length );
|
2007-12-25 17:22:51 +00:00
|
|
|
}
|
2008-03-18 17:46:29 +00:00
|
|
|
reqListClear( &tmp );
|
|
|
|
|
|
|
|
/* cancel requests that were sent too long ago */
|
2008-10-27 04:12:42 +00:00
|
|
|
oldestAllowed = now - SENT_REQUEST_TTL_SECS;
|
2008-03-18 17:46:29 +00:00
|
|
|
reqListCopy( &tmp, &msgs->clientAskedFor );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tmp.count; ++i )
|
|
|
|
{
|
2008-03-18 17:46:29 +00:00
|
|
|
const struct peer_request * req = &tmp.requests[i];
|
|
|
|
if( req->time_requested < oldestAllowed )
|
2008-10-11 04:07:50 +00:00
|
|
|
tr_peerMsgsCancel( msgs, req->index, req->offset, req->length );
|
2008-03-18 17:46:29 +00:00
|
|
|
}
|
|
|
|
reqListClear( &tmp );
|
2007-12-25 17:22:51 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
static void
|
2008-10-27 04:12:42 +00:00
|
|
|
pumpRequestQueue( tr_peermsgs * msgs, const time_t now )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int max = msgs->maxActiveRequests;
|
|
|
|
const int min = msgs->minActiveRequests;
|
|
|
|
int sent = 0;
|
|
|
|
int count = msgs->clientAskedFor.count;
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request req;
|
2007-10-06 18:20:52 +00:00
|
|
|
|
|
|
|
if( count > min )
|
|
|
|
return;
|
|
|
|
if( msgs->info->clientIsChoked )
|
|
|
|
return;
|
2008-11-07 05:37:59 +00:00
|
|
|
if( !tr_torrentIsPieceTransferAllowed( msgs->torrent, TR_PEER_TO_CLIENT ) )
|
2008-11-07 04:10:27 +00:00
|
|
|
return;
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
while( ( count < max ) && reqListPop( &msgs->clientWillAskFor, &req ) )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-10-08 03:58:21 +00:00
|
|
|
const tr_block_index_t block =
|
|
|
|
_tr_block( msgs->torrent, req.index, req.offset );
|
|
|
|
|
2008-02-29 03:41:50 +00:00
|
|
|
assert( requestIsValid( msgs, &req ) );
|
|
|
|
assert( tr_bitfieldHas( msgs->info->have, req.index ) );
|
|
|
|
|
2008-10-08 03:58:21 +00:00
|
|
|
/* don't ask for it if we've already got it... this block may have
|
|
|
|
* come in from a different peer after we cancelled a request for it */
|
|
|
|
if( !tr_cpBlockIsComplete( msgs->torrent->completion, block ) )
|
|
|
|
{
|
|
|
|
protocolSendRequest( msgs, &req );
|
|
|
|
req.time_requested = now;
|
|
|
|
reqListAppend( &msgs->clientAskedFor, &req );
|
2008-02-29 03:41:50 +00:00
|
|
|
|
2008-10-08 03:58:21 +00:00
|
|
|
++count;
|
|
|
|
++sent;
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( sent )
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs,
|
|
|
|
"pump sent %d requests, now have %d active and %d queued",
|
2007-10-06 18:20:52 +00:00
|
|
|
sent,
|
2008-02-29 03:41:50 +00:00
|
|
|
msgs->clientAskedFor.count,
|
|
|
|
msgs->clientWillAskFor.count );
|
2007-10-06 18:20:52 +00:00
|
|
|
|
|
|
|
if( count < max )
|
|
|
|
fireNeedReq( msgs );
|
|
|
|
}
|
|
|
|
|
2008-06-09 22:53:45 +00:00
|
|
|
static int
|
|
|
|
requestQueueIsFull( const tr_peermsgs * msgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-06 18:20:52 +00:00
|
|
|
const int req_max = msgs->maxActiveRequests;
|
2008-06-09 22:53:45 +00:00
|
|
|
return msgs->clientWillAskFor.count >= req_max;
|
|
|
|
}
|
|
|
|
|
2008-08-22 16:13:52 +00:00
|
|
|
tr_addreq_t
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerMsgsAddRequest( tr_peermsgs * msgs,
|
2008-10-11 04:07:50 +00:00
|
|
|
uint32_t index,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
2008-06-09 22:53:45 +00:00
|
|
|
{
|
2008-02-29 03:41:50 +00:00
|
|
|
struct peer_request req;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
|
|
|
assert( msgs->torrent );
|
2008-10-11 04:07:50 +00:00
|
|
|
assert( reqIsValid( msgs, index, offset, length ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/**
|
|
|
|
*** Reasons to decline the request
|
|
|
|
**/
|
|
|
|
|
|
|
|
/* don't send requests to choked clients */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( msgs->info->clientIsChoked )
|
|
|
|
{
|
2007-10-06 18:20:52 +00:00
|
|
|
dbgmsg( msgs, "declining request because they're choking us" );
|
2007-09-20 16:32:01 +00:00
|
|
|
return TR_ADDREQ_CLIENT_CHOKED;
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/* peer doesn't have this piece */
|
2008-10-11 04:07:50 +00:00
|
|
|
if( !tr_bitfieldHas( msgs->info->have, index ) )
|
2007-09-20 16:32:01 +00:00
|
|
|
return TR_ADDREQ_MISSING;
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/* peer's queue is full */
|
2008-10-11 04:07:50 +00:00
|
|
|
if( requestQueueIsFull( msgs ) ) {
|
2007-10-06 18:20:52 +00:00
|
|
|
dbgmsg( msgs, "declining request because we're full" );
|
2007-09-20 16:32:01 +00:00
|
|
|
return TR_ADDREQ_FULL;
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-05 00:16:47 +00:00
|
|
|
/* have we already asked for this piece? */
|
2008-10-11 04:07:50 +00:00
|
|
|
req.index = index;
|
|
|
|
req.offset = offset;
|
|
|
|
req.length = length;
|
|
|
|
if( reqListFind( &msgs->clientAskedFor, &req ) != -1 ) {
|
|
|
|
dbgmsg( msgs, "declining because it's a duplicate" );
|
|
|
|
return TR_ADDREQ_DUPLICATE;
|
|
|
|
}
|
|
|
|
if( reqListFind( &msgs->clientWillAskFor, &req ) != -1 ) {
|
2007-10-06 18:20:52 +00:00
|
|
|
dbgmsg( msgs, "declining because it's a duplicate" );
|
|
|
|
return TR_ADDREQ_DUPLICATE;
|
|
|
|
}
|
2007-10-05 00:16:47 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/**
|
|
|
|
*** Accept this request
|
|
|
|
**/
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-10-11 04:07:50 +00:00
|
|
|
dbgmsg( msgs, "added req for piece %lu", (unsigned long)index );
|
2008-02-29 03:41:50 +00:00
|
|
|
req.time_requested = time( NULL );
|
2008-10-11 04:07:50 +00:00
|
|
|
reqListAppend( &msgs->clientWillAskFor, &req );
|
2007-09-20 16:32:01 +00:00
|
|
|
return TR_ADDREQ_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
static void
|
2007-10-27 15:45:03 +00:00
|
|
|
cancelAllRequestsToPeer( tr_peermsgs * msgs )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
2008-02-29 03:41:50 +00:00
|
|
|
struct request_list a = msgs->clientWillAskFor;
|
|
|
|
struct request_list b = msgs->clientAskedFor;
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2008-02-29 03:41:50 +00:00
|
|
|
msgs->clientAskedFor = REQUEST_LIST_INIT;
|
|
|
|
msgs->clientWillAskFor = REQUEST_LIST_INIT;
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2008-10-11 04:07:50 +00:00
|
|
|
for( i=0; i<a.count; ++i )
|
|
|
|
fireCancelledReq( msgs, &a.requests[i] );
|
2008-02-29 03:41:50 +00:00
|
|
|
|
2008-10-11 04:07:50 +00:00
|
|
|
for( i = 0; i < b.count; ++i ) {
|
|
|
|
fireCancelledReq( msgs, &b.requests[i] );
|
2008-02-29 03:41:50 +00:00
|
|
|
protocolSendCancel( msgs, &b.requests[i] );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
2008-02-29 03:41:50 +00:00
|
|
|
|
|
|
|
reqListClear( &a );
|
|
|
|
reqListClear( &b );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2008-10-11 04:07:50 +00:00
|
|
|
void
|
|
|
|
tr_peerMsgsCancel( tr_peermsgs * msgs,
|
|
|
|
uint32_t pieceIndex,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
|
|
|
{
|
|
|
|
struct peer_request req;
|
|
|
|
|
|
|
|
assert( msgs != NULL );
|
|
|
|
assert( length > 0 );
|
|
|
|
|
|
|
|
/* have we asked the peer for this piece? */
|
|
|
|
req.index = pieceIndex;
|
|
|
|
req.offset = offset;
|
|
|
|
req.length = length;
|
|
|
|
|
|
|
|
/* if it's only in the queue and hasn't been sent yet, free it */
|
2008-10-25 02:20:16 +00:00
|
|
|
if( reqListRemove( &msgs->clientWillAskFor, &req ) )
|
2008-10-11 04:07:50 +00:00
|
|
|
fireCancelledReq( msgs, &req );
|
|
|
|
|
|
|
|
/* if it's already been sent, send a cancel message too */
|
2008-10-25 02:20:16 +00:00
|
|
|
if( reqListRemove( &msgs->clientAskedFor, &req ) ) {
|
2008-10-11 04:07:50 +00:00
|
|
|
protocolSendCancel( msgs, &req );
|
|
|
|
fireCancelledReq( msgs, &req );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
|
|
|
sendLtepHandshake( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc val, *m;
|
|
|
|
char * buf;
|
|
|
|
int len;
|
|
|
|
int pex;
|
2008-08-16 05:11:28 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-09-29 00:46:41 +00:00
|
|
|
if( msgs->clientSentLtepHandshake )
|
|
|
|
return;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
dbgmsg( msgs, "sending an ltep handshake" );
|
2007-09-29 00:46:41 +00:00
|
|
|
msgs->clientSentLtepHandshake = 1;
|
|
|
|
|
|
|
|
/* decide if we want to advertise pex support */
|
2007-12-24 05:03:40 +00:00
|
|
|
if( !tr_torrentAllowsPex( msgs->torrent ) )
|
2007-09-29 00:46:41 +00:00
|
|
|
pex = 0;
|
|
|
|
else if( msgs->peerSentLtepHandshake )
|
|
|
|
pex = msgs->peerSupportsPex ? 1 : 0;
|
|
|
|
else
|
|
|
|
pex = 1;
|
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_bencInitDict( &val, 4 );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddInt( &val, "e",
|
|
|
|
msgs->session->encryptionMode != TR_CLEAR_PREFERRED );
|
2008-08-16 21:06:57 +00:00
|
|
|
tr_bencDictAddInt( &val, "p", tr_sessionGetPeerPort( msgs->session ) );
|
2008-04-17 02:11:29 +00:00
|
|
|
tr_bencDictAddStr( &val, "v", TR_NAME " " USERAGENT_PREFIX );
|
|
|
|
m = tr_bencDictAddDict( &val, "m", 1 );
|
|
|
|
if( pex )
|
|
|
|
tr_bencDictAddInt( m, "ut_pex", TR_LTEP_PEX );
|
2007-11-06 21:14:30 +00:00
|
|
|
buf = tr_bencSave( &val, &len );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, out, 2 * sizeof( uint8_t ) + len );
|
2008-08-16 05:11:28 +00:00
|
|
|
tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
|
|
|
|
tr_peerIoWriteUint8 ( msgs->io, out, LTEP_HANDSHAKE );
|
|
|
|
tr_peerIoWriteBytes ( msgs->io, out, buf, len );
|
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH( out ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_bencFree( &val );
|
|
|
|
tr_free( buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
parseLtepHandshake( tr_peermsgs * msgs,
|
|
|
|
int len,
|
|
|
|
struct evbuffer * inbuf )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t i;
|
|
|
|
tr_benc val, * sub;
|
2007-09-20 16:32:01 +00:00
|
|
|
uint8_t * tmp = tr_new( uint8_t, len );
|
2007-09-22 03:37:37 +00:00
|
|
|
|
|
|
|
tr_peerIoReadBytes( msgs->io, inbuf, tmp, len );
|
2007-09-29 00:46:41 +00:00
|
|
|
msgs->peerSentLtepHandshake = 1;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencLoad( tmp, len, &val, NULL ) || val.type != TYPE_DICT )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
dbgmsg( msgs, "GET extended-handshake, couldn't get dictionary" );
|
|
|
|
tr_free( tmp );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "here is the ltep handshake we got [%*.*s]", len, len,
|
|
|
|
tmp );
|
2007-09-29 00:46:41 +00:00
|
|
|
|
|
|
|
/* does the peer prefer encrypted connections? */
|
2008-08-20 18:42:45 +00:00
|
|
|
if( tr_bencDictFindInt( &val, "e", &i ) )
|
|
|
|
msgs->info->encryption_preference = i ? ENCRYPTION_PREFERENCE_YES
|
2008-09-23 19:11:04 +00:00
|
|
|
: ENCRYPTION_PREFERENCE_NO;
|
2007-09-29 00:46:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* check supported messages for utorrent pex */
|
2008-01-24 17:16:20 +00:00
|
|
|
msgs->peerSupportsPex = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindDict( &val, "m", &sub ) )
|
|
|
|
{
|
|
|
|
if( tr_bencDictFindInt( sub, "ut_pex", &i ) )
|
|
|
|
{
|
2008-08-20 18:42:45 +00:00
|
|
|
msgs->ut_pex_id = (uint8_t) i;
|
2008-01-24 17:16:20 +00:00
|
|
|
msgs->peerSupportsPex = msgs->ut_pex_id == 0 ? 0 : 1;
|
2007-09-20 16:32:01 +00:00
|
|
|
dbgmsg( msgs, "msgs->ut_pex is %d", (int)msgs->ut_pex_id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get peer's listening port */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( &val, "p", &i ) )
|
|
|
|
{
|
2008-08-20 18:42:45 +00:00
|
|
|
msgs->info->port = htons( (uint16_t)i );
|
2007-09-25 17:44:50 +00:00
|
|
|
dbgmsg( msgs, "msgs->port is now %hu", msgs->info->port );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bencFree( &val );
|
|
|
|
tr_free( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
parseUtPex( tr_peermsgs * msgs,
|
|
|
|
int msglen,
|
|
|
|
struct evbuffer * inbuf )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int loaded = 0;
|
|
|
|
uint8_t * tmp = tr_new( uint8_t, msglen );
|
|
|
|
tr_benc val;
|
2008-03-17 02:45:09 +00:00
|
|
|
const tr_torrent * tor = msgs->torrent;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t * added;
|
|
|
|
size_t added_len;
|
2008-08-20 18:42:45 +00:00
|
|
|
|
2007-09-22 03:37:37 +00:00
|
|
|
tr_peerIoReadBytes( msgs->io, inbuf, tmp, msglen );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-03-17 02:45:09 +00:00
|
|
|
if( tr_torrentAllowsPex( tor )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( ( loaded = !tr_bencLoad( tmp, msglen, &val, NULL ) ) )
|
|
|
|
&& tr_bencDictFindRaw( &val, "added", &added, &added_len ) )
|
2008-01-03 03:45:10 +00:00
|
|
|
{
|
2008-08-10 14:58:11 +00:00
|
|
|
const uint8_t * added_f = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * pex;
|
|
|
|
size_t i, n;
|
|
|
|
size_t added_f_len = 0;
|
2008-08-10 14:58:11 +00:00
|
|
|
tr_bencDictFindRaw( &val, "added.f", &added_f, &added_f_len );
|
2008-09-23 19:11:04 +00:00
|
|
|
pex =
|
|
|
|
tr_peerMgrCompactToPex( added, added_len, added_f, added_f_len,
|
|
|
|
&n );
|
|
|
|
for( i = 0; i < n; ++i )
|
2008-08-16 21:06:57 +00:00
|
|
|
tr_peerMgrAddPex( msgs->session->peerMgr, tor->info.hash,
|
2008-09-23 19:11:04 +00:00
|
|
|
TR_PEER_FROM_PEX, pex + i );
|
2008-04-19 15:07:59 +00:00
|
|
|
tr_free( pex );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-03-01 14:09:18 +00:00
|
|
|
if( loaded )
|
2008-01-03 03:45:10 +00:00
|
|
|
tr_bencFree( &val );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( tmp );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static void sendPex( tr_peermsgs * msgs );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
parseLtep( tr_peermsgs * msgs,
|
|
|
|
int msglen,
|
|
|
|
struct evbuffer * inbuf )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint8_t ltep_msgid;
|
|
|
|
|
2007-09-26 14:42:03 +00:00
|
|
|
tr_peerIoReadUint8( msgs->io, inbuf, <ep_msgid );
|
2007-09-20 16:32:01 +00:00
|
|
|
msglen--;
|
|
|
|
|
|
|
|
if( ltep_msgid == LTEP_HANDSHAKE )
|
|
|
|
{
|
|
|
|
dbgmsg( msgs, "got ltep handshake" );
|
|
|
|
parseLtepHandshake( msgs, msglen, inbuf );
|
2007-11-16 04:42:51 +00:00
|
|
|
if( tr_peerIoSupportsLTEP( msgs->io ) )
|
|
|
|
{
|
|
|
|
sendLtepHandshake( msgs );
|
|
|
|
sendPex( msgs );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-11-22 06:48:08 +00:00
|
|
|
else if( ltep_msgid == TR_LTEP_PEX )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
dbgmsg( msgs, "got ut pex" );
|
|
|
|
msgs->peerSupportsPex = 1;
|
|
|
|
parseUtPex( msgs, msglen, inbuf );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbgmsg( msgs, "skipping unknown ltep message (%d)", (int)ltep_msgid );
|
|
|
|
evbuffer_drain( inbuf, msglen );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
readBtLength( tr_peermsgs * msgs,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t inlen )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint32_t len;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( inlen < sizeof( len ) )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &len );
|
|
|
|
|
2007-09-26 01:55:04 +00:00
|
|
|
if( len == 0 ) /* peer sent us a keepalive message */
|
2007-10-06 18:20:52 +00:00
|
|
|
dbgmsg( msgs, "got KeepAlive" );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2007-11-16 20:40:03 +00:00
|
|
|
msgs->incoming.length = len;
|
|
|
|
msgs->state = AWAITING_BT_ID;
|
2007-09-26 01:55:04 +00:00
|
|
|
}
|
|
|
|
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static int readBtMessage( tr_peermsgs * msgs,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t inlen );
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
readBtId( tr_peermsgs * msgs,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t inlen )
|
2007-11-16 20:40:03 +00:00
|
|
|
{
|
|
|
|
uint8_t id;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( inlen < sizeof( uint8_t ) )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_LATER;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
tr_peerIoReadUint8( msgs->io, inbuf, &id );
|
|
|
|
msgs->incoming.id = id;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( id == BT_PIECE )
|
2007-11-16 20:40:03 +00:00
|
|
|
{
|
|
|
|
msgs->state = AWAITING_BT_PIECE;
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2007-11-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
else if( msgs->incoming.length != 1 )
|
|
|
|
{
|
|
|
|
msgs->state = AWAITING_BT_MESSAGE;
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2007-11-16 20:40:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else return readBtMessage( msgs, inbuf, inlen - 1 );
|
2007-11-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
static void
|
|
|
|
updatePeerProgress( tr_peermsgs * msgs )
|
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have )
|
2008-09-23 19:11:04 +00:00
|
|
|
/ (float)msgs->torrent->info.pieceCount;
|
2007-10-03 16:42:43 +00:00
|
|
|
dbgmsg( msgs, "peer progress is %f", msgs->info->progress );
|
|
|
|
updateInterest( msgs );
|
|
|
|
firePeerProgress( msgs );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int
|
2007-10-27 15:45:03 +00:00
|
|
|
clientCanSendFastBlock( const tr_peermsgs * msgs UNUSED )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
/* don't send a fast piece if peer has MAX_FAST_ALLOWED_THRESHOLD pieces */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bitfieldCountTrueBits( msgs->info->have ) >
|
|
|
|
MAX_FAST_ALLOWED_THRESHOLD )
|
2007-11-10 16:06:00 +00:00
|
|
|
return FALSE;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 16:06:00 +00:00
|
|
|
/* ...or if we don't have ourself enough pieces */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bitfieldCountTrueBits( tr_cpPieceBitfield( msgs->torrent->
|
|
|
|
completion ) ) <
|
|
|
|
MAX_FAST_ALLOWED_THRESHOLD )
|
2007-11-10 16:06:00 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Maybe a bandwidth limit ? */
|
|
|
|
return TRUE;
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
peerMadeRequest( tr_peermsgs * msgs,
|
|
|
|
const struct peer_request * req )
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
|
|
|
const int reqIsValid = requestIsValid( msgs, req );
|
2008-09-23 19:11:04 +00:00
|
|
|
const int clientHasPiece = reqIsValid && tr_cpPieceIsComplete(
|
|
|
|
msgs->torrent->completion, req->index );
|
2007-10-27 15:45:03 +00:00
|
|
|
const int peerIsChoked = msgs->info->peerIsChoked;
|
|
|
|
const int peerIsFast = tr_peerIoSupportsFEXT( msgs->io );
|
2008-09-23 19:11:04 +00:00
|
|
|
const int pieceIsFast = reqIsValid && tr_bitfieldHas(
|
|
|
|
getPeerAllowedPieces( msgs ), req->index );
|
2007-10-27 15:45:03 +00:00
|
|
|
const int canSendFast = clientCanSendFastBlock( msgs );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
if( !reqIsValid ) /* bad request */
|
|
|
|
{
|
|
|
|
dbgmsg( msgs, "rejecting an invalid request." );
|
|
|
|
sendFastReject( msgs, req->index, req->offset, req->length );
|
|
|
|
}
|
|
|
|
else if( !clientHasPiece ) /* we don't have it */
|
|
|
|
{
|
|
|
|
dbgmsg( msgs, "rejecting request for a piece we don't have." );
|
|
|
|
sendFastReject( msgs, req->index, req->offset, req->length );
|
|
|
|
}
|
2007-11-09 01:22:15 +00:00
|
|
|
else if( peerIsChoked && !peerIsFast ) /* doesn't he know he's choked? */
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
|
|
|
tr_peerMsgsSetChoke( msgs, 1 );
|
|
|
|
sendFastReject( msgs, req->index, req->offset, req->length );
|
|
|
|
}
|
|
|
|
else if( peerIsChoked && peerIsFast && ( !pieceIsFast || !canSendFast ) )
|
|
|
|
{
|
|
|
|
sendFastReject( msgs, req->index, req->offset, req->length );
|
|
|
|
}
|
|
|
|
else /* YAY */
|
|
|
|
{
|
|
|
|
if( peerIsFast && pieceIsFast )
|
2008-02-29 03:41:50 +00:00
|
|
|
reqListAppend( &msgs->peerAskedForFast, req );
|
2007-10-27 15:45:03 +00:00
|
|
|
else
|
2008-02-29 03:41:50 +00:00
|
|
|
reqListAppend( &msgs->peerAskedFor, req );
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
messageLengthIsCorrect( const tr_peermsgs * msg,
|
|
|
|
uint8_t id,
|
|
|
|
uint32_t len )
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case BT_CHOKE:
|
|
|
|
case BT_UNCHOKE:
|
|
|
|
case BT_INTERESTED:
|
|
|
|
case BT_NOT_INTERESTED:
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_HAVE_ALL:
|
|
|
|
case BT_HAVE_NONE:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len == 1;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
case BT_HAVE:
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_SUGGEST:
|
|
|
|
case BT_ALLOWED_FAST:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len == 5;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_BITFIELD:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len == ( msg->torrent->info.pieceCount + 7u ) / 8u + 1u;
|
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_REQUEST:
|
|
|
|
case BT_CANCEL:
|
|
|
|
case BT_REJECT:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len == 13;
|
2007-09-28 16:40:21 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_PIECE:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len > 9 && len <= 16393;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
case BT_PORT:
|
2008-09-23 19:11:04 +00:00
|
|
|
return len == 3;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
case BT_LTEP:
|
2007-10-27 15:45:03 +00:00
|
|
|
return len >= 2;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
default:
|
2007-10-27 15:45:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static int clientGotBlock( tr_peermsgs * msgs,
|
|
|
|
const uint8_t * block,
|
|
|
|
const struct peer_request * req );
|
2007-10-27 15:45:03 +00:00
|
|
|
|
2007-11-17 23:43:33 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
clientGotBytes( tr_peermsgs * msgs,
|
|
|
|
uint32_t byteCount )
|
2007-11-17 23:43:33 +00:00
|
|
|
{
|
2008-06-07 21:26:41 +00:00
|
|
|
msgs->info->pieceDataActivityDate = time( NULL );
|
|
|
|
fireClientGotData( msgs, byteCount );
|
2007-11-17 23:43:33 +00:00
|
|
|
}
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
readBtPiece( tr_peermsgs * msgs,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t inlen )
|
2007-11-16 20:40:03 +00:00
|
|
|
{
|
|
|
|
struct peer_request * req = &msgs->incoming.blockReq;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
assert( EVBUFFER_LENGTH( inbuf ) >= inlen );
|
2007-11-16 20:40:03 +00:00
|
|
|
dbgmsg( msgs, "In readBtPiece" );
|
|
|
|
|
|
|
|
if( !req->length )
|
|
|
|
{
|
2007-11-16 21:58:11 +00:00
|
|
|
if( inlen < 8 )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_LATER;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &req->index );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &req->offset );
|
|
|
|
req->length = msgs->incoming.length - 9;
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "got incoming block header %u:%u->%u", req->index,
|
|
|
|
req->offset,
|
|
|
|
req->length );
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2007-11-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
/* read in another chunk of data */
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t nLeft = req->length - EVBUFFER_LENGTH(
|
|
|
|
msgs->incoming.block );
|
|
|
|
size_t n = MIN( nLeft, inlen );
|
|
|
|
uint8_t * buf = tr_new( uint8_t, n );
|
|
|
|
assert( EVBUFFER_LENGTH( inbuf ) >= n );
|
2007-11-16 20:40:03 +00:00
|
|
|
tr_peerIoReadBytes( msgs->io, inbuf, buf, n );
|
|
|
|
evbuffer_add( msgs->incoming.block, buf, n );
|
2007-11-17 23:43:33 +00:00
|
|
|
clientGotBytes( msgs, n );
|
2007-11-16 20:40:03 +00:00
|
|
|
tr_free( buf );
|
|
|
|
dbgmsg( msgs, "got %d bytes for block %u:%u->%u ... %d remain",
|
|
|
|
(int)n, req->index, req->offset, req->length,
|
2008-09-23 19:11:04 +00:00
|
|
|
(int)( req->length - EVBUFFER_LENGTH( msgs->incoming.block ) ) );
|
|
|
|
if( EVBUFFER_LENGTH( msgs->incoming.block ) < req->length )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_LATER;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
/* we've got the whole block ... process it */
|
2008-09-23 19:11:04 +00:00
|
|
|
err = clientGotBlock( msgs, EVBUFFER_DATA(
|
|
|
|
msgs->incoming.block ), req );
|
2007-11-16 20:40:03 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2008-09-23 19:11:04 +00:00
|
|
|
evbuffer_drain( msgs->incoming.block,
|
|
|
|
EVBUFFER_LENGTH( msgs->incoming.block ) );
|
2007-11-16 20:40:03 +00:00
|
|
|
req->length = 0;
|
|
|
|
msgs->state = AWAITING_BT_LENGTH;
|
|
|
|
if( !err )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
fireError( msgs, err );
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_ERR;
|
2007-11-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-27 15:45:03 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
readBtMessage( tr_peermsgs * msgs,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t inlen )
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t ui32;
|
|
|
|
uint32_t msglen = msgs->incoming.length;
|
2007-11-16 20:40:03 +00:00
|
|
|
const uint8_t id = msgs->incoming.id;
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t startBufLen = EVBUFFER_LENGTH( inbuf );
|
2007-10-27 15:45:03 +00:00
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
--msglen; /* id length */
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
if( inlen < msglen )
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_LATER;
|
2007-10-27 15:45:03 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "got BT id %d, len %d, buffer size is %d", (int)id,
|
|
|
|
(int)msglen,
|
|
|
|
(int)inlen );
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !messageLengthIsCorrect( msgs, id, msglen + 1 ) )
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "bad packet - BT message #%d with a length of %d",
|
|
|
|
(int)id, (int)msglen );
|
2008-10-10 00:38:37 +00:00
|
|
|
fireError( msgs, EMSGSIZE );
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_ERR;
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( id )
|
|
|
|
{
|
2007-11-10 04:56:27 +00:00
|
|
|
case BT_CHOKE:
|
|
|
|
dbgmsg( msgs, "got Choke" );
|
|
|
|
msgs->info->clientIsChoked = 1;
|
|
|
|
cancelAllRequestsToPeer( msgs );
|
|
|
|
cancelAllRequestsToClientExceptFast( msgs );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_UNCHOKE:
|
|
|
|
dbgmsg( msgs, "got Unchoke" );
|
|
|
|
msgs->info->clientIsChoked = 0;
|
|
|
|
fireNeedReq( msgs );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_INTERESTED:
|
|
|
|
dbgmsg( msgs, "got Interested" );
|
|
|
|
msgs->info->peerIsInterested = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_NOT_INTERESTED:
|
|
|
|
dbgmsg( msgs, "got Not Interested" );
|
|
|
|
msgs->info->peerIsInterested = 0;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 04:56:27 +00:00
|
|
|
case BT_HAVE:
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
|
|
|
|
dbgmsg( msgs, "got Have: %u", ui32 );
|
2008-03-02 23:44:34 +00:00
|
|
|
if( tr_bitfieldAdd( msgs->info->have, ui32 ) )
|
2008-10-03 04:49:06 +00:00
|
|
|
fireError( msgs, ERANGE );
|
2007-11-10 04:56:27 +00:00
|
|
|
updatePeerProgress( msgs );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rcTransferred( msgs->torrent->swarmSpeed,
|
|
|
|
msgs->torrent->info.pieceSize );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case BT_BITFIELD:
|
|
|
|
{
|
2007-11-10 04:56:27 +00:00
|
|
|
dbgmsg( msgs, "got a bitfield" );
|
2007-11-17 23:43:33 +00:00
|
|
|
msgs->peerSentBitfield = 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits,
|
|
|
|
msglen );
|
2007-11-10 04:56:27 +00:00
|
|
|
updatePeerProgress( msgs );
|
2008-01-06 22:20:44 +00:00
|
|
|
maybeSendFastAllowedSet( msgs );
|
2007-11-10 04:56:27 +00:00
|
|
|
fireNeedReq( msgs );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case BT_REQUEST:
|
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "got Request: %u:%u->%u", r.index, r.offset,
|
|
|
|
r.length );
|
2008-03-01 14:09:18 +00:00
|
|
|
peerMadeRequest( msgs, &r );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case BT_CANCEL:
|
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "got a Cancel %u:%u->%u", r.index, r.offset,
|
|
|
|
r.length );
|
2008-03-01 14:09:18 +00:00
|
|
|
reqListRemove( &msgs->peerAskedForFast, &r );
|
|
|
|
reqListRemove( &msgs->peerAskedFor, &r );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
case BT_PIECE:
|
2008-03-01 14:09:18 +00:00
|
|
|
assert( 0 ); /* handled elsewhere! */
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 04:56:27 +00:00
|
|
|
case BT_PORT:
|
|
|
|
dbgmsg( msgs, "Got a BT_PORT" );
|
|
|
|
tr_peerIoReadUint16( msgs->io, inbuf, &msgs->info->port );
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
case BT_SUGGEST:
|
|
|
|
{
|
2007-11-10 16:06:00 +00:00
|
|
|
dbgmsg( msgs, "Got a BT_SUGGEST" );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
|
2008-03-04 21:09:26 +00:00
|
|
|
/* we don't do anything with this yet */
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-10 04:56:27 +00:00
|
|
|
case BT_HAVE_ALL:
|
|
|
|
dbgmsg( msgs, "Got a BT_HAVE_ALL" );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldAddRange( msgs->info->have, 0,
|
|
|
|
msgs->torrent->info.pieceCount );
|
2007-11-10 04:56:27 +00:00
|
|
|
updatePeerProgress( msgs );
|
2008-01-06 22:20:44 +00:00
|
|
|
maybeSendFastAllowedSet( msgs );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
|
2007-11-10 04:56:27 +00:00
|
|
|
case BT_HAVE_NONE:
|
|
|
|
dbgmsg( msgs, "Got a BT_HAVE_NONE" );
|
|
|
|
tr_bitfieldClear( msgs->info->have );
|
|
|
|
updatePeerProgress( msgs );
|
2008-01-06 22:20:44 +00:00
|
|
|
maybeSendFastAllowedSet( msgs );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
case BT_REJECT:
|
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
2007-11-10 04:56:27 +00:00
|
|
|
dbgmsg( msgs, "Got a BT_REJECT" );
|
2008-03-01 14:09:18 +00:00
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
|
|
|
|
reqListRemove( &msgs->clientAskedFor, &r );
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case BT_ALLOWED_FAST:
|
|
|
|
{
|
2007-11-10 04:56:27 +00:00
|
|
|
dbgmsg( msgs, "Got a BT_ALLOWED_FAST" );
|
|
|
|
tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
|
2008-03-04 21:09:26 +00:00
|
|
|
/* we don't do anything with this yet */
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BT_LTEP:
|
|
|
|
dbgmsg( msgs, "Got a BT_LTEP" );
|
|
|
|
parseLtep( msgs, msglen, inbuf );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dbgmsg( msgs, "peer sent us an UNKNOWN: %d", (int)id );
|
|
|
|
tr_peerIoDrain( msgs->io, inbuf, msglen );
|
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
assert( msglen + 1 == msgs->incoming.length );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( EVBUFFER_LENGTH( inbuf ) == startBufLen - msglen );
|
2007-11-13 05:36:43 +00:00
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
msgs->state = AWAITING_BT_LENGTH;
|
2008-09-19 17:03:25 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
peerGotBytes( tr_peermsgs * msgs,
|
2008-10-27 04:12:42 +00:00
|
|
|
uint32_t byteCount,
|
|
|
|
const time_t now )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-10-27 04:12:42 +00:00
|
|
|
msgs->info->pieceDataActivityDate = now;
|
2008-06-07 21:26:41 +00:00
|
|
|
firePeerGotData( msgs, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-03-18 17:46:29 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
decrementDownloadedCount( tr_peermsgs * msgs,
|
|
|
|
uint32_t byteCount )
|
2008-03-18 17:46:29 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = msgs->torrent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-18 17:46:29 +00:00
|
|
|
tor->downloadedCur -= MIN( tor->downloadedCur, byteCount );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
clientGotUnwantedBlock( tr_peermsgs * msgs,
|
|
|
|
const struct peer_request * req )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-03-18 17:46:29 +00:00
|
|
|
decrementDownloadedCount( msgs, req->length );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addPeerToBlamefield( tr_peermsgs * msgs,
|
|
|
|
uint32_t index )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
if( !msgs->info->blame )
|
2008-09-23 19:11:04 +00:00
|
|
|
msgs->info->blame = tr_bitfieldNew( msgs->torrent->info.pieceCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldAdd( msgs->info->blame, index );
|
|
|
|
}
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/* returns 0 on success, or an errno on failure */
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
clientGotBlock( tr_peermsgs * msgs,
|
|
|
|
const uint8_t * data,
|
|
|
|
const struct peer_request * req )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
tr_torrent * tor = msgs->torrent;
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_block_index_t block = _tr_block( tor, req->index, req->offset );
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( msgs );
|
|
|
|
assert( req );
|
2007-11-13 05:36:43 +00:00
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
if( req->length != tr_torBlockCountBytes( msgs->torrent, block ) )
|
2007-11-13 05:36:43 +00:00
|
|
|
{
|
|
|
|
dbgmsg( msgs, "wrong block size -- expected %u, got %d",
|
|
|
|
tr_torBlockCountBytes( msgs->torrent, block ), req->length );
|
2008-10-03 04:49:06 +00:00
|
|
|
return EMSGSIZE;
|
2007-11-13 05:36:43 +00:00
|
|
|
}
|
2007-11-09 01:22:15 +00:00
|
|
|
|
|
|
|
/* save the block */
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "got block %u:%u->%u", req->index, req->offset,
|
|
|
|
req->length );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** Remove the block from our `we asked for this' list
|
|
|
|
**/
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
if( !reqListRemove( &msgs->clientAskedFor, req ) )
|
2008-02-29 03:41:50 +00:00
|
|
|
{
|
2007-11-09 01:22:15 +00:00
|
|
|
clientGotUnwantedBlock( msgs, req );
|
2007-09-20 16:32:01 +00:00
|
|
|
dbgmsg( msgs, "we didn't ask for this message..." );
|
2007-11-13 05:36:43 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
dbgmsg( msgs, "peer has %d more blocks we've asked for",
|
2008-09-23 19:11:04 +00:00
|
|
|
msgs->clientAskedFor.count );
|
2007-11-16 15:45:26 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
*** Error checks
|
|
|
|
**/
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_cpBlockIsComplete( tor->completion, block ) )
|
|
|
|
{
|
2007-11-09 01:22:15 +00:00
|
|
|
dbgmsg( msgs, "we have this block already..." );
|
|
|
|
clientGotUnwantedBlock( msgs, req );
|
2007-11-13 05:36:43 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-09 01:22:15 +00:00
|
|
|
*** Save the block
|
2007-09-20 16:32:01 +00:00
|
|
|
**/
|
|
|
|
|
2007-11-09 01:22:15 +00:00
|
|
|
msgs->info->peerSentPieceDataAt = time( NULL );
|
2008-10-03 04:49:06 +00:00
|
|
|
if(( err = tr_ioWrite( tor, req->index, req->offset, req->length, data )))
|
2008-01-18 19:13:32 +00:00
|
|
|
return err;
|
2007-10-31 04:23:51 +00:00
|
|
|
|
2007-11-09 01:22:15 +00:00
|
|
|
addPeerToBlamefield( msgs, req->index );
|
|
|
|
fireGotBlock( msgs, req );
|
2007-11-13 05:36:43 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ReadState
|
2008-09-23 19:11:04 +00:00
|
|
|
canRead( struct bufferevent * evin,
|
|
|
|
void * vmsgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
ReadState ret;
|
|
|
|
tr_peermsgs * msgs = vmsgs;
|
2008-02-29 17:09:33 +00:00
|
|
|
struct evbuffer * in = EVBUFFER_INPUT ( evin );
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t inlen = EVBUFFER_LENGTH( in );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-02-29 17:09:33 +00:00
|
|
|
if( !inlen )
|
2007-09-22 05:16:32 +00:00
|
|
|
{
|
2008-09-19 17:03:25 +00:00
|
|
|
ret = READ_LATER;
|
2007-09-22 05:16:32 +00:00
|
|
|
}
|
2008-02-29 17:09:33 +00:00
|
|
|
else if( msgs->state == AWAITING_BT_PIECE )
|
|
|
|
{
|
2008-09-19 17:03:25 +00:00
|
|
|
ret = inlen ? readBtPiece( msgs, in, inlen ) : READ_LATER;
|
2008-02-29 17:09:33 +00:00
|
|
|
}
|
2007-09-22 05:16:32 +00:00
|
|
|
else switch( msgs->state )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
case AWAITING_BT_LENGTH:
|
|
|
|
ret = readBtLength ( msgs, in, inlen ); break;
|
|
|
|
|
|
|
|
case AWAITING_BT_ID:
|
|
|
|
ret = readBtId ( msgs, in, inlen ); break;
|
|
|
|
|
|
|
|
case AWAITING_BT_MESSAGE:
|
|
|
|
ret = readBtMessage( msgs, in, inlen ); break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
2007-09-22 05:16:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-26 17:34:33 +00:00
|
|
|
static void
|
|
|
|
sendKeepalive( tr_peermsgs * msgs )
|
|
|
|
{
|
|
|
|
dbgmsg( msgs, "sending a keepalive message" );
|
|
|
|
tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 0 );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2007-09-26 17:34:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-06-09 22:53:45 +00:00
|
|
|
static int
|
|
|
|
ratePulse( void * vpeer )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2008-06-09 22:53:45 +00:00
|
|
|
tr_peermsgs * peer = vpeer;
|
2008-11-06 02:56:51 +00:00
|
|
|
const double rateToClient = tr_peerGetPieceSpeed( peer->info,
|
|
|
|
TR_PEER_TO_CLIENT );
|
|
|
|
const int estimatedBlocksInNext30Seconds =
|
|
|
|
( rateToClient * 30 * 1024 ) / peer->torrent->blockSize;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-09 22:53:45 +00:00
|
|
|
peer->minActiveRequests = 4;
|
2008-09-23 19:11:04 +00:00
|
|
|
peer->maxActiveRequests = peer->minActiveRequests +
|
|
|
|
estimatedBlocksInNext30Seconds;
|
2007-10-06 18:20:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-02 20:55:05 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
popNextRequest( tr_peermsgs * msgs,
|
|
|
|
struct peer_request * setme )
|
2007-10-28 15:20:24 +00:00
|
|
|
{
|
2008-10-02 20:55:05 +00:00
|
|
|
return reqListPop( &msgs->peerAskedForFast, setme )
|
|
|
|
|| reqListPop( &msgs->peerAskedFor, setme );
|
2007-10-28 15:20:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int
|
2008-07-28 19:47:16 +00:00
|
|
|
peerPulse( void * vmsgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-28 15:20:24 +00:00
|
|
|
tr_peermsgs * msgs = vmsgs;
|
2008-09-23 19:11:04 +00:00
|
|
|
const time_t now = time( NULL );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
ratePulse( msgs );
|
|
|
|
|
|
|
|
/*tr_peerIoTryRead( msgs->io );*/
|
2008-10-27 04:12:42 +00:00
|
|
|
pumpRequestQueue( msgs, now );
|
|
|
|
expireOldRequests( msgs, now );
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
if( msgs->sendingBlock )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
const size_t uploadMax = tr_peerIoGetWriteBufferSpace( msgs->io );
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len = EVBUFFER_LENGTH( msgs->outBlock );
|
2007-10-28 15:20:24 +00:00
|
|
|
const size_t outlen = MIN( len, uploadMax );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
assert( len );
|
2007-10-28 15:20:24 +00:00
|
|
|
|
2007-11-18 04:24:17 +00:00
|
|
|
if( outlen )
|
2007-10-28 15:20:24 +00:00
|
|
|
{
|
2008-10-27 04:12:42 +00:00
|
|
|
tr_peerIoWrite( msgs->io, EVBUFFER_DATA( msgs->outBlock ), outlen );
|
2007-11-18 03:18:26 +00:00
|
|
|
evbuffer_drain( msgs->outBlock, outlen );
|
2008-10-27 04:12:42 +00:00
|
|
|
peerGotBytes( msgs, outlen, now );
|
2007-10-28 15:20:24 +00:00
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
len -= outlen;
|
|
|
|
msgs->clientSentAnythingAt = now;
|
2008-07-27 14:10:32 +00:00
|
|
|
msgs->sendingBlock = len != 0;
|
2007-11-18 03:18:26 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "wrote %d bytes; %d left in block", (int)outlen,
|
|
|
|
(int)len );
|
2007-11-18 03:18:26 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else dbgmsg( msgs,
|
|
|
|
"stalled writing block... uploadMax %lu, outlen %lu",
|
2008-10-27 04:12:42 +00:00
|
|
|
uploadMax, outlen );
|
2007-11-18 03:18:26 +00:00
|
|
|
}
|
2007-10-28 15:20:24 +00:00
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
if( !msgs->sendingBlock )
|
|
|
|
{
|
2008-02-29 03:41:50 +00:00
|
|
|
struct peer_request req;
|
2008-10-27 04:12:42 +00:00
|
|
|
const int haveMessages = EVBUFFER_LENGTH( msgs->outMessages ) != 0;
|
2008-02-29 03:41:50 +00:00
|
|
|
|
2008-05-31 00:16:26 +00:00
|
|
|
if( haveMessages && !msgs->outMessagesBatchedAt ) /* fresh batch */
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "started an outMessages batch (length is %d)",
|
|
|
|
(int)EVBUFFER_LENGTH( msgs->outMessages ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
msgs->outMessagesBatchedAt = now;
|
|
|
|
}
|
|
|
|
else if( haveMessages
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( ( now - msgs->outMessagesBatchedAt ) >
|
|
|
|
msgs->outMessagesBatchPeriod ) )
|
2007-11-18 03:18:26 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "flushing outMessages... (length is %d)",
|
|
|
|
(int)EVBUFFER_LENGTH(
|
|
|
|
msgs->outMessages ) );
|
2007-11-18 03:18:26 +00:00
|
|
|
tr_peerIoWriteBuf( msgs->io, msgs->outMessages );
|
|
|
|
msgs->clientSentAnythingAt = now;
|
2008-05-31 00:16:26 +00:00
|
|
|
msgs->outMessagesBatchedAt = 0;
|
|
|
|
msgs->outMessagesBatchPeriod = LOW_PRIORITY_INTERVAL_SECS;
|
2007-11-18 03:18:26 +00:00
|
|
|
}
|
|
|
|
else if( !EVBUFFER_LENGTH( msgs->outBlock )
|
2008-10-02 20:55:05 +00:00
|
|
|
&& popNextRequest( msgs, &req )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& requestIsValid( msgs, &req )
|
|
|
|
&& tr_cpPieceIsComplete( msgs->torrent->completion,
|
|
|
|
req.index ) )
|
2007-11-18 03:18:26 +00:00
|
|
|
{
|
2008-02-29 03:41:50 +00:00
|
|
|
uint8_t * buf = tr_new( uint8_t, req.length );
|
2008-10-10 03:04:46 +00:00
|
|
|
const int err = tr_ioRead( msgs->torrent,
|
|
|
|
req.index, req.offset, req.length,
|
|
|
|
buf );
|
|
|
|
if( err )
|
|
|
|
{
|
|
|
|
fireError( msgs, err );
|
|
|
|
}
|
|
|
|
else
|
2007-11-18 03:18:26 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIo * io = msgs->io;
|
2007-11-18 03:18:26 +00:00
|
|
|
struct evbuffer * out = msgs->outBlock;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "sending block %u:%u->%u", req.index,
|
|
|
|
req.offset,
|
|
|
|
req.length );
|
|
|
|
tr_peerIoWriteUint32(
|
|
|
|
io, out, sizeof( uint8_t ) + 2 * sizeof( uint32_t ) +
|
|
|
|
req.length );
|
2007-11-18 03:18:26 +00:00
|
|
|
tr_peerIoWriteUint8 ( io, out, BT_PIECE );
|
2008-02-29 03:41:50 +00:00
|
|
|
tr_peerIoWriteUint32( io, out, req.index );
|
|
|
|
tr_peerIoWriteUint32( io, out, req.offset );
|
|
|
|
tr_peerIoWriteBytes ( io, out, buf, req.length );
|
2007-11-18 03:18:26 +00:00
|
|
|
msgs->sendingBlock = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( buf );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( ( !haveMessages )
|
|
|
|
&& ( now - msgs->clientSentAnythingAt ) >
|
|
|
|
KEEPALIVE_INTERVAL_SECS )
|
2007-11-18 03:18:26 +00:00
|
|
|
{
|
|
|
|
sendKeepalive( msgs );
|
|
|
|
}
|
2007-10-28 15:20:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return TRUE; /* loop forever */
|
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
void
|
|
|
|
tr_peerMsgsPulse( tr_peermsgs * msgs )
|
|
|
|
{
|
|
|
|
if( msgs != NULL )
|
|
|
|
peerPulse( msgs );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
gotError( struct bufferevent * evbuf UNUSED,
|
|
|
|
short what,
|
|
|
|
void * vmsgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-17 17:49:30 +00:00
|
|
|
if( what & EVBUFFER_TIMEOUT )
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( vmsgs, "libevent got a timeout, what=%hd, secs=%d", what,
|
|
|
|
evbuf->timeout_read );
|
2008-01-19 03:30:45 +00:00
|
|
|
if( what & ( EVBUFFER_EOF | EVBUFFER_ERROR ) )
|
2007-11-17 17:49:30 +00:00
|
|
|
dbgmsg( vmsgs, "libevent got an error! what=%hd, errno=%d (%s)",
|
2008-09-23 19:11:04 +00:00
|
|
|
what, errno, tr_strerror( errno ) );
|
2008-10-03 04:49:06 +00:00
|
|
|
fireError( vmsgs, ENOTCONN );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sendBitfield( tr_peermsgs * msgs )
|
|
|
|
{
|
2007-10-03 16:42:43 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfield * field;
|
|
|
|
tr_piece_index_t lazyPieces[LAZY_PIECE_COUNT];
|
|
|
|
size_t i;
|
|
|
|
size_t lazyCount = 0;
|
2008-08-16 21:06:57 +00:00
|
|
|
|
|
|
|
field = tr_bitfieldDup( tr_cpPieceBitfield( msgs->torrent->completion ) );
|
|
|
|
|
|
|
|
if( tr_sessionIsLazyBitfieldEnabled( msgs->session ) )
|
|
|
|
{
|
2008-08-27 18:50:21 +00:00
|
|
|
/** Lazy bitfields aren't a high priority or secure, so I'm opting for
|
|
|
|
speed over a truly random sample -- let's limit the pool size to
|
|
|
|
the first 1000 pieces so large torrents don't bog things down */
|
2008-11-16 08:56:18 +00:00
|
|
|
size_t poolSize;
|
|
|
|
const size_t maxPoolSize = MIN( msgs->torrent->info.pieceCount, 1000 );
|
|
|
|
tr_piece_index_t * pool = tr_new( tr_piece_index_t, maxPoolSize );
|
2008-08-27 18:50:21 +00:00
|
|
|
|
|
|
|
/* build the pool */
|
2008-11-16 08:56:18 +00:00
|
|
|
for( i=poolSize=0; i<maxPoolSize; ++i )
|
|
|
|
if( tr_bitfieldHas( field, i ) )
|
|
|
|
pool[poolSize++] = i;
|
2008-08-27 18:50:21 +00:00
|
|
|
|
|
|
|
/* pull random piece indices from the pool */
|
|
|
|
while( ( poolSize > 0 ) && ( lazyCount < LAZY_PIECE_COUNT ) )
|
2008-08-16 21:06:57 +00:00
|
|
|
{
|
2008-11-16 08:56:18 +00:00
|
|
|
const int pos = tr_cryptoWeakRandInt( poolSize );
|
2008-08-27 18:50:21 +00:00
|
|
|
const tr_piece_index_t piece = pool[pos];
|
|
|
|
tr_bitfieldRem( field, piece );
|
|
|
|
lazyPieces[lazyCount++] = piece;
|
|
|
|
pool[pos] = pool[--poolSize];
|
2008-08-16 21:06:57 +00:00
|
|
|
}
|
2008-08-27 18:50:21 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( pool );
|
2008-08-16 21:06:57 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, out,
|
|
|
|
sizeof( uint8_t ) + field->byteCount );
|
2007-10-03 16:42:43 +00:00
|
|
|
tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
|
2008-08-16 21:06:57 +00:00
|
|
|
tr_peerIoWriteBytes ( msgs->io, out, field->bits, field->byteCount );
|
|
|
|
dbgmsg( msgs, "sending bitfield... outMessage size is now %d",
|
2008-09-23 19:11:04 +00:00
|
|
|
(int)EVBUFFER_LENGTH( out ) );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2008-08-16 21:06:57 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < lazyCount; ++i )
|
2008-08-16 21:06:57 +00:00
|
|
|
protocolSendHave( msgs, lazyPieces[i] );
|
|
|
|
|
|
|
|
tr_bitfieldFree( field );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-28 16:14:19 +00:00
|
|
|
/* some peers give us error messages if we send
|
2008-04-12 23:01:40 +00:00
|
|
|
more than this many peers in a single pex message
|
|
|
|
http://wiki.theory.org/BitTorrentPeerExchangeConventions */
|
|
|
|
#define MAX_PEX_ADDED 50
|
|
|
|
#define MAX_PEX_DROPPED 50
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * added;
|
|
|
|
tr_pex * dropped;
|
|
|
|
tr_pex * elements;
|
|
|
|
int addedCount;
|
|
|
|
int droppedCount;
|
|
|
|
int elementCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
PexDiffs;
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
pexAddedCb( void * vpex,
|
|
|
|
void * userData )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-04-12 23:01:40 +00:00
|
|
|
PexDiffs * diffs = userData;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * pex = vpex;
|
|
|
|
|
2008-04-12 23:01:40 +00:00
|
|
|
if( diffs->addedCount < MAX_PEX_ADDED )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
diffs->added[diffs->addedCount++] = *pex;
|
|
|
|
diffs->elements[diffs->elementCount++] = *pex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
pexDroppedCb( void * vpex,
|
|
|
|
void * userData )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-04-12 23:01:40 +00:00
|
|
|
PexDiffs * diffs = userData;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * pex = vpex;
|
|
|
|
|
2008-04-12 23:01:40 +00:00
|
|
|
if( diffs->droppedCount < MAX_PEX_DROPPED )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
diffs->dropped[diffs->droppedCount++] = *pex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
pexElementCb( void * vpex,
|
|
|
|
void * userData )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-04-12 23:01:40 +00:00
|
|
|
PexDiffs * diffs = userData;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pex * pex = vpex;
|
|
|
|
|
2008-04-12 23:01:40 +00:00
|
|
|
diffs->elements[diffs->elementCount++] = *pex;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sendPex( tr_peermsgs * msgs )
|
|
|
|
{
|
2007-12-24 05:03:40 +00:00
|
|
|
if( msgs->peerSupportsPex && tr_torrentAllowsPex( msgs->torrent ) )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
tr_pex * newPex = NULL;
|
|
|
|
const int newCount = tr_peerMgrGetPeers(
|
|
|
|
msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
|
|
|
|
PexDiffs diffs;
|
|
|
|
tr_benc val;
|
|
|
|
uint8_t * tmp, *walk;
|
|
|
|
char * benc;
|
|
|
|
int bencLen;
|
2008-08-16 05:11:28 +00:00
|
|
|
struct evbuffer * out = msgs->outMessages;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* build the diffs */
|
|
|
|
diffs.added = tr_new( tr_pex, newCount );
|
|
|
|
diffs.addedCount = 0;
|
|
|
|
diffs.dropped = tr_new( tr_pex, msgs->pexCount );
|
|
|
|
diffs.droppedCount = 0;
|
|
|
|
diffs.elements = tr_new( tr_pex, newCount + msgs->pexCount );
|
|
|
|
diffs.elementCount = 0;
|
|
|
|
tr_set_compare( msgs->pex, msgs->pexCount,
|
|
|
|
newPex, newCount,
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_pexCompare, sizeof( tr_pex ),
|
2008-04-12 23:01:40 +00:00
|
|
|
pexDroppedCb, pexAddedCb, pexElementCb, &diffs );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg(
|
|
|
|
msgs,
|
|
|
|
"pex: old peer count %d, new peer count %d, added %d, removed %d",
|
|
|
|
msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* update peer */
|
|
|
|
tr_free( msgs->pex );
|
|
|
|
msgs->pex = diffs.elements;
|
|
|
|
msgs->pexCount = diffs.elementCount;
|
|
|
|
|
|
|
|
/* build the pex payload */
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_bencInitDict( &val, 3 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* "added" */
|
|
|
|
tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < diffs.addedCount; ++i )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
|
|
|
|
memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
|
|
|
|
}
|
|
|
|
assert( ( walk - tmp ) == diffs.addedCount * 6 );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddRaw( &val, "added", tmp, walk - tmp );
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_free( tmp );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* "added.f" */
|
|
|
|
tmp = walk = tr_new( uint8_t, diffs.addedCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < diffs.addedCount; ++i )
|
2007-09-20 16:32:01 +00:00
|
|
|
*walk++ = diffs.added[i].flags;
|
|
|
|
assert( ( walk - tmp ) == diffs.addedCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddRaw( &val, "added.f", tmp, walk - tmp );
|
2008-07-18 19:50:38 +00:00
|
|
|
tr_free( tmp );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* "dropped" */
|
|
|
|
tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < diffs.droppedCount; ++i )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
|
|
|
|
memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
|
|
|
|
}
|
|
|
|
assert( ( walk - tmp ) == diffs.droppedCount * 6 );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddRaw( &val, "dropped", tmp, walk - tmp );
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_free( tmp );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* write the pex message */
|
2007-11-06 21:14:30 +00:00
|
|
|
benc = tr_bencSave( &val, &bencLen );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( msgs->io, out,
|
|
|
|
2 * sizeof( uint8_t ) + bencLen );
|
2008-08-16 05:11:28 +00:00
|
|
|
tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
|
|
|
|
tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );
|
|
|
|
tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );
|
2008-05-31 00:16:26 +00:00
|
|
|
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
2008-09-23 19:11:04 +00:00
|
|
|
dbgmsg( msgs, "outMessage size is now %d",
|
|
|
|
(int)EVBUFFER_LENGTH( out ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( benc );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
tr_free( diffs.added );
|
|
|
|
tr_free( diffs.dropped );
|
|
|
|
tr_free( newPex );
|
|
|
|
|
|
|
|
msgs->clientSentPexAt = time( NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pexPulse( void * vpeer )
|
|
|
|
{
|
|
|
|
sendPex( vpeer );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
tr_peermsgs*
|
2007-10-03 04:04:34 +00:00
|
|
|
tr_peerMsgsNew( struct tr_torrent * torrent,
|
2008-09-23 19:11:04 +00:00
|
|
|
struct tr_peer * info,
|
2007-10-03 04:04:34 +00:00
|
|
|
tr_delivery_func func,
|
2008-09-23 19:11:04 +00:00
|
|
|
void * userData,
|
|
|
|
tr_publisher_tag * setme )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-03 04:04:34 +00:00
|
|
|
tr_peermsgs * m;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( info );
|
|
|
|
assert( info->io );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-03 04:04:34 +00:00
|
|
|
m = tr_new0( tr_peermsgs, 1 );
|
|
|
|
m->publisher = tr_publisherNew( );
|
|
|
|
m->info = info;
|
2008-10-02 15:53:33 +00:00
|
|
|
m->session = torrent->session;
|
2007-10-03 04:04:34 +00:00
|
|
|
m->torrent = torrent;
|
|
|
|
m->io = info->io;
|
|
|
|
m->info->clientIsChoked = 1;
|
|
|
|
m->info->peerIsChoked = 1;
|
|
|
|
m->info->clientIsInterested = 0;
|
|
|
|
m->info->peerIsInterested = 0;
|
|
|
|
m->info->have = tr_bitfieldNew( torrent->info.pieceCount );
|
2007-11-16 20:40:03 +00:00
|
|
|
m->state = AWAITING_BT_LENGTH;
|
2008-08-16 21:06:57 +00:00
|
|
|
m->pexTimer = tr_timerNew( m->session, pexPulse, m, PEX_INTERVAL );
|
2007-10-03 04:04:34 +00:00
|
|
|
m->outMessages = evbuffer_new( );
|
2008-05-31 00:16:26 +00:00
|
|
|
m->outMessagesBatchedAt = 0;
|
|
|
|
m->outMessagesBatchPeriod = LOW_PRIORITY_INTERVAL_SECS;
|
2007-11-16 20:40:03 +00:00
|
|
|
m->incoming.block = evbuffer_new( );
|
2007-10-28 15:20:24 +00:00
|
|
|
m->outBlock = evbuffer_new( );
|
2007-10-03 04:04:34 +00:00
|
|
|
m->peerAllowedPieces = NULL;
|
2008-02-29 03:41:50 +00:00
|
|
|
m->peerAskedFor = REQUEST_LIST_INIT;
|
|
|
|
m->peerAskedForFast = REQUEST_LIST_INIT;
|
|
|
|
m->clientAskedFor = REQUEST_LIST_INIT;
|
|
|
|
m->clientWillAskFor = REQUEST_LIST_INIT;
|
2007-10-22 14:15:16 +00:00
|
|
|
*setme = tr_publisherSubscribe( m->publisher, func, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_peerIoSupportsLTEP( m->io ) )
|
2007-10-22 23:27:47 +00:00
|
|
|
sendLtepHandshake( m );
|
2008-04-18 00:02:04 +00:00
|
|
|
|
|
|
|
sendBitfield( m );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
tr_peerIoSetTimeoutSecs( m->io, 150 ); /* timeout after N seconds of
|
|
|
|
inactivity */
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_peerIoSetIOFuncs( m->io, canRead, NULL, gotError, m );
|
2008-01-26 03:43:49 +00:00
|
|
|
ratePulse( m );
|
2007-10-22 23:27:47 +00:00
|
|
|
|
2007-10-03 04:04:34 +00:00
|
|
|
return m;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-24 17:47:15 +00:00
|
|
|
tr_peerMsgsFree( tr_peermsgs* msgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
if( msgs )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-24 17:47:15 +00:00
|
|
|
tr_timerFree( &msgs->pexTimer );
|
|
|
|
tr_publisherFree( &msgs->publisher );
|
2008-02-29 03:41:50 +00:00
|
|
|
reqListClear( &msgs->clientWillAskFor );
|
|
|
|
reqListClear( &msgs->clientAskedFor );
|
|
|
|
reqListClear( &msgs->peerAskedForFast );
|
|
|
|
reqListClear( &msgs->peerAskedFor );
|
2007-11-16 15:33:57 +00:00
|
|
|
tr_bitfieldFree( msgs->peerAllowedPieces );
|
2007-11-16 20:40:03 +00:00
|
|
|
evbuffer_free( msgs->incoming.block );
|
2007-09-24 17:47:15 +00:00
|
|
|
evbuffer_free( msgs->outMessages );
|
2007-10-28 15:20:24 +00:00
|
|
|
evbuffer_free( msgs->outBlock );
|
2007-09-24 17:47:15 +00:00
|
|
|
tr_free( msgs->pex );
|
2007-11-17 07:48:51 +00:00
|
|
|
|
|
|
|
memset( msgs, ~0, sizeof( tr_peermsgs ) );
|
2007-09-24 17:47:15 +00:00
|
|
|
tr_free( msgs );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerMsgsUnsubscribe( tr_peermsgs * peer,
|
|
|
|
tr_publisher_tag tag )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
tr_publisherUnsubscribe( peer->publisher, tag );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|