ce73c474c5c72fc096dd39742761f27a21ed4d56
[oweals/busybox.git] / networking / udhcp / dumpleases.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
4  */
5 #include <getopt.h>
6
7 #include "common.h"
8 #include "dhcpd.h"
9
10
11 #define REMAINING 0
12 #define ABSOLUTE 1
13
14 int dumpleases_main(int argc, char *argv[]);
15 int dumpleases_main(int argc, char *argv[])
16 {
17         int fp;
18         int i, c, mode = REMAINING;
19         unsigned long expires;
20         const char *file = LEASES_FILE;
21         struct dhcpOfferedAddr lease;
22         struct in_addr addr;
23
24         static const struct option options[] = {
25                 {"absolute", 0, 0, 'a'},
26                 {"remaining", 0, 0, 'r'},
27                 {"file", 1, 0, 'f'},
28                 {0, 0, 0, 0}
29         };
30
31         while (1) {
32                 int option_index = 0;
33                 c = getopt_long(argc, argv, "arf:", options, &option_index);
34                 if (c == -1) break;
35
36                 switch (c) {
37                 case 'a': mode = ABSOLUTE; break;
38                 case 'r': mode = REMAINING; break;
39                 case 'f':
40                         file = optarg;
41                         break;
42                 default:
43                         bb_show_usage();
44                 }
45         }
46
47         fp = xopen(file, O_RDONLY);
48
49         printf("Mac Address       IP-Address      Expires %s\n", mode == REMAINING ? "in" : "at");
50         /*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
51         while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) {
52                 printf(":%02x"+1, lease.chaddr[0]);
53                 for (i = 1; i < 6; i++) {
54                         printf(":%02x", lease.chaddr[i]);
55                 }
56                 addr.s_addr = lease.yiaddr;
57                 printf(" %-15s ", inet_ntoa(addr));
58                 expires = ntohl(lease.expires);
59                 if (mode == REMAINING) {
60                         if (!expires)
61                                 printf("expired\n");
62                         else {
63                                 unsigned d, h, m;
64                                 d = expires / (24*60*60); expires %= (24*60*60);
65                                 h = expires / (60*60); expires %= (60*60);
66                                 m = expires / 60; expires %= 60;
67                                 if (d) printf("%u days ", d);
68                                 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
69                         }
70                 } else fputs(ctime(&expires), stdout);
71         }
72         /* close(fp); */
73
74         return 0;
75 }