From 85f77e5953ce6a60235ea3d5af5799668183b497 Mon Sep 17 00:00:00 2001
From: Toni Uhlig <matzeton@googlemail.com>
Date: Wed, 26 Dec 2018 21:12:37 +0100
Subject: support BSD arc4random() if available

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
---
 configure.ac    | 14 +++++++++++---
 src/Makefile.am |  4 ++++
 src/utils.c     |  7 +++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index b0534e0..c9ee465 100644
--- a/configure.ac
+++ b/configure.ac
@@ -146,9 +146,16 @@ if test x"${pcap_enabled}" != x -a \
 fi
 
 dnl Check for more secure randomization functions
-AC_CHECK_FUNCS([timespec_get srandom random],
-               [random_enabled=yes],
-               [random_enabled=])
+AC_CHECK_HEADERS([bsd/stdlib.h],, [random_enabled=yes])
+AC_SEARCH_LIBS([arc4random], [bsd],,,)
+AC_CHECK_FUNCS([arc4random], [random_enabled=],)
+if test x"${random_enabled}" != x; then
+	AC_CHECK_FUNCS([timespec_get srandom random],
+	               [random_enabled=yes],
+	               [random_enabled=])
+else
+	arc4random_enabled=yes
+fi
 
 dnl Check for SELINUX
 if test x"${selinux_enabled}" != x; then
@@ -185,6 +192,7 @@ AM_CONDITIONAL([HAVE_SELINUX], [test x"${selinux_enabled}" = xyes])
 AM_CONDITIONAL([IS_WINDOWS], [test x"${use_msw}" = xyes])
 AM_CONDITIONAL([HAVE_ICMPFILTER], [test x"${with_icmp_filter}" = xyes])
 AM_CONDITIONAL([HAVE_RANDOM], [test x"${random_enabled}" = xyes])
+AM_CONDITIONAL([HAVE_ARC4RANDOM], [test x"${arc4random_enabled}" = xyes])
 
 dnl output config headers
 AC_CONFIG_HEADERS([src/config.h:src/config.h.in])
diff --git a/src/Makefile.am b/src/Makefile.am
index da23fd8..1d161a2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,9 +18,13 @@ if HAVE_ICMPFILTER
 ptunnel_ng_CFLAGS += -DHAVE_ICMPFILTER=1
 endif
 
+if HAVE_ARC4RANDOM
+ptunnel_ng_CFLAGS += -DHAVE_ARC4RANDOM=1
+else
 if HAVE_RANDOM
 ptunnel_ng_CFLAGS += -DHAVE_RANDOM=1
 endif
+endif
 
 ptunnel_ng_SOURCES = \
 	md5.c \
diff --git a/src/utils.c b/src/utils.c
index 6233753..10e8182 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -49,6 +49,9 @@
 #include <string.h>
 #include <time.h>
 #include <assert.h>
+#ifdef HAVE_ARC4RANDOM
+#include <bsd/stdlib.h>
+#endif
 
 #ifndef WIN32
 #include <syslog.h>
@@ -148,6 +151,9 @@ void print_hexstr(unsigned char *buf, size_t siz) {
 #endif
 
 int pt_random(void) {
+#ifdef HAVE_ARC4RANDOM
+	return arc4random();
+#else
 #ifdef HAVE_RANDOM
 #ifndef TIME_UTC
 #define TIME_UTC 1
@@ -161,4 +167,5 @@ int pt_random(void) {
 	srand(time(0));
 	return rand();
 #endif
+#endif
 }
-- 
cgit v1.2.3