Code

4088671987ade01fe1e50ca0e21dd269c6856306
[nagiosplug.git] / gl / m4 / getaddrinfo.m4
1 # getaddrinfo.m4 serial 20
2 dnl Copyright (C) 2004-2009 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.
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"
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"
27   AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [
28     AC_TRY_LINK([
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_TRY_LINK([
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
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_CACHE_CHECK([for gai_strerror (possibly via ws2tcpip.h)],
66     gl_cv_func_gai_strerror, [
67       AC_TRY_LINK([
68 #include <sys/types.h>
69 #ifdef HAVE_SYS_SOCKET_H
70 #include <sys/socket.h>
71 #endif
72 #ifdef HAVE_NETDB_H
73 #include <netdb.h>
74 #endif
75 #ifdef HAVE_WS2TCPIP_H
76 #include <ws2tcpip.h>
77 #endif
78 #include <stddef.h>
79 ], [gai_strerror (NULL);],
80         [gl_cv_func_gai_strerror=yes],
81         [gl_cv_func_gai_strerror=no])])
82   if test $gl_cv_func_gai_strerror = no; then
83     AC_LIBOBJ([gai_strerror])
84   fi
86   LIBS="$gai_saved_LIBS"
88   gl_PREREQ_GETADDRINFO
90   AC_SUBST([GETADDRINFO_LIB])
91 ])
93 # Prerequisites of lib/netdb.in.h and lib/getaddrinfo.c.
94 AC_DEFUN([gl_PREREQ_GETADDRINFO], [
95   AC_REQUIRE([gl_NETDB_H_DEFAULTS])
96   AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
97   AC_REQUIRE([gl_HOSTENT]) dnl for HOSTENT_LIB
98   AC_REQUIRE([gl_SERVENT]) dnl for SERVENT_LIB
99   AC_REQUIRE([AC_C_RESTRICT])
100   AC_REQUIRE([gl_SOCKET_FAMILIES])
101   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
102   AC_REQUIRE([AC_C_INLINE])
103   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
105   dnl Including sys/socket.h is wrong for Windows, but Windows does not
106   dnl have sa_len so the result is correct anyway.
107   AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [#include <sys/socket.h>])
109   AC_CHECK_HEADERS_ONCE([netinet/in.h])
111   AC_CHECK_DECLS([getaddrinfo, freeaddrinfo, gai_strerror, getnameinfo],,,[
112   /* sys/types.h is not needed according to POSIX, but the
113      sys/socket.h in i386-unknown-freebsd4.10 and
114      powerpc-apple-darwin5.5 required it. */
115 #include <sys/types.h>
116 #ifdef HAVE_SYS_SOCKET_H
117 #include <sys/socket.h>
118 #endif
119 #ifdef HAVE_NETDB_H
120 #include <netdb.h>
121 #endif
122 #ifdef HAVE_WS2TCPIP_H
123 #include <ws2tcpip.h>
124 #endif
125 ])
126   if test $ac_cv_have_decl_getaddrinfo = no; then
127     HAVE_DECL_GETADDRINFO=0
128   fi
129   if test $ac_cv_have_decl_freeaddrinfo = no; then
130     HAVE_DECL_FREEADDRINFO=0
131   fi
132   if test $ac_cv_have_decl_gai_strerror = no; then
133     HAVE_DECL_GAI_STRERROR=0
134   fi
135   if test $ac_cv_have_decl_getnameinfo = no; then
136     HAVE_DECL_GETNAMEINFO=0
137   fi
139   AC_CHECK_TYPES([struct addrinfo],,,[
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_type_struct_addrinfo = no; then
152     HAVE_STRUCT_ADDRINFO=0
153   fi
155   dnl Append $HOSTENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
156   case " $GETADDRINFO_LIB " in
157     *" $HOSTENT_LIB "*) ;;
158     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $HOSTENT_LIB" ;;
159   esac
161   dnl Append $SERVENT_LIB to GETADDRINFO_LIB, avoiding gratuitous duplicates.
162   case " $GETADDRINFO_LIB " in
163     *" $SERVENT_LIB "*) ;;
164     *) GETADDRINFO_LIB="$GETADDRINFO_LIB $SERVENT_LIB" ;;
165   esac
166 ])