blob: a0e81bb1306038bed003dfc85f9c93acfe8ffad2 [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{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000016 int fp;
Mike Frysinger7031f622006-05-08 03:20:50 +000017 int i, c, mode = REMAINING;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000018 unsigned long expires;
Mike Frysinger7031f622006-05-08 03:20:50 +000019 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
Denis Vlasenko61126ab2006-11-18 22:03:26 +000046 fp = xopen(file, O_RDONLY);
Mike Frysinger7031f622006-05-08 03:20:50 +000047
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" */
Denis Vlasenko61126ab2006-11-18 22:03:26 +000050 while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) {
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 {
Denis Vlasenko61126ab2006-11-18 22:03:26 +000062 unsigned d, h, m;
63 d = expires / (24*60*60); expires %= (24*60*60);
64 h = expires / (60*60); expires %= (60*60);
65 m = expires / 60; expires %= 60;
66 if (d) printf("%u days ", d);
67 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
Mike Frysinger7031f622006-05-08 03:20:50 +000068 }
Denis Vlasenko61126ab2006-11-18 22:03:26 +000069 } else fputs(ctime(&expires), stdout);
Mike Frysinger7031f622006-05-08 03:20:50 +000070 }
Denis Vlasenko61126ab2006-11-18 22:03:26 +000071 /* close(fp); */
Mike Frysinger7031f622006-05-08 03:20:50 +000072
73 return 0;
74}