/* 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;
/* 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)
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)
/* 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
) {
{
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)
) {
{
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];
{
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];
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;
}
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;
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))
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);
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 */
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;
}
}
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)
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;
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)
}
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);
}
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;
}
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 */
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);
}
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.
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)
}
}
- config_opts = server_config.options;
+ config_opts = server_data.options;
while (config_opts) {
if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) {
/* ^^^^
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;
}
/* 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 */
/* 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
);
}
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
* 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;
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;
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:
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;
&& 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;
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;
}