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