diff options
-rw-r--r-- | networking/udhcp/dumpleases.c | 27 | ||||
-rw-r--r-- | networking/udhcp/files.c | 43 |
2 files changed, 52 insertions, 18 deletions
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 2d16ec160..c0d515d28 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c | |||
@@ -6,14 +6,24 @@ | |||
6 | #include "common.h" | 6 | #include "common.h" |
7 | #include "dhcpd.h" | 7 | #include "dhcpd.h" |
8 | 8 | ||
9 | #if BB_LITTLE_ENDIAN | ||
10 | static inline uint64_t hton64(uint64_t v) | ||
11 | { | ||
12 | return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32); | ||
13 | } | ||
14 | #else | ||
15 | #define hton64(v) (v) | ||
16 | #endif | ||
17 | #define ntoh64(v) hton64(v) | ||
18 | |||
19 | |||
9 | int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 20 | int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
10 | int dumpleases_main(int argc UNUSED_PARAM, char **argv) | 21 | int dumpleases_main(int argc UNUSED_PARAM, char **argv) |
11 | { | 22 | { |
12 | int fd; | 23 | int fd; |
13 | int i; | 24 | int i; |
14 | unsigned opt; | 25 | unsigned opt; |
15 | leasetime_t expires; | 26 | int64_t written_at, curr, expires_abs; |
16 | leasetime_t curr; | ||
17 | const char *file = LEASES_FILE; | 27 | const char *file = LEASES_FILE; |
18 | struct dhcpOfferedAddr lease; | 28 | struct dhcpOfferedAddr lease; |
19 | struct in_addr addr; | 29 | struct in_addr addr; |
@@ -40,7 +50,13 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv) | |||
40 | printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in"); | 50 | printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in"); |
41 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ | 51 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
42 | 52 | ||
53 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) | ||
54 | return 0; | ||
55 | written_at = ntoh64(written_at); | ||
43 | curr = time(NULL); | 56 | curr = time(NULL); |
57 | if (curr < written_at) | ||
58 | written_at = curr; /* lease file from future! :) */ | ||
59 | |||
44 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { | 60 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
45 | const char *fmt = ":%02x" + 1; | 61 | const char *fmt = ":%02x" + 1; |
46 | for (i = 0; i < 6; i++) { | 62 | for (i = 0; i < 6; i++) { |
@@ -49,13 +65,14 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv) | |||
49 | } | 65 | } |
50 | addr.s_addr = lease.yiaddr; | 66 | addr.s_addr = lease.yiaddr; |
51 | printf(" %-15s ", inet_ntoa(addr)); | 67 | printf(" %-15s ", inet_ntoa(addr)); |
52 | if (lease.expires == 0) { | 68 | expires_abs = ntohl(lease.expires) + written_at; |
69 | if (expires_abs <= curr) { | ||
53 | puts("expired"); | 70 | puts("expired"); |
54 | continue; | 71 | continue; |
55 | } | 72 | } |
56 | expires = ntohl(lease.expires); | ||
57 | if (!(opt & OPT_a)) { /* no -a */ | 73 | if (!(opt & OPT_a)) { /* no -a */ |
58 | unsigned d, h, m; | 74 | unsigned d, h, m; |
75 | unsigned expires = expires_abs - curr; | ||
59 | d = expires / (24*60*60); expires %= (24*60*60); | 76 | d = expires / (24*60*60); expires %= (24*60*60); |
60 | h = expires / (60*60); expires %= (60*60); | 77 | h = expires / (60*60); expires %= (60*60); |
61 | m = expires / 60; expires %= 60; | 78 | m = expires / 60; expires %= 60; |
@@ -63,7 +80,7 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv) | |||
63 | printf("%u days ", d); | 80 | printf("%u days ", d); |
64 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); | 81 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
65 | } else { /* -a */ | 82 | } else { /* -a */ |
66 | time_t t = expires + curr; | 83 | time_t t = expires_abs; |
67 | fputs(ctime(&t), stdout); | 84 | fputs(ctime(&t), stdout); |
68 | } | 85 | } |
69 | } | 86 | } |
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 | } |