blob: 64edcbb256d855fce2c125a123172c1b0e3a86dc [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
Denis Vlasenko73121c32009-02-01 00:40:45 +000015#if BB_LITTLE_ENDIAN
16static inline uint64_t hton64(uint64_t v)
17{
18 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
19}
20#else
21#define hton64(v) (v)
22#endif
23#define ntoh64(v) hton64(v)
24
Mike Frysinger7031f622006-05-08 03:20:50 +000025
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000026/* on these functions, make sure your datatype matches */
Mike Frysinger7031f622006-05-08 03:20:50 +000027static int read_ip(const char *line, void *arg)
28{
Denis Vlasenkoe9913032007-02-04 02:41:57 +000029 len_and_sockaddr *lsa;
Mike Frysinger7031f622006-05-08 03:20:50 +000030
Denis Vlasenkoe9913032007-02-04 02:41:57 +000031 lsa = host_and_af2sockaddr(line, 0, AF_INET);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000032 if (!lsa)
33 return 0;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +000034 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000035 free(lsa);
36 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
39static int read_mac(const char *line, void *arg)
40{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000041 return NULL == ether_aton_r(line, (struct ether_addr *)arg);
Mike Frysinger7031f622006-05-08 03:20:50 +000042}
43
44
45static int read_str(const char *line, void *arg)
46{
47 char **dest = arg;
48
49 free(*dest);
Denis Vlasenko4ebaf102007-01-19 21:33:19 +000050 *dest = xstrdup(line);
Mike Frysinger7031f622006-05-08 03:20:50 +000051 return 1;
52}
53
54
55static int read_u32(const char *line, void *arg)
56{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000057 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +000058 return errno == 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000059}
60
61
62static int read_yn(const char *line, void *arg)
63{
64 char *dest = arg;
Mike Frysinger7031f622006-05-08 03:20:50 +000065
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000066 if (!strcasecmp("yes", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000067 *dest = 1;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000068 return 1;
69 }
70 if (!strcasecmp("no", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000071 *dest = 0;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000072 return 1;
73 }
74 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000075}
76
77
78/* find option 'code' in opt_list */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000079struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
Mike Frysinger7031f622006-05-08 03:20:50 +000080{
81 while (opt_list && opt_list->data[OPT_CODE] < code)
82 opt_list = opt_list->next;
83
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000084 if (opt_list && opt_list->data[OPT_CODE] == code)
85 return opt_list;
86 return NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +000087}
88
89
90/* add an option to the opt_list */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000091static void attach_option(struct option_set **opt_list,
92 const struct dhcp_option *option, char *buffer, int length)
Mike Frysinger7031f622006-05-08 03:20:50 +000093{
94 struct option_set *existing, *new, **curr;
95
Denis Vlasenkod686a042006-11-27 14:43:21 +000096 existing = find_option(*opt_list, option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +000097 if (!existing) {
Denis Vlasenko35ff7462007-11-28 19:23:12 +000098 DEBUG("Attaching option %02x to list", option->code);
Mike Frysinger7031f622006-05-08 03:20:50 +000099
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000100#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000101 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
102 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +0000103 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
Denis Vlasenko50664732007-02-27 21:15:08 +0000104#endif
105
Mike Frysinger7031f622006-05-08 03:20:50 +0000106 /* make a new option */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000107 new = xmalloc(sizeof(*new));
Mike Frysinger7031f622006-05-08 03:20:50 +0000108 new->data = xmalloc(length + 2);
109 new->data[OPT_CODE] = option->code;
110 new->data[OPT_LEN] = length;
111 memcpy(new->data + 2, buffer, length);
112
113 curr = opt_list;
114 while (*curr && (*curr)->data[OPT_CODE] < option->code)
115 curr = &(*curr)->next;
116
117 new->next = *curr;
118 *curr = new;
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 && buffer != NULL)
121 free(buffer);
122#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000123 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000124 }
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000125
126 /* add it to an existing option */
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000127 DEBUG("Attaching option %02x to existing member of list", option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000128 if (option->flags & OPTION_LIST) {
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000129#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000130 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
131 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +0000132 buffer = (char *)dname_enc(existing->data + 2,
Denis Vlasenko50664732007-02-27 21:15:08 +0000133 existing->data[OPT_LEN], buffer, &length);
134#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000135 if (existing->data[OPT_LEN] + length <= 255) {
136 existing->data = xrealloc(existing->data,
137 existing->data[OPT_LEN] + length + 3);
138 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
Denis Vlasenkoe129f432007-02-02 01:57:24 +0000139 /* ' ' can bring us to 256 - bad */
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000140 if (existing->data[OPT_LEN] + length >= 255)
141 return;
142 /* add space separator between STRING options in a list */
143 existing->data[existing->data[OPT_LEN] + 2] = ' ';
144 existing->data[OPT_LEN]++;
145 }
146 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
147 existing->data[OPT_LEN] += length;
148 } /* else, ignore the data, we could put this in a second option in the future */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000149#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000150 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
151 free(buffer);
152#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000153 } /* else, ignore the new data */
Mike Frysinger7031f622006-05-08 03:20:50 +0000154}
155
156
157/* read a dhcp option and add it to opt_list */
158static int read_opt(const char *const_line, void *arg)
159{
160 struct option_set **opt_list = arg;
161 char *opt, *val, *endptr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000162 char *line;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000163 const struct dhcp_option *option;
164 int retval, length, idx;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000165 char buffer[8] ALIGNED(4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000166 uint16_t *result_u16 = (uint16_t *) buffer;
167 uint32_t *result_u32 = (uint32_t *) buffer;
168
169 /* Cheat, the only const line we'll actually get is "" */
170 line = (char *) const_line;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000171 opt = strtok(line, " \t=");
Denis Vlasenkob539c842007-11-29 08:17:45 +0000172 if (!opt)
173 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000174
Denis Vlasenko027271e2008-03-20 12:47:01 +0000175 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000176 if (idx < 0)
177 return 0;
178 option = &dhcp_options[idx];
Mike Frysinger7031f622006-05-08 03:20:50 +0000179
Denis Vlasenkob539c842007-11-29 08:17:45 +0000180 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000181 do {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000182 val = strtok(NULL, ", \t");
183 if (!val) break;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000184 length = dhcp_option_lengths[option->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000185 retval = 0;
186 opt = buffer; /* new meaning for variable opt */
187 switch (option->flags & TYPE_MASK) {
188 case OPTION_IP:
189 retval = read_ip(val, buffer);
190 break;
191 case OPTION_IP_PAIR:
192 retval = read_ip(val, buffer);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000193 val = strtok(NULL, ", \t/-");
194 if (!val)
195 retval = 0;
196 if (retval)
197 retval = read_ip(val, buffer + 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000198 break;
199 case OPTION_STRING:
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000200#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000201 case OPTION_STR1035:
202#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000203 length = strlen(val);
204 if (length > 0) {
205 if (length > 254) length = 254;
206 opt = val;
207 retval = 1;
208 }
209 break;
210 case OPTION_BOOLEAN:
211 retval = read_yn(val, buffer);
212 break;
213 case OPTION_U8:
214 buffer[0] = strtoul(val, &endptr, 0);
215 retval = (endptr[0] == '\0');
216 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000217 /* htonX are macros in older libc's, using temp var
218 * in code below for safety */
219 /* TODO: use bb_strtoX? */
220 case OPTION_U16: {
221 unsigned long tmp = strtoul(val, &endptr, 0);
222 *result_u16 = htons(tmp);
223 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
224 break;
225 }
226 case OPTION_S16: {
227 long tmp = strtol(val, &endptr, 0);
228 *result_u16 = htons(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000229 retval = (endptr[0] == '\0');
230 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000231 }
232 case OPTION_U32: {
233 unsigned long tmp = strtoul(val, &endptr, 0);
234 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 retval = (endptr[0] == '\0');
236 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000237 }
238 case OPTION_S32: {
239 long tmp = strtol(val, &endptr, 0);
240 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000241 retval = (endptr[0] == '\0');
242 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000243 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000244 default:
245 break;
246 }
247 if (retval)
248 attach_option(opt_list, option, opt, length);
249 } while (retval && option->flags & OPTION_LIST);
250 return retval;
251}
252
253static int read_staticlease(const char *const_line, void *arg)
254{
Mike Frysinger7031f622006-05-08 03:20:50 +0000255 char *line;
256 char *mac_string;
257 char *ip_string;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000258 struct ether_addr mac_bytes;
259 uint32_t ip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000260
261 /* Read mac */
262 line = (char *) const_line;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000263 mac_string = strtok_r(line, " \t", &line);
264 read_mac(mac_string, &mac_bytes);
Mike Frysinger7031f622006-05-08 03:20:50 +0000265
266 /* Read ip */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000267 ip_string = strtok_r(NULL, " \t", &line);
268 read_ip(ip_string, &ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000269
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000270 addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000271
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000272 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000273
274 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000275}
276
277
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000278struct config_keyword {
279 const char *keyword;
280 int (*handler)(const char *line, void *var);
281 void *var;
282 const char *def;
Mike Frysinger7031f622006-05-08 03:20:50 +0000283};
284
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000285static const struct config_keyword keywords[] = {
286 /* keyword handler variable address default */
287 {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
288 {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
289 {"interface", read_str, &(server_config.interface), "eth0"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000290 /* Avoid "max_leases value not sane" warning by setting default
291 * to default_end_ip - default_start_ip + 1: */
292 {"max_leases", read_u32, &(server_config.max_leases), "235"},
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000293// {"remaining", read_yn, &(server_config.remaining), "yes"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000294 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
295 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
296 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
297 {"offer_time", read_u32, &(server_config.offer_time), "60"},
298 {"min_lease", read_u32, &(server_config.min_lease), "60"},
299 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
300 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000301 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
Denis Vlasenko027271e2008-03-20 12:47:01 +0000302 /* keywords with no defaults must be last! */
303 {"option", read_opt, &(server_config.options), ""},
304 {"opt", read_opt, &(server_config.options), ""},
305 {"notify_file", read_str, &(server_config.notify_file), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000306 {"sname", read_str, &(server_config.sname), ""},
307 {"boot_file", read_str, &(server_config.boot_file), ""},
308 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000309};
Denis Vlasenko027271e2008-03-20 12:47:01 +0000310enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000311
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000312void FAST_FUNC read_config(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000313{
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000314 parser_t *parser;
315 const struct config_keyword *k;
316 unsigned i;
317 char *token[2];
Mike Frysinger7031f622006-05-08 03:20:50 +0000318
Denis Vlasenko027271e2008-03-20 12:47:01 +0000319 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
320 keywords[i].handler(keywords[i].def, keywords[i].var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000321
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000322 parser = config_open(file);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000323 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000324 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
325 if (!strcasecmp(token[0], k->keyword)) {
326 if (!k->handler(token[1], k->var)) {
327 bb_error_msg("can't parse line %u in %s",
328 parser->lineno, file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000329 /* reset back to the default value */
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000330 k->handler(k->def, k->var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000331 }
Denis Vlasenko027271e2008-03-20 12:47:01 +0000332 break;
333 }
334 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000335 }
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000336 config_close(parser);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000337
338 server_config.start_ip = ntohl(server_config.start_ip);
339 server_config.end_ip = ntohl(server_config.end_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000340}
341
342
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000343void FAST_FUNC write_leases(void)
Mike Frysinger7031f622006-05-08 03:20:50 +0000344{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000345 int fd;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000346 unsigned i;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000347 leasetime_t curr;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000348 int64_t written_at;
Mike Frysinger7031f622006-05-08 03:20:50 +0000349
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000350 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
351 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000352 return;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000353
Denis Vlasenko73121c32009-02-01 00:40:45 +0000354 curr = written_at = time(NULL);
355
356 written_at = hton64(written_at);
357 full_write(fd, &written_at, sizeof(written_at));
Mike Frysinger7031f622006-05-08 03:20:50 +0000358
359 for (i = 0; i < server_config.max_leases; i++) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000360 leasetime_t tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000361
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000362 if (leases[i].yiaddr == 0)
363 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000364
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000365 /* screw with the time in the struct, for easier writing */
366 tmp_time = leases[i].expires;
Mike Frysinger7031f622006-05-08 03:20:50 +0000367
Denis Vlasenko73121c32009-02-01 00:40:45 +0000368 leases[i].expires -= curr;
369 if ((signed_leasetime_t) leases[i].expires < 0)
370 leases[i].expires = 0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000371 leases[i].expires = htonl(leases[i].expires);
372
373 /* No error check. If the file gets truncated,
374 * we lose some leases on restart. Oh well. */
375 full_write(fd, &leases[i], sizeof(leases[i]));
376
377 /* then restore it when done */
378 leases[i].expires = tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000379 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000380 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000381
382 if (server_config.notify_file) {
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000383// TODO: vfork-based child creation
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000384 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
385 system(cmd);
386 free(cmd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000387 }
388}
389
390
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000391void FAST_FUNC read_leases(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000392{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000393 int fd;
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000394 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000395 struct dhcpOfferedAddr lease;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000396 int64_t written_at, curr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000397
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000398 fd = open_or_warn(file, O_RDONLY);
399 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000400 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000401
Denis Vlasenko73121c32009-02-01 00:40:45 +0000402 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
403 goto ret;
404 written_at = ntoh64(written_at);
405 curr = time(NULL);
406 if (curr < written_at)
407 written_at = curr; /* lease file from future! :) */
408
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000409 i = 0;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000410 while (i < server_config.max_leases
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000411 && full_read(fd, &lease, sizeof(lease)) == sizeof(lease)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000412 ) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000413 /* ADDME: what if it matches some static lease? */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000414 uint32_t y = ntohl(lease.yiaddr);
415 if (y >= server_config.start_ip && y <= server_config.end_ip) {
Denis Vlasenko73121c32009-02-01 00:40:45 +0000416 int64_t expires = ntohl(lease.expires) + written_at - curr;
417 if (expires <= 0)
418 continue;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000419 /* NB: add_lease takes "relative time", IOW,
420 * lease duration, not lease deadline. */
421 if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000422 bb_error_msg("too many leases while loading %s", file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 break;
424 }
425 i++;
426 }
427 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000428 DEBUG("Read %d leases", i);
Denis Vlasenko73121c32009-02-01 00:40:45 +0000429 ret:
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000430 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000431}