blob: 2b19d976829353a1c95714d5c692794ca84f7f60 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#include <fcntl.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <sys/wait.h>
6#include <sys/stat.h>
7#include <arpa/inet.h>
8#include <netdb.h>
9#include <netinet/in.h>
10#include <stdio.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <unistd.h>
14#include <syslog.h>
15#include <signal.h>
16#include <errno.h>
17#include <getopt.h>
18#include <time.h>
19
20#include "libbb_udhcp.h"
21
22#define REMAINING 0
23#define ABSOLUTE 1
24
25struct lease_t {
26 unsigned char chaddr[16];
27 u_int32_t yiaddr;
28 u_int32_t expires;
29};
30
31#ifdef BB_VER
32int dumpleases_main(int argc, char *argv[])
33#else
34int main(int argc, char *argv[])
35#endif
36{
37 FILE *fp;
38 int i, c, mode = REMAINING;
39 long expires;
40 char file[255] = "/var/lib/misc/udhcpd.leases";
41 struct lease_t lease;
42 struct in_addr addr;
43
44 static struct option options[] = {
45 {"absolute", 0, 0, 'a'},
46 {"remaining", 0, 0, 'r'},
47 {"file", 1, 0, 'f'},
48 {"help", 0, 0, 'h'},
49 {0, 0, 0, 0}
50 };
51
52 while (1) {
53 int option_index = 0;
54 c = getopt_long(argc, argv, "arf:h", options, &option_index);
55 if (c == -1) break;
56
57 switch (c) {
58 case 'a': mode = ABSOLUTE; break;
59 case 'r': mode = REMAINING; break;
60 case 'f':
61 strncpy(file, optarg, 255);
62 file[254] = '\0';
63 break;
64 case 'h':
65 printf("Usage: dumpleases -f <file> -[r|a]\n\n");
66 printf(" -f, --file=FILENAME Leases file to load\n");
67 printf(" -r, --remaining Interepret lease times as time remaing\n");
68 printf(" -a, --absolute Interepret lease times as expire time\n");
69 break;
70 }
71 }
72
73 if (!(fp = fopen(file, "r"))) {
74 perror("could not open input file");
75 return 0;
76 }
77
78 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
79 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
80 while (fread(&lease, sizeof(lease), 1, fp)) {
81
82 for (i = 0; i < 6; i++) {
83 printf("%02x", lease.chaddr[i]);
84 if (i != 5) printf(":");
85 }
86 addr.s_addr = lease.yiaddr;
87 printf(" %-15s", inet_ntoa(addr));
88 expires = ntohl(lease.expires);
89 printf(" ");
90 if (mode == REMAINING) {
91 if (!expires) printf("expired\n");
92 else {
93 if (expires > 60*60*24) {
94 printf("%ld days, ", expires / (60*60*24));
95 expires %= 60*60*24;
96 }
97 if (expires > 60*60) {
98 printf("%ld hours, ", expires / (60*60));
99 expires %= 60*60;
100 }
101 if (expires > 60) {
102 printf("%ld minutes, ", expires / 60);
103 expires %= 60;
104 }
105 printf("%ld seconds\n", expires);
106 }
107 } else printf("%s", ctime(&expires));
108 }
109 fclose(fp);
110
111 return 0;
112}