blob: ca5c6059e4aa85cba000af680635e07e423cd1d0 [file] [log] [blame]
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00001/* vi: set sw=4 ts=4: */
2/*
3 Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
4 */
Russ Dill61fb4892002-10-14 21:41:28 +00005#include <fcntl.h>
6#include <string.h>
7#include <stdlib.h>
Russ Dill4b77aca2003-12-16 02:28:20 +00008#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +00009#include <sys/wait.h>
Russ Dill61fb4892002-10-14 21:41:28 +000010#include <arpa/inet.h>
11#include <netdb.h>
12#include <netinet/in.h>
13#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +000014#include <sys/socket.h>
15#include <unistd.h>
Russ Dill61fb4892002-10-14 21:41:28 +000016#include <getopt.h>
17#include <time.h>
18
Russ Dill4040d3d2003-12-16 02:34:28 +000019#include "dhcpd.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000020#include "leases.h"
Russ Dill4b77aca2003-12-16 02:28:20 +000021#include "libbb_udhcp.h"
Russ Dill61fb4892002-10-14 21:41:28 +000022
23#define REMAINING 0
24#define ABSOLUTE 1
25
Russ Dill4b77aca2003-12-16 02:28:20 +000026
27#ifndef IN_BUSYBOX
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000028static void ATTRIBUTE_NORETURN show_usage(void)
Russ Dill4b77aca2003-12-16 02:28:20 +000029{
30 printf(
31"Usage: dumpleases -f <file> -[r|a]\n\n"
32" -f, --file=FILENAME Leases file to load\n"
33" -r, --remaining Interepret lease times as time remaing\n"
34" -a, --absolute Interepret lease times as expire time\n");
35 exit(0);
36}
37#else
38#define show_usage bb_show_usage
39#endif
40
41
42#ifdef IN_BUSYBOX
Russ Dill61fb4892002-10-14 21:41:28 +000043int dumpleases_main(int argc, char *argv[])
Russ Dill4b77aca2003-12-16 02:28:20 +000044#else
45int main(int argc, char *argv[])
46#endif
Russ Dill61fb4892002-10-14 21:41:28 +000047{
48 FILE *fp;
49 int i, c, mode = REMAINING;
50 long expires;
Russ Dill8b384512003-12-16 02:30:53 +000051 const char *file = LEASES_FILE;
Glenn L McGrath24833432003-06-10 17:22:49 +000052 struct dhcpOfferedAddr lease;
Russ Dill61fb4892002-10-14 21:41:28 +000053 struct in_addr addr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Glenn L McGrath24833432003-06-10 17:22:49 +000055 static const struct option options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000056 {"absolute", 0, 0, 'a'},
57 {"remaining", 0, 0, 'r'},
58 {"file", 1, 0, 'f'},
Russ Dill61fb4892002-10-14 21:41:28 +000059 {0, 0, 0, 0}
60 };
61
62 while (1) {
63 int option_index = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +000064 c = getopt_long(argc, argv, "arf:", options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +000065 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066
Russ Dill61fb4892002-10-14 21:41:28 +000067 switch (c) {
68 case 'a': mode = ABSOLUTE; break;
69 case 'r': mode = REMAINING; break;
70 case 'f':
Russ Dill4b77aca2003-12-16 02:28:20 +000071 file = optarg;
Russ Dill61fb4892002-10-14 21:41:28 +000072 break;
Glenn L McGrath24833432003-06-10 17:22:49 +000073 default:
Russ Dill4b77aca2003-12-16 02:28:20 +000074 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +000075 }
76 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Russ Dill4b77aca2003-12-16 02:28:20 +000078 fp = xfopen(file, "r");
Russ Dill61fb4892002-10-14 21:41:28 +000079
Eric Andersenc7bda1c2004-03-15 08:29:22 +000080 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
Russ Dill61fb4892002-10-14 21:41:28 +000081 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
82 while (fread(&lease, sizeof(lease), 1, fp)) {
83
84 for (i = 0; i < 6; i++) {
85 printf("%02x", lease.chaddr[i]);
86 if (i != 5) printf(":");
87 }
88 addr.s_addr = lease.yiaddr;
89 printf(" %-15s", inet_ntoa(addr));
90 expires = ntohl(lease.expires);
91 printf(" ");
92 if (mode == REMAINING) {
93 if (!expires) printf("expired\n");
94 else {
95 if (expires > 60*60*24) {
96 printf("%ld days, ", expires / (60*60*24));
97 expires %= 60*60*24;
98 }
99 if (expires > 60*60) {
100 printf("%ld hours, ", expires / (60*60));
101 expires %= 60*60;
102 }
103 if (expires > 60) {
104 printf("%ld minutes, ", expires / 60);
105 expires %= 60;
106 }
107 printf("%ld seconds\n", expires);
108 }
109 } else printf("%s", ctime(&expires));
110 }
111 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000112
Russ Dill61fb4892002-10-14 21:41:28 +0000113 return 0;
114}