Changeset 8880


Ignore:
Timestamp:
Aug 7, 2009, 2:38:09 PM (13 years ago)
Author:
charles
Message:

(trunk libT) #2281: add logging related to DHT (Patch from Juliusz)

Location:
trunk/libtransmission
Files:
2 edited

Legend:

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

    r8727 r8880  
    116116    tr_free( cl->nodes );
    117117    tr_free( closure );
     118    tr_ndbg( "DHT", "Finished bootstrapping" );
    118119}
    119120
     
    134135        return -1;
    135136
    136     dht_socket = socket(PF_INET, SOCK_DGRAM, 0);
    137     if(dht_socket < 0)
    138         return -1;
    139 
    140137    dht_port = tr_sessionGetPeerPort(ss);
    141138    if(dht_port <= 0)
    142139        return -1;
     140
     141    tr_ndbg( "DHT", "Initialising DHT" );
     142
     143    dht_socket = socket(PF_INET, SOCK_DGRAM, 0);
     144    if(dht_socket < 0)
     145        goto fail;
    143146
    144147    memset(&sin, 0, sizeof(sin));
     
    158161        if(( have_id = tr_bencDictFindRaw( &benc, "id", &raw, &len ) && len==20 ))
    159162            memcpy( myid, raw, len );
    160         if( tr_bencDictFindRaw( &benc, "nodes", &raw, &len ) && !(len%6) )
     163        if( tr_bencDictFindRaw( &benc, "nodes", &raw, &len ) && !(len%6) ) {
    161164            nodes = tr_memdup( raw, len );
     165            tr_ninf( "DHT", "Bootstrapping from %d old nodes", (int)(len/6) );
     166        }
    162167        tr_bencFree( &benc );
    163168    }
    164169
    165     if(!have_id) {
     170    if( have_id )
     171        tr_ninf( "DHT", "Reusing old id" );
     172    else {
    166173        /* Note that DHT ids need to be distributed uniformly,
    167174         * so it should be something truly random. */
     175        tr_ninf( "DHT", "Generating new id" );
    168176        tr_cryptoRandBuf( myid, 20 );
    169177    }
     
    190198    tr_timerAdd( &dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
    191199
     200    tr_ndbg( "DHT", "DHT initialised" );
     201
    192202    return 1;
    193203
     
    198208        dht_socket = -1;
    199209        session = NULL;
     210        tr_ndbg( "DHT", "DHT initialisation failed (errno = %d)", save );
    200211        errno = save;
    201212    }
     
    210221        return;
    211222
     223    tr_ndbg( "DHT", "Uninitialising DHT" );
     224
    212225    event_del(&dht_event);
    213226
    214227    /* Since we only save known good nodes, avoid erasing older data if we
    215228       don't know enough nodes. */
    216     if(tr_dhtStatus(ss, NULL) >= TR_DHT_FIREWALLED) {
     229    if(tr_dhtStatus(ss, NULL) < TR_DHT_FIREWALLED)
     230        tr_ninf( "DHT", "Not saving nodes, DHT not ready" );
     231    else {
    217232        tr_benc benc;
    218233        struct sockaddr_in sins[300];
     
    222237        int n = dht_get_nodes(sins, 300);
    223238        int j = 0;
     239
     240        tr_ninf( "DHT", "Saving %d nodes", n );
    224241        for( i=0; i<n; ++i ) {
    225242            memcpy( compact + j, &sins[i].sin_addr, 4 );
     
    239256    EVUTIL_CLOSESOCKET( dht_socket );
    240257
     258    tr_ndbg("DHT", "Done uninitialising DHT");
     259
    241260    session = NULL;
    242261}
     
    299318
    300319int
    301 tr_dhtAddNode(tr_session *ss, tr_address *address, tr_port port, tr_bool bootstrap)
     320tr_dhtAddNode(tr_session *ss, tr_address *address, tr_port port,
     321              tr_bool bootstrap)
    302322{
    303323    struct sockaddr_in sin;
     
    323343
    324344    return 1;
     345}
     346
     347const char *
     348tr_dhtPrintableStatus(int status)
     349{
     350    switch(status) {
     351    case TR_DHT_STOPPED: return "stopped";
     352    case TR_DHT_BROKEN: return "broken";
     353    case TR_DHT_POOR: return "poor";
     354    case TR_DHT_FIREWALLED: return "firewalled";
     355    case TR_DHT_GOOD: return "good";
     356    default: return "???";
     357    }
    325358}
    326359
     
    341374                tr_peerMgrAddPex( tor, TR_PEER_FROM_DHT, pex+i );
    342375            tr_free(pex);
     376            tr_torinf(tor, "Learned %d peers from DHT", (int)n);
    343377        }
    344378        tr_globalUnlock( session );
     
    347381    {
    348382        tr_torrent * tor = tr_torrentFindFromHash( session, info_hash );
    349         if( tor )
     383        if( tor ) {
     384            tr_torinf(tor, "DHT announce done");
    350385            tor->dhtAnnounceInProgress = 0;
     386        }
    351387    }
    352388}
     
    355391tr_dhtAnnounce(tr_torrent *tor, tr_bool announce)
    356392{
    357     int rc;
     393    int rc, status, numnodes;
    358394
    359395    if( !tr_torrentAllowsDHT( tor ) )
    360396        return -1;
    361397
    362     if( tr_dhtStatus( tor->session, NULL ) < TR_DHT_POOR )
     398    status = tr_dhtStatus( tor->session, &numnodes );
     399    if(status < TR_DHT_POOR ) {
     400        tr_tordbg(tor, "DHT not ready (%s, %d nodes)",
     401                  tr_dhtPrintableStatus(status), numnodes);
    363402        return 0;
     403    }
    364404
    365405    rc = dht_search( dht_socket, tor->info.hash,
     
    367407                     callback, NULL);
    368408
    369     if( rc >= 1 )
     409    if( rc >= 1 ) {
     410        tr_torinf(tor, "Starting DHT announce (%s, %d nodes)",
     411                  tr_dhtPrintableStatus(status), numnodes);
    370412        tor->dhtAnnounceInProgress = TRUE;
     413    } else {
     414        tr_torerr(tor, "DHT announce failed, errno = %d (%s, %d nodes)",
     415                  errno, tr_dhtPrintableStatus(status), numnodes);
     416    }
    371417
    372418    return 1;
     
    382428            tosleep = 0;
    383429        } else {
    384             perror("dht_periodic");
     430            tr_nerr("DHT", "dht_periodic failed (errno = %d)", errno);
    385431            if(errno == EINVAL || errno == EFAULT)
    386432                    abort();
  • trunk/libtransmission/tr-dht.h

    r8452 r8880  
    3232tr_port tr_dhtPort ( const tr_session * );
    3333int tr_dhtStatus( tr_session *, int * setme_nodeCount );
     34const char *tr_dhtPrintableStatus(int status);
    3435int tr_dhtAddNode( tr_session *, tr_address *, tr_port, tr_bool bootstrap );
    3536int tr_dhtAnnounce( tr_torrent *, tr_bool announce );
Note: See TracChangeset for help on using the changeset viewer.