2007-09-20 16:32:01 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* 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-09-20 16:32:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2007-10-25 13:59:46 +00:00
|
|
|
#include <netinet/in.h> /* struct in_addr */
|
|
|
|
#include <arpa/inet.h> /* inet_ntoa */
|
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"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "peer-io.h"
|
|
|
|
#include "ratecontrol.h"
|
|
|
|
#include "trevent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2007-10-11 02:50:05 +00:00
|
|
|
#define IO_TIMEOUT_SECS 8
|
|
|
|
|
2007-11-17 00:30:36 +00:00
|
|
|
/* arbitrary */
|
2007-11-18 03:18:26 +00:00
|
|
|
#define TR_RDBUF (1024*8)
|
2007-11-16 21:47:55 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
struct tr_extensions
|
|
|
|
{
|
|
|
|
unsigned int extendedProtocolSupported : 1;
|
|
|
|
unsigned int fastPeersSupported : 1;
|
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_peerIo
|
|
|
|
{
|
|
|
|
struct tr_handle * handle;
|
|
|
|
|
|
|
|
struct in_addr in_addr;
|
|
|
|
int port;
|
|
|
|
int socket;
|
|
|
|
int encryptionMode;
|
2007-11-17 17:49:30 +00:00
|
|
|
int timeout;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct bufferevent * bufev;
|
|
|
|
uint8_t peerId[20];
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
tr_extensions extensions;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
unsigned int isEncrypted : 1;
|
|
|
|
unsigned int isIncoming : 1;
|
|
|
|
unsigned int peerIdIsSet : 1;
|
|
|
|
|
|
|
|
tr_can_read_cb canRead;
|
|
|
|
tr_net_error_cb gotError;
|
|
|
|
void * userData;
|
|
|
|
|
|
|
|
tr_crypto * crypto;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
|
|
|
canReadWrapper( struct bufferevent * e, void * userData )
|
|
|
|
{
|
2007-10-02 02:59:07 +00:00
|
|
|
int done = 0;
|
2007-11-09 04:32:19 +00:00
|
|
|
tr_peerIo * c = userData;
|
2007-10-02 02:59:07 +00:00
|
|
|
tr_handle * handle = c->handle;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
if( c->canRead == NULL )
|
|
|
|
return;
|
|
|
|
|
2007-10-02 02:59:07 +00:00
|
|
|
tr_globalLock( handle );
|
|
|
|
|
|
|
|
while( !done )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const int ret = (*c->canRead)( e, c->userData );
|
2007-10-02 02:59:07 +00:00
|
|
|
|
|
|
|
switch( ret )
|
|
|
|
{
|
|
|
|
case READ_AGAIN:
|
|
|
|
if( EVBUFFER_LENGTH( e->input ) )
|
|
|
|
continue;
|
|
|
|
case READ_MORE:
|
|
|
|
case READ_DONE:
|
|
|
|
done = 1;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-02 02:59:07 +00:00
|
|
|
|
|
|
|
tr_globalUnlock( handle );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gotErrorWrapper( struct bufferevent * e, short what, void * userData )
|
|
|
|
{
|
2007-11-09 04:32:19 +00:00
|
|
|
tr_peerIo * c = userData;
|
2007-09-20 16:32:01 +00:00
|
|
|
if( c->gotError != NULL )
|
|
|
|
(*c->gotError)( e, what, c->userData );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
void bufferevent_setwatermark(struct bufferevent *, short, size_t, size_t);
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_peerIo*
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_peerIoNew( struct tr_handle * handle,
|
|
|
|
const struct in_addr * in_addr,
|
|
|
|
uint16_t port,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
int isIncoming,
|
|
|
|
int socket )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
tr_peerIo * c;
|
|
|
|
c = tr_new0( tr_peerIo, 1 );
|
|
|
|
c->crypto = tr_cryptoNew( torrentHash, isIncoming );
|
|
|
|
c->handle = handle;
|
|
|
|
c->in_addr = *in_addr;
|
2007-09-25 23:10:34 +00:00
|
|
|
c->port = port;
|
2007-09-20 16:32:01 +00:00
|
|
|
c->socket = socket;
|
|
|
|
c->isIncoming = isIncoming ? 1 : 0;
|
2007-11-17 17:49:30 +00:00
|
|
|
c->timeout = IO_TIMEOUT_SECS;
|
2007-09-20 16:32:01 +00:00
|
|
|
c->bufev = bufferevent_new( c->socket,
|
|
|
|
canReadWrapper,
|
2007-11-17 02:47:47 +00:00
|
|
|
NULL,
|
2007-09-20 16:32:01 +00:00
|
|
|
gotErrorWrapper,
|
|
|
|
c );
|
2007-11-17 17:49:30 +00:00
|
|
|
bufferevent_settimeout( c->bufev, c->timeout, c->timeout );
|
2007-09-20 16:32:01 +00:00
|
|
|
bufferevent_enable( c->bufev, EV_READ|EV_WRITE );
|
2007-11-16 21:47:55 +00:00
|
|
|
bufferevent_setwatermark( c->bufev, EV_READ, 0, TR_RDBUF );
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_peerIoNewIncoming( struct tr_handle * handle,
|
|
|
|
const struct in_addr * in_addr,
|
|
|
|
uint16_t port,
|
|
|
|
int socket )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
assert( handle != NULL );
|
|
|
|
assert( in_addr != NULL );
|
|
|
|
assert( socket >= 0 );
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
return tr_peerIoNew( handle, in_addr, port,
|
|
|
|
NULL, 1,
|
|
|
|
socket );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo*
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_peerIoNewOutgoing( struct tr_handle * handle,
|
|
|
|
const struct in_addr * in_addr,
|
|
|
|
int port,
|
|
|
|
const uint8_t * torrentHash )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
assert( handle != NULL );
|
|
|
|
assert( in_addr != NULL );
|
|
|
|
assert( port >= 0 );
|
|
|
|
assert( torrentHash != NULL );
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
return tr_peerIoNew( handle, in_addr, port,
|
|
|
|
torrentHash, 0,
|
|
|
|
tr_netOpenTCP( in_addr, port, 0 ) );
|
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
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
bufferevent_free( io->bufev );
|
|
|
|
tr_netClose( io->socket );
|
|
|
|
tr_cryptoFree( io->crypto );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if( io != NULL )
|
|
|
|
{
|
|
|
|
io->canRead = NULL;
|
|
|
|
io->gotError = NULL;
|
|
|
|
tr_runInEventThread( io->handle, io_dtor, io );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_handle*
|
|
|
|
tr_peerIoGetHandle( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( io->handle != NULL );
|
|
|
|
|
|
|
|
return io->handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct in_addr*
|
|
|
|
tr_peerIoGetAddress( const tr_peerIo * io, uint16_t * port )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
|
|
|
|
if( port != NULL )
|
|
|
|
*port = io->port;
|
|
|
|
|
|
|
|
return &io->in_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_peerIoAddrStr( const struct in_addr * addr, uint16_t port )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
static char buf[512];
|
2007-09-30 23:55:49 +00:00
|
|
|
snprintf( buf, sizeof(buf), "%s:%u", inet_ntoa( *addr ), (unsigned int)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 )
|
|
|
|
{
|
|
|
|
return tr_peerIoAddrStr( &io->in_addr, io->port );
|
|
|
|
}
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
void
|
|
|
|
tr_peerIoTryRead( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
if( EVBUFFER_LENGTH( io->bufev->input ) )
|
|
|
|
canReadWrapper( io->bufev, io );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerIoSetIOFuncs( tr_peerIo * io,
|
|
|
|
tr_can_read_cb readcb,
|
|
|
|
tr_net_error_cb errcb,
|
|
|
|
void * userData )
|
|
|
|
{
|
|
|
|
io->canRead = readcb;
|
|
|
|
io->gotError = errcb;
|
|
|
|
io->userData = userData;
|
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
tr_peerIoTryRead( io );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoIsIncoming( const tr_peerIo * c )
|
|
|
|
{
|
|
|
|
return c->isIncoming ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoReconnect( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( !tr_peerIoIsIncoming( io ) );
|
|
|
|
|
|
|
|
if( io->socket >= 0 )
|
|
|
|
tr_netClose( io->socket );
|
|
|
|
|
|
|
|
io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 );
|
|
|
|
|
|
|
|
if( io->socket >= 0 )
|
|
|
|
{
|
|
|
|
bufferevent_free( io->bufev );
|
|
|
|
|
|
|
|
io->bufev = bufferevent_new( io->socket,
|
2007-11-17 02:47:47 +00:00
|
|
|
canReadWrapper, NULL, gotErrorWrapper,
|
2007-09-20 16:32:01 +00:00
|
|
|
io );
|
2007-11-17 17:49:30 +00:00
|
|
|
bufferevent_settimeout( io->bufev, io->timeout, io->timeout );
|
2007-09-20 16:32:01 +00:00
|
|
|
bufferevent_enable( io->bufev, EV_READ|EV_WRITE );
|
2007-11-16 21:47:55 +00:00
|
|
|
bufferevent_setwatermark( io->bufev, EV_READ, 0, TR_RDBUF );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-11 03:54:33 +00:00
|
|
|
void
|
|
|
|
tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs )
|
|
|
|
{
|
2007-11-17 17:49:30 +00:00
|
|
|
io->timeout = secs;
|
|
|
|
bufferevent_settimeout( io->bufev, io->timeout, io->timeout );
|
2007-10-11 03:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoSetTorrentHash( tr_peerIo * io,
|
|
|
|
const uint8_t * hash )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
|
|
|
|
tr_cryptoSetTorrentHash( io->crypto, hash );
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t*
|
|
|
|
tr_peerIoGetTorrentHash( tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( io->crypto != NULL );
|
|
|
|
|
|
|
|
return tr_cryptoGetTorrentHash( io->crypto );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoHasTorrentHash( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( io->crypto != NULL );
|
|
|
|
|
|
|
|
return tr_cryptoHasTorrentHash( io->crypto );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoSetPeersId( tr_peerIo * io,
|
|
|
|
const uint8_t * peer_id )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
|
|
|
|
if(( io->peerIdIsSet = peer_id != NULL ))
|
|
|
|
memcpy( io->peerId, peer_id, 20 );
|
|
|
|
else
|
|
|
|
memset( io->peerId, 0, 20 );
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t*
|
|
|
|
tr_peerIoGetPeersId( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( io->peerIdIsSet );
|
|
|
|
|
|
|
|
return io->peerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
void
|
|
|
|
tr_peerIoEnableLTEP( tr_peerIo * io, int flag )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( flag==0 || flag==1 );
|
|
|
|
|
|
|
|
io->extensions.extendedProtocolSupported = flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoEnableFEXT( tr_peerIo * io, int flag )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( flag==0 || flag==1 );
|
|
|
|
|
|
|
|
io->extensions.fastPeersSupported = flag;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
int
|
|
|
|
tr_peerIoSupportsLTEP( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
|
|
|
|
return io->extensions.extendedProtocolSupported;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:50:29 +00:00
|
|
|
int
|
|
|
|
tr_peerIoSupportsFEXT( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
|
|
|
|
return io->extensions.fastPeersSupported;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
size_t
|
|
|
|
tr_peerIoWriteBytesWaiting( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
return EVBUFFER_LENGTH( EVBUFFER_OUTPUT( io->bufev ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoWrite( tr_peerIo * io,
|
|
|
|
const void * writeme,
|
|
|
|
int writeme_len )
|
|
|
|
{
|
2007-11-15 16:43:46 +00:00
|
|
|
assert( tr_amInEventThread( io->handle ) );
|
2007-11-18 04:24:17 +00:00
|
|
|
bufferevent_write( io->bufev, writeme, writeme_len );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoWriteBuf( tr_peerIo * io,
|
|
|
|
struct evbuffer * buf )
|
|
|
|
{
|
2007-11-18 03:18:26 +00:00
|
|
|
const size_t n = EVBUFFER_LENGTH( buf );
|
|
|
|
tr_peerIoWrite( io, EVBUFFER_DATA(buf), n );
|
|
|
|
evbuffer_drain( buf, n );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
tr_crypto*
|
|
|
|
tr_peerIoGetCrypto( tr_peerIo * c )
|
|
|
|
{
|
|
|
|
return c->crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoSetEncryption( tr_peerIo * io,
|
|
|
|
int encryptionMode )
|
|
|
|
{
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( encryptionMode==PEER_ENCRYPTION_NONE || encryptionMode==PEER_ENCRYPTION_RC4 );
|
|
|
|
|
|
|
|
io->encryptionMode = encryptionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerIoIsEncrypted( const tr_peerIo * io )
|
|
|
|
{
|
|
|
|
return io!=NULL && io->encryptionMode==PEER_ENCRYPTION_RC4;
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerIoWriteBytes( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
const void * bytes,
|
2007-11-19 04:44:14 +00:00
|
|
|
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
|
|
|
|
tr_peerIoWriteUint8( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint8_t writeme )
|
|
|
|
{
|
|
|
|
tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof(uint8_t) );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerIoWriteUint16( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint16_t writeme )
|
|
|
|
{
|
|
|
|
uint16_t tmp = htons( writeme );
|
|
|
|
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint16_t) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoWriteUint32( tr_peerIo * io,
|
|
|
|
struct evbuffer * outbuf,
|
|
|
|
uint32_t writeme )
|
|
|
|
{
|
|
|
|
uint32_t tmp = htonl( writeme );
|
|
|
|
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint32_t) );
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerIoReadBytes( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
void * bytes,
|
2007-11-19 04:44:14 +00:00
|
|
|
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:
|
2007-11-08 21:25:17 +00:00
|
|
|
evbuffer_remove( inbuf, bytes, byteCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PEER_ENCRYPTION_RC4:
|
2007-11-08 21:25:17 +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
|
|
|
|
tr_peerIoReadUint8( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
|
|
|
uint8_t * setme )
|
|
|
|
{
|
|
|
|
tr_peerIoReadBytes( io, inbuf, setme, sizeof(uint8_t) );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerIoDrain( tr_peerIo * io,
|
|
|
|
struct evbuffer * inbuf,
|
2007-11-19 04:44:14 +00:00
|
|
|
size_t byteCount )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint8_t * tmp = tr_new( uint8_t, byteCount );
|
|
|
|
tr_peerIoReadBytes( io, inbuf, tmp, byteCount );
|
|
|
|
tr_free( tmp );
|
|
|
|
}
|