Changeset 8242


Ignore:
Timestamp:
Apr 15, 2009, 9:05:58 PM (13 years ago)
Author:
jhujhiti
Message:

(trunk) #1497 Options to listen on specific network sockets

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r8122 r8242  
    8585    { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL },
    8686    { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL },
     87    { 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", 1, "<ipv4 address>" },
     88    { 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", 1, "<ipv6 address" },
     89    { 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 address>" },
    8790    { 0, NULL, NULL, NULL, 0, NULL }
    8891};
     
    277280            case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED );
    278281                      break;
     282            case 'i':
     283                      tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, optarg );
     284                      break;
     285            case 'I':
     286                      tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, optarg );
     287                      break;
     288            case 'r':
     289                      tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, optarg );
     290                      break;
    279291            default:  showUsage( );
    280292                      break;
  • trunk/daemon/transmission-daemon.1

    r7971 r8242  
    7575Print command-line option descriptions.
    7676
     77.It Fl i Fl -bind-address-ipv4
     78Listen for IPv4 BitTorrent connections on a specific address. Only one IPv4 listening address is allowed. Default: 0.0.0.0 (All addresses)
     79.It Fl I Fl -bind-address-ipv6
     80Listen for IPv6 BitTorrent connections on a specific address. Only one IPv6 listening address is allowed. Default: :: (All addresses)
     81.It Fl r Fl -rpc-bind-address
     82Listen for RPC connections on a specific address. This must be an IPv4 address. Only one RPC listening address is allowed. Default: 0.0.0.0 (All addresses)
     83
    7784.It Fl L Fl -peerlimit-global Ar limit
    7885Overall peer limit. Useful on embedded systems where the default might be unsuitable. Default: 240
  • trunk/libtransmission/net.c

    r8217 r8242  
    155155{
    156156    int retval = inet_pton( AF_INET, src, &dst->addr );
     157    assert( dst );
    157158    if( retval < 0 )
    158159        return NULL;
     
    317318}
    318319
     320const tr_address *
     321tr_socketListGetType( const tr_socketList * const el, tr_address_type type )
     322{
     323    const tr_socketList * tmp = el;
     324    while( tmp )
     325    {
     326        if( tmp->addr.type == type )
     327            return &tmp->addr;
     328        tmp = tmp->next;
     329    }
     330    return NULL;
     331}
     332
    319333/***********************************************************************
    320334 * TCP sockets
     
    447461}
    448462
     463const tr_socketList * tr_getSessionBindSockets( const tr_session * session );
     464
    449465int
    450466tr_netOpenTCP( tr_session        * session,
     
    456472    const int               type = SOCK_STREAM;
    457473    socklen_t               addrlen;
     474    const tr_address      * source_addr;
     475    socklen_t               sourcelen;
     476    struct sockaddr_storage source_sock;
    458477
    459478    assert( tr_isAddress( addr ) );
     
    468487
    469488    addrlen = setup_sockaddr( addr, port, &sock );
     489   
     490    /* set source address */
     491    source_addr = tr_socketListGetType( tr_getSessionBindSockets( session ),
     492                                        addr->type );
     493    assert( source_addr );
     494    sourcelen = setup_sockaddr( source_addr, 0, &source_sock );
     495    if( bind( s, ( struct sockaddr * ) &source_sock, sourcelen ) )
     496    {
     497        tr_err( _( "Couldn't set source address %s on %d: %s" ),
     498                tr_ntop_non_ts( source_addr ), s, tr_strerror( errno ) );
     499        return -errno;
     500    }
    470501
    471502    if( ( connect( s, (struct sockaddr *) &sock,
  • trunk/libtransmission/net.h

    r8092 r8242  
    113113                                           void * const ),
    114114                           void * const userData);
     115const tr_address *tr_socketListGetType( const tr_socketList * const el,
     116                                        tr_address_type type );
    115117
    116118/***********************************************************************
  • trunk/libtransmission/port-forwarding.c

    r7883 r8242  
    216216***/
    217217
    218 static tr_socketList *
    219 setupBindSockets( tr_port port )
    220 {
    221     tr_bool hasIPv6 = tr_net_hasIPv6( port );
    222     tr_socketList * socks = NULL;
    223     if( hasIPv6 )
    224         socks = tr_socketListNew( &tr_in6addr_any );
    225 
    226     if( socks )
    227         tr_socketListAppend( socks, &tr_inaddr_any );
    228     else
    229         socks = tr_socketListNew( &tr_inaddr_any );
    230     return socks; /* Because the dryer gremlins won't */
    231 }
    232 
    233218tr_shared *
    234219tr_sharedInit( tr_session  * session,
    235220               tr_bool       isEnabled,
    236                tr_port       publicPort )
     221               tr_port       publicPort,
     222               tr_socketList * socks )
    237223{
    238224    tr_shared * s = tr_new0( tr_shared, 1 );
     
    241227    s->publicPort   = publicPort;
    242228    s->shouldChange = TRUE;
    243     s->bindSockets  = setupBindSockets( publicPort );
     229    s->bindSockets  = socks;
    244230    s->shouldChange = TRUE;
    245231    s->natpmp       = tr_natpmpInit( );
     
    295281}
    296282
     283const tr_socketList *
     284tr_sharedGetBindSockets( const tr_shared * shared )
     285{
     286    return shared->bindSockets;
     287}
  • trunk/libtransmission/port-forwarding.h

    r7392 r8242  
    3131
    3232#include "transmission.h"
     33#include "net.h"
    3334
    3435typedef struct tr_shared tr_shared;
    3536
    36 tr_shared* tr_sharedInit( tr_session*, tr_bool isEnabled, tr_port publicPort );
     37tr_shared* tr_sharedInit( tr_session*, tr_bool isEnabled, tr_port publicPort,
     38                          tr_socketList * socks);
    3739
    3840void       tr_sharedShuttingDown( tr_shared * );
     
    4850int        tr_sharedTraversalStatus( const tr_shared * );
    4951
     52const tr_socketList *tr_sharedGetBindSockets( const tr_shared * shared );
    5053#endif
  • trunk/libtransmission/rpc-server.c

    r8199 r8242  
    3838#include "utils.h"
    3939#include "web.h"
     40#include "net.h"
    4041
    4142#define MY_NAME "RPC Server"
     
    5354    tr_bool            isWhitelistEnabled;
    5455    tr_port            port;
     56    struct in_addr     bindAddress;
    5557    struct evhttp *    httpd;
    5658    tr_session *       session;
     
    529531{
    530532    tr_rpc_server * server  = vserver;
     533    tr_address addr;
    531534
    532535    if( !server->httpd )
    533536    {
     537        addr.type = TR_AF_INET;
     538        addr.addr.addr4 = server->bindAddress;
    534539        server->httpd = evhttp_new( tr_eventGetBase( server->session ) );
    535         evhttp_bind_socket( server->httpd, "0.0.0.0", server->port );
     540        evhttp_bind_socket( server->httpd, tr_ntop_non_ts( &addr ),
     541                            server->port );
    536542        evhttp_set_gencb( server->httpd, handle_request, server );
    537543
     
    708714}
    709715
     716const char *
     717tr_rpcGetBindAddress( const tr_rpc_server * server )
     718{
     719    tr_address addr;
     720    addr.type = TR_AF_INET;
     721    addr.addr.addr4 = server->bindAddress;
     722    return tr_ntop_non_ts( &addr );
     723}
     724
    710725/****
    711726*****  LIFE CYCLE
     
    746761    int64_t i;
    747762    const char *str;
     763    tr_address address;
    748764
    749765    s = tr_new0( tr_rpc_server, 1 );
     
    781797        s->password = strdup( str );
    782798
     799    found = tr_bencDictFindStr( settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, &str );
     800    assert( found );
     801    if( tr_pton( str, &address ) == NULL ) {
     802        tr_err( _( "%s is not a valid address" ), str );
     803        address = tr_inaddr_any;
     804    } else if( address.type != TR_AF_INET ) {
     805        tr_err( _( "%s is not an IPv4 address. RPC listeners must be IPv4" ),
     806                   str );
     807        address = tr_inaddr_any;
     808    }
     809    s->bindAddress = address.addr.addr4;
     810
    783811#ifdef HAVE_ZLIB
    784812    s->stream.zalloc = (alloc_func) Z_NULL;
  • trunk/libtransmission/rpc-server.h

    r7706 r8242  
    6464tr_bool         tr_rpcIsPasswordEnabled( const tr_rpc_server * session );
    6565
     66const char*     tr_rpcGetBindAddress( const tr_rpc_server * server );
    6667
    6768#endif
  • trunk/libtransmission/session.c

    r8233 r8242  
    282282    tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED,            FALSE );
    283283    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED,        FALSE );
     284    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS,         "0.0.0.0" );
    284285    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED,              TRUE );
    285286    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD,             "" );
     
    298299    tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED,           FALSE );
    299300    tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, 14 );
    300 }
     301    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4,        TR_DEFAULT_BIND_ADDRESS_IPV4 );
     302    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6,        TR_DEFAULT_BIND_ADDRESS_IPV6 );
     303}
     304
     305const tr_socketList * tr_getSessionBindSockets( const tr_session * session );
    301306
    302307void
     
    337342    tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED,            s->isRatioLimited );
    338343    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED,        tr_sessionIsRPCPasswordEnabled( s ) );
     344    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS,         tr_sessionGetRPCBindAddress( s ) );
    339345    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED,              tr_sessionIsRPCEnabled( s ) );
    340346    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD,             freeme[n++] = tr_sessionGetRPCPassword( s ) );
     
    353359    tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED,           tr_sessionIsSpeedLimited( s, TR_UP ) );
    354360    tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, s->uploadSlotsPerTorrent );
     361    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4,
     362                        tr_ntop_non_ts( tr_socketListGetType( tr_getSessionBindSockets( s ), TR_AF_INET ) ) );
     363    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6,
     364                        tr_ntop_non_ts( tr_socketListGetType( tr_getSessionBindSockets( s ), TR_AF_INET6 ) ) );
    355365
    356366    for( i=0; i<n; ++i )
     
    501511    tr_benc * clientSettings = data->clientSettings;
    502512    tr_session * session = data->session;
     513    tr_address address;
     514    tr_socketList * socketList;
    503515
    504516    assert( tr_amInEventThread( session ) );
     
    619631    assert( found );
    620632    session->peerPort = session->isPortRandom ? getRandomPort( session ) : j;
    621     session->shared = tr_sharedInit( session, boolVal, session->peerPort );
     633
     634    /* bind addresses */
     635
     636    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4,
     637                                &str );
     638    assert( found );
     639    if( tr_pton( str, &address ) == NULL ) {
     640        tr_err( _( "%s is not a valid address" ), str );
     641        socketList = tr_socketListNew( &tr_inaddr_any );
     642    } else if( address.type != TR_AF_INET ) {
     643        tr_err( _( "%s is not an IPv4 address" ), str );
     644        socketList = tr_socketListNew( &tr_inaddr_any );
     645    } else
     646        socketList = tr_socketListNew( &address );
     647
     648    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6,
     649                                &str );
     650    assert( found );
     651    if( tr_pton( str, &address ) == NULL ) {
     652        tr_err( _( "%s is not a valid address" ), str );
     653        address = tr_in6addr_any;
     654    } else if( address.type != TR_AF_INET6 ) {
     655        tr_err( _( "%s is not an IPv6 address" ), str );
     656        address = tr_in6addr_any;
     657    }
     658    if( tr_net_hasIPv6( session->peerPort ) )
     659        tr_socketListAppend( socketList, &address );
     660    else
     661        tr_inf( _( "System does not seem to support IPv6. Not listening on"
     662                   "an IPv6 address" ) );
     663
     664    session->shared = tr_sharedInit( session, boolVal, session->peerPort,
     665                                     socketList );
    622666    session->isPortSet = session->peerPort > 0;
    623667
     
    18901934}
    18911935
     1936const char *
     1937tr_sessionGetRPCBindAddress( const tr_session * session )
     1938{
     1939    assert( tr_isSession( session ) );
     1940
     1941    return tr_rpcGetBindAddress( session->rpcServer );
     1942}
     1943
    18921944/***
    18931945****
     
    20392091    return ret;
    20402092}
     2093
     2094const tr_socketList *
     2095tr_getSessionBindSockets( const tr_session * session )
     2096{
     2097    return tr_sharedGetBindSockets( session->shared );
     2098}
  • trunk/libtransmission/session.h

    r8132 r8242  
    172172void         tr_globalUnlock( tr_session * );
    173173
    174 tr_bool      tr_globalIsLocked( const tr_session * );
    175 
    176174enum
    177175{
  • trunk/libtransmission/transmission.h

    r8233 r8242  
    151151
    152152
     153#define TR_DEFAULT_BIND_ADDRESS_IPV4 "0.0.0.0"
     154#define TR_DEFAULT_BIND_ADDRESS_IPV6 "::"
    153155#define TR_DEFAULT_OPEN_FILE_LIMIT_STR "32"
    154156#define TR_DEFAULT_RPC_WHITELIST "127.0.0.1"
     
    166168#define TR_PREFS_KEY_ALT_SPEED_TIME_END         "alt-speed-time-end"
    167169#define TR_PREFS_KEY_ALT_SPEED_TIME_DAY         "alt-speed-time-day"
     170#define TR_PREFS_KEY_BIND_ADDRESS_IPV4          "bind-address-ipv4"
     171#define TR_PREFS_KEY_BIND_ADDRESS_IPV6          "bind-address-ipv6"
    168172#define TR_PREFS_KEY_BLOCKLIST_ENABLED          "blocklist-enabled"
    169173#define TR_PREFS_KEY_DOWNLOAD_DIR               "download-dir"
     
    192196#define TR_PREFS_KEY_RATIO_ENABLED              "ratio-limit-enabled"
    193197#define TR_PREFS_KEY_RPC_AUTH_REQUIRED          "rpc-authentication-required"
     198#define TR_PREFS_KEY_RPC_BIND_ADDRESS           "rpc-bind-address"
    194199#define TR_PREFS_KEY_RPC_ENABLED                "rpc-enabled"
    195200#define TR_PREFS_KEY_RPC_PASSWORD               "rpc-password"
     
    398403tr_bool tr_sessionIsRPCPasswordEnabled( const tr_session * session );
    399404
     405const char* tr_sessionGetRPCBindAddress( const tr_session * session );
     406
    400407
    401408typedef enum
Note: See TracChangeset for help on using the changeset viewer.