transmission/libtransmission/ipcparse.c

1179 lines
34 KiB
C
Raw Normal View History

2007-04-18 16:39:10 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2007-2008 Joshua Elsasser
2007-04-18 16:39:10 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
2007-11-09 20:07:52 +00:00
#include <assert.h>
2007-04-18 16:39:10 +00:00
#include <errno.h>
2007-07-12 17:51:45 +00:00
#include <limits.h>
2007-04-18 16:39:10 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "transmission.h"
#include "bencode.h"
#include "utils.h"
2007-05-24 04:05:50 +00:00
#include "ipcparse.h"
2007-04-18 16:39:10 +00:00
#define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( (ary)[0] ) )
2007-05-24 03:44:40 +00:00
#define SAFEFREE( ptr ) \
do \
{ \
int saved = errno; \
free( ptr ); \
errno = saved; \
} \
2007-05-24 03:44:40 +00:00
while( 0 )
#define SAFEBENCFREE( val ) \
do \
{ \
int saved = errno; \
tr_bencFree( val ); \
errno = saved; \
} \
while( 0 )
2007-05-24 03:44:40 +00:00
2007-04-18 16:39:10 +00:00
/* IPC protocol version */
#define PROTO_VERS_MIN ( 1 )
#define PROTO_VERS_MAX ( 2 )
#define MSGVALID( id ) ( IPC__MSG_COUNT > (id) )
#define MSGNAME( id ) ( gl_msgs[(id)].name )
#define DICTPAYLOAD( info ) ( 2 > (info)->vers )
2008-04-04 17:19:44 +00:00
struct ipc_funcs
{
trd_msgfunc msgs[IPC__MSG_COUNT];
trd_msgfunc def;
};
struct ipc_info
{
struct ipc_funcs * funcs;
int vers;
};
int
ipc_hasvers( const struct ipc_info * inf )
{
return inf && ( inf->vers > 0 );
}
2007-04-18 16:39:10 +00:00
struct msg
{
const char * name;
const int minvers;
const enum ipc_msg id;
2007-04-18 16:39:10 +00:00
};
/* these names must be sorted for strcmp() */
static const struct msg gl_msgs[] =
2007-04-18 16:39:10 +00:00
{
{ "addfile-detailed", 2, IPC_MSG_ADDONEFILE },
{ "addfiles", 1, IPC_MSG_ADDMANYFILES },
{ "automap", 2, IPC_MSG_AUTOMAP },
{ "autostart", 2, IPC_MSG_AUTOSTART },
{ "bad-format", 2, IPC_MSG_BAD },
{ "directory", 2, IPC_MSG_DIR },
{ "downlimit", 2, IPC_MSG_DOWNLIMIT },
{ "encryption", 2, IPC_MSG_CRYPTO },
{ "failed", 2, IPC_MSG_FAIL },
{ "get-automap", 2, IPC_MSG_GETAUTOMAP },
{ "get-autostart", 2, IPC_MSG_GETAUTOSTART },
{ "get-directory", 2, IPC_MSG_GETDIR },
{ "get-downlimit", 2, IPC_MSG_GETDOWNLIMIT },
{ "get-encryption", 2, IPC_MSG_GETCRYPTO },
{ "get-info", 2, IPC_MSG_GETINFO },
{ "get-info-all", 2, IPC_MSG_GETINFOALL },
{ "get-pex", 2, IPC_MSG_GETPEX },
{ "get-port", 2, IPC_MSG_GETPORT },
{ "get-status", 2, IPC_MSG_GETSTAT },
{ "get-status-all", 2, IPC_MSG_GETSTATALL },
{ "get-supported", 2, IPC_MSG_GETSUP },
{ "get-uplimit", 2, IPC_MSG_GETUPLIMIT },
{ "info", 2, IPC_MSG_INFO },
{ "lookup", 2, IPC_MSG_LOOKUP },
{ "noop", 2, IPC_MSG_NOOP },
{ "not-supported", 2, IPC_MSG_NOTSUP },
{ "pex", 2, IPC_MSG_PEX },
{ "port", 2, IPC_MSG_PORT },
{ "quit", 1, IPC_MSG_QUIT },
{ "remove", 2, IPC_MSG_REMOVE },
{ "remove-all", 2, IPC_MSG_REMOVEALL },
{ "start", 2, IPC_MSG_START },
{ "start-all", 2, IPC_MSG_STARTALL },
{ "status", 2, IPC_MSG_STAT },
{ "stop", 2, IPC_MSG_STOP },
{ "stop-all", 2, IPC_MSG_STOPALL },
{ "succeeded", 2, IPC_MSG_OK },
{ "supported", 2, IPC_MSG_SUP },
{ "uplimit", 2, IPC_MSG_UPLIMIT },
2008-02-27 17:38:39 +00:00
{ "verify", 2, IPC_MSG_VERIFY },
{ "version", 1, IPC_MSG_VERSION }
2007-04-18 16:39:10 +00:00
};
2008-04-04 17:19:44 +00:00
struct inf
{
const char * name;
const int type;
};
/* these names must be sorted for strcmp() */
static const struct inf gl_inf[] =
2007-04-18 16:39:10 +00:00
{
{ "comment", IPC_INF_COMMENT },
{ "creator", IPC_INF_CREATOR },
{ "date", IPC_INF_DATE },
{ "files", IPC_INF_FILES },
{ "hash", IPC_INF_HASH },
{ "id", IPC_INF_ID },
{ "name", IPC_INF_NAME },
{ "path", IPC_INF_PATH },
{ "private", IPC_INF_PRIVATE },
{ "size", IPC_INF_SIZE },
{ "trackers", IPC_INF_TRACKERS }
2007-04-18 16:39:10 +00:00
};
/* these names must be sorted for strcmp() */
static const struct inf gl_stat[] =
2007-04-18 16:39:10 +00:00
{
{ "completed", IPC_ST_COMPLETED },
{ "download-speed", IPC_ST_DOWNSPEED },
{ "download-total", IPC_ST_DOWNTOTAL },
{ "download-valid", IPC_ST_DOWNVALID },
{ "error", IPC_ST_ERROR },
{ "error-message", IPC_ST_ERRMSG },
{ "eta", IPC_ST_ETA },
{ "id", IPC_ST_ID },
{ "peers-downloading", IPC_ST_PEERDOWN },
{ "peers-from", IPC_ST_PEERFROM },
{ "peers-total", IPC_ST_PEERTOTAL },
{ "peers-uploading", IPC_ST_PEERUP },
{ "running", IPC_ST_RUNNING },
{ "scrape-completed", IPC_ST_TKDONE },
{ "scrape-leechers", IPC_ST_TKLEECH },
{ "scrape-seeders", IPC_ST_TKSEED },
{ "state", IPC_ST_STATE },
{ "swarm-speed", IPC_ST_SWARM },
{ "tracker", IPC_ST_TRACKER },
{ "upload-speed", IPC_ST_UPSPEED },
{ "upload-total", IPC_ST_UPTOTAL }
2007-04-18 16:39:10 +00:00
};
struct ipc_funcs *
ipc_initmsgs( void )
{
return tr_new0( struct ipc_funcs, 1 );
2007-04-18 16:39:10 +00:00
}
void
2008-04-04 17:19:44 +00:00
ipc_addmsg( struct ipc_funcs * funcs,
enum ipc_msg msg_id,
trd_msgfunc func )
2007-04-18 16:39:10 +00:00
{
assert( MSGVALID( msg_id ) );
assert( IPC_MSG_VERSION != msg_id );
2007-04-18 16:39:10 +00:00
funcs->msgs[msg_id] = func;
2007-04-18 16:39:10 +00:00
}
void
ipc_setdefmsg( struct ipc_funcs * funcs, trd_msgfunc func )
2007-04-18 16:39:10 +00:00
{
funcs->def = func;
2007-04-18 16:39:10 +00:00
}
void
ipc_freemsgs( struct ipc_funcs * funcs )
2007-04-18 16:39:10 +00:00
{
tr_free( funcs );
2007-04-18 16:39:10 +00:00
}
struct ipc_info *
ipc_newcon( struct ipc_funcs * funcs )
{
struct ipc_info * info = tr_new0( struct ipc_info, 1 );
info->funcs = funcs;
info->vers = -1;
return info;
}
2007-04-18 16:39:10 +00:00
void
2008-04-04 17:19:44 +00:00
ipc_freecon( struct ipc_info * session )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
tr_free( session );
}
int
2008-04-04 17:19:44 +00:00
ipc_ishandled( const struct ipc_info * session, enum ipc_msg msg_id )
{
2008-04-04 17:19:44 +00:00
assert( MSGVALID( msg_id ) );
2008-04-04 17:19:44 +00:00
return session->funcs->msgs[msg_id] != NULL;
}
int
2008-04-04 17:19:44 +00:00
ipc_havetags( const struct ipc_info * session )
{
2008-04-04 17:19:44 +00:00
return !DICTPAYLOAD( session );
}
static int
sessionSupportsTags( const struct ipc_info * session )
{
return session->vers >= 2;
}
static int
2008-04-04 17:19:44 +00:00
sessionSupportsMessage( const struct ipc_info * session, enum ipc_msg id )
{
assert( MSGVALID( id ) );
2008-04-04 17:19:44 +00:00
assert( ipc_hasvers( session ) );
2008-04-04 17:19:44 +00:00
return gl_msgs[id].minvers <= session->vers;
2007-04-18 16:39:10 +00:00
}
/**
* Creates the benc metainfo structure for a message
* and returns its child where payload should be set.
*
* In protocol version 1, the metainfo is a single-entry
* dictionary with a string from gl_msgs as the key
* and the return tr_benc pointer as the value.
*
* In protocol version 2, the metainfo is a list
* holding a string from gl_msgs, the return benc pointer,
* and (optionally) the integer tag.
*/
tr_benc *
ipc_initval( const struct ipc_info * session,
enum ipc_msg msg_id,
int64_t tag,
tr_benc * pk,
int benc_type )
2007-04-18 16:39:10 +00:00
{
tr_benc * ret;
2007-04-18 16:39:10 +00:00
assert( MSGVALID( msg_id ) );
2007-04-18 16:39:10 +00:00
if( !sessionSupportsMessage( session, msg_id )
|| ( (tag>0) && !sessionSupportsTags( session ) ) )
2007-04-18 16:39:10 +00:00
{
errno = EPERM;
return NULL;
}
if( DICTPAYLOAD( session ) )
2007-04-18 16:39:10 +00:00
{
tr_bencInitDict( pk, 1 );
ret = tr_bencDictAdd( pk, MSGNAME( msg_id ) );
2007-04-18 16:39:10 +00:00
}
else
{
tr_bencInitList( pk, 3 );
tr_bencInitStr( tr_bencListAdd( pk ), MSGNAME( msg_id ), -1, 1 );
2007-04-18 16:39:10 +00:00
ret = tr_bencListAdd( pk );
if( 0 < tag )
tr_bencInitInt( tr_bencListAdd( pk ), tag );
}
tr_bencInit( ret, benc_type );
2007-04-18 16:39:10 +00:00
return ret;
}
/**
* Serialize a benc message into a string appended to a
* printf()'ed string IPC_MIN_MSG_LEN bytes long that
* gives the length of the string.
*/
2007-04-18 16:39:10 +00:00
uint8_t *
2008-04-04 17:19:44 +00:00
ipc_serialize( const tr_benc * benc, size_t * setmeSize )
2007-04-18 16:39:10 +00:00
{
uint8_t * ret = NULL;
2008-04-04 17:19:44 +00:00
int len = 0;
char * str = tr_bencSave( benc, &len );
2007-04-18 16:39:10 +00:00
2008-04-04 17:19:44 +00:00
if( len > IPC_MAX_MSG_LEN )
2007-04-18 16:39:10 +00:00
errno = EFBIG;
else {
2008-04-04 17:19:44 +00:00
const size_t size = IPC_MIN_MSG_LEN + len;
ret = tr_new( uint8_t, size );
2008-04-04 17:19:44 +00:00
snprintf( (char*)ret, size, "%0*X", IPC_MIN_MSG_LEN, len );
memcpy( ret + IPC_MIN_MSG_LEN, str, len );
*setmeSize = size;
2007-04-18 16:39:10 +00:00
}
2008-04-04 17:19:44 +00:00
tr_free( str );
return ret;
2007-04-18 16:39:10 +00:00
}
/**
* Create a serialized message whose payload is a NULL string
*/
2007-04-18 16:39:10 +00:00
uint8_t *
ipc_mkempty( const struct ipc_info * session,
size_t * setmeSize,
enum ipc_msg msg_id,
int64_t tag )
2007-04-18 16:39:10 +00:00
{
return ipc_mkstr( session, setmeSize, msg_id, tag, NULL );
2007-04-18 16:39:10 +00:00
}
/**
* Create a serialized message whose payload is an integer
*/
2007-04-18 16:39:10 +00:00
uint8_t *
ipc_mkint( const struct ipc_info * session,
size_t * setmeSize,
enum ipc_msg msg_id,
int64_t tag,
int64_t num )
2007-04-18 16:39:10 +00:00
{
tr_benc pk, * val;
uint8_t * ret = NULL;
2007-04-18 16:39:10 +00:00
if(( val = ipc_initval( session, msg_id, tag, &pk, TYPE_INT )))
{
val->val.i = num;
ret = ipc_serialize( &pk, setmeSize );
SAFEBENCFREE( &pk );
}
2007-04-18 16:39:10 +00:00
return ret;
}
/**
* Create a serialized message whose payload is a string
*/
2007-04-18 16:39:10 +00:00
uint8_t *
ipc_mkstr( const struct ipc_info * session,
size_t * setmeSize,
enum ipc_msg msg_id,
int64_t tag,
const char * str )
2007-04-18 16:39:10 +00:00
{
tr_benc pk, * val;
uint8_t * ret = NULL;
2007-04-18 16:39:10 +00:00
if(( val = ipc_initval( session, msg_id, tag, &pk, TYPE_STR )))
{
tr_bencInitStr( val, str, -1, 1 );
ret = ipc_serialize( &pk, setmeSize );
SAFEBENCFREE( &pk );
}
2007-04-18 16:39:10 +00:00
return ret;
}
/**
* Create a serialized message whose payload is a dictionary
* giving the minimum and maximum protocol version we support,
* and (optionally) the label passed in.
*
* Note that this message is just the dictionary payload.
* It doesn't contain metainfo as the other ipc_mk*() functions do.
2008-04-04 17:19:44 +00:00
* That's because the metainfo is dependent on the protocol version,
* and this is a handshake message to negotiate protocol versions.
*
* @see handlevers()
*/
2007-04-18 16:39:10 +00:00
uint8_t *
ipc_mkvers( size_t * len, const char * label )
2007-04-18 16:39:10 +00:00
{
tr_benc pk, * dict;
2007-04-18 16:39:10 +00:00
uint8_t * ret;
tr_bencInitDict( &pk, 1 );
2007-04-18 16:39:10 +00:00
dict = tr_bencDictAdd( &pk, MSGNAME( IPC_MSG_VERSION ) );
tr_bencInitDict( dict, 3 );
2007-04-18 16:39:10 +00:00
tr_bencInitInt( tr_bencDictAdd( dict, "min" ), PROTO_VERS_MIN );
tr_bencInitInt( tr_bencDictAdd( dict, "max" ), PROTO_VERS_MAX );
if( label )
tr_bencInitStr( tr_bencDictAdd( dict, "label" ), label, -1, 1 );
2007-04-18 16:39:10 +00:00
ret = ipc_serialize( &pk, len );
2007-04-18 16:39:10 +00:00
SAFEBENCFREE( &pk );
return ret;
}
/**
* Create a serialized message that is used to request
* torrent information or statistics.
*
* msg_id must be one of:
* IPC_MSG_GETINFO
* IPC_MSG_GETINFOALL
* IPC_MSG_GETSTAT
* IPC_MSG_GETSTATALL
*
* "ids" is an optional array of torrent IDs.
* The array, if included, must be terminated by a 0 torrent id.
*
* "types" is a bitwise-and'ed set of fields from either
* the IPC_INF_* or IPC_ST_* enums in ipc-parse.h.
* Which enums are used is dependent on the value of msg_id.
*
* If torrent ids are specified in the "ids" array,
* the payload is a dictionary of two lists, "id" and "type".
* The "id" list holds the torrent IDs, and
* the "type" list holds string keys from either
* gl_inf or gl_stat, depending on the value of msg_id
*
* If no torrent ids are specified, the payload is
* a single list identical to the "type" list described above.
*/
2007-04-18 16:39:10 +00:00
uint8_t *
2008-04-04 17:19:44 +00:00
ipc_createInfoRequest( const struct ipc_info * session,
size_t * setmeSize,
enum ipc_msg msg_id,
int64_t tag,
int types,
const int * ids )
2007-04-18 16:39:10 +00:00
{
tr_benc pk;
tr_benc * typelist;
2007-04-18 16:39:10 +00:00
size_t ii, typecount, used;
const struct inf * typearray;
2007-04-18 16:39:10 +00:00
uint8_t * ret;
/* no ID list, send an -all message */
if( !ids ) {
typelist = ipc_initval( session, msg_id, tag, &pk, TYPE_LIST );
if( !typelist )
2007-04-18 16:39:10 +00:00
return NULL;
}
else
{
tr_benc * top;
tr_benc * idlist;
top = ipc_initval( session, msg_id, tag, &pk, TYPE_DICT );
if( !top )
2007-04-18 16:39:10 +00:00
return NULL;
2007-04-18 16:39:10 +00:00
/* add the requested IDs */
tr_bencDictReserve( top, 2 );
2007-04-18 16:39:10 +00:00
idlist = tr_bencDictAdd( top, "id" );
typelist = tr_bencDictAdd( top, "type" );
tr_bencInit( typelist, TYPE_LIST );
for( ii = 0; TORRENT_ID_VALID( ids[ii] ); ii++ ) { }
tr_bencInitList( idlist, ii );
2007-04-18 16:39:10 +00:00
for( ii = 0; TORRENT_ID_VALID( ids[ii] ); ii++ )
tr_bencInitInt( tr_bencListAdd( idlist ), ids[ii] );
}
/* get the type name array */
switch( msg_id )
2007-04-18 16:39:10 +00:00
{
case IPC_MSG_GETINFO:
case IPC_MSG_GETINFOALL:
typecount = TR_N_ELEMENTS( gl_inf );
2007-04-18 16:39:10 +00:00
typearray = gl_inf;
break;
case IPC_MSG_GETSTAT:
case IPC_MSG_GETSTATALL:
typecount = TR_N_ELEMENTS( gl_stat );
2007-04-18 16:39:10 +00:00
typearray = gl_stat;
break;
default:
assert( 0 );
break;
}
/* add the type names */
for( ii = used = 0; typecount > ii; ii++ )
if( types & ( 1 << ii ) )
used++;
tr_bencListReserve( typelist, used );
2007-04-18 16:39:10 +00:00
for( ii = 0; typecount > ii; ii++ )
{
if( !( types & ( 1 << ii ) ) )
continue;
assert( typearray[ii].type == ( 1 << ii ) );
tr_bencInitStr( tr_bencListAdd( typelist ),
typearray[ii].name, -1, 1 );
}
/* generate packet */
ret = ipc_serialize( &pk, setmeSize );
2007-04-18 16:39:10 +00:00
SAFEBENCFREE( &pk );
return ret;
}
static void
filltracker( tr_benc * val, const tr_tracker_info * tk )
{
tr_bencInitDict( val, 4 );
tr_bencInitStr( tr_bencDictAdd( val, "address" ), tk->address, -1, 1 );
tr_bencInitInt( tr_bencDictAdd( val, "port" ), tk->port );
tr_bencInitStr( tr_bencDictAdd( val, "announce" ), tk->announce, -1, 1 );
2008-04-04 17:19:44 +00:00
if( tk->scrape )
tr_bencInitStr( tr_bencDictAdd( val, "scrape" ), tk->scrape, -1, 1 );
}
/**
* append to "list" a dictionary whose keys are
* the string keys from gl_inf and whose values are
* torrent info set from "torrent_id" and "inf".
*
* "types" is a bitwise-and'ed set of fields
* from the IPC_INF_* enum in ipcparse.h.
* It specifies what to put in the dictionary.
*/
2007-04-18 16:39:10 +00:00
int
ipc_addinfo( tr_benc * list,
int torrent_id,
const tr_info * inf,
int types )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
tr_benc * dict;
2007-04-18 16:39:10 +00:00
int ii, jj, kk;
tr_file_index_t ff;
2007-04-18 16:39:10 +00:00
/* always send torrent id */
types |= IPC_INF_ID;
tr_bencListReserve( list, 1 );
2007-04-18 16:39:10 +00:00
dict = tr_bencListAdd( list );
/* count the number of info keys and allocate a dict for them */
2007-04-18 16:39:10 +00:00
for( ii = jj = 0; IPC_INF__MAX > 1 << ii; ii++ )
{
if( !( types & ( 1 << ii ) ) )
continue;
assert( TR_N_ELEMENTS( gl_inf ) > ( unsigned )ii );
2007-04-18 16:39:10 +00:00
assert( gl_inf[ii].type == ( 1 << ii ) );
jj++;
}
tr_bencInitDict( dict, jj );
/* populate the dict with info key->value pairs */
2007-04-18 16:39:10 +00:00
for( ii = 0; IPC_INF__MAX > 1 << ii; ii++ )
{
2008-04-04 17:19:44 +00:00
tr_benc * item;
2007-04-18 16:39:10 +00:00
if( !( types & ( 1 << ii ) ) )
continue;
item = tr_bencDictAdd( dict, gl_inf[ii].name );
switch( 1 << ii )
{
case IPC_INF_COMMENT:
tr_bencInitStr( item, inf->comment ? inf->comment : "", -1, 1 );
2007-04-18 16:39:10 +00:00
break;
case IPC_INF_CREATOR:
tr_bencInitStr( item, inf->creator ? inf->creator : "", -1, 1 );
2007-04-18 16:39:10 +00:00
break;
case IPC_INF_DATE:
tr_bencInitInt( item, inf->dateCreated );
break;
case IPC_INF_FILES:
tr_bencInitList( item, inf->fileCount );
for( ff = 0; inf->fileCount > ff; ff++ )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
tr_benc * file = tr_bencListAdd( item );
tr_bencInitDict( file, 2 );
2007-04-18 16:39:10 +00:00
tr_bencInitStr( tr_bencDictAdd( file, "name" ),
inf->files[ff].name, -1, 1 );
2007-04-18 16:39:10 +00:00
tr_bencInitInt( tr_bencDictAdd( file, "size" ),
inf->files[ff].length );
2007-04-18 16:39:10 +00:00
}
break;
case IPC_INF_HASH:
tr_bencInitStr( item, inf->hashString, -1, 1 );
break;
case IPC_INF_ID:
tr_bencInitInt( item, torrent_id );
2007-04-18 16:39:10 +00:00
break;
case IPC_INF_NAME:
tr_bencInitStr( item, inf->name, -1, 1 );
break;
case IPC_INF_PATH:
tr_bencInitStr( item, inf->torrent, -1, 1 );
break;
case IPC_INF_PRIVATE:
2007-10-15 20:58:39 +00:00
tr_bencInitInt( item, inf->isPrivate ? 1 : 0 );
2007-04-18 16:39:10 +00:00
break;
case IPC_INF_SIZE:
tr_bencInitInt( item, inf->totalSize );
break;
case IPC_INF_TRACKERS:
tr_bencInitList( item, inf->trackerTiers );
2007-04-18 16:39:10 +00:00
for( jj = 0; inf->trackerTiers > jj; jj++ )
{
2008-04-04 17:19:44 +00:00
tr_benc * tier = tr_bencListAdd( item );
tr_bencInitList( tier, inf->trackerList[jj].count );
2007-04-18 16:39:10 +00:00
for( kk = 0; inf->trackerList[jj].count > kk; kk++ )
filltracker( tr_bencListAdd( tier ),
&inf->trackerList[jj].list[kk] );
2007-04-18 16:39:10 +00:00
}
break;
default:
assert( 0 );
break;
}
}
return 0;
}
/**
* append to "list" a dictionary whose keys are
* the string keys from gl_stat and whose values
* are torrent statistics set from "st".
*
* "types" is a bitwise-and'ed set of fields
* from the IPC_INF_* enum in ipcparse.h.
* It specifies what to put in the dictionary.
*/
2007-04-18 16:39:10 +00:00
int
ipc_addstat( tr_benc * list,
int torrent_id,
const tr_stat * st,
int types )
2007-04-18 16:39:10 +00:00
{
tr_benc * dict;
int ii, used;
/* add the dictionary child */
tr_bencListReserve( list, 1 );
dict = tr_bencListAdd( list );
2007-04-18 16:39:10 +00:00
/* always send torrent id */
types |= IPC_ST_ID;
/* count the number of stat keys and allocate a dict for them */
2007-04-18 16:39:10 +00:00
for( ii = used = 0; IPC_ST__MAX > 1 << ii; ii++ )
if( types & ( 1 << ii ) )
used++;
tr_bencInitDict( dict, used );
/* populate the dict */
2007-04-18 16:39:10 +00:00
for( ii = 0; IPC_ST__MAX > 1 << ii; ii++ )
{
tr_benc * item;
2007-04-18 16:39:10 +00:00
if( !( types & ( 1 << ii ) ) )
continue;
assert( TR_N_ELEMENTS( gl_stat ) > ( unsigned )ii );
2007-04-18 16:39:10 +00:00
assert( gl_stat[ii].type == ( 1 << ii ) );
item = tr_bencDictAdd( dict, gl_stat[ii].name );
switch( 1 << ii )
{
case IPC_ST_COMPLETED:
case IPC_ST_DOWNVALID:
tr_bencInitInt( item, st->haveValid );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_DOWNSPEED:
tr_bencInitInt( item, st->rateDownload * 1024 );
break;
case IPC_ST_DOWNTOTAL:
tr_bencInitInt( item, st->downloadedEver );
break;
case IPC_ST_ERROR: {
const tr_errno error = st->error;
2007-05-24 04:02:36 +00:00
if( TR_OK == error )
2007-04-18 16:39:10 +00:00
{
2008-01-07 00:42:53 +00:00
tr_bencInitStr( item, "", -1, 1 );
2007-04-18 16:39:10 +00:00
}
else if( error == TR_ERROR_ASSERT )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "assert", -1, 1 );
}
else if( error == TR_ERROR_IO_PERMISSIONS )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "io-permissions", -1, 1 );
}
else if( error == TR_ERROR_IO_SPACE )
{
tr_bencInitStr( item, "io-space", -1, 1 );
}
else if( error == TR_ERROR_IO_FILE_TOO_BIG )
{
tr_bencInitStr( item, "io-file-too-big", -1, 1 );
}
else if( error == TR_ERROR_IO_OPEN_FILES )
{
tr_bencInitStr( item, "io-open-files", -1, 1 );
}
else if( TR_ERROR_IS_IO( error ) )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "io-other", -1, 1 );
}
else if( error == TR_ERROR_TC_ERROR )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "tracker-error", -1, 1 );
}
else if( error == TR_ERROR_TC_WARNING )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "tracker-warning", -1, 1 );
}
else if( TR_ERROR_IS_TC( error ) )
{
tr_bencInitStr( item, "tracker-other", -1, 1 );
}
2007-04-18 16:39:10 +00:00
else
{
tr_bencInitStr( item, "other", -1, 1 );
}
break;
}
2007-04-18 16:39:10 +00:00
case IPC_ST_ERRMSG:
2008-01-07 00:42:53 +00:00
if( TR_OK == st->error )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "", -1, 1 );
2007-04-18 16:39:10 +00:00
}
2008-01-07 00:42:53 +00:00
else if( '\0' == st->errorString[0] )
{
tr_bencInitStr( item, "other", -1, 1 );
}
2007-04-18 16:39:10 +00:00
else if( tr_bencInitStrDup( item, st->errorString ) )
{
return -1;
}
break;
case IPC_ST_ETA:
tr_bencInitInt( item, st->eta );
break;
case IPC_ST_ID:
tr_bencInitInt( item, torrent_id );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_PEERDOWN:
tr_bencInitInt( item, st->peersSendingToUs );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_PEERFROM:
tr_bencInitDict( item, 4 );
2007-04-18 16:39:10 +00:00
tr_bencInitInt( tr_bencDictAdd( item, "incoming" ),
st->peersFrom[TR_PEER_FROM_INCOMING] );
tr_bencInitInt( tr_bencDictAdd( item, "tracker" ),
st->peersFrom[TR_PEER_FROM_TRACKER] );
tr_bencInitInt( tr_bencDictAdd( item, "cache" ),
st->peersFrom[TR_PEER_FROM_CACHE] );
tr_bencInitInt( tr_bencDictAdd( item, "pex" ),
st->peersFrom[TR_PEER_FROM_PEX] );
break;
case IPC_ST_PEERTOTAL:
tr_bencInitInt( item, st->peersConnected );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_PEERUP:
tr_bencInitInt( item, st->peersGettingFromUs );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_RUNNING:
tr_bencInitInt( item, TR_STATUS_IS_ACTIVE(st->status) );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_STATE:
if( TR_STATUS_CHECK_WAIT & st->status )
{
tr_bencInitStr( item, "waiting to checking", -1, 1 );
}
else if( TR_STATUS_CHECK & st->status )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "checking", -1, 1 );
}
else if( TR_STATUS_DOWNLOAD & st->status )
{
tr_bencInitStr( item, "downloading", -1, 1 );
}
else if( TR_STATUS_SEED & st->status )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "seeding", -1, 1 );
}
else if( TR_STATUS_STOPPED & st->status )
2007-04-18 16:39:10 +00:00
{
tr_bencInitStr( item, "paused", -1, 1 );
}
else
{
assert( 0 );
}
break;
case IPC_ST_SWARM:
tr_bencInitInt( item, st->swarmspeed * 1024 );
break;
case IPC_ST_TRACKER:
filltracker( item, st->tracker );
2007-04-18 16:39:10 +00:00
break;
case IPC_ST_TKDONE:
tr_bencInitInt( item, st->completedFromTracker );
break;
case IPC_ST_TKLEECH:
tr_bencInitInt( item, st->leechers );
break;
case IPC_ST_TKSEED:
tr_bencInitInt( item, st->seeders );
break;
case IPC_ST_UPSPEED:
tr_bencInitInt( item, st->rateUpload * 1024 );
break;
case IPC_ST_UPTOTAL:
tr_bencInitInt( item, st->uploadedEver );
2007-04-18 16:39:10 +00:00
break;
default:
assert( 0 );
break;
}
}
return 0;
}
2008-04-04 17:19:44 +00:00
/**
* This reads a handshake message from the client to decide
* which IPC protocol version to use.
* Returns 0 on success; otherwise, returns -1 and sets errno.
*
* @see ipc_handleMessages()
* @see ipc_mkvers()
*/
static int
handlevers( struct ipc_info * info, tr_benc * dict )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
tr_benc * vers;
2007-04-18 16:39:10 +00:00
int64_t min, max;
2008-04-04 17:19:44 +00:00
if( !tr_bencIsDict( dict ) )
2007-04-18 16:39:10 +00:00
{
errno = EINVAL;
return -1;
}
vers = tr_bencDictFind( dict, MSGNAME( IPC_MSG_VERSION ) );
if( NULL == vers )
{
errno = EINVAL;
return -1;
}
switch( vers->type )
{
case TYPE_INT:
min = vers->val.i;
max = vers->val.i;
break;
2008-04-04 17:19:44 +00:00
case TYPE_DICT: {
tr_benc * num = tr_bencDictFind( vers, "min" );
min = tr_bencIsInt( num ) ? num->val.i : -1;
2007-04-18 16:39:10 +00:00
num = tr_bencDictFind( vers, "max" );
2008-04-04 17:19:44 +00:00
max = tr_bencIsInt( num ) ? num->val.i : -1;
2007-04-18 16:39:10 +00:00
break;
2008-04-04 17:19:44 +00:00
}
2007-04-18 16:39:10 +00:00
default:
min = -1;
max = -1;
break;
}
if( 0 >= min || 0 >= max || INT_MAX < min || INT_MAX < max )
{
errno = EINVAL;
return -1;
}
assert( PROTO_VERS_MIN <= PROTO_VERS_MAX );
if( min > max )
{
errno = EINVAL;
return -1;
}
if( PROTO_VERS_MAX < min || PROTO_VERS_MIN > max )
{
errno = EPERM;
return -1;
}
info->vers = MIN( PROTO_VERS_MAX, max );
return 0;
}
static int
compareNameToMsg( const void * a, const void * b )
{
const struct msg * msg = b;
return strcmp( a, msg->name );
}
static const struct msg *
msglookup( const char * name )
{
return bsearch( name,
gl_msgs, TR_N_ELEMENTS( gl_msgs ), sizeof( struct msg ),
compareNameToMsg );
}
enum ipc_msg
ipc_msgid( const struct ipc_info * info, const char * name )
{
const struct msg * msg = msglookup( name );
return msg && sessionSupportsMessage( info, msg->id )
? msg->id
: IPC__MSG_COUNT;
}
2008-04-04 17:19:44 +00:00
/**
* Invokes the trd_msgfunc for the message passed in.
* Returns 0 on success; otherwise, returns -1 and sets errno.
*/
static int
2008-04-04 17:19:44 +00:00
callmsgfunc( const struct ipc_info * info,
tr_benc * name,
tr_benc * val,
tr_benc * tagval,
void * user_data )
{
const struct msg * msg;
int64_t tag;
2008-04-04 17:19:44 +00:00
/* extract tag from tagval */
if( !tagval )
tag = -1;
else if( tr_bencIsInt( tagval ) )
tag = tagval->val.i;
else {
errno = EINVAL;
return -1;
}
2008-04-04 17:19:44 +00:00
/* find the msg corresponding to `name' */
if( !tr_bencIsString( name ) ) {
errno = EINVAL;
return -1;
}
msg = msglookup( name->val.s.s );
2008-04-04 17:19:44 +00:00
if( msg && msg->minvers <= info->vers )
{
if( info->funcs->msgs[msg->id] != NULL )
{
2008-04-04 17:19:44 +00:00
(*info->funcs->msgs[msg->id])( msg->id, val, tag, user_data );
}
else if( info->funcs->def )
{
2008-04-04 17:19:44 +00:00
info->funcs->def( msg->id, val, tag, user_data );
}
}
else if( NULL != info->funcs->def )
2008-04-04 17:19:44 +00:00
info->funcs->def( IPC__MSG_UNKNOWN, NULL, tag, user_data );
return 0;
}
static int
2008-04-04 17:19:44 +00:00
handlemsgs( const struct ipc_info * session,
tr_benc * message,
void * user_data )
2007-04-18 16:39:10 +00:00
{
tr_benc * name, * val, * tag;
2007-04-18 16:39:10 +00:00
2008-04-04 17:19:44 +00:00
assert( ipc_hasvers( session ) );
2007-04-18 16:39:10 +00:00
2008-04-04 17:19:44 +00:00
if( DICTPAYLOAD( session ) )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
int ii;
if( TYPE_DICT != message->type || message->val.l.count % 2 )
2007-04-18 16:39:10 +00:00
{
errno = EINVAL;
return -1;
}
2008-04-04 17:19:44 +00:00
for( ii = 0; ii < message->val.l.count; ii += 2 )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
assert( ii + 1 < message->val.l.count );
name = &message->val.l.vals[ii];
val = &message->val.l.vals[ii+1];
if( 0 > callmsgfunc( session, name, val, NULL, user_data ) )
2007-04-18 16:39:10 +00:00
return -1;
}
}
else
{
2008-04-04 17:19:44 +00:00
if( TYPE_LIST != message->type || 2 > message->val.l.count )
2007-04-18 16:39:10 +00:00
{
errno = EINVAL;
return -1;
}
2008-04-04 17:19:44 +00:00
name = &message->val.l.vals[0];
val = &message->val.l.vals[1];
tag = ( 2 == message->val.l.count ? NULL : &message->val.l.vals[2] );
if( 0 > callmsgfunc( session, name, val, tag, user_data ) )
2007-04-18 16:39:10 +00:00
return -1;
}
return 0;
}
ssize_t
2008-04-04 17:19:44 +00:00
ipc_handleMessages( struct ipc_info * info,
const uint8_t * msgs,
ssize_t msgslen,
void * user_data )
2007-04-18 16:39:10 +00:00
{
char hex[IPC_MIN_MSG_LEN+1], * end;
ssize_t off, len;
tr_benc benc;
2007-04-18 16:39:10 +00:00
2008-04-04 17:19:44 +00:00
for( off = 0; off + IPC_MIN_MSG_LEN < msgslen; off += IPC_MIN_MSG_LEN + len )
2007-04-18 16:39:10 +00:00
{
2008-04-04 17:19:44 +00:00
memcpy( hex, msgs + off, IPC_MIN_MSG_LEN );
hex[IPC_MIN_MSG_LEN] = '\0';
end = NULL;
len = strtol( hex, &end, 16 );
if( hex + IPC_MIN_MSG_LEN != end ||
0 > len || IPC_MAX_MSG_LEN < len )
2007-04-18 16:39:10 +00:00
{
errno = EINVAL;
return -1;
}
2008-04-04 17:19:44 +00:00
if( off + IPC_MIN_MSG_LEN + len > msgslen )
2007-04-18 16:39:10 +00:00
{
break;
2007-04-18 16:39:10 +00:00
}
errno = 0;
2008-04-04 17:19:44 +00:00
if( tr_bencLoad( msgs + off + IPC_MIN_MSG_LEN, len, &benc, NULL ) )
2007-04-18 16:39:10 +00:00
{
if( 0 == errno )
{
errno = EINVAL;
}
return -1;
}
2008-04-04 17:19:44 +00:00
if( 0 > ( ipc_hasvers( info ) ? handlemsgs( info, &benc, user_data ) :
handlevers( info, &benc ) ) )
{
SAFEBENCFREE( &benc );
return -1;
2007-04-18 16:39:10 +00:00
}
tr_bencFree( &benc );
2007-04-18 16:39:10 +00:00
}
return off;
2007-04-18 16:39:10 +00:00
}
static int
compareNameToInf( const void * a, const void * b )
{
const struct inf * inf = b;
return strcmp( a, inf->name );
}
2008-04-04 17:19:44 +00:00
/**
* Convert a benc list of string keys from gl_inf or gl_stat
* into a bitwise-or'ed int representation.
* msg_id must be either IPC_MSG_INFO or IPC_MSG_STAT.
* @see ipc_infoname()
*/
2007-04-18 16:39:10 +00:00
int
ipc_infotypes( enum ipc_msg id, const tr_benc * list )
2007-04-18 16:39:10 +00:00
{
const struct inf * array;
size_t len;
int i;
int ret;
2007-04-18 16:39:10 +00:00
switch( id )
{
case IPC_MSG_INFO:
array = gl_inf;
len = TR_N_ELEMENTS( gl_inf );
2007-04-18 16:39:10 +00:00
break;
case IPC_MSG_STAT:
array = gl_stat;
len = TR_N_ELEMENTS( gl_stat );
2007-04-18 16:39:10 +00:00
break;
default:
assert( 0 );
break;
}
ret = IPC_INF_ID;
2008-04-04 17:19:44 +00:00
if( !tr_bencIsList( list ) )
2007-04-18 16:39:10 +00:00
return ret;
for( i=0; i<list->val.l.count; ++i )
2007-04-18 16:39:10 +00:00
{
const tr_benc * name = &list->val.l.vals[i];
const struct inf * inf;
2008-04-04 17:19:44 +00:00
if( !tr_bencIsString( name ) )
2007-04-18 16:39:10 +00:00
continue;
inf = bsearch( name->val.s.s,
array, len, sizeof( struct inf ),
compareNameToInf );
if( inf )
2007-04-18 16:39:10 +00:00
ret |= inf->type;
}
return ret;
}
2008-04-04 17:19:44 +00:00
/**
* This function is the reverse of ipc_infotypes:
* it returns the string key that corresponds to the type passed in.
* Type is one of the IPC_INF_* or IPC_ST_* enums from ipcparse.h.
* msg_id must be either IPC_MSG_INFO or IPC_MSG_STAT.
* @see ipc_infotypes()
*/
2007-04-18 16:39:10 +00:00
const char *
ipc_infoname( enum ipc_msg id, int type )
{
const struct inf * array;
2007-04-18 16:39:10 +00:00
size_t len, ii;
switch( id )
{
case IPC_MSG_INFO:
array = gl_inf;
len = TR_N_ELEMENTS( gl_inf );
2007-04-18 16:39:10 +00:00
break;
case IPC_MSG_STAT:
array = gl_stat;
len = TR_N_ELEMENTS( gl_stat );
2007-04-18 16:39:10 +00:00
break;
default:
assert( 0 );
break;
}
for( ii = 0; len > ii; ii++ )
if( array[ii].type == type )
return array[ii].name;
assert( 0 );
return NULL;
}