[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
| 4 | */
|
---|
| 5 | #include <getopt.h>
|
---|
| 6 |
|
---|
[1765] | 7 | #include "common.h"
|
---|
[821] | 8 | #include "dhcpd.h"
|
---|
| 9 |
|
---|
[1765] | 10 | int dumpleases_main(int argc, char **argv);
|
---|
| 11 | int dumpleases_main(int argc, char **argv)
|
---|
[821] | 12 | {
|
---|
[1765] | 13 | int fd;
|
---|
| 14 | int i;
|
---|
| 15 | unsigned opt;
|
---|
| 16 | time_t expires;
|
---|
[821] | 17 | const char *file = LEASES_FILE;
|
---|
| 18 | struct dhcpOfferedAddr lease;
|
---|
| 19 | struct in_addr addr;
|
---|
| 20 |
|
---|
[1765] | 21 | enum {
|
---|
| 22 | OPT_a = 0x1, // -a
|
---|
| 23 | OPT_r = 0x2, // -r
|
---|
| 24 | OPT_f = 0x4, // -f
|
---|
[821] | 25 | };
|
---|
[1765] | 26 | #if ENABLE_GETOPT_LONG
|
---|
| 27 | static const char dumpleases_longopts[] ALIGN1 =
|
---|
| 28 | "absolute\0" No_argument "a"
|
---|
| 29 | "remaining\0" No_argument "r"
|
---|
| 30 | "file\0" Required_argument "f"
|
---|
| 31 | ;
|
---|
[821] | 32 |
|
---|
[1765] | 33 | applet_long_options = dumpleases_longopts;
|
---|
| 34 | #endif
|
---|
| 35 | opt_complementary = "=0:a--r:r--a";
|
---|
| 36 | opt = getopt32(argv, "arf:", &file);
|
---|
[821] | 37 |
|
---|
[1765] | 38 | fd = xopen(file, O_RDONLY);
|
---|
[821] | 39 |
|
---|
[1765] | 40 | printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in");
|
---|
[821] | 41 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
|
---|
[1765] | 42 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
|
---|
| 43 | printf(":%02x"+1, lease.chaddr[0]);
|
---|
| 44 | for (i = 1; i < 6; i++) {
|
---|
| 45 | printf(":%02x", lease.chaddr[i]);
|
---|
[821] | 46 | }
|
---|
| 47 | addr.s_addr = lease.yiaddr;
|
---|
[1765] | 48 | printf(" %-15s ", inet_ntoa(addr));
|
---|
[821] | 49 | expires = ntohl(lease.expires);
|
---|
[1765] | 50 | if (!(opt & OPT_a)) { /* no -a */
|
---|
| 51 | if (!expires)
|
---|
| 52 | puts("expired");
|
---|
[821] | 53 | else {
|
---|
[1765] | 54 | unsigned d, h, m;
|
---|
| 55 | d = expires / (24*60*60); expires %= (24*60*60);
|
---|
| 56 | h = expires / (60*60); expires %= (60*60);
|
---|
| 57 | m = expires / 60; expires %= 60;
|
---|
| 58 | if (d) printf("%u days ", d);
|
---|
| 59 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
|
---|
[821] | 60 | }
|
---|
[1765] | 61 | } else /* -a */
|
---|
| 62 | fputs(ctime(&expires), stdout);
|
---|
[821] | 63 | }
|
---|
[1765] | 64 | /* close(fd); */
|
---|
[821] | 65 |
|
---|
| 66 | return 0;
|
---|
| 67 | }
|
---|