Fix indentation.
[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     ])
41     if test "$gl_cv_w32_getaddrinfo" = "yes"; then
42       LIBS="$LIBS -lws2_32"
43     else
44       AC_LIBOBJ(getaddrinfo)
45     fi
46   fi
47
48   # We can't use AC_REPLACE_FUNCS here because gai_strerror may be an
49   # inline function declared in ws2tcpip.h, so we need to get that
50   # header included somehow.
51   AC_CACHE_CHECK([for gai_strerror (possibly via ws2tcpip.h)],
52     gl_cv_func_gai_strerror, [
53       AC_TRY_LINK([
54 #include <sys/types.h>
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
57 #endif
58 #ifdef HAVE_NETDB_H
59 #include <netdb.h>
60 #endif
61 #ifdef HAVE_WS2TCPIP_H
62 #include <ws2tcpip.h>
63 #endif
64 #include <stddef.h>
65 ], [gai_strerror (NULL);],
66         [gl_cv_func_gai_strerror=yes],
67         [gl_cv_func_gai_strerror=no])])
68   if test $gl_cv_func_gai_strerror = no; then
69     AC_LIBOBJ(gai_strerror)
70   fi
71
72   gl_PREREQ_GETADDRINFO
73 ])
74
75 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
76 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
77   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
78   AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
79   AC_SEARCH_LIBS(gethostbyname, [inet nsl])
80   AC_SEARCH_LIBS(getservbyname, [inet nsl socket xnet])
81   AC_CHECK_FUNCS(gethostbyname,, [
82     AC_CACHE_CHECK(for gethostbyname in winsock2.h and -lws2_32,
83                    gl_cv_w32_gethostbyname, [
84       gl_cv_w32_gethostbyname=no
85       am_save_LIBS="$LIBS"
86       LIBS="$LIBS -lws2_32"
87       AC_TRY_LINK([
88 #ifdef HAVE_WINSOCK2_H
89 #include <winsock2.h>
90 #endif
91 #include <stddef.h>
92 ], [gethostbyname(NULL);], gl_cv_w32_gethostbyname=yes)
93     LIBS="$am_save_LIBS"])
94     if test "$gl_cv_w32_gethostbyname" = "yes"; then
95       LIBS="$LIBS -lws2_32"
96     fi
97     ])
98   AC_REQUIRE([AC_C_RESTRICT])
99   AC_REQUIRE([gl_SOCKET_FAMILIES])
100   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
101   AC_REQUIRE([AC_C_INLINE])
102   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
103
104   dnl Including sys/socket.h is wrong for Windows, but Windows does not
105   dnl have sa_len so the result is correct anyway.
106   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [#include <sys/socket.h>])
107
108   AC_CHECK_HEADERS_ONCE(netinet/in.h)
109
110   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, gai_strerror, getnameinfo],,,[
111   /* sys/types.h is not needed according to POSIX, but the
112      sys/socket.h in i386-unknown-freebsd4.10 and
113      powerpc-apple-darwin5.5 required it. */
114 #include <sys/types.h>
115 #ifdef HAVE_SYS_SOCKET_H
116 #include <sys/socket.h>
117 #endif
118 #ifdef HAVE_NETDB_H
119 #include <netdb.h>
120 #endif
121 #ifdef HAVE_WS2TCPIP_H
122 #include <ws2tcpip.h>
123 #endif
124 ])
125   if test $ac_cv_have_decl_getaddrinfo = no; then
126     HAVE_DECL_GETADDRINFO=0
127   fi
128   if test $ac_cv_have_decl_freeaddrinfo = no; then
129     HAVE_DECL_FREEADDRINFO=0
130   fi
131   if test $ac_cv_have_decl_gai_strerror = no; then
132     HAVE_DECL_GAI_STRERROR=0
133   fi
134   if test $ac_cv_have_decl_getnameinfo = no; then
135     HAVE_DECL_GETNAMEINFO=0
136   fi
137
138   AC_CHECK_TYPES([struct addrinfo],,,[
139 #include <sys/types.h>
140 #ifdef HAVE_SYS_SOCKET_H
141 #include <sys/socket.h>
142 #endif
143 #ifdef HAVE_NETDB_H
144 #include <netdb.h>
145 #endif
146 #ifdef HAVE_WS2TCPIP_H
147 #include <ws2tcpip.h>
148 #endif
149 ])
150   if test $ac_cv_type_struct_addrinfo = no; then
151     HAVE_STRUCT_ADDRINFO=0
152   fi
153 ])