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