]> granicus.if.org Git - transmission/commitdiff
(trunk libT) add C and RPC API for getting/setting uTP enabled flag
authorJordan Lee <jordan@transmissionbt.com>
Fri, 18 Feb 2011 00:33:11 +0000 (00:33 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Fri, 18 Feb 2011 00:33:11 +0000 (00:33 +0000)
extras/rpc-spec.txt
libtransmission/rpcimpl.c
libtransmission/session.c
libtransmission/session.h
libtransmission/tr-udp.c
libtransmission/tr-utp.c
libtransmission/tr-utp.h
libtransmission/transmission.h

index c4ebf187f3e99a34a85fb326b7e8c2991f85d1d4..6e9c999fa5ad4e0ca18361b16c07e7d4a575bb35 100644 (file)
    "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:                       |
index ed462c7d5f922b25c11422c4c74aa8333bf2f137..2ccfa4020e2ff632048d5c4462f51daacae1c9e0 100644 (file)
@@ -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 ) );
index a82787c9d14850e3ba67fc013411b20e5fb74cc1..6fe0782189235c9d6e8e85139357b63e19bc2589 100644 (file)
@@ -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 )
 {
index 0be846d937fb4f2fe442e45ff6ecf2038aa4ce13..d8ab9c1ebad2f21f947d4a298ca3b8fdcdf35b8a 100644 (file)
@@ -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;
index 082e54489a02a7bb7cd40ac42252e2add215fae7..5ae19009a7d2b735236fc3d68ad516797801279f 100644 (file)
@@ -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");
index b56b85cf4b807e4bd81f7567d0f6ae37da7c210d..83aa51cacf06f8094a000a2f830fe6693acd6739 100644 (file)
@@ -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;
+    }
+}
index dfa70ec1b70fb8ba4f7fc3fd2eed4b892d6c6198..54b2e9ed0dd1b5706f7b6606bed700343e562547 100644 (file)
@@ -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 * );
index 7ac2ca0f58092285d2fed1392995f983d6430d2c..777560fbd13fd682e290e3cfccfeb648aaa48050 100644 (file)
@@ -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 );