From 76a55c18a68a2ff3443606677a841a820398c509 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
Date: Tue, 21 Feb 2017 13:16:29 +0100
Subject: [PATCH 2/2] Allow usage of getaddrinfo provided by the system

---
 config.h.in                    |  3 ++
 configure                      | 70 ++++++++++++++++++++++++++++++++++++++++++
 configure.in                   | 43 ++++++++++++++++++++++++++
 lib/irs/Makefile.in            | 11 ++++---
 lib/irs/include/irs/netdb.h.in |  3 ++
 5 files changed, 126 insertions(+), 4 deletions(-)

diff --git a/config.h.in b/config.h.in
index 4dad23d..d2018c8 100644
--- a/config.h.in
+++ b/config.h.in
@@ -323,6 +323,9 @@ int sigwait(const unsigned int *set, int *sig);
 /* Define to 1 if you have the `nanosleep' function. */
 #undef HAVE_NANOSLEEP
 
+/* Define to 1 to use native getaddrinfo and getnameinfo */
+#undef HAVE_NATIVEADDRINFO
+
 /* Define to 1 if you have the <net/if6.h> header file. */
 #undef HAVE_NET_IF6_H
 
diff --git a/configure b/configure
index 4f2a278..f6a9275 100755
--- a/configure
+++ b/configure
@@ -746,6 +746,9 @@ LWRES_PLATFORM_NEEDSTRTOUL
 ISC_PLATFORM_NEEDSTRTOUL
 ISC_PLATFORM_NEEDMEMMOVE
 ISC_PLATFORM_NEEDSTRSEP
+USE_NATIVE_ADDRINFO_SRCS
+USE_NATIVE_ADDRINFO_OBJS
+USE_CUSTOM_ADDRINFO
 ISC_IRS_GETNAMEINFOSOCKLEN
 ISC_LWRES_GETNAMEINFOPROTO
 ISC_LWRES_GETADDRINFOPROTO
@@ -1005,6 +1008,7 @@ with_export_includedir
 enable_ipv6
 with_kame
 enable_getifaddrs
+enable_nativeaddrinfo
 with_readline
 enable_isc_spnego
 enable_chroot
@@ -1685,6 +1689,7 @@ Optional Features:
 			  [default=no]
   --enable-ipv6           use IPv6 default=autodetect
   --enable-getifaddrs     Enable the use of getifaddrs() [yes|no].
+  --enable-nativeaddrinfo Use native getaddrinfo in irs library [yes|no].
   --disable-isc-spnego    use SPNEGO from GSSAPI library
   --disable-chroot        disable chroot
   --disable-linux-caps	  disable linux capabilities
@@ -17564,6 +17569,71 @@ no)
 ;;
 esac
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for struct addrinfo flags" >&5
+$as_echo_n "checking for struct addrinfo flags... " >&6; }
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+struct addrinfo a; a.ai_flags=AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST;
+int
+main ()
+{
+return(0);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+	ADDRINFO_FLAGS=yes
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+	ADDRINFO_FLAGS=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+# Check whether --enable-nativeaddrinfo was given.
+if test "${enable_nativeaddrinfo+set}" = set; then :
+  enableval=$enable_nativeaddrinfo; want_nativeaddrinfo="$enableval"
+else
+  want_nativeaddrinfo="no"
+fi
+
+
+if test "$want_nativeaddrinfo" = "yes"
+then
+	for FUNC in getnameinfo getaddrinfo gai_strerror
+	do
+		if test "yes" != "$ac_cv_func_$FUNC"
+		then
+			as_fn_error $? "System addrinfo calls not supported" "$LINENO" 5
+			want_nativeaddrinfo="no"
+		fi
+	done
+fi
+
+if test "$want_nativeaddrinfo" = "yes" -a "$ADDRINFO_FLAGS" = "yes"
+then
+	USE_NATIVE_ADDRINFO_OBJS=''
+	USE_NATIVE_ADDRINFO_SRCS=''
+
+$as_echo "#define HAVE_NATIVEADDRINFO 1" >>confdefs.h
+
+	USE_CUSTOM_ADDRINFO=0
+else
+	USE_NATIVE_ADDRINFO_OBJS='${ADDRINFO_OBJS}'
+	USE_NATIVE_ADDRINFO_SRCS='${ADDRINFO_SRCS}'
+	USE_CUSTOM_ADDRINFO=1
+fi
+
+
+
+
 #
 # Look for a sysctl call to get the list of network interfaces.
 #
diff --git a/configure.in b/configure.in
index a39adcf..39a236a 100644
--- a/configure.in
+++ b/configure.in
@@ -2646,6 +2646,49 @@ no)
 ;;
 esac
 
+AC_MSG_CHECKING(for struct addrinfo flags)
+AC_TRY_COMPILE([
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+struct addrinfo a; a.ai_flags=AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST;],
+[return(0);],
+	[AC_MSG_RESULT(yes)
+	ADDRINFO_FLAGS=yes],
+	[AC_MSG_RESULT(no)
+	ADDRINFO_FLAGS=no])
+
+AC_ARG_ENABLE(nativeaddrinfo,
+[  --enable-nativeaddrinfo Use native getaddrinfo in irs library [[yes|no]].],
+    want_nativeaddrinfo="$enableval",  want_nativeaddrinfo="no")
+
+if test "$want_nativeaddrinfo" = "yes"
+then
+	for FUNC in getnameinfo getaddrinfo gai_strerror
+	do
+		if test "yes" != "$ac_cv_func_$FUNC"
+		then
+			AC_MSG_ERROR(System addrinfo calls not supported)
+			want_nativeaddrinfo="no"
+		fi
+	done
+fi
+
+if test "$want_nativeaddrinfo" = "yes" -a "$ADDRINFO_FLAGS" = "yes"
+then
+	USE_NATIVE_ADDRINFO_OBJS=''
+	USE_NATIVE_ADDRINFO_SRCS=''
+	AC_DEFINE(HAVE_NATIVEADDRINFO, [1], [Define to 1 to use native getaddrinfo and getnameinfo])
+	USE_CUSTOM_ADDRINFO=0
+else
+	USE_NATIVE_ADDRINFO_OBJS='${ADDRINFO_OBJS}'
+	USE_NATIVE_ADDRINFO_SRCS='${ADDRINFO_SRCS}'
+	USE_CUSTOM_ADDRINFO=1
+fi
+AC_SUBST(USE_CUSTOM_ADDRINFO)
+AC_SUBST(USE_NATIVE_ADDRINFO_OBJS)
+AC_SUBST(USE_NATIVE_ADDRINFO_SRCS)
+
 #
 # Look for a sysctl call to get the list of network interfaces.
 #
diff --git a/lib/irs/Makefile.in b/lib/irs/Makefile.in
index fc302b2..b4ec562 100644
--- a/lib/irs/Makefile.in
+++ b/lib/irs/Makefile.in
@@ -30,17 +30,20 @@ CINCLUDES =	-I. -I./include -I${srcdir}/include \
 CDEFINES =
 CWARNINGS =
 
+ADDRINFO_OBJS = gai_strerror.@O@ getaddrinfo.@O@ getnameinfo.@O@
+ADDRINFO_SRCS = gai_strerror.c getaddrinfo.c getnameinfo.c
+
 # Alphabetically
 OBJS =		context.@O@ \
 		dnsconf.@O@ \
-		gai_strerror.@O@ getaddrinfo.@O@ getnameinfo.@O@ \
-		resconf.@O@
+		resconf.@O@ \
+		@USE_NATIVE_ADDRINFO_OBJS@
 
 # Alphabetically
 SRCS =		context.c \
 		dnsconf.c \
-		gai_strerror.c getaddrinfo.c getnameinfo.c \
-		resconf.c
+		resconf.c \
+		@USE_NATIVE_ADDRINFO_SRCS@
 
 LIBS =		@LIBS@
 
diff --git a/lib/irs/include/irs/netdb.h.in b/lib/irs/include/irs/netdb.h.in
index 3cf173f..fd9dce3 100644
--- a/lib/irs/include/irs/netdb.h.in
+++ b/lib/irs/include/irs/netdb.h.in
@@ -24,6 +24,7 @@
 #include <stddef.h>	/* Required on FreeBSD (and  others?) for size_t. */
 #include <netdb.h>	/* Contractual provision. */
 
+#if @USE_CUSTOM_ADDRINFO@
 /*
  * Define if <netdb.h> does not declare struct addrinfo.
  */
@@ -185,6 +186,8 @@ irs_gai_strerror(int ecode);
 #define gai_strerror irs_gai_strerror
 #endif
 
+#endif /* USE_CUSTOM_ADDRINFO */
+
 /*
  * Tell Emacs to use C mode on this file.
  * Local variables:
-- 
2.9.3

