20a62261189acfceec4651ed8a8d762cf6f3c071
[gnulib.git] / m4 / getaddrinfo.m4
1 # getaddrinfo.m4 serial 26
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 const char *gai_strerror(int);]])],
96         [gl_cv_func_gai_strerror_posix_signature=yes],
97         [gl_cv_func_gai_strerror_posix_signature=no])])
98     if test $gl_cv_func_gai_strerror_posix_signature = no; then
99       REPLACE_GAI_STRERROR=1
100     fi
101   fi
102
103   LIBS="$gai_saved_LIBS"
104
105   gl_PREREQ_GETADDRINFO
106
107   AC_SUBST([GETADDRINFO_LIB])
108 ])
109
110 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
111 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
112   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
113   AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
114   AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
115   AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
116   AC_REQUIRE([gl_FUNC_INET_NTOP]) dnl for INET_NTOP_LIB
117   AC_REQUIRE([AC_C_RESTRICT])
118   AC_REQUIRE([gl_SOCKET_FAMILIES])
119   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
120   AC_REQUIRE([AC_C_INLINE])
121   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
122
123   dnl Including sys/socket.h is wrong for Windows, but Windows does not
124   dnl have sa_len so the result is correct anyway.
125   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
126 #include <sys/types.h>
127 #include <sys/socket.h>
128 ])
129
130   AC_CHECK_HEADERS_ONCE([netinet/in.h])
131
132   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, getnameinfo],,,[
133   /* sys/types.h is not needed according to POSIX, but the
134      sys/socket.h in i386-unknown-freebsd4.10 and
135      powerpc-apple-darwin5.5 required it. */
136 #include <sys/types.h>
137 #ifdef HAVE_SYS_SOCKET_H
138 #include <sys/socket.h>
139 #endif
140 #ifdef HAVE_NETDB_H
141 #include <netdb.h>
142 #endif
143 #ifdef HAVE_WS2TCPIP_H
144 #include <ws2tcpip.h>
145 #endif
146 ])
147   if test $ac_cv_have_decl_getaddrinfo = no; then
148     HAVE_DECL_GETADDRINFO=0
149   fi
150   if test $ac_cv_have_decl_freeaddrinfo = no; then
151     HAVE_DECL_FREEADDRINFO=0
152   fi
153   if test $ac_cv_have_decl_gai_strerror = no; then
154     HAVE_DECL_GAI_STRERROR=0
155   fi
156   if test $ac_cv_have_decl_getnameinfo = no; then
157     HAVE_DECL_GETNAMEINFO=0
158   fi
159
160   AC_CHECK_TYPES([struct addrinfo],,,[
161 #include <sys/types.h>
162 #ifdef HAVE_SYS_SOCKET_H
163 #include <sys/socket.h>
164 #endif
165 #ifdef HAVE_NETDB_H
166 #include <netdb.h>
167 #endif
168 #ifdef HAVE_WS2TCPIP_H
169 #include <ws2tcpip.h>
170 #endif
171 ])
172   if test $ac_cv_type_struct_addrinfo = no; then
173     HAVE_STRUCT_ADDRINFO=0
174   fi
175
176   dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
177   case " $GETADDRINFO_LIB " in
178     *" $HOSTENT_LIB "*) ;;
179     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
180   esac
181
182   dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
183   case " $GETADDRINFO_LIB " in
184     *" $SERVENT_LIB "*) ;;
185     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
186   esac
187
188   dnl Append $INET_NTOP_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
189   case " $GETADDRINFO_LIB " in
190     *" $INET_NTOP_LIB "*) ;;
191     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $INET_NTOP_LIB" ;;
192   esac
193 ])