2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 03:20:50 +00:00
|
|
|
/*
|
|
|
|
* files.c -- DHCP server file manipulation *
|
|
|
|
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
2006-11-18 19:51:32 +00:00
|
|
|
#include "common.h"
|
2006-05-08 03:20:50 +00:00
|
|
|
#include "dhcpd.h"
|
|
|
|
#include "options.h"
|
2006-11-18 19:51:32 +00:00
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
/* on these functions, make sure your datatype matches */
|
2006-05-08 03:20:50 +00:00
|
|
|
static int read_ip(const char *line, void *arg)
|
|
|
|
{
|
2007-02-04 02:41:57 +00:00
|
|
|
len_and_sockaddr *lsa;
|
|
|
|
|
|
|
|
lsa = host_and_af2sockaddr(line, 0, AF_INET);
|
2007-07-03 15:47:50 +00:00
|
|
|
if (!lsa)
|
|
|
|
return 0;
|
2008-01-29 10:33:34 +00:00
|
|
|
*(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
|
2007-07-03 15:47:50 +00:00
|
|
|
free(lsa);
|
|
|
|
return 1;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_mac(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
uint8_t *mac_bytes = arg;
|
|
|
|
struct ether_addr *temp_ether_addr;
|
|
|
|
|
|
|
|
temp_ether_addr = ether_aton(line);
|
2006-11-18 19:51:32 +00:00
|
|
|
if (temp_ether_addr == NULL)
|
2007-07-01 17:05:57 +00:00
|
|
|
return 0;
|
|
|
|
memcpy(mac_bytes, temp_ether_addr, 6);
|
|
|
|
return 1;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_str(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
char **dest = arg;
|
|
|
|
|
|
|
|
free(*dest);
|
2007-01-19 21:33:19 +00:00
|
|
|
*dest = xstrdup(line);
|
2006-05-08 03:20:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_u32(const char *line, void *arg)
|
|
|
|
{
|
2007-07-01 17:05:57 +00:00
|
|
|
*(uint32_t*)arg = bb_strtou32(line, NULL, 10);
|
2006-11-27 14:43:21 +00:00
|
|
|
return errno == 0;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_yn(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
char *dest = arg;
|
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
if (!strcasecmp("yes", line)) {
|
2006-05-08 03:20:50 +00:00
|
|
|
*dest = 1;
|
2007-07-01 17:05:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp("no", line)) {
|
2006-05-08 03:20:50 +00:00
|
|
|
*dest = 0;
|
2007-07-01 17:05:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* find option 'code' in opt_list */
|
2007-11-29 08:17:45 +00:00
|
|
|
struct option_set *find_option(struct option_set *opt_list, uint8_t code)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
|
|
|
while (opt_list && opt_list->data[OPT_CODE] < code)
|
|
|
|
opt_list = opt_list->next;
|
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
if (opt_list && opt_list->data[OPT_CODE] == code)
|
|
|
|
return opt_list;
|
|
|
|
return NULL;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* add an option to the opt_list */
|
2006-11-23 12:57:49 +00:00
|
|
|
static void attach_option(struct option_set **opt_list,
|
|
|
|
const struct dhcp_option *option, char *buffer, int length)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
|
|
|
struct option_set *existing, *new, **curr;
|
|
|
|
|
2006-11-27 14:43:21 +00:00
|
|
|
existing = find_option(*opt_list, option->code);
|
2007-02-02 01:16:08 +00:00
|
|
|
if (!existing) {
|
2007-11-28 19:23:12 +00:00
|
|
|
DEBUG("Attaching option %02x to list", option->code);
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-02-27 21:15:08 +00:00
|
|
|
#if ENABLE_FEATURE_RFC3397
|
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035)
|
|
|
|
/* reuse buffer and length for RFC1035-formatted string */
|
|
|
|
buffer = dname_enc(NULL, 0, buffer, &length);
|
|
|
|
#endif
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
/* make a new option */
|
2007-07-01 17:05:57 +00:00
|
|
|
new = xmalloc(sizeof(*new));
|
2006-05-08 03:20:50 +00:00
|
|
|
new->data = xmalloc(length + 2);
|
|
|
|
new->data[OPT_CODE] = option->code;
|
|
|
|
new->data[OPT_LEN] = length;
|
|
|
|
memcpy(new->data + 2, buffer, length);
|
|
|
|
|
|
|
|
curr = opt_list;
|
|
|
|
while (*curr && (*curr)->data[OPT_CODE] < option->code)
|
|
|
|
curr = &(*curr)->next;
|
|
|
|
|
|
|
|
new->next = *curr;
|
|
|
|
*curr = new;
|
2007-02-27 21:15:08 +00:00
|
|
|
#if ENABLE_FEATURE_RFC3397
|
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
#endif
|
2007-02-02 01:16:08 +00:00
|
|
|
return;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
2007-02-02 01:16:08 +00:00
|
|
|
|
|
|
|
/* add it to an existing option */
|
2007-11-28 19:23:12 +00:00
|
|
|
DEBUG("Attaching option %02x to existing member of list", option->code);
|
2007-02-02 01:16:08 +00:00
|
|
|
if (option->flags & OPTION_LIST) {
|
2007-02-27 21:15:08 +00:00
|
|
|
#if ENABLE_FEATURE_RFC3397
|
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035)
|
|
|
|
/* reuse buffer and length for RFC1035-formatted string */
|
|
|
|
buffer = dname_enc(existing->data + 2,
|
|
|
|
existing->data[OPT_LEN], buffer, &length);
|
|
|
|
#endif
|
2007-02-02 01:16:08 +00:00
|
|
|
if (existing->data[OPT_LEN] + length <= 255) {
|
|
|
|
existing->data = xrealloc(existing->data,
|
|
|
|
existing->data[OPT_LEN] + length + 3);
|
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STRING) {
|
2007-02-02 01:57:24 +00:00
|
|
|
/* ' ' can bring us to 256 - bad */
|
2007-02-02 01:16:08 +00:00
|
|
|
if (existing->data[OPT_LEN] + length >= 255)
|
|
|
|
return;
|
|
|
|
/* add space separator between STRING options in a list */
|
|
|
|
existing->data[existing->data[OPT_LEN] + 2] = ' ';
|
|
|
|
existing->data[OPT_LEN]++;
|
|
|
|
}
|
|
|
|
memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
|
|
|
|
existing->data[OPT_LEN] += length;
|
|
|
|
} /* else, ignore the data, we could put this in a second option in the future */
|
2007-02-27 21:15:08 +00:00
|
|
|
#if ENABLE_FEATURE_RFC3397
|
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
#endif
|
2007-02-02 01:16:08 +00:00
|
|
|
} /* else, ignore the new data */
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read a dhcp option and add it to opt_list */
|
|
|
|
static int read_opt(const char *const_line, void *arg)
|
|
|
|
{
|
|
|
|
struct option_set **opt_list = arg;
|
|
|
|
char *opt, *val, *endptr;
|
|
|
|
char *line;
|
2007-11-29 08:17:45 +00:00
|
|
|
const struct dhcp_option *option;
|
|
|
|
int retval, length, idx;
|
|
|
|
char buffer[8] __attribute__((aligned(4)));
|
2006-05-08 03:20:50 +00:00
|
|
|
uint16_t *result_u16 = (uint16_t *) buffer;
|
|
|
|
uint32_t *result_u32 = (uint32_t *) buffer;
|
|
|
|
|
|
|
|
/* Cheat, the only const line we'll actually get is "" */
|
|
|
|
line = (char *) const_line;
|
2006-11-23 12:57:49 +00:00
|
|
|
opt = strtok(line, " \t=");
|
2007-11-29 08:17:45 +00:00
|
|
|
if (!opt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
idx = index_in_strings(opt, dhcp_option_strings); /* NB: was strcasecmp! */
|
|
|
|
if (idx < 0)
|
|
|
|
return 0;
|
|
|
|
option = &dhcp_options[idx];
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-11-29 08:17:45 +00:00
|
|
|
retval = 0;
|
2006-05-08 03:20:50 +00:00
|
|
|
do {
|
2006-11-23 12:57:49 +00:00
|
|
|
val = strtok(NULL, ", \t");
|
|
|
|
if (!val) break;
|
2007-11-29 08:17:45 +00:00
|
|
|
length = dhcp_option_lengths[option->flags & TYPE_MASK];
|
2006-05-08 03:20:50 +00:00
|
|
|
retval = 0;
|
|
|
|
opt = buffer; /* new meaning for variable opt */
|
|
|
|
switch (option->flags & TYPE_MASK) {
|
|
|
|
case OPTION_IP:
|
|
|
|
retval = read_ip(val, buffer);
|
|
|
|
break;
|
|
|
|
case OPTION_IP_PAIR:
|
|
|
|
retval = read_ip(val, buffer);
|
2007-07-01 17:05:57 +00:00
|
|
|
val = strtok(NULL, ", \t/-");
|
|
|
|
if (!val)
|
|
|
|
retval = 0;
|
|
|
|
if (retval)
|
|
|
|
retval = read_ip(val, buffer + 4);
|
2006-05-08 03:20:50 +00:00
|
|
|
break;
|
|
|
|
case OPTION_STRING:
|
2007-02-27 21:15:08 +00:00
|
|
|
#if ENABLE_FEATURE_RFC3397
|
|
|
|
case OPTION_STR1035:
|
|
|
|
#endif
|
2006-05-08 03:20:50 +00:00
|
|
|
length = strlen(val);
|
|
|
|
if (length > 0) {
|
|
|
|
if (length > 254) length = 254;
|
|
|
|
opt = val;
|
|
|
|
retval = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPTION_BOOLEAN:
|
|
|
|
retval = read_yn(val, buffer);
|
|
|
|
break;
|
|
|
|
case OPTION_U8:
|
|
|
|
buffer[0] = strtoul(val, &endptr, 0);
|
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 17:05:57 +00:00
|
|
|
/* htonX are macros in older libc's, using temp var
|
|
|
|
* in code below for safety */
|
|
|
|
/* TODO: use bb_strtoX? */
|
|
|
|
case OPTION_U16: {
|
|
|
|
unsigned long tmp = strtoul(val, &endptr, 0);
|
|
|
|
*result_u16 = htons(tmp);
|
|
|
|
retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
|
2006-05-08 03:20:50 +00:00
|
|
|
break;
|
2007-07-01 17:05:57 +00:00
|
|
|
}
|
|
|
|
case OPTION_S16: {
|
|
|
|
long tmp = strtol(val, &endptr, 0);
|
|
|
|
*result_u16 = htons(tmp);
|
2006-05-08 03:20:50 +00:00
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 17:05:57 +00:00
|
|
|
}
|
|
|
|
case OPTION_U32: {
|
|
|
|
unsigned long tmp = strtoul(val, &endptr, 0);
|
|
|
|
*result_u32 = htonl(tmp);
|
2006-05-08 03:20:50 +00:00
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 17:05:57 +00:00
|
|
|
}
|
|
|
|
case OPTION_S32: {
|
|
|
|
long tmp = strtol(val, &endptr, 0);
|
|
|
|
*result_u32 = htonl(tmp);
|
2006-05-08 03:20:50 +00:00
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 17:05:57 +00:00
|
|
|
}
|
2006-05-08 03:20:50 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (retval)
|
|
|
|
attach_option(opt_list, option, opt, length);
|
|
|
|
} while (retval && option->flags & OPTION_LIST);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_staticlease(const char *const_line, void *arg)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
char *mac_string;
|
|
|
|
char *ip_string;
|
|
|
|
uint8_t *mac_bytes;
|
|
|
|
uint32_t *ip;
|
|
|
|
|
|
|
|
/* Allocate memory for addresses */
|
|
|
|
mac_bytes = xmalloc(sizeof(unsigned char) * 8);
|
|
|
|
ip = xmalloc(sizeof(uint32_t));
|
|
|
|
|
|
|
|
/* Read mac */
|
|
|
|
line = (char *) const_line;
|
|
|
|
mac_string = strtok(line, " \t");
|
|
|
|
read_mac(mac_string, mac_bytes);
|
|
|
|
|
|
|
|
/* Read ip */
|
|
|
|
ip_string = strtok(NULL, " \t");
|
|
|
|
read_ip(ip_string, ip);
|
|
|
|
|
|
|
|
addStaticLease(arg, mac_bytes, ip);
|
|
|
|
|
2006-05-28 01:06:36 +00:00
|
|
|
if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
struct config_keyword {
|
|
|
|
const char *keyword;
|
|
|
|
int (*handler)(const char *line, void *var);
|
|
|
|
void *var;
|
|
|
|
const char *def;
|
|
|
|
};
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
static const struct config_keyword keywords[] = {
|
2007-07-01 17:05:57 +00:00
|
|
|
/* keyword handler variable address default */
|
|
|
|
{"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
|
|
|
|
{"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
|
|
|
|
{"interface", read_str, &(server_config.interface), "eth0"},
|
|
|
|
{"option", read_opt, &(server_config.options), ""},
|
|
|
|
{"opt", read_opt, &(server_config.options), ""},
|
|
|
|
/* Avoid "max_leases value not sane" warning by setting default
|
|
|
|
* to default_end_ip - default_start_ip + 1: */
|
|
|
|
{"max_leases", read_u32, &(server_config.max_leases), "235"},
|
|
|
|
{"remaining", read_yn, &(server_config.remaining), "yes"},
|
|
|
|
{"auto_time", read_u32, &(server_config.auto_time), "7200"},
|
|
|
|
{"decline_time", read_u32, &(server_config.decline_time), "3600"},
|
|
|
|
{"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
|
|
|
|
{"offer_time", read_u32, &(server_config.offer_time), "60"},
|
|
|
|
{"min_lease", read_u32, &(server_config.min_lease), "60"},
|
|
|
|
{"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
|
|
|
|
{"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
|
|
|
|
{"notify_file", read_str, &(server_config.notify_file), ""},
|
|
|
|
{"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
|
|
|
|
{"sname", read_str, &(server_config.sname), ""},
|
|
|
|
{"boot_file", read_str, &(server_config.boot_file), ""},
|
|
|
|
{"static_lease", read_staticlease, &(server_config.static_leases), ""},
|
|
|
|
/* ADDME: static lease */
|
2006-05-08 03:20:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
/*
|
|
|
|
* Domain names may have 254 chars, and string options can be 254
|
|
|
|
* chars long. However, 80 bytes will be enough for most, and won't
|
|
|
|
* hog up memory. If you have a special application, change it
|
|
|
|
*/
|
|
|
|
#define READ_CONFIG_BUF_SIZE 80
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
int read_config(const char *file)
|
|
|
|
{
|
|
|
|
FILE *in;
|
|
|
|
char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
|
|
|
|
int i, lm = 0;
|
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(keywords); i++)
|
2006-05-08 03:20:50 +00:00
|
|
|
if (keywords[i].def[0])
|
|
|
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
|
|
|
|
2007-04-11 23:20:53 +00:00
|
|
|
in = fopen_or_warn(file, "r");
|
2006-11-18 22:03:26 +00:00
|
|
|
if (!in) {
|
2006-05-08 03:20:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
|
2006-05-28 01:06:36 +00:00
|
|
|
char debug_orig[READ_CONFIG_BUF_SIZE];
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
lm++;
|
2007-12-26 20:56:55 +00:00
|
|
|
*strchrnul(buffer, '\n') = '\0';
|
|
|
|
if (ENABLE_FEATURE_UDHCP_DEBUG)
|
|
|
|
strcpy(debug_orig, buffer);
|
|
|
|
*strchrnul(buffer, '#') = '\0';
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-11-06 02:23:39 +00:00
|
|
|
token = strtok(buffer, " \t");
|
|
|
|
if (!token) continue;
|
|
|
|
line = strtok(NULL, "");
|
|
|
|
if (!line) continue;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
/* eat leading whitespace */
|
2006-11-18 22:03:26 +00:00
|
|
|
line = skip_whitespace(line);
|
2006-05-08 03:20:50 +00:00
|
|
|
/* eat trailing whitespace */
|
2006-11-18 22:03:26 +00:00
|
|
|
i = strlen(line) - 1;
|
|
|
|
while (i >= 0 && isspace(line[i]))
|
|
|
|
line[i--] = '\0';
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(keywords); i++)
|
2006-05-08 03:20:50 +00:00
|
|
|
if (!strcasecmp(token, keywords[i].keyword))
|
|
|
|
if (!keywords[i].handler(line, keywords[i].var)) {
|
2006-11-18 19:51:32 +00:00
|
|
|
bb_error_msg("cannot parse line %d of %s", lm, file);
|
2006-09-06 18:36:50 +00:00
|
|
|
if (ENABLE_FEATURE_UDHCP_DEBUG)
|
2006-11-18 19:51:32 +00:00
|
|
|
bb_error_msg("cannot parse '%s'", debug_orig);
|
2006-05-08 03:20:50 +00:00
|
|
|
/* reset back to the default value */
|
|
|
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(in);
|
2007-07-01 17:05:57 +00:00
|
|
|
|
|
|
|
server_config.start_ip = ntohl(server_config.start_ip);
|
|
|
|
server_config.end_ip = ntohl(server_config.end_ip);
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void write_leases(void)
|
|
|
|
{
|
2006-11-18 22:03:26 +00:00
|
|
|
int fp;
|
|
|
|
unsigned i;
|
2006-05-08 03:20:50 +00:00
|
|
|
time_t curr = time(0);
|
|
|
|
unsigned long tmp_time;
|
|
|
|
|
2008-03-17 09:07:36 +00:00
|
|
|
fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
|
2006-11-18 22:03:26 +00:00
|
|
|
if (fp < 0) {
|
2006-05-08 03:20:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < server_config.max_leases; i++) {
|
|
|
|
if (leases[i].yiaddr != 0) {
|
|
|
|
|
|
|
|
/* screw with the time in the struct, for easier writing */
|
|
|
|
tmp_time = leases[i].expires;
|
|
|
|
|
|
|
|
if (server_config.remaining) {
|
|
|
|
if (lease_expired(&(leases[i])))
|
|
|
|
leases[i].expires = 0;
|
|
|
|
else leases[i].expires -= curr;
|
|
|
|
} /* else stick with the time we got */
|
|
|
|
leases[i].expires = htonl(leases[i].expires);
|
2006-11-18 22:03:26 +00:00
|
|
|
// FIXME: error check??
|
|
|
|
full_write(fp, &leases[i], sizeof(leases[i]));
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2006-11-18 22:03:26 +00:00
|
|
|
/* then restore it when done */
|
2006-05-08 03:20:50 +00:00
|
|
|
leases[i].expires = tmp_time;
|
|
|
|
}
|
|
|
|
}
|
2006-11-18 22:03:26 +00:00
|
|
|
close(fp);
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
if (server_config.notify_file) {
|
2006-11-18 22:03:26 +00:00
|
|
|
char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
|
|
|
|
system(cmd);
|
|
|
|
free(cmd);
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void read_leases(const char *file)
|
|
|
|
{
|
2006-11-18 22:03:26 +00:00
|
|
|
int fp;
|
2006-05-08 03:20:50 +00:00
|
|
|
unsigned int i = 0;
|
|
|
|
struct dhcpOfferedAddr lease;
|
|
|
|
|
2007-04-11 23:20:53 +00:00
|
|
|
fp = open_or_warn(file, O_RDONLY);
|
2006-11-18 22:03:26 +00:00
|
|
|
if (fp < 0) {
|
2006-05-08 03:20:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-18 22:03:26 +00:00
|
|
|
while (i < server_config.max_leases
|
|
|
|
&& full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
|
|
|
|
) {
|
2006-05-08 03:20:50 +00:00
|
|
|
/* ADDME: is it a static lease */
|
2007-07-01 17:05:57 +00:00
|
|
|
uint32_t y = ntohl(lease.yiaddr);
|
|
|
|
if (y >= server_config.start_ip && y <= server_config.end_ip) {
|
2006-05-08 03:20:50 +00:00
|
|
|
lease.expires = ntohl(lease.expires);
|
2007-07-01 17:05:57 +00:00
|
|
|
if (!server_config.remaining)
|
|
|
|
lease.expires -= time(0);
|
2006-05-08 03:20:50 +00:00
|
|
|
if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
|
2006-10-20 13:28:22 +00:00
|
|
|
bb_error_msg("too many leases while loading %s", file);
|
2006-05-08 03:20:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 18:36:50 +00:00
|
|
|
DEBUG("Read %d leases", i);
|
2006-11-18 22:03:26 +00:00
|
|
|
close(fp);
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|