Use the new libc_random_u32() in ncmlib to replace random_xid().
This commit is contained in:
@@ -1,13 +1,3 @@
|
||||
set(NCMLIB_SRCS
|
||||
malloc.c
|
||||
chroot.c
|
||||
pidfile.c
|
||||
signals.c
|
||||
strlist.c
|
||||
strl.c
|
||||
log.c
|
||||
cap.c
|
||||
io.c
|
||||
)
|
||||
file(GLOB NCMLIB_SRCS "*.c")
|
||||
|
||||
add_library(ncmlib ${NCMLIB_SRCS})
|
||||
add_library(ncmlib ${NCMLIB_SRCS})
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
#ifndef NCM_IO_H_
|
||||
#define NCM_IO_H_
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
int safe_read(int fd, char *buf, int len);
|
||||
int safe_write(int fd, const char *buf, int len);
|
||||
int safe_sendto(int fd, const char *buf, int len, int flags,
|
||||
|
||||
38
ncmlib/random.c
Normal file
38
ncmlib/random.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "log.h"
|
||||
#include "io.h"
|
||||
|
||||
// Generate a 32-bit pseudorandom number using libc rand()
|
||||
uint32_t libc_random_u32(void)
|
||||
{
|
||||
static int initialized;
|
||||
if (initialized)
|
||||
return rand();
|
||||
|
||||
uint32_t seed;
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd != -1) {
|
||||
int r = safe_read(fd, (char *)&seed, sizeof seed);
|
||||
if (r == -1) {
|
||||
log_warning("Could not read /dev/urandom: %s", strerror(errno));
|
||||
close(fd);
|
||||
seed = time(0);
|
||||
}
|
||||
} else {
|
||||
log_warning("Could not open /dev/urandom: %s", strerror(errno));
|
||||
seed = time(0);
|
||||
}
|
||||
srand(seed);
|
||||
initialized = 1;
|
||||
return rand();
|
||||
}
|
||||
|
||||
|
||||
6
ncmlib/random.h
Normal file
6
ncmlib/random.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef NCMLIB_RANDOM__
|
||||
#define NCMLIB_RANDOM__
|
||||
uint32_t libc_random_u32(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user