4fecfb2fa357db020f25b12aefcba9985d435fbd
[gnulib.git] / m4 / getaddrinfo.m4
1 # getaddrinfo.m4 serial 27
2 dnl Copyright (C) 2004-2011 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.
6
7 AC_DEFUN([gl_GETADDRINFO],
8 [
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   AC_MSG_NOTICE([checking how to do getaddrinfo, freeaddrinfo and getnameinfo])
12   GETADDRINFO_LIB=
13   gai_saved_LIBS="$LIBS"
14
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"
26
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       am_save_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="$am_save_LIBS"
54     ])
55     if test "$gl_cv_w32_getaddrinfo" = "yes"; then
56       GETADDRINFO_LIB="-lws2_32"
57       LIBS="$gai_saved_LIBS $GETADDRINFO_LIB"
58     else
59       HAVE_GETADDRINFO=0
60     fi
61   fi
62
63   # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
64   # inline function declared in ws2tcpip.h, so we need to get that
65   # header included somehow.
66   AC_CHECK_DECLS([gai_strerror, gai_strerrorA], [], [break], [[
67 #include <sys/types.h>
68 #ifdef HAVE_SYS_SOCKET_H
69 #include <sys/socket.h>
70 #endif
71 #ifdef HAVE_NETDB_H
72 #include <netdb.h>
73 #endif
74 #ifdef HAVE_WS2TCPIP_H
75 #include <ws2tcpip.h>
76 #endif
77 #include <stddef.h>
78 ]])
79   if test $ac_cv_have_decl_gai_strerror = yes; then
80     dnl check for correct signature
81     AC_CACHE_CHECK([for gai_strerror with POSIX signature],
82      [gl_cv_func_gai_strerror_posix_signature], [
83       AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
84 #include <sys/types.h>
85 #ifdef HAVE_SYS_SOCKET_H
86 #include <sys/socket.h>
87 #endif
88 #ifdef HAVE_NETDB_H
89 #include <netdb.h>
90 #endif
91 #ifdef HAVE_WS2TCPIP_H
92 #include <ws2tcpip.h>
93 #endif
94 #include <stddef.h>
95 extern
96 #ifdef __cplusplus
97 "C"
98 #endif
99 const char *gai_strerror(int);]])],
100         [gl_cv_func_gai_strerror_posix_signature=yes],
101         [gl_cv_func_gai_strerror_posix_signature=no])])
102     if test $gl_cv_func_gai_strerror_posix_signature = no; then
103       REPLACE_GAI_STRERROR=1
104     fi
105   fi
106
107   LIBS="$gai_saved_LIBS"
108
109   gl_PREREQ_GETADDRINFO
110
111   AC_SUBST([GETADDRINFO_LIB])
112 ])
113
114 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
115 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
116   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
117   AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
118   AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
119   AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
120   AC_REQUIRE([gl_FUNC_INET_NTOP]) dnl for INET_NTOP_LIB
121   AC_REQUIRE([AC_C_RESTRICT])
122   AC_REQUIRE([gl_SOCKET_FAMILIES])
123   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
124   AC_REQUIRE([AC_C_INLINE])
125   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
126
127   dnl Including sys/socket.h is wrong for Windows, but Windows does not
128   dnl have sa_len so the result is correct anyway.
129   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
130 #include <sys/types.h>
131 #include <sys/socket.h>
132 ])
133
134   AC_CHECK_HEADERS_ONCE([netinet/in.h])
135
136   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, getnameinfo],,,[
137   /* sys/types.h is not needed according to POSIX, but the
138      sys/socket.h in i386-unknown-freebsd4.10 and
139      powerpc-apple-darwin5.5 required it. */
140 #include <sys/types.h>
141 #ifdef HAVE_SYS_SOCKET_H
142 #include <sys/socket.h>
143 #endif
144 #ifdef HAVE_NETDB_H
145 #include <netdb.h>
146 #endif
147 #ifdef HAVE_WS2TCPIP_H
148 #include <ws2tcpip.h>
149 #endif
150 ])
151   if test $ac_cv_have_decl_getaddrinfo = no; then
152     HAVE_DECL_GETADDRINFO=0
153   fi
154   if test $ac_cv_have_decl_freeaddrinfo = no; then
155     HAVE_DECL_FREEADDRINFO=0
156   fi
157   if test $ac_cv_have_decl_gai_strerror = no; then
158     HAVE_DECL_GAI_STRERROR=0
159   fi
160   if test $ac_cv_have_decl_getnameinfo = no; then
161     HAVE_DECL_GETNAMEINFO=0
162   fi
163
164   AC_CHECK_TYPES([struct addrinfo],,,[
165 #include <sys/types.h>
166 #ifdef HAVE_SYS_SOCKET_H
167 #include <sys/socket.h>
168 #endif
169 #ifdef HAVE_NETDB_H
170 #include <netdb.h>
171 #endif
172 #ifdef HAVE_WS2TCPIP_H
173 #include <ws2tcpip.h>
174 #endif
175 ])
176   if test $ac_cv_type_struct_addrinfo = no; then
177     HAVE_STRUCT_ADDRINFO=0
178   fi
179
180   dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
181   case " $GETADDRINFO_LIB " in
182     *" $HOSTENT_LIB "*) ;;
183     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
184   esac
185
186   dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
187   case " $GETADDRINFO_LIB " in
188     *" $SERVENT_LIB "*) ;;
189     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
190   esac
191
192   dnl Append $INET_NTOP_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
193   case " $GETADDRINFO_LIB " in
194     *" $INET_NTOP_LIB "*) ;;
195     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $INET_NTOP_LIB" ;;
196   esac
197 ])