Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 5 | #include <sys/wait.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 6 | #include <arpa/inet.h> |
| 7 | #include <netdb.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <stdio.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <unistd.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 12 | #include <getopt.h> |
| 13 | #include <time.h> |
| 14 | |
Russ Dill | 4040d3d | 2003-12-16 02:34:28 +0000 | [diff] [blame] | 15 | #include "dhcpd.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 16 | #include "leases.h" |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 17 | #include "libbb_udhcp.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 18 | |
| 19 | #define REMAINING 0 |
| 20 | #define ABSOLUTE 1 |
| 21 | |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 22 | |
| 23 | #ifndef IN_BUSYBOX |
| 24 | static void __attribute__ ((noreturn)) show_usage(void) |
| 25 | { |
| 26 | printf( |
| 27 | "Usage: dumpleases -f <file> -[r|a]\n\n" |
| 28 | " -f, --file=FILENAME Leases file to load\n" |
| 29 | " -r, --remaining Interepret lease times as time remaing\n" |
| 30 | " -a, --absolute Interepret lease times as expire time\n"); |
| 31 | exit(0); |
| 32 | } |
| 33 | #else |
| 34 | #define show_usage bb_show_usage |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | #ifdef IN_BUSYBOX |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 39 | int dumpleases_main(int argc, char *argv[]) |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 40 | #else |
| 41 | int main(int argc, char *argv[]) |
| 42 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 43 | { |
| 44 | FILE *fp; |
| 45 | int i, c, mode = REMAINING; |
| 46 | long expires; |
Russ Dill | 8b38451 | 2003-12-16 02:30:53 +0000 | [diff] [blame] | 47 | const char *file = LEASES_FILE; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 48 | struct dhcpOfferedAddr lease; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 49 | struct in_addr addr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 51 | static const struct option options[] = { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 52 | {"absolute", 0, 0, 'a'}, |
| 53 | {"remaining", 0, 0, 'r'}, |
| 54 | {"file", 1, 0, 'f'}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 55 | {0, 0, 0, 0} |
| 56 | }; |
| 57 | |
| 58 | while (1) { |
| 59 | int option_index = 0; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 60 | c = getopt_long(argc, argv, "arf:", options, &option_index); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 61 | if (c == -1) break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 62 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 63 | switch (c) { |
| 64 | case 'a': mode = ABSOLUTE; break; |
| 65 | case 'r': mode = REMAINING; break; |
| 66 | case 'f': |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 67 | file = optarg; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 68 | break; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 69 | default: |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 70 | show_usage(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 73 | |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 74 | fp = xfopen(file, "r"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 75 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 76 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 77 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
| 78 | while (fread(&lease, sizeof(lease), 1, fp)) { |
| 79 | |
| 80 | for (i = 0; i < 6; i++) { |
| 81 | printf("%02x", lease.chaddr[i]); |
| 82 | if (i != 5) printf(":"); |
| 83 | } |
| 84 | addr.s_addr = lease.yiaddr; |
| 85 | printf(" %-15s", inet_ntoa(addr)); |
| 86 | expires = ntohl(lease.expires); |
| 87 | printf(" "); |
| 88 | if (mode == REMAINING) { |
| 89 | if (!expires) printf("expired\n"); |
| 90 | else { |
| 91 | if (expires > 60*60*24) { |
| 92 | printf("%ld days, ", expires / (60*60*24)); |
| 93 | expires %= 60*60*24; |
| 94 | } |
| 95 | if (expires > 60*60) { |
| 96 | printf("%ld hours, ", expires / (60*60)); |
| 97 | expires %= 60*60; |
| 98 | } |
| 99 | if (expires > 60) { |
| 100 | printf("%ld minutes, ", expires / 60); |
| 101 | expires %= 60; |
| 102 | } |
| 103 | printf("%ld seconds\n", expires); |
| 104 | } |
| 105 | } else printf("%s", ctime(&expires)); |
| 106 | } |
| 107 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 108 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |