1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);
}
|