FS-9775: Most of search functionality is finished, needs testing when route table is ready, still reviewing a few things related to recent lock changes
This commit is contained in:
parent
68e5321da0
commit
73e4c22255
File diff suppressed because it is too large
Load Diff
|
@ -27,7 +27,7 @@ KS_BEGIN_EXTERN_C
|
|||
#define KS_DHT_MESSAGE_QUERY_MAX_SIZE 20
|
||||
#define KS_DHT_MESSAGE_ERROR_MAX_SIZE 256
|
||||
|
||||
#define KS_DHT_TRANSACTION_EXPIRATION_DELAY 30
|
||||
#define KS_DHT_TRANSACTION_EXPIRATION 30
|
||||
#define KS_DHT_SEARCH_EXPIRATION 10
|
||||
#define KS_DHT_SEARCH_RESULTS_MAX_SIZE 8 // @todo replace with KS_DHTRT_BUCKET_SIZE
|
||||
|
||||
|
|
|
@ -17,14 +17,11 @@ KS_DECLARE(ks_status_t) ks_dht_datagram_create(ks_dht_datagram_t **datagram,
|
|||
ks_assert(endpoint);
|
||||
ks_assert(raddr);
|
||||
ks_assert(raddr->family == AF_INET || raddr->family == AF_INET6);
|
||||
|
||||
*datagram = dg = ks_pool_alloc(pool, sizeof(ks_dht_datagram_t));
|
||||
if (!dg) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
dg->pool = pool;
|
||||
|
||||
*datagram = dg = ks_pool_alloc(pool, sizeof(ks_dht_datagram_t));
|
||||
ks_assert(dg);
|
||||
|
||||
dg->pool = pool;
|
||||
dg->dht = dht;
|
||||
dg->endpoint = endpoint;
|
||||
dg->raddr = *raddr;
|
||||
|
@ -32,7 +29,7 @@ KS_DECLARE(ks_status_t) ks_dht_datagram_create(ks_dht_datagram_t **datagram,
|
|||
memcpy(dg->buffer, dht->recv_buffer, dht->recv_buffer_length);
|
||||
dg->buffer_length = dht->recv_buffer_length;
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (dg) ks_dht_datagram_destroy(&dg);
|
||||
*datagram = NULL;
|
||||
|
|
|
@ -18,19 +18,17 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_create(ks_dht_endpoint_t **endpoint,
|
|||
ks_assert(pool);
|
||||
ks_assert(addr);
|
||||
ks_assert(addr->family == AF_INET || addr->family == AF_INET6);
|
||||
|
||||
|
||||
*endpoint = ep = ks_pool_alloc(pool, sizeof(ks_dht_endpoint_t));
|
||||
if (!ep) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
ks_assert(ep);
|
||||
|
||||
ep->pool = pool;
|
||||
if (!nodeid) randombytes_buf(ep->nodeid.id, KS_DHT_NODEID_SIZE);
|
||||
else memcpy(ep->nodeid.id, nodeid->id, KS_DHT_NODEID_SIZE);
|
||||
ep->addr = *addr;
|
||||
ep->sock = sock;
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (ep) ks_dht_endpoint_destroy(&ep);
|
||||
*endpoint = NULL;
|
||||
|
@ -50,9 +48,6 @@ KS_DECLARE(void) ks_dht_endpoint_destroy(ks_dht_endpoint_t **endpoint)
|
|||
|
||||
ep = *endpoint;
|
||||
|
||||
if (ep->node) {
|
||||
// @todo release the node?
|
||||
}
|
||||
if (ep->sock != KS_SOCK_INVALID) ks_socket_close(&ep->sock);
|
||||
ks_pool_free(ep->pool, ep);
|
||||
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
#include "ks_dht.h"
|
||||
#include "ks_dht-int.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_create(ks_dht_message_t **message,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_endpoint_t *endpoint,
|
||||
|
@ -17,17 +14,17 @@ KS_DECLARE(ks_status_t) ks_dht_message_create(ks_dht_message_t **message,
|
|||
ks_assert(pool);
|
||||
|
||||
*message = m = ks_pool_alloc(pool, sizeof(ks_dht_message_t));
|
||||
if (!m) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
m->pool = pool;
|
||||
ks_assert(m);
|
||||
|
||||
m->pool = pool;
|
||||
m->endpoint = endpoint;
|
||||
m->raddr = *raddr;
|
||||
if (alloc_data) m->data = ben_dict();
|
||||
if (alloc_data) {
|
||||
m->data = ben_dict();
|
||||
ks_assert(m->data);
|
||||
}
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (m) ks_dht_message_destroy(&m);
|
||||
*message = NULL;
|
||||
|
@ -35,9 +32,6 @@ KS_DECLARE(ks_status_t) ks_dht_message_create(ks_dht_message_t **message,
|
|||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_message_destroy(ks_dht_message_t **message)
|
||||
{
|
||||
ks_dht_message_t *m;
|
||||
|
@ -57,9 +51,6 @@ KS_DECLARE(void) ks_dht_message_destroy(ks_dht_message_t **message)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_parse(ks_dht_message_t *message, const uint8_t *buffer, ks_size_t buffer_length)
|
||||
{
|
||||
struct bencode *t;
|
||||
|
@ -121,9 +112,6 @@ KS_DECLARE(ks_status_t) ks_dht_message_parse(ks_dht_message_t *message, const ui
|
|||
return KS_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_query(ks_dht_message_t *message,
|
||||
uint32_t transactionid,
|
||||
const char *query,
|
||||
|
@ -143,6 +131,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_query(ks_dht_message_t *message,
|
|||
|
||||
// @note r joins message->data and will be freed with it
|
||||
a = ben_dict();
|
||||
ks_assert(a);
|
||||
ben_dict_set(message->data, ben_blob("a", 1), a);
|
||||
|
||||
if (args) *args = a;
|
||||
|
@ -150,9 +139,6 @@ KS_DECLARE(ks_status_t) ks_dht_message_query(ks_dht_message_t *message,
|
|||
return KS_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_response(ks_dht_message_t *message,
|
||||
uint8_t *transactionid,
|
||||
ks_size_t transactionid_length,
|
||||
|
@ -168,6 +154,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_response(ks_dht_message_t *message,
|
|||
|
||||
// @note r joins message->data and will be freed with it
|
||||
r = ben_dict();
|
||||
ks_assert(r);
|
||||
ben_dict_set(message->data, ben_blob("r", 1), r);
|
||||
|
||||
if (args) *args = r;
|
||||
|
@ -193,6 +180,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_error(ks_dht_message_t *message,
|
|||
|
||||
// @note r joins message->data and will be freed with it
|
||||
e = ben_list();
|
||||
ks_assert(e);
|
||||
ben_dict_set(message->data, ben_blob("e", 1), e);
|
||||
|
||||
if (args) *args = e;
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
#include "ks_dht-int.h"
|
||||
#include "sodium.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_search_create(ks_dht_search_t **search, ks_pool_t *pool, const ks_dht_nodeid_t *target)
|
||||
{
|
||||
ks_dht_search_t *s;
|
||||
|
@ -15,32 +12,27 @@ KS_DECLARE(ks_status_t) ks_dht_search_create(ks_dht_search_t **search, ks_pool_t
|
|||
ks_assert(target);
|
||||
|
||||
*search = s = ks_pool_alloc(pool, sizeof(ks_dht_search_t));
|
||||
if (!s) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
ks_assert(s);
|
||||
|
||||
s->pool = pool;
|
||||
|
||||
if ((ret = ks_mutex_create(&s->mutex, KS_MUTEX_FLAG_DEFAULT, s->pool)) != KS_STATUS_SUCCESS) goto done;
|
||||
ks_mutex_create(&s->mutex, KS_MUTEX_FLAG_DEFAULT, s->pool);
|
||||
ks_assert(s->mutex);
|
||||
|
||||
memcpy(s->target.id, target->id, KS_DHT_NODEID_SIZE);
|
||||
|
||||
if ((ret = ks_hash_create(&s->pending,
|
||||
KS_HASH_MODE_ARBITRARY,
|
||||
KS_HASH_FLAG_RWLOCK,
|
||||
s->pool)) != KS_STATUS_SUCCESS) goto done;
|
||||
ks_hash_create(&s->pending, KS_HASH_MODE_ARBITRARY, KS_HASH_FLAG_RWLOCK, s->pool);
|
||||
ks_assert(s->pending);
|
||||
ks_hash_set_keysize(s->pending, KS_DHT_NODEID_SIZE);
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (s) ks_dht_search_destroy(&s);
|
||||
*search = NULL;
|
||||
}
|
||||
return KS_STATUS_SUCCESS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_search_destroy(ks_dht_search_t **search)
|
||||
{
|
||||
ks_dht_search_t *s;
|
||||
|
@ -83,7 +75,7 @@ KS_DECLARE(ks_status_t) ks_dht_search_callback_add(ks_dht_search_t *search, ks_d
|
|||
search->callbacks = (ks_dht_search_callback_t *)ks_pool_resize(search->pool,
|
||||
(void *)search->callbacks,
|
||||
sizeof(ks_dht_search_callback_t) * search->callbacks_size);
|
||||
if (!search->callbacks) return KS_STATUS_NO_MEM;
|
||||
ks_assert(search->callbacks);
|
||||
search->callbacks[index] = callback;
|
||||
ks_mutex_unlock(search->mutex);
|
||||
}
|
||||
|
@ -97,19 +89,16 @@ KS_DECLARE(ks_status_t) ks_dht_search_pending_create(ks_dht_search_pending_t **p
|
|||
|
||||
ks_assert(pending);
|
||||
ks_assert(pool);
|
||||
|
||||
*pending = p = ks_pool_alloc(pool, sizeof(ks_dht_search_pending_t));
|
||||
if (!p) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
p->pool = pool;
|
||||
|
||||
*pending = p = ks_pool_alloc(pool, sizeof(ks_dht_search_pending_t));
|
||||
ks_assert(p);
|
||||
|
||||
p->pool = pool;
|
||||
p->nodeid = *nodeid;
|
||||
p->expiration = ks_time_now_sec() + KS_DHT_SEARCH_EXPIRATION;
|
||||
p->expiration = ks_time_now() + (KS_DHT_SEARCH_EXPIRATION * 1000);
|
||||
p->finished = KS_FALSE;
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (p) ks_dht_search_pending_destroy(&p);
|
||||
*pending = NULL;
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
#include "ks_dht-int.h"
|
||||
#include "sodium.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_create_immutable(ks_dht_storageitem_t **item, ks_pool_t *pool, struct bencode *v)
|
||||
{
|
||||
ks_dht_storageitem_t *si;
|
||||
|
@ -19,27 +16,21 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_create_immutable(ks_dht_storageitem_t
|
|||
ks_assert(SHA_DIGEST_LENGTH == KS_DHT_NODEID_SIZE);
|
||||
|
||||
*item = si = ks_pool_alloc(pool, sizeof(ks_dht_storageitem_t));
|
||||
if (!si) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
si->pool = pool;
|
||||
ks_assert(si);
|
||||
|
||||
si->pool = pool;
|
||||
si->mutable = KS_FALSE;
|
||||
|
||||
si->v = ben_clone(v);
|
||||
if (!si->v) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
ks_assert(si->v);
|
||||
|
||||
enc = ben_encode(&enc_len, si->v);
|
||||
ks_assert(enc);
|
||||
SHA1_Init(&sha);
|
||||
SHA1_Update(&sha, enc, enc_len);
|
||||
SHA1_Final(si->id.id, &sha);
|
||||
free(enc);
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (si) ks_dht_storageitem_destroy(&si);
|
||||
*item = NULL;
|
||||
|
@ -70,15 +61,12 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_create_mutable(ks_dht_storageitem_t *
|
|||
ks_assert(signature);
|
||||
|
||||
*item = si = ks_pool_alloc(pool, sizeof(ks_dht_storageitem_t));
|
||||
if (!si) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
ks_assert(si);
|
||||
|
||||
si->pool = pool;
|
||||
|
||||
si->v = ben_clone(v);
|
||||
|
||||
si->mutable = KS_TRUE;
|
||||
si->v = ben_clone(v);
|
||||
ks_assert(si->v);
|
||||
|
||||
memcpy(si->pk.key, k->key, KS_DHT_STORAGEITEM_KEY_SIZE);
|
||||
if (salt && salt_length > 0) {
|
||||
|
@ -93,7 +81,7 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_create_mutable(ks_dht_storageitem_t *
|
|||
if (si->salt && si->salt_length > 0) SHA1_Update(&sha, si->salt, si->salt_length);
|
||||
SHA1_Final(si->id.id, &sha);
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (si) ks_dht_storageitem_destroy(&si);
|
||||
*item = NULL;
|
||||
|
|
|
@ -15,18 +15,15 @@ KS_DECLARE(ks_status_t) ks_dht_transaction_create(ks_dht_transaction_t **transac
|
|||
ks_assert(raddr);
|
||||
|
||||
*transaction = t = ks_pool_alloc(pool, sizeof(ks_dht_transaction_t));
|
||||
if (!t) {
|
||||
ret = KS_STATUS_NO_MEM;
|
||||
goto done;
|
||||
}
|
||||
t->pool = pool;
|
||||
ks_assert(t);
|
||||
|
||||
t->pool = pool;
|
||||
t->raddr = *raddr;
|
||||
t->transactionid = transactionid;
|
||||
t->callback = callback;
|
||||
t->expiration = ks_time_now_sec() + KS_DHT_TRANSACTION_EXPIRATION_DELAY;
|
||||
t->expiration = ks_time_now() + (KS_DHT_TRANSACTION_EXPIRATION * 1000);
|
||||
|
||||
done:
|
||||
// done:
|
||||
if (ret != KS_STATUS_SUCCESS) {
|
||||
if (t) ks_dht_transaction_destroy(&t);
|
||||
*transaction = NULL;
|
||||
|
|
Loading…
Reference in New Issue