From: Jordan Lee Date: Fri, 18 Feb 2011 00:33:11 +0000 (+0000) Subject: (trunk libT) add C and RPC API for getting/setting uTP enabled flag X-Git-Tag: 2.30b1~332 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c4b1d1e6c53e0dccbf7368fed3cee2a16b30bd84;p=transmission (trunk libT) add C and RPC API for getting/setting uTP enabled flag --- diff --git a/extras/rpc-spec.txt b/extras/rpc-spec.txt index c4ebf187f..6e9c999fa 100644 --- a/extras/rpc-spec.txt +++ b/extras/rpc-spec.txt @@ -443,6 +443,7 @@ "start-added-torrents" | boolean | true means added torrents will be started right away "trash-original-torrent-files" | boolean | true means the .torrent file of added torrents will be deleted "units" | object | see below + "utp-enabled" | boolean | true means allow utp "version" | string | long version string "$version ($revision)" ---------------------------------+------------+-----------------------------+ units | object containing: | diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index ed462c7d5..2ccfa4020 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -1472,6 +1472,8 @@ sessionSet( tr_session * session, tr_sessionSetPexEnabled( session, boolVal ); if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) ) tr_sessionSetDHTEnabled( session, boolVal ); + if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_UTP_ENABLED, &boolVal ) ) + tr_sessionSetUTPEnabled( session, boolVal ); if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_LPD_ENABLED, &boolVal ) ) tr_sessionSetLPDEnabled( session, boolVal ); if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, &boolVal ) ) @@ -1596,6 +1598,7 @@ sessionGet( tr_session * s, tr_bencDictAddStr ( d, TR_PREFS_KEY_INCOMPLETE_DIR, tr_sessionGetIncompleteDir( s ) ); tr_bencDictAddBool( d, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, tr_sessionIsIncompleteDirEnabled( s ) ); tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED, tr_sessionIsPexEnabled( s ) ); + tr_bencDictAddBool( d, TR_PREFS_KEY_UTP_ENABLED, tr_sessionIsUTPEnabled( s ) ); tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, tr_sessionIsDHTEnabled( s ) ); tr_bencDictAddBool( d, TR_PREFS_KEY_LPD_ENABLED, tr_sessionIsLPDEnabled( s ) ); tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT, tr_sessionGetPeerPort( s ) ); diff --git a/libtransmission/session.c b/libtransmission/session.c index a82787c9d..6fe078218 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -44,6 +44,7 @@ #include "stats.h" #include "torrent.h" #include "tr-udp.h" +#include "tr-utp.h" #include "tr-lpd.h" #include "trevent.h" #include "utils.h" @@ -314,6 +315,7 @@ tr_sessionGetDefaultSettings( const char * configDir UNUSED, tr_benc * d ) tr_bencDictAddStr ( d, TR_PREFS_KEY_BLOCKLIST_URL, "http://www.example.com/blocklist" ); tr_bencDictAddInt ( d, TR_PREFS_KEY_MAX_CACHE_SIZE_MB, DEFAULT_CACHE_SIZE_MB ); tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, TRUE ); + tr_bencDictAddBool( d, TR_PREFS_KEY_UTP_ENABLED, FALSE ); tr_bencDictAddBool( d, TR_PREFS_KEY_LPD_ENABLED, FALSE ); tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR, tr_getDefaultDownloadDir( ) ); tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED_KBps, 100 ); @@ -378,6 +380,7 @@ tr_sessionGetSettings( tr_session * s, struct tr_benc * d ) tr_bencDictAddStr ( d, TR_PREFS_KEY_BLOCKLIST_URL, tr_blocklistGetURL( s ) ); tr_bencDictAddInt ( d, TR_PREFS_KEY_MAX_CACHE_SIZE_MB, tr_sessionGetCacheLimit_MB( s ) ); tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, s->isDHTEnabled ); + tr_bencDictAddBool( d, TR_PREFS_KEY_UTP_ENABLED, s->isUTPEnabled ); tr_bencDictAddBool( d, TR_PREFS_KEY_LPD_ENABLED, s->isLPDEnabled ); tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR, s->downloadDir ); tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED_KBps, tr_sessionGetSpeedLimit_KBps( s, TR_DOWN ) ); @@ -763,6 +766,8 @@ sessionSetImpl( void * vdata ) tr_sessionSetPexEnabled( session, boolVal ); if( tr_bencDictFindBool( settings, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) ) tr_sessionSetDHTEnabled( session, boolVal ); + if( tr_bencDictFindBool( settings, TR_PREFS_KEY_UTP_ENABLED, &boolVal ) ) + tr_sessionSetUTPEnabled( session, boolVal ); if( tr_bencDictFindBool( settings, TR_PREFS_KEY_LPD_ENABLED, &boolVal ) ) tr_sessionSetLPDEnabled( session, boolVal ); if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ENCRYPTION, &i ) ) @@ -1734,6 +1739,7 @@ sessionCloseImpl( void * vsession ) if( session->isLPDEnabled ) tr_lpdUninit( session ); + tr_utpClose( session ); tr_udpUninit( session ); event_free( session->saveTimer ); @@ -1964,6 +1970,44 @@ tr_sessionSetDHTEnabled( tr_session * session, tr_bool enabled ) tr_runInEventThread( session, toggleDHTImpl, session ); } +/*** +**** +***/ + +tr_bool +tr_sessionIsUTPEnabled( const tr_session * session ) +{ + assert( tr_isSession( session ) ); + + return session->isUTPEnabled; +} + +static void +toggle_utp( void * data ) +{ + tr_session * session = data; + assert( tr_isSession( session ) ); + + session->isUTPEnabled = !session->isUTPEnabled; + + if( !session->isUTPEnabled ) + tr_utpClose( session ); +} + +void +tr_sessionSetUTPEnabled( tr_session * session, tr_bool enabled ) +{ + assert( tr_isSession( session ) ); + assert( tr_isBool( enabled ) ); + + if( ( enabled != 0 ) != ( session->isUTPEnabled != 0 ) ) + tr_runInEventThread( session, toggle_utp, session ); +} + +/*** +**** +***/ + static void toggleLPDImpl( void * data ) { diff --git a/libtransmission/session.h b/libtransmission/session.h index 0be846d93..d8ab9c1eb 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -89,6 +89,7 @@ struct tr_session tr_bool isPortRandom; tr_bool isPexEnabled; tr_bool isDHTEnabled; + tr_bool isUTPEnabled; tr_bool isLPDEnabled; tr_bool isBlocklistEnabled; tr_bool isPrefetchEnabled; diff --git a/libtransmission/tr-udp.c b/libtransmission/tr-udp.c index 082e54489..5ae19009a 100644 --- a/libtransmission/tr-udp.c +++ b/libtransmission/tr-udp.c @@ -135,11 +135,11 @@ event_callback(int s, short type UNUSED, void *sv) if(rc <= 0) return; - if(buf[0] == 'd') { + if((buf[0] == 'd') && tr_sessionIsDHTEnabled(ss)) { /* DHT packet. */ buf[rc] = '\0'; tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, sv); - } else { + } else if(tr_sessionIsDHTEnabled(ss)){ rc = tr_utpPacket(buf, rc, (struct sockaddr*)&from, fromlen, ss); if(!rc) tr_ndbg("UDP", "Unexpected UDP packet"); diff --git a/libtransmission/tr-utp.c b/libtransmission/tr-utp.c index b56b85cf4..83aa51cac 100644 --- a/libtransmission/tr-utp.c +++ b/libtransmission/tr-utp.c @@ -82,12 +82,19 @@ send_to(void *closure, const unsigned char *buf, size_t buflen, sendto(ss->udp6_socket, buf, buflen, 0, to, tolen); } +static void +reset_timer( void ) +{ + int sec = 0; + int usec = UTP_INTERVAL_US / 2 + tr_cryptoWeakRandInt(UTP_INTERVAL_US); + tr_timerAdd( utp_timer, sec, usec ); +} + static void timer_callback(int s UNUSED, short type UNUSED, void *closure UNUSED) { UTP_CheckTimeouts(); - tr_timerAdd(utp_timer, 0, - UTP_INTERVAL_US / 2 + tr_cryptoWeakRandInt(UTP_INTERVAL_US)); + reset_timer(); } int @@ -95,17 +102,24 @@ tr_utpPacket(const unsigned char *buf, size_t buflen, const struct sockaddr *from, socklen_t fromlen, tr_session *ss) { - if(utp_timer == NULL) { - utp_timer = evtimer_new( ss->event_base, timer_callback, NULL); + if(utp_timer == NULL) + { + utp_timer = evtimer_new( ss->event_base, timer_callback, NULL ); if(utp_timer == NULL) return -1; - tr_timerAdd(utp_timer, 0, - UTP_INTERVAL_US / 2 + - tr_cryptoWeakRandInt(UTP_INTERVAL_US)); + reset_timer(); } return UTP_IsIncomingUTP(incoming, send_to, ss, buf, buflen, from, fromlen); } - +void +tr_utpClose( tr_session * session ) +{ + if( utp_timer ) + { + evtimer_del( utp_timer ); + utp_timer = NULL; + } +} diff --git a/libtransmission/tr-utp.h b/libtransmission/tr-utp.h index dfa70ec1b..54b2e9ed0 100644 --- a/libtransmission/tr-utp.h +++ b/libtransmission/tr-utp.h @@ -24,3 +24,5 @@ THE SOFTWARE. int tr_utpPacket(const unsigned char *buf, size_t buflen, const struct sockaddr *from, socklen_t fromlen, tr_session *ss); + +void tr_utpClose( tr_session * ); diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 7ac2ca0f5..777560fbd 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -161,6 +161,7 @@ const char* tr_getDefaultDownloadDir( void ); #define TR_PREFS_KEY_BLOCKLIST_URL "blocklist-url" #define TR_PREFS_KEY_MAX_CACHE_SIZE_MB "cache-size-mb" #define TR_PREFS_KEY_DHT_ENABLED "dht-enabled" +#define TR_PREFS_KEY_UTP_ENABLED "utp-enabled" #define TR_PREFS_KEY_LPD_ENABLED "lpd-enabled" #define TR_PREFS_KEY_PREFETCH_ENABLED "prefetch-enabled" #define TR_PREFS_KEY_DOWNLOAD_DIR "download-dir" @@ -572,6 +573,9 @@ tr_bool tr_sessionIsPexEnabled( const tr_session * session ); tr_bool tr_sessionIsDHTEnabled( const tr_session * session ); void tr_sessionSetDHTEnabled( tr_session * session, tr_bool ); +tr_bool tr_sessionIsUTPEnabled( const tr_session * session ); +void tr_sessionSetUTPEnabled( tr_session * session, tr_bool ); + tr_bool tr_sessionIsLPDEnabled( const tr_session * session ); void tr_sessionSetLPDEnabled( tr_session * session, tr_bool enabled );