Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 4 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 5 | #include <getopt.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 6 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 7 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | #include "dhcpd.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 9 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 10 | int dumpleases_main(int argc, char **argv); |
| 11 | int dumpleases_main(int argc, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | { |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 13 | int fd; |
| 14 | int i; |
| 15 | unsigned opt; |
| 16 | time_t expires; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 17 | const char *file = LEASES_FILE; |
| 18 | struct dhcpOfferedAddr lease; |
| 19 | struct in_addr addr; |
| 20 | |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 21 | enum { |
| 22 | OPT_a = 0x1, // -a |
| 23 | OPT_r = 0x2, // -r |
| 24 | OPT_f = 0x4, // -f |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | }; |
Denis Vlasenko | ba2fb71 | 2007-04-01 09:39:03 +0000 | [diff] [blame] | 26 | #if ENABLE_GETOPT_LONG |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 27 | static const char dumpleases_longopts[] = |
| 28 | "absolute\0" No_argument "a" |
| 29 | "remaining\0" No_argument "r" |
| 30 | "file\0" Required_argument "f" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 31 | ; |
Denis Vlasenko | ba2fb71 | 2007-04-01 09:39:03 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 33 | applet_long_options = dumpleases_longopts; |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 34 | #endif |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 35 | opt_complementary = "=0:a--r:r--a"; |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 36 | opt = getopt32(argc, argv, "arf:", &file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 38 | fd = xopen(file, O_RDONLY); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 40 | printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 42 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 43 | printf(":%02x"+1, lease.chaddr[0]); |
| 44 | for (i = 1; i < 6; i++) { |
| 45 | printf(":%02x", lease.chaddr[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 46 | } |
| 47 | addr.s_addr = lease.yiaddr; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 48 | printf(" %-15s ", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 49 | expires = ntohl(lease.expires); |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 50 | if (!(opt & OPT_a)) { /* no -a */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 51 | if (!expires) |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 52 | puts("expired"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 53 | else { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 54 | unsigned d, h, m; |
| 55 | d = expires / (24*60*60); expires %= (24*60*60); |
| 56 | h = expires / (60*60); expires %= (60*60); |
| 57 | m = expires / 60; expires %= 60; |
| 58 | if (d) printf("%u days ", d); |
| 59 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 60 | } |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 61 | } else /* -a */ |
| 62 | fputs(ctime(&expires), stdout); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 63 | } |
Denis Vlasenko | 5e43d85 | 2007-03-29 19:29:32 +0000 | [diff] [blame] | 64 | /* close(fd); */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |