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