git-merge-changelog: Simplify installation instructions.
[gnulib.git] / m4 / getaddrinfo.m4
blob8e20917782b5436328df5ad7fa8e2a17370b3ca0
1 # getaddrinfo.m4
2 # serial 35
3 dnl Copyright (C) 2004-2024 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.
8 AC_DEFUN([gl_GETADDRINFO],
10   AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
11   AC_REQUIRE([gl_NETDB_H])dnl for HAVE_NETDB_H
12   GETADDRINFO_LIB=
13   gai_saved_LIBS="$LIBS"
15   dnl Where is getaddrinfo()?
16   dnl - On Solaris, it is in libsocket.
17   dnl - On Haiku, it is in libnetwork.
18   dnl - On BeOS, it is in libnet.
19   dnl - On native Windows, it is in ws2_32.dll.
20   dnl - Otherwise it is in libc.
21   AC_SEARCH_LIBS([getaddrinfo], [socket network net],
22     [if test "$ac_cv_search_getaddrinfo" != "none required"; then
23        GETADDRINFO_LIB="$ac_cv_search_getaddrinfo"
24      fi])
25   LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
27   HAVE_GETADDRINFO=1
28   AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [
29     AC_LINK_IFELSE([AC_LANG_PROGRAM([[
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34 #ifdef HAVE_NETDB_H
35 #include <netdb.h>
36 #endif
37 #include <stddef.h>
38 ]], [[getaddrinfo("", "", NULL, NULL);]])],
39       [gl_cv_func_getaddrinfo=yes],
40       [gl_cv_func_getaddrinfo=no])])
41   if test $gl_cv_func_getaddrinfo = no; then
42     AC_CACHE_CHECK([for getaddrinfo in ws2tcpip.h and -lws2_32],
43                    gl_cv_w32_getaddrinfo, [
44       gl_cv_w32_getaddrinfo=no
45       gl_saved_LIBS="$LIBS"
46       LIBS="$LIBS -lws2_32"
47       AC_LINK_IFELSE([AC_LANG_PROGRAM([[
48 #ifdef HAVE_WS2TCPIP_H
49 #include <ws2tcpip.h>
50 #endif
51 #include <stddef.h>
52 ]], [[getaddrinfo(NULL, NULL, NULL, NULL);]])], [gl_cv_w32_getaddrinfo=yes])
53       LIBS="$gl_saved_LIBS"
54     ])
55     if test "$gl_cv_w32_getaddrinfo" = "yes"; then
56       GETADDRINFO_LIB="-lws2_32"
57       LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
58       dnl Check for correct signature, in particular for a cdecl-compatible
59       dnl calling convention.
60       AC_CACHE_CHECK([for getaddrinfo with POSIX signature],
61         [gl_cv_func_getaddrinfo_posix_signature],
62         [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
63 #include <sys/types.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
66 #endif
67 #ifdef HAVE_NETDB_H
68 #include <netdb.h>
69 #endif
70 #ifdef HAVE_WS2TCPIP_H
71 #include <ws2tcpip.h>
72 #endif
73 #include <stddef.h>
74 extern
75 #ifdef __cplusplus
76 "C"
77 #endif
78 int getaddrinfo (const char *, const char *, const struct addrinfo *, struct addrinfo **);
79 ]])],
80            [gl_cv_func_getaddrinfo_posix_signature=yes],
81            [gl_cv_func_getaddrinfo_posix_signature=no])
82         ])
83       if test $gl_cv_func_getaddrinfo_posix_signature = no; then
84         REPLACE_GETADDRINFO=1
85       fi
86     else
87       HAVE_GETADDRINFO=0
88     fi
89   fi
90   AC_DEFINE_UNQUOTED([HAVE_GETADDRINFO], [$HAVE_GETADDRINFO],
91     [Define to 1 if getaddrinfo exists, or to 0 otherwise.])
93   # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
94   # inline function declared in ws2tcpip.h, so we need to get that
95   # header included somehow.
96   AC_CHECK_DECLS([gai_strerror], [], [], [[
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>
109   if test $ac_cv_have_decl_gai_strerror = yes; then
110     AC_CHECK_DECLS([gai_strerrorA], [], [], [[
111 #include <sys/types.h>
112 #ifdef HAVE_SYS_SOCKET_H
113 #include <sys/socket.h>
114 #endif
115 #ifdef HAVE_NETDB_H
116 #include <netdb.h>
117 #endif
118 #ifdef HAVE_WS2TCPIP_H
119 #include <ws2tcpip.h>
120 #endif
121 #include <stddef.h>
123     dnl check for correct signature
124     AC_CACHE_CHECK([for gai_strerror with POSIX signature],
125      [gl_cv_func_gai_strerror_posix_signature], [
126       AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
127 #include <sys/types.h>
128 #ifdef HAVE_SYS_SOCKET_H
129 #include <sys/socket.h>
130 #endif
131 #ifdef HAVE_NETDB_H
132 #include <netdb.h>
133 #endif
134 #ifdef HAVE_WS2TCPIP_H
135 #include <ws2tcpip.h>
136 #endif
137 #include <stddef.h>
138 extern
139 #ifdef __cplusplus
141 #endif
142 const char *gai_strerror(int);]])],
143         [gl_cv_func_gai_strerror_posix_signature=yes],
144         [gl_cv_func_gai_strerror_posix_signature=no])])
145     if test $gl_cv_func_gai_strerror_posix_signature = no; then
146       REPLACE_GAI_STRERROR=1
147     fi
148   fi
150   LIBS="$gai_saved_LIBS"
152   gl_PREREQ_GETADDRINFO
154   AC_SUBST([GETADDRINFO_LIB])
157 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
158 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
159   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
160   AC_REQUIRE([gl_SYS_SOCKET_H])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
161   AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
162   AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
163   AC_REQUIRE([gl_FUNC_INET_NTOP]) dnl for INET_NTOP_LIB
164   AC_REQUIRE([AC_C_RESTRICT])
165   AC_REQUIRE([gl_SOCKET_FAMILIES])
166   AC_REQUIRE([gl_SYS_SOCKET_H])
167   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
169   dnl Including sys/socket.h is wrong for Windows, but Windows does not
170   dnl have sa_len so the result is correct anyway.
171   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
172 #include <sys/types.h>
173 #include <sys/socket.h>
176   AC_CHECK_HEADERS_ONCE([netinet/in.h])
178   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, getnameinfo],,,[[
179   /* sys/types.h is not needed according to POSIX, but the
180      sys/socket.h in i386-unknown-freebsd4.10 and
181      powerpc-apple-darwin5.5 required it. */
182 #include <sys/types.h>
183 #ifdef HAVE_SYS_SOCKET_H
184 #include <sys/socket.h>
185 #endif
186 #ifdef HAVE_NETDB_H
187 #include <netdb.h>
188 #endif
189 #ifdef HAVE_WS2TCPIP_H
190 #include <ws2tcpip.h>
191 #endif
193   if test $ac_cv_have_decl_getaddrinfo = no; then
194     HAVE_DECL_GETADDRINFO=0
195   fi
196   if test $ac_cv_have_decl_freeaddrinfo = no; then
197     HAVE_DECL_FREEADDRINFO=0
198   fi
199   if test $ac_cv_have_decl_gai_strerror = no; then
200     HAVE_DECL_GAI_STRERROR=0
201   fi
202   if test $ac_cv_have_decl_getnameinfo = no; then
203     HAVE_DECL_GETNAMEINFO=0
204   fi
206   AC_CHECK_TYPES([struct addrinfo],,,[
207 #include <sys/types.h>
208 #ifdef HAVE_SYS_SOCKET_H
209 #include <sys/socket.h>
210 #endif
211 #ifdef HAVE_NETDB_H
212 #include <netdb.h>
213 #endif
214 #ifdef HAVE_WS2TCPIP_H
215 #include <ws2tcpip.h>
216 #endif
218   if test $ac_cv_type_struct_addrinfo = no; then
219     HAVE_STRUCT_ADDRINFO=0
220   fi
222   dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
223   case " $GETADDRINFO_LIB " in
224     *" $HOSTENT_LIB "*) ;;
225     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
226   esac
228   dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
229   case " $GETADDRINFO_LIB " in
230     *" $SERVENT_LIB "*) ;;
231     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
232   esac
234   dnl Append $INET_NTOP_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
235   case " $GETADDRINFO_LIB " in
236     *" $INET_NTOP_LIB "*) ;;
237     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $INET_NTOP_LIB" ;;
238   esac