2 ** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #else /* Not STDC_HEADERS */
31 extern char *malloc();
32 #endif /* STDC_HEADERS */
41 #include "dhcpd-pools.h"
44 int main(int argc, char **argv)
49 struct range_t *tmp_ranges;
51 /* Options for getopt_long */
52 static struct option const long_options[] = {
53 {"config", required_argument, 0, (int) 'c'},
54 {"leases", required_argument, 0, (int) 'l'},
55 {"format", required_argument, 0, (int) 'f'},
56 {"sort", required_argument, 0, (int) 's'},
57 {"reverse", no_argument, 0, (int) 'r'},
58 {"output", required_argument, 0, (int) 'o'},
59 {"limit", required_argument, 0, (int) 'L'},
60 {"version", no_argument, 0, (int) 'v'},
61 {"help", no_argument, 0, (int) 'h'},
65 program_name = argv[0];
68 /* TODO: make either dynamic or find out max path lenght that auto config
70 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
71 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
72 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
74 /* Make sure string has zero lenght if there is no command line
76 config.output_file[0] = '\0';
78 /* File location defaults */
79 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
81 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
84 config.output_limit[0] = (int) (*tmp - '0');
86 config.output_limit[1] = (int) (*tmp - '0');
89 /* Make sure some output format is selected by default */
90 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
92 /* Default sort order is by IPs small to big */
93 config.reverse_order = false;
95 /* Parse command line options */
97 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
98 long_options, &option_index);
108 if (optarg != NULL) {
109 strncpy(config.dhcpdconf_file, optarg,
110 (size_t) MAXLEN - 1);
113 ("main: for argument configuration file parameter not set");
119 if (optarg != NULL) {
120 strncpy(config.dhcpdlease_file, optarg,
121 (size_t) MAXLEN - 1);
124 ("main: for argument lease file parameter not set");
130 if (optarg != NULL) {
131 strncpy(config.output_format, optarg,
135 ("main: for argument output format parameter not set");
140 /* Output sorting option */
141 if (optarg != NULL) {
142 sorts = strlen(optarg);
145 ("main: only 5 first sort orders will be used");
146 strncpy(config.sort, optarg,
150 strncpy(config.sort, optarg,
155 ("main: for argument sort order parameter not set");
160 /* What ever sort in reverse order */
161 config.reverse_order = true;
165 if (optarg != NULL) {
166 strncpy(config.output_file, optarg,
167 (size_t) MAXLEN - 1);
170 ("main: for argument output file parameter not set");
175 /* Specification what will be printed */
176 if (optarg != NULL) {
177 if (optarg[0] >= '0' && optarg[0] < '8') {
178 config.output_limit[0] =
179 (int) optarg[0] - '0';
182 ("main: output mask %s illegal",
186 if (optarg[1] >= '0' && optarg[1] < '8') {
187 config.output_limit[1] =
188 (int) optarg[1] - '0';
191 ("main: output mask %s illegal",
197 ("main: for argument output mask parameter not set");
213 /* Output function selection */
214 switch (config.output_format[0]) {
216 output_analysis = output_txt;
219 output_analysis = output_html;
222 output_analysis = output_html;
226 output_analysis = output_xml;
229 output_analysis = output_xml;
232 output_analysis = output_csv;
235 /* output_analysis = output_snmp; */
236 output_analysis = output_txt;
239 eprintf("main: unknown ouput format");
245 parse_config(true, config.dhcpdconf_file, shared_net_names,
246 shared_net_names + strlen(shared_net_names) + 1,
249 if ((config.output_format[0] == 'x')
250 || (config.output_format[0] == 'X')) {
251 printf("<dhcpstatus>\n");
257 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
259 mergesort_ranges(ranges, num_ranges, tmp_ranges);
261 if (config.reverse_order == true) {
262 flip_ranges(ranges, tmp_ranges);
266 /* After fopen in ouput ioctl does like /dev/null which
267 * cause ENOTTY, and clean_up will see that without this
268 * reset. At least linux does this, and possibly some
269 * other systems. There's a report from FreeBSD 8.0 which
270 * matches quite well with the symptom. */
274 if ((config.output_format[0] == 'x')
275 || (config.output_format[0] == 'X')) {
276 printf("</dhcpstatus>\n");
282 /* Global allocations, counter resets etc */
285 num_ranges = num_shared_networks = 0;
287 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
289 safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
291 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
293 /* First shared network entry is all networks */
294 strcpy(shared_net_names, "All networks");