Changeset 6140


Ignore:
Timestamp:
Jun 11, 2008, 4:15:45 PM (15 years ago)
Author:
charles
Message:

(libT) allow custom blocklists... now all you need to do is place a file in the blocklists/ folder and Transmission will automatically use it the next time it starts.

Location:
trunk/libtransmission
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/blocklist.c

    r5959 r6140  
    1515#include <string.h>
    1616
     17#include <libgen.h> /* basename */
    1718#include <sys/mman.h>
    1819#include <sys/types.h>
     
    9091    b->ruleCount = st.st_size / sizeof( struct tr_ip_range );
    9192    b->fd = fd;
    92     tr_inf( _( "Blocklist contains %'u entries" ), (unsigned int)b->ruleCount );
     93
     94    {
     95        char * name;
     96        char buf[MAX_PATH_LENGTH];
     97        tr_strlcpy( buf, b->filename, sizeof( buf ) );
     98        name = basename( buf );
     99        tr_inf( _( "Blocklist \"%s\" contains %'u entries" ), name, (unsigned int)b->ruleCount );
     100    }
    93101}
    94102
     
    134142}
    135143
     144const char*
     145_tr_blocklistGetFilename( const tr_blocklist * b )
     146{
     147    return b->filename;
     148}
     149
    136150void
    137151_tr_blocklistFree( tr_blocklist * b )
     
    143157
    144158int
    145 _tr_blocklistGetRuleCount( tr_blocklist * b )
    146 {
    147     blocklistEnsureLoaded( b );
     159_tr_blocklistExists( const tr_blocklist * b )
     160{
     161    struct stat st;
     162    return !stat( b->filename, &st );
     163}
     164
     165int
     166_tr_blocklistGetRuleCount( const tr_blocklist * b )
     167{
     168    blocklistEnsureLoaded( (tr_blocklist*)b );
    148169
    149170    return b->ruleCount;
     
    187208
    188209int
    189 _tr_blocklistExists( const tr_blocklist * b )
    190 {
    191     struct stat st;
    192     return !stat( b->filename, &st );
    193 }
    194 
    195 int
    196210_tr_blocklistSetContent( tr_blocklist * b,
    197211                         const char   * filename )
     
    256270    }
    257271
    258     tr_inf( _( "Blocklist updated with %'d entries" ), lineCount );
     272    {
     273        char * name;
     274        char buf[MAX_PATH_LENGTH];
     275        tr_strlcpy( buf, b->filename, sizeof( buf ) );
     276        name = basename( buf );
     277        tr_inf( _( "Blocklist \"%s\" updated with %'d entries" ), name, lineCount );
     278    }
     279
    259280
    260281    fclose( out );
  • trunk/libtransmission/blocklist.h

    r5959 r6140  
    1818
    1919tr_blocklist* _tr_blocklistNew         ( const char * filename, int isEnabled );
    20 void          _tr_blocklistFree        ( tr_blocklist * b );
    21 int           _tr_blocklistGetRuleCount( tr_blocklist * b );
    22 int           _tr_blocklistIsEnabled   ( tr_blocklist * b );
    23 void          _tr_blocklistSetEnabled  ( tr_blocklist * b, int isEnabled );
    24 int           _tr_blocklistHasAddress  ( tr_blocklist * b, const struct in_addr * addr );
    25 int           _tr_blocklistExists      ( const tr_blocklist * b );
    26 int           _tr_blocklistSetContent  ( tr_blocklist * b, const char * filename );
     20int           _tr_blocklistExists      ( const tr_blocklist * );
     21const char*   _tr_blocklistGetFilename ( const tr_blocklist * );
     22int           _tr_blocklistGetRuleCount( const tr_blocklist * );
     23void          _tr_blocklistFree        ( tr_blocklist * );
     24int           _tr_blocklistIsEnabled   ( tr_blocklist * );
     25void          _tr_blocklistSetEnabled  ( tr_blocklist *, int isEnabled );
     26int           _tr_blocklistHasAddress  ( tr_blocklist *, const struct in_addr * addr );
     27int           _tr_blocklistSetContent  ( tr_blocklist *, const char * filename );
    2728
    2829#endif
  • trunk/libtransmission/peer-mgr.c

    r6115 r6140  
    10161016    managerLock( manager );
    10171017
    1018     if( tr_blocklistHasAddress( manager->handle, addr ) )
     1018    if( tr_sessionIsAddressBlocked( manager->handle, addr ) )
    10191019    {
    10201020        tr_dbg( "Banned IP address \"%s\" tried to connect to us",
     
    10541054
    10551055    t = getExistingTorrent( manager, torrentHash );
    1056     if( !tr_blocklistHasAddress( t->manager->handle, &pex->in_addr ) )
     1056    if( !tr_sessionIsAddressBlocked( t->manager->handle, &pex->in_addr ) )
    10571057        ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from );
    10581058
     
    18001800
    18011801        /* Don't connect to peers in our blocklist */
    1802         if( tr_blocklistHasAddress( t->manager->handle, &atom->addr ) )
     1802        if( tr_sessionIsAddressBlocked( t->manager->handle, &atom->addr ) )
    18031803            continue;
    18041804
  • trunk/libtransmission/rpc-test.c

    r6139 r6140  
    3131    int ok;
    3232    char * str = cidrize( in );
    33 fprintf( stderr, "in [%s] out [%s] should be [%s]\n", in, str, expected );
     33/* fprintf( stderr, "in [%s] out [%s] should be [%s]\n", in, str, expected ); */
    3434    ok = expected ? !strcmp( expected, str ) : !str;
    3535    tr_free( str );
  • trunk/libtransmission/session.c

    r6120 r6140  
    111111
    112112    session->encryptionMode = mode;
     113}
     114
     115/***
     116****
     117***/
     118
     119static void
     120loadBlocklists( tr_session * session )
     121{
     122    int binCount = 0;
     123    int newCount = 0;
     124    struct stat sb;
     125    char dirname[MAX_PATH_LENGTH];
     126    DIR * odir = NULL;
     127    tr_list * list = NULL;
     128    const int isEnabled = session->isBlocklistEnabled;
     129
     130    /* walk through the directory and find blocklists */
     131    tr_buildPath( dirname, sizeof( dirname ), session->configDir, "blocklists", NULL );
     132    if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && (( odir = opendir( dirname ))))
     133    {
     134        struct dirent *d;
     135        for( d=readdir( odir ); d; d=readdir( odir ) )
     136        {
     137            char filename[MAX_PATH_LENGTH];
     138
     139            if( !d->d_name || d->d_name[0]=='.' ) /* skip dotfiles, ., and .. */
     140                continue;
     141
     142            tr_buildPath( filename, sizeof(filename), dirname, d->d_name, NULL );
     143
     144            if( tr_stringEndsWith( filename, ".bin" ) )
     145            {
     146                /* if we don't already have this blocklist, add it */
     147                if( !tr_list_find( list, filename, (TrListCompareFunc)strcmp ) )
     148                {
     149                    tr_list_append( &list, _tr_blocklistNew( filename, isEnabled ) );
     150                    ++binCount;
     151                }
     152            }
     153            else
     154            {
     155                /* strip out the file suffix, if there is one, and add ".bin" instead */
     156                tr_blocklist * b;
     157                const char * dot = strrchr( d->d_name, '.' );
     158                const int len = dot ? dot - d->d_name : (int)strlen( d->d_name );
     159                char tmp[MAX_PATH_LENGTH];
     160                snprintf( tmp, sizeof( tmp ),
     161                          "%s%c%*.*s.bin", dirname, TR_PATH_DELIMITER, len, len, d->d_name );
     162                b = _tr_blocklistNew( tmp, isEnabled );
     163                _tr_blocklistSetContent( b, filename );
     164                tr_list_append( &list, b );
     165                ++newCount;
     166            }
     167        }
     168
     169        closedir( odir );
     170    }
     171
     172    session->blocklists = list;
     173
     174    if( binCount )
     175        tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname );
     176    if( newCount )
     177        tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname );
    113178}
    114179
     
    207272    tr_buildPath( filename, sizeof( filename ), h->configDir, "blocklists", NULL );
    208273    tr_mkdirp( filename, 0777 );
    209     tr_buildPath( filename, sizeof( filename ), h->configDir, "blocklists", "level1.bin", NULL );
    210     h->blocklist = _tr_blocklistNew( filename, isBlocklistEnabled );
     274    h->isBlocklistEnabled = isBlocklistEnabled;
     275    loadBlocklists( h );
    211276
    212277    tr_statsInit( h );
     
    486551        tr_wait( 100 );
    487552
    488     _tr_blocklistFree( h->blocklist );
    489     h->blocklist = NULL;
     553    tr_list_free( &h->blocklists, (TrListForeachFunc)_tr_blocklistFree );
    490554    tr_webClose( &h->web );
    491555
     
    536600                char filename[MAX_PATH_LENGTH];
    537601                tr_buildPath( filename, sizeof(filename), dirname, d->d_name, NULL );
    538                 tr_ctorSetMetainfoFromFile( ctor, filename );
    539                 tor = tr_torrentNew( h, ctor, NULL );
    540                 if( tor ) {
    541                     tr_list_append( &list, tor );
    542                     n++;
     602                if( tr_stringEndsWith( filename, ".torrent" ) )
     603                {
     604                    tr_ctorSetMetainfoFromFile( ctor, filename );
     605                    tor = tr_torrentNew( h, ctor, NULL );
     606                    if( tor ) {
     607                        tr_list_append( &list, tor );
     608                        ++n;
     609                    }
    543610                }
    544611            }
     
    601668
    602669int
    603 tr_blocklistGetRuleCount( tr_handle * handle )
    604 {
    605     return _tr_blocklistGetRuleCount( handle->blocklist );
    606 }
    607 
    608 int
    609 tr_blocklistIsEnabled( const tr_handle * handle )
    610 {
    611     return _tr_blocklistIsEnabled( handle->blocklist );
    612 }
    613 
    614 void
    615 tr_blocklistSetEnabled( tr_handle * handle, int isEnabled )
    616 {
    617     _tr_blocklistSetEnabled( handle->blocklist, isEnabled );
    618 }
    619 
    620 int
    621 tr_blocklistExists( const tr_handle * handle )
    622 {
    623     return _tr_blocklistExists( handle->blocklist );
    624 }
    625 
    626 int
    627 tr_blocklistSetContent( tr_handle  * handle, const char * filename )
    628 {
    629     return _tr_blocklistSetContent( handle->blocklist, filename );
    630 }
    631 
    632 int
    633 tr_blocklistHasAddress( tr_handle * handle, const struct in_addr * addr )
    634 {
    635     return _tr_blocklistHasAddress( handle->blocklist, addr );
     670tr_blocklistGetRuleCount( const tr_session * session )
     671{
     672    int n = 0;
     673    tr_list * l;
     674    for( l=session->blocklists; l; l=l->next )
     675        n += _tr_blocklistGetRuleCount( l->data );
     676    return n;
     677}
     678
     679int
     680tr_blocklistIsEnabled( const tr_session * session )
     681{
     682    return session->isBlocklistEnabled;
     683}
     684
     685void
     686tr_blocklistSetEnabled( tr_session * session, int isEnabled )
     687{
     688    tr_list * l;
     689    session->isBlocklistEnabled = isEnabled ? 1 : 0;
     690    for( l=session->blocklists; l; l=l->next )
     691        _tr_blocklistSetEnabled( l->data, isEnabled );
     692}
     693
     694int
     695tr_blocklistExists( const tr_session * session )
     696{
     697    return session->blocklists != NULL;
     698}
     699
     700int
     701tr_blocklistSetContent( tr_session  * session, const char * contentFilename )
     702{
     703    tr_list * l;
     704    tr_blocklist * b;
     705    const char * defaultName = "level1.bin";
     706
     707    for( b=NULL, l=session->blocklists; !b && l; l=l->next )
     708        if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ), defaultName ) )
     709            b = l->data;
     710
     711    if( !b ) {
     712        char filename[MAX_PATH_LENGTH];
     713        tr_buildPath( filename, sizeof( filename ), session->configDir, "blocklists", defaultName, NULL );
     714        b = _tr_blocklistNew( filename, session->isBlocklistEnabled );
     715        tr_list_append( &session->blocklists, b );
     716    }
     717
     718    return _tr_blocklistSetContent( b, contentFilename );
     719}
     720
     721int
     722tr_sessionIsAddressBlocked( const tr_session      * session,
     723                            const struct in_addr  * addr )
     724{
     725    tr_list * l;
     726    for( l=session->blocklists; l; l=l->next )
     727        if( _tr_blocklistHasAddress( l->data, addr ) )
     728            return TRUE;
     729    return FALSE;
    636730}
    637731
  • trunk/libtransmission/session.h

    r6120 r6140  
    5252    unsigned int                 isPortSet          : 1;
    5353    unsigned int                 isPexEnabled       : 1;
     54    unsigned int                 isBlocklistEnabled : 1;
    5455    unsigned int                 isProxyEnabled     : 1;
    5556    unsigned int                 isProxyAuthEnabled : 1;
     
    8182    struct tr_ratecontrol      * download;
    8283
    83     struct tr_blocklist        * blocklist;
     84    struct tr_list             * blocklists;
    8485    struct tr_peerMgr          * peerMgr;
    8586    struct tr_shared           * shared;
     
    107108                               const char   * filename );
    108109
     110struct in_addr;
     111
     112int tr_sessionIsAddressBlocked( const tr_session     * session,
     113                                const struct in_addr * addr );
     114
     115
    109116void tr_globalLock       ( tr_session * );
    110117void tr_globalUnlock     ( tr_session * );
  • trunk/libtransmission/transmission.h

    r6120 r6140  
    606606 * Passing NULL for a filename will clear the blocklist.
    607607 */
    608 int tr_blocklistSetContent( tr_handle  * handle,
    609                             const char * filename );
    610 
    611 int tr_blocklistGetRuleCount( tr_handle * handle );
    612 
    613 int tr_blocklistExists( const tr_handle * handle );
    614 
    615 int tr_blocklistIsEnabled( const tr_handle * handle );
    616 
    617 void tr_blocklistSetEnabled( tr_handle * handle,
    618                              int         isEnabled );
    619 
    620 struct in_addr;
    621 
    622 int tr_blocklistHasAddress( tr_handle             * handle,
    623                             const struct in_addr  * addr);
     608int tr_blocklistSetContent   ( tr_session       * session,
     609                               const char       * filename );
     610
     611int tr_blocklistGetRuleCount ( const tr_session * session );
     612
     613int tr_blocklistExists       ( const tr_session * session );
     614
     615int tr_blocklistIsEnabled    ( const tr_session * session );
     616
     617void tr_blocklistSetEnabled  ( tr_session       * session,
     618                               int                isEnabled );
     619
    624620
    625621/** @} */
  • trunk/libtransmission/utils-test.c

    r6101 r6140  
    9494    tr_free( out );
    9595
     96    /* tr_stringEndsWith */
     97    check( tr_stringEndsWith( "the", "the" ) );
     98    check( tr_stringEndsWith( "dress", "dress" ) );
     99    check( tr_stringEndsWith( "address", "dress" ) );
     100    check( !tr_stringEndsWith( "foo.bin", "gfoo.bin" ) );
     101    check( !tr_stringEndsWith( "xyz", "xyw" ) );
     102
    96103    /* simple bitfield tests */
    97104    for( l=0; l<NUM_LOOPS; ++l )
  • trunk/libtransmission/utils.c

    r6101 r6140  
    922922***/
    923923
     924int
     925tr_stringEndsWith( const char * str, const char * end )
     926{
     927    const size_t slen = strlen( str );
     928    const size_t elen = strlen( end );
     929    return slen>=elen && !memcmp( &str[slen-elen], end, elen );
     930}
     931
    924932/*
    925933 * Copy src to string dst of size siz.  At most siz-1 characters
  • trunk/libtransmission/utils.h

    r6101 r6140  
    198198size_t tr_strlcpy( char * dst, const char * src, size_t siz );
    199199
    200 
     200int   tr_stringEndsWith( const char * string, const char * end );
    201201
    202202const char* tr_strerror( int );
Note: See TracChangeset for help on using the changeset viewer.