2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 08:50:50 +05:30
|
|
|
/*
|
|
|
|
* files.c -- DHCP server file manipulation *
|
|
|
|
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
2008-12-07 06:22:58 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
2006-05-08 08:50:50 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
2006-11-19 01:21:32 +05:30
|
|
|
#include "common.h"
|
2006-05-08 08:50:50 +05:30
|
|
|
#include "dhcpd.h"
|
|
|
|
#include "options.h"
|
2006-11-19 01:21:32 +05:30
|
|
|
|
2009-02-01 06:10:45 +05:30
|
|
|
#if BB_LITTLE_ENDIAN
|
|
|
|
static inline uint64_t hton64(uint64_t v)
|
|
|
|
{
|
|
|
|
return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define hton64(v) (v)
|
|
|
|
#endif
|
|
|
|
#define ntoh64(v) hton64(v)
|
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-07-01 22:35:57 +05:30
|
|
|
/* on these functions, make sure your datatype matches */
|
2006-05-08 08:50:50 +05:30
|
|
|
static int read_ip(const char *line, void *arg)
|
|
|
|
{
|
2007-02-04 08:11:57 +05:30
|
|
|
len_and_sockaddr *lsa;
|
|
|
|
|
|
|
|
lsa = host_and_af2sockaddr(line, 0, AF_INET);
|
2007-07-03 21:17:50 +05:30
|
|
|
if (!lsa)
|
|
|
|
return 0;
|
2008-01-29 16:03:34 +05:30
|
|
|
*(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
|
2007-07-03 21:17:50 +05:30
|
|
|
free(lsa);
|
|
|
|
return 1;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2009-02-02 16:18:06 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
static int read_mac(const char *line, void *arg)
|
|
|
|
{
|
2009-01-01 23:22:09 +05:30
|
|
|
return NULL == ether_aton_r(line, (struct ether_addr *)arg);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_str(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
char **dest = arg;
|
|
|
|
|
|
|
|
free(*dest);
|
2007-01-20 03:03:19 +05:30
|
|
|
*dest = xstrdup(line);
|
2006-05-08 08:50:50 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_u32(const char *line, void *arg)
|
|
|
|
{
|
2007-07-01 22:35:57 +05:30
|
|
|
*(uint32_t*)arg = bb_strtou32(line, NULL, 10);
|
2006-11-27 20:13:21 +05:30
|
|
|
return errno == 0;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_yn(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
char *dest = arg;
|
|
|
|
|
2007-07-01 22:35:57 +05:30
|
|
|
if (!strcasecmp("yes", line)) {
|
2006-05-08 08:50:50 +05:30
|
|
|
*dest = 1;
|
2007-07-01 22:35:57 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp("no", line)) {
|
2006-05-08 08:50:50 +05:30
|
|
|
*dest = 0;
|
2007-07-01 22:35:57 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* find option 'code' in opt_list */
|
2008-09-26 15:04:59 +05:30
|
|
|
struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
while (opt_list && opt_list->data[OPT_CODE] < code)
|
|
|
|
opt_list = opt_list->next;
|
|
|
|
|
2007-07-01 22:35:57 +05:30
|
|
|
if (opt_list && opt_list->data[OPT_CODE] == code)
|
|
|
|
return opt_list;
|
|
|
|
return NULL;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* add an option to the opt_list */
|
2006-11-23 18:27:49 +05:30
|
|
|
static void attach_option(struct option_set **opt_list,
|
|
|
|
const struct dhcp_option *option, char *buffer, int length)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct option_set *existing, *new, **curr;
|
|
|
|
|
2006-11-27 20:13:21 +05:30
|
|
|
existing = find_option(*opt_list, option->code);
|
2007-02-02 06:46:08 +05:30
|
|
|
if (!existing) {
|
2007-11-29 00:53:12 +05:30
|
|
|
DEBUG("Attaching option %02x to list", option->code);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2008-11-06 06:19:59 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_RFC3397
|
2007-02-28 02:45:08 +05:30
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035)
|
|
|
|
/* reuse buffer and length for RFC1035-formatted string */
|
2008-05-12 20:05:56 +05:30
|
|
|
buffer = (char *)dname_enc(NULL, 0, buffer, &length);
|
2007-02-28 02:45:08 +05:30
|
|
|
#endif
|
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
/* make a new option */
|
2007-07-01 22:35:57 +05:30
|
|
|
new = xmalloc(sizeof(*new));
|
2006-05-08 08:50:50 +05:30
|
|
|
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;
|
2008-11-06 06:19:59 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_RFC3397
|
2007-02-28 02:45:08 +05:30
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
#endif
|
2007-02-02 06:46:08 +05:30
|
|
|
return;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
2007-02-02 06:46:08 +05:30
|
|
|
|
|
|
|
/* add it to an existing option */
|
2007-11-29 00:53:12 +05:30
|
|
|
DEBUG("Attaching option %02x to existing member of list", option->code);
|
2007-02-02 06:46:08 +05:30
|
|
|
if (option->flags & OPTION_LIST) {
|
2008-11-06 06:19:59 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_RFC3397
|
2007-02-28 02:45:08 +05:30
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035)
|
|
|
|
/* reuse buffer and length for RFC1035-formatted string */
|
2008-05-12 20:05:56 +05:30
|
|
|
buffer = (char *)dname_enc(existing->data + 2,
|
2007-02-28 02:45:08 +05:30
|
|
|
existing->data[OPT_LEN], buffer, &length);
|
|
|
|
#endif
|
2007-02-02 06:46:08 +05:30
|
|
|
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 07:27:24 +05:30
|
|
|
/* ' ' can bring us to 256 - bad */
|
2007-02-02 06:46:08 +05:30
|
|
|
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 */
|
2008-11-06 06:19:59 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_RFC3397
|
2007-02-28 02:45:08 +05:30
|
|
|
if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
#endif
|
2007-02-02 06:46:08 +05:30
|
|
|
} /* else, ignore the new data */
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 13:47:45 +05:30
|
|
|
const struct dhcp_option *option;
|
|
|
|
int retval, length, idx;
|
2008-07-05 14:48:54 +05:30
|
|
|
char buffer[8] ALIGNED(4);
|
2006-05-08 08:50:50 +05:30
|
|
|
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 18:27:49 +05:30
|
|
|
opt = strtok(line, " \t=");
|
2007-11-29 13:47:45 +05:30
|
|
|
if (!opt)
|
|
|
|
return 0;
|
|
|
|
|
2008-03-20 18:17:01 +05:30
|
|
|
idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
|
2007-11-29 13:47:45 +05:30
|
|
|
if (idx < 0)
|
|
|
|
return 0;
|
|
|
|
option = &dhcp_options[idx];
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-11-29 13:47:45 +05:30
|
|
|
retval = 0;
|
2006-05-08 08:50:50 +05:30
|
|
|
do {
|
2006-11-23 18:27:49 +05:30
|
|
|
val = strtok(NULL, ", \t");
|
|
|
|
if (!val) break;
|
2007-11-29 13:47:45 +05:30
|
|
|
length = dhcp_option_lengths[option->flags & TYPE_MASK];
|
2006-05-08 08:50:50 +05:30
|
|
|
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 22:35:57 +05:30
|
|
|
val = strtok(NULL, ", \t/-");
|
|
|
|
if (!val)
|
|
|
|
retval = 0;
|
|
|
|
if (retval)
|
|
|
|
retval = read_ip(val, buffer + 4);
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
|
|
|
case OPTION_STRING:
|
2008-11-06 06:19:59 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_RFC3397
|
2007-02-28 02:45:08 +05:30
|
|
|
case OPTION_STR1035:
|
|
|
|
#endif
|
2006-05-08 08:50:50 +05:30
|
|
|
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 22:35:57 +05:30
|
|
|
/* 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 08:50:50 +05:30
|
|
|
break;
|
2007-07-01 22:35:57 +05:30
|
|
|
}
|
|
|
|
case OPTION_S16: {
|
|
|
|
long tmp = strtol(val, &endptr, 0);
|
|
|
|
*result_u16 = htons(tmp);
|
2006-05-08 08:50:50 +05:30
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 22:35:57 +05:30
|
|
|
}
|
|
|
|
case OPTION_U32: {
|
|
|
|
unsigned long tmp = strtoul(val, &endptr, 0);
|
|
|
|
*result_u32 = htonl(tmp);
|
2006-05-08 08:50:50 +05:30
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 22:35:57 +05:30
|
|
|
}
|
|
|
|
case OPTION_S32: {
|
|
|
|
long tmp = strtol(val, &endptr, 0);
|
|
|
|
*result_u32 = htonl(tmp);
|
2006-05-08 08:50:50 +05:30
|
|
|
retval = (endptr[0] == '\0');
|
|
|
|
break;
|
2007-07-01 22:35:57 +05:30
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
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;
|
2009-01-01 23:22:09 +05:30
|
|
|
struct ether_addr mac_bytes;
|
|
|
|
uint32_t ip;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
/* Read mac */
|
|
|
|
line = (char *) const_line;
|
2009-01-01 23:22:09 +05:30
|
|
|
mac_string = strtok_r(line, " \t", &line);
|
|
|
|
read_mac(mac_string, &mac_bytes);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
/* Read ip */
|
2009-01-01 23:22:09 +05:30
|
|
|
ip_string = strtok_r(NULL, " \t", &line);
|
|
|
|
read_ip(ip_string, &ip);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2009-01-01 23:22:09 +05:30
|
|
|
addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2008-11-06 06:19:59 +05:30
|
|
|
if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-01 22:35:57 +05:30
|
|
|
struct config_keyword {
|
|
|
|
const char *keyword;
|
|
|
|
int (*handler)(const char *line, void *var);
|
|
|
|
void *var;
|
|
|
|
const char *def;
|
|
|
|
};
|
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
static const struct config_keyword keywords[] = {
|
2007-07-01 22:35:57 +05:30
|
|
|
/* 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"},
|
|
|
|
/* 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"},
|
2009-01-01 23:22:09 +05:30
|
|
|
// {"remaining", read_yn, &(server_config.remaining), "yes"},
|
2007-07-01 22:35:57 +05:30
|
|
|
{"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"},
|
|
|
|
{"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
|
2008-03-20 18:17:01 +05:30
|
|
|
/* keywords with no defaults must be last! */
|
|
|
|
{"option", read_opt, &(server_config.options), ""},
|
|
|
|
{"opt", read_opt, &(server_config.options), ""},
|
|
|
|
{"notify_file", read_str, &(server_config.notify_file), ""},
|
2007-07-01 22:35:57 +05:30
|
|
|
{"sname", read_str, &(server_config.sname), ""},
|
|
|
|
{"boot_file", read_str, &(server_config.boot_file), ""},
|
|
|
|
{"static_lease", read_staticlease, &(server_config.static_leases), ""},
|
2006-05-08 08:50:50 +05:30
|
|
|
};
|
2008-03-20 18:17:01 +05:30
|
|
|
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2008-09-26 15:04:59 +05:30
|
|
|
void FAST_FUNC read_config(const char *file)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2008-07-20 23:28:12 +05:30
|
|
|
parser_t *parser;
|
|
|
|
const struct config_keyword *k;
|
|
|
|
unsigned i;
|
|
|
|
char *token[2];
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2008-03-20 18:17:01 +05:30
|
|
|
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
|
|
|
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2008-07-20 23:28:12 +05:30
|
|
|
parser = config_open(file);
|
2008-07-27 04:38:31 +05:30
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
|
2008-07-20 23:28:12 +05:30
|
|
|
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
|
|
|
|
if (!strcasecmp(token[0], k->keyword)) {
|
|
|
|
if (!k->handler(token[1], k->var)) {
|
|
|
|
bb_error_msg("can't parse line %u in %s",
|
|
|
|
parser->lineno, file);
|
2006-05-08 08:50:50 +05:30
|
|
|
/* reset back to the default value */
|
2008-07-20 23:28:12 +05:30
|
|
|
k->handler(k->def, k->var);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
2008-03-20 18:17:01 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
2008-07-20 23:28:12 +05:30
|
|
|
config_close(parser);
|
2007-07-01 22:35:57 +05:30
|
|
|
|
|
|
|
server_config.start_ip = ntohl(server_config.start_ip);
|
|
|
|
server_config.end_ip = ntohl(server_config.end_ip);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-26 15:04:59 +05:30
|
|
|
void FAST_FUNC write_leases(void)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2009-01-01 23:22:09 +05:30
|
|
|
int fd;
|
2006-11-19 03:33:26 +05:30
|
|
|
unsigned i;
|
2009-01-01 23:22:09 +05:30
|
|
|
leasetime_t curr;
|
2009-02-01 06:10:45 +05:30
|
|
|
int64_t written_at;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2009-01-01 23:22:09 +05:30
|
|
|
fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
|
|
|
|
if (fd < 0)
|
2006-05-08 08:50:50 +05:30
|
|
|
return;
|
2009-01-01 23:22:09 +05:30
|
|
|
|
2009-02-01 06:10:45 +05:30
|
|
|
curr = written_at = time(NULL);
|
|
|
|
|
|
|
|
written_at = hton64(written_at);
|
|
|
|
full_write(fd, &written_at, sizeof(written_at));
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
for (i = 0; i < server_config.max_leases; i++) {
|
2009-01-01 23:22:09 +05:30
|
|
|
leasetime_t tmp_time;
|
|
|
|
|
|
|
|
if (leases[i].yiaddr == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-02-02 16:18:06 +05:30
|
|
|
/* Screw with the time in the struct, for easier writing */
|
2009-01-01 23:22:09 +05:30
|
|
|
tmp_time = leases[i].expires;
|
|
|
|
|
2009-02-01 06:10:45 +05:30
|
|
|
leases[i].expires -= curr;
|
|
|
|
if ((signed_leasetime_t) leases[i].expires < 0)
|
|
|
|
leases[i].expires = 0;
|
2009-01-01 23:22:09 +05:30
|
|
|
leases[i].expires = htonl(leases[i].expires);
|
|
|
|
|
|
|
|
/* No error check. If the file gets truncated,
|
|
|
|
* we lose some leases on restart. Oh well. */
|
|
|
|
full_write(fd, &leases[i], sizeof(leases[i]));
|
|
|
|
|
2009-02-02 16:18:06 +05:30
|
|
|
/* Then restore it when done */
|
2009-01-01 23:22:09 +05:30
|
|
|
leases[i].expires = tmp_time;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
2009-01-01 23:22:09 +05:30
|
|
|
close(fd);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
if (server_config.notify_file) {
|
2008-04-02 18:34:19 +05:30
|
|
|
// TODO: vfork-based child creation
|
2006-11-19 03:33:26 +05:30
|
|
|
char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
|
|
|
|
system(cmd);
|
|
|
|
free(cmd);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-26 15:04:59 +05:30
|
|
|
void FAST_FUNC read_leases(const char *file)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct dhcpOfferedAddr lease;
|
2009-02-02 16:18:06 +05:30
|
|
|
int64_t written_at, time_passed;
|
|
|
|
int fd;
|
|
|
|
USE_UDHCP_DEBUG(unsigned i;)
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2009-01-01 23:22:09 +05:30
|
|
|
fd = open_or_warn(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2006-05-08 08:50:50 +05:30
|
|
|
return;
|
|
|
|
|
2009-02-01 06:10:45 +05:30
|
|
|
if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
|
|
|
|
goto ret;
|
|
|
|
written_at = ntoh64(written_at);
|
2009-02-02 16:18:06 +05:30
|
|
|
|
|
|
|
time_passed = time(NULL) - written_at;
|
|
|
|
/* Strange written_at, or lease file from old version of udhcpd
|
|
|
|
* which had no "written_at" field? */
|
|
|
|
if ((uint64_t)time_passed > 12 * 60 * 60)
|
|
|
|
goto ret;
|
|
|
|
|
|
|
|
USE_UDHCP_DEBUG(i = 0;)
|
|
|
|
while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
|
2009-01-01 23:22:09 +05:30
|
|
|
/* ADDME: what if it matches some static lease? */
|
2007-07-01 22:35:57 +05:30
|
|
|
uint32_t y = ntohl(lease.yiaddr);
|
|
|
|
if (y >= server_config.start_ip && y <= server_config.end_ip) {
|
2009-02-02 16:18:06 +05:30
|
|
|
signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
|
2009-02-01 06:10:45 +05:30
|
|
|
if (expires <= 0)
|
|
|
|
continue;
|
2009-01-01 23:22:09 +05:30
|
|
|
/* NB: add_lease takes "relative time", IOW,
|
|
|
|
* lease duration, not lease deadline. */
|
2009-04-01 18:06:09 +05:30
|
|
|
if (!(add_lease(lease.chaddr, lease.yiaddr, expires, lease.hostname))) {
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_error_msg("too many leases while loading %s", file);
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
|
|
|
}
|
2009-02-02 16:18:06 +05:30
|
|
|
USE_UDHCP_DEBUG(i++;)
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
}
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("Read %d leases", i);
|
2009-02-01 06:10:45 +05:30
|
|
|
ret:
|
2009-01-01 23:22:09 +05:30
|
|
|
close(fd);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|