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 | { |
| 16 | FILE *fp; |
| 17 | int i, c, mode = REMAINING; |
| 18 | long expires; |
| 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 | |
| 46 | fp = xfopen(file, "r"); |
| 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" */ |
| 50 | while (fread(&lease, sizeof(lease), 1, fp)) { |
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 { |
| 62 | if (expires > 60*60*24) { |
| 63 | printf("%ld days, ", expires / (60*60*24)); |
| 64 | expires %= 60*60*24; |
| 65 | } |
| 66 | if (expires > 60*60) { |
| 67 | printf("%ld hours, ", expires / (60*60)); |
| 68 | expires %= 60*60; |
| 69 | } |
| 70 | if (expires > 60) { |
| 71 | printf("%ld minutes, ", expires / 60); |
| 72 | expires %= 60; |
| 73 | } |
| 74 | printf("%ld seconds\n", expires); |
| 75 | } |
| 76 | } else printf("%s", ctime(&expires)); |
| 77 | } |
| 78 | fclose(fp); |
| 79 | |
| 80 | return 0; |
| 81 | } |