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