Function dht_init

Synopsis

#include <dht.h>

int dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)

Description

No description yet.

Mentioned in

Source

Lines 1769-1843 in dht.c. Line 40 in dht.h.

int
dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)
{
    int rc;

    if(dht_socket >= 0 || dht_socket6 >= 0 || buckets || buckets6) {
        errno = EBUSY;
        return -1;
    }

    searches = NULL;
    numsearches = 0;

    storage = NULL;
    numstorage = 0;

    if(s >= 0) {
        buckets = calloc(1, sizeof(struct bucket));
        if(buckets == NULL)
            return -1;
        buckets->max_count = 128;
        buckets->af = AF_INET;
    }

    if(s6 >= 0) {
        buckets6 = calloc(1, sizeof(struct bucket));
        if(buckets6 == NULL)
            return -1;
        buckets6->max_count = 128;
        buckets6->af = AF_INET6;
    }

    memcpy(myid, id, 20);
    if(v) {
        memcpy(my_v, "1:v4:", 5);
        memcpy(my_v + 5, v, 4);
        have_v = 1;
    } else {
        have_v = 0;
    }

    dht_gettimeofday(&now, NULL);

    mybucket_grow_time = now.tv_sec;
    mybucket6_grow_time = now.tv_sec;
    confirm_nodes_time = now.tv_sec + random() % 3;

    search_id = random() & 0xFFFF;
    search_time = 0;

    next_blacklisted = 0;

    token_bucket_time = now.tv_sec;
    token_bucket_tokens = MAX_TOKEN_BUCKET_TOKENS;

    memset(secret, 0, sizeof(secret));
    rc = rotate_secrets();
    if(rc < 0)
        goto fail;

    dht_socket = s;
    dht_socket6 = s6;

    expire_buckets(buckets);
    expire_buckets(buckets6);

    return 1;

 fail:
    free(buckets);
    buckets = NULL;
    free(buckets6);
    buckets6 = NULL;
    return -1;
}





Add Discussion as Guest

Log in