xref: /openbsd-src/usr.sbin/nsd/acx_nlnetlabs.m4 (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1# acx_nlnetlabs.m4 - common macros for configure checks
2# Copyright 2009-2011, NLnet Labs, Wouter Wijngaards.
3# BSD licensed.
4#
5# Version 10
6# 2010-07-02 Add check for ss_family (for minix).
7# 2010-04-26 Fix to use CPPFLAGS for CHECK_COMPILER_FLAGS.
8# 2010-03-01 Fix RPATH using CONFIG_COMMANDS to run at the very end.
9# 2010-02-18 WITH_SSL outputs the LIBSSL_LDFLAGS, LIBS, CPPFLAGS seperate, -ldl
10# 2010-02-01 added ACX_CHECK_MEMCMP_SIGNED, AHX_MEMCMP_BROKEN
11# 2010-01-20 added AHX_COONFIG_STRLCAT
12# 2009-07-14 U_CHAR detection improved for windows crosscompile.
13#            added ACX_FUNC_MALLOC
14#            fixup some #if to #ifdef
15#            NONBLOCKING test for mingw crosscompile.
16# 2009-07-13 added ACX_WITH_SSL_OPTIONAL
17# 2009-07-03 fixup LDFLAGS for empty ssl dir.
18#
19# Automates some of the checking constructs.  Aims at portability for POSIX.
20# Documentation for functions is below.
21#
22# the following macro's are provided in this file:
23# (see below for details on each macro).
24#
25# ACX_ESCAPE_BACKSLASH		- escape backslashes in var for C-preproc.
26# ACX_RSRC_VERSION		- create windows resource version number.
27# ACX_CHECK_COMPILER_FLAG	- see if cc supports a flag.
28# ACX_CHECK_ERROR_FLAGS		- see which flag is -werror (used below).
29# ACX_CHECK_COMPILER_FLAG_NEEDED - see if flags make the code compile cleanly.
30# ACX_DEPFLAG			- find cc dependency flags.
31# ACX_DETERMINE_EXT_FLAGS_UNBOUND - find out which flags enable BSD and POSIX.
32# ACX_CHECK_FORMAT_ATTRIBUTE	- find cc printf format syntax.
33# ACX_CHECK_UNUSED_ATTRIBUTE	- find cc variable unused syntax.
34# ACX_LIBTOOL_C_ONLY		- create libtool for C only, improved.
35# ACX_TYPE_U_CHAR		- u_char type.
36# ACX_TYPE_RLIM_T		- rlim_t type.
37# ACX_TYPE_SOCKLEN_T		- socklen_t type.
38# ACX_TYPE_IN_ADDR_T		- in_addr_t type.
39# ACX_TYPE_IN_PORT_T		- in_port_t type.
40# ACX_ARG_RPATH			- add --disable-rpath option.
41# ACX_WITH_SSL			- add --with-ssl option, link -lcrypto.
42# ACX_WITH_SSL_OPTIONAL		- add --with-ssl option, link -lcrypto,
43#				  where --without-ssl is also accepted
44# ACX_LIB_SSL			- setup to link -lssl.
45# ACX_SYS_LARGEFILE		- improved sys_largefile, fseeko, >2G files.
46# ACX_CHECK_GETADDRINFO_WITH_INCLUDES - find getaddrinfo, portably.
47# ACX_FUNC_DEPRECATED		- see if func is deprecated.
48# ACX_CHECK_NONBLOCKING_BROKEN	- see if nonblocking sockets really work.
49# ACX_MKDIR_ONE_ARG		- determine mkdir(2) number of arguments.
50# ACX_FUNC_IOCTLSOCKET		- find ioctlsocket, portably.
51# ACX_FUNC_MALLOC		- check malloc, define replacement .
52# AHX_CONFIG_FORMAT_ATTRIBUTE	- config.h text for format.
53# AHX_CONFIG_UNUSED_ATTRIBUTE	- config.h text for unused.
54# AHX_CONFIG_FSEEKO		- define fseeko, ftello fallback.
55# AHX_CONFIG_RAND_MAX		- define RAND_MAX if needed.
56# AHX_CONFIG_MAXHOSTNAMELEN	- define MAXHOSTNAMELEN if needed.
57# AHX_CONFIG_IPV6_MIN_MTU	- define IPV6_MIN_MTU if needed.
58# AHX_CONFIG_SNPRINTF		- snprintf compat prototype
59# AHX_CONFIG_INET_PTON		- inet_pton compat prototype
60# AHX_CONFIG_INET_NTOP		- inet_ntop compat prototype
61# AHX_CONFIG_INET_ATON		- inet_aton compat prototype
62# AHX_CONFIG_MEMMOVE		- memmove compat prototype
63# AHX_CONFIG_STRLCAT		- strlcat compat prototype
64# AHX_CONFIG_STRLCPY		- strlcpy compat prototype
65# AHX_CONFIG_GMTIME_R		- gmtime_r compat prototype
66# AHX_CONFIG_W32_SLEEP		- w32 compat for sleep
67# AHX_CONFIG_W32_USLEEP		- w32 compat for usleep
68# AHX_CONFIG_W32_RANDOM		- w32 compat for random
69# AHX_CONFIG_W32_SRANDOM	- w32 compat for srandom
70# AHX_CONFIG_W32_FD_SET_T	- w32 detection of FD_SET_T.
71# ACX_CFLAGS_STRIP		- strip one flag from CFLAGS
72# ACX_STRIP_EXT_FLAGS		- strip extension flags from CFLAGS
73# AHX_CONFIG_FLAG_OMITTED	- define omitted flag
74# AHX_CONFIG_FLAG_EXT		- define omitted extension flag
75# AHX_CONFIG_EXT_FLAGS		- define the stripped extension flags
76# ACX_CHECK_MEMCMP_SIGNED	- check if memcmp uses signed characters.
77# AHX_MEMCMP_BROKEN		- replace memcmp func for CHECK_MEMCMP_SIGNED.
78# ACX_CHECK_SS_FAMILY           - check for sockaddr_storage.ss_family
79#
80
81dnl Escape backslashes as \\, for C:\ paths, for the C preprocessor defines.
82dnl for example, ACX_ESCAPE_BACKSLASH($from_var, to_var)
83dnl $1: the text to change.
84dnl $2: the result.
85AC_DEFUN([ACX_ESCAPE_BACKSLASH], [$2="`echo $1 | sed -e 's/\\\\/\\\\\\\\/g'`"
86])
87
88dnl Calculate comma separated windows-resource numbers from package version.
89dnl Picks the first three(,0) or four numbers out of the name.
90dnl $1: variable for the result
91AC_DEFUN([ACX_RSRC_VERSION],
92[$1=[`echo $PACKAGE_VERSION | sed -e 's/^[^0-9]*\([0-9]\)[^0-9]*\([0-9]\)[^0-9]*\([0-9]\)[^0-9]*\([0-9]\).*$/\1,\2,\3,\4/' -e 's/^[^0-9]*\([0-9]\)[^0-9]*\([0-9]\)[^0-9]*\([0-9]\)[^0-9]*$/\1,\2,\3,0/' `]
93])
94
95dnl Routine to help check for compiler flags.
96dnl Checks if the compiler will accept the flag.
97dnl $1: the flag without a - in front, so g to check -g.
98dnl $2: executed if yes
99dnl $3: executed if no
100AC_DEFUN([ACX_CHECK_COMPILER_FLAG],
101[
102AC_REQUIRE([AC_PROG_CC])
103AC_MSG_CHECKING(whether $CC supports -$1)
104cache=`echo $1 | sed 'y%.=/+-%___p_%'`
105AC_CACHE_VAL(cv_prog_cc_flag_$cache,
106[
107echo 'void f(){}' >conftest.c
108if test -z "`$CC $CPPFLAGS $CFLAGS -$1 -c conftest.c 2>&1`"; then
109eval "cv_prog_cc_flag_$cache=yes"
110else
111eval "cv_prog_cc_flag_$cache=no"
112fi
113rm -f conftest conftest.o conftest.c
114])
115if eval "test \"`echo '$cv_prog_cc_flag_'$cache`\" = yes"; then
116AC_MSG_RESULT(yes)
117:
118$2
119else
120AC_MSG_RESULT(no)
121:
122$3
123fi
124])
125
126dnl setup flags for ACX_CHECK_COMPILER_FLAG_NEEDED
127dnl ERRFLAG: result, compiler flag to turn warnings into errors
128AC_DEFUN([ACX_CHECK_ERROR_FLAGS],
129[
130ACX_CHECK_COMPILER_FLAG(Werror, [ERRFLAG="-Werror"], [ERRFLAG="-errwarn"])
131ACX_CHECK_COMPILER_FLAG(Wall, [ERRFLAG="$ERRFLAG -Wall"],
132                        	[ERRFLAG="$ERRFLAG -errfmt"])
133])
134
135dnl Routine to help check for needed compiler flags.
136dnl $1: flags for CC
137dnl $2: the includes and code
138dnl $3: if the given code only compiles with the flag, execute argument 3
139dnl $4: if the given code compiles without the flag, execute argument 4
140dnl $5: with and without flag the compile fails, execute argument 5.
141AC_DEFUN([ACX_CHECK_COMPILER_FLAG_NEEDED],
142[
143AC_REQUIRE([AC_PROG_CC])
144AC_REQUIRE([ACX_CHECK_ERROR_FLAGS])
145AC_MSG_CHECKING(whether we need $1 as a flag for $CC)
146cache=AS_TR_SH($1)
147dnl cache=`echo $1 | sed 'y%.=/+- %___p__%'`
148AC_CACHE_VAL(cv_prog_cc_flag_needed_$cache,
149[
150echo '$2' > conftest.c
151echo 'void f(){}' >>conftest.c
152if test -z "`$CC $CPPFLAGS $CFLAGS $ERRFLAG -c conftest.c 2>&1`"; then
153eval "cv_prog_cc_flag_needed_$cache=no"
154else
155[
156if test -z "`$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`"; then
157eval "cv_prog_cc_flag_needed_$cache=yes"
158else
159eval "cv_prog_cc_flag_needed_$cache=fail"
160#echo 'Test with flag fails too!'
161#cat conftest.c
162#echo "$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1"
163#echo `$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`
164#exit 1
165fi
166]
167fi
168rm -f conftest conftest.c conftest.o
169])
170if eval "test \"`echo '$cv_prog_cc_flag_needed_'$cache`\" = yes"; then
171AC_MSG_RESULT(yes)
172:
173$3
174else
175if eval "test \"`echo '$cv_prog_cc_flag_needed_'$cache`\" = no"; then
176AC_MSG_RESULT(no)
177#echo 'Test with flag is no!'
178#cat conftest.c
179#echo "$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1"
180#echo `$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`
181#exit 1
182:
183$4
184else
185AC_MSG_RESULT(failed)
186:
187$5
188fi
189fi
190])
191
192dnl Check for CC dependency flag
193dnl DEPFLAG: set to flag that generates dependencies.
194AC_DEFUN([ACX_DEPFLAG],
195[
196AC_MSG_CHECKING([$CC dependency flag])
197echo 'void f(){}' >conftest.c
198if test "`$CC -MM conftest.c 2>&1`" = "conftest.o: conftest.c"; then
199	DEPFLAG="-MM"
200else
201  if test "`$CC -xM1 conftest.c 2>&1`" = "conftest.o: conftest.c"; then
202	DEPFLAG="-xM1"
203  else
204	DEPFLAG="-MM"  # dunno do something
205  fi
206fi
207AC_MSG_RESULT($DEPFLAG)
208rm -f conftest.c
209AC_SUBST(DEPFLAG)
210])
211
212dnl Determine flags that gives POSIX and BSD functionality.
213dnl CFLAGS is modified for the result.
214AC_DEFUN([ACX_DETERMINE_EXT_FLAGS_UNBOUND],
215[
216ACX_CHECK_COMPILER_FLAG(std=c99, [C99FLAG="-std=c99"])
217ACX_CHECK_COMPILER_FLAG(xc99, [C99FLAG="-xc99"])
218
219AC_CHECK_HEADERS([getopt.h time.h],,, [AC_INCLUDES_DEFAULT])
220
221ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_ALL_SOURCE,
222[
223#include "confdefs.h"
224#include <stdlib.h>
225#include <ctype.h>
226#include <sys/time.h>
227#ifdef HAVE_TIME_H
228#include <time.h>
229#endif
230#include <unistd.h>
231#include <netdb.h>
232#ifdef HAVE_GETOPT_H
233#include <getopt.h>
234#endif
235
236int test() {
237	int a;
238	char **opts = NULL;
239	struct timeval tv;
240	char *t;
241	time_t time = 0;
242	char *buf = NULL;
243	const char* str = NULL;
244	struct msghdr msg;
245	msg.msg_control = 0;
246	t = ctime_r(&time, buf);
247	tv.tv_usec = 10;
248	srandom(32);
249	a = getopt(2, opts, "a");
250	a = isascii(32);
251	str = gai_strerror(0);
252	return a;
253}
254], [CFLAGS="$CFLAGS $C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_ALL_SOURCE"])
255
256ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE,
257[
258#include "confdefs.h"
259#include <stdlib.h>
260#include <ctype.h>
261#include <sys/time.h>
262#ifdef HAVE_TIME_H
263#include <time.h>
264#endif
265#include <unistd.h>
266#include <netdb.h>
267#ifdef HAVE_GETOPT_H
268#include <getopt.h>
269#endif
270
271int test() {
272	int a;
273	char **opts = NULL;
274	struct timeval tv;
275	char *t;
276	time_t time = 0;
277	char *buf = NULL;
278	const char* str = NULL;
279	struct msghdr msg;
280	msg.msg_control = 0;
281	t = ctime_r(&time, buf);
282	tv.tv_usec = 10;
283	srandom(32);
284	a = getopt(2, opts, "a");
285	a = isascii(32);
286	str = gai_strerror(0);
287	return a;
288}
289], [CFLAGS="$CFLAGS $C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE"])
290
291ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG,
292[
293#include <stdbool.h>
294#include <ctype.h>
295int test() {
296        int a = 0;
297        return a;
298}
299], [CFLAGS="$CFLAGS $C99FLAG"])
300
301ACX_CHECK_COMPILER_FLAG_NEEDED(-D_BSD_SOURCE,
302[
303#include <ctype.h>
304
305int test() {
306        int a;
307        a = isascii(32);
308        return a;
309}
310], [CFLAGS="$CFLAGS -D_BSD_SOURCE"])
311
312ACX_CHECK_COMPILER_FLAG_NEEDED(-D_GNU_SOURCE,
313[
314#include <netinet/in.h>
315
316int test() {
317        struct in6_pktinfo inf;
318	int a = (int)sizeof(inf);
319        return a;
320}
321], [CFLAGS="$CFLAGS -D_GNU_SOURCE"])
322
323# check again for GNU_SOURCE for setresgid. May fail if setresgid
324# is not available at all. -D_FRSRESGID is to make this check unique.
325# otherwise we would get the previous cached result.
326ACX_CHECK_COMPILER_FLAG_NEEDED(-D_GNU_SOURCE -D_FRSRESGID,
327[
328#include <unistd.h>
329
330int test() {
331	int a = setresgid(0,0,0);
332	a = setresuid(0,0,0);
333        return a;
334}
335], [CFLAGS="$CFLAGS -D_GNU_SOURCE"])
336
337ACX_CHECK_COMPILER_FLAG_NEEDED(-D_POSIX_C_SOURCE=200112,
338[
339#include "confdefs.h"
340#ifdef HAVE_TIME_H
341#include <time.h>
342#endif
343#include <netdb.h>
344
345int test() {
346        int a = 0;
347        char *t;
348        time_t time = 0;
349        char *buf = NULL;
350	const char* str = NULL;
351        t = ctime_r(&time, buf);
352	str = gai_strerror(0);
353        return a;
354}
355], [CFLAGS="$CFLAGS -D_POSIX_C_SOURCE=200112"])
356
357ACX_CHECK_COMPILER_FLAG_NEEDED(-D__EXTENSIONS__,
358[
359#include "confdefs.h"
360#include <stdlib.h>
361#include <ctype.h>
362#include <sys/time.h>
363#ifdef HAVE_TIME_H
364#include <time.h>
365#endif
366#include <unistd.h>
367#ifdef HAVE_GETOPT_H
368#include <getopt.h>
369#endif
370
371int test() {
372        int a;
373        char **opts = NULL;
374        struct timeval tv;
375        tv.tv_usec = 10;
376        srandom(32);
377        a = getopt(2, opts, "a");
378        a = isascii(32);
379        return a;
380}
381], [CFLAGS="$CFLAGS -D__EXTENSIONS__"])
382
383])dnl End of ACX_DETERMINE_EXT_FLAGS_UNBOUND
384
385dnl Check the printf-format attribute (if any)
386dnl result in HAVE_ATTR_FORMAT.
387dnl Make sure you also include the AHX_CONFIG_FORMAT_ATTRIBUTE.
388AC_DEFUN([ACX_CHECK_FORMAT_ATTRIBUTE],
389[AC_REQUIRE([AC_PROG_CC])
390AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "format" attribute)
391AC_CACHE_VAL(ac_cv_c_format_attribute,
392[ac_cv_c_format_attribute=no
393AC_TRY_COMPILE(
394[#include <stdio.h>
395void f (char *format, ...) __attribute__ ((format (printf, 1, 2)));
396void (*pf) (char *format, ...) __attribute__ ((format (printf, 1, 2)));
397], [
398   f ("%s", "str");
399],
400[ac_cv_c_format_attribute="yes"],
401[ac_cv_c_format_attribute="no"])
402])
403
404AC_MSG_RESULT($ac_cv_c_format_attribute)
405if test $ac_cv_c_format_attribute = yes; then
406  AC_DEFINE(HAVE_ATTR_FORMAT, 1, [Whether the C compiler accepts the "format" attribute])
407fi
408])dnl End of ACX_CHECK_FORMAT_ATTRIBUTE
409
410dnl Setup ATTR_FORMAT config.h parts.
411dnl make sure you call ACX_CHECK_FORMAT_ATTRIBUTE also.
412AC_DEFUN([AHX_CONFIG_FORMAT_ATTRIBUTE],
413[
414#ifdef HAVE_ATTR_FORMAT
415#  define ATTR_FORMAT(archetype, string_index, first_to_check) \
416    __attribute__ ((format (archetype, string_index, first_to_check)))
417#else /* !HAVE_ATTR_FORMAT */
418#  define ATTR_FORMAT(archetype, string_index, first_to_check) /* empty */
419#endif /* !HAVE_ATTR_FORMAT */
420])
421
422dnl Check how to mark function arguments as unused.
423dnl result in HAVE_ATTR_UNUSED.
424dnl Make sure you include AHX_CONFIG_UNUSED_ATTRIBUTE also.
425AC_DEFUN([ACX_CHECK_UNUSED_ATTRIBUTE],
426[AC_REQUIRE([AC_PROG_CC])
427AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "unused" attribute)
428AC_CACHE_VAL(ac_cv_c_unused_attribute,
429[ac_cv_c_unused_attribute=no
430AC_TRY_COMPILE(
431[#include <stdio.h>
432void f (char *u __attribute__((unused)));
433], [
434   f ("x");
435],
436[ac_cv_c_unused_attribute="yes"],
437[ac_cv_c_unused_attribute="no"])
438])
439
440dnl Setup ATTR_UNUSED config.h parts.
441dnl make sure you call ACX_CHECK_UNUSED_ATTRIBUTE also.
442AC_DEFUN([AHX_CONFIG_UNUSED_ATTRIBUTE],
443[
444#if defined(DOXYGEN)
445#  define ATTR_UNUSED(x)  x
446#elif defined(__cplusplus)
447#  define ATTR_UNUSED(x)
448#elif defined(HAVE_ATTR_UNUSED)
449#  define ATTR_UNUSED(x)  x __attribute__((unused))
450#else /* !HAVE_ATTR_UNUSED */
451#  define ATTR_UNUSED(x)  x
452#endif /* !HAVE_ATTR_UNUSED */
453])
454
455AC_MSG_RESULT($ac_cv_c_unused_attribute)
456if test $ac_cv_c_unused_attribute = yes; then
457  AC_DEFINE(HAVE_ATTR_UNUSED, 1, [Whether the C compiler accepts the "unused" attribute])
458fi
459])dnl
460
461dnl Pre-fun for ACX_LIBTOOL_C_ONLY
462AC_DEFUN([ACX_LIBTOOL_C_PRE], [
463# skip these tests, we do not need them.
464AC_DEFUN([AC_PROG_F77], [:])
465AC_DEFUN([AC_PROG_FC], [:])
466AC_DEFUN([AC_PROG_CXX], [:])
467AC_DEFUN([AC_PROG_CXXCPP], [:])
468AC_DEFUN([AC_PROG_OBJC], [:])
469AC_DEFUN([AC_PROG_OBJCCPP], [:])
470AC_DEFUN([AC_LIBTOOL_CXX], [:])
471AC_DEFUN([AC_LIBTOOL_F77], [:])
472# always use ./libtool unless override from commandline (libtool=mylibtool)
473if test -z "$libtool"; then
474	libtool="./libtool"
475fi
476AC_SUBST(libtool)
477# avoid libtool max commandline length test on systems that fork slowly.
478AC_CANONICAL_HOST
479if echo "$host_os" | grep "sunos4" >/dev/null; then
480	lt_cv_sys_max_cmd_len=32750;
481fi
482AC_PATH_TOOL(AR, ar, [false])
483if test $AR = false; then
484	AC_MSG_ERROR([Cannot find 'ar', please extend PATH to include it])
485fi
486])
487
488dnl Perform libtool check, portably, only for C
489AC_DEFUN([ACX_LIBTOOL_C_ONLY], [
490dnl as a requirement so that is gets called before LIBTOOL
491dnl because libtools 'AC_REQUIRE' names are right after this one, before
492dnl this function contents.
493AC_REQUIRE([ACX_LIBTOOL_C_PRE])
494AC_PROG_LIBTOOL
495])
496
497dnl Detect if u_char type is defined, otherwise define it.
498AC_DEFUN([ACX_TYPE_U_CHAR],
499[AC_CHECK_TYPE([u_char], ,
500	[AC_DEFINE([u_char], [unsigned char], [Define to 'unsigned char if not defined])], [
501AC_INCLUDES_DEFAULT
502#ifdef HAVE_WINSOCK2_H
503#  include <winsock2.h>
504#endif
505]) ])
506
507dnl Detect if rlim_t type is defined, otherwise define it.
508AC_DEFUN([ACX_TYPE_RLIM_T],
509[AC_CHECK_TYPE(rlim_t, ,
510	[AC_DEFINE([rlim_t], [unsigned long], [Define to 'int' if not defined])], [
511AC_INCLUDES_DEFAULT
512#ifdef HAVE_SYS_RESOURCE_H
513#  include <sys/resource.h>
514#endif
515]) ])
516
517dnl Detect if socklen_t type is defined, otherwise define it.
518AC_DEFUN([ACX_TYPE_SOCKLEN_T],
519[
520AC_CHECK_TYPE(socklen_t, ,
521	[AC_DEFINE([socklen_t], [int], [Define to 'int' if not defined])], [
522AC_INCLUDES_DEFAULT
523#ifdef HAVE_SYS_SOCKET_H
524#  include <sys/socket.h>
525#endif
526#ifdef HAVE_WS2TCPIP_H
527#  include <ws2tcpip.h>
528#endif
529]) ])
530
531dnl Detect if in_addr_t type is defined, otherwise define it.
532AC_DEFUN([ACX_TYPE_IN_ADDR_T],
533[ AC_CHECK_TYPE(in_addr_t, [], [AC_DEFINE([in_addr_t], [uint32_t], [in_addr_t])], [
534AC_INCLUDES_DEFAULT
535#ifdef HAVE_SYS_TYPES_H
536# include <sys/types.h>
537#endif
538#ifdef HAVE_NETINET_IN_H
539# include <netinet/in.h>
540#endif
541]) ])
542
543dnl Detect if in_port_t type is defined, otherwise define it.
544AC_DEFUN([ACX_TYPE_IN_PORT_T],
545[ AC_CHECK_TYPE(in_port_t, [], [AC_DEFINE([in_port_t], [uint16_t], [in_port_t])], [
546AC_INCLUDES_DEFAULT
547#ifdef HAVE_SYS_TYPES_H
548# include <sys/types.h>
549#endif
550#ifdef HAVE_NETINET_IN_H
551# include <netinet/in.h>
552#endif
553]) ])
554
555dnl Add option to disable the evil rpath. Check whether to use rpath or not.
556dnl Adds the --disable-rpath option. Uses trick to edit the ./libtool.
557AC_DEFUN([ACX_ARG_RPATH],
558[
559AC_ARG_ENABLE(rpath,
560        [  --disable-rpath         disable hardcoded rpath (default=enabled)],
561	enable_rpath=$enableval, enable_rpath=yes)
562if test "x$enable_rpath" = xno; then
563	dnl AC_MSG_RESULT([Fixing libtool for -rpath problems.])
564	AC_CONFIG_COMMANDS([disable-rpath], [
565	sed < libtool > libtool-2 \
566	's/^hardcode_libdir_flag_spec.*$'/'hardcode_libdir_flag_spec=" -D__LIBTOOL_RPATH_SED__ "/'
567	mv libtool-2 libtool
568	chmod 755 libtool
569	libtool="./libtool"
570	])
571fi
572])
573
574dnl Add a -R to the RUNTIME_PATH.  Only if rpath is enabled and it is
575dnl an absolute path.
576dnl $1: the pathname to add.
577AC_DEFUN([ACX_RUNTIME_PATH_ADD], [
578	if test "x$enable_rpath" = xyes; then
579		if echo "$1" | grep "^/" >/dev/null; then
580			RUNTIME_PATH="$RUNTIME_PATH -R$1"
581		fi
582	fi
583])
584
585dnl Common code for both ACX_WITH_SSL and ACX_WITH_SSL_OPTIONAL
586dnl Takes one argument; the withval checked in those 2 functions
587dnl sets up the environment for the given openssl path
588AC_DEFUN([ACX_SSL_CHECKS], [
589    withval=$1
590    if test x_$withval != x_no; then
591        AC_MSG_CHECKING(for SSL)
592        if test x_$withval = x_ -o x_$withval = x_yes; then
593            withval="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr"
594        fi
595        for dir in $withval; do
596            ssldir="$dir"
597            if test -f "$dir/include/openssl/ssl.h"; then
598                found_ssl="yes"
599                AC_DEFINE_UNQUOTED([HAVE_SSL], [], [Define if you have the SSL libraries installed.])
600                dnl assume /usr/include is already in the include-path.
601                if test "$ssldir" != "/usr"; then
602                        CPPFLAGS="$CPPFLAGS -I$ssldir/include"
603                        LIBSSL_CPPFLAGS="$LIBSSL_CPPFLAGS -I$ssldir/include"
604                fi
605                break;
606            fi
607        done
608        if test x_$found_ssl != x_yes; then
609            AC_MSG_ERROR(Cannot find the SSL libraries in $withval)
610        else
611            AC_MSG_RESULT(found in $ssldir)
612            HAVE_SSL=yes
613            dnl assume /usr is already in the lib and dynlib paths.
614            if test "$ssldir" != "/usr" -a "$ssldir" != ""; then
615                LDFLAGS="$LDFLAGS -L$ssldir/lib"
616                LIBSSL_LDFLAGS="$LIBSSL_LDFLAGS -L$ssldir/lib"
617                ACX_RUNTIME_PATH_ADD([$ssldir/lib])
618            fi
619
620            AC_MSG_CHECKING([for HMAC_CTX_init in -lcrypto])
621            LIBS="$LIBS -lcrypto"
622            LIBSSL_LIBS="$LIBSSL_LIBS -lcrypto"
623            AC_TRY_LINK(, [
624                int HMAC_CTX_init(void);
625                (void)HMAC_CTX_init();
626              ], [
627                AC_MSG_RESULT(yes)
628                AC_DEFINE([HAVE_HMAC_CTX_INIT], 1,
629                          [If you have HMAC_CTX_init])
630              ], [
631                AC_MSG_RESULT(no)
632                # check if -lwsock32 or -lgdi32 are needed.
633                BAKLIBS="$LIBS"
634                BAKSSLLIBS="$LIBSSL_LIBS"
635                LIBS="$LIBS -lgdi32"
636                LIBSSL_LIBS="$LIBSSL_LIBS -lgdi32"
637                AC_MSG_CHECKING([if -lcrypto needs -lgdi32])
638                AC_TRY_LINK([], [
639                    int HMAC_CTX_init(void);
640                    (void)HMAC_CTX_init();
641                  ],[
642                    AC_DEFINE([HAVE_HMAC_CTX_INIT], 1,
643                        [If you have HMAC_CTX_init])
644                    AC_MSG_RESULT(yes)
645                  ],[
646                    AC_MSG_RESULT(no)
647                    LIBS="$BAKLIBS"
648                    LIBSSL_LIBS="$BAKSSLLIBS"
649                    LIBS="$LIBS -ldl"
650                    LIBSSL_LIBS="$LIBSSL_LIBS -ldl"
651                    AC_MSG_CHECKING([if -lcrypto needs -ldl])
652                    AC_TRY_LINK([], [
653                        int HMAC_CTX_init(void);
654                        (void)HMAC_CTX_init();
655                      ],[
656                        AC_DEFINE([HAVE_HMAC_CTX_INIT], 1,
657                            [If you have HMAC_CTX_init])
658                        AC_MSG_RESULT(yes)
659                      ],[
660                        AC_MSG_RESULT(no)
661                    AC_MSG_ERROR([OpenSSL found in $ssldir, but version 0.9.7 or higher is required])
662                    ])
663                ])
664            ])
665        fi
666        AC_SUBST(HAVE_SSL)
667        AC_SUBST(RUNTIME_PATH)
668	# openssl engine functionality needs dlopen().
669	BAKLIBS="$LIBS"
670	AC_SEARCH_LIBS([dlopen], [dl])
671	if test "$LIBS" != "$BAKLIBS"; then
672		LIBSSL_LIBS="$LIBSSL_LIBS -ldl"
673	fi
674    fi
675AC_CHECK_HEADERS([openssl/ssl.h],,, [AC_INCLUDES_DEFAULT])
676AC_CHECK_HEADERS([openssl/err.h],,, [AC_INCLUDES_DEFAULT])
677AC_CHECK_HEADERS([openssl/rand.h],,, [AC_INCLUDES_DEFAULT])
678])dnl End of ACX_SSL_CHECKS
679
680dnl Check for SSL, where SSL is mandatory
681dnl Adds --with-ssl option, searches for openssl and defines HAVE_SSL if found
682dnl Setup of CPPFLAGS, CFLAGS.  Adds -lcrypto to LIBS.
683dnl Checks main header files of SSL.
684dnl
685AC_DEFUN([ACX_WITH_SSL],
686[
687AC_ARG_WITH(ssl, AC_HELP_STRING([--with-ssl=pathname],
688                                    [enable SSL (will check /usr/local/ssl
689                            /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr)]),[
690        ],[
691            withval="yes"
692        ])
693    if test x_$withval = x_no; then
694	AC_MSG_ERROR([Need SSL library to do digital signature cryptography])
695    fi
696    ACX_SSL_CHECKS($withval)
697])dnl End of ACX_WITH_SSL
698
699dnl Check for SSL, where ssl is optional (--without-ssl is allowed)
700dnl Adds --with-ssl option, searches for openssl and defines HAVE_SSL if found
701dnl Setup of CPPFLAGS, CFLAGS.  Adds -lcrypto to LIBS.
702dnl Checks main header files of SSL.
703dnl
704AC_DEFUN([ACX_WITH_SSL_OPTIONAL],
705[
706AC_ARG_WITH(ssl, AC_HELP_STRING([--with-ssl=pathname],
707                                [enable SSL (will check /usr/local/ssl
708                                /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr)]),[
709        ],[
710            withval="yes"
711        ])
712    ACX_SSL_CHECKS($withval)
713])dnl End of ACX_WITH_SSL_OPTIONAL
714
715dnl Setup to use -lssl
716dnl To use -lcrypto, use the ACX_WITH_SSL setup (before this one).
717AC_DEFUN([ACX_LIB_SSL],
718[
719# check if libssl needs libdl
720BAKLIBS="$LIBS"
721LIBS="-lssl $LIBS"
722AC_MSG_CHECKING([if libssl needs libdl])
723AC_TRY_LINK_FUNC([SSL_CTX_new], [
724	AC_MSG_RESULT([no])
725	LIBS="$BAKLIBS"
726] , [
727	AC_MSG_RESULT([yes])
728	LIBS="$BAKLIBS"
729	AC_SEARCH_LIBS([dlopen], [dl])
730]) ])dnl End of ACX_LIB_SSL
731
732dnl Setup to use very large files (>2Gb).
733dnl setups fseeko and its own
734AC_DEFUN([ACX_SYS_LARGEFILE],
735[
736AC_SYS_LARGEFILE
737dnl try to see if an additional _LARGEFILE_SOURCE 1 is needed to get fseeko
738ACX_CHECK_COMPILER_FLAG_NEEDED(-D_LARGEFILE_SOURCE=1,
739[
740#include <stdio.h>
741int test() {
742        int a = fseeko(stdin, 0, 0);
743        return a;
744}
745], [CFLAGS="$CFLAGS -D_LARGEFILE_SOURCE=1"])
746])
747
748dnl Check getaddrinfo.
749dnl Works on linux, solaris, bsd and windows(links winsock).
750dnl defines HAVE_GETADDRINFO, USE_WINSOCK.
751AC_DEFUN([ACX_CHECK_GETADDRINFO_WITH_INCLUDES],
752[AC_REQUIRE([AC_PROG_CC])
753AC_MSG_CHECKING(for getaddrinfo)
754ac_cv_func_getaddrinfo=no
755AC_LINK_IFELSE(
756[
757#ifdef __cplusplus
758extern "C"
759{
760#endif
761char* getaddrinfo();
762char* (*f) () = getaddrinfo;
763#ifdef __cplusplus
764}
765#endif
766int main() {
767        ;
768        return 0;
769}
770],
771dnl this case on linux, solaris, bsd
772[ac_cv_func_getaddrinfo="yes"],
773dnl no quick getaddrinfo, try mingw32 and winsock2 library.
774ORIGLIBS="$LIBS"
775LIBS="$LIBS -lws2_32"
776AC_LINK_IFELSE(
777AC_LANG_PROGRAM(
778[
779#ifdef HAVE_WS2TCPIP_H
780#include <ws2tcpip.h>
781#endif
782],
783[
784        (void)getaddrinfo(NULL, NULL, NULL, NULL);
785]
786),
787[
788ac_cv_func_getaddrinfo="yes"
789dnl already: LIBS="$LIBS -lws2_32"
790AC_DEFINE(USE_WINSOCK, 1, [Whether the windows socket API is used])
791USE_WINSOCK="1"
792],
793[
794ac_cv_func_getaddrinfo="no"
795LIBS="$ORIGLIBS"
796])
797)
798
799AC_MSG_RESULT($ac_cv_func_getaddrinfo)
800if test $ac_cv_func_getaddrinfo = yes; then
801  AC_DEFINE(HAVE_GETADDRINFO, 1, [Whether getaddrinfo is available])
802fi
803])dnl Endof AC_CHECK_GETADDRINFO_WITH_INCLUDES
804
805dnl check if a function is deprecated. defines DEPRECATED_func in config.h.
806dnl $1: function name
807dnl $2: C-statement that calls the function.
808dnl $3: includes for the program.
809dnl $4: executes if yes
810dnl $5: executes if no
811AC_DEFUN([ACX_FUNC_DEPRECATED],
812[
813AC_REQUIRE([AC_PROG_CC])
814AC_MSG_CHECKING(if $1 is deprecated)
815cache=`echo $1 | sed 'y%.=/+-%___p_%'`
816AC_CACHE_VAL(cv_cc_deprecated_$cache,
817[
818echo '$3' >conftest.c
819echo 'void f(){ $2 }' >>conftest.c
820if test -z "`$CC -c conftest.c 2>&1 | grep deprecated`"; then
821eval "cv_cc_deprecated_$cache=no"
822else
823eval "cv_cc_deprecated_$cache=yes"
824fi
825rm -f conftest conftest.o conftest.c
826])
827if eval "test \"`echo '$cv_cc_deprecated_'$cache`\" = yes"; then
828AC_MSG_RESULT(yes)
829AC_DEFINE_UNQUOTED(AS_TR_CPP([DEPRECATED_$1]), 1, [Whether $1 is deprecated])
830:
831$4
832else
833AC_MSG_RESULT(no)
834:
835$5
836fi
837])dnl end of ACX_FUNC_DEPRECATED
838
839dnl check if select and nonblocking sockets actually work.
840dnl Needs fork(2) and select(2).
841dnl defines NONBLOCKING_IS_BROKEN, and if that is true multiple reads from
842dnl a nonblocking socket do not work, a new call to select is necessary.
843AC_DEFUN([ACX_CHECK_NONBLOCKING_BROKEN],
844[
845AC_MSG_CHECKING([if nonblocking sockets work])
846if echo $target | grep mingw32 >/dev/null; then
847	AC_MSG_RESULT([no (windows)])
848	AC_DEFINE([NONBLOCKING_IS_BROKEN], 1, [Define if the network stack does not fully support nonblocking io (causes lower performance).])
849else
850AC_RUN_IFELSE(AC_LANG_PROGRAM([
851#include <stdio.h>
852#include <string.h>
853#include <stdlib.h>
854#include <fcntl.h>
855#include <errno.h>
856#ifdef HAVE_SYS_TYPES_H
857#include <sys/types.h>
858#endif
859#ifdef HAVE_SYS_SOCKET_H
860#include <sys/socket.h>
861#endif
862#ifdef HAVE_NETINET_IN_H
863#include <netinet/in.h>
864#endif
865#ifdef HAVE_ARPA_INET_H
866#include <arpa/inet.h>
867#endif
868#ifdef HAVE_UNISTD_H
869#include <unistd.h>
870#endif
871#ifdef HAVE_TIME_H
872#include <time.h>
873#endif
874],[[
875	int port;
876	int sfd, cfd;
877	int num = 10;
878	int i, p;
879	struct sockaddr_in a;
880	/* test if select and nonblocking reads work well together */
881	/* open port.
882	   fork child to send 10 messages.
883	   select to read.
884	   then try to nonblocking read the 10 messages
885	   then, nonblocking read must give EAGAIN
886	*/
887
888	port = 12345 + (time(0)%32);
889	sfd = socket(PF_INET, SOCK_DGRAM, 0);
890	if(sfd == -1) {
891		perror("socket");
892		return 1;
893	}
894	memset(&a, 0, sizeof(a));
895	a.sin_family = AF_INET;
896	a.sin_port = htons(port);
897	a.sin_addr.s_addr = inet_addr("127.0.0.1");
898	if(bind(sfd, (struct sockaddr*)&a, sizeof(a)) < 0) {
899		perror("bind");
900		return 1;
901	}
902	if(fcntl(sfd, F_SETFL, O_NONBLOCK) == -1) {
903		perror("fcntl");
904		return 1;
905	}
906
907	cfd = socket(PF_INET, SOCK_DGRAM, 0);
908	if(cfd == -1) {
909		perror("client socket");
910		return 1;
911	}
912	a.sin_port = 0;
913	if(bind(cfd, (struct sockaddr*)&a, sizeof(a)) < 0) {
914		perror("client bind");
915		return 1;
916	}
917	a.sin_port = htons(port);
918
919	/* no handler, causes exit in 10 seconds */
920	alarm(10);
921
922	/* send and receive on the socket */
923	if((p=fork()) == 0) {
924		for(i=0; i<num; i++) {
925			if(sendto(cfd, &i, sizeof(i), 0,
926				(struct sockaddr*)&a, sizeof(a)) < 0) {
927				perror("sendto");
928				return 1;
929			}
930		}
931	} else {
932		/* parent */
933		fd_set rset;
934		int x;
935		if(p == -1) {
936			perror("fork");
937			return 1;
938		}
939		FD_ZERO(&rset);
940		FD_SET(sfd, &rset);
941		if(select(sfd+1, &rset, NULL, NULL, NULL) < 1) {
942			perror("select");
943			return 1;
944		}
945		i = 0;
946		while(i < num) {
947			if(recv(sfd, &x, sizeof(x), 0) != sizeof(x)) {
948				if(errno == EAGAIN)
949					continue;
950				perror("recv");
951				return 1;
952			}
953			i++;
954		}
955		/* now we want to get EAGAIN: nonblocking goodness */
956		errno = 0;
957		recv(sfd, &x, sizeof(x), 0);
958		if(errno != EAGAIN) {
959			perror("trying to recv again");
960			return 1;
961		}
962		/* EAGAIN encountered */
963	}
964
965	close(sfd);
966	close(cfd);
967]]), [
968	AC_MSG_RESULT([yes])
969], [
970	AC_MSG_RESULT([no])
971	AC_DEFINE([NONBLOCKING_IS_BROKEN], 1, [Define if the network stack does not fully support nonblocking io (causes lower performance).])
972], [
973	AC_MSG_RESULT([crosscompile(yes)])
974])
975fi
976])dnl End of ACX_CHECK_NONBLOCKING_BROKEN
977
978dnl Check if mkdir has one or two arguments.
979dnl defines MKDIR_HAS_ONE_ARG
980AC_DEFUN([ACX_MKDIR_ONE_ARG],
981[
982AC_MSG_CHECKING([whether mkdir has one arg])
983AC_TRY_COMPILE([
984#include <stdio.h>
985#include <unistd.h>
986#ifdef HAVE_WINSOCK2_H
987#include <winsock2.h>
988#endif
989#ifdef HAVE_SYS_STAT_H
990#include <sys/stat.h>
991#endif
992], [
993	(void)mkdir("directory");
994],
995AC_MSG_RESULT(yes)
996AC_DEFINE(MKDIR_HAS_ONE_ARG, 1, [Define if mkdir has one argument.])
997,
998AC_MSG_RESULT(no)
999)
1000])dnl end of ACX_MKDIR_ONE_ARG
1001
1002dnl Check for ioctlsocket function. works on mingw32 too.
1003AC_DEFUN([ACX_FUNC_IOCTLSOCKET],
1004[
1005# check ioctlsocket
1006AC_MSG_CHECKING(for ioctlsocket)
1007AC_LINK_IFELSE(AC_LANG_PROGRAM([
1008#ifdef HAVE_WINSOCK2_H
1009#include <winsock2.h>
1010#endif
1011], [
1012	(void)ioctlsocket(0, 0, NULL);
1013]), [
1014AC_MSG_RESULT(yes)
1015AC_DEFINE(HAVE_IOCTLSOCKET, 1, [if the function 'ioctlsocket' is available])
1016],[AC_MSG_RESULT(no)])
1017])dnl end of ACX_FUNC_IOCTLSOCKET
1018
1019dnl detect malloc and provide malloc compat prototype.
1020dnl $1: unique name for compat code
1021AC_DEFUN([ACX_FUNC_MALLOC],
1022[
1023	AC_FUNC_MALLOC
1024	if test "$ac_cv_func_malloc_0_nonnull" = no; then
1025		AC_DEFINE_UNQUOTED([malloc], [rpl_malloc_$1], [Define if  replacement function should be used.])
1026	fi
1027])
1028
1029dnl Define fallback for fseeko and ftello if needed.
1030AC_DEFUN([AHX_CONFIG_FSEEKO],
1031[
1032#ifndef HAVE_FSEEKO
1033#define fseeko fseek
1034#define ftello ftell
1035#endif /* HAVE_FSEEKO */
1036])
1037
1038dnl Define RAND_MAX if not defined
1039AC_DEFUN([AHX_CONFIG_RAND_MAX],
1040[
1041#ifndef RAND_MAX
1042#define RAND_MAX	2147483647
1043#endif
1044])
1045
1046dnl Define MAXHOSTNAMELEN if not defined
1047AC_DEFUN([AHX_CONFIG_MAXHOSTNAMELEN],
1048[
1049#ifndef MAXHOSTNAMELEN
1050#define MAXHOSTNAMELEN 256
1051#endif
1052])
1053
1054dnl Define IPV6_MIN_MTU if not defined
1055AC_DEFUN([AHX_CONFIG_IPV6_MIN_MTU],
1056[
1057#ifndef IPV6_MIN_MTU
1058#define IPV6_MIN_MTU 1280
1059#endif /* IPV6_MIN_MTU */
1060])
1061
1062dnl provide snprintf, vsnprintf compat prototype
1063dnl $1: unique name for compat code
1064AC_DEFUN([AHX_CONFIG_SNPRINTF],
1065[
1066#ifndef HAVE_SNPRINTF
1067#define snprintf snprintf_$1
1068#define vsnprintf vsnprintf_$1
1069#include <stdarg.h>
1070int snprintf (char *str, size_t count, const char *fmt, ...);
1071int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
1072#endif /* HAVE_SNPRINTF */
1073])
1074
1075dnl provide inet_pton compat prototype.
1076dnl $1: unique name for compat code
1077AC_DEFUN([AHX_CONFIG_INET_PTON],
1078[
1079#ifndef HAVE_INET_PTON
1080#define inet_pton inet_pton_$1
1081int inet_pton(int af, const char* src, void* dst);
1082#endif /* HAVE_INET_PTON */
1083])
1084
1085dnl provide inet_ntop compat prototype.
1086dnl $1: unique name for compat code
1087AC_DEFUN([AHX_CONFIG_INET_NTOP],
1088[
1089#ifndef HAVE_INET_NTOP
1090#define inet_ntop inet_ntop_$1
1091const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1092#endif
1093])
1094
1095dnl provide inet_aton compat prototype.
1096dnl $1: unique name for compat code
1097AC_DEFUN([AHX_CONFIG_INET_ATON],
1098[
1099#ifndef HAVE_INET_ATON
1100#define inet_aton inet_aton_$1
1101int inet_aton(const char *cp, struct in_addr *addr);
1102#endif
1103])
1104
1105dnl provide memmove compat prototype.
1106dnl $1: unique name for compat code
1107AC_DEFUN([AHX_CONFIG_MEMMOVE],
1108[
1109#ifndef HAVE_MEMMOVE
1110#define memmove memmove_$1
1111void *memmove(void *dest, const void *src, size_t n);
1112#endif
1113])
1114
1115dnl provide strlcat compat prototype.
1116dnl $1: unique name for compat code
1117AC_DEFUN([AHX_CONFIG_STRLCAT],
1118[
1119#ifndef HAVE_STRLCAT
1120#define strlcat strlcat_$1
1121size_t strlcat(char *dst, const char *src, size_t siz);
1122#endif
1123])
1124
1125dnl provide strlcpy compat prototype.
1126dnl $1: unique name for compat code
1127AC_DEFUN([AHX_CONFIG_STRLCPY],
1128[
1129#ifndef HAVE_STRLCPY
1130#define strlcpy strlcpy_$1
1131size_t strlcpy(char *dst, const char *src, size_t siz);
1132#endif
1133])
1134
1135dnl provide gmtime_r compat prototype.
1136dnl $1: unique name for compat code
1137AC_DEFUN([AHX_CONFIG_GMTIME_R],
1138[
1139#ifndef HAVE_GMTIME_R
1140#define gmtime_r gmtime_r_$1
1141struct tm *gmtime_r(const time_t *timep, struct tm *result);
1142#endif
1143])
1144
1145dnl provide w32 compat definition for sleep
1146AC_DEFUN([AHX_CONFIG_W32_SLEEP],
1147[
1148#ifndef HAVE_SLEEP
1149#define sleep(x) Sleep((x)*1000) /* on win32 */
1150#endif /* HAVE_SLEEP */
1151])
1152
1153dnl provide w32 compat definition for usleep
1154AC_DEFUN([AHX_CONFIG_W32_USLEEP],
1155[
1156#ifndef HAVE_USLEEP
1157#define usleep(x) Sleep((x)/1000 + 1) /* on win32 */
1158#endif /* HAVE_USLEEP */
1159])
1160
1161dnl provide w32 compat definition for random
1162AC_DEFUN([AHX_CONFIG_W32_RANDOM],
1163[
1164#ifndef HAVE_RANDOM
1165#define random rand /* on win32, for tests only (bad random) */
1166#endif /* HAVE_RANDOM */
1167])
1168
1169dnl provide w32 compat definition for srandom
1170AC_DEFUN([AHX_CONFIG_W32_SRANDOM],
1171[
1172#ifndef HAVE_SRANDOM
1173#define srandom(x) srand(x) /* on win32, for tests only (bad random) */
1174#endif /* HAVE_SRANDOM */
1175])
1176
1177dnl provide w32 compat definition for FD_SET_T
1178AC_DEFUN([AHX_CONFIG_W32_FD_SET_T],
1179[
1180/* detect if we need to cast to unsigned int for FD_SET to avoid warnings */
1181#ifdef HAVE_WINSOCK2_H
1182#define FD_SET_T (u_int)
1183#else
1184#define FD_SET_T
1185#endif
1186])
1187
1188dnl Remove an extension flag from CFLAGS, define replacement to be made.
1189dnl Used by ACX_STRIP_EXT_FLAGS.
1190dnl $1: the name of the flag, for example -D_GNU_SOURCE.
1191AC_DEFUN([ACX_CFLAGS_STRIP],
1192[
1193  if echo $CFLAGS | grep " $1" >/dev/null 2>&1; then
1194    CFLAGS="`echo $CFLAGS | sed -e 's/ $1//g'`"
1195    AC_DEFINE(AS_TR_CPP(OMITTED_$1), 1, Put $1 define in config.h)
1196  fi
1197])
1198
1199dnl Remove EXT flags from the CFLAGS and set them to be defined in config.h
1200dnl use with ACX_DETERMINE_EXT_FLAGS.
1201AC_DEFUN([ACX_STRIP_EXT_FLAGS],
1202[
1203  AC_MSG_NOTICE([Stripping extension flags...])
1204  ACX_CFLAGS_STRIP(-D_GNU_SOURCE)
1205  ACX_CFLAGS_STRIP(-D_BSD_SOURCE)
1206  ACX_CFLAGS_STRIP(-D__EXTENSIONS__)
1207  ACX_CFLAGS_STRIP(-D_POSIX_C_SOURCE=200112)
1208  ACX_CFLAGS_STRIP(-D_XOPEN_SOURCE=600)
1209  ACX_CFLAGS_STRIP(-D_XOPEN_SOURCE_EXTENDED=1)
1210  ACX_CFLAGS_STRIP(-D_ALL_SOURCE)
1211  ACX_CFLAGS_STRIP(-D_LARGEFILE_SOURCE=1)
1212]) dnl End of ACX_STRIP_EXT_FLAGS
1213
1214dnl define one omitted flag for config.h
1215dnl $1: flag name. -D_GNU_SOURCE
1216dnl $2: replacement define. _GNU_SOURCE
1217dnl $3: define value, 1
1218AC_DEFUN([AHX_CONFIG_FLAG_OMITTED],
1219[#if defined($1) && !defined($2)
1220#define $2 $3
1221[#]endif ])
1222
1223dnl Wrapper for AHX_CONFIG_FLAG_OMITTED for -D style flags
1224dnl $1: the -DNAME or -DNAME=value string.
1225AC_DEFUN([AHX_CONFIG_FLAG_EXT],
1226[AHX_CONFIG_FLAG_OMITTED(AS_TR_CPP(OMITTED_$1),m4_bpatsubst(m4_bpatsubst($1,-D,),=.*$,),m4_if(m4_bregexp($1,=),-1,1,m4_bpatsubst($1,^.*=,)))
1227])
1228
1229dnl config.h part to define omitted cflags, use with ACX_STRIP_EXT_FLAGS.
1230AC_DEFUN([AHX_CONFIG_EXT_FLAGS],
1231[AHX_CONFIG_FLAG_EXT(-D_GNU_SOURCE)
1232AHX_CONFIG_FLAG_EXT(-D_BSD_SOURCE)
1233AHX_CONFIG_FLAG_EXT(-D__EXTENSIONS__)
1234AHX_CONFIG_FLAG_EXT(-D_POSIX_C_SOURCE=200112)
1235AHX_CONFIG_FLAG_EXT(-D_XOPEN_SOURCE=600)
1236AHX_CONFIG_FLAG_EXT(-D_XOPEN_SOURCE_EXTENDED=1)
1237AHX_CONFIG_FLAG_EXT(-D_ALL_SOURCE)
1238AHX_CONFIG_FLAG_EXT(-D_LARGEFILE_SOURCE=1)
1239])
1240
1241dnl check if memcmp is using signed characters and replace if so.
1242AC_DEFUN([ACX_CHECK_MEMCMP_SIGNED],
1243[AC_MSG_CHECKING([if memcmp compares unsigned])
1244AC_RUN_IFELSE([AC_LANG_SOURCE([[
1245#include <stdio.h>
1246#include <stdlib.h>
1247#include <string.h>
1248int main(void)
1249{
1250	char a = 255, b = 0;
1251	if(memcmp(&a, &b, 1) < 0)
1252		return 1;
1253	return 0;
1254}
1255]])], [AC_MSG_RESULT([yes]) ],
1256[ AC_MSG_RESULT([no])
1257  AC_DEFINE([MEMCMP_IS_BROKEN], [1], [Define if memcmp() does not compare unsigned bytes])
1258  AC_LIBOBJ([memcmp])
1259], [ AC_MSG_RESULT([cross-compile no])
1260  AC_DEFINE([MEMCMP_IS_BROKEN], [1], [Define if memcmp() does not compare unsigned bytes])
1261  AC_LIBOBJ([memcmp])
1262]) ])
1263
1264dnl define memcmp to its replacement, pass unique id for program as arg
1265AC_DEFUN([AHX_MEMCMP_BROKEN], [
1266#ifdef MEMCMP_IS_BROKEN
1267#  ifdef memcmp
1268#  undef memcmp
1269#  endif
1270#define memcmp memcmp_$1
1271int memcmp(const void *x, const void *y, size_t n);
1272#endif
1273])
1274
1275dnl ACX_CHECK_SS_FAMILY           - check for sockaddr_storage.ss_family
1276AC_DEFUN([ACX_CHECK_SS_FAMILY],
1277[AC_CHECK_MEMBER([struct sockaddr_storage.ss_family], [], [
1278        AC_CHECK_MEMBER([struct sockaddr_storage.__ss_family], [
1279                AC_DEFINE([ss_family], [__ss_family], [Fallback member name for socket family in struct sockaddr_storage])
1280        ],, [AC_INCLUDES_DEFAULT
1281#ifdef HAVE_NETINET_IN_H
1282#include <netinet/in.h>
1283#endif
1284#ifdef HAVE_SYS_SOCKET_H
1285#include <sys/socket.h>
1286#endif
1287#ifdef HAVE_NETDB_H
1288#include <netdb.h>
1289#endif
1290#ifdef HAVE_ARPA_INET_H
1291#include <arpa/inet.h>
1292#endif
1293    ])
1294], [AC_INCLUDES_DEFAULT
1295#ifdef HAVE_NETINET_IN_H
1296#include <netinet/in.h>
1297#endif
1298#ifdef HAVE_SYS_SOCKET_H
1299#include <sys/socket.h>
1300#endif
1301#ifdef HAVE_NETDB_H
1302#include <netdb.h>
1303#endif
1304#ifdef HAVE_ARPA_INET_H
1305#include <arpa/inet.h>
1306#endif
1307]) ])
1308
1309dnl End of file
1310