diff options
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r-- | networking/udhcp/files.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 64edcbb25..fb6fe01ae 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c | |||
@@ -36,6 +36,7 @@ static int read_ip(const char *line, void *arg) | |||
36 | return 1; | 36 | return 1; |
37 | } | 37 | } |
38 | 38 | ||
39 | |||
39 | static int read_mac(const char *line, void *arg) | 40 | static int read_mac(const char *line, void *arg) |
40 | { | 41 | { |
41 | return NULL == ether_aton_r(line, (struct ether_addr *)arg); | 42 | return NULL == ether_aton_r(line, (struct ether_addr *)arg); |
@@ -362,7 +363,7 @@ void FAST_FUNC write_leases(void) | |||
362 | if (leases[i].yiaddr == 0) | 363 | if (leases[i].yiaddr == 0) |
363 | continue; | 364 | continue; |
364 | 365 | ||
365 | /* screw with the time in the struct, for easier writing */ | 366 | /* Screw with the time in the struct, for easier writing */ |
366 | tmp_time = leases[i].expires; | 367 | tmp_time = leases[i].expires; |
367 | 368 | ||
368 | leases[i].expires -= curr; | 369 | leases[i].expires -= curr; |
@@ -374,7 +375,7 @@ void FAST_FUNC write_leases(void) | |||
374 | * we lose some leases on restart. Oh well. */ | 375 | * we lose some leases on restart. Oh well. */ |
375 | full_write(fd, &leases[i], sizeof(leases[i])); | 376 | full_write(fd, &leases[i], sizeof(leases[i])); |
376 | 377 | ||
377 | /* then restore it when done */ | 378 | /* Then restore it when done */ |
378 | leases[i].expires = tmp_time; | 379 | leases[i].expires = tmp_time; |
379 | } | 380 | } |
380 | close(fd); | 381 | close(fd); |
@@ -390,10 +391,10 @@ void FAST_FUNC write_leases(void) | |||
390 | 391 | ||
391 | void FAST_FUNC read_leases(const char *file) | 392 | void FAST_FUNC read_leases(const char *file) |
392 | { | 393 | { |
393 | int fd; | ||
394 | unsigned i; | ||
395 | struct dhcpOfferedAddr lease; | 394 | struct dhcpOfferedAddr lease; |
396 | int64_t written_at, curr; | 395 | int64_t written_at, time_passed; |
396 | int fd; | ||
397 | USE_UDHCP_DEBUG(unsigned i;) | ||
397 | 398 | ||
398 | fd = open_or_warn(file, O_RDONLY); | 399 | fd = open_or_warn(file, O_RDONLY); |
399 | if (fd < 0) | 400 | if (fd < 0) |
@@ -402,18 +403,19 @@ void FAST_FUNC read_leases(const char *file) | |||
402 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) | 403 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) |
403 | goto ret; | 404 | goto ret; |
404 | written_at = ntoh64(written_at); | 405 | written_at = ntoh64(written_at); |
405 | curr = time(NULL); | 406 | |
406 | if (curr < written_at) | 407 | time_passed = time(NULL) - written_at; |
407 | written_at = curr; /* lease file from future! :) */ | 408 | /* Strange written_at, or lease file from old version of udhcpd |
408 | 409 | * which had no "written_at" field? */ | |
409 | i = 0; | 410 | if ((uint64_t)time_passed > 12 * 60 * 60) |
410 | while (i < server_config.max_leases | 411 | goto ret; |
411 | && full_read(fd, &lease, sizeof(lease)) == sizeof(lease) | 412 | |
412 | ) { | 413 | USE_UDHCP_DEBUG(i = 0;) |
414 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { | ||
413 | /* ADDME: what if it matches some static lease? */ | 415 | /* ADDME: what if it matches some static lease? */ |
414 | uint32_t y = ntohl(lease.yiaddr); | 416 | uint32_t y = ntohl(lease.yiaddr); |
415 | if (y >= server_config.start_ip && y <= server_config.end_ip) { | 417 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
416 | int64_t expires = ntohl(lease.expires) + written_at - curr; | 418 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
417 | if (expires <= 0) | 419 | if (expires <= 0) |
418 | continue; | 420 | continue; |
419 | /* NB: add_lease takes "relative time", IOW, | 421 | /* NB: add_lease takes "relative time", IOW, |
@@ -422,7 +424,7 @@ void FAST_FUNC read_leases(const char *file) | |||
422 | bb_error_msg("too many leases while loading %s", file); | 424 | bb_error_msg("too many leases while loading %s", file); |
423 | break; | 425 | break; |
424 | } | 426 | } |
425 | i++; | 427 | USE_UDHCP_DEBUG(i++;) |
426 | } | 428 | } |
427 | } | 429 | } |
428 | DEBUG("Read %d leases", i); | 430 | DEBUG("Read %d leases", i); |