summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xconfigure1
-rw-r--r--make/test/arc4random_buf.cpp26
-rw-r--r--src/helperfuncs.cpp10
3 files changed, 34 insertions, 3 deletions
diff --git a/configure b/configure
index db0464d19..ee11086b5 100755
--- a/configure
+++ b/configure
@@ -159,6 +159,7 @@ unless ($config{CXX}) {
}
my %compiler = get_compiler_info($config{CXX});
+$config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
$config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
$config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
diff --git a/make/test/arc4random_buf.cpp b/make/test/arc4random_buf.cpp
new file mode 100644
index 000000000..86f74f624
--- /dev/null
+++ b/make/test/arc4random_buf.cpp
@@ -0,0 +1,26 @@
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ * Copyright (C) 2018 Peter Powell <petpow@saberuk.com>
+ *
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <stdlib.h>
+
+int main() {
+ char buffer[100];
+ arc4random_buf(buffer, sizeof(buffer));
+ return 0;
+}
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index 8ced78ae8..6830612c7 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -477,8 +477,11 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max)
// This is overridden by a higher-quality algorithm when SSL support is loaded
void InspIRCd::DefaultGenRandom(char* output, size_t max)
{
- for(unsigned int i=0; i < max; i++)
-#ifdef _WIN32
+#if defined HAS_ARC4RANDOM_BUF
+ arc4random_buf(output, max);
+#else
+ for (unsigned int i = 0; i < max; ++i)
+# ifdef _WIN32
{
unsigned int uTemp;
if(rand_s(&uTemp) != 0)
@@ -486,7 +489,8 @@ void InspIRCd::DefaultGenRandom(char* output, size_t max)
else
output[i] = uTemp;
}
-#else
+# else
output[i] = random();
+# endif
#endif
}