From 78d5b74dcee1208efe19dd115006034501c4380f Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 27 Feb 2018 04:44:18 +0000 Subject: [PATCH] Simplify sendpacket function, deduplicate some logic. --- toxcore/network.c | 53 ++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/toxcore/network.c b/toxcore/network.c index e9c82c39..3aaeb075 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -423,7 +423,7 @@ uint16_t net_port(const Networking_Core *net) */ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) { - if (net->family == 0) { /* Socket not initialized */ + if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */ return -1; } @@ -436,42 +436,35 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 size_t addrsize = 0; + if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) { + /* must convert to IPV4-in-IPV6 address */ + IP6 ip6; + + /* there should be a macro for this in a standards compliant + * environment, not found */ + ip6.uint32[0] = 0; + ip6.uint32[1] = 0; + ip6.uint32[2] = net_htonl(0xFFFF); + ip6.uint32[3] = ip_port.ip.ip.v4.uint32; + + ip_port.ip.family = TOX_AF_INET6; + ip_port.ip.ip.v6 = ip6; + } + if (ip_port.ip.family == TOX_AF_INET) { - if (net->family == TOX_AF_INET6) { - /* must convert to IPV4-in-IPV6 address */ - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; + struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; - addrsize = sizeof(struct sockaddr_in6); - addr6->sin6_family = AF_INET6; - addr6->sin6_port = ip_port.port; - - /* there should be a macro for this in a standards compliant - * environment, not found */ - IP6 ip6; - - ip6.uint32[0] = 0; - ip6.uint32[1] = 0; - ip6.uint32[2] = net_htonl(0xFFFF); - ip6.uint32[3] = ip_port.ip.ip.v4.uint32; - fill_addr6(ip6, &addr6->sin6_addr); - - addr6->sin6_flowinfo = 0; - addr6->sin6_scope_id = 0; - } else { - struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; - - addrsize = sizeof(struct sockaddr_in); - addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); - addr4->sin_port = ip_port.port; - } + addrsize = sizeof(struct sockaddr_in); + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); + addr4->sin_family = AF_INET; + addr4->sin_port = ip_port.port; } else if (ip_port.ip.family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_family = AF_INET6; addr6->sin6_port = ip_port.port; - fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -480,7 +473,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 return -1; } - int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); + const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); loglogdata(net->log, "O=>", data, length, ip_port, res);