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