From dcf889cb72d2d385db03e3ee29b80fbf035abf69 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 15 May 2008 19:21:15 +0000 Subject: [PATCH] Use arc4random as everything apart from glibc seems to have it. --- client.c | 10 +++++----- common.c | 20 -------------------- common.h | 13 +++++-------- dhcpcd.c | 23 ++++++++++++++++++++++- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/client.c b/client.c index 7208675c..dca85e87 100644 --- a/client.c +++ b/client.c @@ -307,7 +307,7 @@ ipv4ll_get_address(struct interface *iface, struct dhcp_lease *lease) { for (;;) { addr.s_addr = htonl(LINKLOCAL_ADDR | - (((uint32_t)abs((int)random()) + (((uint32_t)abs((int)arc4random()) % 0xFD00) + 0x0100)); errno = 0; if (!arp_claim(iface, addr)) @@ -786,7 +786,7 @@ handle_signal(int sig, struct if_state *state, const struct options *options) logger (LOG_INFO, "received SIGHUP, releasing lease"); if (!IN_LINKLOCAL(ntohl(lease->addr.s_addr))) { do_socket(state, SOCKET_OPEN); - state->xid = (uint32_t)random(); + state->xid = arc4random(); send_message(state, DHCP_RELEASE, options); do_socket(state, SOCKET_CLOSED); } @@ -887,7 +887,7 @@ handle_timeout(struct if_state *state, const struct options *options) switch (state->state) { case STATE_INIT: - state->xid = (uint32_t) random (); + state->xid = arc4random(); do_socket(state, SOCKET_OPEN); state->timeout = options->timeout; iface->start_uptime = uptime (); @@ -917,7 +917,7 @@ handle_timeout(struct if_state *state, const struct options *options) break; } state->state = STATE_RENEWING; - state->xid = (uint32_t)random(); + state->xid = arc4random(); /* FALLTHROUGH */ case STATE_RENEWING: iface->start_uptime = uptime(); @@ -932,7 +932,7 @@ handle_timeout(struct if_state *state, const struct options *options) lease->addr.s_addr = 0; do_socket(state, SOCKET_OPEN); if (state->xid == 0) - state->xid = (uint32_t)random(); + state->xid = arc4random(); lease->server.s_addr = 0; send_message(state, DHCP_REQUEST, options); state->timeout = lease->leasetime - lease->rebindtime; diff --git a/common.c b/common.c index 84429449..e207258e 100644 --- a/common.c +++ b/common.c @@ -36,7 +36,6 @@ #include #include #include -#include #include #include "common.h" @@ -75,25 +74,6 @@ get_line(char **line, size_t *len, FILE *fp) return last; } -/* OK, this should be in dhcpcd.c - * It's here to make dhcpcd more readable */ -#if HAVE_SRANDOMDEV -#else -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 - /* strlcpy is nice, shame glibc does not define it */ #if HAVE_STRLCPY #else diff --git a/common.h b/common.h index f1105321..41918467 100644 --- a/common.h +++ b/common.h @@ -29,6 +29,7 @@ #define COMMON_H /* string.h pulls in features.h so the below define checks work */ +#include #include #include #include @@ -40,6 +41,10 @@ # define _unused #endif +#ifdef __GLIBC__ +#define arc4random (uint32_t)random +#endif + #ifndef HAVE_STRLCPY # define HAVE_STRLCPY 1 #endif @@ -51,14 +56,6 @@ size_t strlcpy(char *, const char *, size_t); # endif #endif -#ifndef HAVE_SRANDOMDEV -# define HAVE_SRANDOMDEV 1 -#endif -#if defined(__GLIBC__) || defined(__NetBSD__) -# undef HAVE_SRANDOMDEV -void srandomdev(void); -#endif - #ifndef HAVE_CLOSEFROM #define HAVE_CLOSEFROM 1 #endif diff --git a/dhcpcd.c b/dhcpcd.c index 435c99b6..4f2725bf 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -40,6 +40,9 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; #include #include #include +#ifdef __GLIBC__ +# include /* for srandomdev */ +#endif #include #include "config.h" @@ -133,6 +136,22 @@ 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) { @@ -770,8 +789,10 @@ main(int argc, char **argv) logger(LOG_INFO, PACKAGE " " VERSION " starting"); } - /* Seed random */ +#ifdef __GLIBC__ + /* We need to seed random for our fake arc4random call */ srandomdev(); +#endif #ifdef __linux__ /* Massage our filters per platform */ -- 2.47.3