aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-11-10 18:25:18 -0600
committerRob Landley <rob@landley.net>2013-11-10 18:25:18 -0600
commit78af19df5138759bbd008aeb8a332b21da0bc8b1 (patch)
treee59d0e99faaee28e8ce981a2fef5c594c465fb74 /toys
parent35b40be7aa6597fc37be3016483df88a2c7cc93c (diff)
downloadtoybox-78af19df5138759bbd008aeb8a332b21da0bc8b1.tar.gz
dumpleases (for dhcpd) submitted by Ashwini Sharma.
Diffstat (limited to 'toys')
-rw-r--r--toys/pending/dumpleases.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/toys/pending/dumpleases.c b/toys/pending/dumpleases.c
new file mode 100644
index 00000000..b3ce4a9c
--- /dev/null
+++ b/toys/pending/dumpleases.c
@@ -0,0 +1,78 @@
+/* dumpleases.c - Dump the leases granted by udhcpd.
+ *
+ * Copyright 2013 Sandeep Sharma <sandeep.jack2756@gmail.com>
+ * Copyright 2013 Kyungwan Han <asura321@gmail.com>
+ *
+
+USE_DUMPLEASES(NEWTOY(dumpleases, ">0arf:[!ar]", TOYFLAG_USR|TOYFLAG_BIN))
+
+config DUMPLEASES
+ bool "dumpleases"
+ default n
+ help
+ Usage: dumpleases [-r|-a] [-f LEASEFILE]
+
+ Display DHCP leases granted by udhcpd
+ -f FILE, Lease file
+ -r Show remaining time
+ -a Show expiration time
+*/
+
+#define FOR_dumpleases
+#include "toys.h"
+#include "toynet.h"
+
+GLOBALS(
+ char *file;
+)
+
+//lease structure
+struct lease {
+ uint32_t expires;
+ uint32_t lease_nip;
+ uint8_t lease_mac[6];
+ char hostname[20];
+ uint8_t pad[2]; //Padding
+};
+
+void dumpleases_main(void)
+{
+ struct in_addr addr;
+ struct lease lease_struct;
+ int64_t written_time , current_time, exp;
+ int i, fd;
+
+ if(!(toys.optflags & FLAG_f)) TT.file = "/var/lib/misc/udhcpd.leases"; //DEF_LEASE_FILE
+ fd = xopen(TT.file, O_RDONLY);
+ xprintf("Mac Address IP Address Host Name Expires %s\n", (toys.optflags & FLAG_a) ? "at" : "in");
+ xread(fd, &written_time, sizeof(written_time));
+ current_time = time(NULL);
+ written_time = SWAP_BE64(written_time);
+ if(current_time < written_time) written_time = current_time;
+
+ while(sizeof(lease_struct) ==
+ (readall(fd, &lease_struct, sizeof(lease_struct)))) {
+ for (i = 0; i < 6; i++) printf(":%02x"+ !i, lease_struct.lease_mac[i]);
+
+ addr.s_addr = lease_struct.lease_nip;
+ lease_struct.hostname[19] = '\0';
+ xprintf(" %-16s%-20s", inet_ntoa(addr), lease_struct.hostname );
+ exp = ntohl(lease_struct.expires) + written_time;
+ if (exp <= current_time) {
+ xputs("expired");
+ continue;
+ }
+ if (!(toys.optflags & FLAG_a)) {
+ unsigned dt, hr, m;
+ unsigned expires = exp - current_time;
+ dt = expires / (24*60*60); expires %= (24*60*60);
+ hr = expires / (60*60); expires %= (60*60);
+ m = expires / 60; expires %= 60;
+ if (dt) xprintf("%u days ", dt);
+ xprintf("%02u:%02u:%02u\n", hr, m, (unsigned)expires);
+ } else {
+ fputs(ctime((const time_t*)&exp), stdout);
+ }
+ }
+ xclose(fd);
+}