blob: 76b42f0aff976f64a38f060c247ae96938bb622d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * files.c -- DHCP server file manipulation *
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00007 */
8
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include <netinet/ether.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000010
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000011#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000012#include "dhcpd.h"
13#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000014
Mike Frysinger7031f622006-05-08 03:20:50 +000015
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000016/* on these functions, make sure your datatype matches */
Mike Frysinger7031f622006-05-08 03:20:50 +000017static int read_ip(const char *line, void *arg)
18{
Denis Vlasenkoe9913032007-02-04 02:41:57 +000019 len_and_sockaddr *lsa;
Mike Frysinger7031f622006-05-08 03:20:50 +000020
Denis Vlasenkoe9913032007-02-04 02:41:57 +000021 lsa = host_and_af2sockaddr(line, 0, AF_INET);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000022 if (!lsa)
23 return 0;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +000024 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000025 free(lsa);
26 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000027}
28
29static int read_mac(const char *line, void *arg)
30{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000031 return NULL == ether_aton_r(line, (struct ether_addr *)arg);
Mike Frysinger7031f622006-05-08 03:20:50 +000032}
33
34
35static int read_str(const char *line, void *arg)
36{
37 char **dest = arg;
38
39 free(*dest);
Denis Vlasenko4ebaf102007-01-19 21:33:19 +000040 *dest = xstrdup(line);
Mike Frysinger7031f622006-05-08 03:20:50 +000041 return 1;
42}
43
44
45static int read_u32(const char *line, void *arg)
46{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000047 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +000048 return errno == 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000049}
50
51
52static int read_yn(const char *line, void *arg)
53{
54 char *dest = arg;
Mike Frysinger7031f622006-05-08 03:20:50 +000055
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000056 if (!strcasecmp("yes", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000057 *dest = 1;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000058 return 1;
59 }
60 if (!strcasecmp("no", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000061 *dest = 0;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000062 return 1;
63 }
64 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000065}
66
67
68/* find option 'code' in opt_list */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000069struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
Mike Frysinger7031f622006-05-08 03:20:50 +000070{
71 while (opt_list && opt_list->data[OPT_CODE] < code)
72 opt_list = opt_list->next;
73
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000074 if (opt_list && opt_list->data[OPT_CODE] == code)
75 return opt_list;
76 return NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +000077}
78
79
80/* add an option to the opt_list */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000081static void attach_option(struct option_set **opt_list,
82 const struct dhcp_option *option, char *buffer, int length)
Mike Frysinger7031f622006-05-08 03:20:50 +000083{
84 struct option_set *existing, *new, **curr;
85
Denis Vlasenkod686a042006-11-27 14:43:21 +000086 existing = find_option(*opt_list, option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +000087 if (!existing) {
Denis Vlasenko35ff7462007-11-28 19:23:12 +000088 DEBUG("Attaching option %02x to list", option->code);
Mike Frysinger7031f622006-05-08 03:20:50 +000089
Denis Vlasenko35a064b2008-11-06 00:49:59 +000090#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +000091 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
92 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +000093 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
Denis Vlasenko50664732007-02-27 21:15:08 +000094#endif
95
Mike Frysinger7031f622006-05-08 03:20:50 +000096 /* make a new option */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000097 new = xmalloc(sizeof(*new));
Mike Frysinger7031f622006-05-08 03:20:50 +000098 new->data = xmalloc(length + 2);
99 new->data[OPT_CODE] = option->code;
100 new->data[OPT_LEN] = length;
101 memcpy(new->data + 2, buffer, length);
102
103 curr = opt_list;
104 while (*curr && (*curr)->data[OPT_CODE] < option->code)
105 curr = &(*curr)->next;
106
107 new->next = *curr;
108 *curr = new;
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000109#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000110 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
111 free(buffer);
112#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000113 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000114 }
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000115
116 /* add it to an existing option */
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000117 DEBUG("Attaching option %02x to existing member of list", option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000118 if (option->flags & OPTION_LIST) {
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000119#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000120 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
121 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +0000122 buffer = (char *)dname_enc(existing->data + 2,
Denis Vlasenko50664732007-02-27 21:15:08 +0000123 existing->data[OPT_LEN], buffer, &length);
124#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000125 if (existing->data[OPT_LEN] + length <= 255) {
126 existing->data = xrealloc(existing->data,
127 existing->data[OPT_LEN] + length + 3);
128 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
Denis Vlasenkoe129f432007-02-02 01:57:24 +0000129 /* ' ' can bring us to 256 - bad */
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000130 if (existing->data[OPT_LEN] + length >= 255)
131 return;
132 /* add space separator between STRING options in a list */
133 existing->data[existing->data[OPT_LEN] + 2] = ' ';
134 existing->data[OPT_LEN]++;
135 }
136 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
137 existing->data[OPT_LEN] += length;
138 } /* else, ignore the data, we could put this in a second option in the future */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000139#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000140 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
141 free(buffer);
142#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000143 } /* else, ignore the new data */
Mike Frysinger7031f622006-05-08 03:20:50 +0000144}
145
146
147/* read a dhcp option and add it to opt_list */
148static int read_opt(const char *const_line, void *arg)
149{
150 struct option_set **opt_list = arg;
151 char *opt, *val, *endptr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 char *line;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000153 const struct dhcp_option *option;
154 int retval, length, idx;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000155 char buffer[8] ALIGNED(4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000156 uint16_t *result_u16 = (uint16_t *) buffer;
157 uint32_t *result_u32 = (uint32_t *) buffer;
158
159 /* Cheat, the only const line we'll actually get is "" */
160 line = (char *) const_line;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000161 opt = strtok(line, " \t=");
Denis Vlasenkob539c842007-11-29 08:17:45 +0000162 if (!opt)
163 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000164
Denis Vlasenko027271e2008-03-20 12:47:01 +0000165 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000166 if (idx < 0)
167 return 0;
168 option = &dhcp_options[idx];
Mike Frysinger7031f622006-05-08 03:20:50 +0000169
Denis Vlasenkob539c842007-11-29 08:17:45 +0000170 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 do {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000172 val = strtok(NULL, ", \t");
173 if (!val) break;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000174 length = dhcp_option_lengths[option->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000175 retval = 0;
176 opt = buffer; /* new meaning for variable opt */
177 switch (option->flags & TYPE_MASK) {
178 case OPTION_IP:
179 retval = read_ip(val, buffer);
180 break;
181 case OPTION_IP_PAIR:
182 retval = read_ip(val, buffer);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000183 val = strtok(NULL, ", \t/-");
184 if (!val)
185 retval = 0;
186 if (retval)
187 retval = read_ip(val, buffer + 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000188 break;
189 case OPTION_STRING:
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000190#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000191 case OPTION_STR1035:
192#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000193 length = strlen(val);
194 if (length > 0) {
195 if (length > 254) length = 254;
196 opt = val;
197 retval = 1;
198 }
199 break;
200 case OPTION_BOOLEAN:
201 retval = read_yn(val, buffer);
202 break;
203 case OPTION_U8:
204 buffer[0] = strtoul(val, &endptr, 0);
205 retval = (endptr[0] == '\0');
206 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000207 /* htonX are macros in older libc's, using temp var
208 * in code below for safety */
209 /* TODO: use bb_strtoX? */
210 case OPTION_U16: {
211 unsigned long tmp = strtoul(val, &endptr, 0);
212 *result_u16 = htons(tmp);
213 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
214 break;
215 }
216 case OPTION_S16: {
217 long tmp = strtol(val, &endptr, 0);
218 *result_u16 = htons(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000219 retval = (endptr[0] == '\0');
220 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000221 }
222 case OPTION_U32: {
223 unsigned long tmp = strtoul(val, &endptr, 0);
224 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000225 retval = (endptr[0] == '\0');
226 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000227 }
228 case OPTION_S32: {
229 long tmp = strtol(val, &endptr, 0);
230 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000231 retval = (endptr[0] == '\0');
232 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000233 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000234 default:
235 break;
236 }
237 if (retval)
238 attach_option(opt_list, option, opt, length);
239 } while (retval && option->flags & OPTION_LIST);
240 return retval;
241}
242
243static int read_staticlease(const char *const_line, void *arg)
244{
Mike Frysinger7031f622006-05-08 03:20:50 +0000245 char *line;
246 char *mac_string;
247 char *ip_string;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000248 struct ether_addr mac_bytes;
249 uint32_t ip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000250
251 /* Read mac */
252 line = (char *) const_line;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000253 mac_string = strtok_r(line, " \t", &line);
254 read_mac(mac_string, &mac_bytes);
Mike Frysinger7031f622006-05-08 03:20:50 +0000255
256 /* Read ip */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000257 ip_string = strtok_r(NULL, " \t", &line);
258 read_ip(ip_string, &ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000259
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000260 addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000261
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000262 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000263
264 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000265}
266
267
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000268struct config_keyword {
269 const char *keyword;
270 int (*handler)(const char *line, void *var);
271 void *var;
272 const char *def;
Mike Frysinger7031f622006-05-08 03:20:50 +0000273};
274
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000275static const struct config_keyword keywords[] = {
276 /* keyword handler variable address default */
277 {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
278 {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
279 {"interface", read_str, &(server_config.interface), "eth0"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000280 /* Avoid "max_leases value not sane" warning by setting default
281 * to default_end_ip - default_start_ip + 1: */
282 {"max_leases", read_u32, &(server_config.max_leases), "235"},
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000283// {"remaining", read_yn, &(server_config.remaining), "yes"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000284 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
285 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
286 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
287 {"offer_time", read_u32, &(server_config.offer_time), "60"},
288 {"min_lease", read_u32, &(server_config.min_lease), "60"},
289 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
290 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000291 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
Denis Vlasenko027271e2008-03-20 12:47:01 +0000292 /* keywords with no defaults must be last! */
293 {"option", read_opt, &(server_config.options), ""},
294 {"opt", read_opt, &(server_config.options), ""},
295 {"notify_file", read_str, &(server_config.notify_file), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000296 {"sname", read_str, &(server_config.sname), ""},
297 {"boot_file", read_str, &(server_config.boot_file), ""},
298 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000299};
Denis Vlasenko027271e2008-03-20 12:47:01 +0000300enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000301
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000302void FAST_FUNC read_config(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000303{
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000304 parser_t *parser;
305 const struct config_keyword *k;
306 unsigned i;
307 char *token[2];
Mike Frysinger7031f622006-05-08 03:20:50 +0000308
Denis Vlasenko027271e2008-03-20 12:47:01 +0000309 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
310 keywords[i].handler(keywords[i].def, keywords[i].var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000311
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000312 parser = config_open(file);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000313 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000314 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
315 if (!strcasecmp(token[0], k->keyword)) {
316 if (!k->handler(token[1], k->var)) {
317 bb_error_msg("can't parse line %u in %s",
318 parser->lineno, file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000319 /* reset back to the default value */
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000320 k->handler(k->def, k->var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000321 }
Denis Vlasenko027271e2008-03-20 12:47:01 +0000322 break;
323 }
324 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000325 }
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000326 config_close(parser);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000327
328 server_config.start_ip = ntohl(server_config.start_ip);
329 server_config.end_ip = ntohl(server_config.end_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000330}
331
332
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000333void FAST_FUNC write_leases(void)
Mike Frysinger7031f622006-05-08 03:20:50 +0000334{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000335 int fd;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000336 unsigned i;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000337 leasetime_t curr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000338
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000339 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
340 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000341 return;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000342
343 curr = time(NULL);
344//TODO: write out current time? Readers need to adjust .expires field
345// to account for time between file was written and when it was read back.
Mike Frysinger7031f622006-05-08 03:20:50 +0000346
347 for (i = 0; i < server_config.max_leases; i++) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000348 leasetime_t tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000349
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000350 if (leases[i].yiaddr == 0)
351 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000352
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000353 /* screw with the time in the struct, for easier writing */
354 tmp_time = leases[i].expires;
Mike Frysinger7031f622006-05-08 03:20:50 +0000355
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000356 //if (server_config.remaining) {
357 leases[i].expires -= curr;
358 if ((signed_leasetime_t) leases[i].expires < 0)
359 leases[i].expires = 0;
360 //} /* else stick with the time we got */
361 leases[i].expires = htonl(leases[i].expires);
362
363 /* No error check. If the file gets truncated,
364 * we lose some leases on restart. Oh well. */
365 full_write(fd, &leases[i], sizeof(leases[i]));
366
367 /* then restore it when done */
368 leases[i].expires = tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000369 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000370 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000371
372 if (server_config.notify_file) {
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000373// TODO: vfork-based child creation
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000374 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
375 system(cmd);
376 free(cmd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000377 }
378}
379
380
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000381void FAST_FUNC read_leases(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000382{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000383 int fd;
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000384 unsigned i;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000385// leasetime_t curr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000386 struct dhcpOfferedAddr lease;
387
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000388 fd = open_or_warn(file, O_RDONLY);
389 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000390 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000391
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000392// curr = time(NULL);
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000393 i = 0;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000394 while (i < server_config.max_leases
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000395 && full_read(fd, &lease, sizeof(lease)) == sizeof(lease)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000396 ) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000397 /* ADDME: what if it matches some static lease? */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000398 uint32_t y = ntohl(lease.yiaddr);
399 if (y >= server_config.start_ip && y <= server_config.end_ip) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000400 leasetime_t expires = ntohl(lease.expires);
401// if (!server_config.remaining)
402// expires -= curr;
403 /* NB: add_lease takes "relative time", IOW,
404 * lease duration, not lease deadline. */
405 if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000406 bb_error_msg("too many leases while loading %s", file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000407 break;
408 }
409 i++;
410 }
411 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000412 DEBUG("Read %d leases", i);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000413 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000414}