blob: 4c1ee131f01ffb9910705d3a7f4822e38996e6f5 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#include <fcntl.h>
2#include <string.h>
3#include <stdlib.h>
Russ Dill4b77aca2003-12-16 02:28:20 +00004#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +00005#include <sys/wait.h>
Russ Dill61fb4892002-10-14 21:41:28 +00006#include <arpa/inet.h>
7#include <netdb.h>
8#include <netinet/in.h>
9#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +000010#include <sys/socket.h>
11#include <unistd.h>
Russ Dill61fb4892002-10-14 21:41:28 +000012#include <getopt.h>
13#include <time.h>
14
Glenn L McGrath24833432003-06-10 17:22:49 +000015#include "leases.h"
Russ Dill4b77aca2003-12-16 02:28:20 +000016#include "libbb_udhcp.h"
Russ Dill61fb4892002-10-14 21:41:28 +000017
18#define REMAINING 0
19#define ABSOLUTE 1
20
Russ Dill4b77aca2003-12-16 02:28:20 +000021
22#ifndef IN_BUSYBOX
23static 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 Dill61fb4892002-10-14 21:41:28 +000038int dumpleases_main(int argc, char *argv[])
Russ Dill4b77aca2003-12-16 02:28:20 +000039#else
40int main(int argc, char *argv[])
41#endif
Russ Dill61fb4892002-10-14 21:41:28 +000042{
43 FILE *fp;
44 int i, c, mode = REMAINING;
45 long expires;
Russ Dill8b384512003-12-16 02:30:53 +000046 const char *file = LEASES_FILE;
Glenn L McGrath24833432003-06-10 17:22:49 +000047 struct dhcpOfferedAddr lease;
Russ Dill61fb4892002-10-14 21:41:28 +000048 struct in_addr addr;
49
Glenn L McGrath24833432003-06-10 17:22:49 +000050 static const struct option options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000051 {"absolute", 0, 0, 'a'},
52 {"remaining", 0, 0, 'r'},
53 {"file", 1, 0, 'f'},
Russ Dill61fb4892002-10-14 21:41:28 +000054 {0, 0, 0, 0}
55 };
56
57 while (1) {
58 int option_index = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +000059 c = getopt_long(argc, argv, "arf:", options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +000060 if (c == -1) break;
61
62 switch (c) {
63 case 'a': mode = ABSOLUTE; break;
64 case 'r': mode = REMAINING; break;
65 case 'f':
Russ Dill4b77aca2003-12-16 02:28:20 +000066 file = optarg;
Russ Dill61fb4892002-10-14 21:41:28 +000067 break;
Glenn L McGrath24833432003-06-10 17:22:49 +000068 default:
Russ Dill4b77aca2003-12-16 02:28:20 +000069 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +000070 }
71 }
72
Russ Dill4b77aca2003-12-16 02:28:20 +000073 fp = xfopen(file, "r");
Russ Dill61fb4892002-10-14 21:41:28 +000074
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}