diff options
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r-- | networking/udhcp/files.c | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 76b42f0af..64edcbb25 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c | |||
@@ -12,6 +12,16 @@ | |||
12 | #include "dhcpd.h" | 12 | #include "dhcpd.h" |
13 | #include "options.h" | 13 | #include "options.h" |
14 | 14 | ||
15 | #if BB_LITTLE_ENDIAN | ||
16 | static 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 | |||
15 | 25 | ||
16 | /* on these functions, make sure your datatype matches */ | 26 | /* on these functions, make sure your datatype matches */ |
17 | static int read_ip(const char *line, void *arg) | 27 | static int read_ip(const char *line, void *arg) |
@@ -335,14 +345,16 @@ void FAST_FUNC write_leases(void) | |||
335 | int fd; | 345 | int fd; |
336 | unsigned i; | 346 | unsigned i; |
337 | leasetime_t curr; | 347 | leasetime_t curr; |
348 | int64_t written_at; | ||
338 | 349 | ||
339 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); | 350 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
340 | if (fd < 0) | 351 | if (fd < 0) |
341 | return; | 352 | return; |
342 | 353 | ||
343 | curr = time(NULL); | 354 | curr = written_at = time(NULL); |
344 | //TODO: write out current time? Readers need to adjust .expires field | 355 | |
345 | // to account for time between file was written and when it was read back. | 356 | written_at = hton64(written_at); |
357 | full_write(fd, &written_at, sizeof(written_at)); | ||
346 | 358 | ||
347 | for (i = 0; i < server_config.max_leases; i++) { | 359 | for (i = 0; i < server_config.max_leases; i++) { |
348 | leasetime_t tmp_time; | 360 | leasetime_t tmp_time; |
@@ -353,11 +365,9 @@ void FAST_FUNC write_leases(void) | |||
353 | /* screw with the time in the struct, for easier writing */ | 365 | /* screw with the time in the struct, for easier writing */ |
354 | tmp_time = leases[i].expires; | 366 | tmp_time = leases[i].expires; |
355 | 367 | ||
356 | //if (server_config.remaining) { | 368 | leases[i].expires -= curr; |
357 | leases[i].expires -= curr; | 369 | if ((signed_leasetime_t) leases[i].expires < 0) |
358 | if ((signed_leasetime_t) leases[i].expires < 0) | 370 | 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); | 371 | leases[i].expires = htonl(leases[i].expires); |
362 | 372 | ||
363 | /* No error check. If the file gets truncated, | 373 | /* No error check. If the file gets truncated, |
@@ -382,14 +392,20 @@ void FAST_FUNC read_leases(const char *file) | |||
382 | { | 392 | { |
383 | int fd; | 393 | int fd; |
384 | unsigned i; | 394 | unsigned i; |
385 | // leasetime_t curr; | ||
386 | struct dhcpOfferedAddr lease; | 395 | struct dhcpOfferedAddr lease; |
396 | int64_t written_at, curr; | ||
387 | 397 | ||
388 | fd = open_or_warn(file, O_RDONLY); | 398 | fd = open_or_warn(file, O_RDONLY); |
389 | if (fd < 0) | 399 | if (fd < 0) |
390 | return; | 400 | return; |
391 | 401 | ||
392 | // curr = time(NULL); | 402 | 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 | |||
393 | i = 0; | 409 | i = 0; |
394 | while (i < server_config.max_leases | 410 | while (i < server_config.max_leases |
395 | && full_read(fd, &lease, sizeof(lease)) == sizeof(lease) | 411 | && full_read(fd, &lease, sizeof(lease)) == sizeof(lease) |
@@ -397,9 +413,9 @@ void FAST_FUNC read_leases(const char *file) | |||
397 | /* ADDME: what if it matches some static lease? */ | 413 | /* ADDME: what if it matches some static lease? */ |
398 | uint32_t y = ntohl(lease.yiaddr); | 414 | uint32_t y = ntohl(lease.yiaddr); |
399 | if (y >= server_config.start_ip && y <= server_config.end_ip) { | 415 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
400 | leasetime_t expires = ntohl(lease.expires); | 416 | int64_t expires = ntohl(lease.expires) + written_at - curr; |
401 | // if (!server_config.remaining) | 417 | if (expires <= 0) |
402 | // expires -= curr; | 418 | continue; |
403 | /* NB: add_lease takes "relative time", IOW, | 419 | /* NB: add_lease takes "relative time", IOW, |
404 | * lease duration, not lease deadline. */ | 420 | * lease duration, not lease deadline. */ |
405 | if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) { | 421 | if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) { |
@@ -410,5 +426,6 @@ void FAST_FUNC read_leases(const char *file) | |||
410 | } | 426 | } |
411 | } | 427 | } |
412 | DEBUG("Read %d leases", i); | 428 | DEBUG("Read %d leases", i); |
429 | ret: | ||
413 | close(fd); | 430 | close(fd); |
414 | } | 431 | } |