Use the new libc_random_u32() in ncmlib to replace random_xid().
This commit is contained in:
parent
b72b551a0d
commit
27524d9d34
@ -1,13 +1,3 @@
|
|||||||
set(NCMLIB_SRCS
|
file(GLOB NCMLIB_SRCS "*.c")
|
||||||
malloc.c
|
|
||||||
chroot.c
|
|
||||||
pidfile.c
|
|
||||||
signals.c
|
|
||||||
strlist.c
|
|
||||||
strl.c
|
|
||||||
log.c
|
|
||||||
cap.c
|
|
||||||
io.c
|
|
||||||
)
|
|
||||||
|
|
||||||
add_library(ncmlib ${NCMLIB_SRCS})
|
add_library(ncmlib ${NCMLIB_SRCS})
|
||||||
|
@ -30,6 +30,8 @@
|
|||||||
#ifndef NCM_IO_H_
|
#ifndef NCM_IO_H_
|
||||||
#define NCM_IO_H_
|
#define NCM_IO_H_
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
int safe_read(int fd, char *buf, int len);
|
int safe_read(int fd, char *buf, int len);
|
||||||
int safe_write(int fd, const 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,
|
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
|
||||||
|
|
@ -46,6 +46,7 @@
|
|||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "strl.h"
|
#include "strl.h"
|
||||||
|
#include "random.h"
|
||||||
|
|
||||||
// Returns fd of new listen socket bound to @ip:@port on interface @inf
|
// Returns fd of new listen socket bound to @ip:@port on interface @inf
|
||||||
// on success, or -1 on failure.
|
// on success, or -1 on failure.
|
||||||
@ -636,32 +637,6 @@ void handle_packet(struct client_state_t *cs)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a 32-bit pseudorandom number
|
|
||||||
uint32_t random_xid(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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize a DHCP client packet that will be sent to a server
|
// Initialize a DHCP client packet that will be sent to a server
|
||||||
static struct dhcpmsg init_packet(char type, uint32_t xid)
|
static struct dhcpmsg init_packet(char type, uint32_t xid)
|
||||||
{
|
{
|
||||||
@ -740,7 +715,7 @@ int send_decline(uint32_t xid, uint32_t server, uint32_t requested)
|
|||||||
// Unicasts a DHCP release message
|
// Unicasts a DHCP release message
|
||||||
int send_release(uint32_t server, uint32_t ciaddr)
|
int send_release(uint32_t server, uint32_t ciaddr)
|
||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPRELEASE, random_xid());
|
struct dhcpmsg packet = init_packet(DHCPRELEASE, libc_random_u32());
|
||||||
packet.ciaddr = ciaddr;
|
packet.ciaddr = ciaddr;
|
||||||
add_u32_option(&packet, DHCP_REQUESTED_IP, ciaddr);
|
add_u32_option(&packet, DHCP_REQUESTED_IP, ciaddr);
|
||||||
add_u32_option(&packet, DHCP_SERVER_ID, server);
|
add_u32_option(&packet, DHCP_SERVER_ID, server);
|
||||||
|
@ -77,7 +77,6 @@ struct udp_dhcp_packet {
|
|||||||
|
|
||||||
void change_listen_mode(struct client_state_t *cs, int new_mode);
|
void change_listen_mode(struct client_state_t *cs, int new_mode);
|
||||||
void handle_packet(struct client_state_t *cs);
|
void handle_packet(struct client_state_t *cs);
|
||||||
uint32_t random_xid(void);
|
|
||||||
int send_discover(uint32_t xid, uint32_t requested);
|
int send_discover(uint32_t xid, uint32_t requested);
|
||||||
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested);
|
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested);
|
||||||
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr);
|
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr);
|
||||||
|
@ -27,13 +27,14 @@
|
|||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "random.h"
|
||||||
|
|
||||||
#define DELAY_SEC (((RETRY_DELAY - (RETRY_DELAY / NUMPACKETS)) / NUMPACKETS) + 1)
|
#define DELAY_SEC (((RETRY_DELAY - (RETRY_DELAY / NUMPACKETS)) / NUMPACKETS) + 1)
|
||||||
static void init_selecting_timeout(struct client_state_t *cs)
|
static void init_selecting_timeout(struct client_state_t *cs)
|
||||||
{
|
{
|
||||||
if (cs->packetNum < NUMPACKETS) {
|
if (cs->packetNum < NUMPACKETS) {
|
||||||
if (cs->packetNum == 0)
|
if (cs->packetNum == 0)
|
||||||
cs->xid = random_xid();
|
cs->xid = libc_random_u32();
|
||||||
/* broadcast */
|
/* broadcast */
|
||||||
send_discover(cs->xid, cs->requestedIP);
|
send_discover(cs->xid, cs->requestedIP);
|
||||||
cs->timeout = DELAY_SEC * (cs->packetNum + 1) * 1000;
|
cs->timeout = DELAY_SEC * (cs->packetNum + 1) * 1000;
|
||||||
|
Loading…
Reference in New Issue
Block a user