]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use arc4random as everything apart from glibc seems to have it.
authorRoy Marples <roy@marples.name>
Thu, 15 May 2008 19:21:15 +0000 (19:21 +0000)
committerRoy Marples <roy@marples.name>
Thu, 15 May 2008 19:21:15 +0000 (19:21 +0000)
client.c
common.c
common.h
dhcpcd.c

index 7208675c0e54c52044c153aa20a65072280df26d..dca85e872250e63bb6ec9f32728626ee3352f63f 100644 (file)
--- 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;
index 84429449bb147ac5bf995496fc7bfecbdc8cf0de..e207258ed0c1434c32ffeae964de16b45e87965a 100644 (file)
--- a/common.c
+++ b/common.c
@@ -36,7 +36,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>
 
 #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
index f11053212f35c7c9bdfa541224809264915a739c..4191846787b93da5f2cc538292ab64cabd46e666 100644 (file)
--- 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 <sys/cdefs.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <stdio.h>
 # 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
index 435c99b6295cc29001d0a938714b8e6f9b9e339b..4f2725bfc65f67da9ebcda96e904c26bcc5e9537 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -40,6 +40,9 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples";
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef __GLIBC__
+#  include <time.h> /* for srandomdev */
+#endif
 #include <unistd.h>
 
 #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 */