From: Roy Marples Date: Mon, 19 May 2008 09:16:55 +0000 (+0000) Subject: Create a real arc4linux function so we don't have to visible seed random in dhcpcd... X-Git-Tag: v4.0.2~383 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32391c95110f44cd7dd0245221c21b7e0d7e5d5a;p=thirdparty%2Fdhcpcd.git Create a real arc4linux function so we don't have to visible seed random in dhcpcd main. --- diff --git a/common.c b/common.c index e207258e..91fcd7a9 100644 --- a/common.c +++ b/common.c @@ -33,9 +33,11 @@ #ifdef BSD # include #endif +#include #include #include #include +#include #include #include "common.h" @@ -74,6 +76,27 @@ get_line(char **line, size_t *len, FILE *fp) return last; } +/* Simple hack to return a random number without arc4random */ +#ifndef HAVE_ARC4RANDOM +uint32_t arc4random(void) +{ + int fd; + static unsigned long seed = 0; + + if (!seed) { + fd = open("/dev/urandom", 0); + if (fd == -1 || read(fd, &seed, sizeof(seed)) == -1) + seed = time(0); + if (fd >= 0) + close(fd); + + srandom(seed); + } + + return (uint32_t)random(); +} +#endif + /* strlcpy is nice, shame glibc does not define it */ #if HAVE_STRLCPY #else diff --git a/common.h b/common.h index 41918467..9df668a8 100644 --- a/common.h +++ b/common.h @@ -29,9 +29,9 @@ #define COMMON_H /* string.h pulls in features.h so the below define checks work */ -#include #include #include +#include #include #include @@ -41,8 +41,12 @@ # define _unused #endif -#ifdef __GLIBC__ -#define arc4random (uint32_t)random +#ifndef HAVE_ARC4RANDOM +# ifdef __GLIBC__ +uint32_t arc4random(void); +#else +# define HAVE_ARC4RANDOM +# endif #endif #ifndef HAVE_STRLCPY diff --git a/dhcpcd.c b/dhcpcd.c index 4f2725bf..92701094 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -40,9 +40,6 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; #include #include #include -#ifdef __GLIBC__ -# include /* for srandomdev */ -#endif #include #include "config.h" @@ -136,22 +133,6 @@ atoint(const char *s) return (int)n; } -#ifdef __GLIBC__ -static void srandomdev(void) -{ - int fd; - unsigned long seed; - - fd = open("/dev/urandom", 0); - if (fd == -1 || read(fd, &seed, sizeof(seed)) == -1) - seed = time(0); - if (fd >= 0) - close(fd); - - srandom(seed); -} -#endif - static pid_t read_pid(const char *pidfile) { @@ -789,11 +770,6 @@ main(int argc, char **argv) logger(LOG_INFO, PACKAGE " " VERSION " starting"); } -#ifdef __GLIBC__ - /* We need to seed random for our fake arc4random call */ - srandomdev(); -#endif - #ifdef __linux__ /* Massage our filters per platform */ setup_packet_filters();