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