#include #include <../dht/ks_dht.h> #include <../dht/ks_dht-int.h> #include ks_dht_storageitem_skey_t sk; ks_dht_storageitem_pkey_t pk; ks_status_t dht2_put_callback(ks_dht_t *dht, ks_dht_job_t *job) { diag("dht2_put_callback\n"); return KS_STATUS_SUCCESS; } ks_status_t dht2_get_token_callback(ks_dht_t *dht, ks_dht_job_t *job) { char buf[KS_DHT_TOKEN_SIZE * 2 + 1]; const char *v = "Hello World!"; size_t v_len = strlen(v); ks_dht_storageitem_signature_t sig; ks_dht_storageitem_t *mutable = NULL; diag("dht2_get_token_callback %s\n", ks_dht_hex(job->response_token.token, buf, KS_DHT_TOKEN_SIZE)); ks_dht_storageitem_signature_generate(&sig, &sk, NULL, 0, 1, (uint8_t *)v, v_len); // @todo check if exists ks_dht_storageitem_create_mutable(&mutable, dht->pool, &job->query_target, (uint8_t *)v, v_len, &pk, NULL, 0, 1, &sig); mutable->sk = sk; ks_dht_storageitems_insert(dht, mutable); ks_dht_put(dht, &job->raddr, dht2_put_callback, &job->response_token, 0, mutable); return KS_STATUS_SUCCESS; } int main() { //ks_size_t buflen; ks_status_t err; int mask = 0; ks_dht_t *dht1 = NULL; ks_dht_t *dht2 = NULL; 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 raddr1; //ks_sockaddr_t raddr2; //ks_sockaddr_t raddr3; ks_dht_nodeid_t target; //ks_dht_storageitem_t *immutable = NULL; //ks_dht_storageitem_t *mutable = NULL; //const char *v = "Hello World!"; //size_t v_len = strlen(v); //ks_dht_storageitem_skey_t sk; //= { { 0xe0, 0x6d, 0x31, 0x83, 0xd1, 0x41, 0x59, 0x22, 0x84, 0x33, 0xed, 0x59, 0x92, 0x21, 0xb8, 0x0b, //0xd0, 0xa5, 0xce, 0x83, 0x52, 0xe4, 0xbd, 0xf0, 0x26, 0x2f, 0x76, 0x78, 0x6e, 0xf1, 0xc7, 0x4d, //0xb7, 0xe7, 0xa9, 0xfe, 0xa2, 0xc0, 0xeb, 0x26, 0x9d, 0x61, 0xe3, 0xb3, 0x8e, 0x45, 0x0a, 0x22, //0xe7, 0x54, 0x94, 0x1a, 0xc7, 0x84, 0x79, 0xd6, 0xc5, 0x4e, 0x1f, 0xaf, 0x60, 0x37, 0x88, 0x1d } }; //ks_dht_storageitem_pkey_t pk; //= { { 0x77, 0xff, 0x84, 0x90, 0x5a, 0x91, 0x93, 0x63, 0x67, 0xc0, 0x13, 0x60, 0x80, 0x31, 0x04, 0xf9, //0x24, 0x32, 0xfc, 0xd9, 0x04, 0xa4, 0x35, 0x11, 0x87, 0x6d, 0xf5, 0xcd, 0xf3, 0xe7, 0xe5, 0x48 } }; //uint8_t sk1[KS_DHT_STORAGEITEM_SKEY_SIZE]; //uint8_t pk1[KS_DHT_STORAGEITEM_PKEY_SIZE]; //ks_dht_storageitem_signature_t sig; //char sk_buf[KS_DHT_STORAGEITEM_SKEY_SIZE * 2 + 1]; //char pk_buf[KS_DHT_STORAGEITEM_PKEY_SIZE * 2 + 1]; //const char *test1vector = "3:seqi1e1:v12:Hello World!"; //const char *test1vector = "4:salt6:foobar3:seqi1e1:v12:Hello World!"; //size_t test1vector_len = strlen(test1vector); //uint8_t test1vector_sig[KS_DHT_STORAGEITEM_SIGNATURE_SIZE]; //char test1vector_buf[KS_DHT_STORAGEITEM_SIGNATURE_SIZE * 2 + 1]; err = ks_init(); ok(!err); ks_global_set_default_logger(7); err = ks_find_local_ip(v4, sizeof(v4), &mask, AF_INET, NULL); ok(err == KS_STATUS_SUCCESS); have_v4 = !zstr_buf(v4); //err = ks_find_local_ip(v6, sizeof(v6), NULL, AF_INET6, NULL); //ok(err == KS_STATUS_SUCCESS); have_v6 = KS_FALSE;//!zstr_buf(v6); ok(have_v4 || have_v6); if (have_v4) { diag("Binding to %s on ipv4\n", v4); } if (have_v6) { diag("Binding to %s on ipv6\n", v6); } err = ks_dht_create(&dht1, NULL, NULL); ok(err == KS_STATUS_SUCCESS); err = ks_dht_create(&dht2, NULL, NULL); ok(err == KS_STATUS_SUCCESS); err = ks_dht_create(&dht3, NULL, NULL); ok(err == KS_STATUS_SUCCESS); if (have_v4) { err = ks_addr_set(&addr, v4, KS_DHT_DEFAULT_PORT, AF_INET); ok(err == KS_STATUS_SUCCESS); 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); //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) { err = ks_addr_set(&addr, v6, KS_DHT_DEFAULT_PORT, AF_INET6); ok(err == KS_STATUS_SUCCESS); err = ks_dht_bind(dht1, NULL, &addr, NULL); ok(err == KS_STATUS_SUCCESS); err = ks_addr_set(&addr, v6, KS_DHT_DEFAULT_PORT + 1, AF_INET6); ok(err == KS_STATUS_SUCCESS); 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("Ping test\n"); ks_dht_ping(dht2, &raddr1, NULL); // (QUERYING) ks_dht_pulse(dht2, 100); // Send queued ping from dht2 to dht1 (RESPONDING) ks_dht_pulse(dht1, 100); // Receive and process ping query from dht2, queue and send ping response ok(ks_dhtrt_find_node(dht1->rt_ipv4, ep2->nodeid) == NULL); // The node should be dubious, and thus not be returned as good yet ks_dht_pulse(dht2, 100); // Receive and process ping response from dht1 (PROCESSING then COMPLETING) ok(ks_dhtrt_find_node(dht2->rt_ipv4, ep1->nodeid) != NULL); // The node should be good, and thus be returned as good ks_dht_pulse(dht2, 100); // Call finish callback and purge the job (COMPLETING) diag("Pulsing for route table pings\n"); // Wait for route table pinging to catch up for (int i = 0; i < 10; ++i) { //diag("DHT 1\n"); ks_dht_pulse(dht1, 100); //diag("DHT 2\n"); ks_dht_pulse(dht2, 100); } ok(ks_dhtrt_find_node(dht1->rt_ipv4, ep2->nodeid) != NULL); // The node should be good by now, and thus be returned as good */ //diag("Get test\n"); /* ks_dht_storageitem_target_immutable((uint8_t *)v, v_len, &target); ks_dht_storageitem_create_immutable(&immutable, dht1->pool, &target, (uint8_t *)v, v_len); ks_dht_storageitems_insert(dht1, immutable); */ /* crypto_sign_keypair(pk.key, sk.key); ks_dht_storageitem_signature_generate(&sig, &sk, NULL, 0, 1, (uint8_t *)v, v_len); ks_dht_storageitem_target_mutable(&pk, NULL, 0, &target); ks_dht_storageitem_create_mutable(&mutable, dht1->pool, &target, (uint8_t *)v, v_len, &pk, NULL, 0, 1, &sig); mutable->sk = sk; ks_dht_storageitems_insert(dht1, mutable); ks_dht_get(dht2, &raddr1, dht2_get_callback, &target, NULL, 0); ks_dht_pulse(dht2, 100); // send get query ks_dht_pulse(dht1, 100); // receive get query and send get response ks_dht_pulse(dht2, 100); // receive get response ok(ks_dht_storageitems_find(dht2, &target) != NULL); // item should be verified and stored ks_dht_pulse(dht2, 100); // Call finish callback and purge the job (COMPLETING) */ diag("Put test\n"); crypto_sign_keypair(pk.key, sk.key); ks_dht_storageitem_target_mutable(&pk, NULL, 0, &target); ks_dht_get(dht2, &raddr1, dht2_get_token_callback, &target, NULL, 0); // create job ks_dht_pulse(dht2, 100); // send get query ks_dht_pulse(dht1, 100); // receive get query and send get response ks_dht_pulse(dht2, 100); // receive get response ks_dht_pulse(dht2, 100); // Call finish callback and purge the job (COMPLETING), send put query ks_dht_pulse(dht1, 100); // receive put query and send put response ks_dht_pulse(dht2, 100); // receive put response ks_dht_pulse(dht2, 100); // Call finish callback and purse the job (COMPLETING) for (int i = 0; i < 10; ++i) { //diag("DHT 1\n"); ks_dht_pulse(dht1, 100); //diag("DHT 2\n"); ks_dht_pulse(dht2, 100); } // Test bootstrap find_node from dht3 to dht1 to find dht2 nodeid //diag("Find_Node test\n"); //ks_dht_findnode(dht3, &raddr1, NULL, &ep2->nodeid); //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 //ok(ks_dhtrt_find_node(dht1->rt_ipv4, ep3->nodeid) == NULL); // The node should be dubious, and thus not be returned as good yet //ks_dht_pulse(dht3, 100); // Receive and process findnode response from dht1 //ok(ks_dhtrt_find_node(dht3->rt_ipv4, ep2->nodeid) == NULL); // The node should be dubious, and thus not be returned as good yet //diag("Pulsing for route table pings\n"); // Wait for route table pinging to catch up //for (int i = 0; i < 10; ++i) { //diag("DHT 1\n"); //ks_dht_pulse(dht1, 100); //diag("DHT 2\n"); //ks_dht_pulse(dht2, 100); //} //ok(ks_dhtrt_find_node(dht3->rt_ipv4, ep2->nodeid) != NULL); // The node should be good by now, and thus be returned as good /* Cleanup and shutdown */ diag("Cleanup\n"); ks_dht_destroy(&dht3); ks_dht_destroy(&dht2); ks_dht_destroy(&dht1); ks_shutdown(); done_testing(); }