Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 4 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 5 | #include <getopt.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 6 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 7 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | #include "dhcpd.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 9 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | |
| 11 | #define REMAINING 0 |
| 12 | #define ABSOLUTE 1 |
| 13 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | int dumpleases_main(int argc, char *argv[]) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 15 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 16 | int fp; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 17 | int i, c, mode = REMAINING; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 18 | unsigned long expires; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 19 | const char *file = LEASES_FILE; |
| 20 | struct dhcpOfferedAddr lease; |
| 21 | struct in_addr addr; |
| 22 | |
| 23 | static const struct option options[] = { |
| 24 | {"absolute", 0, 0, 'a'}, |
| 25 | {"remaining", 0, 0, 'r'}, |
| 26 | {"file", 1, 0, 'f'}, |
| 27 | {0, 0, 0, 0} |
| 28 | }; |
| 29 | |
| 30 | while (1) { |
| 31 | int option_index = 0; |
| 32 | c = getopt_long(argc, argv, "arf:", options, &option_index); |
| 33 | if (c == -1) break; |
| 34 | |
| 35 | switch (c) { |
| 36 | case 'a': mode = ABSOLUTE; break; |
| 37 | case 'r': mode = REMAINING; break; |
| 38 | case 'f': |
| 39 | file = optarg; |
| 40 | break; |
| 41 | default: |
Bernhard Reutner-Fischer | 5bd2a4b | 2006-08-18 17:22:23 +0000 | [diff] [blame] | 42 | bb_show_usage(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 46 | fp = xopen(file, O_RDONLY); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | |
| 48 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); |
| 49 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 50 | while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 51 | printf(":%02x"+1, lease.chaddr[0]); |
| 52 | for (i = 1; i < 6; i++) { |
| 53 | printf(":%02x", lease.chaddr[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 54 | } |
| 55 | addr.s_addr = lease.yiaddr; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 56 | printf(" %-15s ", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | expires = ntohl(lease.expires); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | if (mode == REMAINING) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 59 | if (!expires) |
| 60 | printf("expired\n"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 61 | else { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 62 | unsigned d, h, m; |
| 63 | d = expires / (24*60*60); expires %= (24*60*60); |
| 64 | h = expires / (60*60); expires %= (60*60); |
| 65 | m = expires / 60; expires %= 60; |
| 66 | if (d) printf("%u days ", d); |
| 67 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 69 | } else fputs(ctime(&expires), stdout); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 71 | /* close(fp); */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |