1 # getaddrinfo.m4 serial 34
2 dnl Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_GETADDRINFO],
9 AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
10 AC_REQUIRE([gl_NETDB_H])dnl for HAVE_NETDB_H
12 gai_saved_LIBS="$LIBS"
14 dnl Where is getaddrinfo()?
15 dnl - On Solaris, it is in libsocket.
16 dnl - On Haiku, it is in libnetwork.
17 dnl - On BeOS, it is in libnet.
18 dnl - On native Windows, it is in ws2_32.dll.
19 dnl - Otherwise it is in libc.
20 AC_SEARCH_LIBS([getaddrinfo], [socket network net],
21 [if test "$ac_cv_search_getaddrinfo" != "none required"; then
22 GETADDRINFO_LIB="$ac_cv_search_getaddrinfo"
24 LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
27 AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [
28 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
37 ]], [[getaddrinfo("", "", NULL, NULL);]])],
38 [gl_cv_func_getaddrinfo=yes],
39 [gl_cv_func_getaddrinfo=no])])
40 if test $gl_cv_func_getaddrinfo = no; then
41 AC_CACHE_CHECK([for getaddrinfo in ws2tcpip.h and -lws2_32],
42 gl_cv_w32_getaddrinfo, [
43 gl_cv_w32_getaddrinfo=no
46 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
47 #ifdef HAVE_WS2TCPIP_H
51 ]], [[getaddrinfo(NULL, NULL, NULL, NULL);]])], [gl_cv_w32_getaddrinfo=yes])
54 if test "$gl_cv_w32_getaddrinfo" = "yes"; then
55 GETADDRINFO_LIB="-lws2_32"
56 LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
57 dnl Check for correct signature, in particular for a cdecl-compatible
58 dnl calling convention.
59 AC_CACHE_CHECK([for getaddrinfo with POSIX signature],
60 [gl_cv_func_getaddrinfo_posix_signature],
61 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
62 #include <sys/types.h>
63 #ifdef HAVE_SYS_SOCKET_H
64 #include <sys/socket.h>
69 #ifdef HAVE_WS2TCPIP_H
77 int getaddrinfo (const char *, const char *, const struct addrinfo *, struct addrinfo **);
79 [gl_cv_func_getaddrinfo_posix_signature=yes],
80 [gl_cv_func_getaddrinfo_posix_signature=no])
82 if test $gl_cv_func_getaddrinfo_posix_signature = no; then
89 AC_DEFINE_UNQUOTED([HAVE_GETADDRINFO], [$HAVE_GETADDRINFO],
90 [Define to 1 if getaddrinfo exists, or to 0 otherwise.])
92 # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
93 # inline function declared in ws2tcpip.h, so we need to get that
94 # header included somehow.
95 AC_CHECK_DECLS([gai_strerror], [], [], [[
96 #include <sys/types.h>
97 #ifdef HAVE_SYS_SOCKET_H
98 #include <sys/socket.h>
103 #ifdef HAVE_WS2TCPIP_H
104 #include <ws2tcpip.h>
108 if test $ac_cv_have_decl_gai_strerror = yes; then
109 AC_CHECK_DECLS([gai_strerrorA], [], [], [[
110 #include <sys/types.h>
111 #ifdef HAVE_SYS_SOCKET_H
112 #include <sys/socket.h>
117 #ifdef HAVE_WS2TCPIP_H
118 #include <ws2tcpip.h>
122 dnl check for correct signature
123 AC_CACHE_CHECK([for gai_strerror with POSIX signature],
124 [gl_cv_func_gai_strerror_posix_signature], [
125 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
126 #include <sys/types.h>
127 #ifdef HAVE_SYS_SOCKET_H
128 #include <sys/socket.h>
133 #ifdef HAVE_WS2TCPIP_H
134 #include <ws2tcpip.h>
141 const char *gai_strerror(int);]])],
142 [gl_cv_func_gai_strerror_posix_signature=yes],
143 [gl_cv_func_gai_strerror_posix_signature=no])])
144 if test $gl_cv_func_gai_strerror_posix_signature = no; then
145 REPLACE_GAI_STRERROR=1
149 LIBS="$gai_saved_LIBS"
151 gl_PREREQ_GETADDRINFO
153 AC_SUBST([GETADDRINFO_LIB])
156 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
157 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
158 AC_REQUIRE([gl_NETDB_H_DEFAULTS])
159 AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
160 AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
161 AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
162 AC_REQUIRE([gl_FUNC_INET_NTOP]) dnl for INET_NTOP_LIB
163 AC_REQUIRE([AC_C_RESTRICT])
164 AC_REQUIRE([gl_SOCKET_FAMILIES])
165 AC_REQUIRE([gl_SYS_SOCKET_H])
166 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
168 dnl Including sys/socket.h is wrong for Windows, but Windows does not
169 dnl have sa_len so the result is correct anyway.
170 AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
171 #include <sys/types.h>
172 #include <sys/socket.h>
175 AC_CHECK_HEADERS_ONCE([netinet/in.h])
177 AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, getnameinfo],,,[[
178 /* sys/types.h is not needed according to POSIX, but the
179 sys/socket.h in i386-unknown-freebsd4.10 and
180 powerpc-apple-darwin5.5 required it. */
181 #include <sys/types.h>
182 #ifdef HAVE_SYS_SOCKET_H
183 #include <sys/socket.h>
188 #ifdef HAVE_WS2TCPIP_H
189 #include <ws2tcpip.h>
192 if test $ac_cv_have_decl_getaddrinfo = no; then
193 HAVE_DECL_GETADDRINFO=0
195 if test $ac_cv_have_decl_freeaddrinfo = no; then
196 HAVE_DECL_FREEADDRINFO=0
198 if test $ac_cv_have_decl_gai_strerror = no; then
199 HAVE_DECL_GAI_STRERROR=0
201 if test $ac_cv_have_decl_getnameinfo = no; then
202 HAVE_DECL_GETNAMEINFO=0
205 AC_CHECK_TYPES([struct addrinfo],,,[
206 #include <sys/types.h>
207 #ifdef HAVE_SYS_SOCKET_H
208 #include <sys/socket.h>
213 #ifdef HAVE_WS2TCPIP_H
214 #include <ws2tcpip.h>
217 if test $ac_cv_type_struct_addrinfo = no; then
218 HAVE_STRUCT_ADDRINFO=0
221 dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
222 case " $GETADDRINFO_LIB " in
223 *" $HOSTENT_LIB "*) ;;
224 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
227 dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
228 case " $GETADDRINFO_LIB " in
229 *" $SERVENT_LIB "*) ;;
230 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
233 dnl Append $INET_NTOP_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
234 case " $GETADDRINFO_LIB " in
235 *" $INET_NTOP_LIB "*) ;;
236 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $INET_NTOP_LIB" ;;