/* * This file Copyright (C) 2008 Charles Kerr * * 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$ */ #include #include /* isdigit */ #include /* strtol */ #include /* strcmp */ #include "transmission.h" #include "bencode.h" #include "ratecontrol.h" #include "rpc.h" #include "rpc-utils.h" #include "json.h" #include "session.h" #include "torrent.h" #include "utils.h" #define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) ) /*** **** ***/ static void notify( tr_handle * session, int type, tr_torrent * tor ) { if( session->rpc_func != NULL ) session->rpc_func( session, type, tor, session->rpc_func_user_data ); } /*** **** ***/ static tr_torrent ** getTorrents( tr_handle * handle, tr_benc * args, int * setmeCount ) { int method; int torrentCount = 0; int64_t id; int64_t sortAscending; tr_torrent ** torrents = NULL; const char * str; tr_benc * ids; /*** **** Build the array of torrents ***/ if( tr_bencDictFindList( args, "ids", &ids ) ) { int i; const int n = tr_bencListSize( ids ); torrents = tr_new0( tr_torrent*, n ); for( i=0; ifileCount; ++i ) { const tr_file * file = &info->files[i]; tr_benc * d = tr_bencListAddDict( files, 2 ); tr_bencDictAddInt( d, "length", file->length ); tr_bencDictAddStr( d, "name", file->name ); } } static void addWebseeds( const tr_info * info, tr_benc * webseeds ) { int i; for( i=0; iwebseedCount; ++i ) tr_bencListAddStr( webseeds, info->webseeds[i] ); } static void addTrackers( const tr_info * info, tr_benc * trackers ) { int i; for( i=0; itrackerCount; ++i ) { const tr_tracker_info * t = &info->trackers[i]; tr_benc * d = tr_bencListAddDict( trackers, 3 ); tr_bencDictAddStr( d, "announce", t->announce ); tr_bencDictAddStr( d, "scrape", t->scrape ); tr_bencDictAddInt( d, "tier", t->tier ); } } static void addInfo( const tr_torrent * tor, tr_benc * d, uint64_t fields ) { const tr_info * inf = tr_torrentInfo( tor ); const tr_stat * st = tr_torrentStat( (tr_torrent*)tor ); tr_bencInitDict( d, 64 ); if( fields & TR_RPC_TORRENT_FIELD_ACTIVITY ) { tr_bencDictAddInt( d, "desiredAvailable", st->desiredAvailable ); tr_bencDictAddInt( d, "eta", st->eta ); tr_bencDictAddInt( d, "peersConnected", st->peersConnected ); tr_bencDictAddInt( d, "peersGettingFromUs", st->peersGettingFromUs ); tr_bencDictAddInt( d, "peersSendingToUs", st->peersSendingToUs ); tr_bencDictAddInt( d, "rateDownload", (int)(st->rateDownload*1024) ); tr_bencDictAddInt( d, "rateUpload", (int)(st->rateUpload*1024) ); tr_bencDictAddDouble( d, "recheckProgress", st->recheckProgress ); tr_bencDictAddInt( d, "status", st->status ); tr_bencDictAddDouble( d, "swarmSpeed", st->swarmSpeed ); tr_bencDictAddDouble( d, "ratio", st->ratio ); tr_bencDictAddInt( d, "webseedsSendingToUs", st->webseedsSendingToUs ); } if( fields & TR_RPC_TORRENT_FIELD_ANNOUNCE ) { tr_bencDictAddStr( d, "announceResponse", st->announceResponse ); tr_bencDictAddStr( d, "announceURL", st->announceURL ); tr_bencDictAddInt( d, "lastAnnounceTime", st->lastAnnounceTime ); tr_bencDictAddInt( d, "manualAnnounceTime", st->manualAnnounceTime ); tr_bencDictAddInt( d, "nextAnnounceTime", st->nextAnnounceTime ); } if( fields & TR_RPC_TORRENT_FIELD_ERROR ) { tr_bencDictAddInt( d, "error", st->error ); tr_bencDictAddStr( d, "errorString", st->errorString ); } if( fields & TR_RPC_TORRENT_FIELD_FILES ) addFiles( inf, tr_bencDictAddList( d, "files", inf->fileCount ) ); if( fields & TR_RPC_TORRENT_FIELD_HISTORY ) { tr_bencDictAddInt( d, "activityDate", st->activityDate ); tr_bencDictAddInt( d, "addedDate", st->addedDate ); tr_bencDictAddInt( d, "corruptEver", st->corruptEver ); tr_bencDictAddInt( d, "doneDate", st->doneDate ); tr_bencDictAddInt( d, "downloadedEver", st->downloadedEver ); tr_bencDictAddInt( d, "startDate", st->startDate ); tr_bencDictAddInt( d, "uploadedEver", st->uploadedEver ); } if( fields & TR_RPC_TORRENT_FIELD_ID ) { tr_bencDictAddInt( d, "id", st->id ); tr_bencDictAddStr( d, "hashString", tor->info.hashString ); tr_bencDictAddStr( d, "name", inf->name ); } if( fields & TR_RPC_TORRENT_FIELD_INFO ) { tr_bencDictAddStr( d, "comment", inf->comment ? inf->comment : "" ); tr_bencDictAddStr( d, "creator", inf->creator ? inf->creator : "" ); tr_bencDictAddInt( d, "dateCreated", inf->dateCreated ); tr_bencDictAddInt( d, "isPrivate", tr_torrentIsPrivate( tor ) ); tr_bencDictAddInt( d, "pieceCount", inf->pieceCount ); tr_bencDictAddInt( d, "pieceSize", inf->pieceSize ); } if( fields & TR_RPC_TORRENT_FIELD_LIMITS ) { tr_bencDictAddInt( d, "downloadLimit", tr_torrentGetSpeedLimit( tor, TR_DOWN ) ); tr_bencDictAddInt( d, "downloadLimitMode", tr_torrentGetSpeedMode( tor, TR_DOWN ) ); tr_bencDictAddInt( d, "maxConnectedPeers", tr_torrentGetPeerLimit( tor ) ); tr_bencDictAddInt( d, "uploadLimit", tr_torrentGetSpeedLimit( tor, TR_UP ) ); tr_bencDictAddInt( d, "uploadLimitMode", tr_torrentGetSpeedMode( tor, TR_UP ) ); } if( fields & TR_RPC_TORRENT_FIELD_PEERS ) { const int * f = st->peersFrom; tr_bencDictAddInt( d, "fromCache", f[TR_PEER_FROM_CACHE] ); tr_bencDictAddInt( d, "fromIncoming", f[TR_PEER_FROM_INCOMING] ); tr_bencDictAddInt( d, "fromPex", f[TR_PEER_FROM_PEX] ); tr_bencDictAddInt( d, "fromTracker", f[TR_PEER_FROM_TRACKER] ); } if( fields & TR_RPC_TORRENT_FIELD_PRIORITIES ) { tr_file_index_t i; tr_benc * p = tr_bencDictAddList( d, "priorities", inf->fileCount ); tr_benc * w = tr_bencDictAddList( d, "wanted", inf->fileCount ); for( i=0; ifileCount; ++i ) { tr_bencListAddInt( p, inf->files[i].priority ); tr_bencListAddInt( w, inf->files[i].dnd ? 0 : 1 ); } } if( fields & TR_RPC_TORRENT_FIELD_SCRAPE ) { tr_bencDictAddInt( d, "lastScrapeTime", st->lastScrapeTime ); tr_bencDictAddInt( d, "nextScrapeTime", st->nextScrapeTime ); tr_bencDictAddStr( d, "scrapeResponse", st->scrapeResponse ); tr_bencDictAddStr( d, "scrapeURL", st->scrapeURL ); } if( fields & TR_RPC_TORRENT_FIELD_SIZE ) { tr_bencDictAddInt( d, "haveUnchecked", st->haveUnchecked ); tr_bencDictAddInt( d, "haveValid", st->haveValid ); tr_bencDictAddInt( d, "leftUntilDone", st->leftUntilDone ); tr_bencDictAddInt( d, "sizeWhenDone", st->sizeWhenDone ); tr_bencDictAddInt( d, "totalSize", inf->totalSize ); } if( fields & TR_RPC_TORRENT_FIELD_TRACKER_STATS ) { tr_bencDictAddInt( d, "leechers", st->leechers ); tr_bencDictAddInt( d, "peersKnown", st->peersKnown ); tr_bencDictAddInt( d, "seeders", st->seeders ); tr_bencDictAddDouble( d, "timesCompleted", st->timesCompleted ); } if( fields & TR_RPC_TORRENT_FIELD_TRACKERS ) addTrackers( inf, tr_bencDictAddList( d, "trackers", inf->trackerCount ) ); if( fields & TR_RPC_TORRENT_FIELD_WEBSEEDS ) addWebseeds( inf, tr_bencDictAddList( d, "webseeds", inf->trackerCount ) ); } static const char* torrentGet( tr_handle * handle, tr_benc * args_in, tr_benc * args_out ) { int i, torrentCount; tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); tr_benc * list = tr_bencDictAddList( args_out, "torrents", torrentCount ); int64_t fields = 0; if( !tr_bencDictFindInt( args_in, "fields", &fields ) ) fields = ~(int64_t)0; tr_bencDictAddInt( args_out, "fields", fields ); for( i=0; iisRunning ) ++running; } tr_bencDictAddInt( d, "activeTorrentCount", running ); tr_bencDictAddInt( d, "downloadSpeed", (int)(down*1024) ); tr_bencDictAddInt( d, "pausedTorrentCount", total-running ); tr_bencDictAddInt( d, "torrentCount", total ); tr_bencDictAddInt( d, "uploadSpeed", (int)(up*1024) ); return NULL; } static const char* sessionGet( tr_handle * h, tr_benc * args_in UNUSED, tr_benc * args_out ) { const char * str; tr_benc * d = tr_bencDictAddDict( args_out, "session", 10 ); tr_bencDictAddStr( d, "download-dir", tr_sessionGetDownloadDir( h ) ); tr_bencDictAddInt( d, "peer-limit", tr_sessionGetPeerLimit( h ) ); tr_bencDictAddInt( d, "pex-allowed", tr_sessionIsPexEnabled( h ) ); tr_bencDictAddInt( d, "port", tr_sessionGetPeerPort( h ) ); tr_bencDictAddInt( d, "port-forwarding-enabled", tr_sessionIsPortForwardingEnabled( h ) ); tr_bencDictAddInt( d, "speed-limit-up", tr_sessionGetSpeedLimit( h, TR_UP ) ); tr_bencDictAddInt( d, "speed-limit-up-enabled", tr_sessionIsSpeedLimitEnabled( h, TR_UP ) ); tr_bencDictAddInt( d, "speed-limit-down", tr_sessionGetSpeedLimit( h, TR_DOWN ) ); tr_bencDictAddInt( d, "speed-limit-down-enabled", tr_sessionIsSpeedLimitEnabled( h, TR_DOWN ) ); switch( tr_sessionGetEncryption( h ) ) { case TR_PLAINTEXT_PREFERRED: str = "tolerated"; break; case TR_ENCRYPTION_REQUIRED: str = "required"; break; default: str = "preferred"; break; } tr_bencDictAddStr( d, "encryption", str ); return NULL; } /*** **** ***/ typedef const char* (handler)( tr_handle*, tr_benc*, tr_benc* ); struct method { const char * name; handler * func; } methods[] = { { "session-get", sessionGet }, { "session-set", sessionSet }, { "session-stats", sessionStats }, { "torrent-add", torrentAdd }, { "torrent-get", torrentGet }, { "torrent-remove", torrentRemove }, { "torrent-set", torrentSet }, { "torrent-start", torrentStart }, { "torrent-stop", torrentStop }, { "torrent-verify", torrentVerify } }; static char* request_exec( struct tr_handle * handle, tr_benc * request, int * response_len ) { int64_t i; const char * str; char * out; tr_benc response; tr_benc * args_in = tr_bencDictFind( request, "arguments" ); tr_benc * args_out = NULL; const char * result = NULL; /* build the response skeleton */ tr_bencInitDict( &response, 3 ); args_out = tr_bencDictAddDict( &response, "arguments", 0 ); /* parse the request */ if( !tr_bencDictFindStr( request, "method", &str ) ) result = "no method name"; else { const int n = TR_N_ELEMENTS( methods ); for( i=0; i