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