Exemple #1
0
int
tr_blocklistSetContent( tr_session * session,
                        const char * contentFilename )
{
    tr_list *      l;
    tr_blocklist * b;
    const char *   defaultName = "level1.bin";

    assert( tr_isSession( session ) );

    for( b = NULL, l = session->blocklists; !b && l; l = l->next )
        if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ),
                               defaultName ) )
            b = l->data;

    if( !b )
    {
        char * path = tr_buildPath( session->configDir, "blocklists", defaultName, NULL );
        b = _tr_blocklistNew( path, session->isBlocklistEnabled );
        tr_list_append( &session->blocklists, b );
        tr_free( path );
    }

    return _tr_blocklistSetContent( b, contentFilename );
}
int
main( void )
{
#ifndef WIN32
    const char *   tmpfile_txt = "/tmp/transmission-blocklist-test.txt";
    const char *   tmpfile_bin = "/tmp/transmission-blocklist-test.bin";
#else
    const char *   tmpfile_txt = "transmission-blocklist-test.txt";
    const char *   tmpfile_bin = "transmission-blocklist-test.bin";
#endif
    struct tr_address addr;
    int            test = 0;
    tr_blocklist * b;

    remove( tmpfile_txt );
    remove( tmpfile_bin );

    b = _tr_blocklistNew( tmpfile_bin, true );
    createTestBlocklist( tmpfile_txt );
    _tr_blocklistSetContent( b, tmpfile_txt );

    /* now run some tests */
    check( tr_address_from_string( &addr, "216.16.1.143" ) );
    check( !_tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.144" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.145" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.146" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.147" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.148" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.149" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.150" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.151" ) );
    check( _tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.152" ) );
    check( !_tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "216.16.1.153" ) );
    check( !_tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "217.0.0.1" ) );
    check( !_tr_blocklistHasAddress( b, &addr ) );
    check( tr_address_from_string( &addr, "255.0.0.1" ) );

    /* cleanup */
    _tr_blocklistFree( b );
    remove( tmpfile_txt );
    remove( tmpfile_bin );
    return 0;
}
static int
testBlockList (void)
{
    const char * tmpfile_txt = TEMPFILE_TXT;
    const char * tmpfile_bin = TEMPFILE_BIN;
    struct tr_address addr;
    tr_blocklist * b;

    remove (tmpfile_txt);
    remove (tmpfile_bin);

    b = _tr_blocklistNew (tmpfile_bin, true);
    createTestBlocklist (tmpfile_txt);
    _tr_blocklistSetContent (b, tmpfile_txt);

    /* now run some tests */
    check (tr_address_from_string (&addr, "216.16.1.143"));
    check (!_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.144"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.145"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.146"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.147"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.148"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.149"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.150"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.151"));
    check (_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.152"));
    check (!_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "216.16.1.153"));
    check (!_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "217.0.0.1"));
    check (!_tr_blocklistHasAddress (b, &addr));
    check (tr_address_from_string (&addr, "255.0.0.1"));

    /* cleanup */
    _tr_blocklistFree (b);
    remove (tmpfile_txt);
    remove (tmpfile_bin);
    return 0;
}
Exemple #4
0
static void
loadBlocklists( tr_session * session )
{
    int         binCount = 0;
    int         newCount = 0;
    struct stat sb;
    char      * dirname;
    DIR *       odir = NULL;
    tr_list *   list = NULL;
    const tr_bool   isEnabled = session->isBlocklistEnabled;

    /* walk through the directory and find blocklists */
    dirname = tr_buildPath( session->configDir, "blocklists", NULL );
    if( !stat( dirname,
               &sb ) && S_ISDIR( sb.st_mode )
      && ( ( odir = opendir( dirname ) ) ) )
    {
        struct dirent *d;
        for( d = readdir( odir ); d; d = readdir( odir ) )
        {
            char * filename;

            if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
                                                      */
                continue;

            filename = tr_buildPath( dirname, d->d_name, NULL );

            if( tr_stringEndsWith( filename, ".bin" ) )
            {
                /* if we don't already have this blocklist, add it */
                if( !tr_list_find( list, filename,
                                   (TrListCompareFunc)strcmp ) )
                {
                    tr_list_append( &list,
                                   _tr_blocklistNew( filename, isEnabled ) );
                    ++binCount;
                }
            }
            else
            {
                /* strip out the file suffix, if there is one, and add ".bin"
                  instead */
                tr_blocklist * b;
                const char *   dot = strrchr( d->d_name, '.' );
                const int      len = dot ? dot - d->d_name
                                         : (int)strlen( d->d_name );
                char         * tmp = tr_strdup_printf(
                                        "%s" TR_PATH_DELIMITER_STR "%*.*s.bin",
                                        dirname, len, len, d->d_name );
                b = _tr_blocklistNew( tmp, isEnabled );
                _tr_blocklistSetContent( b, filename );
                tr_list_append( &list, b );
                ++newCount;
                tr_free( tmp );
            }

            tr_free( filename );
        }

        closedir( odir );
    }

    session->blocklists = list;

    if( binCount )
        tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname );
    if( newCount )
        tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname );

    tr_free( dirname );
}