From 12505b248b386948a0678d5ced6dd8c340c1f8b7 Mon Sep 17 00:00:00 2001 From: proller Date: Mon, 31 Oct 2016 23:33:45 +0300 Subject: [PATCH 1/3] add .gitignore, fix windows warnings --- .gitignore | 4 ++++ include/enet/win32.h | 1 + 2 files changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..fb24003a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +CMakeFiles/* +Makefile +cmake_install.cmake +libenet.a diff --git a/include/enet/win32.h b/include/enet/win32.h index e73ca9d0..063111c7 100644 --- a/include/enet/win32.h +++ b/include/enet/win32.h @@ -16,6 +16,7 @@ #include #include +#include typedef SOCKET ENetSocket; From aefa2bd251d9a08ebdf42ba3253118a4999a48c0 Mon Sep 17 00:00:00 2001 From: proller Date: Mon, 31 Oct 2016 23:35:22 +0300 Subject: [PATCH 2/3] Everything converted to ipv6. You still can connect to ipv4 via IPv4-Mapped IPv6 Addresses. Squashed commit of the following: commit c5595516a6bee82580df8841ea6d3dadbc47a773 Author: proller Date: Mon Oct 31 23:13:36 2016 +0300 Everything converted to ipv6. You still can connect to ipv4 via IPv4-Mapped IPv6 Addresses. Incompatible lib ABI. Squashed commit of the following: commit d643eb33df3fb2f232ca7ae3ba19d74952ba1a33 Merge: c3fec7a 9ce6c59 Author: proller Date: Mon Oct 31 23:06:18 2016 +0300 Merge branch 'master' into ipv6 commit c3fec7a427938300cc8022cdbb6ec6630391b702 Merge: 1f8eb62 f46fee0 Author: proller Date: Fri Sep 16 13:16:45 2016 +0300 Merge remote-tracking branch 'upstream/master' into ipv6 commit 1f8eb6217339d121ed6c2b96f4ca75c6441546ec Author: proller Date: Sat Mar 12 02:11:52 2016 +0300 support sin6_scope_id in address commit f499bc5cbbf354ae31d3370a8a04e3b5bee908e3 Merge: 720e7c4 4d10671 Author: proller Date: Sat Mar 12 01:14:00 2016 +0300 Merge remote-tracking branch 'upstream/master' into ipv6 commit 720e7c4ac649b963fc0510b905464cdbfd5733e5 Author: proller Date: Thu Dec 17 01:17:38 2015 +0300 return setting ENET_SOCKOPT_IPV6_V6ONLY=>0 after bind commit b0484734ab89327ad6fb86312a53b5570f8e4af3 Author: proller Date: Sat Sep 26 11:40:41 2015 +0400 fix win32 compiler warning commit 5a9492d03fb857809333133375772995c3daf548 Merge: 09018e4 2a567b0 Author: proller Date: Tue Aug 25 18:12:34 2015 +0300 Merge pull request #1 from K-os/ipv6 Allow connecting to IPv4 addresses commit 2a567b0b1a3350320c8ece854bfeb39ae616fd88 Author: Gabriel Hege Date: Mon Aug 24 12:07:09 2015 +0200 Set socket option IPV6_V6ONLY before bind() commit 2b52599874cc979624062f54f0d57a80c3bef392 Author: Gabriel Hege Date: Mon Aug 24 12:11:09 2015 +0200 Handle IPv4/v6 addresses returend from getaddrinfo commit a91524fd1544d89623d3a83ede9ab8f05dd352b5 Author: Gabriel Hege Date: Thu Aug 13 19:25:17 2015 +0200 gethostbyname(): Use correct address family AF_INET6 for result from gethostbyname() commit 90620b293ec57e6ffcb825b8d426070c71b4a183 Author: Gabriel Hege Date: Thu Aug 13 19:26:41 2015 +0200 Fail or Warn, when getaddrinfo(), inet_pton() and friends are missing commit 09018e42ce7780fd1f1f5a94e164d69741ec7949 Author: proller Date: Mon May 11 22:03:48 2015 +0300 fix commit a2651c61349949b8b22ed8e23640682d226451d5 Merge: 2c1bac4 62755b1 Author: proller Date: Mon May 11 20:28:46 2015 +0300 Merge branch 'master' into ipv6 Conflicts: unix.c commit 2c1bac444dd80a3324c276b725c1c65dae33f4aa Author: proller Date: Mon May 11 17:53:49 2015 +0300 add IPV6_V6ONLY socket option = 0 commit 52b0da7484290b10fe5adbaea8611c6576617e88 Author: proller Date: Tue May 5 22:37:06 2015 +0400 very ugly win32 fix commit 139439267914c84b544cf7bc33091d4e1197aa68 Merge: e236d01 b1758c4 Author: proller Date: Mon May 4 22:45:36 2015 +0300 Merge branch 'master' into ipv6 commit e236d0151c587222a159e6d9202b613d237ede26 Author: proller Date: Mon May 4 19:41:47 2015 +0400 fix aton types commit 0ff6534f302308901a765e9d88c8538826d062b4 Merge: cf46ab3 c10af34 Author: proller Date: Fri Apr 10 23:40:21 2015 +0300 Merge remote-tracking branch 'origin/master' into ipv6 Conflicts: include/enet/win32.h commit cf46ab34fe70681752ac7cdfa54352b07f35adca Author: proller Date: Mon Apr 6 03:20:12 2015 +0300 win32: fix include order commit b9de5d21fa9f88238c4d950af636470b30492be7 Merge: 3f5bab2 a2eb357 Author: proller Date: Sun Apr 5 03:38:19 2015 +0300 Merge branch 'master' into ipv6 Conflicts: unix.c commit 3f5bab275417734d7a0edf08df78eb0456e5bb7d Author: proller Date: Fri Jan 9 21:05:50 2015 +0300 fix inet_ntoa commit 7d9fa2b9af6809e30325fc3f526d4224e6cc15c4 Author: proller Date: Tue Dec 16 23:53:47 2014 +0300 fix win32 build errors commit 04394e682026b2f903a02f102992577f26669735 Author: proller Date: Wed Dec 3 01:07:18 2014 +0300 apple fix commit 9122c469864673f9c8797a019f92e5abb93ed0fc Merge: b3f2bf9 b8ae4a1 Author: proller Date: Wed Dec 3 01:01:51 2014 +0300 Merge branch 'master' into ipv6 commit b3f2bf918fa8da172c6fc135fa50e8fa053e5a31 Merge: 512a43b 8096edb Author: proller Date: Wed Oct 8 17:07:54 2014 +0400 Merge branch 'master' into ipv6 commit 512a43bac504c63d10ca3fd89cf204d229066c9b Author: proller Date: Thu Jul 3 18:46:25 2014 +0400 Copypaste resolving functions from unix.c to win32.c commit dce98b9c8bdf2df1cd2188043092ec88c4bb3be0 Author: proller Date: Wed May 21 01:21:39 2014 +0400 ENET_IPV6 flag commit db8fb8c2ce95b6127760fb59650440fd8ff5a1ba Author: proller Date: Wed May 21 00:25:51 2014 +0400 Try to add windows tcpip headers commit 28c5f1e4060ece2e59274817c9c80407e29244e6 Merge: 3963c69 06b535c Author: proller Date: Mon May 12 01:00:21 2014 +0400 Merge branch 'master' into ipv6 commit 3963c69b0dc239390cb705cc0b6ef6eeb2de9a87 Merge: 3586b8a 47c8379 Author: proller Date: Mon May 12 00:56:06 2014 +0400 Merge remote-tracking branch 'origin/master' into ipv6 commit 3586b8a3d4b6b3be14ebf77dfbf5766af7d7971c Author: proller Date: Thu Apr 10 23:50:10 2014 +0400 win32 convert, unfinished commit 3d27904f22f6fd8513538c3ec7158bdb3fa95146 Author: proller Date: Thu Apr 10 23:20:32 2014 +0400 ipv6 support commit 54d26810101a14126ae2c1abd93bec75372060dc Author: proller Date: Thu Apr 10 22:56:28 2014 +0400 add CMakeLists.txt commit 9ce6c592d13dcd1143bd1d175ec1959f16fd77b5 Merge: 62755b1 f46fee0 Author: proller Date: Mon Oct 31 23:05:11 2016 +0300 Merge remote-tracking branch 'upstream/master' commit 62755b1169c83c4b06a2c36624093c4eb63a8d1c Merge: b1758c4 5f47654 Author: proller Date: Mon May 11 18:02:12 2015 +0300 Merge remote-tracking branch 'enet/master' commit b1758c4595a80e127a56a641933de1592b69a865 Merge: c10af34 f7c46f0 Author: proller Date: Mon May 4 22:45:11 2015 +0300 Merge remote-tracking branch 'enet/master' commit c10af34fd72ac04629853e4cf1452c0ca188a4a0 Author: proller Date: Fri Apr 10 23:39:21 2015 +0300 fix windows compiler warning commit a2eb35707d7c8166e3e4628355fc38d4028af715 Merge: b8ae4a1 7c27a5d Author: proller Date: Sun Apr 5 03:33:47 2015 +0300 Merge remote-tracking branch 'upstream/master' commit b8ae4a150672088e719a75c725d917708c83689f Merge: 8096edb c8fa0ae Author: proller Date: Wed Dec 3 01:01:02 2014 +0300 Merge remote-tracking branch 'upstream/master' commit 8096edb3dba5229dc92c09bb86e9a0d544598c67 Merge: 4133fd2 6ef4e7d Author: proller Date: Wed Oct 8 17:07:07 2014 +0400 Merge remote-tracking branch 'upstream/master' commit 4133fd26b810f23108f776a1059e44f7ce199f74 Author: proller Date: Thu Apr 10 22:56:28 2014 +0400 add CMakeLists.txt commit eba42ff2d10901fbccb209e9f6df3fdc4b70a792 Author: proller Date: Wed May 21 00:35:29 2014 +0400 Revert "Try to add windows tcpip headers" This reverts commit 9008b3cadf09c1f92bff3fb60e0c7ef09e79d059. commit 9008b3cadf09c1f92bff3fb60e0c7ef09e79d059 Author: proller Date: Wed May 21 00:25:51 2014 +0400 Try to add windows tcpip headers commit 06b535cc9f0fdd62a7efd6a6308dc250fe9796a0 Merge: 47c8379 736474c Author: proller Date: Mon May 12 00:59:43 2014 +0400 Merge remote-tracking branch 'upstream/master' commit 47c8379293b91d7d715ca6b73d8d55c4788f5fea Author: proller Date: Mon May 12 00:55:31 2014 +0400 .gitignore --- host.c | 6 +- include/enet/enet.h | 9 ++- include/enet/win32.h | 11 ++++ protocol.c | 6 +- unix.c | 142 ++++++++++++++++++++++++++---------------- win32.c | 143 ++++++++++++++++++++++++++++--------------- 6 files changed, 208 insertions(+), 109 deletions(-) diff --git a/host.c b/host.c index 3be6c092..6418f270 100644 --- a/host.c +++ b/host.c @@ -49,6 +49,8 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM); + if (host -> socket != ENET_SOCKET_NULL) + enet_socket_set_option (host -> socket, ENET_SOCKOPT_IPV6_V6ONLY, 0); if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0)) { if (host -> socket != ENET_SOCKET_NULL) @@ -64,6 +66,8 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1); enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); + enet_socket_set_option (host -> socket, ENET_SOCKOPT_IPV6_V6ONLY, 0); + if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0) host -> address = * address; @@ -87,7 +91,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL host -> commandCount = 0; host -> bufferCount = 0; host -> checksum = NULL; - host -> receivedAddress.host = ENET_HOST_ANY; + host -> receivedAddress.host = in6addr_any; host -> receivedAddress.port = 0; host -> receivedData = NULL; host -> receivedDataLength = 0; diff --git a/include/enet/enet.h b/include/enet/enet.h index 650b199e..433e0205 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -62,7 +62,8 @@ typedef enum _ENetSocketOption ENET_SOCKOPT_RCVTIMEO = 6, ENET_SOCKOPT_SNDTIMEO = 7, ENET_SOCKOPT_ERROR = 8, - ENET_SOCKOPT_NODELAY = 9 + ENET_SOCKOPT_NODELAY = 9, + ENET_SOCKOPT_IPV6_V6ONLY = 10 } ENetSocketOption; typedef enum _ENetSocketShutdown @@ -72,6 +73,7 @@ typedef enum _ENetSocketShutdown ENET_SOCKET_SHUTDOWN_READ_WRITE = 2 } ENetSocketShutdown; +#define ENET_IPV6 1 #define ENET_HOST_ANY 0 #define ENET_HOST_BROADCAST 0xFFFFFFFFU #define ENET_PORT_ANY 0 @@ -88,10 +90,13 @@ typedef enum _ENetSocketShutdown */ typedef struct _ENetAddress { - enet_uint32 host; + struct in6_addr host; enet_uint16 port; + enet_uint16 sin6_scope_id; } ENetAddress; +#define in6_equal(in6_addr_a, in6_addr_b) (memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0) + /** * Packet flag bit constants. * diff --git a/include/enet/win32.h b/include/enet/win32.h index 063111c7..f90db46f 100644 --- a/include/enet/win32.h +++ b/include/enet/win32.h @@ -14,9 +14,20 @@ #endif #endif + +#ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN +#endif +#ifndef _WIN32_WINNT + #define _WIN32_WINNT 0x0501 +#endif +#ifndef NOMINMAX + #define NOMINMAX +#endif #include #include #include +#include typedef SOCKET ENetSocket; diff --git a/protocol.c b/protocol.c index 29d64873..8e7bd3b3 100644 --- a/protocol.c +++ b/protocol.c @@ -298,7 +298,7 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet } else if (currentPeer -> state != ENET_PEER_STATE_CONNECTING && - currentPeer -> address.host == host -> receivedAddress.host) + in6_equal(currentPeer -> address.host , host -> receivedAddress.host)) { if (currentPeer -> address.port == host -> receivedAddress.port && currentPeer -> connectID == command -> connect.connectID) @@ -1010,9 +1010,9 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) if (peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE || - ((host -> receivedAddress.host != peer -> address.host || + ((!in6_equal(host -> receivedAddress.host , peer -> address.host) || host -> receivedAddress.port != peer -> address.port) && - peer -> address.host != ENET_HOST_BROADCAST) || + 1 /* no broadcast in ipv6 !in6_equal(peer -> address.host , ENET_HOST_BROADCAST)*/) || (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID && sessionID != peer -> incomingSessionID)) return 0; diff --git a/unix.c b/unix.c index b3cadd0d..12b26a26 100644 --- a/unix.c +++ b/unix.c @@ -108,28 +108,46 @@ enet_address_set_host (ENetAddress * address, const char * name) struct addrinfo hints, * resultList = NULL, * result = NULL; memset (& hints, 0, sizeof (hints)); - hints.ai_family = AF_INET; + hints.ai_family = AF_UNSPEC; - if (getaddrinfo (name, NULL, NULL, & resultList) != 0) + if (getaddrinfo (name, NULL, & hints, & resultList) != 0) return -1; for (result = resultList; result != NULL; result = result -> ai_next) { - if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in)) + if (result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in)) { - struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr; + if (result -> ai_family == AF_INET) + { + struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr; - address -> host = sin -> sin_addr.s_addr; + ((uint32_t *) & address -> host.s6_addr)[0] = 0; + ((uint32_t *) & address -> host.s6_addr)[1] = 0; + ((uint32_t *) & address -> host.s6_addr)[2] = htonl(0xffff); + ((uint32_t *) & address -> host.s6_addr)[3] = sin->sin_addr.s_addr; - freeaddrinfo (resultList); + freeaddrinfo (resultList); - return 0; + return 0; + } + else if(result -> ai_family == AF_INET6) + { + struct sockaddr_in6 * sin = (struct sockaddr_in6 *) result -> ai_addr; + + address -> host = sin -> sin6_addr; + address -> sin6_scope_id = sin -> sin6_scope_id; + + freeaddrinfo (resultList); + + return 0; + } } } if (resultList != NULL) freeaddrinfo (resultList); #else +#warning "Really use gethostbyname() with IPv6? Not all platforms support it." struct hostent * hostEntry = NULL; #ifdef HAS_GETHOSTBYNAME_R struct hostent hostData; @@ -145,17 +163,18 @@ enet_address_set_host (ENetAddress * address, const char * name) hostEntry = gethostbyname (name); #endif - if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET) + if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET6) { - address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; + address -> host = *(struct in6_addr *) hostEntry -> h_addr_list [0]; return 0; } #endif #ifdef HAS_INET_PTON - if (! inet_pton (AF_INET, name, & address -> host)) + if (! inet_pton (AF_INET6, name, & address -> host)) #else +#error "inet_pton() is needed for IPv6 support" if (! inet_aton (name, (struct in_addr *) & address -> host)) #endif return -1; @@ -167,8 +186,9 @@ int enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) { #ifdef HAS_INET_NTOP - if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL) + if (inet_ntop (AF_INET6, & address -> host, name, nameLength) == NULL) #else +#error "inet_ntop() is needed for IPv6 support" char * addr = inet_ntoa (* (struct in_addr *) & address -> host); if (addr != NULL) { @@ -186,15 +206,17 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL int enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) { + #ifdef HAS_GETNAMEINFO - struct sockaddr_in sin; + struct sockaddr_in6 sin; int err; memset (& sin, 0, sizeof (struct sockaddr_in)); - sin.sin_family = AF_INET; - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_family = AF_INET6; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; err = getnameinfo ((struct sockaddr *) & sin, sizeof (sin), name, nameLength, NULL, 0, NI_NAMEREQD); if (! err) @@ -206,24 +228,25 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng if (err != EAI_NONAME) return -1; #else - struct in_addr in; +#warning "Really use gethostbyaddr() with IPv6? Not all platforms support it." + struct in6_addr in; struct hostent * hostEntry = NULL; #ifdef HAS_GETHOSTBYADDR_R struct hostent hostData; char buffer [2048]; int errnum; - in.s_addr = address -> host; + in = address -> host; #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) - gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); + gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); #else - hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum); + hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & errnum); #endif #else - in.s_addr = address -> host; + in = address -> host; - hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); + hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in6_addr), AF_INET6); #endif if (hostEntry != NULL) @@ -242,39 +265,42 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng int enet_socket_bind (ENetSocket socket, const ENetAddress * address) { - struct sockaddr_in sin; + struct sockaddr_in6 sin; - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; + sin.sin6_family = AF_INET6; if (address != NULL) { - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; } else { - sin.sin_port = 0; - sin.sin_addr.s_addr = INADDR_ANY; + sin.sin6_port = 0; + sin.sin6_addr = in6addr_any; + sin.sin6_scope_id = 0; } return bind (socket, (struct sockaddr *) & sin, - sizeof (struct sockaddr_in)); + sizeof (struct sockaddr_in6)); } int enet_socket_get_address (ENetSocket socket, ENetAddress * address) { - struct sockaddr_in sin; - socklen_t sinLength = sizeof (struct sockaddr_in); + struct sockaddr_in6 sin; + socklen_t sinLength = sizeof (struct sockaddr_in6); if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1) return -1; - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; return 0; } @@ -288,7 +314,7 @@ enet_socket_listen (ENetSocket socket, int backlog) ENetSocket enet_socket_create (ENetSocketType type) { - return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); + return socket (PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); } int @@ -343,6 +369,10 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int)); break; + case ENET_SOCKOPT_IPV6_V6ONLY: + result = setsockopt (socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) & value, sizeof (int)); + break; + default: break; } @@ -370,16 +400,17 @@ enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value) int enet_socket_connect (ENetSocket socket, const ENetAddress * address) { - struct sockaddr_in sin; + struct sockaddr_in6 sin; int result; - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_family = AF_INET6; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; - result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)); + result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6)); if (result == -1 && errno == EINPROGRESS) return 0; @@ -390,8 +421,8 @@ ENetSocket enet_socket_accept (ENetSocket socket, ENetAddress * address) { int result; - struct sockaddr_in sin; - socklen_t sinLength = sizeof (struct sockaddr_in); + struct sockaddr_in6 sin; + socklen_t sinLength = sizeof (struct sockaddr_in6); result = accept (socket, address != NULL ? (struct sockaddr *) & sin : NULL, @@ -402,8 +433,9 @@ enet_socket_accept (ENetSocket socket, ENetAddress * address) if (address != NULL) { - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; } return result; @@ -429,21 +461,22 @@ enet_socket_send (ENetSocket socket, size_t bufferCount) { struct msghdr msgHdr; - struct sockaddr_in sin; + struct sockaddr_in6 sin; int sentLength; memset (& msgHdr, 0, sizeof (struct msghdr)); if (address != NULL) { - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_family = AF_INET6; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; msgHdr.msg_name = & sin; - msgHdr.msg_namelen = sizeof (struct sockaddr_in); + msgHdr.msg_namelen = sizeof (struct sockaddr_in6); } msgHdr.msg_iov = (struct iovec *) buffers; @@ -469,7 +502,7 @@ enet_socket_receive (ENetSocket socket, size_t bufferCount) { struct msghdr msgHdr; - struct sockaddr_in sin; + struct sockaddr_in6 sin; int recvLength; memset (& msgHdr, 0, sizeof (struct msghdr)); @@ -477,7 +510,7 @@ enet_socket_receive (ENetSocket socket, if (address != NULL) { msgHdr.msg_name = & sin; - msgHdr.msg_namelen = sizeof (struct sockaddr_in); + msgHdr.msg_namelen = sizeof (struct sockaddr_in6); } msgHdr.msg_iov = (struct iovec *) buffers; @@ -500,8 +533,9 @@ enet_socket_receive (ENetSocket socket, if (address != NULL) { - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; } return recvLength; diff --git a/win32.c b/win32.c index 5cc16799..58b33c69 100644 --- a/win32.c +++ b/win32.c @@ -62,20 +62,36 @@ enet_time_set (enet_uint32 newTimeBase) int enet_address_set_host (ENetAddress * address, const char * name) { - struct hostent * hostEntry; - + struct hostent * hostEntry = NULL; +#ifdef HAS_GETHOSTBYNAME_R + struct hostent hostData; + char buffer [2048]; + int errnum; + +#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); +#else + hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); +#endif +#else hostEntry = gethostbyname (name); +#endif + if (hostEntry == NULL || hostEntry -> h_addrtype != AF_INET) { - unsigned long host = inet_addr (name); - if (host == INADDR_NONE) +#ifdef HAS_INET_PTON + if (! inet_pton (AF_INET6, name, & address -> host)) +#elif _MSC_VER + // TODO FIXME +#else + if (! inet_aton (name, (struct in_addr *) & address -> host)) +#endif return -1; - address -> host = host; return 0; } - address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; +// address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; return 0; } @@ -83,28 +99,46 @@ enet_address_set_host (ENetAddress * address, const char * name) int enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) { +#ifdef HAS_INET_NTOP + if (inet_ntop (AF_INET6, & address -> host, name, nameLength) == NULL) +#else char * addr = inet_ntoa (* (struct in_addr *) & address -> host); - if (addr == NULL) - return -1; - else + if (addr != NULL) { size_t addrLen = strlen(addr); if (addrLen >= nameLength) return -1; memcpy (name, addr, addrLen + 1); - } + } + else +#endif + return -1; return 0; } int enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) { - struct in_addr in; - struct hostent * hostEntry; - - in.s_addr = address -> host; - - hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); + struct in6_addr in; + struct hostent * hostEntry = NULL; +#ifdef HAS_GETHOSTBYADDR_R + struct hostent hostData; + char buffer [2048]; + int errnum; + + in = address -> host; + +#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); +#else + hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & errnum); +#endif +#else + in = address -> host; + + hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in6_addr), AF_INET6); +#endif + if (hostEntry == NULL) return enet_address_get_host_ip (address, name, nameLength); else @@ -121,39 +155,42 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng int enet_socket_bind (ENetSocket socket, const ENetAddress * address) { - struct sockaddr_in sin; + struct sockaddr_in6 sin; - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; + sin.sin6_family = AF_INET6; if (address != NULL) { - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; } else { - sin.sin_port = 0; - sin.sin_addr.s_addr = INADDR_ANY; + sin.sin6_port = 0; + sin.sin6_addr = in6addr_any; + sin.sin6_scope_id = 0; } return bind (socket, (struct sockaddr *) & sin, - sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; + sizeof (struct sockaddr_in6)) == SOCKET_ERROR ? -1 : 0; } int enet_socket_get_address (ENetSocket socket, ENetAddress * address) { - struct sockaddr_in sin; - int sinLength = sizeof (struct sockaddr_in); + struct sockaddr_in6 sin; + int sinLength = sizeof (struct sockaddr_in6); if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1) return -1; - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; return 0; } @@ -167,7 +204,7 @@ enet_socket_listen (ENetSocket socket, int backlog) ENetSocket enet_socket_create (ENetSocketType type) { - return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); + return socket (PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); } int @@ -211,6 +248,10 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int)); break; + case ENET_SOCKOPT_IPV6_V6ONLY: + result = setsockopt (socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) & value, sizeof (int)); + break; + default: break; } @@ -237,16 +278,17 @@ enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value) int enet_socket_connect (ENetSocket socket, const ENetAddress * address) { - struct sockaddr_in sin; + struct sockaddr_in6 sin; int result; - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_family = AF_INET6; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; - result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)); + result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6)); if (result == SOCKET_ERROR && WSAGetLastError () != WSAEWOULDBLOCK) return -1; @@ -257,8 +299,8 @@ ENetSocket enet_socket_accept (ENetSocket socket, ENetAddress * address) { SOCKET result; - struct sockaddr_in sin; - int sinLength = sizeof (struct sockaddr_in); + struct sockaddr_in6 sin; + int sinLength = sizeof (struct sockaddr_in6); result = accept (socket, address != NULL ? (struct sockaddr *) & sin : NULL, @@ -269,8 +311,9 @@ enet_socket_accept (ENetSocket socket, ENetAddress * address) if (address != NULL) { - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; } return result; @@ -295,16 +338,17 @@ enet_socket_send (ENetSocket socket, const ENetBuffer * buffers, size_t bufferCount) { - struct sockaddr_in sin; + struct sockaddr_in6 sin; DWORD sentLength; if (address != NULL) { - memset (& sin, 0, sizeof (struct sockaddr_in)); + memset (& sin, 0, sizeof (struct sockaddr_in6)); - sin.sin_family = AF_INET; - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + sin.sin6_family = AF_INET6; + sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port); + sin.sin6_addr = address -> host; + sin.sin6_scope_id = address -> sin6_scope_id; } if (WSASendTo (socket, @@ -313,7 +357,7 @@ enet_socket_send (ENetSocket socket, & sentLength, 0, address != NULL ? (struct sockaddr *) & sin : NULL, - address != NULL ? sizeof (struct sockaddr_in) : 0, + address != NULL ? sizeof (struct sockaddr_in6) : 0, NULL, NULL) == SOCKET_ERROR) { @@ -332,10 +376,10 @@ enet_socket_receive (ENetSocket socket, ENetBuffer * buffers, size_t bufferCount) { - INT sinLength = sizeof (struct sockaddr_in); + INT sinLength = sizeof (struct sockaddr_in6); DWORD flags = 0, recvLength; - struct sockaddr_in sin; + struct sockaddr_in6 sin; if (WSARecvFrom (socket, (LPWSABUF) buffers, @@ -362,8 +406,9 @@ enet_socket_receive (ENetSocket socket, if (address != NULL) { - address -> host = (enet_uint32) sin.sin_addr.s_addr; - address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); + address -> host = sin.sin6_addr; + address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port); + address -> sin6_scope_id = sin.sin6_scope_id; } return (int) recvLength; From 61728bea90b699d10e49c9826928c4dc23b29977 Mon Sep 17 00:00:00 2001 From: proller Date: Wed, 19 Sep 2018 23:27:55 +0300 Subject: [PATCH 3/3] merge fix --- unix.c | 1 - win32.c | 2 -- 2 files changed, 3 deletions(-) diff --git a/unix.c b/unix.c index 98a06e45..7af0097c 100644 --- a/unix.c +++ b/unix.c @@ -107,7 +107,6 @@ enet_address_set_host_ip (ENetAddress * address, const char * name) #ifdef HAS_INET_PTON if (! inet_pton (AF_INET6, name, & address -> host)) #else -#error "inet_pton() is needed for IPv6 support" if (! inet_aton (name, (struct in_addr *) & address -> host)) #endif diff --git a/win32.c b/win32.c index f47792bf..40d112d7 100644 --- a/win32.c +++ b/win32.c @@ -66,8 +66,6 @@ enet_address_set_host_ip (ENetAddress * address, const char * name) if (! inet_pton (AF_INET6, name, & address -> host)) return -1; #else -#error "inet_pton() is needed for IPv6 support" - enet_uint8 vals [4] = { 0, 0, 0, 0 }; int i;