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