summaryrefslogtreecommitdiff
path: root/toxcore/onion.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-27 01:07:46 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-05-20 19:35:28 +0000
commit21675ce0d2581597b0e0a727ab4cf6cfb796a037 (patch)
treee1ac4ca63c5cc2b2dc78fcd5b20a8202eab98d9d /toxcore/onion.c
parent4f6ab0708c85f3e3da7726f6caecc381c6d21370 (diff)
Finish @Diadlo's network Family abstraction.
The Family stuff in toxcore is a big mess. I'm sure I saw a bunch of bugs on the way, but I'm not verifying that code now, so the bugs stay.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r--toxcore/onion.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c
index 476f76e9..5bbc7f75 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -54,9 +54,9 @@ static void change_symmetric_key(Onion *onion)
54/* packing and unpacking functions */ 54/* packing and unpacking functions */
55static void ip_pack(uint8_t *data, IP source) 55static void ip_pack(uint8_t *data, IP source)
56{ 56{
57 data[0] = source.family; 57 data[0] = source.family.value;
58 58
59 if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) { 59 if (net_family_is_ipv4(source.family) || net_family_is_tox_tcp_ipv4(source.family)) {
60 memset(data + 1, 0, SIZE_IP6); 60 memset(data + 1, 0, SIZE_IP6);
61 memcpy(data + 1, source.ip.v4.uint8, SIZE_IP4); 61 memcpy(data + 1, source.ip.v4.uint8, SIZE_IP4);
62 } else { 62 } else {
@@ -71,17 +71,18 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo
71 return -1; 71 return -1;
72 } 72 }
73 73
74 target->family = data[0]; 74 // TODO(iphydf): Validate input.
75 target->family.value = data[0];
75 76
76 if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) { 77 if (net_family_is_ipv4(target->family) || net_family_is_tox_tcp_ipv4(target->family)) {
77 memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4); 78 memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4);
78 } else { 79 } else {
79 memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6); 80 memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6);
80 } 81 }
81 82
82 bool valid = disable_family_check || 83 bool valid = disable_family_check ||
83 target->family == TOX_AF_INET || 84 net_family_is_ipv4(target->family) ||
84 target->family == TOX_AF_INET6; 85 net_family_is_ipv6(target->family);
85 86
86 return valid ? 0 : -1; 87 return valid ? 0 : -1;
87} 88}
@@ -642,8 +643,8 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
642 uint16_t data_len = length - (1 + RETURN_1); 643 uint16_t data_len = length - (1 + RETURN_1);
643 644
644 if (onion->recv_1_function && 645 if (onion->recv_1_function &&
645 send_to.ip.family != TOX_AF_INET && 646 !net_family_is_ipv4(send_to.ip.family) &&
646 send_to.ip.family != TOX_AF_INET6) { 647 !net_family_is_ipv6(send_to.ip.family)) {
647 return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len); 648 return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len);
648 } 649 }
649 650