Mercurial > hg > toybox
changeset 1117:4c664a479dd5 draft
dumpleases (for dhcpd) submitted by Ashwini Sharma.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 10 Nov 2013 18:25:18 -0600 |
parents | b2a8f64a564b |
children | 04f83dae08b4 |
files | toys/pending/dumpleases.c |
diffstat | 1 files changed, 78 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/pending/dumpleases.c Sun Nov 10 18:25:18 2013 -0600 @@ -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); +}