udhcp: rename server_config to server_data

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2019-05-30 16:23:34 +02:00
parent 726cbb1be8
commit 91755cb16d
2 changed files with 79 additions and 79 deletions

View File

@ -46,7 +46,7 @@
/* globals */
#define g_leases ((struct dyn_lease*)ptr_to_globals)
/* struct server_config_t server_config is in bb_common_bufsiz1 */
/* struct server_data_t server_data is in bb_common_bufsiz1 */
struct static_lease {
struct static_lease *next;
@ -100,7 +100,7 @@ static void add_static_lease(struct static_lease **st_lease_pp,
/* Find static lease IP by mac */
static uint32_t get_static_nip_by_mac(void *mac)
{
struct static_lease *st_lease = server_config.static_leases;
struct static_lease *st_lease = server_data.static_leases;
while (st_lease) {
if (memcmp(st_lease->mac, mac, 6) == 0)
@ -113,7 +113,7 @@ static uint32_t get_static_nip_by_mac(void *mac)
static int is_nip_reserved_as_static(uint32_t nip)
{
struct static_lease *st_lease = server_config.static_leases;
struct static_lease *st_lease = server_data.static_leases;
while (st_lease) {
if (st_lease->nip == nip)
@ -133,7 +133,7 @@ static struct dyn_lease *oldest_expired_lease(void)
/* Unexpired leases have g_leases[i].expires >= current time
* and therefore can't ever match */
for (i = 0; i < server_config.max_leases; i++) {
for (i = 0; i < server_data.max_leases; i++) {
if (g_leases[i].expires == 0 /* empty entry */
|| g_leases[i].expires < oldest_time
) {
@ -151,7 +151,7 @@ static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
{
unsigned i;
for (i = 0; i < server_config.max_leases; i++) {
for (i = 0; i < server_data.max_leases; i++) {
if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
|| (yiaddr && g_leases[i].lease_nip == yiaddr)
) {
@ -216,7 +216,7 @@ static struct dyn_lease *find_lease_by_mac(const uint8_t *mac)
{
unsigned i;
for (i = 0; i < server_config.max_leases; i++)
for (i = 0; i < server_data.max_leases; i++)
if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
return &g_leases[i];
@ -228,7 +228,7 @@ static struct dyn_lease *find_lease_by_nip(uint32_t nip)
{
unsigned i;
for (i = 0; i < server_config.max_leases; i++)
for (i = 0; i < server_data.max_leases; i++)
if (g_leases[i].lease_nip == nip)
return &g_leases[i];
@ -242,17 +242,17 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigne
int r;
r = arpping(nip, safe_mac,
server_config.server_nip,
server_config.server_mac,
server_config.interface,
server_data.server_nip,
server_data.server_mac,
server_data.interface,
arpping_ms);
if (r)
return r;
temp.s_addr = nip;
bb_info_msg("%s belongs to someone, reserving it for %u seconds",
inet_ntoa(temp), (unsigned)server_config.conflict_time);
add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
inet_ntoa(temp), (unsigned)server_data.conflict_time);
add_lease(NULL, nip, server_data.conflict_time, NULL, 0);
return 0;
}
@ -274,12 +274,12 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi
hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
/* pick a seed based on hwaddr then iterate until we find a free address. */
addr = server_config.start_ip
+ (hash % (1 + server_config.end_ip - server_config.start_ip));
addr = server_data.start_ip
+ (hash % (1 + server_data.end_ip - server_data.start_ip));
stop = addr;
#else
addr = server_config.start_ip;
#define stop (server_config.end_ip + 1)
addr = server_data.start_ip;
#define stop (server_data.end_ip + 1)
#endif
do {
uint32_t nip;
@ -293,7 +293,7 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi
goto next_addr;
nip = htonl(addr);
/* skip our own address */
if (nip == server_config.server_nip)
if (nip == server_data.server_nip)
goto next_addr;
/* is this a static lease addr? */
if (is_nip_reserved_as_static(nip))
@ -312,8 +312,8 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi
next_addr:
addr++;
#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
if (addr > server_config.end_ip)
addr = server_config.start_ip;
if (addr > server_data.end_ip)
addr = server_data.start_ip;
#endif
} while (addr != stop);
@ -386,7 +386,7 @@ struct config_keyword {
const char *def;
};
#define OFS(field) offsetof(struct server_config_t, field)
#define OFS(field) offsetof(struct server_data_t, field)
static const struct config_keyword keywords[] = {
/* keyword handler variable address default */
@ -422,17 +422,17 @@ static NOINLINE void read_config(const char *file)
char *token[2];
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs);
keywords[i].handler(keywords[i].def, (char*)&server_data + keywords[i].ofs);
parser = config_open(file);
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
if (strcasecmp(token[0], k->keyword) == 0) {
if (!k->handler(token[1], (char*)&server_config + k->ofs)) {
if (!k->handler(token[1], (char*)&server_data + k->ofs)) {
bb_error_msg("can't parse line %u in %s",
parser->lineno, file);
/* reset back to the default value */
k->handler(k->def, (char*)&server_config + k->ofs);
k->handler(k->def, (char*)&server_data + k->ofs);
}
break;
}
@ -440,8 +440,8 @@ static NOINLINE void read_config(const char *file)
}
config_close(parser);
server_config.start_ip = ntohl(server_config.start_ip);
server_config.end_ip = ntohl(server_config.end_ip);
server_data.start_ip = ntohl(server_data.start_ip);
server_data.end_ip = ntohl(server_data.end_ip);
}
static void write_leases(void)
@ -451,7 +451,7 @@ static void write_leases(void)
leasetime_t curr;
int64_t written_at;
fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
fd = open_or_warn(server_data.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
if (fd < 0)
return;
@ -460,7 +460,7 @@ static void write_leases(void)
written_at = SWAP_BE64(written_at);
full_write(fd, &written_at, sizeof(written_at));
for (i = 0; i < server_config.max_leases; i++) {
for (i = 0; i < server_data.max_leases; i++) {
leasetime_t tmp_time;
if (g_leases[i].lease_nip == 0)
@ -483,10 +483,10 @@ static void write_leases(void)
}
close(fd);
if (server_config.notify_file) {
if (server_data.notify_file) {
char *argv[3];
argv[0] = server_config.notify_file;
argv[1] = server_config.lease_file;
argv[0] = server_data.notify_file;
argv[1] = server_data.lease_file;
argv[2] = NULL;
spawn_and_wait(argv);
}
@ -517,7 +517,7 @@ static NOINLINE void read_leases(const char *file)
while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
uint32_t y = ntohl(lease.lease_nip);
if (y >= server_config.start_ip && y <= server_config.end_ip) {
if (y >= server_data.start_ip && y <= server_data.end_ip) {
signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
uint32_t static_nip;
@ -590,9 +590,9 @@ static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadc
}
udhcp_send_raw_packet(dhcp_pkt,
/*src*/ server_config.server_nip, SERVER_PORT,
/*src*/ server_data.server_nip, SERVER_PORT,
/*dst*/ ciaddr, CLIENT_PORT, chaddr,
server_config.ifindex);
server_data.ifindex);
}
/* Send a packet to gateway_nip using the kernel ip stack */
@ -601,7 +601,7 @@ static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
log1("forwarding packet to relay");
udhcp_send_kernel_packet(dhcp_pkt,
server_config.server_nip, SERVER_PORT,
server_data.server_nip, SERVER_PORT,
dhcp_pkt->gateway_nip, SERVER_PORT);
}
@ -633,7 +633,7 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke
packet->flags = oldpacket->flags;
packet->gateway_nip = oldpacket->gateway_nip;
packet->ciaddr = oldpacket->ciaddr;
udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_data.server_nip);
}
/* Fill options field, siaddr_nip, and sname and boot_file fields.
@ -646,7 +646,7 @@ static void add_server_options(struct dhcp_packet *packet)
client_hostname_opt = NULL;
if (packet->yiaddr) { /* if we aren't from send_inform()... */
struct static_lease *st_lease = server_config.static_leases;
struct static_lease *st_lease = server_data.static_leases;
while (st_lease) {
if (st_lease->nip == packet->yiaddr) {
if (st_lease->opt[0] != 0)
@ -657,7 +657,7 @@ static void add_server_options(struct dhcp_packet *packet)
}
}
config_opts = server_config.options;
config_opts = server_data.options;
while (config_opts) {
if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) {
/* ^^^^
@ -684,25 +684,25 @@ static void add_server_options(struct dhcp_packet *packet)
if (client_hostname_opt)
udhcp_add_binary_option(packet, client_hostname_opt);
packet->siaddr_nip = server_config.siaddr_nip;
packet->siaddr_nip = server_data.siaddr_nip;
if (server_config.sname)
strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
if (server_config.boot_file)
strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
if (server_data.sname)
strncpy((char*)packet->sname, server_data.sname, sizeof(packet->sname) - 1);
if (server_data.boot_file)
strncpy((char*)packet->file, server_data.boot_file, sizeof(packet->file) - 1);
}
static uint32_t select_lease_time(struct dhcp_packet *packet)
{
uint32_t lease_time_sec = server_config.max_lease_sec;
uint32_t lease_time_sec = server_data.max_lease_sec;
uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME);
if (lease_time_opt) {
move_from_unaligned32(lease_time_sec, lease_time_opt);
lease_time_sec = ntohl(lease_time_sec);
if (lease_time_sec > server_config.max_lease_sec)
lease_time_sec = server_config.max_lease_sec;
if (lease_time_sec < server_config.min_lease_sec)
lease_time_sec = server_config.min_lease_sec;
if (lease_time_sec > server_data.max_lease_sec)
lease_time_sec = server_data.max_lease_sec;
if (lease_time_sec < server_data.min_lease_sec)
lease_time_sec = server_data.min_lease_sec;
}
return lease_time_sec;
}
@ -737,8 +737,8 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
/* Or: if client has requested an IP */
else if (requested_nip != 0
/* and the IP is in the lease range */
&& ntohl(requested_nip) >= server_config.start_ip
&& ntohl(requested_nip) <= server_config.end_ip
&& ntohl(requested_nip) >= server_data.start_ip
&& ntohl(requested_nip) <= server_data.end_ip
/* and */
&& ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */
|| is_expired_lease(lease) /* or is taken, but expired */
@ -758,7 +758,7 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
/* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
lease = add_lease(packet.chaddr, packet.yiaddr,
server_config.offer_time,
server_data.offer_time,
p_host_name,
p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
);
@ -886,7 +886,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
}
if (opt & 4) { /* -I */
len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
server_data.server_nip = lsa->u.sin.sin_addr.s_addr;
free(lsa);
}
#if ENABLE_FEATURE_UDHCP_PORT
@ -901,42 +901,42 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
* otherwise NOMMU machines will parse config twice */
read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
/* prevent poll timeout overflow */
if (server_config.auto_time > INT_MAX / 1000)
server_config.auto_time = INT_MAX / 1000;
if (server_data.auto_time > INT_MAX / 1000)
server_data.auto_time = INT_MAX / 1000;
/* Make sure fd 0,1,2 are open */
bb_sanitize_stdio();
/* Create pidfile */
write_pidfile(server_config.pidfile);
write_pidfile(server_data.pidfile);
/* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
bb_info_msg("started, v"BB_VER);
option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
server_config.max_lease_sec = DEFAULT_LEASE_TIME;
option = udhcp_find_option(server_data.options, DHCP_LEASE_TIME);
server_data.max_lease_sec = DEFAULT_LEASE_TIME;
if (option) {
move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
move_from_unaligned32(server_data.max_lease_sec, option->data + OPT_DATA);
server_data.max_lease_sec = ntohl(server_data.max_lease_sec);
}
/* Sanity check */
num_ips = server_config.end_ip - server_config.start_ip + 1;
if (server_config.max_leases > num_ips) {
num_ips = server_data.end_ip - server_data.start_ip + 1;
if (server_data.max_leases > num_ips) {
bb_error_msg("max_leases=%u is too big, setting to %u",
(unsigned)server_config.max_leases, num_ips);
server_config.max_leases = num_ips;
(unsigned)server_data.max_leases, num_ips);
server_data.max_leases = num_ips;
}
/* this sets g_leases */
SET_PTR_TO_GLOBALS(xzalloc(server_config.max_leases * sizeof(g_leases[0])));
SET_PTR_TO_GLOBALS(xzalloc(server_data.max_leases * sizeof(g_leases[0])));
read_leases(server_config.lease_file);
read_leases(server_data.lease_file);
if (udhcp_read_interface(server_config.interface,
&server_config.ifindex,
(server_config.server_nip == 0 ? &server_config.server_nip : NULL),
server_config.server_mac)
if (udhcp_read_interface(server_data.interface,
&server_data.ifindex,
(server_data.server_nip == 0 ? &server_data.server_nip : NULL),
server_data.server_mac)
) {
retval = 1;
goto ret;
@ -946,7 +946,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
udhcp_sp_setup();
continue_with_autotime:
timeout_end = monotonic_sec() + server_config.auto_time;
timeout_end = monotonic_sec() + server_data.auto_time;
while (1) { /* loop until universe collapses */
struct pollfd pfds[2];
struct dhcp_packet packet;
@ -960,14 +960,14 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
if (server_socket < 0) {
server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
server_config.interface);
server_data.interface);
}
udhcp_sp_fd_set(pfds, server_socket);
new_tv:
tv = -1;
if (server_config.auto_time) {
if (server_data.auto_time) {
tv = timeout_end - monotonic_sec();
if (tv <= 0) {
write_leases:
@ -1037,7 +1037,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
if (server_id_opt) {
uint32_t server_id_network_order;
move_from_unaligned32(server_id_network_order, server_id_opt);
if (server_id_network_order != server_config.server_nip) {
if (server_id_network_order != server_data.server_nip) {
/* client talks to somebody else */
log1("server ID doesn't match, ignoring");
continue;
@ -1204,7 +1204,7 @@ o DHCPREQUEST generated during REBINDING state:
&& requested_nip == lease->lease_nip
) {
memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
lease->expires = time(NULL) + server_config.decline_time;
lease->expires = time(NULL) + server_data.decline_time;
}
break;
@ -1235,7 +1235,7 @@ o DHCPREQUEST generated during REBINDING state:
ret0:
retval = 0;
ret:
/*if (server_config.pidfile) - server_config.pidfile is never NULL */
remove_pidfile(server_config.pidfile);
/*if (server_data.pidfile) - server_data.pidfile is never NULL */
remove_pidfile(server_data.pidfile);
return retval;
}

View File

@ -17,7 +17,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
struct static_lease;
struct server_config_t {
struct server_data_t {
char *interface; /* interface to use */
//TODO: ifindex, server_nip, server_mac
// are obtained from interface name.
@ -53,12 +53,12 @@ struct server_config_t {
struct static_lease *static_leases; /* List of ip/mac pairs to assign static leases */
} FIX_ALIASING;
#define server_config (*(struct server_config_t*)bb_common_bufsiz1)
#define server_data (*(struct server_data_t*)bb_common_bufsiz1)
/* client_data sits in 2nd half of bb_common_bufsiz1 */
#if ENABLE_FEATURE_UDHCP_PORT
#define SERVER_PORT (server_config.port)
#define SERVER_PORT6 (server_config.port)
#define SERVER_PORT (server_data.port)
#define SERVER_PORT6 (server_data.port)
#else
#define SERVER_PORT 67
#define SERVER_PORT6 547