Index: code/branches/ipv6/src/external/enet/unix.c
===================================================================
--- code/branches/ipv6/src/external/enet/unix.c	(revision 7328)
+++ code/branches/ipv6/src/external/enet/unix.c	(revision 7330)
@@ -75,105 +75,94 @@
 enet_address_set_host (ENetAddress * address, const char * name)
 {
-    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)
-    {
-#ifdef HAS_INET_PTON
-        if (! inet_pton (AF_INET, name, & address -> host))
-#else
-        if (! inet_aton (name, (struct in_addr *) & address -> host))
-#endif
-            return -1;
-        return 0;
-    }
-
-    address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
+    struct addrinfo hints;
+    struct addrinfo * result;
+    struct addrinfo * res;
+
+    memset(& hints, 0, sizeof (hints));
+    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 = enet_address_map4( ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr );
+            address -> scopeID = 0;
+            break;
+        }
+        if (res -> ai_family == AF_INET6)
+        {
+            address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) res -> ai_addr ) -> sin6_addr;
+            address -> scopeID = ((struct sockaddr_in6 *) res -> ai_addr ) -> sin6_scope_id;
+            break;
+        }
+    }
+    freeaddrinfo(result);
+    if (res == NULL) return -1;
 
     return 0;
 }
 
+static int
+enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
+{
+    struct sockaddr_in6 sin;
+
+    memset (& sin, 0, sizeof (struct sockaddr_in6));
+
+    sin.sin6_family = AF_INET6;
+    sin.sin6_addr = * (struct in6_addr *) & address -> host;
+    sin.sin6_scope_id = address -> scopeID;
+
+    if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags))
+    {
+        return -1;
+    }
+
+    return 0;
+}
+
 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)
-#else
-    char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
-    if (addr != NULL)
-        strncpy (name, addr, nameLength);
+    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
+}
+
+int
+enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
+{
+    return enet_address_get_host_x(address, name, nameLength, 0);
+}
+
+int
+enet_socket_bind (ENetSocket socket, const ENetAddress * address)
+{
+    struct sockaddr_in6 sin;
+
+    memset (& sin, 0, sizeof (struct sockaddr_in6));
+
+    sin.sin6_family = AF_INET6;
+
+    if (address != NULL)
+    {
+       sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
+       sin.sin6_addr = * (struct in6_addr *) & address -> host;
+       sin.sin6_scope_id = address -> scopeID;
+    }
     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 = NULL;
-#ifdef HAS_GETHOSTBYADDR_R
-    struct hostent hostData;
-    char buffer [2048];
-    int errnum;
-
-    in.s_addr = address -> host;
-
-#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-    gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
-#else
-    hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
-#endif
-#else
-    in.s_addr = address -> host;
-
-    hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
-#endif
-
-    if (hostEntry == NULL)
-      return enet_address_get_host_ip (address, name, nameLength);
-
-    strncpy (name, hostEntry -> h_name, nameLength);
-
-    return 0;
-}
-
-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;
-
-    if (address != NULL)
-    {
-       sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
-       sin.sin_addr.s_addr = address -> host;
-    }
-    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));
 }
 
@@ -187,5 +176,5 @@
 enet_socket_create (ENetSocketType type)
 {
-    return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
+    return socket (AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
 }
 
@@ -220,4 +209,8 @@
             break;
 
+        case ENET_SOCKOPT_V6ONLY:
+            result = setsockopt (socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) & value, sizeof (int));
+            break;
+
         default:
             break;
@@ -229,13 +222,14 @@
 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
 {
-    struct sockaddr_in sin;
-
-    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));
+    struct sockaddr_in6 sin;
+
+    memset (& sin, 0, sizeof (struct sockaddr_in6));
+
+    sin.sin6_family = AF_INET6;
+    sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
+    sin.sin6_addr = * (struct in6_addr *) & address -> host;
+    sin.sin6_scope_id = address -> scopeID;
+
+    return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
 }
 
@@ -244,6 +238,6 @@
 {
     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, 
@@ -256,6 +250,7 @@
     if (address != NULL)
     {
-        address -> host = (enet_uint32) sin.sin_addr.s_addr;
-        address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
+        address -> host = * (ENetHostAddress *) & sin.sin6_addr;
+        address -> scopeID = sin.sin6_scope_id;
+        address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
     }
 
@@ -276,5 +271,5 @@
 {
     struct msghdr msgHdr;
-    struct sockaddr_in sin;
+    struct sockaddr_in6 sin;
     int sentLength;
 
@@ -283,12 +278,13 @@
     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;
+        memset (& sin, 0, sizeof (struct sockaddr_in6));
+
+        sin.sin6_family = AF_INET6;
+        sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
+        sin.sin6_addr = * (struct in6_addr *) & address -> host;
+        sin.sin6_scope_id = address -> scopeID;
 
         msgHdr.msg_name = & sin;
-        msgHdr.msg_namelen = sizeof (struct sockaddr_in);
+        msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     }
 
@@ -316,5 +312,5 @@
 {
     struct msghdr msgHdr;
-    struct sockaddr_in sin;
+    struct sockaddr_in6 sin;
     int recvLength;
 
@@ -324,5 +320,5 @@
     {
         msgHdr.msg_name = & sin;
-        msgHdr.msg_namelen = sizeof (struct sockaddr_in);
+        msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     }
 
@@ -347,6 +343,7 @@
     if (address != NULL)
     {
-        address -> host = (enet_uint32) sin.sin_addr.s_addr;
-        address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
+        address -> host = * (ENetHostAddress *) & sin.sin6_addr;
+        address -> scopeID = sin.sin6_scope_id;
+        address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
     }
 
