From 0075374f2bb3bb72ef1112471f1aacf12b6b6658 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 27 Aug 2018 12:52:34 +0000 Subject: [PATCH] Make `ip_is_lan` return bool instead of 0/-1. This inverts the truthiness of the return value. Previously, 0 meant `true` and -1 meant `false`. Now, `true` (1) means `true` and `false` (0) means `false`. --- toxcore/DHT.c | 20 ++++++++++---------- toxcore/DHT.h | 2 +- toxcore/LAN_discovery.api.h | 7 +++---- toxcore/LAN_discovery.c | 23 ++++++++++------------- toxcore/LAN_discovery.h | 7 +++---- toxcore/net_crypto.c | 4 ++-- toxcore/onion_announce.c | 4 ++-- toxcore/onion_client.c | 10 +++++----- 8 files changed, 36 insertions(+), 41 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index dc21575e..68143421 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -695,7 +695,7 @@ static void update_client(const Logger *log, const Mono_Time *mono_time, int ind net_ntohs(ip_port.port)); } - if (ip_is_lan(assoc->ip_port.ip) != 0 && ip_is_lan(ip_port.ip) == 0) { + if (!ip_is_lan(assoc->ip_port.ip) && ip_is_lan(ip_port.ip)) { return; } @@ -798,7 +798,7 @@ static uint8_t hardening_correct(const Hardening *h) */ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, const Client_data *client_list, uint32_t client_list_length, - uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) + uint32_t *num_nodes_ptr, bool is_LAN, uint8_t want_good) { if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) { return; @@ -832,11 +832,11 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub } /* don't send LAN ips to non LAN peers */ - if (ip_is_lan(ipptp->ip_port.ip) == 0 && !is_LAN) { + if (ip_is_lan(ipptp->ip_port.ip) && !is_LAN) { continue; } - if (ip_is_lan(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK + if (!ip_is_lan(ipptp->ip_port.ip) && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK && !id_equal(public_key, client->public_key)) { continue; } @@ -862,7 +862,7 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub * want_good : do we want only good nodes as checked with the hardening returned or not? */ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, - Family sa_family, uint8_t is_LAN, uint8_t want_good) + Family sa_family, bool is_LAN, uint8_t want_good) { uint32_t num_nodes = 0; get_close_nodes_inner(dht->mono_time, public_key, nodes_list, sa_family, @@ -889,7 +889,7 @@ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, N } int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, - uint8_t is_LAN, uint8_t want_good) + bool is_LAN, uint8_t want_good) { memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good); @@ -1370,8 +1370,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public const size_t node_format_size = sizeof(Node_format); Node_format nodes_list[MAX_SENT_NODES]; - const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip) == 0, - 1); + const uint32_t num_nodes = + get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip), 1); VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); @@ -2996,12 +2996,12 @@ bool dht_non_lan_connected(const DHT *dht) const Client_data *const client = &dht->close_clientlist[i]; if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) - && ip_is_lan(client->assoc4.ip_port.ip) == -1) { + && !ip_is_lan(client->assoc4.ip_port.ip)) { return true; } if (!mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT) - && ip_is_lan(client->assoc6.ip_port.ip) == -1) { + && !ip_is_lan(client->assoc6.ip_port.ip)) { return true; } } diff --git a/toxcore/DHT.h b/toxcore/DHT.h index ef3fd0b0..4a7af4c7 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -322,7 +322,7 @@ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_ * */ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, - uint8_t is_LAN, uint8_t want_good); + bool is_LAN, uint8_t want_good); /* Put up to max_num nodes in nodes from the random friends. diff --git a/toxcore/LAN_discovery.api.h b/toxcore/LAN_discovery.api.h index 1ffe68c8..515552f7 100644 --- a/toxcore/LAN_discovery.api.h +++ b/toxcore/LAN_discovery.api.h @@ -61,12 +61,11 @@ static void kill(dHT::this *dht); static bool ip_is_local(iP::this ip); /** - * checks if a given IP isn't routable + * Checks if a given IP isn't routable. * - * return 0 if ip is a LAN ip. - * return -1 if it is not. + * @return true if ip is a LAN ip, false if it is not. */ -static int32_t ip_is_lan(iP::this ip); +static bool ip_is_lan(iP::this ip); %{ #endif diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 3b1d8bf8..fd5bca34 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -300,13 +300,10 @@ bool ip_is_local(IP ip) return 0; } -/* return 0 if ip is a LAN ip. - * return -1 if it is not. - */ -int ip_is_lan(IP ip) +bool ip_is_lan(IP ip) { if (ip_is_local(ip)) { - return 0; + return true; } if (net_family_is_ipv4(ip.family)) { @@ -314,29 +311,29 @@ int ip_is_lan(IP ip) /* 10.0.0.0 to 10.255.255.255 range. */ if (ip4.uint8[0] == 10) { - return 0; + return true; } /* 172.16.0.0 to 172.31.255.255 range. */ if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) { - return 0; + return true; } /* 192.168.0.0 to 192.168.255.255 range. */ if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) { - return 0; + return true; } /* 169.254.1.0 to 169.254.254.255 range. */ if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 && ip4.uint8[2] != 255) { - return 0; + return true; } /* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10) * (shared address space to stack another layer of NAT) */ if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) { - return 0; + return true; } } else if (net_family_is_ipv6(ip.family)) { @@ -344,7 +341,7 @@ int ip_is_lan(IP ip) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) || ((ip.ip.v6.uint8[0] == 0xFE) && ((ip.ip.v6.uint8[1] & 0xC0) == 0x80))) { - return 0; + return true; } /* embedded IPv4-in-IPv6 */ @@ -356,7 +353,7 @@ int ip_is_lan(IP ip) } } - return -1; + return false; } static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) @@ -366,7 +363,7 @@ static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *pack char ip_str[IP_NTOA_LEN] = { 0 }; ip_ntoa(&source.ip, ip_str, sizeof(ip_str)); - if (ip_is_lan(source.ip) == -1) { + if (!ip_is_lan(source.ip)) { return 1; } diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 10a286e0..3e563782 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -64,11 +64,10 @@ void lan_discovery_kill(DHT *dht); bool ip_is_local(IP ip); /** - * checks if a given IP isn't routable + * Checks if a given IP isn't routable. * - * return 0 if ip is a LAN ip. - * return -1 if it is not. + * @return true if ip is a LAN ip, false if it is not. */ -int32_t ip_is_lan(IP ip); +bool ip_is_lan(IP ip); #endif diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 8213ce47..76cd27e7 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -584,7 +584,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por } if (net_family_is_ipv4(ip_port.ip.family)) { - if (!ipport_equal(&ip_port, &conn->ip_portv4) && ip_is_lan(conn->ip_portv4.ip) != 0) { + if (!ipport_equal(&ip_port, &conn->ip_portv4) && !ip_is_lan(conn->ip_portv4.ip)) { if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { return -1; } @@ -634,7 +634,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) v6 = 1; } - if (v4 && ip_is_lan(conn->ip_portv4.ip) == 0) { + if (v4 && ip_is_lan(conn->ip_portv4.ip)) { return conn->ip_portv4; } diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index b7fdb5f7..f38772dd 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -412,8 +412,8 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * /*Respond with a announce response packet*/ Node_format nodes_list[MAX_SENT_NODES]; - unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, net_family_unspec, - ip_is_lan(source.ip) == 0, 1); + unsigned int num_nodes = + get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, net_family_unspec, ip_is_lan(source.ip), 1); uint8_t nonce[CRYPTO_NONCE_SIZE]; random_nonce(nonce); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index ae12f908..87b0c79c 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -806,13 +806,11 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for last_pinged_index = &onion_c->friends_list[num - 1].last_pinged_index; } - unsigned int i, j; - int lan_ips_accepted = (ip_is_lan(source.ip) == 0); - - for (i = 0; i < num_nodes; ++i) { + const bool lan_ips_accepted = ip_is_lan(source.ip); + for (uint32_t i = 0; i < num_nodes; ++i) { if (!lan_ips_accepted) { - if (ip_is_lan(nodes[i].ip_port.ip) == 0) { + if (ip_is_lan(nodes[i].ip_port.ip)) { continue; } } @@ -821,6 +819,8 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2 || onion_node_timed_out(&list_nodes[1], onion_c->mono_time) || id_closest(reference_id, list_nodes[1].public_key, nodes[i].public_key) == 2) { + uint32_t j; + /* check if node is already in list. */ for (j = 0; j < list_length; ++j) { if (public_key_cmp(list_nodes[j].public_key, nodes[i].public_key) == 0) {