From 672225b710815865449e7930255468d1c085b137 Mon Sep 17 00:00:00 2001 From: Ariel Manzur Date: Thu, 20 Oct 2016 07:04:10 -0300 Subject: added windows support for ipv6, cleaned up unix code --- drivers/unix/socket_helpers.h | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 drivers/unix/socket_helpers.h (limited to 'drivers/unix/socket_helpers.h') diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h new file mode 100644 index 0000000000..622b45c500 --- /dev/null +++ b/drivers/unix/socket_helpers.h @@ -0,0 +1,66 @@ +#ifndef SOCKET_HELPERS_H +#define SOCKET_HELPERS_H + +#include + +// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this + +static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port) { + + memset(p_addr, 0, sizeof(struct sockaddr_storage)); + if (p_ip.type == IP_Address::TYPE_IPV6) { + + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + addr6->sin6_family = AF_INET6; + addr6->sin6_port = htons(p_port); + copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16); + + } else { + + struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + addr4->sin_family = AF_INET; // host byte order + addr4->sin_port = htons(p_port); // short, network byte order + addr4->sin_addr = *((struct in_addr*)&p_ip.field32[0]); + }; +}; + +static void _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List *p_accepted_hosts) { + + memset(p_addr, 0, sizeof(struct sockaddr_storage)); + if (p_address_type == IP_Address::TYPE_IPV4) { + struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + addr4->sin_family = AF_INET; + addr4->sin_port = htons(p_port); + addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list + } else { + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + + addr6->sin6_family = AF_INET6; + addr6->sin6_port = htons(p_port); + addr6->sin6_addr = in6addr_any; // TODO: use accepted hosts list + }; +}; + +static void _set_ip_addr_port(IP_Address& r_ip, int& r_port, struct sockaddr_storage* p_addr) { + + if (p_addr->ss_family == AF_INET) { + r_ip.type = IP_Address::TYPE_IPV4; + + struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + r_ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr; + + r_port = ntohs(addr4->sin_port); + + } else if (p_addr->ss_family == AF_INET6) { + + r_ip.type = IP_Address::TYPE_IPV6; + + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + copymem(&addr6->sin6_addr.s6_addr, r_ip.field8, 16); + + r_port = ntohs(addr6->sin6_port); + }; +}; + + +#endif -- cgit v1.2.3 From 80e911647c5df21c5b6a06876f1d48e21cd1f5fc Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 22 Oct 2016 07:23:07 +0200 Subject: Pass correct address size (ipv4,ipv6) to socket connect, bind, sendto The address size passed to network system calls now reflects the the actual IP type (v4 or v6). Fix Windows and OSX ipv6 sockets --- drivers/unix/socket_helpers.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'drivers/unix/socket_helpers.h') diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h index 622b45c500..4ee40e3d3d 100644 --- a/drivers/unix/socket_helpers.h +++ b/drivers/unix/socket_helpers.h @@ -5,7 +5,7 @@ // helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this -static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port) { +static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port) { memset(p_addr, 0, sizeof(struct sockaddr_storage)); if (p_ip.type == IP_Address::TYPE_IPV6) { @@ -14,6 +14,7 @@ static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_i addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(p_port); copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16); + return sizeof(sockaddr_in6); } else { @@ -21,10 +22,11 @@ static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_i addr4->sin_family = AF_INET; // host byte order addr4->sin_port = htons(p_port); // short, network byte order addr4->sin_addr = *((struct in_addr*)&p_ip.field32[0]); + return sizeof(sockaddr_in); }; }; -static void _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List *p_accepted_hosts) { +static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List *p_accepted_hosts) { memset(p_addr, 0, sizeof(struct sockaddr_storage)); if (p_address_type == IP_Address::TYPE_IPV4) { @@ -32,12 +34,14 @@ static void _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP addr4->sin_family = AF_INET; addr4->sin_port = htons(p_port); addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list + return sizeof(sockaddr_in); } else { struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(p_port); addr6->sin6_addr = in6addr_any; // TODO: use accepted hosts list + return sizeof(sockaddr_in6); }; }; -- cgit v1.2.3