blob: 4c6107cf7d7ecebeba31b71b13b488759a6838a6 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#include <fcntl.h>
2#include <string.h>
3#include <stdlib.h>
Russ Dill61fb4892002-10-14 21:41:28 +00004#include <sys/wait.h>
Russ Dill61fb4892002-10-14 21:41:28 +00005#include <arpa/inet.h>
6#include <netdb.h>
7#include <netinet/in.h>
8#include <stdio.h>
Russ Dill61fb4892002-10-14 21:41:28 +00009#include <sys/socket.h>
10#include <unistd.h>
Russ Dill61fb4892002-10-14 21:41:28 +000011#include <getopt.h>
12#include <time.h>
13
Glenn L McGrath24833432003-06-10 17:22:49 +000014#include "leases.h"
15#include "busybox.h"
Russ Dill61fb4892002-10-14 21:41:28 +000016
17#define REMAINING 0
18#define ABSOLUTE 1
19
Russ Dill61fb4892002-10-14 21:41:28 +000020int dumpleases_main(int argc, char *argv[])
Russ Dill61fb4892002-10-14 21:41:28 +000021{
22 FILE *fp;
23 int i, c, mode = REMAINING;
24 long expires;
Glenn L McGrath24833432003-06-10 17:22:49 +000025 const char *file = leases_file;
26 struct dhcpOfferedAddr lease;
Russ Dill61fb4892002-10-14 21:41:28 +000027 struct in_addr addr;
28
Glenn L McGrath24833432003-06-10 17:22:49 +000029 static const struct option options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000030 {"absolute", 0, 0, 'a'},
31 {"remaining", 0, 0, 'r'},
32 {"file", 1, 0, 'f'},
Russ Dill61fb4892002-10-14 21:41:28 +000033 {0, 0, 0, 0}
34 };
35
36 while (1) {
37 int option_index = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +000038 c = getopt_long(argc, argv, "arf:", options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +000039 if (c == -1) break;
40
41 switch (c) {
42 case 'a': mode = ABSOLUTE; break;
43 case 'r': mode = REMAINING; break;
44 case 'f':
Glenn L McGrath24833432003-06-10 17:22:49 +000045 file = optarg;
Russ Dill61fb4892002-10-14 21:41:28 +000046 break;
Glenn L McGrath24833432003-06-10 17:22:49 +000047 default:
48 bb_show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +000049 }
50 }
51
Glenn L McGrath24833432003-06-10 17:22:49 +000052 fp = bb_xfopen(file, "r");
Russ Dill61fb4892002-10-14 21:41:28 +000053
54 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
55 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
56 while (fread(&lease, sizeof(lease), 1, fp)) {
57
58 for (i = 0; i < 6; i++) {
59 printf("%02x", lease.chaddr[i]);
60 if (i != 5) printf(":");
61 }
62 addr.s_addr = lease.yiaddr;
63 printf(" %-15s", inet_ntoa(addr));
64 expires = ntohl(lease.expires);
65 printf(" ");
66 if (mode == REMAINING) {
67 if (!expires) printf("expired\n");
68 else {
69 if (expires > 60*60*24) {
70 printf("%ld days, ", expires / (60*60*24));
71 expires %= 60*60*24;
72 }
73 if (expires > 60*60) {
74 printf("%ld hours, ", expires / (60*60));
75 expires %= 60*60;
76 }
77 if (expires > 60) {
78 printf("%ld minutes, ", expires / 60);
79 expires %= 60;
80 }
81 printf("%ld seconds\n", expires);
82 }
83 } else printf("%s", ctime(&expires));
84 }
85 fclose(fp);
86
87 return 0;
88}