From 887a897c02144f2d01896d3112bdae5ce7d6df5c Mon Sep 17 00:00:00 2001 From: Ariel Manzur Date: Tue, 18 Oct 2016 18:53:18 -0300 Subject: adding ipv6 --- drivers/unix/tcp_server_posix.cpp | 52 +++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'drivers/unix/tcp_server_posix.cpp') diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp index c67bb51334..03312a7538 100644 --- a/drivers/unix/tcp_server_posix.cpp +++ b/drivers/unix/tcp_server_posix.cpp @@ -55,6 +55,7 @@ #include #include #include + TCP_Server* TCPServerPosix::_create() { return memnew(TCPServerPosix); @@ -65,10 +66,11 @@ void TCPServerPosix::make_default() { TCP_Server::_create = TCPServerPosix::_create; }; -Error TCPServerPosix::listen(uint16_t p_port,const List *p_accepted_hosts) { +Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const List *p_accepted_hosts) { int sockfd; - sockfd = socket(AF_INET, SOCK_STREAM, 0); + int family = p_type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET; + sockfd = socket(family, SOCK_STREAM, 0); ERR_FAIL_COND_V(sockfd == -1, FAILED); #ifndef NO_FCNTL fcntl(sockfd, F_SETFL, O_NONBLOCK); @@ -82,13 +84,22 @@ Error TCPServerPosix::listen(uint16_t p_port,const List *p_accepted_host WARN_PRINT("REUSEADDR failed!") } - struct sockaddr_in my_addr; - my_addr.sin_family = AF_INET; // host byte order - my_addr.sin_port = htons(p_port); // short, network byte order - my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP TODO: use p_accepted_hosts - memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); + sockaddr_storage addr = {0}; + if (p_type == IP_Address::TYPE_IPV4) { + struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr; + addr4->sin_family = AF_INET; + addr4->sin_port = htons(p_port); + addr4->sin_addr.s_addr = INADDR_ANY; + } else { + + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr; + addr6->sin6_family = AF_INET6; + addr6->sin6_port = htons(p_port); + addr6->sin6_addr = in6addr_any; + }; + // automatically fill with my IP TODO: use p_accepted_hosts - if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) != -1) { + if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != -1) { if (::listen(sockfd, 1) == -1) { @@ -136,9 +147,9 @@ Ref TCPServerPosix::take_connection() { return Ref(); }; - struct sockaddr_in their_addr; - socklen_t sin_size = sizeof(their_addr); - int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size); + struct sockaddr_storage their_addr; + socklen_t size = sizeof(their_addr); + int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size); ERR_FAIL_COND_V(fd == -1, Ref()); #ifndef NO_FCNTL fcntl(fd, F_SETFL, O_NONBLOCK); @@ -149,8 +160,23 @@ Ref TCPServerPosix::take_connection() { Ref conn = memnew(StreamPeerTCPPosix); IP_Address ip; - ip.host = (uint32_t)their_addr.sin_addr.s_addr; - conn->set_socket(fd, ip, ntohs(their_addr.sin_port)); + + if (their_addr.ss_family == AF_INET) { + ip.type = IP_Address::TYPE_IPV4; + + struct sockaddr_in* addr4 = (struct sockaddr_in*)&their_addr; + ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr; + conn->set_socket(fd, ip, ntohs(addr4->sin_port)); + + } else if (their_addr.ss_family == AF_INET6) { + + ip.type = IP_Address::TYPE_IPV6; + + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&their_addr; + copymem(&addr6->sin6_addr.s6_addr, ip.field8, 16); + + conn->set_socket(fd, ip, ntohs(addr6->sin6_port)); + }; return conn; }; -- cgit v1.2.3 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/tcp_server_posix.cpp | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) (limited to 'drivers/unix/tcp_server_posix.cpp') diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp index 03312a7538..27c5618479 100644 --- a/drivers/unix/tcp_server_posix.cpp +++ b/drivers/unix/tcp_server_posix.cpp @@ -56,6 +56,8 @@ #include #include +#include "drivers/unix/socket_helpers.h" + TCP_Server* TCPServerPosix::_create() { return memnew(TCPServerPosix); @@ -84,19 +86,9 @@ Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const WARN_PRINT("REUSEADDR failed!") } - sockaddr_storage addr = {0}; - if (p_type == IP_Address::TYPE_IPV4) { - struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr; - addr4->sin_family = AF_INET; - addr4->sin_port = htons(p_port); - addr4->sin_addr.s_addr = INADDR_ANY; - } else { - - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr; - addr6->sin6_family = AF_INET6; - addr6->sin6_port = htons(p_port); - addr6->sin6_addr = in6addr_any; - }; + struct sockaddr_storage addr; + _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts); + // automatically fill with my IP TODO: use p_accepted_hosts if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != -1) { @@ -161,22 +153,10 @@ Ref TCPServerPosix::take_connection() { Ref conn = memnew(StreamPeerTCPPosix); IP_Address ip; - if (their_addr.ss_family == AF_INET) { - ip.type = IP_Address::TYPE_IPV4; - - struct sockaddr_in* addr4 = (struct sockaddr_in*)&their_addr; - ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr; - conn->set_socket(fd, ip, ntohs(addr4->sin_port)); + int port; + _set_ip_addr_port(ip, port, &their_addr); - } else if (their_addr.ss_family == AF_INET6) { - - ip.type = IP_Address::TYPE_IPV6; - - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&their_addr; - copymem(&addr6->sin6_addr.s6_addr, ip.field8, 16); - - conn->set_socket(fd, ip, ntohs(addr6->sin6_port)); - }; + conn->set_socket(fd, ip, port); return conn; }; -- 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/tcp_server_posix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/unix/tcp_server_posix.cpp') diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp index 27c5618479..05b739804c 100644 --- a/drivers/unix/tcp_server_posix.cpp +++ b/drivers/unix/tcp_server_posix.cpp @@ -87,11 +87,11 @@ Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const } struct sockaddr_storage addr; - _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts); + size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts); // automatically fill with my IP TODO: use p_accepted_hosts - if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != -1) { + if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) { if (::listen(sockfd, 1) == -1) { -- cgit v1.2.3