summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-27 12:52:34 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-27 12:54:46 +0000
commit0075374f2bb3bb72ef1112471f1aacf12b6b6658 (patch)
treeadd97dd9763a59685c3c36003c5962905cf6d751
parent25a477a7e429f108cb690d0c24701272ec30195f (diff)
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`.
-rw-r--r--toxcore/DHT.c20
-rw-r--r--toxcore/DHT.h2
-rw-r--r--toxcore/LAN_discovery.api.h7
-rw-r--r--toxcore/LAN_discovery.c23
-rw-r--r--toxcore/LAN_discovery.h7
-rw-r--r--toxcore/net_crypto.c4
-rw-r--r--toxcore/onion_announce.c4
-rw-r--r--toxcore/onion_client.c10
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
695 net_ntohs(ip_port.port)); 695 net_ntohs(ip_port.port));
696 } 696 }
697 697
698 if (ip_is_lan(assoc->ip_port.ip) != 0 && ip_is_lan(ip_port.ip) == 0) { 698 if (!ip_is_lan(assoc->ip_port.ip) && ip_is_lan(ip_port.ip)) {
699 return; 699 return;
700 } 700 }
701 701
@@ -798,7 +798,7 @@ static uint8_t hardening_correct(const Hardening *h)
798 */ 798 */
799static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *public_key, Node_format *nodes_list, 799static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *public_key, Node_format *nodes_list,
800 Family sa_family, const Client_data *client_list, uint32_t client_list_length, 800 Family sa_family, const Client_data *client_list, uint32_t client_list_length,
801 uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) 801 uint32_t *num_nodes_ptr, bool is_LAN, uint8_t want_good)
802{ 802{
803 if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) { 803 if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
804 return; 804 return;
@@ -832,11 +832,11 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub
832 } 832 }
833 833
834 /* don't send LAN ips to non LAN peers */ 834 /* don't send LAN ips to non LAN peers */
835 if (ip_is_lan(ipptp->ip_port.ip) == 0 && !is_LAN) { 835 if (ip_is_lan(ipptp->ip_port.ip) && !is_LAN) {
836 continue; 836 continue;
837 } 837 }
838 838
839 if (ip_is_lan(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK 839 if (!ip_is_lan(ipptp->ip_port.ip) && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK
840 && !id_equal(public_key, client->public_key)) { 840 && !id_equal(public_key, client->public_key)) {
841 continue; 841 continue;
842 } 842 }
@@ -862,7 +862,7 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub
862 * want_good : do we want only good nodes as checked with the hardening returned or not? 862 * want_good : do we want only good nodes as checked with the hardening returned or not?
863 */ 863 */
864static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, 864static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
865 Family sa_family, uint8_t is_LAN, uint8_t want_good) 865 Family sa_family, bool is_LAN, uint8_t want_good)
866{ 866{
867 uint32_t num_nodes = 0; 867 uint32_t num_nodes = 0;
868 get_close_nodes_inner(dht->mono_time, public_key, nodes_list, sa_family, 868 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
889} 889}
890 890
891int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, 891int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
892 uint8_t is_LAN, uint8_t want_good) 892 bool is_LAN, uint8_t want_good)
893{ 893{
894 memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); 894 memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
895 return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good); 895 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
1370 const size_t node_format_size = sizeof(Node_format); 1370 const size_t node_format_size = sizeof(Node_format);
1371 1371
1372 Node_format nodes_list[MAX_SENT_NODES]; 1372 Node_format nodes_list[MAX_SENT_NODES];
1373 const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip) == 0, 1373 const uint32_t num_nodes =
1374 1); 1374 get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip), 1);
1375 1375
1376 VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); 1376 VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length);
1377 1377
@@ -2996,12 +2996,12 @@ bool dht_non_lan_connected(const DHT *dht)
2996 const Client_data *const client = &dht->close_clientlist[i]; 2996 const Client_data *const client = &dht->close_clientlist[i];
2997 2997
2998 if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) 2998 if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT)
2999 && ip_is_lan(client->assoc4.ip_port.ip) == -1) { 2999 && !ip_is_lan(client->assoc4.ip_port.ip)) {
3000 return true; 3000 return true;
3001 } 3001 }
3002 3002
3003 if (!mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT) 3003 if (!mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)
3004 && ip_is_lan(client->assoc6.ip_port.ip) == -1) { 3004 && !ip_is_lan(client->assoc6.ip_port.ip)) {
3005 return true; 3005 return true;
3006 } 3006 }
3007 } 3007 }
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_
322 * 322 *
323 */ 323 */
324int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, 324int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
325 uint8_t is_LAN, uint8_t want_good); 325 bool is_LAN, uint8_t want_good);
326 326
327 327
328/* Put up to max_num nodes in nodes from the random friends. 328/* 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);
61static bool ip_is_local(iP::this ip); 61static bool ip_is_local(iP::this ip);
62 62
63/** 63/**
64 * checks if a given IP isn't routable 64 * Checks if a given IP isn't routable.
65 * 65 *
66 * return 0 if ip is a LAN ip. 66 * @return true if ip is a LAN ip, false if it is not.
67 * return -1 if it is not.
68 */ 67 */
69static int32_t ip_is_lan(iP::this ip); 68static bool ip_is_lan(iP::this ip);
70 69
71%{ 70%{
72#endif 71#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)
300 return 0; 300 return 0;
301} 301}
302 302
303/* return 0 if ip is a LAN ip. 303bool ip_is_lan(IP ip)
304 * return -1 if it is not.
305 */
306int ip_is_lan(IP ip)
307{ 304{
308 if (ip_is_local(ip)) { 305 if (ip_is_local(ip)) {
309 return 0; 306 return true;
310 } 307 }
311 308
312 if (net_family_is_ipv4(ip.family)) { 309 if (net_family_is_ipv4(ip.family)) {
@@ -314,29 +311,29 @@ int ip_is_lan(IP ip)
314 311
315 /* 10.0.0.0 to 10.255.255.255 range. */ 312 /* 10.0.0.0 to 10.255.255.255 range. */
316 if (ip4.uint8[0] == 10) { 313 if (ip4.uint8[0] == 10) {
317 return 0; 314 return true;
318 } 315 }
319 316
320 /* 172.16.0.0 to 172.31.255.255 range. */ 317 /* 172.16.0.0 to 172.31.255.255 range. */
321 if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) { 318 if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) {
322 return 0; 319 return true;
323 } 320 }
324 321
325 /* 192.168.0.0 to 192.168.255.255 range. */ 322 /* 192.168.0.0 to 192.168.255.255 range. */
326 if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) { 323 if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) {
327 return 0; 324 return true;
328 } 325 }
329 326
330 /* 169.254.1.0 to 169.254.254.255 range. */ 327 /* 169.254.1.0 to 169.254.254.255 range. */
331 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 328 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
332 && ip4.uint8[2] != 255) { 329 && ip4.uint8[2] != 255) {
333 return 0; 330 return true;
334 } 331 }
335 332
336 /* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10) 333 /* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
337 * (shared address space to stack another layer of NAT) */ 334 * (shared address space to stack another layer of NAT) */
338 if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) { 335 if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) {
339 return 0; 336 return true;
340 } 337 }
341 } else if (net_family_is_ipv6(ip.family)) { 338 } else if (net_family_is_ipv6(ip.family)) {
342 339
@@ -344,7 +341,7 @@ int ip_is_lan(IP ip)
344 FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ 341 FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
345 if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) || 342 if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) ||
346 ((ip.ip.v6.uint8[0] == 0xFE) && ((ip.ip.v6.uint8[1] & 0xC0) == 0x80))) { 343 ((ip.ip.v6.uint8[0] == 0xFE) && ((ip.ip.v6.uint8[1] & 0xC0) == 0x80))) {
347 return 0; 344 return true;
348 } 345 }
349 346
350 /* embedded IPv4-in-IPv6 */ 347 /* embedded IPv4-in-IPv6 */
@@ -356,7 +353,7 @@ int ip_is_lan(IP ip)
356 } 353 }
357 } 354 }
358 355
359 return -1; 356 return false;
360} 357}
361 358
362static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 359static 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
366 char ip_str[IP_NTOA_LEN] = { 0 }; 363 char ip_str[IP_NTOA_LEN] = { 0 };
367 ip_ntoa(&source.ip, ip_str, sizeof(ip_str)); 364 ip_ntoa(&source.ip, ip_str, sizeof(ip_str));
368 365
369 if (ip_is_lan(source.ip) == -1) { 366 if (!ip_is_lan(source.ip)) {
370 return 1; 367 return 1;
371 } 368 }
372 369
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);
64bool ip_is_local(IP ip); 64bool ip_is_local(IP ip);
65 65
66/** 66/**
67 * checks if a given IP isn't routable 67 * Checks if a given IP isn't routable.
68 * 68 *
69 * return 0 if ip is a LAN ip. 69 * @return true if ip is a LAN ip, false if it is not.
70 * return -1 if it is not.
71 */ 70 */
72int32_t ip_is_lan(IP ip); 71bool ip_is_lan(IP ip);
73 72
74#endif 73#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
584 } 584 }
585 585
586 if (net_family_is_ipv4(ip_port.ip.family)) { 586 if (net_family_is_ipv4(ip_port.ip.family)) {
587 if (!ipport_equal(&ip_port, &conn->ip_portv4) && ip_is_lan(conn->ip_portv4.ip) != 0) { 587 if (!ipport_equal(&ip_port, &conn->ip_portv4) && !ip_is_lan(conn->ip_portv4.ip)) {
588 if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { 588 if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) {
589 return -1; 589 return -1;
590 } 590 }
@@ -634,7 +634,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id)
634 v6 = 1; 634 v6 = 1;
635 } 635 }
636 636
637 if (v4 && ip_is_lan(conn->ip_portv4.ip) == 0) { 637 if (v4 && ip_is_lan(conn->ip_portv4.ip)) {
638 return conn->ip_portv4; 638 return conn->ip_portv4;
639 } 639 }
640 640
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 *
412 412
413 /*Respond with a announce response packet*/ 413 /*Respond with a announce response packet*/
414 Node_format nodes_list[MAX_SENT_NODES]; 414 Node_format nodes_list[MAX_SENT_NODES];
415 unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, net_family_unspec, 415 unsigned int num_nodes =
416 ip_is_lan(source.ip) == 0, 1); 416 get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, net_family_unspec, ip_is_lan(source.ip), 1);
417 uint8_t nonce[CRYPTO_NONCE_SIZE]; 417 uint8_t nonce[CRYPTO_NONCE_SIZE];
418 random_nonce(nonce); 418 random_nonce(nonce);
419 419
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
806 last_pinged_index = &onion_c->friends_list[num - 1].last_pinged_index; 806 last_pinged_index = &onion_c->friends_list[num - 1].last_pinged_index;
807 } 807 }
808 808
809 unsigned int i, j; 809 const bool lan_ips_accepted = ip_is_lan(source.ip);
810 int lan_ips_accepted = (ip_is_lan(source.ip) == 0);
811
812 for (i = 0; i < num_nodes; ++i) {
813 810
811 for (uint32_t i = 0; i < num_nodes; ++i) {
814 if (!lan_ips_accepted) { 812 if (!lan_ips_accepted) {
815 if (ip_is_lan(nodes[i].ip_port.ip) == 0) { 813 if (ip_is_lan(nodes[i].ip_port.ip)) {
816 continue; 814 continue;
817 } 815 }
818 } 816 }
@@ -821,6 +819,8 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for
821 || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2 819 || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2
822 || onion_node_timed_out(&list_nodes[1], onion_c->mono_time) 820 || onion_node_timed_out(&list_nodes[1], onion_c->mono_time)
823 || id_closest(reference_id, list_nodes[1].public_key, nodes[i].public_key) == 2) { 821 || id_closest(reference_id, list_nodes[1].public_key, nodes[i].public_key) == 2) {
822 uint32_t j;
823
824 /* check if node is already in list. */ 824 /* check if node is already in list. */
825 for (j = 0; j < list_length; ++j) { 825 for (j = 0; j < list_length; ++j) {
826 if (public_key_cmp(list_nodes[j].public_key, nodes[i].public_key) == 0) { 826 if (public_key_cmp(list_nodes[j].public_key, nodes[i].public_key) == 0) {