blob: a9036dfbdf5930f7d02e498d35154dd3e77b8170 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#include <fcntl.h>
2#include <string.h>
3#include <stdlib.h>
Russ Dill4b77aca2003-12-16 02:28:20 +00004#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +00005#include <sys/wait.h>
Russ Dill61fb4892002-10-14 21:41:28 +00006#include <arpa/inet.h>
7#include <netdb.h>
8#include <netinet/in.h>
9#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +000010#include <sys/socket.h>
11#include <unistd.h>
Russ Dill61fb4892002-10-14 21:41:28 +000012#include <getopt.h>
13#include <time.h>
14
Russ Dill4040d3d2003-12-16 02:34:28 +000015#include "dhcpd.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000016#include "leases.h"
Russ Dill4b77aca2003-12-16 02:28:20 +000017#include "libbb_udhcp.h"
Russ Dill61fb4892002-10-14 21:41:28 +000018
19#define REMAINING 0
20#define ABSOLUTE 1
21
Russ Dill4b77aca2003-12-16 02:28:20 +000022
23#ifndef IN_BUSYBOX
24static void __attribute__ ((noreturn)) show_usage(void)
25{
26 printf(
27"Usage: dumpleases -f <file> -[r|a]\n\n"
28" -f, --file=FILENAME Leases file to load\n"
29" -r, --remaining Interepret lease times as time remaing\n"
30" -a, --absolute Interepret lease times as expire time\n");
31 exit(0);
32}
33#else
34#define show_usage bb_show_usage
35#endif
36
37
38#ifdef IN_BUSYBOX
Russ Dill61fb4892002-10-14 21:41:28 +000039int dumpleases_main(int argc, char *argv[])
Russ Dill4b77aca2003-12-16 02:28:20 +000040#else
41int main(int argc, char *argv[])
42#endif
Russ Dill61fb4892002-10-14 21:41:28 +000043{
44 FILE *fp;
45 int i, c, mode = REMAINING;
46 long expires;
Russ Dill8b384512003-12-16 02:30:53 +000047 const char *file = LEASES_FILE;
Glenn L McGrath24833432003-06-10 17:22:49 +000048 struct dhcpOfferedAddr lease;
Russ Dill61fb4892002-10-14 21:41:28 +000049 struct in_addr addr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050
Glenn L McGrath24833432003-06-10 17:22:49 +000051 static const struct option options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000052 {"absolute", 0, 0, 'a'},
53 {"remaining", 0, 0, 'r'},
54 {"file", 1, 0, 'f'},
Russ Dill61fb4892002-10-14 21:41:28 +000055 {0, 0, 0, 0}
56 };
57
58 while (1) {
59 int option_index = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +000060 c = getopt_long(argc, argv, "arf:", options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +000061 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000062
Russ Dill61fb4892002-10-14 21:41:28 +000063 switch (c) {
64 case 'a': mode = ABSOLUTE; break;
65 case 'r': mode = REMAINING; break;
66 case 'f':
Russ Dill4b77aca2003-12-16 02:28:20 +000067 file = optarg;
Russ Dill61fb4892002-10-14 21:41:28 +000068 break;
Glenn L McGrath24833432003-06-10 17:22:49 +000069 default:
Russ Dill4b77aca2003-12-16 02:28:20 +000070 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +000071 }
72 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073
Russ Dill4b77aca2003-12-16 02:28:20 +000074 fp = xfopen(file, "r");
Russ Dill61fb4892002-10-14 21:41:28 +000075
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
Russ Dill61fb4892002-10-14 21:41:28 +000077 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
78 while (fread(&lease, sizeof(lease), 1, fp)) {
79
80 for (i = 0; i < 6; i++) {
81 printf("%02x", lease.chaddr[i]);
82 if (i != 5) printf(":");
83 }
84 addr.s_addr = lease.yiaddr;
85 printf(" %-15s", inet_ntoa(addr));
86 expires = ntohl(lease.expires);
87 printf(" ");
88 if (mode == REMAINING) {
89 if (!expires) printf("expired\n");
90 else {
91 if (expires > 60*60*24) {
92 printf("%ld days, ", expires / (60*60*24));
93 expires %= 60*60*24;
94 }
95 if (expires > 60*60) {
96 printf("%ld hours, ", expires / (60*60));
97 expires %= 60*60;
98 }
99 if (expires > 60) {
100 printf("%ld minutes, ", expires / 60);
101 expires %= 60;
102 }
103 printf("%ld seconds\n", expires);
104 }
105 } else printf("%s", ctime(&expires));
106 }
107 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000108
Russ Dill61fb4892002-10-14 21:41:28 +0000109 return 0;
110}