aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/files.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r--networking/udhcp/files.c107
1 files changed, 53 insertions, 54 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 4f7b52056..76b42f0af 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -28,12 +28,7 @@ static int read_ip(const char *line, void *arg)
28 28
29static int read_mac(const char *line, void *arg) 29static int read_mac(const char *line, void *arg)
30{ 30{
31 struct ether_addr *temp_ether_addr; 31 return NULL == ether_aton_r(line, (struct ether_addr *)arg);
32
33 temp_ether_addr = ether_aton_r(line, (struct ether_addr *)arg);
34 if (temp_ether_addr == NULL)
35 return 0;
36 return 1;
37} 32}
38 33
39 34
@@ -250,23 +245,19 @@ static int read_staticlease(const char *const_line, void *arg)
250 char *line; 245 char *line;
251 char *mac_string; 246 char *mac_string;
252 char *ip_string; 247 char *ip_string;
253 uint8_t *mac_bytes; 248 struct ether_addr mac_bytes;
254 uint32_t *ip; 249 uint32_t ip;
255
256 /* Allocate memory for addresses */
257 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
258 ip = xmalloc(sizeof(uint32_t));
259 250
260 /* Read mac */ 251 /* Read mac */
261 line = (char *) const_line; 252 line = (char *) const_line;
262 mac_string = strtok(line, " \t"); 253 mac_string = strtok_r(line, " \t", &line);
263 read_mac(mac_string, mac_bytes); 254 read_mac(mac_string, &mac_bytes);
264 255
265 /* Read ip */ 256 /* Read ip */
266 ip_string = strtok(NULL, " \t"); 257 ip_string = strtok_r(NULL, " \t", &line);
267 read_ip(ip_string, ip); 258 read_ip(ip_string, &ip);
268 259
269 addStaticLease(arg, mac_bytes, ip); 260 addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
270 261
271 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg); 262 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
272 263
@@ -289,7 +280,7 @@ static const struct config_keyword keywords[] = {
289 /* Avoid "max_leases value not sane" warning by setting default 280 /* Avoid "max_leases value not sane" warning by setting default
290 * to default_end_ip - default_start_ip + 1: */ 281 * to default_end_ip - default_start_ip + 1: */
291 {"max_leases", read_u32, &(server_config.max_leases), "235"}, 282 {"max_leases", read_u32, &(server_config.max_leases), "235"},
292 {"remaining", read_yn, &(server_config.remaining), "yes"}, 283// {"remaining", read_yn, &(server_config.remaining), "yes"},
293 {"auto_time", read_u32, &(server_config.auto_time), "7200"}, 284 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
294 {"decline_time", read_u32, &(server_config.decline_time), "3600"}, 285 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
295 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"}, 286 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
@@ -305,7 +296,6 @@ static const struct config_keyword keywords[] = {
305 {"sname", read_str, &(server_config.sname), ""}, 296 {"sname", read_str, &(server_config.sname), ""},
306 {"boot_file", read_str, &(server_config.boot_file), ""}, 297 {"boot_file", read_str, &(server_config.boot_file), ""},
307 {"static_lease", read_staticlease, &(server_config.static_leases), ""}, 298 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
308 /* ADDME: static lease */
309}; 299};
310enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; 300enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
311 301
@@ -342,36 +332,42 @@ void FAST_FUNC read_config(const char *file)
342 332
343void FAST_FUNC write_leases(void) 333void FAST_FUNC write_leases(void)
344{ 334{
345 int fp; 335 int fd;
346 unsigned i; 336 unsigned i;
347 time_t curr = time(0); 337 leasetime_t curr;
348 unsigned long tmp_time;
349 338
350 fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); 339 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
351 if (fp < 0) { 340 if (fd < 0)
352 return; 341 return;
353 } 342
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.
354 346
355 for (i = 0; i < server_config.max_leases; i++) { 347 for (i = 0; i < server_config.max_leases; i++) {
356 if (leases[i].yiaddr != 0) { 348 leasetime_t tmp_time;
357 349
358 /* screw with the time in the struct, for easier writing */ 350 if (leases[i].yiaddr == 0)
359 tmp_time = leases[i].expires; 351 continue;
360 352
361 if (server_config.remaining) { 353 /* screw with the time in the struct, for easier writing */
362 if (lease_expired(&(leases[i]))) 354 tmp_time = leases[i].expires;
363 leases[i].expires = 0; 355
364 else leases[i].expires -= curr; 356 //if (server_config.remaining) {
365 } /* else stick with the time we got */ 357 leases[i].expires -= curr;
366 leases[i].expires = htonl(leases[i].expires); 358 if ((signed_leasetime_t) leases[i].expires < 0)
367 // FIXME: error check?? 359 leases[i].expires = 0;
368 full_write(fp, &leases[i], sizeof(leases[i])); 360 //} /* else stick with the time we got */
369 361 leases[i].expires = htonl(leases[i].expires);
370 /* then restore it when done */ 362
371 leases[i].expires = tmp_time; 363 /* No error check. If the file gets truncated,
372 } 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;
373 } 369 }
374 close(fp); 370 close(fd);
375 371
376 if (server_config.notify_file) { 372 if (server_config.notify_file) {
377// TODO: vfork-based child creation 373// TODO: vfork-based child creation
@@ -384,26 +380,29 @@ void FAST_FUNC write_leases(void)
384 380
385void FAST_FUNC read_leases(const char *file) 381void FAST_FUNC read_leases(const char *file)
386{ 382{
387 int fp; 383 int fd;
388 unsigned i; 384 unsigned i;
385// leasetime_t curr;
389 struct dhcpOfferedAddr lease; 386 struct dhcpOfferedAddr lease;
390 387
391 fp = open_or_warn(file, O_RDONLY); 388 fd = open_or_warn(file, O_RDONLY);
392 if (fp < 0) { 389 if (fd < 0)
393 return; 390 return;
394 }
395 391
392// curr = time(NULL);
396 i = 0; 393 i = 0;
397 while (i < server_config.max_leases 394 while (i < server_config.max_leases
398 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease) 395 && full_read(fd, &lease, sizeof(lease)) == sizeof(lease)
399 ) { 396 ) {
400 /* ADDME: is it a static lease */ 397 /* ADDME: what if it matches some static lease? */
401 uint32_t y = ntohl(lease.yiaddr); 398 uint32_t y = ntohl(lease.yiaddr);
402 if (y >= server_config.start_ip && y <= server_config.end_ip) { 399 if (y >= server_config.start_ip && y <= server_config.end_ip) {
403 lease.expires = ntohl(lease.expires); 400 leasetime_t expires = ntohl(lease.expires);
404 if (!server_config.remaining) 401// if (!server_config.remaining)
405 lease.expires -= time(NULL); 402// expires -= curr;
406 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { 403 /* NB: add_lease takes "relative time", IOW,
404 * lease duration, not lease deadline. */
405 if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) {
407 bb_error_msg("too many leases while loading %s", file); 406 bb_error_msg("too many leases while loading %s", file);
408 break; 407 break;
409 } 408 }
@@ -411,5 +410,5 @@ void FAST_FUNC read_leases(const char *file)
411 } 410 }
412 } 411 }
413 DEBUG("Read %d leases", i); 412 DEBUG("Read %d leases", i);
414 close(fp); 413 close(fd);
415} 414}