libreplace: make sure system/network.h is only used when AC_LIBREPLACE_NETWORK_CHECKS...
[Samba.git] / source / lib / replace / libreplace_network.m4
blob56c603a1552b5adca2c0d98f5374c5e104edbbe1
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/ip.h netinet/tcp.h netinet/in_systm.h netinet/in_ip.h)
12 dnl we need to check that net/if.h really can be used, to cope with hpux
13 dnl where including it always fails
14 AC_CACHE_CHECK([for usable net/if.h],libreplace_cv_USABLE_NET_IF_H,[
15         AC_COMPILE_IFELSE([AC_LANG_SOURCE([
16                 AC_INCLUDES_DEFAULT
17                 #if HAVE_SYS_SOCKET_H
18                 # include <sys/socket.h>
19                 #endif
20                 #include <net/if.h>
21                 int main(void) {return 0;}])],
22                 [libreplace_cv_USABLE_NET_IF_H=yes],
23                 [libreplace_cv_USABLE_NET_IF_H=no]
24         )
26 if test x"$libreplace_cv_USABLE_NET_IF_H" = x"yes";then
27         AC_DEFINE(HAVE_NET_IF_H, 1, usability of net/if.h)
30 AC_HAVE_TYPE([socklen_t],[#include <sys/socket.h>])
31 AC_HAVE_TYPE([sa_family_t],[#include <sys/socket.h>])
32 AC_HAVE_TYPE([struct addrinfo], [#include <netdb.h>])
33 AC_HAVE_TYPE([struct sockaddr], [#include <sys/socket.h>])
34 AC_HAVE_TYPE([struct sockaddr_storage], [
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
39 AC_HAVE_TYPE([struct sockaddr_in6], [
40 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <netinet/in.h>
45 if test x"$ac_cv_type_struct_sockaddr_storage" = x"yes"; then
46 AC_CHECK_MEMBER(struct sockaddr_storage.ss_family,
47                 AC_DEFINE(HAVE_SS_FAMILY, 1, [Defined if struct sockaddr_storage has ss_family field]),,
48                 [
49 #include <sys/socket.h>
50 #include <sys/types.h>
51 #include <netinet/in.h>
52                 ])
54 if test x"$ac_cv_member_struct_sockaddr_storage_ss_family" != 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                 ])
65 dnl The following test is roughl taken from the cvs sources.
66 dnl
67 dnl If we can't find connect, try looking in -lsocket, -lnsl, and -linet.
68 dnl The Irix 5 libc.so has connect and gethostbyname, but Irix 5 also has
69 dnl libsocket.so which has a bad implementation of gethostbyname (it
70 dnl only looks in /etc/hosts), so we only look for -lsocket if we need
71 dnl it.
72 AC_CHECK_FUNCS(connect)
73 if test x"$ac_cv_func_connect" = x"no"; then
74         AC_CHECK_LIB_EXT(nsl_s, LIBREPLACE_NETWORK_LIBS, connect)
75         AC_CHECK_LIB_EXT(nsl, LIBREPLACE_NETWORK_LIBS, connect)
76         AC_CHECK_LIB_EXT(socket, LIBREPLACE_NETWORK_LIBS, connect)
77         AC_CHECK_LIB_EXT(inet, LIBREPLACE_NETWORK_LIBS, connect)
78         dnl We can't just call AC_CHECK_FUNCS(connect) here,
79         dnl because the value has been cached.
80         if test x"$ac_cv_lib_ext_nsl_s_connect" = x"yes" ||
81                 test x"$ac_cv_lib_ext_nsl_connect" = x"yes" ||
82                 test x"$ac_cv_lib_ext_socket_connect" = x"yes" ||
83                 test x"$ac_cv_lib_ext_inet_connect" = x"yes"
84         then
85                 AC_DEFINE(HAVE_CONNECT,1,[Whether the system has connect()])
86         fi
89 AC_CHECK_FUNCS(gethostbyname)
90 if test x"$ac_cv_func_gethostbyname" = x"no"; then
91         AC_CHECK_LIB_EXT(nsl_s, LIBREPLACE_NETWORK_LIBS, gethostbyname)
92         AC_CHECK_LIB_EXT(nsl, LIBREPLACE_NETWORK_LIBS, gethostbyname)
93         AC_CHECK_LIB_EXT(socket, LIBREPLACE_NETWORK_LIBS, gethostbyname)
94         dnl We can't just call AC_CHECK_FUNCS(gethostbyname) here,
95         dnl because the value has been cached.
96         if test x"$ac_cv_lib_ext_nsl_s_gethostbyname" = x"yes" ||
97                 test x"$ac_cv_lib_ext_nsl_gethostbyname" = x"yes" ||
98                 test x"$ac_cv_lib_ext_socket_gethostbyname" = x"yes"
99         then
100                 AC_DEFINE(HAVE_GETHOSTBYNAME,1,
101                           [Whether the system has gethostbyname()])
102         fi
105 # The following tests need LIBS="${LIBREPLACE_NETWORK_LIBS}"
106 old_LIBS=$LIBS
107 LIBS="${LIBREPLACE_NETWORK_LIBS}"
108 SAVE_CPPFLAGS="$CPPFLAGS"
109 CPPFLAGS="$CPPFLAGS -I$libreplacedir"
111 AC_CHECK_FUNCS(socketpair,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} socketpair.o"])
113 AC_CACHE_CHECK([for broken inet_ntoa],libreplace_cv_REPLACE_INET_NTOA,[
114 AC_TRY_RUN([
115 #include <stdio.h>
116 #include <unistd.h>
117 #include <sys/types.h>
118 #include <netinet/in.h>
119 #ifdef HAVE_ARPA_INET_H
120 #include <arpa/inet.h>
121 #endif
122 main() { struct in_addr ip; ip.s_addr = 0x12345678;
123 if (strcmp(inet_ntoa(ip),"18.52.86.120") &&
124     strcmp(inet_ntoa(ip),"120.86.52.18")) { exit(0); }
125 exit(1);}],
126            libreplace_cv_REPLACE_INET_NTOA=yes,libreplace_cv_REPLACE_INET_NTOA=no,libreplace_cv_REPLACE_INET_NTOA=cross)])
128 AC_CHECK_FUNCS(inet_ntoa,[],[libreplace_cv_REPLACE_INET_NTOA=yes])
129 if test x"$libreplace_cv_REPLACE_INET_NTOA" = x"yes"; then
130     AC_DEFINE(REPLACE_INET_NTOA,1,[Whether inet_ntoa should be replaced])
131     LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_ntoa.o"
134 AC_CHECK_FUNCS(inet_aton,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_aton.o"])
136 AC_CHECK_FUNCS(inet_ntop,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_ntop.o"])
138 AC_CHECK_FUNCS(inet_pton,[],[LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} inet_pton.o"])
140 dnl test for getaddrinfo/getnameinfo
141 AC_CACHE_CHECK([for getaddrinfo],libreplace_cv_HAVE_GETADDRINFO,[
142 AC_TRY_LINK([
143 #include <sys/types.h>
144 #if STDC_HEADERS
145 #include <stdlib.h>
146 #include <stddef.h>
147 #endif
148 #include <sys/socket.h>
149 #include <netdb.h>],
151 struct sockaddr sa;
152 struct addrinfo *ai = NULL;
153 int ret = getaddrinfo(NULL, NULL, NULL, &ai);
154 if (ret != 0) {
155         const char *es = gai_strerror(ret);
157 freeaddrinfo(ai);
158 ret = getnameinfo(&sa, sizeof(sa),
159                 NULL, 0,
160                 NULL, 0, 0);
163 libreplace_cv_HAVE_GETADDRINFO=yes,libreplace_cv_HAVE_GETADDRINFO=no)])
164 if test x"$libreplace_cv_HAVE_GETADDRINFO" = x"yes"; then
165         AC_DEFINE(HAVE_GETADDRINFO,1,[Whether the system has getaddrinfo])
166         AC_DEFINE(HAVE_GETNAMEINFO,1,[Whether the system has getnameinfo])
167         AC_DEFINE(HAVE_FREEADDRINFO,1,[Whether the system has freeaddrinfo])
168         AC_DEFINE(HAVE_GAI_STRERROR,1,[Whether the system has gai_strerror])
169 else
170         LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} getaddrinfo.o"
173 AC_CHECK_HEADERS([ifaddrs.h])
175 dnl Used when getifaddrs is not available
176 AC_CHECK_MEMBERS([struct sockaddr.sa_len], 
177          [AC_DEFINE(HAVE_SOCKADDR_SA_LEN, 1, [Whether struct sockaddr has a sa_len member])],
178          [],
179          [#include <sys/socket.h>])
181 dnl test for getifaddrs and freeifaddrs
182 AC_CACHE_CHECK([for getifaddrs and freeifaddrs],libreplace_cv_HAVE_GETIFADDRS,[
183 AC_TRY_COMPILE([
184 #include <sys/types.h>
185 #if STDC_HEADERS
186 #include <stdlib.h>
187 #include <stddef.h>
188 #endif
189 #include <sys/socket.h>
190 #include <netinet/in.h>
191 #include <arpa/inet.h>
192 #include <ifaddrs.h>
193 #include <netdb.h>],
195 struct ifaddrs *ifp = NULL;
196 int ret = getifaddrs (&ifp);
197 freeifaddrs(ifp);
199 libreplace_cv_HAVE_GETIFADDRS=yes,libreplace_cv_HAVE_GETIFADDRS=no)])
200 if test x"$libreplace_cv_HAVE_GETIFADDRS" = x"yes"; then
201     AC_DEFINE(HAVE_GETIFADDRS,1,[Whether the system has getifaddrs])
202     AC_DEFINE(HAVE_FREEIFADDRS,1,[Whether the system has freeifaddrs])
203         AC_DEFINE(HAVE_STRUCT_IFADDRS,1,[Whether struct ifaddrs is available])
206 ##################
207 # look for a method of finding the list of network interfaces
208 iface=no;
209 AC_CACHE_CHECK([for iface getifaddrs],libreplace_cv_HAVE_IFACE_GETIFADDRS,[
210 AC_TRY_RUN([
211 #define HAVE_IFACE_GETIFADDRS 1
212 #define NO_CONFIG_H 1
213 #define AUTOCONF_TEST 1
214 #define SOCKET_WRAPPER_NOT_REPLACE
215 #include "$libreplacedir/replace.c"
216 #include "$libreplacedir/inet_ntop.c"
217 #include "$libreplacedir/snprintf.c"
218 #include "$libreplacedir/getifaddrs.c"
219 #define getifaddrs_test main
220 #include "$libreplacedir/test/getifaddrs.c"],
221            libreplace_cv_HAVE_IFACE_GETIFADDRS=yes,libreplace_cv_HAVE_IFACE_GETIFADDRS=no,libreplace_cv_HAVE_IFACE_GETIFADDRS=cross)])
222 if test x"$libreplace_cv_HAVE_IFACE_GETIFADDRS" = x"yes"; then
223     iface=yes;AC_DEFINE(HAVE_IFACE_GETIFADDRS,1,[Whether iface getifaddrs is available])
224 else
225         LIBREPLACE_NETWORK_OBJS="${LIBREPLACE_NETWORK_OBJS} getifaddrs.o"
229 if test $iface = no; then
230 AC_CACHE_CHECK([for iface AIX],libreplace_cv_HAVE_IFACE_AIX,[
231 AC_TRY_RUN([
232 #define HAVE_IFACE_AIX 1
233 #define NO_CONFIG_H 1
234 #define AUTOCONF_TEST 1
235 #undef _XOPEN_SOURCE_EXTENDED
236 #define SOCKET_WRAPPER_NOT_REPLACE
237 #include "$libreplacedir/replace.c"
238 #include "$libreplacedir/inet_ntop.c"
239 #include "$libreplacedir/snprintf.c"
240 #include "$libreplacedir/getifaddrs.c"
241 #define getifaddrs_test main
242 #include "$libreplacedir/test/getifaddrs.c"],
243            libreplace_cv_HAVE_IFACE_AIX=yes,libreplace_cv_HAVE_IFACE_AIX=no,libreplace_cv_HAVE_IFACE_AIX=cross)])
244 if test x"$libreplace_cv_HAVE_IFACE_AIX" = x"yes"; then
245     iface=yes;AC_DEFINE(HAVE_IFACE_AIX,1,[Whether iface AIX is available])
250 if test $iface = no; then
251 AC_CACHE_CHECK([for iface ifconf],libreplace_cv_HAVE_IFACE_IFCONF,[
252 AC_TRY_RUN([
253 #define HAVE_IFACE_IFCONF 1
254 #define NO_CONFIG_H 1
255 #define AUTOCONF_TEST 1
256 #define SOCKET_WRAPPER_NOT_REPLACE
257 #include "$libreplacedir/replace.c"
258 #include "$libreplacedir/inet_ntop.c"
259 #include "$libreplacedir/snprintf.c"
260 #include "$libreplacedir/getifaddrs.c"
261 #define getifaddrs_test main
262 #include "$libreplacedir/test/getifaddrs.c"],
263            libreplace_cv_HAVE_IFACE_IFCONF=yes,libreplace_cv_HAVE_IFACE_IFCONF=no,libreplace_cv_HAVE_IFACE_IFCONF=cross)])
264 if test x"$libreplace_cv_HAVE_IFACE_IFCONF" = x"yes"; then
265     iface=yes;AC_DEFINE(HAVE_IFACE_IFCONF,1,[Whether iface ifconf is available])
269 if test $iface = no; then
270 AC_CACHE_CHECK([for iface ifreq],libreplace_cv_HAVE_IFACE_IFREQ,[
271 AC_TRY_RUN([
272 #define HAVE_IFACE_IFREQ 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_IFREQ=yes,libreplace_cv_HAVE_IFACE_IFREQ=no,libreplace_cv_HAVE_IFACE_IFREQ=cross)])
283 if test x"$libreplace_cv_HAVE_IFACE_IFREQ" = x"yes"; then
284     iface=yes;AC_DEFINE(HAVE_IFACE_IFREQ,1,[Whether iface ifreq is available])
288 LIBS=$old_LIBS
289 CPPFLAGS="$SAVE_CPPFLAGS"
291 LIBREPLACEOBJ="${LIBREPLACEOBJ} ${LIBREPLACE_NETWORK_OBJS}"
293 echo "LIBREPLACE_NETWORK_CHECKS: END"
294 ]) dnl end AC_LIBREPLACE_NETWORK_CHECKS