immutable: Add tests.
[gnulib.git] / m4 / getaddrinfo.m4
blob97889a8b546e122bcf36d403e91bf4c5d1f4df17
1 # getaddrinfo.m4 serial 33
2 dnl Copyright (C) 2004-2021 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_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
10   AC_REQUIRE([gl_HEADER_NETDB])dnl for HAVE_NETDB_H
11   GETADDRINFO_LIB=
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"
23      fi])
24   LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
26   HAVE_GETADDRINFO=1
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>
32 #endif
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36 #include <stddef.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
44       am_save_LIBS="$LIBS"
45       LIBS="$LIBS -lws2_32"
46       AC_LINK_IFELSE([AC_LANG_PROGRAM([[
47 #ifdef HAVE_WS2TCPIP_H
48 #include <ws2tcpip.h>
49 #endif
50 #include <stddef.h>
51 ]], [[getaddrinfo(NULL, NULL, NULL, NULL);]])], [gl_cv_w32_getaddrinfo=yes])
52       LIBS="$am_save_LIBS"
53     ])
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>
65 #endif
66 #ifdef HAVE_NETDB_H
67 #include <netdb.h>
68 #endif
69 #ifdef HAVE_WS2TCPIP_H
70 #include <ws2tcpip.h>
71 #endif
72 #include <stddef.h>
73 extern
74 #ifdef __cplusplus
75 "C"
76 #endif
77 int getaddrinfo (const char *, const char *, const struct addrinfo *, struct addrinfo **);
78 ]])],
79            [gl_cv_func_getaddrinfo_posix_signature=yes],
80            [gl_cv_func_getaddrinfo_posix_signature=no])
81         ])
82       if test $gl_cv_func_getaddrinfo_posix_signature = no; then
83         REPLACE_GETADDRINFO=1
84       fi
85     else
86       HAVE_GETADDRINFO=0
87     fi
88   fi
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>
99 #endif
100 #ifdef HAVE_NETDB_H
101 #include <netdb.h>
102 #endif
103 #ifdef HAVE_WS2TCPIP_H
104 #include <ws2tcpip.h>
105 #endif
106 #include <stddef.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>
113 #endif
114 #ifdef HAVE_NETDB_H
115 #include <netdb.h>
116 #endif
117 #ifdef HAVE_WS2TCPIP_H
118 #include <ws2tcpip.h>
119 #endif
120 #include <stddef.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>
129 #endif
130 #ifdef HAVE_NETDB_H
131 #include <netdb.h>
132 #endif
133 #ifdef HAVE_WS2TCPIP_H
134 #include <ws2tcpip.h>
135 #endif
136 #include <stddef.h>
137 extern
138 #ifdef __cplusplus
140 #endif
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
146     fi
147   fi
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_HEADER_SYS_SOCKET])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_HEADER_SYS_SOCKET])
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>
184 #endif
185 #ifdef HAVE_NETDB_H
186 #include <netdb.h>
187 #endif
188 #ifdef HAVE_WS2TCPIP_H
189 #include <ws2tcpip.h>
190 #endif
192   if test $ac_cv_have_decl_getaddrinfo = no; then
193     HAVE_DECL_GETADDRINFO=0
194   fi
195   if test $ac_cv_have_decl_freeaddrinfo = no; then
196     HAVE_DECL_FREEADDRINFO=0
197   fi
198   if test $ac_cv_have_decl_gai_strerror = no; then
199     HAVE_DECL_GAI_STRERROR=0
200   fi
201   if test $ac_cv_have_decl_getnameinfo = no; then
202     HAVE_DECL_GETNAMEINFO=0
203   fi
205   AC_CHECK_TYPES([struct addrinfo],,,[
206 #include <sys/types.h>
207 #ifdef HAVE_SYS_SOCKET_H
208 #include <sys/socket.h>
209 #endif
210 #ifdef HAVE_NETDB_H
211 #include <netdb.h>
212 #endif
213 #ifdef HAVE_WS2TCPIP_H
214 #include <ws2tcpip.h>
215 #endif
217   if test $ac_cv_type_struct_addrinfo = no; then
218     HAVE_STRUCT_ADDRINFO=0
219   fi
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" ;;
225   esac
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" ;;
231   esac
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" ;;
237   esac