From 0559904ea28c026baed4a62ff39b8280cd63e58a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 23 Apr 2014 12:29:24 -0400 Subject: [PATCH] Updated some files so that they could build with latest core. Fixed some issues. --- auto_tests/onion_test.c | 34 +++++++++++++++++----------------- other/DHT_bootstrap.c | 23 +++++++++-------------- testing/DHT_test.c | 6 ++---- toxcore/DHT.h | 4 ++++ toxcore/net_crypto.h | 3 --- toxcore/onion_announce.c | 2 +- toxcore/onion_client.c | 4 ++-- 7 files changed, 35 insertions(+), 41 deletions(-) diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index cd060b1f..b8145cc2 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -80,7 +80,7 @@ static int handle_test_3(void *object, IP_Port source, uint8_t *packet, uint32_t uint8_t plain[1 + crypto_hash_sha256_BYTES]; //print_client_id(packet, length); - int len = decrypt_data(test_3_pub_key, onion->dht->c->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, + int len = decrypt_data(test_3_pub_key, onion->dht->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES, 1 + crypto_hash_sha256_BYTES + crypto_box_MACBYTES, plain); @@ -111,7 +111,7 @@ static int handle_test_4(void *object, IP_Port source, uint8_t *packet, uint32_t if (memcmp(nonce, packet + 1, crypto_box_NONCEBYTES) != 0) return 1; - int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion->dht->c->self_secret_key, packet + 1, + int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion->dht->self_secret_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, sizeof("Install gentoo") + crypto_box_MACBYTES, plain); if (len == -1) @@ -129,8 +129,8 @@ START_TEST(test_basic) IP ip; ip_init(&ip, 1); ip.ip6.uint8[15] = 1; - Onion *onion1 = new_onion(new_DHT(new_net_crypto(new_networking(ip, 34567)))); - Onion *onion2 = new_onion(new_DHT(new_net_crypto(new_networking(ip, 34568)))); + Onion *onion1 = new_onion(new_DHT(new_networking(ip, 34567))); + Onion *onion2 = new_onion(new_DHT(new_networking(ip, 34568))); ck_assert_msg((onion1 != NULL) && (onion2 != NULL), "Onion failed initializing."); networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2); @@ -177,9 +177,9 @@ START_TEST(test_basic) uint8_t zeroes[64] = {0}; randombytes(sb_data, sizeof(sb_data)); memcpy(test_3_pub_key, nodes[3].client_id, crypto_box_PUBLICKEYBYTES); - ret = send_announce_request(onion1->net, &path, nodes[3], onion1->dht->c->self_public_key, - onion1->dht->c->self_secret_key, - zeroes, onion1->dht->c->self_public_key, onion1->dht->c->self_public_key, sb_data); + ret = send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key, + onion1->dht->self_secret_key, + zeroes, onion1->dht->self_public_key, onion1->dht->self_public_key, sb_data); ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet."); handled_test_3 = 0; @@ -193,10 +193,10 @@ START_TEST(test_basic) memcpy(onion2_a->entries[1].public_key, onion2->dht->self_public_key, crypto_box_PUBLICKEYBYTES); onion2_a->entries[1].time = unix_time(); networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1); - send_announce_request(onion1->net, &path, nodes[3], onion1->dht->c->self_public_key, onion1->dht->c->self_secret_key, - test_3_ping_id, onion1->dht->c->self_public_key, onion1->dht->c->self_public_key, sb_data); + send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key, onion1->dht->self_secret_key, + test_3_ping_id, onion1->dht->self_public_key, onion1->dht->self_public_key, sb_data); - while (memcmp(onion2_a->entries[ONION_ANNOUNCE_MAX_ENTRIES - 2].public_key, onion1->dht->c->self_public_key, + while (memcmp(onion2_a->entries[ONION_ANNOUNCE_MAX_ENTRIES - 2].public_key, onion1->dht->self_public_key, crypto_box_PUBLICKEYBYTES) != 0) { do_onion(onion1); do_onion(onion2); @@ -204,12 +204,12 @@ START_TEST(test_basic) } c_sleep(1000); - Onion *onion3 = new_onion(new_DHT(new_net_crypto(new_networking(ip, 34569)))); + Onion *onion3 = new_onion(new_DHT(new_networking(ip, 34569))); ck_assert_msg((onion3 != NULL), "Onion failed initializing."); new_nonce(nonce); - ret = send_data_request(onion3->net, &path, nodes[3].ip_port, onion1->dht->c->self_public_key, - onion1->dht->c->self_public_key, + ret = send_data_request(onion3->net, &path, nodes[3].ip_port, onion1->dht->self_public_key, + onion1->dht->self_public_key, nonce, (uint8_t *)"Install gentoo", sizeof("Install gentoo")); ck_assert_msg(ret == 0, "Failed to create/send onion data_request packet."); handled_test_4 = 0; @@ -234,10 +234,10 @@ Onions *new_onions(uint16_t port) ip_init(&ip, 1); ip.ip6.uint8[15] = 1; Onions *on = malloc(sizeof(Onions)); - DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); + DHT *dht = new_DHT(new_networking(ip, port)); on->onion = new_onion(dht); on->onion_a = new_onion_announce(dht); - on->onion_c = new_onion_client(dht); + on->onion_c = new_onion_client(new_net_crypto(dht)); if (on->onion && on->onion_a && on->onion_c) return on; @@ -298,8 +298,8 @@ START_TEST(test_announce) c_sleep(50); } - onion_addfriend(onions[7]->onion_c, onions[37]->onion->dht->c->self_public_key); - int frnum = onion_addfriend(onions[37]->onion_c, onions[7]->onion->dht->c->self_public_key); + onion_addfriend(onions[7]->onion_c, onions[37]->onion_c->c->self_public_key); + int frnum = onion_addfriend(onions[37]->onion_c, onions[7]->onion_c->c->self_public_key); int ok = -1; diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 738c2a0c..462360c3 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -75,11 +75,12 @@ void manage_keys(DHT *dht) exit(1); } - load_keys(dht->c, keys); + memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES); + memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); printf("Keys loaded successfully.\n"); } else { - new_keys(dht->c); - save_keys(dht->c, keys); + memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES); + memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES); keys_file = fopen("key", "w"); if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { @@ -113,7 +114,7 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, ipv6enabled); - DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); + DHT *dht = new_DHT(new_networking(ip, PORT)); Onion *onion = new_onion(dht); Onion_Announce *onion_a = new_onion_announce(dht); @@ -129,9 +130,6 @@ int main(int argc, char *argv[]) perror("Initialization"); manage_keys(dht); - /* We want our DHT public key to be the same as our internal one since this is a bootstrap node */ - memcpy(dht->self_public_key, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); - memcpy(dht->self_secret_key, dht->c->self_secret_key, crypto_box_SECRETKEYBYTES); printf("Public key: "); uint32_t i; @@ -151,17 +149,14 @@ int main(int argc, char *argv[]) file = fopen("PUBLIC_ID.txt", "w"); for (i = 0; i < 32; i++) { - if (dht->c->self_public_key[i] < 16) - printf("0"); - - printf("%hhX", dht->c->self_public_key[i]); - fprintf(file, "%hhX", dht->c->self_public_key[i]); + printf("%02hhX", dht->self_public_key[i]); + fprintf(file, "%02hhX", dht->self_public_key[i]); } fclose(file); printf("\n"); - printf("Port: %u\n", ntohs(dht->c->lossless_udp->net->port)); + printf("Port: %u\n", ntohs(dht->net->port)); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); @@ -198,7 +193,7 @@ int main(int argc, char *argv[]) #ifdef TCP_RELAY_ENABLED do_TCP_server(tcp_s); #endif - networking_poll(dht->c->lossless_udp->net); + networking_poll(dht->net); c_sleep(1); } diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 3f8b58bd..2636ed02 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -187,9 +187,7 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, ipv6enabled); - DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); - - new_keys(dht->c); + DHT *dht = new_DHT(new_networking(ip, PORT)); printf("OUR ID: "); uint32_t i; @@ -245,7 +243,7 @@ int main(int argc, char *argv[]) } } */ - networking_poll(dht->c->lossless_udp->net); + networking_poll(dht->net); print_clientlist(dht); print_friendlist(dht); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index c08b498c..c0731a3a 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -337,6 +337,10 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) */ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); +/* Function to handle crypto packets. + */ +void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object); + /* NAT PUNCHING FUNCTIONS */ /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index ef56b304..b2b9f4f5 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -87,9 +87,6 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) */ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); -/* Function to call when request beginning with byte is received. */ -void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); - /* Start a secure connection with other peer who has public_key and ip_port. * * return -1 if failure. diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 1e73aef9..abe72486 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -90,7 +90,7 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format de int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *public_key, uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length) { - if (DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE) + if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE) return -1; uint8_t packet[DATA_REQUEST_MIN_SIZE + length]; diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 35fb4423..ee902d99 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1007,7 +1007,7 @@ Onion_Client *new_onion_client(Net_Crypto *c) networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_announce_response, onion_c); networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c); oniondata_registerhandler(onion_c, FAKEID_DATA_ID, &handle_fakeid_announce, onion_c); - cryptopacket_registerhandler(onion_c->c, FAKEID_DATA_ID, &handle_dht_fakeid, onion_c); + cryptopacket_registerhandler(onion_c->dht, FAKEID_DATA_ID, &handle_dht_fakeid, onion_c); return onion_c; } @@ -1021,7 +1021,7 @@ void kill_onion_client(Onion_Client *onion_c) networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); oniondata_registerhandler(onion_c, FAKEID_DATA_ID, NULL, NULL); - cryptopacket_registerhandler(onion_c->c, FAKEID_DATA_ID, NULL, NULL); + cryptopacket_registerhandler(onion_c->dht, FAKEID_DATA_ID, NULL, NULL); memset(onion_c, 0, sizeof(Onion_Client)); free(onion_c); }