blob: 01c7c27e804c21a0ba19e182579d2f35744e525b [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
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000010int dumpleases_main(int argc, char **argv);
11int dumpleases_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000012{
Denis Vlasenko5e43d852007-03-29 19:29:32 +000013 int fd;
14 int i;
15 unsigned opt;
16 time_t expires;
Mike Frysinger7031f622006-05-08 03:20:50 +000017 const char *file = LEASES_FILE;
18 struct dhcpOfferedAddr lease;
19 struct in_addr addr;
20
Denis Vlasenko5e43d852007-03-29 19:29:32 +000021 enum {
22 OPT_a = 0x1, // -a
23 OPT_r = 0x2, // -r
24 OPT_f = 0x4, // -f
Mike Frysinger7031f622006-05-08 03:20:50 +000025 };
Denis Vlasenkoba2fb712007-04-01 09:39:03 +000026#if ENABLE_GETOPT_LONG
Denis Vlasenko5e43d852007-03-29 19:29:32 +000027 static const struct option options[] = {
28 { "absolute", no_argument, 0, 'a' },
29 { "remaining", no_argument, 0, 'r' },
30 { "file", required_argument, 0, 'f' },
31 { NULL, 0, 0, 0 }
32 };
Denis Vlasenkoba2fb712007-04-01 09:39:03 +000033
Denis Vlasenko5e43d852007-03-29 19:29:32 +000034 applet_long_options = options;
35#endif
36 opt_complementary = "=0:?:a--r:r--a";
37 opt = getopt32(argc, argv, "arf:", &file);
Mike Frysinger7031f622006-05-08 03:20:50 +000038
Denis Vlasenko5e43d852007-03-29 19:29:32 +000039 fd = xopen(file, O_RDONLY);
Mike Frysinger7031f622006-05-08 03:20:50 +000040
Denis Vlasenko5e43d852007-03-29 19:29:32 +000041 printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in");
Mike Frysinger7031f622006-05-08 03:20:50 +000042 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
Denis Vlasenko5e43d852007-03-29 19:29:32 +000043 while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000044 printf(":%02x"+1, lease.chaddr[0]);
45 for (i = 1; i < 6; i++) {
46 printf(":%02x", lease.chaddr[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +000047 }
48 addr.s_addr = lease.yiaddr;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000049 printf(" %-15s ", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000050 expires = ntohl(lease.expires);
Denis Vlasenko5e43d852007-03-29 19:29:32 +000051 if (!(opt & OPT_a)) { /* no -a */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052 if (!expires)
Denis Vlasenko5e43d852007-03-29 19:29:32 +000053 puts("expired");
Mike Frysinger7031f622006-05-08 03:20:50 +000054 else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +000055 unsigned d, h, m;
56 d = expires / (24*60*60); expires %= (24*60*60);
57 h = expires / (60*60); expires %= (60*60);
58 m = expires / 60; expires %= 60;
59 if (d) printf("%u days ", d);
60 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
Mike Frysinger7031f622006-05-08 03:20:50 +000061 }
Denis Vlasenko5e43d852007-03-29 19:29:32 +000062 } else /* -a */
63 fputs(ctime(&expires), stdout);
Mike Frysinger7031f622006-05-08 03:20:50 +000064 }
Denis Vlasenko5e43d852007-03-29 19:29:32 +000065 /* close(fd); */
Mike Frysinger7031f622006-05-08 03:20:50 +000066
67 return 0;
68}