From fc72c66f6b8a6b1d924ffa86861ce00a1c591839 Mon Sep 17 00:00:00 2001 From: Adrian Friedli Date: Thu, 2 Sep 2010 14:27:07 +0200 Subject: [PATCH 2/4] basic IPv6 support on Linux systems this uses IPv4-mapped IPv6 addresses --- include/enet/enet.h | 22 ++++++-- protocol.c | 26 +++++++--- unix.c | 134 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 114 insertions(+), 68 deletions(-) diff --git a/include/enet/enet.h b/include/enet/enet.h index 2f656d6..d3ca971 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -53,13 +53,16 @@ typedef enum _ENetSocketOption ENET_SOCKOPT_REUSEADDR = 5 } ENetSocketOption; -enum +typedef struct _ENetHostAddress { - ENET_HOST_ANY = 0, /**< specifies the default server host */ - ENET_HOST_BROADCAST = 0xFFFFFFFF, /**< specifies a subnet-wide broadcast */ + enet_uint8 addr[16]; +} ENetHostAddress; - ENET_PORT_ANY = 0 /**< specifies that a port should be automatically chosen */ -}; +extern const ENetHostAddress ENET_HOST_ANY; /**< specifies the default server host */ +extern const ENetHostAddress ENET_IPV4MAPPED_PREFIX; /**< specifies the IPv4-mapped IPv6 prefix */ +extern const ENetHostAddress ENET_HOST_BROADCAST; /**< specifies a IPv4 subnet-wide broadcast */ +#define ENET_IPV4MAPPED_PREFIX_LEN 12 /**< specifies the length of the IPv4-mapped IPv6 prefix */ +#define ENET_PORT_ANY 0 /**< specifies that a port should be automatically chosen */ /** * Portable internet address structure. @@ -73,7 +76,8 @@ enum */ typedef struct _ENetAddress { - enet_uint32 host; + ENetHostAddress host; + enet_uint32 scopeID; //FIXME: this is of different size on Windows enet_uint16 port; } ENetAddress; @@ -488,6 +492,12 @@ ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostN */ ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength); +/** Maps an IPv4 Address to an IPv6 address. + @param address IPv4 address in network byte order + @returns the IPv4-mapped IPv6 address in network byte order +*/ +ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address); + /** @} */ ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32); diff --git a/protocol.c b/protocol.c index 8e26dfb..930835e 100644 --- a/protocol.c +++ b/protocol.c @@ -9,6 +9,10 @@ #include "enet/time.h" #include "enet/enet.h" +const ENetHostAddress ENET_HOST_ANY = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }; +const ENetHostAddress ENET_IPV4MAPPED_PREFIX = { { 0,0,0,0,0,0,0,0,0,0, 0xff, 0xff, 0,0,0,0 } }; +const ENetHostAddress ENET_HOST_BROADCAST = { { 0,0,0,0,0,0,0,0,0,0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; + static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] = { 0, @@ -25,6 +29,14 @@ static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] = sizeof (ENetProtocolThrottleConfigure), }; +ENetHostAddress +enet_address_map4 (enet_uint32 address) +{ + ENetHostAddress addr = ENET_IPV4MAPPED_PREFIX; + ((enet_uint32 *)addr.addr)[3] = address; + return addr; +} + size_t enet_protocol_command_size (enet_uint8 commandNumber) { @@ -262,9 +274,9 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet ++ currentPeer) { if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED && - currentPeer -> address.host == host -> receivedAddress.host && currentPeer -> address.port == host -> receivedAddress.port && - currentPeer -> connectID == command -> connect.connectID) + currentPeer -> connectID == command -> connect.connectID && + !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress))) return NULL; } @@ -848,10 +860,11 @@ 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 && - peer -> address.host != ENET_HOST_BROADCAST) || (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID && - sessionID != peer -> incomingSessionID)) + sessionID != peer -> incomingSessionID) || + ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) && + memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) && + peer -> address.host.addr[0] != 0xff ) ) return 0; } @@ -891,8 +904,7 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) if (peer != NULL) { - peer -> address.host = host -> receivedAddress.host; - peer -> address.port = host -> receivedAddress.port; + peer -> address = host -> receivedAddress; peer -> incomingDataTotal += host -> receivedDataLength; } diff --git a/unix.c b/unix.c index 7329e8d..de032bb 100644 --- a/unix.c +++ b/unix.c @@ -71,30 +71,72 @@ enet_time_set (enet_uint32 newTimeBase) timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase; } +static int +enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) +{ + memset (address, 0, sizeof (ENetAddress)); + if (sin -> sa_family == AF_INET) + { + address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); + //address -> scopeID = 0; + address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); + return 0; + } + if (sin -> sa_family == AF_INET6) + { + address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; + address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; + address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); + return 0; + } + return -1; +} + +static int +enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, sa_family_t family) +{ + if (family == AF_INET) + { + memset (sin, 0, sizeof (struct sockaddr_in)); + ((struct sockaddr_in *) sin) -> sin_family = AF_INET; + ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; + ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); + return 0; + } + else if (family == AF_INET6) + { + memset (sin, 0, sizeof (struct sockaddr_in6)); + ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; + ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; + ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; + ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); + return 0; + } + return -1; +} + int enet_address_set_host (ENetAddress * address, const char * name) { + enet_uint16 port = address -> port; struct addrinfo hints; struct addrinfo * result; struct addrinfo * res; memset(& hints, 0, sizeof (hints)); - hints.ai_flags = AI_NUMERICSERV; - hints.ai_family = AF_INET; + hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG; + hints.ai_family = AF_UNSPEC; if ( getaddrinfo(name, NULL, &hints, &result) ) - { return -1; - } for (res = result; res != NULL; res = res -> ai_next) { - if (res -> ai_family == AF_INET) - { - address -> host = ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr; + if ( !enet_address_set_address(address, res -> ai_addr) ) break; - } } + + address -> port = port; freeaddrinfo(result); if (res == NULL) return -1; @@ -104,17 +146,11 @@ enet_address_set_host (ENetAddress * address, const char * name) static int enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) { - struct sockaddr_in sin; - - memset (& sin, 0, sizeof (struct sockaddr_in)); + struct sockaddr_storage sin; + enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6); - sin.sin_family = AF_INET; - sin.sin_addr = * (struct in_addr *) & address -> host; - - if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags)) - { + if ( getnameinfo((struct sockaddr *) & sin, sizeof(struct sockaddr_in6), name, nameLength, NULL, 0, flags)) return -1; - } return 0; } @@ -134,29 +170,22 @@ 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; - - memset (& sin, 0, sizeof (struct sockaddr_in)); - - sin.sin_family = AF_INET; + struct sockaddr_storage sin; if (address != NULL) { - sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); - sin.sin_addr.s_addr = address -> host; + enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6); } else { - sin.sin_port = 0; - sin.sin_addr.s_addr = INADDR_ANY; + ENetAddress address_ = { ENET_HOST_ANY, 0, 0 }; + enet_address_set_sin((struct sockaddr *) & sin, & address_, AF_INET6); } - return bind (socket, - (struct sockaddr *) & sin, - sizeof (struct sockaddr_in)); + return bind (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6)); } -int +int enet_socket_listen (ENetSocket socket, int backlog) { return listen (socket, backlog < 0 ? SOMAXCONN : backlog); @@ -165,7 +194,14 @@ 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); + ENetSocket sock = socket (AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); + +#ifdef IPV6_V6ONLY + int value = 0; + setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); +#endif // IPV6_V6ONLY + + return sock; } int @@ -207,23 +243,18 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) int enet_socket_connect (ENetSocket socket, const ENetAddress * address) { - struct sockaddr_in sin; + struct sockaddr_storage sin; + enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6); - 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; - - return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)); + return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6)); } ENetSocket enet_socket_accept (ENetSocket socket, ENetAddress * address) { int result; - struct sockaddr_in sin; - socklen_t sinLength = sizeof (struct sockaddr_in); + struct sockaddr_storage sin; + socklen_t sinLength = sizeof (struct sockaddr_in6); result = accept (socket, address != NULL ? (struct sockaddr *) & sin : NULL, @@ -234,8 +265,7 @@ 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); + enet_address_set_address(address, (struct sockaddr *) & sin); } return result; @@ -254,21 +284,16 @@ enet_socket_send (ENetSocket socket, size_t bufferCount) { struct msghdr msgHdr; - struct sockaddr_in sin; + struct sockaddr_storage sin; int sentLength; memset (& msgHdr, 0, sizeof (struct msghdr)); if (address != NULL) { - 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; - + enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6); msgHdr.msg_name = & sin; - msgHdr.msg_namelen = sizeof (struct sockaddr_in); + msgHdr.msg_namelen = sizeof (struct sockaddr_in6); } msgHdr.msg_iov = (struct iovec *) buffers; @@ -294,7 +319,7 @@ enet_socket_receive (ENetSocket socket, size_t bufferCount) { struct msghdr msgHdr; - struct sockaddr_in sin; + struct sockaddr_storage sin; int recvLength; memset (& msgHdr, 0, sizeof (struct msghdr)); @@ -302,7 +327,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; @@ -325,8 +350,7 @@ 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); + enet_address_set_address(address, (struct sockaddr *) & sin); } return recvLength; -- 1.7.1