2007-09-20 16:32:01 +00:00
|
|
|
/*
|
2008-12-16 00:20:44 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.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>
|
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
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <event.h>
|
2007-10-25 14:34:20 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.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"
|
|
|
|
#include "peer-io.h"
|
|
|
|
#include "trevent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
#define MAGIC_NUMBER 206745
|
2007-10-11 02:50:05 +00:00
|
|
|
|
2008-11-06 02:56:51 +00:00
|
|
|
static size_t
|
2008-11-26 15:58:26 +00:00
|
|
|
getPacketOverhead( 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
|
|
|
|
*/
|
|
|
|
static const double assumed_payload_data_rate = 94.0;
|
|
|
|
|
2008-11-26 15:58:26 +00:00
|
|
|
return (size_t)( 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
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_peerIo
|
|
|
|
{
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bool isEncrypted;
|
|
|
|
tr_bool isIncoming;
|
|
|
|
tr_bool peerIdIsSet;
|
|
|
|
tr_bool extendedProtocolSupported;
|
|
|
|
tr_bool fastExtensionSupported;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
int magicNumber;
|
2008-05-23 18:56:42 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
uint8_t encryptionMode;
|
|
|
|
tr_port port;
|
|
|
|
int socket;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
uint8_t peerId[20];
|
|
|
|
time_t timeCreated;
|
2007-09-21 14:50:29 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_session * session;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_address addr;
|
|
|
|
tr_list * output_datatypes; /* struct tr_datatype */
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_can_read_cb canRead;
|
|
|
|
tr_did_write_cb didWrite;
|
|
|
|
tr_net_error_cb gotError;
|
|
|
|
void * userData;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
size_t bufferSize[2];
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bandwidth * bandwidth;
|
|
|
|
tr_crypto * crypto;
|
|
|
|
|
|
|
|
struct evbuffer * inbuf;
|
|
|
|
struct evbuffer * outbuf;
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
struct event event_read;
|
|
|
|
struct event event_write;
|
2008-11-25 21:35:17 +00:00
|
|
|
};
|
2008-09-17 19:44:24 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2008-12-20 22:19:34 +00:00
|
|
|
didWriteWrapper( tr_peerIo * io, size_t bytes_transferred )
|
2007-12-01 23:08:34 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
while( bytes_transferred )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
struct tr_datatype * next = io->output_datatypes->data;
|
2008-11-26 15:58:26 +00:00
|
|
|
const size_t payload = MIN( next->length, bytes_transferred );
|
|
|
|
const size_t overhead = getPacketOverhead( payload );
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2008-11-26 15:58:26 +00:00
|
|
|
tr_bandwidthUsed( io->bandwidth, TR_UP, payload, next->isPieceData );
|
|
|
|
|
|
|
|
if( overhead > 0 )
|
|
|
|
tr_bandwidthUsed( io->bandwidth, TR_UP, overhead, FALSE );
|
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 );
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2008-11-26 15:58:26 +00:00
|
|
|
bytes_transferred -= payload;
|
|
|
|
next->length -= payload;
|
2008-11-25 21:35:17 +00:00
|
|
|
if( !next->length )
|
|
|
|
tr_free( tr_list_pop_front( &io->output_datatypes ) );
|
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 done = 0;
|
|
|
|
tr_bool err = 0;
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_session * session = io->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
|
|
|
|
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
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_globalLock( 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;
|
2008-12-16 22:08:17 +00:00
|
|
|
const size_t oldLen = EVBUFFER_LENGTH( io->inbuf );
|
|
|
|
const int ret = io->canRead( io, io->userData, &piece );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
const size_t used = oldLen - EVBUFFER_LENGTH( io->inbuf );
|
|
|
|
|
|
|
|
if( piece )
|
|
|
|
tr_bandwidthUsed( io->bandwidth, TR_DOWN, piece, TRUE );
|
|
|
|
|
|
|
|
if( used != piece )
|
|
|
|
tr_bandwidthUsed( io->bandwidth, TR_DOWN, used - piece, FALSE );
|
2008-09-17 19:44:24 +00:00
|
|
|
|
|
|
|
switch( ret )
|
|
|
|
{
|
2008-09-19 17:03:25 +00:00
|
|
|
case READ_NOW:
|
2008-12-16 22:08:17 +00:00
|
|
|
if( EVBUFFER_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
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
|
|
|
tr_globalUnlock( session );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
#define _isBool(b) (((b)==0 || (b)==1))
|
|
|
|
|
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 )
|
|
|
|
&& ( tr_isAddress( &io->addr ) )
|
|
|
|
&& ( _isBool( io->isEncrypted ) )
|
|
|
|
&& ( _isBool( io->isIncoming ) )
|
|
|
|
&& ( _isBool( io->peerIdIsSet ) )
|
|
|
|
&& ( _isBool( io->extendedProtocolSupported ) )
|
|
|
|
&& ( _isBool( io->fastExtensionSupported ) );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
short what = EVBUFFER_READ;
|
|
|
|
tr_peerIo * io = vio;
|
|
|
|
const size_t howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, io->session->so_rcvbuf );
|
|
|
|
const tr_direction dir = TR_DOWN;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = evbuffer_read( io->inbuf, fd, howmuch );
|
|
|
|
if( res == -1 ) {
|
|
|
|
if( errno == EAGAIN || errno == EINTR )
|
|
|
|
goto reschedule;
|
|
|
|
/* error case */
|
|
|
|
what |= EVBUFFER_ERROR;
|
|
|
|
} else if( res == 0 ) {
|
|
|
|
/* eof case */
|
|
|
|
what |= EVBUFFER_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( res <= 0 )
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
|
|
|
|
/* Invoke the user callback - must always be called last */
|
|
|
|
canReadWrapper( io );
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
reschedule:
|
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
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
|
|
|
|
|
|
|
static int
|
|
|
|
tr_evbuffer_write( tr_peerIo * io, int fd, size_t howmuch )
|
|
|
|
{
|
|
|
|
struct evbuffer * buffer = io->outbuf;
|
|
|
|
int n = MIN( EVBUFFER_LENGTH( buffer ), howmuch );
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
n = send(fd, buffer->buffer, n, 0 );
|
|
|
|
#else
|
|
|
|
n = write(fd, buffer->buffer, n );
|
2008-12-16 22:08:17 +00:00
|
|
|
#endif
|
2008-12-20 22:19:34 +00:00
|
|
|
dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?strerror(errno):"") );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
if( n == -1 )
|
|
|
|
return -1;
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
evbuffer_drain( buffer, n );
|
|
|
|
|
|
|
|
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;
|
|
|
|
short what = EVBUFFER_WRITE;
|
|
|
|
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
|
|
|
|
|
|
|
dbgmsg( io, "libevent says this peer is ready to write" );
|
|
|
|
|
|
|
|
howmuch = MIN( (size_t)io->session->so_sndbuf, EVBUFFER_LENGTH( io->outbuf ) );
|
|
|
|
howmuch = tr_bandwidthClamp( io->bandwidth, dir, howmuch );
|
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop writing */
|
|
|
|
if( howmuch < 1 ) {
|
|
|
|
tr_peerIoSetEnabled( io, dir, FALSE );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = tr_evbuffer_write( io, fd, howmuch );
|
|
|
|
if (res == -1) {
|
|
|
|
#ifndef WIN32
|
|
|
|
/*todo. evbuffer uses WriteFile when WIN32 is set. WIN32 system calls do not
|
|
|
|
* *set errno. thus this error checking is not portable*/
|
|
|
|
if (errno == EAGAIN || errno == EINTR || errno == EINPROGRESS)
|
|
|
|
goto reschedule;
|
|
|
|
/* error case */
|
|
|
|
what |= EVBUFFER_ERROR;
|
|
|
|
|
|
|
|
#else
|
|
|
|
goto reschedule;
|
2008-12-16 22:08:17 +00:00
|
|
|
#endif
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
} else if (res == 0) {
|
|
|
|
/* eof case */
|
|
|
|
what |= EVBUFFER_EOF;
|
|
|
|
}
|
|
|
|
if (res <= 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if( EVBUFFER_LENGTH( io->outbuf ) )
|
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
|
|
|
|
didWriteWrapper( io, res );
|
|
|
|
return;
|
|
|
|
|
|
|
|
reschedule:
|
|
|
|
if( EVBUFFER_LENGTH( io->outbuf ) )
|
|
|
|
tr_peerIoSetEnabled( io, dir, TRUE );
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
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
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2008-12-02 17:10:54 +00:00
|
|
|
static int
|
|
|
|
isFlag( int flag )
|
|
|
|
{
|
|
|
|
return( ( flag == 0 ) || ( flag == 1 ) );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_peerIo*
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_peerIoNew( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
int isIncoming,
|
|
|
|
int 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
|
|
|
|
|
|
|
if( socket >= 0 )
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_netSetTOS( socket, session->peerSocketTOS );
|
|
|
|
|
|
|
|
io = tr_new0( tr_peerIo, 1 );
|
2008-11-25 21:35:17 +00:00
|
|
|
io->magicNumber = MAGIC_NUMBER;
|
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;
|
2008-09-17 19:44:24 +00:00
|
|
|
io->port = port;
|
|
|
|
io->socket = socket;
|
|
|
|
io->isIncoming = isIncoming != 0;
|
|
|
|
io->timeCreated = time( NULL );
|
2008-12-16 22:08:17 +00:00
|
|
|
io->inbuf = evbuffer_new( );
|
|
|
|
io->outbuf = evbuffer_new( );
|
2008-12-20 22:19:34 +00:00
|
|
|
event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
|
|
|
|
event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io );
|
2008-12-16 22:08:17 +00:00
|
|
|
#if 0
|
2008-09-17 19:44:24 +00:00
|
|
|
bufevNew( io );
|
2008-12-16 22:08:17 +00:00
|
|
|
#endif
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_peerIoSetBandwidth( io, session->bandwidth );
|
2008-09-17 19:44:24 +00:00
|
|
|
return io;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_peerIoNewIncoming( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
|
|
|
int socket )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( session );
|
2008-12-02 03:41:58 +00:00
|
|
|
assert( addr );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( socket >= 0 );
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
return tr_peerIoNew( session, addr, port, NULL, 1, socket );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_peerIoNewOutgoing( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
|
|
|
const uint8_t * torrentHash )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-12-15 04:26:31 +00:00
|
|
|
int socket;
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( session );
|
2008-12-02 03:41:58 +00:00
|
|
|
assert( addr );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( torrentHash );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
socket = tr_netOpenTCP( session, addr, port );
|
2007-12-15 04:26:31 +00:00
|
|
|
|
|
|
|
return socket < 0
|
2008-09-23 19:11:04 +00:00
|
|
|
? NULL
|
2008-12-02 03:41:58 +00:00
|
|
|
: tr_peerIoNew( session, addr, port, torrentHash, 0, socket );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
event_del( &io->event_read );
|
|
|
|
event_del( &io->event_write );
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_peerIoSetBandwidth( io, NULL );
|
2008-12-16 22:08:17 +00:00
|
|
|
evbuffer_free( io->outbuf );
|
|
|
|
evbuffer_free( io->inbuf );
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_netClose( io->socket );
|
|
|
|
tr_cryptoFree( io->crypto );
|
2008-11-17 04:00:57 +00:00
|
|
|
tr_list_free( &io->output_datatypes, tr_free );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
io->magicNumber = 0xDEAD;
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_free( io );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
void
|
|
|
|
tr_peerIoFree( tr_peerIo * io )
|
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
if( io )
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
tr_session*
|
|
|
|
tr_peerIoGetSession( tr_peerIo * io )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( io->session );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
return io->session;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
const tr_address*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoGetAddress( const tr_peerIo * io,
|
2008-12-01 20:51:01 +00:00
|
|
|
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
|
|
|
|
2008-12-02 03:41:58 +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;
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
const char*
|
|
|
|
tr_peerIoGetAddrStr( const tr_peerIo * io )
|
|
|
|
{
|
2008-12-02 03:41:58 +00:00
|
|
|
return tr_peerIoAddrStr( &io->addr, io->port );
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bool
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoIsIncoming( const tr_peerIo * c )
|
|
|
|
{
|
2008-12-16 22:08:17 +00:00
|
|
|
return c->isIncoming != 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoReconnect( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( !tr_peerIoIsIncoming( io ) );
|
|
|
|
|
|
|
|
if( io->socket >= 0 )
|
|
|
|
tr_netClose( io->socket );
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
io->socket = tr_netOpenTCP( io->session, &io->addr, io->port );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
if( io->socket >= 0 )
|
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidth * bandwidth = io->bandwidth;
|
|
|
|
tr_peerIoSetBandwidth( io, NULL );
|
2008-04-12 00:29:49 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_netSetTOS( io->socket, io->session->peerSocketTOS );
|
2008-12-16 22:08:17 +00:00
|
|
|
#if 0
|
2008-09-17 19:44:24 +00:00
|
|
|
bufevNew( io );
|
2008-12-16 22:08:17 +00:00
|
|
|
#endif
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
tr_peerIoSetBandwidth( io, bandwidth );
|
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 );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoGetPeersId( const tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( io->peerIdIsSet );
|
|
|
|
|
|
|
|
return io->peerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-12-02 17:10:54 +00:00
|
|
|
void
|
|
|
|
tr_peerIoEnableFEXT( tr_peerIo * io,
|
2008-12-05 22:56:19 +00:00
|
|
|
tr_bool flag )
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-02 17:10:54 +00:00
|
|
|
assert( isFlag( flag ) );
|
|
|
|
|
|
|
|
dbgmsg( io, "setting FEXT support flag to %d", (flag?1:0) );
|
|
|
|
io->fastExtensionSupported = flag;
|
|
|
|
}
|
|
|
|
|
2008-12-05 22:56:19 +00:00
|
|
|
tr_bool
|
2008-12-02 17:10:54 +00:00
|
|
|
tr_peerIoSupportsFEXT( const tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-02 17:10:54 +00:00
|
|
|
|
|
|
|
return io->fastExtensionSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
void
|
2008-12-05 22:56:19 +00:00
|
|
|
tr_peerIoEnableLTEP( tr_peerIo * io,
|
|
|
|
tr_bool flag )
|
2007-09-21 14:50:29 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-02 17:10:54 +00:00
|
|
|
assert( isFlag( flag ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-02 17:10:54 +00:00
|
|
|
dbgmsg( io, "setting LTEP support flag to %d", (flag?1:0) );
|
2008-05-23 18:56:42 +00:00
|
|
|
io->extendedProtocolSupported = flag;
|
2007-09-21 14:50:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-05 22:56:19 +00:00
|
|
|
tr_bool
|
2007-09-21 14:50:29 +00:00
|
|
|
tr_peerIoSupportsLTEP( const tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-23 18:56:42 +00:00
|
|
|
return io->extendedProtocolSupported;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-11-28 16:00:29 +00:00
|
|
|
static size_t
|
|
|
|
getDesiredOutputBufferSize( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
const double maxBlockSize = 16 * 1024; /* 16 KiB is from BT spec */
|
|
|
|
const double currentSpeed = tr_bandwidthGetPieceSpeed( io->bandwidth, TR_UP );
|
|
|
|
const double period = 20; /* arbitrary */
|
2008-12-20 22:19:34 +00:00
|
|
|
const double numBlocks = 5.5; /* the 5 is arbitrary; the .5 is to leave room for messages */
|
|
|
|
return MAX( maxBlockSize*numBlocks, currentSpeed*1024*period );
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
size_t
|
|
|
|
tr_peerIoGetWriteBufferSpace( const tr_peerIo * io )
|
|
|
|
{
|
2008-11-28 16:00:29 +00:00
|
|
|
const size_t desiredLen = getDesiredOutputBufferSize( io );
|
2008-12-16 22:08:17 +00:00
|
|
|
const size_t currentLen = EVBUFFER_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
|
|
|
void
|
2008-11-24 04:21:23 +00:00
|
|
|
tr_peerIoSetBandwidth( tr_peerIo * io,
|
|
|
|
tr_bandwidth * bandwidth )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
if( io->bandwidth )
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bandwidthRemovePeer( io->bandwidth, io );
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
io->bandwidth = bandwidth;
|
|
|
|
|
|
|
|
if( io->bandwidth )
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bandwidthAddPeer( io->bandwidth, io );
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_crypto*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoGetCrypto( tr_peerIo * c )
|
|
|
|
{
|
|
|
|
return c->crypto;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoSetEncryption( tr_peerIo * io,
|
|
|
|
int encryptionMode )
|
|
|
|
{
|
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
|
|
|
|
|| encryptionMode == PEER_ENCRYPTION_RC4 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
io->encryptionMode = encryptionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoIsEncrypted( const tr_peerIo * io )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return io != NULL && io->encryptionMode == PEER_ENCRYPTION_RC4;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
void
|
2008-11-17 04:00:57 +00:00
|
|
|
tr_peerIoWrite( tr_peerIo * io,
|
|
|
|
const void * writeme,
|
|
|
|
size_t writemeLen,
|
|
|
|
int isPieceData )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2008-11-17 04:00:57 +00:00
|
|
|
struct tr_datatype * datatype;
|
2008-09-17 19:44:24 +00:00
|
|
|
assert( tr_amInEventThread( io->session ) );
|
|
|
|
dbgmsg( io, "adding %zu bytes into io->output", writemeLen );
|
|
|
|
|
2008-11-17 04:00:57 +00:00
|
|
|
datatype = tr_new( struct tr_datatype, 1 );
|
|
|
|
datatype->isPieceData = isPieceData != 0;
|
|
|
|
datatype->length = writemeLen;
|
|
|
|
tr_list_append( &io->output_datatypes, datatype );
|
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
evbuffer_add( io->outbuf, writeme, writemeLen );
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-11-17 04:00:57 +00:00
|
|
|
tr_peerIoWriteBuf( tr_peerIo * io,
|
|
|
|
struct evbuffer * buf,
|
|
|
|
int isPieceData )
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
|
|
|
const size_t n = EVBUFFER_LENGTH( buf );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-11-17 04:00:57 +00:00
|
|
|
tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData );
|
2008-09-17 19:44:24 +00:00
|
|
|
evbuffer_drain( buf, n );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteBytes( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
const void * bytes,
|
|
|
|
size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint8_t * tmp;
|
|
|
|
|
|
|
|
switch( io->encryptionMode )
|
|
|
|
{
|
|
|
|
case PEER_ENCRYPTION_NONE:
|
|
|
|
evbuffer_add( outbuf, bytes, byteCount );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PEER_ENCRYPTION_RC4:
|
|
|
|
tmp = tr_new( uint8_t, byteCount );
|
|
|
|
tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp );
|
2007-09-22 21:48:02 +00:00
|
|
|
evbuffer_add( outbuf, tmp, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( tmp );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint8( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint8_t writeme )
|
2007-11-17 17:49:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
|
2007-11-17 17:49:30 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint16( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint16_t writeme )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint16_t tmp = htons( writeme );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoWriteUint32( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint32_t writeme )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint32_t tmp = htonl( writeme );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadBytes( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
void * bytes,
|
|
|
|
size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-19 04:44:14 +00:00
|
|
|
assert( EVBUFFER_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:
|
2008-08-08 22:44:32 +00:00
|
|
|
evbuffer_remove( inbuf, bytes, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadUint8( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint8_t * setme )
|
2007-11-17 17:49:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) );
|
2007-11-17 17:49:30 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadUint16( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint16_t * setme )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint16_t tmp;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
*setme = ntohs( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadUint32( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint32_t * setme )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint32_t tmp;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
*setme = ntohl( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_peerIoDrain( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
uint8_t * tmp;
|
|
|
|
|
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
tmp = tr_new( uint8_t, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoReadBytes( io, inbuf, tmp, byteCount );
|
|
|
|
tr_free( tmp );
|
|
|
|
}
|
2008-01-11 02:09:20 +00:00
|
|
|
|
|
|
|
int
|
2008-01-11 02:40:32 +00:00
|
|
|
tr_peerIoGetAge( const tr_peerIo * io )
|
2008-01-11 02:09:20 +00:00
|
|
|
{
|
|
|
|
return time( NULL ) - io->timeCreated;
|
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
|
|
|
tr_peerIoTryRead( tr_peerIo * io, size_t howmuch )
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch );
|
|
|
|
|
|
|
|
res = howmuch ? evbuffer_read( io->inbuf, io->socket, howmuch ) : 0;
|
|
|
|
|
|
|
|
dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(errno):"") );
|
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
if( EVBUFFER_LENGTH( io->inbuf ) )
|
|
|
|
canReadWrapper( io );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2008-12-17 13:29:03 +00:00
|
|
|
if( ( res <= 0 ) && ( io->gotError ) && ( errno != EAGAIN ) && ( errno != EINTR ) && ( errno != EINPROGRESS ) )
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
|
|
|
short what = EVBUFFER_READ | EVBUFFER_ERROR;
|
|
|
|
if( res == 0 )
|
|
|
|
what |= EVBUFFER_EOF;
|
|
|
|
io->gotError( io, what, io->userData );
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch )
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch );
|
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
n = tr_evbuffer_write( io, io->socket, (int)howmuch );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
if( n > 0 )
|
2008-12-20 22:19:34 +00:00
|
|
|
didWriteWrapper( io, n );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
if( ( n < 0 ) && ( io->gotError ) && ( errno != EPIPE ) && ( errno != EAGAIN ) && ( errno != EINTR ) && ( errno != EINPROGRESS ) ) {
|
2008-12-16 22:08:17 +00:00
|
|
|
short what = EVBUFFER_WRITE | EVBUFFER_ERROR;
|
|
|
|
io->gotError( io, what, io->userData );
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
if( dir==TR_DOWN )
|
|
|
|
ret = tr_peerIoTryRead( io, limit );
|
|
|
|
else
|
|
|
|
ret = tr_peerIoTryWrite( io, limit );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct evbuffer *
|
|
|
|
tr_peerIoGetReadBuffer( tr_peerIo * io )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-16 22:08:17 +00:00
|
|
|
|
|
|
|
return io->inbuf;
|
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
tr_bool
|
|
|
|
tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_enable( tr_peerIo * io, short event )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
if( event & EV_READ )
|
|
|
|
event_add( &io->event_read, NULL );
|
|
|
|
|
|
|
|
if( event & EV_WRITE )
|
|
|
|
event_add( &io->event_write, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_disable( struct tr_peerIo * io, short event )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isPeerIo( io ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
if( event & EV_READ )
|
|
|
|
event_del( &io->event_read );
|
|
|
|
|
|
|
|
if( event & EV_WRITE )
|
|
|
|
event_del( &io->event_write );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoSetEnabled( tr_peerIo * io,
|
|
|
|
tr_direction dir,
|
|
|
|
tr_bool isEnabled )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
short event;
|
|
|
|
|
|
|
|
assert( tr_isPeerIo( io ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
|
|
|
|
|
|
|
event = dir == TR_UP ? EV_WRITE : EV_READ;
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
if( isEnabled )
|
|
|
|
event_enable( io, event );
|
|
|
|
else
|
|
|
|
event_disable( io, event );
|
|
|
|
}
|