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 */
30 extern char *malloc();
31 #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 /* FIXME: make these allocations dynamic up on need. */
66 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
67 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
68 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
70 /* Make sure string has zero lenght if there is no
71 * command line option */
72 config.output_file[0] = '\0';
74 /* File location defaults */
75 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
77 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
80 config.output_limit[0] = (int) (*tmp - '0');
82 config.output_limit[1] = (int) (*tmp - '0');
85 /* Make sure some output format is selected by default */
86 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
88 /* Default sort order is by IPs small to big */
89 config.reverse_order = false;
91 /* Parse command line options */
93 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
94 long_options, &option_index);
104 strncpy(config.dhcpdconf_file, optarg,
105 (size_t) MAXLEN - 1);
109 strncpy(config.dhcpdlease_file, optarg,
110 (size_t) MAXLEN - 1);
114 strncpy(config.output_format, optarg, (size_t) 1);
117 /* Output sorting option */
118 sorts = strlen(optarg);
120 warn("main: only first 5 sort orders will be used");
121 strncpy(config.sort, optarg, (size_t) 5);
124 strncpy(config.sort, optarg,
129 /* What ever sort in reverse order */
130 config.reverse_order = true;
134 strncpy(config.output_file, optarg,
135 (size_t) MAXLEN - 1);
138 /* Specification what will be printed */
139 for (i = 0; i < 2; i++) {
140 if (optarg[i] >= '0' && optarg[i] < '8') {
141 config.output_limit[i] =
142 (int) optarg[i] - '0';
145 "main: output mask `%s' is illegal",
153 return (EXIT_SUCCESS);
159 "Try `%s --help' for more information.",
160 program_invocation_short_name);
164 /* Output function selection */
165 switch (config.output_format[0]) {
167 output_analysis = output_txt;
170 output_analysis = output_html;
173 output_analysis = output_html;
177 output_analysis = output_xml;
180 output_analysis = output_xml;
183 output_analysis = output_csv;
186 /* output_analysis = output_snmp; */
187 output_analysis = output_txt;
190 errx(EXIT_FAILURE, "main: unknown ouput format `%c'",
191 config.output_format[0]);
196 parse_config(true, config.dhcpdconf_file, shared_net_names,
197 shared_net_names + strlen(shared_net_names) + 1,
200 /* FIXME: move to output.c and use FILE *outfile */
201 if ((config.output_format[0] == 'x')
202 || (config.output_format[0] == 'X')) {
203 printf("<dhcpstatus>\n");
209 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
211 mergesort_ranges(ranges, num_ranges, tmp_ranges);
213 if (config.reverse_order == true) {
214 flip_ranges(ranges, tmp_ranges);
218 /* After fopen in ouput ioctl does like /dev/null which
219 * cause ENOTTY, and clean_up will see that without this
220 * reset. At least linux does this, and possibly some
221 * other systems. There's a report from FreeBSD 8.0 which
222 * matches quite well with the symptom. */
226 /* FIXME: move to output.c and use FILE *outfile */
227 if ((config.output_format[0] == 'x')
228 || (config.output_format[0] == 'X')) {
229 printf("</dhcpstatus>\n");
233 return (EXIT_SUCCESS);
236 /* Global allocations, counter resets etc */
239 num_ranges = num_shared_networks = 0;
241 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
243 safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
245 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
247 /* First shared network entry is all networks */
248 strcpy(shared_net_names, "All networks");