aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/dumpleases.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-02-01 00:40:45 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-02-01 00:40:45 +0000
commit73121c3c8c28d299280a2f1360e038e93f0b254f (patch)
treedd021a127aa19bccfceb9a2894bcc76aea01a636 /networking/udhcp/dumpleases.c
parent49325969e75dd85366d9a149625f75c323163723 (diff)
downloadbusybox-w32-73121c3c8c28d299280a2f1360e038e93f0b254f.tar.gz
busybox-w32-73121c3c8c28d299280a2f1360e038e93f0b254f.tar.bz2
busybox-w32-73121c3c8c28d299280a2f1360e038e93f0b254f.zip
dhcpd,dumpleases: write and use 64-bit current time in lease file.
without it, determination of remaining lease time is unreliable. Yes, it's costly in code size, but without it, dumpleases output is misleading! function old new delta read_leases 143 282 +139 dumpleases_main 447 572 +125 write_leases 193 233 +40 hton64 29 58 +29 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 4/0 up/down: 333/0) Total: 333 bytes
Diffstat (limited to 'networking/udhcp/dumpleases.c')
-rw-r--r--networking/udhcp/dumpleases.c27
1 files changed, 22 insertions, 5 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
10static 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
9int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 20int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
10int dumpleases_main(int argc UNUSED_PARAM, char **argv) 21int 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 }