Use substituted macros instead of C preprocessor macros.
[gnulib.git] / m4 / getaddrinfo.m4
1 # getaddrinfo.m4 serial 17
2 dnl Copyright (C) 2004, 2005, 2006, 2007, 2008 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
13   AC_SEARCH_LIBS(getaddrinfo, [nsl socket])
14   AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [
15     AC_TRY_LINK([
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
19 #endif
20 #ifdef HAVE_NETDB_H
21 #include <netdb.h>
22 #endif
23 #include <stddef.h>
24 ], [getaddrinfo("", "", NULL, NULL);],
25       [gl_cv_func_getaddrinfo=yes],
26       [gl_cv_func_getaddrinfo=no])])
27   if test $gl_cv_func_getaddrinfo = no; then
28     AC_CACHE_CHECK(for getaddrinfo in ws2tcpip.h and -lws2_32,
29                    gl_cv_w32_getaddrinfo, [
30       gl_cv_w32_getaddrinfo=no
31       am_save_LIBS="$LIBS"
32       LIBS="$LIBS -lws2_32"
33       AC_TRY_LINK([
34 #ifdef HAVE_WS2TCPIP_H
35 #include <ws2tcpip.h>
36 #endif
37 #include <stddef.h>
38 ], [getaddrinfo(NULL, NULL, NULL, NULL);], gl_cv_w32_getaddrinfo=yes)
39     LIBS="$am_save_LIBS"])
40     if test "$gl_cv_w32_getaddrinfo" = "yes"; then
41       LIBS="$LIBS -lws2_32"
42     else
43       AC_LIBOBJ(getaddrinfo)
44     fi
45   fi
46
47   # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
48   # inline function declared in ws2tcpip.h, so we need to get that
49   # header included somehow.
50   AC_CACHE_CHECK([for gai_strerror (possibly via ws2tcpip.h)],
51     gl_cv_func_gai_strerror, [
52       AC_TRY_LINK([
53 #include <sys/types.h>
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_NETDB_H
58 #include <netdb.h>
59 #endif
60 #ifdef HAVE_WS2TCPIP_H
61 #include <ws2tcpip.h>
62 #endif
63 #include <stddef.h>
64 ], [gai_strerror (NULL);],
65         [gl_cv_func_gai_strerror=yes],
66         [gl_cv_func_gai_strerror=no])])
67   if test $gl_cv_func_gai_strerror = no; then
68     AC_LIBOBJ(gai_strerror)
69   fi
70
71   gl_PREREQ_GETADDRINFO
72 ])
73
74 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
75 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
76   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
77   AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
78   AC_SEARCH_LIBS(gethostbyname, [inet nsl])
79   AC_SEARCH_LIBS(getservbyname, [inet nsl socket xnet])
80   AC_CHECK_FUNCS(gethostbyname,, [
81     AC_CACHE_CHECK(for gethostbyname in winsock2.h and -lws2_32,
82                    gl_cv_w32_gethostbyname, [
83       gl_cv_w32_gethostbyname=no
84       am_save_LIBS="$LIBS"
85       LIBS="$LIBS -lws2_32"
86       AC_TRY_LINK([
87 #ifdef HAVE_WINSOCK2_H
88 #include <winsock2.h>
89 #endif
90 #include <stddef.h>
91 ], [gethostbyname(NULL);], gl_cv_w32_gethostbyname=yes)
92     LIBS="$am_save_LIBS"])
93     if test "$gl_cv_w32_gethostbyname" = "yes"; then
94       LIBS="$LIBS -lws2_32"
95     fi
96     ])
97   AC_REQUIRE([AC_C_RESTRICT])
98   AC_REQUIRE([gl_SOCKET_FAMILIES])
99   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
100   AC_REQUIRE([AC_C_INLINE])
101   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
102
103   dnl Including sys/socket.h is wrong for Windows, but Windows does not
104   dnl have sa_len so the result is correct anyway.
105   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [#include <sys/socket.h>])
106
107   AC_CHECK_HEADERS_ONCE(netinet/in.h)
108
109   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, gai_strerror, getnameinfo],,,[
110   /* sys/types.h is not needed according to POSIX, but the
111      sys/socket.h in i386-unknown-freebsd4.10 and
112      powerpc-apple-darwin5.5 required it. */
113 #include <sys/types.h>
114 #ifdef HAVE_SYS_SOCKET_H
115 #include <sys/socket.h>
116 #endif
117 #ifdef HAVE_NETDB_H
118 #include <netdb.h>
119 #endif
120 #ifdef HAVE_WS2TCPIP_H
121 #include <ws2tcpip.h>
122 #endif
123 ])
124   if test $ac_cv_have_decl_getaddrinfo = no; then
125     HAVE_DECL_GETADDRINFO=0
126   fi
127   if test $ac_cv_have_decl_freeaddrinfo = no; then
128     HAVE_DECL_FREEADDRINFO=0
129   fi
130   if test $ac_cv_have_decl_gai_strerror = no; then
131     HAVE_DECL_GAI_STRERROR=0
132   fi
133   if test $ac_cv_have_decl_getnameinfo = no; then
134     HAVE_DECL_GETNAMEINFO=0
135   fi
136
137   AC_CHECK_TYPES([struct addrinfo],,,[
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_type_struct_addrinfo = no; then
150     HAVE_STRUCT_ADDRINFO=0
151   fi
152 ])