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 | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 4 | #include <sys/wait.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 5 | #include <arpa/inet.h> |
| 6 | #include <netdb.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <stdio.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <unistd.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 11 | #include <getopt.h> |
| 12 | #include <time.h> |
| 13 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 14 | #include "leases.h" |
| 15 | #include "busybox.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 16 | |
| 17 | #define REMAINING 0 |
| 18 | #define ABSOLUTE 1 |
| 19 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 20 | int dumpleases_main(int argc, char *argv[]) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 21 | { |
| 22 | FILE *fp; |
| 23 | int i, c, mode = REMAINING; |
| 24 | long expires; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 25 | const char *file = leases_file; |
| 26 | struct dhcpOfferedAddr lease; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 27 | struct in_addr addr; |
| 28 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 29 | static const struct option options[] = { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 30 | {"absolute", 0, 0, 'a'}, |
| 31 | {"remaining", 0, 0, 'r'}, |
| 32 | {"file", 1, 0, 'f'}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 33 | {0, 0, 0, 0} |
| 34 | }; |
| 35 | |
| 36 | while (1) { |
| 37 | int option_index = 0; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 38 | c = getopt_long(argc, argv, "arf:", options, &option_index); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 39 | if (c == -1) break; |
| 40 | |
| 41 | switch (c) { |
| 42 | case 'a': mode = ABSOLUTE; break; |
| 43 | case 'r': mode = REMAINING; break; |
| 44 | case 'f': |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 45 | file = optarg; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 46 | break; |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 47 | default: |
| 48 | bb_show_usage(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 52 | fp = bb_xfopen(file, "r"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 53 | |
| 54 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); |
| 55 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
| 56 | while (fread(&lease, sizeof(lease), 1, fp)) { |
| 57 | |
| 58 | for (i = 0; i < 6; i++) { |
| 59 | printf("%02x", lease.chaddr[i]); |
| 60 | if (i != 5) printf(":"); |
| 61 | } |
| 62 | addr.s_addr = lease.yiaddr; |
| 63 | printf(" %-15s", inet_ntoa(addr)); |
| 64 | expires = ntohl(lease.expires); |
| 65 | printf(" "); |
| 66 | if (mode == REMAINING) { |
| 67 | if (!expires) printf("expired\n"); |
| 68 | else { |
| 69 | if (expires > 60*60*24) { |
| 70 | printf("%ld days, ", expires / (60*60*24)); |
| 71 | expires %= 60*60*24; |
| 72 | } |
| 73 | if (expires > 60*60) { |
| 74 | printf("%ld hours, ", expires / (60*60)); |
| 75 | expires %= 60*60; |
| 76 | } |
| 77 | if (expires > 60) { |
| 78 | printf("%ld minutes, ", expires / 60); |
| 79 | expires %= 60; |
| 80 | } |
| 81 | printf("%ld seconds\n", expires); |
| 82 | } |
| 83 | } else printf("%s", ctime(&expires)); |
| 84 | } |
| 85 | fclose(fp); |
| 86 | |
| 87 | return 0; |
| 88 | } |