diff options
author | dartraiden <wowemuh@gmail.com> | 2022-02-07 00:34:21 +0300 |
---|---|---|
committer | dartraiden <wowemuh@gmail.com> | 2022-02-07 00:35:37 +0300 |
commit | 223306302f39455970b67a97ba62e4a542224f7a (patch) | |
tree | 21da0622af231bea688b413d1238d15315d658ce /protocols/Tox/libtox/src/toxcore/ping.c | |
parent | 41317031f257ad6a06fa743860aff212074dc416 (diff) |
Update libtox to 0.2.15
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/ping.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxcore/ping.c | 103 |
1 files changed, 53 insertions, 50 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/ping.c b/protocols/Tox/libtox/src/toxcore/ping.c index 305cce47c4..249d4a0647 100644 --- a/protocols/Tox/libtox/src/toxcore/ping.c +++ b/protocols/Tox/libtox/src/toxcore/ping.c @@ -4,13 +4,9 @@ * Copyright © 2013 plutooo */ -/* +/** * Buffered pinging using cyclic arrays. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - #include "ping.h" #include <stdlib.h> @@ -24,10 +20,10 @@ #define PING_NUM_MAX 512 -/* Maximum newly announced nodes to ping per TIME_TO_PING seconds. */ +/** Maximum newly announced nodes to ping per TIME_TO_PING seconds. */ #define MAX_TO_PING 32 -/* Ping newly announced nodes to ping per TIME_TO_PING seconds*/ +/** Ping newly announced nodes to ping per TIME_TO_PING seconds*/ #define TIME_TO_PING 2 @@ -45,14 +41,14 @@ struct Ping { #define DHT_PING_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) #define PING_DATA_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port)) -int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) +void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key) { uint8_t pk[DHT_PING_SIZE]; int rc; uint64_t ping_id; if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { - return 1; + return; } uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; @@ -62,11 +58,12 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) // Generate random ping_id. uint8_t data[PING_DATA_SIZE]; id_copy(data, public_key); - memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, &ipp, sizeof(IP_Port)); + memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, ipp, sizeof(IP_Port)); ping_id = ping_array_add(ping->ping_array, ping->mono_time, data, sizeof(data)); if (ping_id == 0) { - return 1; + crypto_memzero(shared_key, sizeof(shared_key)); + return; } uint8_t ping_plain[PING_PLAIN_SIZE]; @@ -83,18 +80,20 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) ping_plain, sizeof(ping_plain), pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); + crypto_memzero(shared_key, sizeof(shared_key)); + if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) { - return 1; + return; } - return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); + // We never check this return value and failures in sendpacket are already logged + sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); } -static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id, - uint8_t *shared_encryption_key) +static int ping_send_response(const Ping *ping, const IP_Port *ipp, const uint8_t *public_key, + uint64_t ping_id, const uint8_t *shared_encryption_key) { - uint8_t pk[DHT_PING_SIZE]; - int rc; + uint8_t pk[DHT_PING_SIZE]; if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { return 1; @@ -109,10 +108,10 @@ static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce // Encrypt ping_id using recipient privkey - rc = encrypt_data_symmetric(shared_encryption_key, - pk + 1 + CRYPTO_PUBLIC_KEY_SIZE, - ping_plain, sizeof(ping_plain), - pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); + const int rc = encrypt_data_symmetric(shared_encryption_key, + pk + 1 + CRYPTO_PUBLIC_KEY_SIZE, + ping_plain, sizeof(ping_plain), + pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) { return 1; @@ -121,10 +120,10 @@ static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); } -static int handle_ping_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) +static int handle_ping_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, + void *userdata) { - DHT *dht = (DHT *)object; - int rc; + DHT *dht = (DHT *)object; if (length != DHT_PING_SIZE) { return 1; @@ -137,34 +136,39 @@ static int handle_ping_request(void *object, IP_Port source, const uint8_t *pack } uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; - uint8_t ping_plain[PING_PLAIN_SIZE]; + // Decrypt ping_id dht_get_shared_key_recv(dht, shared_key, packet + 1); - rc = decrypt_data_symmetric(shared_key, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, - PING_PLAIN_SIZE + CRYPTO_MAC_SIZE, - ping_plain); + const int rc = decrypt_data_symmetric(shared_key, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE, + ping_plain); if (rc != sizeof(ping_plain)) { + crypto_memzero(shared_key, sizeof(shared_key)); return 1; } if (ping_plain[0] != NET_PACKET_PING_REQUEST) { + crypto_memzero(shared_key, sizeof(shared_key)); return 1; } - uint64_t ping_id; + uint64_t ping_id; memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); // Send response ping_send_response(ping, source, packet + 1, ping_id, shared_key); ping_add(ping, packet + 1, source); + crypto_memzero(shared_key, sizeof(shared_key)); + return 0; } -static int handle_ping_response(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) +static int handle_ping_response(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, + void *userdata) { DHT *dht = (DHT *)object; int rc; @@ -192,6 +196,8 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac PING_PLAIN_SIZE + CRYPTO_MAC_SIZE, ping_plain); + crypto_memzero(shared_key, sizeof(shared_key)); + if (rc != sizeof(ping_plain)) { return 1; } @@ -215,7 +221,7 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac IP_Port ipp; memcpy(&ipp, data + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port)); - if (!ipport_equal(&ipp, &source)) { + if (!ipport_equal(&ipp, source)) { return 1; } @@ -223,27 +229,26 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac return 0; } -/* Check if public_key with ip_port is in the list. +/** Check if public_key with ip_port is in the list. * * return 1 if it is. * return 0 if it isn't. */ static int in_list(const Client_data *list, uint16_t length, const Mono_Time *mono_time, const uint8_t *public_key, - IP_Port ip_port) + const IP_Port *ip_port) { - unsigned int i; - - for (i = 0; i < length; ++i) { + for (unsigned int i = 0; i < length; ++i) { if (id_equal(list[i].public_key, public_key)) { const IPPTsPng *ipptp; - if (net_family_is_ipv4(ip_port.ip.family)) { + if (net_family_is_ipv4(ip_port->ip.family)) { ipptp = &list[i].assoc4; } else { ipptp = &list[i].assoc6; } - if (!mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT) && ipport_equal(&ipptp->ip_port, &ip_port)) { + if (!mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT) + && ipport_equal(&ipptp->ip_port, ip_port)) { return 1; } } @@ -252,7 +257,7 @@ static int in_list(const Client_data *list, uint16_t length, const Mono_Time *mo return 0; } -/* Add nodes to the to_ping list. +/** Add nodes to the to_ping list. * All nodes in this list are pinged every TIME_TO_PING seconds * and are then removed from the list. * If the list is full the nodes farthest from our public_key are replaced. @@ -262,9 +267,9 @@ static int in_list(const Client_data *list, uint16_t length, const Mono_Time *mo * return 0 if node was added. * return -1 if node was not added. */ -int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port) +int32_t ping_add(Ping *ping, const uint8_t *public_key, const IP_Port *ip_port) { - if (!ip_isset(&ip_port.ip)) { + if (!ip_isset(&ip_port->ip)) { return -1; } @@ -283,12 +288,10 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port) return -1; } - unsigned int i; - - for (i = 0; i < MAX_TO_PING; ++i) { + for (unsigned int i = 0; i < MAX_TO_PING; ++i) { if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { memcpy(ping->to_ping[i].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - ipport_copy(&ping->to_ping[i].ip_port, &ip_port); + ipport_copy(&ping->to_ping[i].ip_port, ip_port); return 0; } @@ -305,7 +308,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port) } -/* Ping all the valid nodes in the to_ping list every TIME_TO_PING seconds. +/** Ping all the valid nodes in the to_ping list every TIME_TO_PING seconds. * This function must be run at least once every TIME_TO_PING seconds. */ void ping_iterate(Ping *ping) @@ -325,11 +328,11 @@ void ping_iterate(Ping *ping) break; } - if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, ping->to_ping[i].ip_port)) { + if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, &ping->to_ping[i].ip_port)) { continue; } - ping_send_request(ping, ping->to_ping[i].ip_port, ping->to_ping[i].public_key); + ping_send_request(ping, &ping->to_ping[i].ip_port, ping->to_ping[i].public_key); ip_reset(&ping->to_ping[i].ip_port.ip); } |