blob: 4422d309925e9697998717fcb1d07a57bb75e1e0 [file] [log] [blame]
Mike Frysinger7031f622006-05-08 03:20:50 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
4 */
Mike Frysinger7031f622006-05-08 03:20:50 +00005#include <getopt.h>
Mike Frysinger7031f622006-05-08 03:20:50 +00006
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00007#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +00008#include "dhcpd.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00009
Mike Frysinger7031f622006-05-08 03:20:50 +000010
11#define REMAINING 0
12#define ABSOLUTE 1
13
Mike Frysinger7031f622006-05-08 03:20:50 +000014int dumpleases_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +000015{
16 FILE *fp;
17 int i, c, mode = REMAINING;
18 long expires;
19 const char *file = LEASES_FILE;
20 struct dhcpOfferedAddr lease;
21 struct in_addr addr;
22
23 static const struct option options[] = {
24 {"absolute", 0, 0, 'a'},
25 {"remaining", 0, 0, 'r'},
26 {"file", 1, 0, 'f'},
27 {0, 0, 0, 0}
28 };
29
30 while (1) {
31 int option_index = 0;
32 c = getopt_long(argc, argv, "arf:", options, &option_index);
33 if (c == -1) break;
34
35 switch (c) {
36 case 'a': mode = ABSOLUTE; break;
37 case 'r': mode = REMAINING; break;
38 case 'f':
39 file = optarg;
40 break;
41 default:
Bernhard Reutner-Fischer5bd2a4b2006-08-18 17:22:23 +000042 bb_show_usage();
Mike Frysinger7031f622006-05-08 03:20:50 +000043 }
44 }
45
46 fp = xfopen(file, "r");
47
48 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
49 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
50 while (fread(&lease, sizeof(lease), 1, fp)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000051 printf(":%02x"+1, lease.chaddr[0]);
52 for (i = 1; i < 6; i++) {
53 printf(":%02x", lease.chaddr[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +000054 }
55 addr.s_addr = lease.yiaddr;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000056 printf(" %-15s ", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000057 expires = ntohl(lease.expires);
Mike Frysinger7031f622006-05-08 03:20:50 +000058 if (mode == REMAINING) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000059 if (!expires)
60 printf("expired\n");
Mike Frysinger7031f622006-05-08 03:20:50 +000061 else {
62 if (expires > 60*60*24) {
63 printf("%ld days, ", expires / (60*60*24));
64 expires %= 60*60*24;
65 }
66 if (expires > 60*60) {
67 printf("%ld hours, ", expires / (60*60));
68 expires %= 60*60;
69 }
70 if (expires > 60) {
71 printf("%ld minutes, ", expires / 60);
72 expires %= 60;
73 }
74 printf("%ld seconds\n", expires);
75 }
76 } else printf("%s", ctime(&expires));
77 }
78 fclose(fp);
79
80 return 0;
81}