From 8c41244627528ba35dc122ed166cc664fee8c4de Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 15 Sep 2013 09:31:27 -0400 Subject: [PATCH] Fixed portablity problems. struct in6_addr member names can differ per platform. --- toxcore/DHT.c | 2 +- toxcore/LAN_discovery.c | 20 ++++++++++---------- toxcore/Lossless_UDP.c | 2 +- toxcore/network.c | 12 ++++++------ toxcore/network.h | 7 ++++++- toxcore/tox.h | 7 ++++++- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 11f25880..32a1d96d 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -564,7 +564,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl if ((node_ip->family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&node_ip->ip6)) /* embedded IPv4-in-IPv6 address: return it in regular sendnodes packet */ - nodes4_list[num_nodes_ok].ip_port.ip.uint32 = node_ip->ip6.s6_addr32[3]; + nodes4_list[num_nodes_ok].ip_port.ip.uint32 = node_ip->ip6.uint32[3]; else if (node_ip->family == AF_INET) nodes4_list[num_nodes_ok].ip_port.ip.uint32 = node_ip->ip4.uint32; else /* shouldn't happen */ diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index c7afb991..db07755d 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -99,15 +99,15 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ /* FE80::*: MUST be exact, for that we would need to look over all * interfaces and check in which status they are */ - ip.ip6.s6_addr[ 0] = 0xFF; - ip.ip6.s6_addr[ 1] = 0x02; - ip.ip6.s6_addr[15] = 0x01; + ip.ip6.uint8[ 0] = 0xFF; + ip.ip6.uint8[ 1] = 0x02; + ip.ip6.uint8[15] = 0x01; } else if (family_broadcast == AF_INET) { ip.family = AF_INET6; - ip.ip6.s6_addr32[0] = 0; - ip.ip6.s6_addr32[1] = 0; - ip.ip6.s6_addr32[2] = htonl(0xFFFF); - ip.ip6.s6_addr32[3] = INADDR_BROADCAST; + ip.ip6.uint32[0] = 0; + ip.ip6.uint32[1] = 0; + ip.ip6.uint32[2] = htonl(0xFFFF); + ip.ip6.uint32[3] = INADDR_BROADCAST; } } else if (family_socket == AF_INET) { if (family_broadcast == AF_INET) { @@ -166,15 +166,15 @@ static int LAN_ip(IP ip) { /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ - if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || - ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80))) + if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) || + ((ip.ip6.uint8[0] == 0xFE) && ((ip.ip6.uint8[1] & 0xC0) == 0x80))) return 0; /* embedded IPv4-in-IPv6 */ if (IN6_IS_ADDR_V4MAPPED(&ip.ip6)) { IP ip4; ip4.family = AF_INET; - ip4.ip4.uint32 = ip.ip6.s6_addr32[3]; + ip4.ip4.uint32 = ip.ip6.uint32[3]; return LAN_ip(ip4); } } diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index f942d8e5..46b0bed1 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -100,7 +100,7 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) int k; for (k = 0; k < 16; k++) { - id ^= randtable_initget(ludp, i++, source.ip.ip6.s6_addr[k]); + id ^= randtable_initget(ludp, i++, source.ip.ip6.uint8[k]); } } diff --git a/toxcore/network.c b/toxcore/network.c index 75d968be..e32e9a4c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -120,7 +120,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = ip_port.port; - addr6->sin6_addr = ip_port.ip.ip6; + addr6->sin6_addr = ip_port.ip.ip6.in6_addr; addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -177,7 +177,7 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t } else if (addr.ss_family == AF_INET6) { struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; ip_port->ip.family = addr_in6->sin6_family; - ip_port->ip.ip6 = addr_in6->sin6_addr; + ip_port->ip.ip6.in6_addr = addr_in6->sin6_addr; ip_port->port = addr_in6->sin6_port; } else return -1; @@ -371,7 +371,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = 0; - addr6->sin6_addr = ip.ip6; + addr6->sin6_addr = ip.ip6.in6_addr; addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -672,7 +672,7 @@ int addr_parse_ip(const char *address, IP *to) if (1 == inet_pton(AF_INET6, address, &addr6)) { to->family = AF_INET6; - to->ip6 = addr6; + to->ip6.in6_addr = addr6; return 1; }; @@ -773,13 +773,13 @@ int addr_resolve(const char *address, IP *to, IP *extra) if (walker->ai_family == family) { if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - to->ip6 = addr->sin6_addr; + to->ip6.in6_addr = addr->sin6_addr; rc = 3; } } else if (!(rc & 2)) { if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - ip6 = addr->sin6_addr; + ip6.in6_addr = addr->sin6_addr; rc |= 2; } } diff --git a/toxcore/network.h b/toxcore/network.h index 7dea8c16..416d794d 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -96,7 +96,12 @@ typedef union { struct in_addr in_addr; } IP4; -typedef struct in6_addr IP6; +typedef union { + uint8_t uint8[16]; + uint16_t uint16[8]; + uint32_t uint32[4]; + struct in6_addr in6_addr; +} IP6; typedef struct { sa_family_t family; diff --git a/toxcore/tox.h b/toxcore/tox.h index a15d7ea0..89242f1f 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -63,7 +63,12 @@ typedef union { } tox_IP4; -typedef struct in6_addr tox_IP6; +typedef union { + uint8_t uint8[16]; + uint16_t uint16[8]; + uint32_t uint32[4]; + struct in6_addr in6_addr; +} tox_IP6; typedef struct { sa_family_t family;