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