From 71263165c4d13d2962a51bb91f3d6401569517ea Mon Sep 17 00:00:00 2001
From: Alberto Massari <amassari@apache.org>
Date: Fri, 20 Oct 2006 15:04:20 +0000
Subject: [PATCH] Enable IPv6 addresses by using getaddrinfo when available
 [based on a patch by Ramanjaneyulu Malisetti]

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@466137 13f79535-47bb-0310-9956-ffa450edef68
---
 configure.ac                                  | 16 +++--
 .../Socket/UnixHTTPURLInputStream.cpp         | 65 +++++++++++++++++--
 2 files changed, 67 insertions(+), 14 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8f8b39c99..57ebdf2c8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -104,13 +104,15 @@ ACX_PTHREAD
 #AC_FUNC_MEMCMP
 #AC_FUNC_STRCOLL
 #AC_FUNC_STRTOD
-AC_CHECK_FUNCS([clock_gettime ftime getcwd gethostbyaddr gethostbyname gettimeofday localeconv \
-				mblen memmove memset nl_langinfo pathconf realpath setlocale socket \
-				strcasecmp strncasecmp stricmp strnicmp strchr strdup \
-				strrchr strstr strtol strtoul \
-				towupper towlower \
-				mbrlen wcsrtombs mbsrtowcs \
-				])
+AC_CHECK_FUNCS([getcwd pathconf realpath \
+		getaddrinfo gethostbyaddr gethostbyname socket \
+		clock_gettime ftime gettimeofday \
+		memmove memset nl_langinfo setlocale localeconv \
+		strcasecmp strncasecmp stricmp strnicmp strchr strdup \
+		strrchr strstr strtol strtoul \
+		towupper towlower \
+		mblen mbrlen wcsrtombs mbsrtowcs \
+		])
 				
 AC_SUBST([SHREXT], [$shrext_cmds])
 
diff --git a/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp b/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
index 8219be09a..5610c82a6 100644
--- a/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
+++ b/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
@@ -18,18 +18,32 @@
  * $Id$
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#if !defined(XML_BEOS)
-  #include <netinet/in.h>
-  #include <arpa/inet.h>
+#if HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+#if HAVE_SYS_TYPES_H
+#  include <sys/types.h>
+#endif
+#if HAVE_SYS_SOCKET_H 
+#  include <sys/socket.h>
+#endif
+#if HAVE_NETINET_IN_H 
+#  include <netinet/in.h>
+#endif
+#if HAVE_ARPA_INET_H
+#  include <arpa/inet.h>
+#endif
+#if HAVE_NETDB_H 
+#  include <netdb.h>
 #endif
-#include <netdb.h>
 #include <errno.h>
 
 #include <xercesc/util/XMLNetAccessor.hpp>
@@ -247,6 +261,42 @@ UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XM
     //
     // Set up a socket.
     //
+#if HAVE_GETADDRINFO
+    struct addrinfo hints, *res, *ai;
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = PF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    int n = getaddrinfo(hostNameAsCharStar,portAsASCII,&hints, &res);
+    if(n<0)
+    {
+        hints.ai_flags = AI_NUMERICHOST;
+        n = getaddrinfo(hostNameAsCharStar,portAsASCII,&hints, &res);
+        if(n<0)
+            ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager);
+    }
+    int s;
+    for (ai = res; ai != NULL; ai = ai->ai_next) {
+        // Open a socket with the correct address family for this address.
+        s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+        if (s < 0)
+            continue;
+        if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0)
+        {
+            freeaddrinfo(res);
+            ThrowXMLwithMemMgr1(NetAccessorException,
+                     XMLExcepts::NetAcc_ConnSocket, urlSource.getURLText(), fMemoryManager);
+        }
+        break;
+    }
+    freeaddrinfo(res);
+    if (s < 0)
+    {
+        ThrowXMLwithMemMgr1(NetAccessorException,
+                 XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager);
+    }
+    SocketJanitor janSock(&s);
+#else
     struct hostent*     hostEntPtr = 0;
     struct sockaddr_in  sa;
 
@@ -282,6 +332,7 @@ UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XM
         ThrowXMLwithMemMgr1(NetAccessorException,
                  XMLExcepts::NetAcc_ConnSocket, urlSource.getURLText(), fMemoryManager);
     }
+#endif
 
     // The port is open and ready to go.
     // Build up the http GET command to send to the server.
-- 
GitLab