Ticket #7: provide-node-count.patch

File provide-node-count.patch, 3.4 KB (added by jch, 13 years ago)
  • libtransmission/tr-dht.c

    diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c
    index c428e2a..319b3ca 100644
    a b dht_bootstrap(void *closure) 
    8383        port = ntohs(port);
    8484        /* There's no race here -- if we uninit between the test and the
    8585           AddNode, the AddNode will be ignored. */
    86         status = tr_dhtStatus(cl->session);
     86        status = tr_dhtStatus(cl->session, NULL);
    8787        if(status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED)
    8888            break;
    8989        tr_dhtAddNode(cl->session, &addr, port, 1);
    tr_dhtUninit(tr_session *ss) 
    207207
    208208    /* Since we only save known good nodes, avoid erasing older data if we
    209209       don't know enough nodes. */
    210     if(tr_dhtStatus(ss) >= TR_DHT_FIREWALLED) {
     210    if(tr_dhtStatus(ss, NULL) >= TR_DHT_FIREWALLED) {
    211211        tr_benc benc;
    212212        struct sockaddr_in sins[300];
    213213        char compact[300 * 6];
    tr_dhtEnabled(tr_session *ss) 
    239239    return (ss && session == ss);
    240240}
    241241
     242struct getstatus_closure {
     243    sig_atomic_t status;
     244    sig_atomic_t count;
     245};
     246
    242247static void
    243248getstatus(void *closure)
    244249{
    245     sig_atomic_t *ret = (sig_atomic_t*)closure;
     250    struct getstatus_closure *ret = (struct getstatus_closure*)closure;
    246251    int good, dubious, incoming;
    247252
    248253    dht_nodes(&good, &dubious, NULL, &incoming);
    249254    if(good < 4 || good + dubious <= 8)
    250         *ret = TR_DHT_BROKEN;
     255        ret->status = TR_DHT_BROKEN;
    251256    else if(good < 40)
    252         *ret = TR_DHT_POOR;
     257        ret->status = TR_DHT_POOR;
    253258    else if(incoming < 8)
    254         *ret = TR_DHT_FIREWALLED;
     259        ret->status = TR_DHT_FIREWALLED;
    255260    else
    256         *ret = TR_DHT_GOOD;
     261        ret->status = TR_DHT_GOOD;
     262
     263    ret->count = good + dubious;
    257264}
    258265
    259266int
    260 tr_dhtStatus(tr_session *ss)
     267tr_dhtStatus(tr_session *ss, int *nodes_return)
    261268{
    262     sig_atomic_t ret = -1;
     269    struct getstatus_closure ret = {-1, -1};
    263270
    264271    if(!tr_dhtEnabled(ss))
    265272        return TR_DHT_STOPPED;
    tr_dhtStatus(tr_session *ss) 
    267274    tr_runInEventThread(ss, getstatus, &ret);
    268275    do {
    269276        usleep(1000);
    270     } while(ret < 0);
    271     return ret;
     277    } while(ret.status < 0);
     278
     279    if(nodes_return)
     280        *nodes_return = ret.count;
     281
     282    return ret.status;
    272283}
    273284
    274285int
    tr_dhtAddNode(tr_session *ss, tr_address *address, unsigned short port, 
    294305    /* Since we don't want to abuse our bootstrap nodes, we don't ping them
    295306       if the DHT is in a good state. */
    296307    if(bootstrap) {
    297         if(tr_dhtStatus(ss) >= TR_DHT_FIREWALLED)
     308        if(tr_dhtStatus(ss, NULL) >= TR_DHT_FIREWALLED)
    298309            return 0;
    299310    }
    300311
    tr_dhtAnnounce(tr_torrent *tor, int announce) 
    344355    if(!tr_torrentAllowsDHT(tor))
    345356        return -1;
    346357
    347     if(tr_dhtStatus(tor->session) < TR_DHT_POOR)
     358    if(tr_dhtStatus(tor->session, NULL) < TR_DHT_POOR)
    348359        return 0;
    349360
    350361    dht_search(dht_socket, tor->info.hash,
  • libtransmission/tr-dht.h

    diff --git a/libtransmission/tr-dht.h b/libtransmission/tr-dht.h
    index 9e2c904..07008ae 100644
    a b THE SOFTWARE. 
    2929int tr_dhtInit(tr_session *ss);
    3030void tr_dhtUninit(tr_session *ss);
    3131int tr_dhtEnabled(tr_session *ss);
    32 int tr_dhtStatus(tr_session *ss);
     32int tr_dhtStatus(tr_session *ss, int *nodes_return);
    3333int tr_dhtPort(tr_session *ss);
    3434int tr_dhtAddNode(tr_session *ss, tr_address *address, unsigned short port,
    3535                  int bootstrap);