From 93b72115d103f1c64be87699a15402044ff6765b Mon Sep 17 00:00:00 2001 From: Joshua Colp Date: Sat, 21 Jul 2007 14:39:52 +0000 Subject: Add support for using /dev/urandom to get random numbers on systems that support it. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@76296 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/utils.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'main/utils.c') diff --git a/main/utils.c b/main/utils.c index d719837af..fd39d5228 100644 --- a/main/utils.c +++ b/main/utils.c @@ -40,6 +40,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #include +#ifdef HAVE_DEV_URANDOM +#include +#endif + #define AST_API_MODULE /* ensure that inlinable API functions will be built in lock.h if required */ #include "asterisk/lock.h" #include "asterisk/io.h" @@ -501,8 +505,15 @@ const char *ast_inet_ntoa(struct in_addr ia) return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN); } +#ifdef HAVE_DEV_URANDOM +static int dev_urandom_fd; +#endif + int ast_utils_init(void) { +#ifdef HAVE_DEV_URANDOM + dev_urandom_fd = open("/dev/urandom", O_RDONLY); +#endif base64_init(); return 0; } @@ -807,19 +818,30 @@ struct timeval ast_tvsub(struct timeval a, struct timeval b) /*! \brief glibc puts a lock inside random(3), so that the results are thread-safe. * BSD libc (and others) do not. */ -#ifndef linux +#ifndef linux AST_MUTEX_DEFINE_STATIC(randomlock); +#endif long int ast_random(void) { long int res; +#ifdef HAVE_DEV_URANDOM + if (dev_urandom_fd >= 0) { + int read_res = read(dev_urandom_fd, &res, sizeof(res)); + if (read_res > 0) + return res; + } +#endif +#ifdef linux + res = random(); +#else ast_mutex_lock(&randomlock); res = random(); ast_mutex_unlock(&randomlock); +#endif return res; } -#endif char *ast_process_quotes_and_slashes(char *start, char find, char replace_with) { -- cgit v1.2.3