summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c89
1 files changed, 44 insertions, 45 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 31d2ca0a..6134f59b 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -385,20 +385,17 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke
385/* Return packet size of packed node with ip_family on success. 385/* Return packet size of packed node with ip_family on success.
386 * Return -1 on failure. 386 * Return -1 on failure.
387 */ 387 */
388int packed_node_size(uint8_t ip_family) 388int packed_node_size(Family ip_family)
389{ 389{
390 switch (ip_family) { 390 if (net_family_is_ipv4(ip_family) || net_family_is_tcp_ipv4(ip_family)) {
391 case TOX_AF_INET: 391 return PACKED_NODE_SIZE_IP4;
392 case TCP_INET: 392 }
393 return PACKED_NODE_SIZE_IP4;
394
395 case TOX_AF_INET6:
396 case TCP_INET6:
397 return PACKED_NODE_SIZE_IP6;
398 393
399 default: 394 if (net_family_is_ipv6(ip_family) || net_family_is_tcp_ipv6(ip_family)) {
400 return -1; 395 return PACKED_NODE_SIZE_IP6;
401 } 396 }
397
398 return -1;
402} 399}
403 400
404 401
@@ -416,17 +413,17 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
416 bool is_ipv4; 413 bool is_ipv4;
417 uint8_t net_family; 414 uint8_t net_family;
418 415
419 if (ip_port->ip.family == TOX_AF_INET) { 416 if (net_family_is_ipv4(ip_port->ip.family)) {
420 // TODO(irungentoo): use functions to convert endianness 417 // TODO(irungentoo): use functions to convert endianness
421 is_ipv4 = true; 418 is_ipv4 = true;
422 net_family = TOX_AF_INET; 419 net_family = TOX_AF_INET;
423 } else if (ip_port->ip.family == TCP_INET) { 420 } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) {
424 is_ipv4 = true; 421 is_ipv4 = true;
425 net_family = TOX_TCP_INET; 422 net_family = TOX_TCP_INET;
426 } else if (ip_port->ip.family == TOX_AF_INET6) { 423 } else if (net_family_is_ipv6(ip_port->ip.family)) {
427 is_ipv4 = false; 424 is_ipv4 = false;
428 net_family = TOX_AF_INET6; 425 net_family = TOX_AF_INET6;
429 } else if (ip_port->ip.family == TCP_INET6) { 426 } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) {
430 is_ipv4 = false; 427 is_ipv4 = false;
431 net_family = TOX_TCP_INET6; 428 net_family = TOX_TCP_INET6;
432 } else { 429 } else {
@@ -492,28 +489,28 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8
492 } 489 }
493 490
494 bool is_ipv4; 491 bool is_ipv4;
495 uint8_t host_family; 492 Family host_family;
496 493
497 if (data[0] == TOX_AF_INET) { 494 if (data[0] == TOX_AF_INET) {
498 is_ipv4 = true; 495 is_ipv4 = true;
499 host_family = TOX_AF_INET; 496 host_family = net_family_ipv4;
500 } else if (data[0] == TOX_TCP_INET) { 497 } else if (data[0] == TOX_TCP_INET) {
501 if (!tcp_enabled) { 498 if (!tcp_enabled) {
502 return -1; 499 return -1;
503 } 500 }
504 501
505 is_ipv4 = true; 502 is_ipv4 = true;
506 host_family = TCP_INET; 503 host_family = net_family_tcp_ipv4;
507 } else if (data[0] == TOX_AF_INET6) { 504 } else if (data[0] == TOX_AF_INET6) {
508 is_ipv4 = false; 505 is_ipv4 = false;
509 host_family = TOX_AF_INET6; 506 host_family = net_family_ipv6;
510 } else if (data[0] == TOX_TCP_INET6) { 507 } else if (data[0] == TOX_TCP_INET6) {
511 if (!tcp_enabled) { 508 if (!tcp_enabled) {
512 return -1; 509 return -1;
513 } 510 }
514 511
515 is_ipv4 = false; 512 is_ipv4 = false;
516 host_family = TCP_INET6; 513 host_family = net_family_tcp_ipv6;
517 } else { 514 } else {
518 return -1; 515 return -1;
519 } 516 }
@@ -650,8 +647,8 @@ static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const
650static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, const IP_Port *ip_port) 647static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, const IP_Port *ip_port)
651{ 648{
652 for (uint32_t i = 0; i < size; ++i) { 649 for (uint32_t i = 0; i < size; ++i) {
653 if ((ip_port->ip.family == TOX_AF_INET && ipport_equal(&array[i].assoc4.ip_port, ip_port)) || 650 if ((net_family_is_ipv4(ip_port->ip.family) && ipport_equal(&array[i].assoc4.ip_port, ip_port)) ||
654 (ip_port->ip.family == TOX_AF_INET6 && ipport_equal(&array[i].assoc6.ip_port, ip_port))) { 651 (net_family_is_ipv6(ip_port->ip.family) && ipport_equal(&array[i].assoc6.ip_port, ip_port))) {
655 return i; 652 return i;
656 } 653 }
657 } 654 }
@@ -666,10 +663,10 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i
666 IPPTsPng *assoc; 663 IPPTsPng *assoc;
667 int ip_version; 664 int ip_version;
668 665
669 if (ip_port.ip.family == TOX_AF_INET) { 666 if (net_family_is_ipv4(ip_port.ip.family)) {
670 assoc = &client->assoc4; 667 assoc = &client->assoc4;
671 ip_version = 4; 668 ip_version = 4;
672 } else if (ip_port.ip.family == TOX_AF_INET6) { 669 } else if (net_family_is_ipv6(ip_port.ip.family)) {
673 assoc = &client->assoc6; 670 assoc = &client->assoc6;
674 ip_version = 6; 671 ip_version = 6;
675 } else { 672 } else {
@@ -727,7 +724,7 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le
727 IPPTsPng *assoc; 724 IPPTsPng *assoc;
728 int ip_version; 725 int ip_version;
729 726
730 if (ip_port.ip.family == TOX_AF_INET) { 727 if (net_family_is_ipv4(ip_port.ip.family)) {
731 assoc = &list[index].assoc4; 728 assoc = &list[index].assoc4;
732 ip_version = 4; 729 ip_version = 4;
733 } else { 730 } else {
@@ -791,7 +788,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
791 Family sa_family, const Client_data *client_list, uint32_t client_list_length, 788 Family sa_family, const Client_data *client_list, uint32_t client_list_length,
792 uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) 789 uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good)
793{ 790{
794 if ((sa_family != TOX_AF_INET) && (sa_family != TOX_AF_INET6) && (sa_family != 0)) { 791 if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
795 return; 792 return;
796 } 793 }
797 794
@@ -807,9 +804,9 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
807 804
808 const IPPTsPng *ipptp = nullptr; 805 const IPPTsPng *ipptp = nullptr;
809 806
810 if (sa_family == TOX_AF_INET) { 807 if (net_family_is_ipv4(sa_family)) {
811 ipptp = &client->assoc4; 808 ipptp = &client->assoc4;
812 } else if (sa_family == TOX_AF_INET6) { 809 } else if (net_family_is_ipv6(sa_family)) {
813 ipptp = &client->assoc6; 810 ipptp = &client->assoc6;
814 } else if (client->assoc4.timestamp >= client->assoc6.timestamp) { 811 } else if (client->assoc4.timestamp >= client->assoc6.timestamp) {
815 ipptp = &client->assoc4; 812 ipptp = &client->assoc4;
@@ -978,7 +975,7 @@ static void update_client_with_reset(Client_data *client, const IP_Port *ip_port
978 IPPTsPng *ipptp_write = nullptr; 975 IPPTsPng *ipptp_write = nullptr;
979 IPPTsPng *ipptp_clear = nullptr; 976 IPPTsPng *ipptp_clear = nullptr;
980 977
981 if (ip_port->ip.family == TOX_AF_INET) { 978 if (net_family_is_ipv4(ip_port->ip.family)) {
982 ipptp_write = &client->assoc4; 979 ipptp_write = &client->assoc4;
983 ipptp_clear = &client->assoc6; 980 ipptp_clear = &client->assoc6;
984 } else { 981 } else {
@@ -1016,7 +1013,7 @@ static bool replace_all(Client_data *list,
1016 IP_Port ip_port, 1013 IP_Port ip_port,
1017 const uint8_t *comp_public_key) 1014 const uint8_t *comp_public_key)
1018{ 1015{
1019 if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { 1016 if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) {
1020 return false; 1017 return false;
1021 } 1018 }
1022 1019
@@ -1087,7 +1084,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li
1087 return 0; 1084 return 0;
1088 } 1085 }
1089 1086
1090 const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET 1087 const IPPTsPng *assoc = net_family_is_ipv4(ip_port.ip.family)
1091 ? &list[index].assoc4 1088 ? &list[index].assoc4
1092 : &list[index].assoc6; 1089 : &list[index].assoc6;
1093 1090
@@ -1180,8 +1177,8 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
1180 uint32_t used = 0; 1177 uint32_t used = 0;
1181 1178
1182 /* convert IPv4-in-IPv6 to IPv4 */ 1179 /* convert IPv4-in-IPv6 to IPv4 */
1183 if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { 1180 if (net_family_is_ipv6(ip_port.ip.family) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) {
1184 ip_port.ip.family = TOX_AF_INET; 1181 ip_port.ip.family = net_family_ipv4;
1185 ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; 1182 ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3];
1186 } 1183 }
1187 1184
@@ -1241,9 +1238,9 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port,
1241 Client_data *const data = &array[index]; 1238 Client_data *const data = &array[index];
1242 IPPTsPng *assoc; 1239 IPPTsPng *assoc;
1243 1240
1244 if (ip_port.ip.family == TOX_AF_INET) { 1241 if (net_family_is_ipv4(ip_port.ip.family)) {
1245 assoc = &data->assoc4; 1242 assoc = &data->assoc4;
1246 } else if (ip_port.ip.family == TOX_AF_INET6) { 1243 } else if (net_family_is_ipv6(ip_port.ip.family)) {
1247 assoc = &data->assoc6; 1244 assoc = &data->assoc6;
1248 } else { 1245 } else {
1249 return true; 1246 return true;
@@ -1260,8 +1257,8 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port,
1260static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) 1257static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key)
1261{ 1258{
1262 /* convert IPv4-in-IPv6 to IPv4 */ 1259 /* convert IPv4-in-IPv6 to IPv4 */
1263 if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { 1260 if (net_family_is_ipv6(ip_port.ip.family) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) {
1264 ip_port.ip.family = TOX_AF_INET; 1261 ip_port.ip.family = net_family_ipv4;
1265 ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; 1262 ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3];
1266 } 1263 }
1267 1264
@@ -1346,7 +1343,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1346 const size_t node_format_size = sizeof(Node_format); 1343 const size_t node_format_size = sizeof(Node_format);
1347 1344
1348 Node_format nodes_list[MAX_SENT_NODES]; 1345 Node_format nodes_list[MAX_SENT_NODES];
1349 const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); 1346 const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip) == 0,
1347 1);
1350 1348
1351 VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); 1349 VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length);
1352 1350
@@ -1591,7 +1589,8 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
1591 *lock_count = lock_num + 1; 1589 *lock_count = lock_num + 1;
1592 } 1590 }
1593 1591
1594 dht_friend->num_to_bootstrap = get_close_nodes(dht, dht_friend->public_key, dht_friend->to_bootstrap, 0, 1, 0); 1592 dht_friend->num_to_bootstrap = get_close_nodes(dht, dht_friend->public_key, dht_friend->to_bootstrap, net_family_unspec,
1593 1, 0);
1595 1594
1596 return 0; 1595 return 0;
1597} 1596}
@@ -1823,7 +1822,7 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable
1823 1822
1824 if (ipv6enabled) { 1823 if (ipv6enabled) {
1825 /* setup for getting BOTH: an IPv6 AND an IPv4 address */ 1824 /* setup for getting BOTH: an IPv6 AND an IPv4 address */
1826 ip_port_v64.ip.family = TOX_AF_UNSPEC; 1825 ip_port_v64.ip.family = net_family_unspec;
1827 ip_reset(&ip_port_v4.ip); 1826 ip_reset(&ip_port_v4.ip);
1828 ip_extra = &ip_port_v4.ip; 1827 ip_extra = &ip_port_v4.ip;
1829 } 1828 }
@@ -2350,11 +2349,11 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Fam
2350 continue; 2349 continue;
2351 } 2350 }
2352 2351
2353 if (sa_family == TOX_AF_INET) { 2352 if (net_family_is_ipv4(sa_family)) {
2354 return &dht->close_clientlist[i].assoc4; 2353 return &dht->close_clientlist[i].assoc4;
2355 } 2354 }
2356 2355
2357 if (sa_family == TOX_AF_INET6) { 2356 if (net_family_is_ipv6(sa_family)) {
2358 return &dht->close_clientlist[i].assoc6; 2357 return &dht->close_clientlist[i].assoc6;
2359 } 2358 }
2360 } 2359 }
@@ -2579,10 +2578,10 @@ static void do_hardening(DHT *dht)
2579 2578
2580 if (i % 2 == 0) { 2579 if (i % 2 == 0) {
2581 cur_iptspng = &dht->close_clientlist[i / 2].assoc4; 2580 cur_iptspng = &dht->close_clientlist[i / 2].assoc4;
2582 sa_family = TOX_AF_INET; 2581 sa_family = net_family_ipv4;
2583 } else { 2582 } else {
2584 cur_iptspng = &dht->close_clientlist[i / 2].assoc6; 2583 cur_iptspng = &dht->close_clientlist[i / 2].assoc6;
2585 sa_family = TOX_AF_INET6; 2584 sa_family = net_family_ipv6;
2586 } 2585 }
2587 2586
2588 if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) { 2587 if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) {
@@ -2794,7 +2793,7 @@ uint32_t DHT_size(const DHT *dht)
2794 const uint32_t size32 = sizeof(uint32_t); 2793 const uint32_t size32 = sizeof(uint32_t);
2795 const uint32_t sizesubhead = size32 * 2; 2794 const uint32_t sizesubhead = size32 * 2;
2796 2795
2797 return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * numv6); 2796 return size32 + sizesubhead + packed_node_size(net_family_ipv4) * numv4 + packed_node_size(net_family_ipv6) * numv6;
2798} 2797}
2799 2798
2800static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 2799static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type)