FS-9775: Incorporated route table to test find_node before adding deep searching, but routetable bug is currently returning same values for all closest nodes results

This commit is contained in:
Shane Bryldt 2016-12-12 01:02:43 +00:00 committed by Mike Jerris
parent e56c388707
commit 9e9adb8e4b
7 changed files with 781 additions and 564 deletions

View File

@ -8,22 +8,30 @@ KS_BEGIN_EXTERN_C
/**
*
*/
KS_DECLARE(ks_status_t) ks_dht_utility_compact_address(ks_sockaddr_t *address,
uint8_t *buffer,
KS_DECLARE(ks_status_t) ks_dht_utility_compact_addressinfo(const ks_sockaddr_t *address,
uint8_t *buffer,
ks_size_t *buffer_length,
ks_size_t buffer_size);
KS_DECLARE(ks_status_t) ks_dht_utility_expand_addressinfo(const uint8_t *buffer,
ks_size_t *buffer_length,
ks_size_t buffer_size,
ks_sockaddr_t *address);
KS_DECLARE(ks_status_t) ks_dht_utility_compact_nodeinfo(const ks_dht_nodeid_t *nodeid,
const ks_sockaddr_t *address,
uint8_t *buffer,
ks_size_t *buffer_length,
ks_size_t buffer_size);
KS_DECLARE(ks_status_t) ks_dht_utility_expand_nodeinfo(const uint8_t *buffer,
ks_size_t *buffer_length,
ks_size_t buffer_size);
KS_DECLARE(ks_status_t) ks_dht_utility_compact_node(ks_dht_nodeid_t *nodeid,
ks_sockaddr_t *address,
uint8_t *buffer,
ks_size_t *buffer_length,
ks_size_t buffer_size);
ks_size_t buffer_size,
ks_dht_nodeid_t *nodeid,
ks_sockaddr_t *address);
/**
*
*/
KS_DECLARE(void) ks_dht_idle(ks_dht_t *dht);
KS_DECLARE(void) ks_dht_idle_expirations(ks_dht_t *dht);
KS_DECLARE(void) ks_dht_idle_send(ks_dht_t *dht);
KS_DECLARE(void) ks_dht_pulse_expirations(ks_dht_t *dht);
KS_DECLARE(void) ks_dht_pulse_send(ks_dht_t *dht);
KS_DECLARE(ks_status_t) ks_dht_send(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_send_error(ks_dht_t *dht,
@ -44,14 +52,17 @@ KS_DECLARE(ks_status_t) ks_dht_process_response(ks_dht_t *dht, ks_dht_message_t
KS_DECLARE(ks_status_t) ks_dht_process_error(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_ping(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_findnode(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_get(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_put(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_response_ping(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_findnode(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_response_findnode(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_get(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_response_get(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_query_put(ks_dht_t *dht, ks_dht_message_t *message);
KS_DECLARE(ks_status_t) ks_dht_process_response_put(ks_dht_t *dht, ks_dht_message_t *message);
/**
*
*/

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ KS_BEGIN_EXTERN_C
#define KS_DHT_DEFAULT_PORT 5309
#define KS_DHT_RECV_BUFFER_SIZE 0xFFFF
#define KS_DHT_PULSE_EXPIRATIONS 10
#define KS_DHT_NODEID_SIZE 20
@ -19,6 +20,7 @@ KS_BEGIN_EXTERN_C
#define KS_DHT_MESSAGE_ERROR_MAX_SIZE 256
#define KS_DHT_TRANSACTION_EXPIRATION_DELAY 30
#define KS_DHT_SEARCH_EXPIRATION 10
#define KS_DHT_STORAGEITEM_KEY_SIZE crypto_sign_PUBLICKEYBYTES
#define KS_DHT_STORAGEITEM_SALT_MAX_SIZE 64
@ -106,6 +108,7 @@ struct ks_dht_endpoint_s {
ks_dht_nodeid_t nodeid;
ks_sockaddr_t addr;
ks_socket_t sock;
ks_dht_node_t *node;
};
struct ks_dht_transaction_s {
@ -151,6 +154,8 @@ struct ks_dht_s {
ks_hash_t *endpoints_hash;
struct pollfd *endpoints_poll;
ks_time_t pulse_expirations;
ks_q_t *send_q;
ks_dht_message_t *send_q_unsent;
uint8_t recv_buffer[KS_DHT_RECV_BUFFER_SIZE];

View File

@ -62,11 +62,8 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_init(ks_dht_endpoint_t *endpoint, const
ks_assert(addr);
ks_assert(addr->family == AF_INET || addr->family == AF_INET6);
if (!nodeid) {
randombytes_buf(endpoint->nodeid.id, KS_DHT_NODEID_SIZE);
} else {
memcpy(endpoint->nodeid.id, nodeid->id, KS_DHT_NODEID_SIZE);
}
if (!nodeid) randombytes_buf(endpoint->nodeid.id, KS_DHT_NODEID_SIZE);
else memcpy(endpoint->nodeid.id, nodeid->id, KS_DHT_NODEID_SIZE);
endpoint->addr = *addr;
endpoint->sock = sock;
@ -81,10 +78,9 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_deinit(ks_dht_endpoint_t *endpoint)
{
ks_assert(endpoint);
if (endpoint->sock != KS_SOCK_INVALID) {
ks_socket_close(&endpoint->sock);
endpoint->sock = KS_SOCK_INVALID;
}
endpoint->node = NULL;
if (endpoint->sock != KS_SOCK_INVALID) ks_socket_close(&endpoint->sock);
endpoint->addr = (const ks_sockaddr_t){ 0 };
return KS_STATUS_SUCCESS;
}

View File

@ -59,13 +59,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_init(ks_dht_message_t *message, ks_dht_en
message->endpoint = ep;
message->raddr = *raddr;
message->data = NULL;
message->args = NULL;
message->transactionid_length = 0;
message->type[0] = '\0';
if (alloc_data) {
message->data = ben_dict();
}
if (alloc_data) message->data = ben_dict();
return KS_STATUS_SUCCESS;
}
@ -173,7 +167,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_query(ks_dht_message_t *message,
ks_assert(query);
tid = htonl(transactionid);
ben_dict_set(message->data, ben_blob("t", 1), ben_blob((uint8_t *)&tid, sizeof(uint32_t)));
ben_dict_set(message->data, ben_blob("y", 1), ben_blob("q", 1));
ben_dict_set(message->data, ben_blob("q", 1), ben_blob(query, strlen(query)));
@ -182,9 +176,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_query(ks_dht_message_t *message,
a = ben_dict();
ben_dict_set(message->data, ben_blob("a", 1), a);
if (args) {
*args = a;
}
if (args) *args = a;
return KS_STATUS_SUCCESS;
}
@ -198,20 +190,18 @@ KS_DECLARE(ks_status_t) ks_dht_message_response(ks_dht_message_t *message,
struct bencode **args)
{
struct bencode *r;
ks_assert(message);
ks_assert(transactionid);
ben_dict_set(message->data, ben_blob("t", 1), ben_blob(transactionid, transactionid_length));
ben_dict_set(message->data, ben_blob("y", 1), ben_blob("r", 1));
// @note r joins message->data and will be freed with it
r = ben_dict();
ben_dict_set(message->data, ben_blob("r", 1), r);
if (args) {
*args = r;
}
if (args) *args = r;
return KS_STATUS_SUCCESS;
}
@ -225,20 +215,18 @@ KS_DECLARE(ks_status_t) ks_dht_message_error(ks_dht_message_t *message,
struct bencode **args)
{
struct bencode *e;
ks_assert(message);
ks_assert(transactionid);
ben_dict_set(message->data, ben_blob("t", 1), ben_blob(transactionid, transactionid_length));
ben_dict_set(message->data, ben_blob("y", 1), ben_blob("e", 1));
// @note r joins message->data and will be freed with it
e = ben_list();
ben_dict_set(message->data, ben_blob("e", 1), e);
if (args) {
*args = e;
}
if (args) *args = e;
return KS_STATUS_SUCCESS;
}

View File

@ -185,9 +185,7 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_mutable(ks_dht_storageitem_t *item,
SHA1_Init(&sha);
SHA1_Update(&sha, item->pk.key, KS_DHT_STORAGEITEM_KEY_SIZE);
if (item->salt && item->salt_length > 0) {
SHA1_Update(&sha, item->salt, item->salt_length);
}
if (item->salt && item->salt_length > 0) SHA1_Update(&sha, item->salt, item->salt_length);
SHA1_Final(item->id.id, &sha);
return KS_STATUS_SUCCESS;

View File

@ -15,18 +15,22 @@ ks_status_t dht_z_callback(ks_dht_t *dht, ks_dht_message_t *message)
}
int main() {
ks_size_t buflen;
//ks_size_t buflen;
ks_status_t err;
int mask = 0;
ks_dht_t *dht1 = NULL;
ks_dht_t dht2;
ks_dht_t *dht3 = NULL;
ks_dht_endpoint_t *ep1;
ks_dht_endpoint_t *ep2;
ks_dht_endpoint_t *ep3;
ks_bool_t have_v4, have_v6;
char v4[48] = {0}, v6[48] = {0};
ks_sockaddr_t addr;
ks_sockaddr_t raddr;
ks_sockaddr_t raddr1;
//ks_sockaddr_t raddr2;
//ks_sockaddr_t raddr3;
err = ks_init();
ok(!err);
@ -61,6 +65,13 @@ int main() {
err = ks_dht_init(&dht2);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_alloc(&dht3, NULL);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_init(dht3);
ok(err == KS_STATUS_SUCCESS);
ks_dht_register_type(dht1, "z", dht_z_callback);
if (have_v4) {
@ -70,13 +81,23 @@ int main() {
err = ks_dht_bind(dht1, NULL, &addr, &ep1);
ok(err == KS_STATUS_SUCCESS);
raddr1 = addr;
err = ks_addr_set(&addr, v4, KS_DHT_DEFAULT_PORT + 1, AF_INET);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_bind(&dht2, NULL, &addr, &ep2);
ok(err == KS_STATUS_SUCCESS);
raddr = addr;
//raddr2 = addr;
err = ks_addr_set(&addr, v4, KS_DHT_DEFAULT_PORT + 2, AF_INET);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_bind(dht3, NULL, &addr, &ep3);
ok(err == KS_STATUS_SUCCESS);
//raddr3 = addr;
}
if (have_v6) {
@ -91,20 +112,26 @@ int main() {
err = ks_dht_bind(&dht2, NULL, &addr, NULL);
ok(err == KS_STATUS_SUCCESS);
err = ks_addr_set(&addr, v6, KS_DHT_DEFAULT_PORT + 2, AF_INET6);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_bind(dht3, NULL, &addr, NULL);
ok(err == KS_STATUS_SUCCESS);
}
diag("Custom type tests\n");
//diag("Custom type tests\n");
buflen = strlen(TEST_DHT1_REGISTER_TYPE_BUFFER);
memcpy(dht1->recv_buffer, TEST_DHT1_REGISTER_TYPE_BUFFER, buflen);
dht1->recv_buffer_length = buflen;
//buflen = strlen(TEST_DHT1_REGISTER_TYPE_BUFFER);
//memcpy(dht1->recv_buffer, TEST_DHT1_REGISTER_TYPE_BUFFER, buflen);
//dht1->recv_buffer_length = buflen;
err = ks_dht_process(dht1, ep1, &raddr);
ok(err == KS_STATUS_SUCCESS);
//err = ks_dht_process(dht1, ep1, &raddr);
//ok(err == KS_STATUS_SUCCESS);
ks_dht_pulse(dht1, 100);
//ks_dht_pulse(dht1, 100);
ks_dht_pulse(&dht2, 100);
//ks_dht_pulse(&dht2, 100);
//buflen = strlen(TEST_DHT1_PROCESS_QUERY_PING_BUFFER);
@ -115,20 +142,39 @@ int main() {
//ok(err == KS_STATUS_SUCCESS);
diag("Ping tests\n");
diag("Ping test\n");
ks_dht_send_ping(dht1, ep1, &raddr);
ks_dht_send_ping(&dht2, ep2, &raddr1); // Queue ping from dht2 to dht1
ks_dht_pulse(dht1, 100);
ks_dht_pulse(&dht2, 100); // Send queued ping from dht2 to dht1
ks_dht_pulse(&dht2, 100);
ks_dht_pulse(dht1, 100); // Receive and process ping query from dht2, queue and send ping response
ks_dht_pulse(dht1, 100);
ks_dht_pulse(&dht2, 100); // Receive and process ping response from dht1
// Test blind find_node from dht3 to dht1 to find dht2 nodeid
diag("Find_Node test\n");
ks_dht_send_findnode(dht3, ep3, &raddr1, &ep2->nodeid); // Queue findnode from dht3 to dht1
ks_dht_pulse(dht3, 100); // Send queued findnode from dht3 to dht1
ks_dht_pulse(dht1, 100); // Receive and process findnode query from dht3, queue and send findnode response
ks_dht_pulse(dht3, 100); // Receive and process findnode response from dht1
ok(ks_dhtrt_find_node(dht3->rt_ipv4, ep2->nodeid) != NULL);
diag("Cleanup\n");
/* Cleanup and shutdown */
err = ks_dht_deinit(dht3);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_free(&dht3);
ok(err == KS_STATUS_SUCCESS);
err = ks_dht_deinit(&dht2);
ok(err == KS_STATUS_SUCCESS);