lib/replace: use AC_TRY_LINK() to verify that getifaddrs() and freeifaddrs() are...
[Samba/bb.git] / lib / replace / libreplace_network.m4
blob9223d94b7575937188963cd28c137de30d90b7ca
1 AC_DEFUN_ONCE(AC_LIBREPLACE_NETWORK_CHECKS,
3 echo "LIBREPLACE_NETWORK_CHECKS: START"
5 AC_DEFINE(LIBREPLACE_NETWORK_CHECKS, 1, [LIBREPLACE_NETWORK_CHECKS were used])
6 LIBREPLACE_NETWORK_OBJS=""
7 LIBREPLACE_NETWORK_LIBS=""
9 AC_CHECK_HEADERS(sys/socket.h netinet/in.h netdb.h arpa/inet.h)
10 AC_CHECK_HEADERS(netinet/in_systm.h)
11 AC_CHECK_HEADERS([netinet/ip.h], [], [],[#ifdef HAVE_NETINET_IN_H
12 #include <netinet/in.h>
13 #endif
14 #ifdef HAVE_NETINET_IN_SYSTM_H
15 #include <netinet/in_systm.h>
16 #endif])
17 AC_CHECK_HEADERS(netinet/tcp.h netinet/in_ip.h)
18 AC_CHECK_HEADERS(sys/sockio.h sys/un.h)
19 AC_CHECK_HEADERS(sys/uio.h)
21 dnl we need to check that net/if.h really can be used, to cope with hpux
22 dnl where including it always fails
23 AC_CACHE_CHECK([for usable net/if.h],libreplace_cv_USABLE_NET_IF_H,[
24         AC_COMPILE_IFELSE([AC_LANG_SOURCE([
25                 AC_INCLUDES_DEFAULT
26                 #if HAVE_SYS_SOCKET_H
27                 # include <sys/socket.h>
28                 #endif
29                 #include <net/if.h>
30                 int main(void) {return 0;}])],
31                 [libreplace_cv_USABLE_NET_IF_H=yes],
32                 [libreplace_cv_USABLE_NET_IF_H=no]
33         )
35 if test x"$libreplace_cv_USABLE_NET_IF_H" = x"yes";then
36         AC_DEFINE(HAVE_NET_IF_H, 1, usability of net/if.h)
39 AC_HAVE_TYPE([socklen_t],[#include <sys/socket.h>])
40 AC_HAVE_TYPE([sa_family_t],[#include <sys/socket.h>])
41 AC_HAVE_TYPE([struct addrinfo], [#include <netdb.h>])
42 AC_HAVE_TYPE([struct sockaddr], [#include <sys/socket.h>])
43 AC_HAVE_TYPE([struct sockaddr_storage], [
44 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <netinet/in.h>
48 AC_HAVE_TYPE([struct sockaddr_in6], [
49 #include <sys/socket.h>
50 #include <sys/types.h>
51 #include <netinet/in.h>
54 if test x"$ac_cv_type_struct_sockaddr_storage" = x"yes"; then
55 AC_CHECK_MEMBER(struct sockaddr_storage.ss_family,
56                 AC_DEFINE(HAVE_SS_FAMILY, 1, [Defined if struct sockaddr_storage has ss_family field]),,
57                 [
58 #include <sys/socket.h>
59 #include <sys/types.h>
60 #include <netinet/in.h>
61                 ])
63 if test x"$ac_cv_member_struct_sockaddr_storage_ss_family" != x"yes"; then
64 AC_CHECK_MEMBER(struct sockaddr_storage.__ss_family,
65                 AC_DEFINE(HAVE___SS_FAMILY, 1, [Defined if struct sockaddr_storage has __ss_family field]),,
66                 [
67 #include <sys/socket.h>
68 #include <sys/types.h>
69 #include <netinet/in.h>
70                 ])
74 AC_CACHE_CHECK([for sin_len in sock],libreplace_cv_HAVE_SOCK_SIN_LEN,[
75         AC_TRY_COMPILE(
76                 [
77 #include <sys/types.h>
78 #include <sys/socket.h>
79 #include <netinet/in.h>
80                 ],[
81 struct sockaddr_in sock; sock.sin_len = sizeof(sock);
82                 ],[
83                 libreplace_cv_HAVE_SOCK_SIN_LEN=yes
84                 ],[
85                 libreplace_cv_HAVE_SOCK_SIN_LEN=no
86                 ])
88 if test x"$libreplace_cv_HAVE_SOCK_SIN_LEN" = x"yes"; then
89         AC_DEFINE(HAVE_SOCK_SIN_LEN,1,[Whether the sockaddr_in struct has a sin_len property])
92 ############################################
93 # check for unix domain sockets
94 AC_CACHE_CHECK([for unix domain sockets],libreplace_cv_HAVE_UNIXSOCKET,[
95         AC_TRY_COMPILE([
96 #include <sys/types.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 #include <sys/socket.h>
100 #include <sys/un.h>
101                 ],[
102 struct sockaddr_un sunaddr;
103 sunaddr.sun_family = AF_UNIX;
104                 ],[
105                 libreplace_cv_HAVE_UNIXSOCKET=yes
106                 ],[
107                 libreplace_cv_HAVE_UNIXSOCKET=no
108                 ])
110 if test x"$libreplace_cv_HAVE_UNIXSOCKET" = x"yes"; then
111         AC_DEFINE(HAVE_UNIXSOCKET,1,[If we need to build with unixscoket support])
114 dnl The following test is roughl taken from the cvs sources.
116 dnl If we can't find connect, try looking in -lsocket, -lnsl, and -linet.
117 dnl The Irix 5 libc.so has connect and gethostbyname, but Irix 5 also has
118 dnl libsocket.so which has a bad implementation of gethostbyname (it
119 dnl only looks in /etc/hosts), so we only look for -lsocket if we need
120 dnl it.
121 AC_CHECK_FUNCS(connect)
122 if test x"$ac_cv_func_connect" = x"no"; then
123         AC_CHECK_LIB_EXT(nsl_s, LIBREPLACE_NETWORK_LIBS, connect)
124         AC_CHECK_LIB_EXT(nsl, LIBREPLACE_NETWORK_LIBS, connect)
125         AC_CHECK_LIB_EXT(socket, LIBREPLACE_NETWORK_LIBS, connect)
126         AC_CHECK_LIB_EXT(inet, LIBREPLACE_NETWORK_LIBS, connect)
127         dnl We can't just call AC_CHECK_FUNCS(connect) here,
128         dnl because the value has been cached.
129         if test x"$ac_cv_lib_ext_nsl_s_connect" = x"yes" ||
130                 test x"$ac_cv_lib_ext_nsl_connect" = x"yes" ||
131                 test x"$ac_cv_lib_ext_socket_connect" = x"yes" ||
132                 test x"$ac_cv_lib_ext_inet_connect" = x"yes"
133         then
134                 AC_DEFINE(HAVE_CONNECT,1,[Whether the system has connect()])
135         fi
138 AC_CHECK_FUNCS(gethostbyname)
139 if test x"$ac_cv_func_gethostbyname" = x"no"; then
140         AC_CHECK_LIB_EXT(nsl_s, LIBREPLACE_NETWORK_LIBS, gethostbyname)
141         AC_CHECK_LIB_EXT(nsl, LIBREPLACE_NETWORK_LIBS, gethostbyname)
142         AC_CHECK_LIB_EXT(socket, LIBREPLACE_NETWORK_LIBS, gethostbyname)
143         dnl We can't just call AC_CHECK_FUNCS(gethostbyname) here,
144         dnl because the value has been cached.
145         if test x"$ac_cv_lib_ext_nsl_s_gethostbyname" = x"yes" ||
146                 test x"$ac_cv_lib_ext_nsl_gethostbyname" = x"yes" ||
147                 test x"$ac_cv_lib_ext_socket_gethostbyname" = x"yes"
148         then
149                 AC_DEFINE(HAVE_GETHOSTBYNAME,1,
150                           [Whether the system has gethostbyname()])
151         fi
154 dnl HP-UX has if_nametoindex in -lipv6
155 AC_CHECK_FUNCS(if_nametoindex)
156 if test x"$ac_cv_func_if_nametoindex" = x"no"; then
157         AC_CHECK_LIB_EXT(ipv6, LIBREPLACE_NETWORK_LIBS, if_nametoindex)
158         dnl We can't just call AC_CHECK_FUNCS(if_nametoindex) here,
159         dnl because the value has been cached.
160         if test x"$ac_cv_lib_ext_ipv6_if_nametoindex" = x"yes"
161         then
162                 AC_DEFINE(HAVE_IF_NAMETOINDEX, 1,
163                           [Whether the system has if_nametoindex()])
164         fi
167 # The following tests need LIBS="${LIBREPLACE_NETWORK_LIBS}"
168 old_LIBS=$LIBS
169 LIBS="${LIBREPLACE_NETWORK_LIBS}"
170 SAVE_CPPFLAGS="$CPPFLAGS"
171 CPPFLAGS="$CPPFLAGS -I$libreplacedir"
173 AC_CHECK_FUNCS(socketpair,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} socketpair.o"])
175 AC_CACHE_CHECK([for broken inet_ntoa],libreplace_cv_REPLACE_INET_NTOA,[
176 AC_TRY_RUN([
177 #include <stdio.h>
178 #include <unistd.h>
179 #include <sys/types.h>
180 #include <netinet/in.h>
181 #ifdef HAVE_ARPA_INET_H
182 #include <arpa/inet.h>
183 #endif
184 main() { struct in_addr ip; ip.s_addr = 0x12345678;
185 if (strcmp(inet_ntoa(ip),"18.52.86.120") &&
186     strcmp(inet_ntoa(ip),"120.86.52.18")) { exit(0); }
187 exit(1);}],
188            libreplace_cv_REPLACE_INET_NTOA=yes,libreplace_cv_REPLACE_INET_NTOA=no,libreplace_cv_REPLACE_INET_NTOA=cross)])
190 AC_CHECK_FUNCS(inet_ntoa,[],[libreplace_cv_REPLACE_INET_NTOA=yes])
191 if test x"$libreplace_cv_REPLACE_INET_NTOA" = x"yes"; then
192     AC_DEFINE(REPLACE_INET_NTOA,1,[Whether inet_ntoa should be replaced])
193     LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_ntoa.o"
196 AC_CHECK_FUNCS(inet_aton,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_aton.o"])
198 AC_CHECK_FUNCS(inet_ntop,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_ntop.o"])
200 AC_CHECK_FUNCS(inet_pton,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_pton.o"])
202 dnl test for getaddrinfo/getnameinfo
203 AC_CACHE_CHECK([for getaddrinfo],libreplace_cv_HAVE_GETADDRINFO,[
204 AC_TRY_LINK([
205 #include <sys/types.h>
206 #if STDC_HEADERS
207 #include <stdlib.h>
208 #include <stddef.h>
209 #endif
210 #include <sys/socket.h>
211 #include <netdb.h>],
213 struct sockaddr sa;
214 struct addrinfo *ai = NULL;
215 int ret = getaddrinfo(NULL, NULL, NULL, &ai);
216 if (ret != 0) {
217         const char *es = gai_strerror(ret);
219 freeaddrinfo(ai);
220 ret = getnameinfo(&sa, sizeof(sa),
221                 NULL, 0,
222                 NULL, 0, 0);
225 libreplace_cv_HAVE_GETADDRINFO=yes,libreplace_cv_HAVE_GETADDRINFO=no)])
226 if test x"$libreplace_cv_HAVE_GETADDRINFO" = x"yes"; then
227         AC_DEFINE(HAVE_GETADDRINFO,1,[Whether the system has getaddrinfo])
228         AC_DEFINE(HAVE_GETNAMEINFO,1,[Whether the system has getnameinfo])
229         AC_DEFINE(HAVE_FREEADDRINFO,1,[Whether the system has freeaddrinfo])
230         AC_DEFINE(HAVE_GAI_STRERROR,1,[Whether the system has gai_strerror])
231 else
232         LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} getaddrinfo.o"
235 AC_CHECK_HEADERS([ifaddrs.h])
237 dnl Used when getifaddrs is not available
238 AC_CHECK_MEMBERS([struct sockaddr.sa_len], 
239          [AC_DEFINE(HAVE_SOCKADDR_SA_LEN, 1, [Whether struct sockaddr has a sa_len member])],
240          [],
241          [#include <sys/socket.h>])
243 dnl test for getifaddrs and freeifaddrs
244 AC_CACHE_CHECK([for getifaddrs and freeifaddrs],libreplace_cv_HAVE_GETIFADDRS,[
245 AC_TRY_LINK([
246 #include <sys/types.h>
247 #if STDC_HEADERS
248 #include <stdlib.h>
249 #include <stddef.h>
250 #endif
251 #include <sys/socket.h>
252 #include <netinet/in.h>
253 #include <arpa/inet.h>
254 #include <ifaddrs.h>
255 #include <netdb.h>],
257 struct ifaddrs *ifp = NULL;
258 int ret = getifaddrs (&ifp);
259 freeifaddrs(ifp);
261 libreplace_cv_HAVE_GETIFADDRS=yes,libreplace_cv_HAVE_GETIFADDRS=no)])
262 if test x"$libreplace_cv_HAVE_GETIFADDRS" = x"yes"; then
263     AC_DEFINE(HAVE_GETIFADDRS,1,[Whether the system has getifaddrs])
264     AC_DEFINE(HAVE_FREEIFADDRS,1,[Whether the system has freeifaddrs])
265         AC_DEFINE(HAVE_STRUCT_IFADDRS,1,[Whether struct ifaddrs is available])
268 ##################
269 # look for a method of finding the list of network interfaces
270 iface=no;
271 AC_CACHE_CHECK([for iface getifaddrs],libreplace_cv_HAVE_IFACE_GETIFADDRS,[
272 AC_TRY_RUN([
273 #define HAVE_IFACE_GETIFADDRS 1
274 #define NO_CONFIG_H 1
275 #define AUTOCONF_TEST 1
276 #define SOCKET_WRAPPER_NOT_REPLACE
277 #include "$libreplacedir/replace.c"
278 #include "$libreplacedir/inet_ntop.c"
279 #include "$libreplacedir/snprintf.c"
280 #include "$libreplacedir/getifaddrs.c"
281 #define getifaddrs_test main
282 #include "$libreplacedir/test/getifaddrs.c"],
283            libreplace_cv_HAVE_IFACE_GETIFADDRS=yes,libreplace_cv_HAVE_IFACE_GETIFADDRS=no,libreplace_cv_HAVE_IFACE_GETIFADDRS=cross)])
284 if test x"$libreplace_cv_HAVE_IFACE_GETIFADDRS" = x"yes"; then
285     iface=yes;AC_DEFINE(HAVE_IFACE_GETIFADDRS,1,[Whether iface getifaddrs is available])
286 else
287         LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} getifaddrs.o"
291 if test $iface = no; then
292 AC_CACHE_CHECK([for iface AIX],libreplace_cv_HAVE_IFACE_AIX,[
293 AC_TRY_RUN([
294 #define HAVE_IFACE_AIX 1
295 #define NO_CONFIG_H 1
296 #define AUTOCONF_TEST 1
297 #undef _XOPEN_SOURCE_EXTENDED
298 #define SOCKET_WRAPPER_NOT_REPLACE
299 #include "$libreplacedir/replace.c"
300 #include "$libreplacedir/inet_ntop.c"
301 #include "$libreplacedir/snprintf.c"
302 #include "$libreplacedir/getifaddrs.c"
303 #define getifaddrs_test main
304 #include "$libreplacedir/test/getifaddrs.c"],
305            libreplace_cv_HAVE_IFACE_AIX=yes,libreplace_cv_HAVE_IFACE_AIX=no,libreplace_cv_HAVE_IFACE_AIX=cross)])
306 if test x"$libreplace_cv_HAVE_IFACE_AIX" = x"yes"; then
307     iface=yes;AC_DEFINE(HAVE_IFACE_AIX,1,[Whether iface AIX is available])
312 if test $iface = no; then
313 AC_CACHE_CHECK([for iface ifconf],libreplace_cv_HAVE_IFACE_IFCONF,[
314 AC_TRY_RUN([
315 #define HAVE_IFACE_IFCONF 1
316 #define NO_CONFIG_H 1
317 #define AUTOCONF_TEST 1
318 #define SOCKET_WRAPPER_NOT_REPLACE
319 #include "$libreplacedir/replace.c"
320 #include "$libreplacedir/inet_ntop.c"
321 #include "$libreplacedir/snprintf.c"
322 #include "$libreplacedir/getifaddrs.c"
323 #define getifaddrs_test main
324 #include "$libreplacedir/test/getifaddrs.c"],
325            libreplace_cv_HAVE_IFACE_IFCONF=yes,libreplace_cv_HAVE_IFACE_IFCONF=no,libreplace_cv_HAVE_IFACE_IFCONF=cross)])
326 if test x"$libreplace_cv_HAVE_IFACE_IFCONF" = x"yes"; then
327     iface=yes;AC_DEFINE(HAVE_IFACE_IFCONF,1,[Whether iface ifconf is available])
331 if test $iface = no; then
332 AC_CACHE_CHECK([for iface ifreq],libreplace_cv_HAVE_IFACE_IFREQ,[
333 AC_TRY_RUN([
334 #define HAVE_IFACE_IFREQ 1
335 #define NO_CONFIG_H 1
336 #define AUTOCONF_TEST 1
337 #define SOCKET_WRAPPER_NOT_REPLACE
338 #include "$libreplacedir/replace.c"
339 #include "$libreplacedir/inet_ntop.c"
340 #include "$libreplacedir/snprintf.c"
341 #include "$libreplacedir/getifaddrs.c"
342 #define getifaddrs_test main
343 #include "$libreplacedir/test/getifaddrs.c"],
344            libreplace_cv_HAVE_IFACE_IFREQ=yes,libreplace_cv_HAVE_IFACE_IFREQ=no,libreplace_cv_HAVE_IFACE_IFREQ=cross)])
345 if test x"$libreplace_cv_HAVE_IFACE_IFREQ" = x"yes"; then
346     iface=yes;AC_DEFINE(HAVE_IFACE_IFREQ,1,[Whether iface ifreq is available])
350 dnl test for ipv6
351 AC_CACHE_CHECK([for ipv6 support],libreplace_cv_HAVE_IPV6,[
352         AC_TRY_LINK([
353 #include <stdlib.h> /* for NULL */
354 #include <sys/socket.h>
355 #include <sys/types.h>
356 #include <netdb.h>
357 #include <netinet/in.h>
358                 ],
359                 [
360 struct sockaddr_storage sa_store;
361 struct addrinfo *ai = NULL;
362 struct in6_addr in6addr;
363 int idx = if_nametoindex("iface1");
364 int s = socket(AF_INET6, SOCK_STREAM, 0);
365 int ret = getaddrinfo(NULL, NULL, NULL, &ai);
366 if (ret != 0) {
367         const char *es = gai_strerror(ret);
369 freeaddrinfo(ai);
370                 ],[
371                 libreplace_cv_HAVE_IPV6=yes
372                 ],[
373                 libreplace_cv_HAVE_IPV6=no
374                 ])
376 if test x"$libreplace_cv_HAVE_IPV6" = x"yes"; then
377     AC_DEFINE(HAVE_IPV6,1,[Whether the system has IPv6 support])
380 LIBS=$old_LIBS
381 CPPFLAGS="$SAVE_CPPFLAGS"
383 LIBREPLACEOBJ="${LIBREPLACEOBJ} ${LIBREPLACE_NETWORK_OBJS}"
385 echo "LIBREPLACE_NETWORK_CHECKS: END"
386 ]) dnl end AC_LIBREPLACE_NETWORK_CHECKS