Debian Codegraph @

m4/getaddrinfo.m4

raw
1 # getaddrinfo.m4
2 # serial 39
3 dnl Copyright (C) 2004-2026 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_GETADDRINFO],
10 [
11 AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
12 AC_REQUIRE([gl_NETDB_H])dnl for HAVE_NETDB_H
13 AC_REQUIRE([AC_CANONICAL_HOST])
14 GETADDRINFO_LIB=
15 gai_saved_LIBS="$LIBS"
16
17 dnl Where is getaddrinfo()?
18 dnl - On Solaris, it is in libsocket.
19 dnl - On Haiku, it is in libnetwork.
20 dnl - On BeOS, it is in libnet.
21 dnl - On native Windows, it is in ws2_32.dll.
22 dnl - Otherwise it is in libc.
23 AC_SEARCH_LIBS([getaddrinfo], [socket network net],
24 [if test "$ac_cv_search_getaddrinfo" != "none required"; then
25 GETADDRINFO_LIB="$ac_cv_search_getaddrinfo"
26 fi])
27 LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
28
29 HAVE_GETADDRINFO=1
30 AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [
31 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_NETDB_H
37 #include <netdb.h>
38 #endif
39 #include <stddef.h>
40 ]], [[getaddrinfo("", "", NULL, NULL);]])],
41 [gl_cv_func_getaddrinfo=yes],
42 [gl_cv_func_getaddrinfo=no])])
43 if test $gl_cv_func_getaddrinfo = no; then
44 AC_CACHE_CHECK([for getaddrinfo in ws2tcpip.h and -lws2_32],
45 gl_cv_w32_getaddrinfo, [
46 gl_cv_w32_getaddrinfo=no
47 gl_saved_LIBS="$LIBS"
48 LIBS="$LIBS -lws2_32"
49 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
50 #ifdef HAVE_WS2TCPIP_H
51 #include <ws2tcpip.h>
52 #endif
53 #include <stddef.h>
54 ]], [[getaddrinfo(NULL, NULL, NULL, NULL);]])], [gl_cv_w32_getaddrinfo=yes])
55 LIBS="$gl_saved_LIBS"
56 ])
57 if test "$gl_cv_w32_getaddrinfo" = "yes"; then
58 GETADDRINFO_LIB="-lws2_32"
59 LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
60 dnl Check for correct signature, in particular for a cdecl-compatible
61 dnl calling convention.
62 AC_CACHE_CHECK([for getaddrinfo with POSIX signature],
63 [gl_cv_func_getaddrinfo_posix_signature],
64 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
65 #include <sys/types.h>
66 #ifdef HAVE_SYS_SOCKET_H
67 #include <sys/socket.h>
68 #endif
69 #ifdef HAVE_NETDB_H
70 #include <netdb.h>
71 #endif
72 #ifdef HAVE_WS2TCPIP_H
73 #include <ws2tcpip.h>
74 #endif
75 #include <stddef.h>
76 extern
77 #ifdef __cplusplus
78 "C"
79 #endif
80 int getaddrinfo (const char *, const char *, const struct addrinfo *, struct addrinfo **);
81 ]])],
82 [gl_cv_func_getaddrinfo_posix_signature=yes],
83 [gl_cv_func_getaddrinfo_posix_signature=no])
84 ])
85 if test $gl_cv_func_getaddrinfo_posix_signature = no; then
86 REPLACE_GETADDRINFO=1
87 fi
88 else
89 HAVE_GETADDRINFO=0
90 fi
91 fi
92 if test $HAVE_GETADDRINFO != 0; then
93 AC_CACHE_CHECK([whether getaddrinfo supports AI_NUMERICHOST and AI_NUMERICSERV],
94 [gl_cv_func_getaddrinfo_works],
95 [AC_RUN_IFELSE(
96 [AC_LANG_PROGRAM([[
97 #include <sys/types.h>
98 #ifdef HAVE_SYS_SOCKET_H
99 #include <sys/socket.h>
100 #endif
101 #ifdef HAVE_NETDB_H
102 #include <netdb.h>
103 #endif
104 #ifdef HAVE_WS2TCPIP_H
105 #include <ws2tcpip.h>
106 #endif
107 #include <stddef.h>
108 #include <string.h>
109 ]], [[
110 int result = 0;
111 struct addrinfo *ai;
112 {
113 struct addrinfo hints;
114 memset (&hints, 0, sizeof (hints));
115 hints.ai_flags = AI_NUMERICHOST;
116 if (getaddrinfo ("www.gnu.org", "http", &hints, &ai) != EAI_NONAME)
117 result |= 1;
118 }
119 {
120 struct addrinfo hints;
121 memset (&hints, 0, sizeof (hints));
122 hints.ai_flags = AI_NUMERICSERV;
123 if (getaddrinfo ("www.gnu.org", "http", &hints, &ai) != EAI_NONAME)
124 result |= 2;
125 }
126 return result;
127 ]])
128 ],
129 [gl_cv_func_getaddrinfo_works=yes],
130 [gl_cv_func_getaddrinfo_works=no],
131 [case "$host_os" in
132 # Guess no on Solaris.
133 solaris*) gl_cv_func_getaddrinfo_works="guessing no" ;;
134 # Guess no on native Windows.
135 mingw* | windows*) gl_cv_func_getaddrinfo_works="guessing no" ;;
136 # Guess yes otherwise.
137 *) gl_cv_func_getaddrinfo_works="guessing yes" ;;
138 esac
139 ])
140 ])
141 case "$gl_cv_func_getaddrinfo_works" in
142 *yes) ;;
143 *) REPLACE_GETADDRINFO=1 ;;
144 esac
145 fi
146 AC_DEFINE_UNQUOTED([HAVE_GETADDRINFO], [$HAVE_GETADDRINFO],
147 [Define to 1 if getaddrinfo exists, or to 0 otherwise.])
148
149 # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
150 # inline function declared in ws2tcpip.h, so we need to get that
151 # header included somehow.
152 AC_CHECK_DECLS([gai_strerror], [], [], [[
153 #include <sys/types.h>
154 #ifdef HAVE_SYS_SOCKET_H
155 #include <sys/socket.h>
156 #endif
157 #ifdef HAVE_NETDB_H
158 #include <netdb.h>
159 #endif
160 #ifdef HAVE_WS2TCPIP_H
161 #include <ws2tcpip.h>
162 #endif
163 #include <stddef.h>
164 ]])
165 if test $ac_cv_have_decl_gai_strerror = yes; then
166 AC_CHECK_DECLS([gai_strerrorA], [], [], [[
167 #include <sys/types.h>
168 #ifdef HAVE_SYS_SOCKET_H
169 #include <sys/socket.h>
170 #endif
171 #ifdef HAVE_NETDB_H
172 #include <netdb.h>
173 #endif
174 #ifdef HAVE_WS2TCPIP_H
175 #include <ws2tcpip.h>
176 #endif
177 #include <stddef.h>
178 ]])
179 dnl check for correct signature
180 AC_CACHE_CHECK([for gai_strerror with POSIX signature],
181 [gl_cv_func_gai_strerror_posix_signature], [
182 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
183 #include <sys/types.h>
184 #ifdef HAVE_SYS_SOCKET_H
185 #include <sys/socket.h>
186 #endif
187 #ifdef HAVE_NETDB_H
188 #include <netdb.h>
189 #endif
190 #ifdef HAVE_WS2TCPIP_H
191 #include <ws2tcpip.h>
192 #endif
193 #include <stddef.h>
194 extern
195 #ifdef __cplusplus
196 "C"
197 #endif
198 const char *gai_strerror(int);]])],
199 [gl_cv_func_gai_strerror_posix_signature=yes],
200 [gl_cv_func_gai_strerror_posix_signature=no])])
201 if test $gl_cv_func_gai_strerror_posix_signature = no; then
202 REPLACE_GAI_STRERROR=1
203 fi
204 fi
205
206 LIBS="$gai_saved_LIBS"
207
208 gl_PREREQ_GETADDRINFO
209
210 AC_SUBST([GETADDRINFO_LIB])
211 ])
212
213 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
214 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
215 AC_REQUIRE([gl_NETDB_H_DEFAULTS])
216 AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
217 AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
218 AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
219 AC_REQUIRE([gl_FUNC_INET_NTOP]) dnl for INET_NTOP_LIB
220 AC_REQUIRE([AC_C_RESTRICT])
221 AC_REQUIRE([gl_SOCKET_FAMILIES])
222 AC_REQUIRE([gl_SYS_SOCKET_H])
223 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
224
225 dnl Including sys/socket.h is wrong for Windows, but Windows does not
226 dnl have sa_len so the result is correct anyway.
227 AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
228 #include <sys/types.h>
229 #include <sys/socket.h>
230 ])
231
232 AC_CHECK_HEADERS_ONCE([netinet/in.h])
233
234 AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, getnameinfo],,,[[
235 /* sys/types.h is not needed according to POSIX, but the
236 sys/socket.h in i386-unknown-freebsd4.10 and
237 powerpc-apple-darwin5.5 required it. */
238 #include <sys/types.h>
239 #ifdef HAVE_SYS_SOCKET_H
240 #include <sys/socket.h>
241 #endif
242 #ifdef HAVE_NETDB_H
243 #include <netdb.h>
244 #endif
245 #ifdef HAVE_WS2TCPIP_H
246 #include <ws2tcpip.h>
247 #endif
248 ]])
249 if test $ac_cv_have_decl_getaddrinfo = no; then
250 HAVE_DECL_GETADDRINFO=0
251 fi
252 if test $ac_cv_have_decl_freeaddrinfo = no; then
253 HAVE_DECL_FREEADDRINFO=0
254 fi
255 if test $ac_cv_have_decl_gai_strerror = no; then
256 HAVE_DECL_GAI_STRERROR=0
257 fi
258 if test $ac_cv_have_decl_getnameinfo = no; then
259 HAVE_DECL_GETNAMEINFO=0
260 fi
261
262 AC_CHECK_TYPES([struct addrinfo],,,[
263 #include <sys/types.h>
264 #ifdef HAVE_SYS_SOCKET_H
265 #include <sys/socket.h>
266 #endif
267 #ifdef HAVE_NETDB_H
268 #include <netdb.h>
269 #endif
270 #ifdef HAVE_WS2TCPIP_H
271 #include <ws2tcpip.h>
272 #endif
273 ])
274 if test $ac_cv_type_struct_addrinfo = no; then
275 HAVE_STRUCT_ADDRINFO=0
276 fi
277
278 dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
279 case " $GETADDRINFO_LIB " in
280 *" $HOSTENT_LIB "*) ;;
281 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
282 esac
283
284 dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
285 case " $GETADDRINFO_LIB " in
286 *" $SERVENT_LIB "*) ;;
287 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
288 esac
289
290 dnl Append $INET_NTOP_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
291 case " $GETADDRINFO_LIB " in
292 *" $INET_NTOP_LIB "*) ;;
293 *) GETADDRINFO_LIB="$GETADDRINFO_LIB $INET_NTOP_LIB" ;;
294 esac
295 ])