blob: 3e193903d081baf0356b9d65f31d90dce42c490c [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
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00006#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +00007#include "dhcpd.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00008
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00009int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000010int dumpleases_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000011{
Denis Vlasenko5e43d852007-03-29 19:29:32 +000012 int fd;
13 int i;
14 unsigned opt;
15 time_t expires;
Mike Frysinger7031f622006-05-08 03:20:50 +000016 const char *file = LEASES_FILE;
17 struct dhcpOfferedAddr lease;
18 struct in_addr addr;
19
Denis Vlasenko5e43d852007-03-29 19:29:32 +000020 enum {
21 OPT_a = 0x1, // -a
22 OPT_r = 0x2, // -r
23 OPT_f = 0x4, // -f
Mike Frysinger7031f622006-05-08 03:20:50 +000024 };
Denis Vlasenkoba2fb712007-04-01 09:39:03 +000025#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000026 static const char dumpleases_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000027 "absolute\0" No_argument "a"
28 "remaining\0" No_argument "r"
29 "file\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +000030 ;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +000031
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000032 applet_long_options = dumpleases_longopts;
Denis Vlasenko5e43d852007-03-29 19:29:32 +000033#endif
Denis Vlasenko09196572007-07-21 13:27:44 +000034 opt_complementary = "=0:a--r:r--a";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000035 opt = getopt32(argv, "arf:", &file);
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenko5e43d852007-03-29 19:29:32 +000037 fd = xopen(file, O_RDONLY);
Mike Frysinger7031f622006-05-08 03:20:50 +000038
Denis Vlasenko5e43d852007-03-29 19:29:32 +000039 printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in");
Mike Frysinger7031f622006-05-08 03:20:50 +000040 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
Denis Vlasenko5e43d852007-03-29 19:29:32 +000041 while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000042 printf(":%02x"+1, lease.chaddr[0]);
43 for (i = 1; i < 6; i++) {
44 printf(":%02x", lease.chaddr[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +000045 }
46 addr.s_addr = lease.yiaddr;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000047 printf(" %-15s ", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000048 expires = ntohl(lease.expires);
Denis Vlasenko5e43d852007-03-29 19:29:32 +000049 if (!(opt & OPT_a)) { /* no -a */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000050 if (!expires)
Denis Vlasenko5e43d852007-03-29 19:29:32 +000051 puts("expired");
Mike Frysinger7031f622006-05-08 03:20:50 +000052 else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +000053 unsigned d, h, m;
54 d = expires / (24*60*60); expires %= (24*60*60);
55 h = expires / (60*60); expires %= (60*60);
56 m = expires / 60; expires %= 60;
57 if (d) printf("%u days ", d);
58 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
Mike Frysinger7031f622006-05-08 03:20:50 +000059 }
Denis Vlasenko5e43d852007-03-29 19:29:32 +000060 } else /* -a */
61 fputs(ctime(&expires), stdout);
Mike Frysinger7031f622006-05-08 03:20:50 +000062 }
Denis Vlasenko5e43d852007-03-29 19:29:32 +000063 /* close(fd); */
Mike Frysinger7031f622006-05-08 03:20:50 +000064
65 return 0;
66}