diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 0e0ead52..937e6196 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -24,7 +24,6 @@ /*----------------------------------------------------------------------------------*/ #include "DHT.h" -#include "packets.h" #include "ping.h" /* The number of seconds for a non responsive node to become bad. */ @@ -563,7 +562,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 memcpy(&ping_id, plain, sizeof(ping_id)); sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); - // send_ping_request(dht, source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ + //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ return 0; } @@ -606,7 +605,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 uint32_t i; for (i = 0; i < num_nodes; ++i) { - send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id); + send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, nodes_list[i].client_id); returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); } @@ -713,7 +712,7 @@ static void do_DHT_friends(DHT *dht) if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, - (clientid_t *) &dht->friends_list[i].client_list[j].client_id ); + dht->friends_list[i].client_list[j].client_id ); dht->friends_list[i].client_list[j].last_pinged = temp_time; } @@ -751,7 +750,7 @@ static void do_Close(DHT *dht) if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, - (clientid_t *) &dht->close_clientlist[i].client_id ); + dht->close_clientlist[i].client_id ); dht->close_clientlist[i].last_pinged = temp_time; } @@ -775,7 +774,7 @@ static void do_Close(DHT *dht) void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) { getnodes(dht, ip_port, public_key, dht->c->self_public_key); - send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); + send_ping_request(dht->ping, dht->c, ip_port, public_key); } /* Send the given packet to node with client_id @@ -1042,7 +1041,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, /* TODO: improve port guessing algorithm */ uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); IP_Port pinging = {ip, htons(port)}; - send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); + send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); } dht->friends_list[friend_num].punching_index = i; @@ -1143,7 +1142,7 @@ static void do_toping(DHT *dht) if (dht->toping[i].ip_port.ip.i == 0) return; - send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); + send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, dht->toping[i].client_id); dht->toping[i].ip_port.ip.i = 0; } } diff --git a/toxcore/ping.c b/toxcore/ping.c index 1c2de777..384b2036 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -10,7 +10,6 @@ #include "DHT.h" #include "net_crypto.h" -#include "packets.h" #include "network.h" #include "util.h" @@ -116,84 +115,88 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl return false; } -int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) +#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING) + +int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id) { - pingreq_t pk; + uint8_t pk[DHT_PING_SIZE]; int rc; uint64_t ping_id; - if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) + if (is_pinging(ping, ipp, 0) || id_eq(client_id, c->self_public_key)) return 1; // Generate random ping_id. ping_id = add_ping(ping, ipp); - pk.packet_id = NET_PACKET_PING_REQUEST; - id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. - random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. + pk[0] = NET_PACKET_PING_REQUEST; + id_cpy(pk + 1, c->self_public_key); // Our pubkey + random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce - /* Encrypt ping_id using recipient privkey. */ - rc = encrypt_data((uint8_t *) client_id, + // Encrypt ping_id using recipient privkey + rc = encrypt_data(client_id, c->self_secret_key, - (uint8_t *) &pk.nonce, + pk + 1 + CLIENT_ID_SIZE, (uint8_t *) &ping_id, sizeof(ping_id), - (uint8_t *) &pk.ping_id); + pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); } -int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id) +int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id) { - pingres_t pk; + uint8_t pk[DHT_PING_SIZE]; int rc; - if (id_eq(client_id, (clientid_t *)c->self_public_key)) + if (id_eq(client_id, c->self_public_key)) return 1; - pk.packet_id = NET_PACKET_PING_RESPONSE; - id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. - random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. + pk[0] = NET_PACKET_PING_RESPONSE; + id_cpy(pk + 1, c->self_public_key); // Our pubkey + random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce - /* Encrypt ping_id using recipient privkey */ - rc = encrypt_data((uint8_t *) client_id, + // Encrypt ping_id using recipient privkey + rc = encrypt_data(client_id, c->self_secret_key, - (uint8_t *) &pk.nonce, + pk + 1 + CLIENT_ID_SIZE, (uint8_t *) &ping_id, sizeof(ping_id), - (uint8_t *) &pk.ping_id); + pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); } int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) { DHT *dht = object; - pingreq_t *p = (pingreq_t *) packet; int rc; uint64_t ping_id; - if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) + if (length != DHT_PING_SIZE) return 1; - /* Decrypt ping_id. */ - rc = decrypt_data((uint8_t *) &p->client_id, + if (id_eq(packet + 1, dht->c->self_public_key)) + return 1; + + // Decrypt ping_id + rc = decrypt_data(packet + 1, dht->c->self_secret_key, - (uint8_t *) &p->nonce, - (uint8_t *) &p->ping_id, + packet + 1 + CLIENT_ID_SIZE, + packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *) &ping_id); if (rc != sizeof(ping_id)) return 1; - /* Send response. */ - send_ping_response(dht->c, source, &p->client_id, ping_id); - add_toping(dht, (uint8_t *) &p->client_id, source); + // Send response + send_ping_response(dht->c, source, packet + 1, ping_id); + add_toping(dht, packet + 1, source); return 0; } @@ -201,18 +204,20 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length) { DHT *dht = object; - pingres_t *p = (pingres_t *) packet; int rc; uint64_t ping_id; - if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) + if (length != DHT_PING_SIZE) return 1; - /* Decrypt ping_id. */ - rc = decrypt_data((uint8_t *) &p->client_id, + if (id_eq(packet + 1, dht->c->self_public_key)) + return 1; + + // Decrypt ping_id + rc = decrypt_data(packet + 1, dht->c->self_secret_key, - (uint8_t *) &p->nonce, - (uint8_t *) &p->ping_id, + packet + 1 + CLIENT_ID_SIZE, + packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *) &ping_id); @@ -223,7 +228,7 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t if (!is_pinging(dht->ping, source, ping_id)) return 1; - /* Associate source ip with client_id. */ - addto_lists(dht, source, (uint8_t *) &p->client_id); + // Associate source ip with client_id + addto_lists(dht, source, packet + 1); return 0; } diff --git a/toxcore/ping.h b/toxcore/ping.h index c04ec80e..5d080e34 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -11,7 +11,7 @@ void *new_ping(void); void kill_ping(void *ping); uint64_t add_ping(void *ping, IP_Port ipp); bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id); -int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); -int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); +int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id); +int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id); int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length); int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length); diff --git a/toxcore/util.c b/toxcore/util.c index 6f346db1..40723eab 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -10,7 +10,6 @@ #include #include "DHT.h" -#include "packets.h" uint64_t now() { @@ -34,12 +33,12 @@ bool ipp_eq(IP_Port a, IP_Port b) return (a.ip.i == b.ip.i) && (a.port == b.port); } -bool id_eq(clientid_t *dest, clientid_t *src) +bool id_eq(uint8_t *dest, uint8_t *src) { - return memcmp(dest, src, sizeof(clientid_t)) == 0; + return memcmp(dest, src, CLIENT_ID_SIZE) == 0; } -void id_cpy(clientid_t *dest, clientid_t *src) +void id_cpy(uint8_t *dest, uint8_t *src) { - memcpy(dest, src, sizeof(clientid_t)); + memcpy(dest, src, CLIENT_ID_SIZE); } diff --git a/toxcore/util.h b/toxcore/util.h index 5209c2ca..90a3c8e4 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -8,5 +8,5 @@ uint64_t now(); uint64_t random_64b(); bool ipp_eq(IP_Port a, IP_Port b); -bool id_eq(clientid_t *dest, clientid_t *src); -void id_cpy(clientid_t *dest, clientid_t *src); +bool id_eq(uint8_t *dest, uint8_t *src); +void id_cpy(uint8_t *dest, uint8_t *src);