Mercurial > hg > toybox
comparison toys/pending/dumpleases.c @ 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 | |
children | 473491dbd8c3 |
comparison
equal
deleted
inserted
replaced
1116:b2a8f64a564b | 1117:4c664a479dd5 |
---|---|
1 /* dumpleases.c - Dump the leases granted by udhcpd. | |
2 * | |
3 * Copyright 2013 Sandeep Sharma <sandeep.jack2756@gmail.com> | |
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> | |
5 * | |
6 | |
7 USE_DUMPLEASES(NEWTOY(dumpleases, ">0arf:[!ar]", TOYFLAG_USR|TOYFLAG_BIN)) | |
8 | |
9 config DUMPLEASES | |
10 bool "dumpleases" | |
11 default n | |
12 help | |
13 Usage: dumpleases [-r|-a] [-f LEASEFILE] | |
14 | |
15 Display DHCP leases granted by udhcpd | |
16 -f FILE, Lease file | |
17 -r Show remaining time | |
18 -a Show expiration time | |
19 */ | |
20 | |
21 #define FOR_dumpleases | |
22 #include "toys.h" | |
23 #include "toynet.h" | |
24 | |
25 GLOBALS( | |
26 char *file; | |
27 ) | |
28 | |
29 //lease structure | |
30 struct lease { | |
31 uint32_t expires; | |
32 uint32_t lease_nip; | |
33 uint8_t lease_mac[6]; | |
34 char hostname[20]; | |
35 uint8_t pad[2]; //Padding | |
36 }; | |
37 | |
38 void dumpleases_main(void) | |
39 { | |
40 struct in_addr addr; | |
41 struct lease lease_struct; | |
42 int64_t written_time , current_time, exp; | |
43 int i, fd; | |
44 | |
45 if(!(toys.optflags & FLAG_f)) TT.file = "/var/lib/misc/udhcpd.leases"; //DEF_LEASE_FILE | |
46 fd = xopen(TT.file, O_RDONLY); | |
47 xprintf("Mac Address IP Address Host Name Expires %s\n", (toys.optflags & FLAG_a) ? "at" : "in"); | |
48 xread(fd, &written_time, sizeof(written_time)); | |
49 current_time = time(NULL); | |
50 written_time = SWAP_BE64(written_time); | |
51 if(current_time < written_time) written_time = current_time; | |
52 | |
53 while(sizeof(lease_struct) == | |
54 (readall(fd, &lease_struct, sizeof(lease_struct)))) { | |
55 for (i = 0; i < 6; i++) printf(":%02x"+ !i, lease_struct.lease_mac[i]); | |
56 | |
57 addr.s_addr = lease_struct.lease_nip; | |
58 lease_struct.hostname[19] = '\0'; | |
59 xprintf(" %-16s%-20s", inet_ntoa(addr), lease_struct.hostname ); | |
60 exp = ntohl(lease_struct.expires) + written_time; | |
61 if (exp <= current_time) { | |
62 xputs("expired"); | |
63 continue; | |
64 } | |
65 if (!(toys.optflags & FLAG_a)) { | |
66 unsigned dt, hr, m; | |
67 unsigned expires = exp - current_time; | |
68 dt = expires / (24*60*60); expires %= (24*60*60); | |
69 hr = expires / (60*60); expires %= (60*60); | |
70 m = expires / 60; expires %= 60; | |
71 if (dt) xprintf("%u days ", dt); | |
72 xprintf("%02u:%02u:%02u\n", hr, m, (unsigned)expires); | |
73 } else { | |
74 fputs(ctime((const time_t*)&exp), stdout); | |
75 } | |
76 } | |
77 xclose(fd); | |
78 } |