Changeset 1631


Ignore:
Timestamp:
Apr 1, 2007, 7:35:28 AM (16 years ago)
Author:
joshe
Message:

Add message type for adding torrent with individual settings.
Add message types for disabling pex.

Location:
branches/daemon/daemon
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/daemon/daemon/client.c

    r1626 r1631  
    331331    struct req * req;
    332332
    333     req = addreq( IPC_MSG_ADDFILES, -1, NULL );
     333    req = addreq( IPC_MSG_ADDMANYFILES, -1, NULL );
    334334    if( NULL == req )
    335335    {
     
    696696                buf = ipc_mkempty( &con->ipc, &buflen, req->id, req->tag );
    697697                break;
    698             case IPC_MSG_ADDFILES:
     698            case IPC_MSG_ADDMANYFILES:
    699699                buf = ipc_mkstrlist( &con->ipc, &buflen, req->id, -1,
    700700                                     req->strs );
  • branches/daemon/daemon/ipc.c

    r1617 r1631  
    7878static struct msg gl_msgs[] =
    7979{
    80     { "addfiles",            1, IPC_MSG_ADDFILES,     RB_ENTRY_INITIALIZER() },
     80    { "addfiles",            1, IPC_MSG_ADDMANYFILES, RB_ENTRY_INITIALIZER() },
     81    { "addfile-detailed",    2, IPC_MSG_ADDONEFILE,   RB_ENTRY_INITIALIZER() },
    8182    { "automap",             2, IPC_MSG_AUTOMAP,      RB_ENTRY_INITIALIZER() },
    8283    { "autostart",           2, IPC_MSG_AUTOSTART,    RB_ENTRY_INITIALIZER() },
     
    9091    { "get-info",            2, IPC_MSG_GETINFO,      RB_ENTRY_INITIALIZER() },
    9192    { "get-info-all",        2, IPC_MSG_GETINFOALL,   RB_ENTRY_INITIALIZER() },
     93    { "get-pex",             2, IPC_MSG_GETPEX,       RB_ENTRY_INITIALIZER() },
    9294    { "get-port",            2, IPC_MSG_GETPORT,      RB_ENTRY_INITIALIZER() },
    9395    { "get-status",          2, IPC_MSG_GETSTAT,      RB_ENTRY_INITIALIZER() },
     
    99101    { "noop",                2, IPC_MSG_NOOP,         RB_ENTRY_INITIALIZER() },
    100102    { "not-supported",       2, IPC_MSG_NOTSUP,       RB_ENTRY_INITIALIZER() },
     103    { "pex",                 2, IPC_MSG_PORT,         RB_ENTRY_INITIALIZER() },
    101104    { "port",                2, IPC_MSG_PORT,         RB_ENTRY_INITIALIZER() },
    102105    { "quit",                1, IPC_MSG_QUIT,         RB_ENTRY_INITIALIZER() },
  • branches/daemon/daemon/ipc.h

    r1621 r1631  
    3838enum ipc_msg
    3939{
    40     IPC_MSG_ADDFILES = 0,
     40    IPC_MSG_ADDMANYFILES = 0,
     41    IPC_MSG_ADDONEFILE,
    4142    IPC_MSG_AUTOMAP,
    4243    IPC_MSG_AUTOSTART,
     
    5051    IPC_MSG_GETINFO,
    5152    IPC_MSG_GETINFOALL,
     53    IPC_MSG_GETPEX,
    5254    IPC_MSG_GETPORT,
    5355    IPC_MSG_GETSTAT,
     
    5961    IPC_MSG_NOOP,
    6062    IPC_MSG_NOTSUP,
     63    IPC_MSG_PEX,
    6164    IPC_MSG_PORT,
    6265    IPC_MSG_QUIT,
  • branches/daemon/daemon/ipcproto.txt

    r1617 r1631  
    8383understand.
    8484
     85If a reference to a boolean is seen, it should be taken to mean an
     86integer with a value of 0 representing false, 1 representing true, and
     87any other value undefined.
     88
     89Individual torrents are identified by a unique integer. This integer
     90is only valid for the current connection and may be invalid or refer
     91to another torrent in a future connection. If a torrent is closed it's
     92ID will never be reused to refer to another torrent for at least the
     93duration of the connection. Negative integers or 0 are not valid IDs.
     94
    8595A list of keys and the format of their values follows. Also listed is
    8696the minimum protocol version that the key may be used with.
     
    91101Format:  list of strings
    92102Example: 8:addfilesl21:/torrents/foo.torrent20:/home/me/bar.torrente
    93 Details: Each string is the absolute path to a torrent file for the
    94          server to add. Note that whether or not the torrent file is
    95          copied (allowing the original to be moved or deleted safely)
    96          is implementation dependent and may not currently be known or
    97          changed with this protocol.
     103Details: Each string is the absolute path to a torrent metainfo file
     104         for the server to add. Note that whether or not the torrent
     105         metainfo file is copied (allowing the original to be moved or
     106         deleted safely) is implementation dependent and may not
     107         currently be known or changed with this protocol.
     108
     109Key:     "addfile-detailed"
     110Version: 2
     111Format:  dict
     112Example: 16:addfile-detailedde
     113Details: Dictionary containing information about a torrent for the
     114         server to add. Valid keys include:
     115           "file"       string, filename of torrent metainfo file
     116           "data"       string, contents of a torrent metainfo file
     117           "directory"  string, directory for data files for the torrent
     118           "autostart"  boolean, start the torrent automatically
     119         Either "file" or "data" is required, but both are not allowed.
    98120
    99121Key:     "automap"
    100122Version: 2
    101 Format:  int, 0 or 1
     123Format:  boolean
    102124Example: 7:automapi1e
    103125Details: Enable (1) or disable (0) automatic port mapping on the
     
    107129Key:     "autostart"
    108130Version: 2
    109 Format:  int, 0 or 1
     131Format:  boolean
    110132Example: 9:autostarti0e
    111133Details: Enable (1) or disable (0) automatic starting of new torrents
     
    173195Details: Same as "getinfo" message with all torrent IDs specified.
    174196
     197Key:     "get-pex"
     198Version: 2
     199Format:  value is ignored
     200Example: 7:get-pex0:
     201Details: Requests that a "pex" message be sent back.
     202
    175203Key:     "get-port"
    176204Version: 2
     
    234262Details: Sent in response to a tagged message to indicated that the
    235263         message was not supported.
     264
     265Key:     "pex"
     266Version: 2
     267Format:  boolean
     268Example: 3:pexi0e
     269Details: Enables or disables peer exchange.
    236270
    237271Key:     "port"
     
    315349         Negative values are interpreted as no limit.
    316350
    317 Key:     "version"
    318 Version: 1
    319 Format:  dict containing int "min" and "max"
    320 Example: 7:versiond3:mini1e3:maxi2ee
    321 Details: Described above.
    322 
    323 
    324 Individual torrents are identified by a unique integer. This integer
    325 is only valid for the current connection and may be invalid or refer
    326 to another torrent in a future connection. If a torrent is closed it's
    327 ID will never be reused to refer to another torrent for at least the
    328 duration of the connection. Negative integers or 0 are not valid IDs.
    329 
    330351
    331352Info types for "get-info" and "info" messages. The only type for which
     
    336357"name"     string, torrent name
    337358"path"     string, path to .torrent file
    338 "saved"    integer, 1 if a copy of this torrent was saved, 0 otherwise
    339 "private"  integer, 1 if the torrent is private, 0 otherwise
     359"saved"    boolean, true if a copy of this torrent was saved
     360"private"  boolean, true if the torrent is private
    340361"trackers" a list of lists of dictionaries containing tracker info:
    341362             "address"  string, hostname or ip address of tracker
     
    378399"peers-total"       integer, total connected peers
    379400"peers-uploading"   integer, peers uploading to us
    380 "running"           integer, 0 if torrent is stopped or stopping, 1 otherwise
     401"running"           boolean, false if torrent is stopped or stopping
    381402"state"             string, one of the following:
    382403                      "checking"    performing hash check on file data
  • branches/daemon/daemon/remote.c

    r1627 r1631  
    6666    struct strlist    remove;
    6767    char              dir[MAXPATHLEN];
     68    int               pex;
    6869};
    6970
  • branches/daemon/daemon/server.c

    r1621 r1631  
    6666static void defmsg   ( enum ipc_msg, benc_val_t *, int64_t, void * );
    6767static void noopmsg  ( enum ipc_msg, benc_val_t *, int64_t, void * );
    68 static void addmsg   ( enum ipc_msg, benc_val_t *, int64_t, void * );
     68static void addmsg1  ( enum ipc_msg, benc_val_t *, int64_t, void * );
     69static void addmsg2  ( enum ipc_msg, benc_val_t *, int64_t, void * );
    6970static void quitmsg  ( enum ipc_msg, benc_val_t *, int64_t, void * );
    7071static void intmsg   ( enum ipc_msg, benc_val_t *, int64_t, void * );
     
    9899    }
    99100
    100     if( 0 > ipc_addmsg( gl_tree, IPC_MSG_ADDFILES,     addmsg  ) ||
     101    if( 0 > ipc_addmsg( gl_tree, IPC_MSG_ADDMANYFILES, addmsg1 ) ||
     102        0 > ipc_addmsg( gl_tree, IPC_MSG_ADDONEFILE,   addmsg2 ) ||
    101103        0 > ipc_addmsg( gl_tree, IPC_MSG_AUTOMAP,      intmsg  ) ||
    102104        0 > ipc_addmsg( gl_tree, IPC_MSG_AUTOSTART,    intmsg  ) ||
     
    109111        0 > ipc_addmsg( gl_tree, IPC_MSG_GETINFO,      infomsg ) ||
    110112        0 > ipc_addmsg( gl_tree, IPC_MSG_GETINFOALL,   infomsg ) ||
     113        0 > ipc_addmsg( gl_tree, IPC_MSG_GETPEX,       prefmsg ) ||
    111114        0 > ipc_addmsg( gl_tree, IPC_MSG_GETPORT,      prefmsg ) ||
    112115        0 > ipc_addmsg( gl_tree, IPC_MSG_GETSTAT,      infomsg ) ||
     
    116119        0 > ipc_addmsg( gl_tree, IPC_MSG_LOOKUP,       lookmsg ) ||
    117120        0 > ipc_addmsg( gl_tree, IPC_MSG_NOOP,         noopmsg ) ||
     121        0 > ipc_addmsg( gl_tree, IPC_MSG_PEX,          intmsg  ) ||
    118122        0 > ipc_addmsg( gl_tree, IPC_MSG_PORT,         intmsg  ) ||
    119123        0 > ipc_addmsg( gl_tree, IPC_MSG_QUIT,         quitmsg ) ||
     
    390394
    391395void
    392 addmsg( enum ipc_msg id UNUSED, benc_val_t * val, int64_t tag, void * arg )
    393 {
    394     struct client * client = arg;
    395     benc_val_t    * file;
    396     int64_t       * added, tor;
    397     int             ii;
    398     size_t          count, buflen;
     396addmsg1( enum ipc_msg id UNUSED, benc_val_t * val, int64_t tag, void * arg )
     397{
     398    struct client * client = arg;
     399    benc_val_t      pk, * added, * file;
     400    int             ii, tor;
     401    size_t          buflen;
    399402    uint8_t       * buf;
     403    tr_info_t     * inf;
    400404
    401405    if( NULL == val || TYPE_LIST != val->type )
     
    405409    }
    406410
    407     if( 0 < tag )
    408     {
    409         added = calloc( val->val.l.count, sizeof( added[0] ) );
    410         if( NULL == added )
    411         {
    412             mallocmsg( val->val.l.count * sizeof( added[0] ) );
    413             byebye( client->ev, EVBUFFER_EOF, NULL );
    414             return;
    415         }
    416         count = 0;
     411    if( 0 > ipc_initinfo( &client->ipc, IPC_MSG_INFO, tag, &pk, &added ) )
     412    {
     413        errnomsg( "failed to build message" );
     414        byebye( client->ev, EVBUFFER_EOF, NULL );
     415        return;
    417416    }
    418417
     
    425424        }
    426425        /* XXX need to somehow inform client of skipped or failed files */
    427         tor = torrent_add( file->val.s.s );
    428         if( 0 < tag )
    429         {
    430             added[count] = tor;
    431             count++;
    432         }
    433     }
    434 
    435     if( 0 < tag )
    436     {
    437         buf = ipc_mkints( &client->ipc, &buflen, tag, IPC_MSG_INFO,
    438                           count, added );
     426        tor = torrent_add( file->val.s.s, NULL, -1 );
     427        if( TORRENT_ID_VALID( tor ) )
     428        {
     429            inf = torrent_info( tor );
     430            if( 0 > ipc_addinfo( added, tor, inf, 0 ) )
     431            {
     432                errnomsg( "failed to build message" );
     433                tr_bencFree( &pk );
     434                byebye( client->ev, EVBUFFER_EOF, NULL );
     435                return;
     436            }
     437        }
     438    }
     439
     440    buf = ipc_mkinfo( &pk, &buflen );
     441    tr_bencFree( &pk );
     442    queuemsg( client, buf, buflen );
     443    free( buf );
     444}
     445
     446void
     447addmsg2( enum ipc_msg id UNUSED, benc_val_t * dict, int64_t tag, void * arg )
     448{
     449    struct client * client = arg;
     450    benc_val_t    * val, pk;
     451    int             tor, start;
     452    size_t          buflen;
     453    uint8_t       * buf;
     454    const char    * dir;
     455    tr_info_t     * inf;
     456
     457    if( NULL == dict || TYPE_DICT != dict->type )
     458    {
     459        msgresp( client, tag, IPC_MSG_NOTSUP );
     460        return;
     461    }
     462
     463    val   = tr_bencDictFind( dict, "directory" );
     464    dir   = ( NULL == val || TYPE_STR != val->type ? NULL : val->val.s.s );
     465    val   = tr_bencDictFind( dict, "autostart" );
     466    start = ( NULL == val || TYPE_INT != val->type ? -1 :
     467              ( val->val.i ? 1 : 0 ) );
     468    val   = tr_bencDictFind( dict, "data" );
     469    if( NULL != val && TYPE_STR == val->type )
     470    {
     471        /* XXX not quite yet */
     472        msgresp( client, tag, IPC_MSG_NOTSUP );
     473        return;
     474    }
     475    else
     476    {
     477        val = tr_bencDictFind( dict, "file" );
     478        if( NULL == val || TYPE_STR != val->type )
     479        {
     480            msgresp( client, tag, IPC_MSG_NOTSUP );
     481            return;
     482        }
     483        /* XXX detect duplicates and return a message indicating so */
     484        tor = torrent_add( val->val.s.s, dir, start );
     485    }
     486
     487    if( TORRENT_ID_VALID( tor ) )
     488    {
     489        if( 0 > ipc_initinfo( &client->ipc, IPC_MSG_INFO, tag, &pk, &val ) )
     490        {
     491            errnomsg( "failed to build message" );
     492            byebye( client->ev, EVBUFFER_EOF, NULL );
     493            return;
     494        }
     495        inf = torrent_info( tor );
     496        if( 0 > ipc_addinfo( val, tor, inf, 0 ) )
     497        {
     498            errnomsg( "failed to build message" );
     499            tr_bencFree( &pk );
     500            byebye( client->ev, EVBUFFER_EOF, NULL );
     501            return;
     502        }
     503        buf = ipc_mkinfo( &pk, &buflen );
     504        tr_bencFree( &pk );
    439505        queuemsg( client, buf, buflen );
    440         free( added );
    441506        free( buf );
     507    }
     508    else
     509    {
     510        msgresp( client, tag, IPC_MSG_FAIL );
    442511    }
    443512}
     
    482551        case IPC_MSG_DOWNLIMIT:
    483552            torrent_set_downlimit( num );
     553            break;
     554        case IPC_MSG_PEX:
     555            torrent_set_pex( num ? 1 : 0 );
    484556            break;
    485557        case IPC_MSG_PORT:
     
    803875                             torrent_get_downlimit() );
    804876            break;
     877        case IPC_MSG_GETPEX:
     878            buf = ipc_mkint( &client->ipc, &buflen, IPC_MSG_PEX, tag,
     879                             torrent_get_pex() );
     880            break;
    805881        case IPC_MSG_GETPORT:
    806882            buf = ipc_mkint( &client->ipc, &buflen, IPC_MSG_PORT, tag,
  • branches/daemon/daemon/torrents.c

    r1621 r1631  
    5757    uint8_t         hash[SHA_DIGEST_LENGTH];
    5858    tr_torrent_t  * tor;
     59    int             pexset;
     60    int             pex;
    5961    RB_ENTRY( tor ) idlinks;
    6062    RB_ENTRY( tor ) hashlinks;
     
    6466RB_HEAD( hashtree, tor );
    6567
    66 static struct tor * opentorrent ( const char *, const char * );
     68static struct tor * opentorrent ( const char *, const char *, const char * );
    6769static void         closetorrent( struct tor *, int );
    6870static void         starttimer  ( int );
     
    7678static struct tor * idlookup    ( int, int );
    7779static struct tor * hashlookup  ( const uint8_t *, int );
     80static struct tor * iterate     ( struct tor * );
    7881
    7982static struct event_base * gl_base      = NULL;
     
    8992
    9093static int                 gl_autostart = 1;
     94static int                 gl_pex       = 1;
    9195static int                 gl_port      = TR_DEFAULT_PORT;
    9296static int                 gl_mapping   = 0;
     
    113117
    114118    loadstate();
    115 
    116     if( TR_DEFAULT_PORT != gl_port )
    117     {
    118         tr_setBindPort( gl_handle, gl_port );
    119     }
    120     tr_natTraversalEnable( gl_handle, gl_mapping );
    121     tr_setGlobalUploadLimit( gl_handle, gl_uplimit );
    122     tr_setGlobalDownloadLimit( gl_handle, gl_downlimit );
    123 }
    124 
    125 int
    126 torrent_add( const char * path )
     119}
     120
     121int
     122torrent_add( const char * path, const char * dir, int autostart )
    127123{
    128124    struct tor * tor;
     
    131127    assert( !gl_exiting );
    132128
    133     tor = opentorrent( path, NULL );
     129    tor = opentorrent( path, NULL, dir );
    134130    if( NULL == tor )
    135131    {
     
    137133    }
    138134
    139     if( gl_autostart )
     135    if( 0 > autostart )
     136    {
     137        autostart = gl_autostart;
     138    }
     139    if( autostart )
    140140    {
    141141        tr_torrentStart( tor->tor );
     
    284284    assert( !gl_exiting );
    285285
    286     if( NULL == tor )
    287     {
    288         tor = RB_MIN( tortree, &gl_tree );
    289     }
    290     else
    291     {
    292         tor = RB_NEXT( tortree, &gl_tree, tor );
    293     }
    294 
    295     while( NULL != tor && tor->deleting )
    296     {
    297         tor = RB_NEXT( tortree, &gl_tree, tor );
    298     }
     286    tor = iterate( tor );
    299287
    300288    if( NULL != tor )
     
    360348
    361349void
     350torrent_set_pex( int pex )
     351{
     352    struct tor * tor;
     353
     354    assert( NULL != gl_handle );
     355    assert( !gl_exiting );
     356
     357    if( pex == gl_pex )
     358    {
     359        return;
     360    }
     361    gl_pex = pex;
     362
     363    for( tor = iterate( NULL ); NULL != tor; tor = iterate( tor ) )
     364    {
     365        if( tor->pexset )
     366        {
     367            continue;
     368        }
     369        tr_torrentDisablePex( tor->tor, !gl_pex );
     370    }
     371
     372    savestate();
     373}
     374
     375int
     376torrent_get_pex( void )
     377{
     378    return gl_pex;
     379}
     380
     381void
    362382torrent_enable_port_mapping( int automap )
    363383{
     
    424444
    425445struct tor *
    426 opentorrent( const char * path, const char * hash )
     446opentorrent( const char * path, const char * hash, const char * dir )
    427447{
    428448    struct tor * tor, * found;
     
    432452    assert( NULL == path || NULL == hash );
    433453    assert( NULL != path || NULL != hash );
     454
     455    /* XXX should probably wrap around back to 1 and avoid duplicates */
     456    if( INT_MAX == gl_lastid )
     457    {
     458        errmsg( "Congratulations, you're the %ith torrent! Your prize the "
     459                "inability to load any more torrents, enjoy!", INT_MAX );
     460        return NULL;
     461    }
    434462
    435463    tor = calloc( 1, sizeof *tor );
     
    473501        return found;
    474502    }
    475     tor->id       = ++gl_lastid;
     503    gl_lastid++;
     504    tor->id       = gl_lastid;
    476505    tor->deleting = 0;
    477506
     
    480509    memcpy( tor->hash, inf->hash, sizeof tor->hash );
    481510
    482     tr_torrentSetFolder( tor->tor, gl_dir );
     511    tr_torrentSetFolder( tor->tor, ( NULL == dir ? gl_dir : dir ) );
     512
     513    if( TR_FLAG_PRIVATE & inf->flags )
     514    {
     515        tor->pexset = 1;
     516        tor->pex    = 0;
     517    }
     518    else
     519    {
     520        tr_torrentDisablePex( tor->tor, !gl_pex );
     521    }
    483522
    484523    found = RB_INSERT( tortree, &gl_tree, tor );
     
    600639    uint8_t   *  buf;
    601640    size_t       len;
    602     benc_val_t   top, * num, * str, * list, * dict, * hash, * pause, * dir;
     641    benc_val_t   top, * num, * str, * list, * dict;
    603642    int          ii;
    604643    struct tor * tor;
     644    const char * dir;
    605645
    606646    buf = readfile( gl_state, &len );
     
    621661    if( NULL != num && TYPE_INT == num->type )
    622662    {
    623         gl_autostart = num->val.i;
    624     }
     663        gl_autostart = ( num->val.i ? 1 : 0 );
     664    }
     665
    625666    num = tr_bencDictFind( &top, "port" );
    626667    if( NULL != num && TYPE_INT == num->type &&
     
    629670        gl_port = num->val.i;
    630671    }
     672    tr_setBindPort( gl_handle, gl_port );
     673
     674    num = tr_bencDictFind( &top, "default-pex" );
     675    if( NULL != num && TYPE_INT == num->type )
     676    {
     677        gl_pex = ( num->val.i ? 1 : 0 );
     678    }
     679
    631680    num = tr_bencDictFind( &top, "port-mapping" );
    632681    if( NULL != num && TYPE_INT == num->type )
    633682    {
    634         gl_mapping = num->val.i;
    635     }
     683        gl_mapping = ( num->val.i ? 1 : 0 );
     684    }
     685    tr_natTraversalEnable( gl_handle, gl_mapping );
     686
    636687    num = tr_bencDictFind( &top, "upload-limit" );
    637688    if( NULL != num && TYPE_INT == num->type )
     
    639690        gl_uplimit = num->val.i;
    640691    }
     692    tr_setGlobalUploadLimit( gl_handle, gl_uplimit );
     693
    641694    num = tr_bencDictFind( &top, "download-limit" );
    642695    if( NULL != num && TYPE_INT == num->type )
     
    644697        gl_downlimit = num->val.i;
    645698    }
     699    tr_setGlobalDownloadLimit( gl_handle, gl_downlimit );
     700
    646701    str = tr_bencDictFind( &top, "default-directory" );
    647702    if( NULL != str && TYPE_STR == str->type )
     
    651706
    652707    list = tr_bencDictFind( &top, "torrents" );
    653     if( NULL != list && TYPE_LIST == list->type )
    654     {
    655         for( ii = 0; ii < list->val.l.count; ii++ )
    656         {
    657             dict = &list->val.l.vals[ii];
    658             if( TYPE_DICT != dict->type )
    659             {
    660                 continue;
    661             }
    662             hash = tr_bencDictFind( dict, "hash" );
    663             if( NULL == hash || TYPE_STR != hash->type ||
    664                 2 * SHA_DIGEST_LENGTH != hash->val.s.i )
    665             {
    666                 continue;
    667             }
    668             tor = opentorrent( NULL, hash->val.s.s );
    669             if( NULL == tor )
    670             {
    671                 continue;
    672             }
    673             dir = tr_bencDictFind( dict, "directory" );
    674             if( NULL != dir && TYPE_STR == dir->type )
    675             {
    676                 tr_torrentSetFolder( tor->tor, dir->val.s.s );
    677             }
    678             pause = tr_bencDictFind( dict, "paused" );
    679             if( NULL != pause && TYPE_INT == pause->type && !pause->val.i )
    680             {
    681                 tr_torrentStart( tor->tor );
    682             }
     708    if( NULL == list || TYPE_LIST != list->type )
     709    {
     710        return 0;
     711    }
     712
     713    for( ii = 0; ii < list->val.l.count; ii++ )
     714    {
     715        dict = &list->val.l.vals[ii];
     716        if( TYPE_DICT != dict->type )
     717        {
     718            continue;
     719        }
     720
     721        str = tr_bencDictFind( dict, "directory" );
     722        dir = ( NULL != str && TYPE_STR == str->type ? str->val.s.s : NULL );
     723
     724        str = tr_bencDictFind( dict, "hash" );
     725        if( NULL == str || TYPE_STR != str->type ||
     726            2 * SHA_DIGEST_LENGTH != str->val.s.i )
     727        {
     728            continue;
     729        }
     730
     731        tor = opentorrent( NULL, str->val.s.s, dir );
     732        if( NULL == tor )
     733        {
     734            continue;
     735        }
     736
     737        num = tr_bencDictFind( dict, "pex" );
     738        if( NULL != num && TYPE_INT == num->type )
     739        {
     740            tor->pexset = 1;
     741            tor->pex = ( num->val.i ? 1 : 0 );
     742        }
     743        tr_torrentDisablePex( tor->tor, !( tor->pexset ? tor->pex : gl_pex ) );
     744
     745        num = tr_bencDictFind( dict, "paused" );
     746        if( NULL != num && TYPE_INT == num->type && !num->val.i )
     747        {
     748            tr_torrentStart( tor->tor );
    683749        }
    684750    }
     
    695761    tr_stat_t  * st;
    696762    uint8_t    * buf;
    697     int          len;
     763    int          len, pexset;
    698764
    699765    tr_bencInit( &top, TYPE_DICT );
     
    707773    tr_bencInitInt( tr_bencDictAdd( &top, "autostart" ),      gl_autostart );
    708774    tr_bencInitInt( tr_bencDictAdd( &top, "port" ),           gl_port );
     775    tr_bencInitInt( tr_bencDictAdd( &top, "default-pex" ),    gl_pex );
    709776    tr_bencInitInt( tr_bencDictAdd( &top, "port-mapping" ),   gl_mapping );
    710777    tr_bencInitInt( tr_bencDictAdd( &top, "upload-limit" ),   gl_uplimit );
     
    737804        assert( NULL != tor );
    738805        tr_bencInit( tor, TYPE_DICT );
    739         if( tr_bencDictReserve( tor, 3 ) )
     806        inf    = tr_torrentInfo( ii->tor );
     807        st     = tr_torrentStat( ii->tor );
     808        pexset = ( ii->pexset && !( TR_FLAG_PRIVATE & inf->flags ) );
     809        if( tr_bencDictReserve( tor, ( pexset ? 4 : 3 ) ) )
    740810        {
    741811            goto nomem;
    742812        }
    743         inf = tr_torrentInfo( ii->tor );
    744         st  = tr_torrentStat( ii->tor );
    745813        tr_bencInitStr( tr_bencDictAdd( tor, "hash" ),
    746814                        inf->hashString, 2 * SHA_DIGEST_LENGTH, 1 );
     
    749817        tr_bencInitStr( tr_bencDictAdd( tor, "directory" ),
    750818                        tr_torrentGetFolder( ii->tor ), -1, 1 );
     819        if( pexset )
     820        {
     821            tr_bencInitInt( tr_bencDictAdd( tor, "pex" ), ii->pex );
     822        }
    751823    }
    752824
     
    894966    return found;
    895967}
     968
     969struct tor *
     970iterate( struct tor * tor )
     971{
     972    assert( NULL != gl_handle );
     973    assert( !gl_exiting );
     974
     975    if( NULL == tor )
     976    {
     977        tor = RB_MIN( tortree, &gl_tree );
     978    }
     979    else
     980    {
     981        tor = RB_NEXT( tortree, &gl_tree, tor );
     982    }
     983
     984    while( NULL != tor && tor->deleting )
     985    {
     986        tor = RB_NEXT( tortree, &gl_tree, tor );
     987    }
     988
     989    return tor;
     990}
  • branches/daemon/daemon/torrents.h

    r1617 r1631  
    3434
    3535void         torrent_init                ( struct event_base * );
    36 int          torrent_add                 ( const char * );
     36int          torrent_add                 ( const char *, const char *, int );
    3737void         torrent_start               ( int );
    3838void         torrent_stop                ( int );
     
    4848void         torrent_set_port            ( int );
    4949int          torrent_get_port            ( void );
     50void         torrent_set_pex             ( int );
     51int          torrent_get_pex             ( void );
    5052void         torrent_enable_port_mapping ( int );
    5153int          torrent_get_port_mapping    ( void );
Note: See TracChangeset for help on using the changeset viewer.