Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/odhcpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/random.h>

#include <libubox/uloop.h>
#include "odhcpd.h"

static int ioctl_sock = -1;
static int urandom_fd = -1;

void __iflog(int lvl, const char *fmt, ...)
{
Expand Down Expand Up @@ -148,13 +148,9 @@ int main(int argc, char **argv)
}

ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);

if (ioctl_sock < 0)
return 4;

if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
return 4;

signal(SIGUSR1, SIG_IGN);
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
Expand Down Expand Up @@ -542,11 +538,33 @@ void odhcpd_process(struct odhcpd_event *event)
odhcpd_receive_packets(&event->uloop, 0);
}

int odhcpd_urandom(void *data, size_t len)
void odhcpd_urandom(void *data, size_t len)
{
return read(urandom_fd, data, len);
}
static bool warned_once = false;

while (true) {
ssize_t r;

if (len == 0)
return;

r = getrandom(data, len, GRND_INSECURE);
if (r < 0) {
if (errno == EINTR)
continue;

if (!warned_once) {
error("getrandom(): %m");
warned_once = true;
}

return;
}

len -= r;
data = (uint8_t *)data + r;
}
}

time_t odhcpd_time(void)
{
Expand Down
2 changes: 1 addition & 1 deletion src/odhcpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ int odhcpd_get_interface_config(const char *ifname, const char *what);
int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
int odhcpd_get_flags(const struct interface *iface);
struct interface* odhcpd_get_interface_by_index(int ifindex);
int odhcpd_urandom(void *data, size_t len);
void odhcpd_urandom(void *data, size_t len);

void odhcpd_run(void);
time_t odhcpd_time(void);
Expand Down