Changeset 8445


Ignore:
Timestamp:
May 20, 2009, 4:08:58 AM (12 years ago)
Author:
charles
Message:

(trunk libT) futz around with tr-dht.c a bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/tr-dht.c

    r8444 r8445  
    112112    char v[5];
    113113
    114     if(session)
     114    if( session ) /* already initialized */
    115115        return -1;
    116116
     
    130130        goto fail;
    131131
    132 #ifdef DEBUG_DHT
    133     dht_debug = stdout;
    134 #endif
     132    if( getenv( "TR_DHT_VERBOSE" ) != NULL )
     133        dht_debug = stderr;
    135134
    136135    dat_file = tr_buildPath( ss->configDir, "dht.dat", NULL );
     
    138137    tr_free( dat_file );
    139138    if(rc == 0) {
    140         if(tr_bencDictFindRaw(&benc, "id", &raw, &len)) {
    141             if(raw && len == 20) {
    142                 memcpy(myid, raw, len);
    143                 have_id = TRUE;
    144             }
    145         }
    146         if(tr_bencDictFindRaw(&benc, "nodes", &raw, &len)) {
    147             if(len % 6 == 2) {
    148                 /* This hack allows reading of uTorrent files, which I find
    149                    convenient. */
    150                 len -= 2;
    151             }
    152             nodes = tr_new( uint8_t, len );
    153             memcpy( nodes, raw, len );
    154         }
    155         tr_bencFree(&benc);
     139        if(( have_id = tr_bencDictFindRaw( &benc, "id", &raw, &len ) && len==20 ))
     140            memcpy( myid, raw, len );
     141        if( tr_bencDictFindRaw( &benc, "nodes", &raw, &len ) && !(len%6) )
     142            nodes = tr_memdup( raw, len );
     143        tr_bencFree( &benc );
    156144    }
    157145
    158146    if(!have_id) {
    159         /* Note that you cannot just use your BT id -- DHT ids need to be
    160            distributed uniformly, so it should either be the SHA-1 of
    161            something, or truly random. */
     147        /* Note that DHT ids need to be distributed uniformly,
     148         * so it should be something truly random. */
    162149        tr_cryptoRandBuf( myid, 20 );
    163150        have_id = TRUE;
     
    176163    if(nodes) {
    177164        struct bootstrap_closure * cl = tr_new( struct bootstrap_closure, 1 );
    178         if( !cl )
    179             tr_free( nodes );
    180         else {
    181             cl->session = session;
    182             cl->nodes = nodes;
    183             cl->len = len;
    184             tr_threadNew( dht_bootstrap, cl );
    185         }
    186     }
     165        cl->session = session;
     166        cl->nodes = nodes;
     167        cl->len = len;
     168        tr_threadNew( dht_bootstrap, cl );
     169    }
     170
    187171    tv.tv_sec = 0;
    188172    tv.tv_usec = tr_cryptoWeakRandInt( 1000000 );
     
    235219    }
    236220
    237     dht_uninit(dht_socket, 0);
     221    dht_uninit( dht_socket, 0 );
     222    EVUTIL_CLOSESOCKET( dht_socket );
    238223
    239224    session = NULL;
     
    255240getstatus(void *closure)
    256241{
    257     struct getstatus_closure *ret = (struct getstatus_closure*)closure;
     242    struct getstatus_closure * ret = closure;
    258243    int good, dubious, incoming;
    259244
    260     dht_nodes(&good, &dubious, NULL, &incoming);
     245    dht_nodes( &good, &dubious, NULL, &incoming );
     246
     247    ret->count = good + dubious;
    261248
    262249    if( good < 4 || good + dubious <= 8 )
     
    268255    else
    269256        ret->status = TR_DHT_GOOD;
    270 
    271     ret->count = good + dubious;
    272257}
    273258
     
    307292        return 0;
    308293
    309     /* Since we don't want to abuse our bootstrap nodes, we don't ping them
    310        if the DHT is in a good state. */
     294    /* Since we don't want to abuse our bootstrap nodes,
     295     * we don't ping them if the DHT is in a good state. */
    311296    if(bootstrap) {
    312297        if(tr_dhtStatus(ss, NULL) >= TR_DHT_FIREWALLED)
     
    324309
    325310static void
    326 callback(void *ignore UNUSED, int event,
    327          unsigned char *info_hash, void *data, size_t data_len)
    328 {
    329     if(event == DHT_EVENT_VALUES) {
     311callback( void *ignore UNUSED, int event,
     312          unsigned char *info_hash, void *data, size_t data_len )
     313{
     314    if( event == DHT_EVENT_VALUES )
     315    {
    330316        tr_torrent *tor;
    331         tr_pex *pex;
    332         size_t i, n;
    333         pex = tr_peerMgrCompactToPex(data, data_len, NULL, 0, &n);
    334         tr_globalLock(session);
    335         tor = tr_torrentFindFromHash(session, info_hash);
    336         if(tor && tr_torrentAllowsDHT(tor)) {
    337             for(i = 0; i < n; i++)
    338                 tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, pex + i);
     317        tr_globalLock( session );
     318        tor = tr_torrentFindFromHash( session, info_hash );
     319        if( tor && tr_torrentAllowsDHT( tor ))
     320        {
     321            size_t i, n;
     322            tr_pex * pex = tr_peerMgrCompactToPex(data, data_len, NULL, 0, &n);
     323            for( i=0; i<n; ++i )
     324                tr_peerMgrAddPex( tor, TR_PEER_FROM_DHT, pex+i );
     325            tr_free(pex);
    339326        }
    340         tr_globalUnlock(session);
    341         tr_free(pex);
    342     } else if(event == DHT_EVENT_SEARCH_DONE) {
     327        tr_globalUnlock( session );
     328    }
     329    else if( event == DHT_EVENT_SEARCH_DONE )
     330    {
    343331        tr_torrent * tor = tr_torrentFindFromHash( session, info_hash );
    344         if(tor)
     332        if( tor )
    345333            tor->dhtAnnounceInProgress = 0;
    346334    }
     
    394382         const void *v3, int len3)
    395383{
    396     unsigned char sha1[20];
    397     tr_sha1(sha1, v1, len1, v2, len2, v3, len3, NULL);
    398     if(hash_size > 20) {
    399         memset((char*)hash_return + 20, 0, hash_size - 20);
    400     }
    401     memcpy(hash_return, sha1, hash_size > 20 ? 20 : hash_size);
     384    unsigned char sha1[SHA_DIGEST_LENGTH];
     385    tr_sha1( sha1, v1, len1, v2, len2, v3, len3, NULL );
     386    memset( hash_return, 0, hash_size );
     387    memcpy( hash_return, sha1, MIN( hash_size, SHA_DIGEST_LENGTH ) );
    402388}
    403389
Note: See TracChangeset for help on using the changeset viewer.