2007-09-20 16:32:01 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-09-20 16:32:01 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2007-09-20 16:32:01 +00:00
|
|
|
* 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>
|
2009-01-10 02:22:13 +00:00
|
|
|
#include <errno.h>
|
2008-09-17 19:44:24 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2008-01-10 19:52:56 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <winsock2.h>
|
2008-01-10 19:52:56 +00:00
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <arpa/inet.h> /* inet_ntoa */
|
2008-01-10 19:52:56 +00:00
|
|
|
#endif
|
2007-10-25 14:34:20 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/bufferevent.h>
|
2011-02-18 00:23:51 +00:00
|
|
|
#include "utp.h"
|
2007-10-25 14:34:20 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h"
|
2008-11-17 04:00:57 +00:00
|
|
|
#include "list.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "net.h"
|
2010-05-26 15:23:21 +00:00
|
|
|
#include "peer-common.h" /* MAX_BLOCK_SIZE */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-io.h"
|
2009-12-10 05:52:46 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
#define MAGIC_NUMBER 206745
|
2007-10-11 02:50:05 +00:00
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define EAGAIN WSAEWOULDBLOCK
|
|
|
|
#define EINTR WSAEINTR
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#define EPIPE WSAECONNRESET
|
|
|
|
#endif
|
|
|
|
|
2008-11-06 02:56:51 +00:00
|
|
|
static size_t
|
2009-04-21 16:18:51 +00:00
|
|
|
guessPacketOverhead( size_t d )
|
2008-11-06 02:56:51 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* http://sd.wareonearth.com/~phil/net/overhead/
|
|
|
|
*
|
|
|
|
* TCP over Ethernet:
|
|
|
|
* Assuming no header compression (e.g. not PPP)
|
|
|
|
* Add 20 IPv4 header or 40 IPv6 header (no options)
|
|
|
|
* Add 20 TCP header
|
|
|
|
* Add 12 bytes optional TCP timestamps
|
|
|
|
* Max TCP Payload data rates over ethernet are thus:
|
|
|
|
* (1500-40)/(38+1500) = 94.9285 % IPv4, minimal headers
|
|
|
|
* (1500-52)/(38+1500) = 94.1482 % IPv4, TCP timestamps
|
|
|
|
* (1500-52)/(42+1500) = 93.9040 % 802.1q, IPv4, TCP timestamps
|
|
|
|
* (1500-60)/(38+1500) = 93.6281 % IPv6, minimal headers
|
|
|
|
* (1500-72)/(38+1500) = 92.8479 % IPv6, TCP timestamps
|
|
|
|
* (1500-72)/(42+1500) = 92.6070 % 802.1q, IPv6, ICP timestamps
|
|
|
|
*/
|
2010-01-20 18:48:52 +00:00
|
|
|
const double assumed_payload_data_rate = 94.0;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
return (unsigned int)( d * ( 100.0 / assumed_payload_data_rate ) - d );
|
2008-11-06 02:56:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-10-14 01:00:15 +00:00
|
|
|
#define dbgmsg( io, ... ) \
|
2008-10-26 15:39:04 +00:00
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, tr_peerIoGetAddrStr( io ), __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-11-17 04:00:57 +00:00
|
|
|
struct tr_datatype
|
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isPieceData;
|
|
|
|
size_t length;
|
2008-11-17 04:00:57 +00:00
|
|
|
};
|
|
|
|
|
2011-01-29 18:59:23 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2010-07-03 00:25:22 +00:00
|
|
|
didWriteWrapper( tr_peerIo * io, unsigned int bytes_transferred )
|
2007-12-01 23:08:34 +00:00
|
|
|
{
|
2009-02-13 18:23:56 +00:00
|
|
|
while( bytes_transferred && tr_isPeerIo( io ) )
|
|
|
|
{
|
2010-04-23 23:45:44 +00:00
|
|
|
struct tr_datatype * next = io->outbuf_datatypes->data;
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
const unsigned int payload = MIN( next->length, bytes_transferred );
|
|
|
|
const unsigned int overhead = guessPacketOverhead( payload );
|
2011-01-29 18:59:23 +00:00
|
|
|
const uint64_t now = tr_sessionGetTimeMsec( io->session );
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2010-10-24 01:08:08 +00:00
|
|
|
tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData, now );
|
2008-11-26 15:58:26 +00:00
|
|
|
|
|
|
|
if( overhead > 0 )
|
2010-10-24 01:08:08 +00:00
|
|
|
tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE, now );
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
if( io->didWrite )
|
2008-11-26 15:58:26 +00:00
|
|
|
io->didWrite( io, payload, next->isPieceData, io->userData );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
if( tr_isPeerIo( io ) )
|
|
|
|
{
|
|
|
|
bytes_transferred -= payload;
|
|
|
|
next->length -= payload;
|
|
|
|
if( !next->length ) {
|
2010-04-23 23:45:44 +00:00
|
|
|
tr_list_pop_front( &io->outbuf_datatypes );
|
2009-02-13 18:23:56 +00:00
|
|
|
tr_free( next );
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-12-01 23:08:34 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-12-20 22:19:34 +00:00
|
|
|
canReadWrapper( tr_peerIo * io )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_bool err = 0;
|
2009-10-10 17:37:34 +00:00
|
|
|
tr_bool done = 0;
|
|
|
|
tr_session * session;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
dbgmsg( io, "canRead" );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-10-10 17:37:34 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isSession( io->session ) );
|
2009-01-05 04:27:54 +00:00
|
|
|
tr_peerIoRef( io );
|
|
|
|
|
2009-10-10 17:37:34 +00:00
|
|
|
session = io->session;
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/* try to consume the input buffer */
|
|
|
|
if( io->canRead )
|
2007-10-02 02:59:07 +00:00
|
|
|
{
|
2011-01-29 18:59:23 +00:00
|
|
|
const uint64_t now = tr_sessionGetTimeMsec( io->session );
|
|
|
|
|
2010-01-04 09:05:02 +00:00
|
|
|
tr_sessionLock( session );
|
2007-10-02 02:59:07 +00:00
|
|
|
|
2008-09-19 17:03:25 +00:00
|
|
|
while( !done && !err )
|
2007-10-02 02:59:07 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
size_t piece = 0;
|
2010-12-20 02:07:51 +00:00
|
|
|
const size_t oldLen = evbuffer_get_length( io->inbuf );
|
2008-12-16 22:08:17 +00:00
|
|
|
const int ret = io->canRead( io, io->userData, &piece );
|
2010-12-20 02:07:51 +00:00
|
|
|
const size_t used = oldLen - evbuffer_get_length( io->inbuf );
|
2011-01-20 00:31:46 +00:00
|
|
|
const unsigned int overhead = guessPacketOverhead( used );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2008-12-30 03:45:12 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2010-10-24 01:08:08 +00:00
|
|
|
if( piece || (piece!=used) )
|
|
|
|
{
|
|
|
|
if( piece )
|
|
|
|
tr_bandwidthUsed( &io->bandwidth, TR_DOWN, piece, TRUE, now );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2010-10-24 01:08:08 +00:00
|
|
|
if( used != piece )
|
|
|
|
tr_bandwidthUsed( &io->bandwidth, TR_DOWN, used - piece, FALSE, now );
|
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2011-01-20 00:31:46 +00:00
|
|
|
if( overhead > 0 )
|
|
|
|
tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE, now );
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
switch( ret )
|
|
|
|
{
|
2008-09-19 17:03:25 +00:00
|
|
|
case READ_NOW:
|
2010-12-20 02:07:51 +00:00
|
|
|
if( evbuffer_get_length( io->inbuf ) )
|
2008-09-17 19:44:24 +00:00
|
|
|
continue;
|
|
|
|
done = 1;
|
2008-09-19 17:03:25 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-19 17:03:25 +00:00
|
|
|
case READ_LATER:
|
|
|
|
done = 1;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-19 17:03:25 +00:00
|
|
|
case READ_ERR:
|
|
|
|
err = 1;
|
|
|
|
break;
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2009-10-10 17:37:34 +00:00
|
|
|
|
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2010-01-04 09:05:02 +00:00
|
|
|
tr_sessionUnlock( session );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2009-10-10 17:37:34 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2009-01-05 04:27:54 +00:00
|
|
|
tr_peerIoUnref( io );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool
|
|
|
|
tr_isPeerIo( const tr_peerIo * io )
|
2008-12-20 22:19:34 +00:00
|
|
|
{
|
|
|
|
return ( io != NULL )
|
|
|
|
&& ( io->magicNumber == MAGIC_NUMBER )
|
2009-01-24 17:37:28 +00:00
|
|
|
&& ( io->refCount >= 0 )
|
2009-01-02 19:56:06 +00:00
|
|
|
&& ( tr_isBandwidth( &io->bandwidth ) )
|
2009-01-08 04:43:00 +00:00
|
|
|
&& ( tr_isAddress( &io->addr ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-12-20 22:19:34 +00:00
|
|
|
event_read_cb( int fd, short event UNUSED, void * vio )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
int res;
|
2008-12-24 02:50:08 +00:00
|
|
|
int e;
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_peerIo * io = vio;
|
2008-12-23 17:11:31 +00:00
|
|
|
|
|
|
|
/* Limit the input buffer to 256K, so it doesn't grow too large */
|
2010-07-03 00:25:22 +00:00
|
|
|
unsigned int howmuch;
|
|
|
|
unsigned int curlen;
|
2008-12-20 22:19:34 +00:00
|
|
|
const tr_direction dir = TR_DOWN;
|
2010-07-03 00:25:22 +00:00
|
|
|
const unsigned int max = 256 * 1024;
|
2008-12-23 17:23:07 +00:00
|
|
|
|
2009-01-05 18:20:47 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
curlen = evbuffer_get_length( io->inbuf );
|
2008-12-23 17:23:07 +00:00
|
|
|
howmuch = curlen >= max ? 0 : max - curlen;
|
2009-01-02 19:56:06 +00:00
|
|
|
howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
dbgmsg( io, "libevent says this peer is ready to read" );
|
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop reading */
|
|
|
|
if( howmuch < 1 ) {
|
|
|
|
tr_peerIoSetEnabled( io, dir, FALSE );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR( 0 );
|
2010-07-03 00:25:22 +00:00
|
|
|
res = evbuffer_read( io->inbuf, fd, (int)howmuch );
|
2010-06-30 21:24:36 +00:00
|
|
|
e = EVUTIL_SOCKET_ERROR( );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2008-12-22 04:55:07 +00:00
|
|
|
if( res > 0 )
|
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2008-12-22 04:55:07 +00:00
|
|
|
/* Invoke the user callback - must always be called last */
|
|
|
|
canReadWrapper( io );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-30 21:24:36 +00:00
|
|
|
char errstr[512];
|
2010-12-20 02:07:51 +00:00
|
|
|
short what = BEV_EVENT_READING;
|
2008-12-22 04:55:07 +00:00
|
|
|
|
|
|
|
if( res == 0 ) /* EOF */
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2008-12-22 04:55:07 +00:00
|
|
|
else if( res == -1 ) {
|
2008-12-24 02:50:08 +00:00
|
|
|
if( e == EAGAIN || e == EINTR ) {
|
2008-12-22 04:55:07 +00:00
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
return;
|
|
|
|
}
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_ERROR;
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2010-11-11 15:31:11 +00:00
|
|
|
tr_net_strerror( errstr, sizeof( errstr ), e );
|
2010-06-30 21:24:36 +00:00
|
|
|
dbgmsg( io, "event_read_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr );
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2008-12-22 04:55:07 +00:00
|
|
|
if( io->gotError != NULL )
|
|
|
|
io->gotError( io, what, io->userData );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
static int
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_evbuffer_write( tr_peerIo * io, int fd, size_t howmuch )
|
|
|
|
{
|
2008-12-24 02:50:08 +00:00
|
|
|
int e;
|
2009-01-19 14:05:43 +00:00
|
|
|
int n;
|
2010-06-30 21:24:36 +00:00
|
|
|
char errstr[256];
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR( 0 );
|
2010-12-20 02:07:51 +00:00
|
|
|
n = evbuffer_write_atmost( io->outbuf, fd, howmuch );
|
2010-06-30 21:24:36 +00:00
|
|
|
e = EVUTIL_SOCKET_ERROR( );
|
|
|
|
dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?tr_net_strerror(errstr,sizeof(errstr),e):"") );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
return n;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
static void
|
2008-12-20 22:19:34 +00:00
|
|
|
event_write_cb( int fd, short event UNUSED, void * vio )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
int res = 0;
|
2008-12-24 02:50:08 +00:00
|
|
|
int e;
|
2010-12-20 02:07:51 +00:00
|
|
|
short what = BEV_EVENT_WRITING;
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_peerIo * io = vio;
|
|
|
|
size_t howmuch;
|
|
|
|
const tr_direction dir = TR_UP;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
dbgmsg( io, "libevent says this peer is ready to write" );
|
|
|
|
|
2008-12-23 17:11:31 +00:00
|
|
|
/* Write as much as possible, since the socket is non-blocking, write() will
|
|
|
|
* return if it can't write any more data without blocking */
|
2010-12-20 02:07:51 +00:00
|
|
|
howmuch = tr_bandwidthClamp( &io->bandwidth, dir, evbuffer_get_length( io->outbuf ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop writing */
|
|
|
|
if( howmuch < 1 ) {
|
|
|
|
tr_peerIoSetEnabled( io, dir, FALSE );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR( 0 );
|
2008-12-20 22:19:34 +00:00
|
|
|
res = tr_evbuffer_write( io, fd, howmuch );
|
2010-06-30 21:24:36 +00:00
|
|
|
e = EVUTIL_SOCKET_ERROR( );
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
if (res == -1) {
|
2010-12-20 02:07:51 +00:00
|
|
|
if (!e || e == EAGAIN || e == EINTR || e == EINPROGRESS)
|
2008-12-20 22:19:34 +00:00
|
|
|
goto reschedule;
|
|
|
|
/* error case */
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_ERROR;
|
2008-12-20 22:19:34 +00:00
|
|
|
} else if (res == 0) {
|
|
|
|
/* eof case */
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2008-12-20 22:19:34 +00:00
|
|
|
}
|
|
|
|
if (res <= 0)
|
|
|
|
goto error;
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
if( evbuffer_get_length( io->outbuf ) )
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
|
|
|
|
didWriteWrapper( io, res );
|
|
|
|
return;
|
|
|
|
|
|
|
|
reschedule:
|
2010-12-20 02:07:51 +00:00
|
|
|
if( evbuffer_get_length( io->outbuf ) )
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2008-12-24 02:50:08 +00:00
|
|
|
|
|
|
|
dbgmsg( io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, strerror( e ) );
|
|
|
|
|
2008-12-21 00:05:54 +00:00
|
|
|
if( io->gotError != NULL )
|
|
|
|
io->gotError( io, what, io->userData );
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2010-04-22 02:04:43 +00:00
|
|
|
static void
|
|
|
|
maybeSetCongestionAlgorithm( int socket, const char * algorithm )
|
|
|
|
{
|
|
|
|
if( algorithm && *algorithm )
|
|
|
|
{
|
|
|
|
const int rc = tr_netSetCongestionControl( socket, algorithm );
|
|
|
|
|
|
|
|
if( rc < 0 )
|
|
|
|
tr_ninf( "Net", "Can't set congestion control algorithm '%s': %s",
|
|
|
|
algorithm, tr_strerror( errno ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_peerIo*
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_peerIoNew( tr_session * session,
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidth * parent,
|
2008-12-02 03:41:58 +00:00
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
|
|
|
const uint8_t * torrentHash,
|
2009-12-02 05:30:46 +00:00
|
|
|
tr_bool isIncoming,
|
|
|
|
tr_bool isSeed,
|
2011-02-18 00:23:51 +00:00
|
|
|
int socket,
|
|
|
|
struct UTPSocket * utp_socket)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_peerIo * io;
|
2008-04-12 00:29:49 +00:00
|
|
|
|
2009-01-28 19:35:39 +00:00
|
|
|
assert( session != NULL );
|
|
|
|
assert( session->events != NULL );
|
2009-12-02 05:30:46 +00:00
|
|
|
assert( tr_isBool( isIncoming ) );
|
|
|
|
assert( tr_isBool( isSeed ) );
|
2009-01-28 19:35:39 +00:00
|
|
|
assert( tr_amInEventThread( session ) );
|
2011-02-18 00:23:51 +00:00
|
|
|
assert( (socket < 0) == (utp_socket != NULL) );
|
2009-01-28 19:35:39 +00:00
|
|
|
|
2010-04-22 01:49:16 +00:00
|
|
|
if( socket >= 0 ) {
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_netSetTOS( socket, session->peerSocketTOS );
|
2010-04-22 02:04:43 +00:00
|
|
|
maybeSetCongestionAlgorithm( socket, session->peer_congestion_algorithm );
|
2010-04-22 01:49:16 +00:00
|
|
|
}
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
io = tr_new0( tr_peerIo, 1 );
|
2008-11-25 21:35:17 +00:00
|
|
|
io->magicNumber = MAGIC_NUMBER;
|
2009-01-05 04:27:54 +00:00
|
|
|
io->refCount = 1;
|
2008-09-17 19:44:24 +00:00
|
|
|
io->crypto = tr_cryptoNew( torrentHash, isIncoming );
|
|
|
|
io->session = session;
|
2008-12-02 03:41:58 +00:00
|
|
|
io->addr = *addr;
|
2009-12-28 23:24:00 +00:00
|
|
|
io->isSeed = isSeed;
|
2008-09-17 19:44:24 +00:00
|
|
|
io->port = port;
|
|
|
|
io->socket = socket;
|
2011-02-18 00:23:51 +00:00
|
|
|
io->utp_socket = utp_socket;
|
2008-09-17 19:44:24 +00:00
|
|
|
io->isIncoming = isIncoming != 0;
|
2009-11-26 18:47:08 +00:00
|
|
|
io->timeCreated = tr_time( );
|
2008-12-16 22:08:17 +00:00
|
|
|
io->inbuf = evbuffer_new( );
|
|
|
|
io->outbuf = evbuffer_new( );
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidthConstruct( &io->bandwidth, session, parent );
|
|
|
|
tr_bandwidthSetPeer( &io->bandwidth, io );
|
|
|
|
dbgmsg( io, "bandwidth is %p; its parent is %p", &io->bandwidth, parent );
|
2008-12-22 19:16:06 +00:00
|
|
|
|
2011-02-18 00:23:51 +00:00
|
|
|
if( io->socket >= 0 ) {
|
|
|
|
io->event_read = event_new( session->event_base,
|
|
|
|
io->socket, EV_READ, event_read_cb, io );
|
|
|
|
io->event_write = event_new( session->event_base,
|
|
|
|
io->socket, EV_WRITE, event_write_cb, io );
|
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
return io;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_peerIoNewIncoming( tr_session * session,
|
|
|
|
tr_bandwidth * parent,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
2011-02-18 00:23:51 +00:00
|
|
|
int fd,
|
|
|
|
struct UTPSocket * utp_socket )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( session );
|
2008-12-22 04:55:07 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2011-02-18 00:23:51 +00:00
|
|
|
return tr_peerIoNew( session, parent, addr, port, NULL, TRUE, FALSE,
|
|
|
|
fd, utp_socket );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_peerIoNewOutgoing( tr_session * session,
|
|
|
|
tr_bandwidth * parent,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
2009-12-02 05:30:46 +00:00
|
|
|
const uint8_t * torrentHash,
|
|
|
|
tr_bool isSeed )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-12-02 05:30:46 +00:00
|
|
|
int fd;
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( session );
|
2008-12-22 04:55:07 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( torrentHash );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-12-15 17:39:19 +00:00
|
|
|
fd = tr_netOpenPeerSocket( session, addr, port, isSeed );
|
|
|
|
dbgmsg( NULL, "tr_netOpenPeerSocket returned fd %d", fd );
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2009-12-02 05:30:46 +00:00
|
|
|
return fd < 0 ? NULL
|
2011-02-18 00:23:51 +00:00
|
|
|
: tr_peerIoNew( session, parent, addr, port,
|
|
|
|
torrentHash, FALSE, isSeed, fd, NULL );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_enable( tr_peerIo * io, short event )
|
|
|
|
{
|
|
|
|
assert( tr_amInEventThread( io->session ) );
|
|
|
|
assert( io->session != NULL );
|
|
|
|
assert( io->session->events != NULL );
|
|
|
|
|
2010-04-27 13:43:03 +00:00
|
|
|
if( io->socket < 0 )
|
|
|
|
return;
|
|
|
|
|
2011-02-18 00:23:51 +00:00
|
|
|
assert( io->session->events != NULL );
|
|
|
|
assert( event_initialized( io->event_read ) );
|
|
|
|
assert( event_initialized( io->event_write ) );
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
if( ( event & EV_READ ) && ! ( io->pendingEvents & EV_READ ) )
|
|
|
|
{
|
|
|
|
dbgmsg( io, "enabling libevent ready-to-read polling" );
|
2010-12-20 02:07:51 +00:00
|
|
|
event_add( io->event_read, NULL );
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( event & EV_WRITE ) && ! ( io->pendingEvents & EV_WRITE ) )
|
|
|
|
{
|
|
|
|
dbgmsg( io, "enabling libevent ready-to-write polling" );
|
2010-12-20 02:07:51 +00:00
|
|
|
event_add( io->event_write, NULL );
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_disable( struct tr_peerIo * io, short event )
|
|
|
|
{
|
|
|
|
assert( tr_amInEventThread( io->session ) );
|
|
|
|
assert( io->session != NULL );
|
2011-02-18 00:23:51 +00:00
|
|
|
|
|
|
|
if( io->socket < 0 )
|
|
|
|
return;
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
assert( io->session->events != NULL );
|
2010-12-20 02:07:51 +00:00
|
|
|
assert( event_initialized( io->event_read ) );
|
|
|
|
assert( event_initialized( io->event_write ) );
|
2010-01-17 19:21:04 +00:00
|
|
|
|
|
|
|
if( ( event & EV_READ ) && ( io->pendingEvents & EV_READ ) )
|
|
|
|
{
|
|
|
|
dbgmsg( io, "disabling libevent ready-to-read polling" );
|
2010-12-20 02:07:51 +00:00
|
|
|
event_del( io->event_read );
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( event & EV_WRITE ) && ( io->pendingEvents & EV_WRITE ) )
|
|
|
|
{
|
|
|
|
dbgmsg( io, "disabling libevent ready-to-write polling" );
|
2010-12-20 02:07:51 +00:00
|
|
|
event_del( io->event_write );
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoSetEnabled( tr_peerIo * io,
|
|
|
|
tr_direction dir,
|
|
|
|
tr_bool isEnabled )
|
|
|
|
{
|
|
|
|
const short event = dir == TR_UP ? EV_WRITE : EV_READ;
|
|
|
|
|
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
|
|
|
assert( tr_amInEventThread( io->session ) );
|
|
|
|
assert( io->session->events != NULL );
|
|
|
|
|
|
|
|
if( isEnabled )
|
|
|
|
event_enable( io, event );
|
|
|
|
else
|
|
|
|
event_disable( io, event );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
static void
|
|
|
|
io_dtor( void * vio )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_peerIo * io = vio;
|
2009-01-26 02:51:50 +00:00
|
|
|
|
2009-01-24 16:21:34 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2009-01-26 02:51:50 +00:00
|
|
|
assert( tr_amInEventThread( io->session ) );
|
|
|
|
assert( io->session->events != NULL );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-01-24 16:21:34 +00:00
|
|
|
dbgmsg( io, "in tr_peerIo destructor" );
|
2010-01-18 19:26:46 +00:00
|
|
|
event_disable( io, EV_READ | EV_WRITE );
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidthDestruct( &io->bandwidth );
|
2008-12-16 22:08:17 +00:00
|
|
|
evbuffer_free( io->outbuf );
|
|
|
|
evbuffer_free( io->inbuf );
|
2011-02-18 00:24:09 +00:00
|
|
|
if( io->socket >= 0 ) {
|
|
|
|
event_free( io->event_read );
|
|
|
|
event_free( io->event_write );
|
2011-02-18 00:23:51 +00:00
|
|
|
tr_netClose( io->session, io->socket );
|
2011-02-18 00:24:09 +00:00
|
|
|
}
|
2011-02-18 00:23:51 +00:00
|
|
|
if( io->utp_socket != NULL )
|
|
|
|
UTP_Close( io->utp_socket );
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_cryptoFree( io->crypto );
|
2010-04-23 23:45:44 +00:00
|
|
|
tr_list_free( &io->outbuf_datatypes, tr_free );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
memset( io, ~0, sizeof( tr_peerIo ) );
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_free( io );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
static void
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_peerIoFree( tr_peerIo * io )
|
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
if( io )
|
2007-10-02 16:12:44 +00:00
|
|
|
{
|
2009-01-24 16:21:34 +00:00
|
|
|
dbgmsg( io, "in tr_peerIoFree" );
|
2007-10-02 16:12:44 +00:00
|
|
|
io->canRead = NULL;
|
2007-12-01 23:08:34 +00:00
|
|
|
io->didWrite = NULL;
|
2007-10-02 16:12:44 +00:00
|
|
|
io->gotError = NULL;
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_runInEventThread( io->session, io_dtor, io );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
void
|
2009-01-24 17:20:07 +00:00
|
|
|
tr_peerIoRefImpl( const char * file, int line, tr_peerIo * io )
|
2009-01-05 04:27:54 +00:00
|
|
|
{
|
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2009-01-24 17:37:28 +00:00
|
|
|
dbgmsg( io, "%s:%d is incrementing the IO's refcount from %d to %d",
|
2009-01-24 17:20:07 +00:00
|
|
|
file, line, io->refCount, io->refCount+1 );
|
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
++io->refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-24 17:20:07 +00:00
|
|
|
tr_peerIoUnrefImpl( const char * file, int line, tr_peerIo * io )
|
2009-01-05 04:27:54 +00:00
|
|
|
{
|
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2009-01-24 17:37:28 +00:00
|
|
|
dbgmsg( io, "%s:%d is decrementing the IO's refcount from %d to %d",
|
|
|
|
file, line, io->refCount, io->refCount-1 );
|
2009-01-24 17:20:07 +00:00
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
if( !--io->refCount )
|
|
|
|
tr_peerIoFree( io );
|
|
|
|
}
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
const tr_address*
|
2009-01-24 17:20:07 +00:00
|
|
|
tr_peerIoGetAddress( const tr_peerIo * io, tr_port * port )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( port )
|
2008-09-23 19:11:04 +00:00
|
|
|
*port = io->port;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
return &io->addr;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2008-12-22 00:52:44 +00:00
|
|
|
tr_peerIoAddrStr( const tr_address * addr, tr_port port )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
static char buf[512];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
if( addr->type == TR_AF_INET )
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%s:%u", tr_ntop_non_ts( addr ), ntohs( port ) );
|
|
|
|
else
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "[%s]:%u", tr_ntop_non_ts( addr ), ntohs( port ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
const char* tr_peerIoGetAddrStr( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
return tr_isPeerIo( io ) ? tr_peerIoAddrStr( &io->addr, io->port ) : "error";
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void
|
2008-12-20 22:19:34 +00:00
|
|
|
tr_peerIoSetIOFuncs( tr_peerIo * io,
|
|
|
|
tr_can_read_cb readcb,
|
|
|
|
tr_did_write_cb writecb,
|
|
|
|
tr_net_error_cb errcb,
|
|
|
|
void * userData )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
io->canRead = readcb;
|
2007-12-01 23:08:34 +00:00
|
|
|
io->didWrite = writecb;
|
2007-09-20 16:32:01 +00:00
|
|
|
io->gotError = errcb;
|
|
|
|
io->userData = userData;
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:20:47 +00:00
|
|
|
void
|
|
|
|
tr_peerIoClear( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
tr_peerIoSetIOFuncs( io, NULL, NULL, NULL, NULL );
|
|
|
|
tr_peerIoSetEnabled( io, TR_UP, FALSE );
|
|
|
|
tr_peerIoSetEnabled( io, TR_DOWN, FALSE );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
|
|
|
tr_peerIoReconnect( tr_peerIo * io )
|
|
|
|
{
|
2010-06-30 16:40:19 +00:00
|
|
|
short int pendingEvents;
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_session * session;
|
|
|
|
|
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( !tr_peerIoIsIncoming( io ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
session = tr_peerIoGetSession( io );
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
pendingEvents = io->pendingEvents;
|
2010-01-18 19:26:46 +00:00
|
|
|
event_disable( io, EV_READ | EV_WRITE );
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2011-02-18 00:23:51 +00:00
|
|
|
if( io->socket >= 0 ) {
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netClose( session, io->socket );
|
2011-02-18 00:23:51 +00:00
|
|
|
io->socket = -1;
|
|
|
|
}
|
|
|
|
if( io->utp_socket != NULL ) {
|
|
|
|
UTP_Close(io->utp_socket);
|
|
|
|
io->utp_socket = NULL;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2011-02-01 17:12:20 +00:00
|
|
|
event_free( io->event_read );
|
|
|
|
event_free( io->event_write );
|
2009-12-15 17:39:19 +00:00
|
|
|
io->socket = tr_netOpenPeerSocket( session, &io->addr, io->port, io->isSeed );
|
2010-12-24 08:58:41 +00:00
|
|
|
io->event_read = event_new( session->event_base, io->socket, EV_READ, event_read_cb, io );
|
|
|
|
io->event_write = event_new( session->event_base, io->socket, EV_WRITE, event_write_cb, io );
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
if( io->socket >= 0 )
|
|
|
|
{
|
2010-04-23 23:48:52 +00:00
|
|
|
event_enable( io, pendingEvents );
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netSetTOS( io->socket, session->peerSocketTOS );
|
2010-04-22 02:04:43 +00:00
|
|
|
maybeSetCongestionAlgorithm( io->socket, session->peer_congestion_algorithm );
|
2007-09-20 16:32:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoSetTorrentHash( tr_peerIo * io,
|
2007-09-20 16:32:01 +00:00
|
|
|
const uint8_t * hash )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_cryptoSetTorrentHash( io->crypto, hash );
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t*
|
|
|
|
tr_peerIoGetTorrentHash( tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( io->crypto );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
return tr_cryptoGetTorrentHash( io->crypto );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoHasTorrentHash( const tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( io->crypto );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
return tr_cryptoHasTorrentHash( io->crypto );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoSetPeersId( tr_peerIo * io,
|
2007-09-20 16:32:01 +00:00
|
|
|
const uint8_t * peer_id )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( io->peerIdIsSet = peer_id != NULL ) )
|
2007-09-20 16:32:01 +00:00
|
|
|
memcpy( io->peerId, peer_id, 20 );
|
|
|
|
else
|
|
|
|
memset( io->peerId, 0, 20 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
static unsigned int
|
2009-01-05 04:27:54 +00:00
|
|
|
getDesiredOutputBufferSize( const tr_peerIo * io, uint64_t now )
|
2008-11-28 16:00:29 +00:00
|
|
|
{
|
|
|
|
/* this is all kind of arbitrary, but what seems to work well is
|
2008-12-15 21:22:08 +00:00
|
|
|
* being large enough to hold the next 20 seconds' worth of input,
|
|
|
|
* or a few blocks, whichever is bigger.
|
2008-11-28 16:00:29 +00:00
|
|
|
* It's okay to tweak this as needed */
|
2010-07-03 00:25:22 +00:00
|
|
|
const unsigned int currentSpeed_Bps = tr_bandwidthGetPieceSpeed_Bps( &io->bandwidth, now, TR_UP );
|
|
|
|
const unsigned int period = 15u; /* arbitrary */
|
|
|
|
/* the 3 is arbitrary; the .5 is to leave room for messages */
|
|
|
|
static const unsigned int ceiling = (unsigned int)( MAX_BLOCK_SIZE * 3.5 );
|
|
|
|
return MAX( ceiling, currentSpeed_Bps*period );
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
size_t
|
2009-01-05 04:27:54 +00:00
|
|
|
tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2009-01-05 04:27:54 +00:00
|
|
|
const size_t desiredLen = getDesiredOutputBufferSize( io, now );
|
2010-12-20 02:07:51 +00:00
|
|
|
const size_t currentLen = evbuffer_get_length( io->outbuf );
|
2008-11-24 04:21:23 +00:00
|
|
|
size_t freeSpace = 0;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
|
|
|
if( desiredLen > currentLen )
|
|
|
|
freeSpace = desiredLen - currentLen;
|
|
|
|
|
|
|
|
return freeSpace;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_peerIoSetEncryption( tr_peerIo * io, uint32_t encryptionMode )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( encryptionMode == PEER_ENCRYPTION_NONE
|
2009-01-02 19:56:06 +00:00
|
|
|
|| encryptionMode == PEER_ENCRYPTION_RC4 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
io->encryptionMode = encryptionMode;
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
static void
|
|
|
|
addDatatype( tr_peerIo * io, size_t byteCount, tr_bool isPieceData )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
struct tr_datatype * d;
|
2008-12-22 04:55:07 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
d = tr_new( struct tr_datatype, 1 );
|
|
|
|
d->isPieceData = isPieceData != 0;
|
|
|
|
d->length = byteCount;
|
|
|
|
tr_list_append( &io->outbuf_datatypes, d );
|
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
static struct evbuffer_iovec *
|
|
|
|
evbuffer_peek_all( struct evbuffer * buf, size_t * setme_vecCount )
|
|
|
|
{
|
|
|
|
const size_t byteCount = evbuffer_get_length( buf );
|
|
|
|
const int vecCount = evbuffer_peek( buf, byteCount, NULL, NULL, 0 );
|
|
|
|
struct evbuffer_iovec * iovec = tr_new0( struct evbuffer_iovec, vecCount );
|
2011-02-08 00:31:50 +00:00
|
|
|
const int n = evbuffer_peek( buf, byteCount, NULL, iovec, vecCount );
|
|
|
|
assert( n == vecCount );
|
2011-02-09 02:26:35 +00:00
|
|
|
*setme_vecCount = n;
|
2010-12-20 02:07:51 +00:00
|
|
|
return iovec;
|
|
|
|
}
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
static void
|
|
|
|
maybeEncryptBuffer( tr_peerIo * io, struct evbuffer * buf )
|
|
|
|
{
|
|
|
|
if( io->encryptionMode == PEER_ENCRYPTION_RC4 )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
size_t i, n;
|
|
|
|
struct evbuffer_iovec * iovec = evbuffer_peek_all( buf, &n );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_cryptoEncrypt( io->crypto, iovec[i].iov_len, iovec[i].iov_base, iovec[i].iov_base );
|
2009-01-22 14:32:29 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
tr_free( iovec );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 14:32:29 +00:00
|
|
|
void
|
2010-12-20 02:07:51 +00:00
|
|
|
tr_peerIoWriteBuf( tr_peerIo * io, struct evbuffer * buf, tr_bool isPieceData )
|
2009-01-22 14:32:29 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
const size_t byteCount = evbuffer_get_length( buf );
|
|
|
|
maybeEncryptBuffer( io, buf );
|
|
|
|
evbuffer_add_buffer( io->outbuf, buf );
|
|
|
|
addDatatype( io, byteCount, isPieceData );
|
2009-01-22 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
void
|
2010-12-20 02:07:51 +00:00
|
|
|
tr_peerIoWriteBytes( tr_peerIo * io, const void * bytes, size_t byteCount, tr_bool isPieceData )
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
evbuffer_add( buf, bytes, byteCount );
|
|
|
|
tr_peerIoWriteBuf( io, buf, isPieceData );
|
|
|
|
evbuffer_free( buf );
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-02-09 02:35:16 +00:00
|
|
|
void
|
|
|
|
evbuffer_add_uint8( struct evbuffer * outbuf, uint8_t byte )
|
|
|
|
{
|
|
|
|
evbuffer_add( outbuf, &byte, 1 );
|
|
|
|
}
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
void
|
2010-12-20 02:07:51 +00:00
|
|
|
evbuffer_add_uint16( struct evbuffer * outbuf, uint16_t addme_hs )
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
const uint16_t ns = htons( addme_hs );
|
|
|
|
evbuffer_add( outbuf, &ns, sizeof( ns ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
evbuffer_add_uint32( struct evbuffer * outbuf, uint32_t addme_hl )
|
|
|
|
{
|
|
|
|
const uint32_t nl = htonl( addme_hl );
|
|
|
|
evbuffer_add( outbuf, &nl, sizeof( nl ) );
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2010-12-20 02:07:51 +00:00
|
|
|
tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-01-02 06:28:22 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2010-12-20 02:07:51 +00:00
|
|
|
assert( evbuffer_get_length( inbuf ) >= byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
switch( io->encryptionMode )
|
|
|
|
{
|
|
|
|
case PEER_ENCRYPTION_NONE:
|
2008-08-08 22:44:32 +00:00
|
|
|
evbuffer_remove( inbuf, bytes, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PEER_ENCRYPTION_RC4:
|
2010-12-20 02:07:51 +00:00
|
|
|
evbuffer_remove( inbuf, bytes, byteCount );
|
|
|
|
tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
|
2007-09-20 16:32:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
void
|
|
|
|
tr_peerIoReadUint16( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint16_t * setme )
|
|
|
|
{
|
|
|
|
uint16_t tmp;
|
|
|
|
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
|
|
|
|
*setme = ntohs( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerIoReadUint32( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint32_t * setme )
|
|
|
|
{
|
|
|
|
uint32_t tmp;
|
|
|
|
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
|
|
|
|
*setme = ntohl( tmp );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-12-22 04:55:07 +00:00
|
|
|
tr_peerIoDrain( tr_peerIo * io,
|
2008-09-23 19:11:04 +00:00
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2010-02-23 07:20:57 +00:00
|
|
|
void * buf = tr_sessionGetBuffer( io->session );
|
|
|
|
const size_t buflen = SESSION_BUFFER_SIZE;
|
2008-12-21 18:15:00 +00:00
|
|
|
|
2008-12-29 19:01:47 +00:00
|
|
|
while( byteCount > 0 )
|
|
|
|
{
|
2010-02-23 07:20:57 +00:00
|
|
|
const size_t thisPass = MIN( byteCount, buflen );
|
|
|
|
tr_peerIoReadBytes( io, inbuf, buf, thisPass );
|
2008-12-29 19:01:47 +00:00
|
|
|
byteCount -= thisPass;
|
|
|
|
}
|
2010-02-23 07:20:57 +00:00
|
|
|
|
|
|
|
tr_sessionReleaseBuffer( io->session );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-01-11 02:09:20 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
static int
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_peerIoTryRead( tr_peerIo * io, size_t howmuch )
|
|
|
|
{
|
2009-01-19 14:05:43 +00:00
|
|
|
int res = 0;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch )))
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
|
|
|
int e;
|
2010-06-30 21:24:36 +00:00
|
|
|
|
|
|
|
EVUTIL_SET_SOCKET_ERROR( 0 );
|
2010-07-03 00:25:22 +00:00
|
|
|
res = evbuffer_read( io->inbuf, io->socket, (int)howmuch );
|
2010-06-30 21:24:36 +00:00
|
|
|
e = EVUTIL_SOCKET_ERROR( );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
if( evbuffer_get_length( io->inbuf ) )
|
2008-12-24 02:50:08 +00:00
|
|
|
canReadWrapper( io );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2008-12-24 02:50:08 +00:00
|
|
|
if( ( res <= 0 ) && ( io->gotError ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
|
|
|
|
{
|
2010-06-30 21:24:36 +00:00
|
|
|
char errstr[512];
|
2010-12-20 02:07:51 +00:00
|
|
|
short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
|
2008-12-24 02:50:08 +00:00
|
|
|
if( res == 0 )
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2010-11-11 15:31:11 +00:00
|
|
|
tr_net_strerror( errstr, sizeof( errstr ), e );
|
2010-06-30 21:24:36 +00:00
|
|
|
dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr );
|
2008-12-24 02:50:08 +00:00
|
|
|
io->gotError( io, what, io->userData );
|
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
static int
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch )
|
|
|
|
{
|
2009-01-19 14:05:43 +00:00
|
|
|
int n = 0;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_UP, howmuch )))
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
|
|
|
int e;
|
2010-06-30 21:24:36 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR( 0 );
|
2009-01-03 04:57:40 +00:00
|
|
|
n = tr_evbuffer_write( io, io->socket, howmuch );
|
2010-06-30 21:24:36 +00:00
|
|
|
e = EVUTIL_SOCKET_ERROR( );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2008-12-24 02:50:08 +00:00
|
|
|
if( n > 0 )
|
|
|
|
didWriteWrapper( io, n );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
if( ( n < 0 ) && ( io->gotError ) && e && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2010-06-30 21:24:36 +00:00
|
|
|
char errstr[512];
|
2010-12-20 02:07:51 +00:00
|
|
|
const short what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
|
2010-06-30 21:24:36 +00:00
|
|
|
|
2010-11-11 15:31:11 +00:00
|
|
|
tr_net_strerror( errstr, sizeof( errstr ), e );
|
2010-06-30 21:24:36 +00:00
|
|
|
dbgmsg( io, "tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)", n, what, e, errstr );
|
2010-03-02 04:51:40 +00:00
|
|
|
|
|
|
|
if( io->gotError != NULL )
|
|
|
|
io->gotError( io, what, io->userData );
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
int
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit )
|
|
|
|
{
|
2009-01-20 02:43:21 +00:00
|
|
|
int bytesUsed = 0;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2011-01-13 01:58:57 +00:00
|
|
|
if( dir == TR_DOWN )
|
|
|
|
bytesUsed = tr_peerIoTryRead( io, limit );
|
|
|
|
else
|
|
|
|
bytesUsed = tr_peerIoTryWrite( io, limit );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2011-01-13 01:58:57 +00:00
|
|
|
dbgmsg( io, "flushing peer-io, direction %d, limit %zu, bytesUsed %d", (int)dir, limit, bytesUsed );
|
2009-01-02 19:56:06 +00:00
|
|
|
return bytesUsed;
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
2009-04-21 16:18:51 +00:00
|
|
|
int
|
|
|
|
tr_peerIoFlushOutgoingProtocolMsgs( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
size_t byteCount = 0;
|
2010-04-23 23:45:44 +00:00
|
|
|
tr_list * it;
|
2009-04-21 16:18:51 +00:00
|
|
|
|
|
|
|
/* count up how many bytes are used by non-piece-data messages
|
|
|
|
at the front of our outbound queue */
|
2010-04-23 23:45:44 +00:00
|
|
|
for( it=io->outbuf_datatypes; it!=NULL; it=it->next )
|
|
|
|
{
|
|
|
|
struct tr_datatype * d = it->data;
|
|
|
|
|
2009-04-21 16:18:51 +00:00
|
|
|
if( d->isPieceData )
|
|
|
|
break;
|
2010-04-23 23:45:44 +00:00
|
|
|
|
2009-04-21 16:18:51 +00:00
|
|
|
byteCount += d->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr_peerIoFlush( io, TR_UP, byteCount );
|
|
|
|
}
|