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 */
40 #include "dhcpd-pools.h"
43 int main(int argc, char **argv)
48 struct range_t *tmp_ranges;
50 /* Options for getopt_long */
51 static struct option const long_options[] = {
52 {"config", required_argument, 0, (int) 'c'},
53 {"leases", required_argument, 0, (int) 'l'},
54 {"format", required_argument, 0, (int) 'f'},
55 {"sort", required_argument, 0, (int) 's'},
56 {"reverse", no_argument, 0, (int) 'r'},
57 {"output", required_argument, 0, (int) 'o'},
58 {"limit", required_argument, 0, (int) 'L'},
59 {"version", no_argument, 0, (int) 'v'},
60 {"help", no_argument, 0, (int) 'h'},
64 program_name = argv[0];
67 /* TODO: make either dynamic or find out max path lenght that auto config
69 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
70 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
71 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
73 /* Make sure string has zero lenght if there is no command line
75 config.output_file[0] = '\0';
77 /* File location defaults */
78 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
80 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
83 config.output_limit[0] = (int) (*tmp - '0');
85 config.output_limit[1] = (int) (*tmp - '0');
88 /* Make sure some output format is selected by default */
89 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
91 /* Default sort order is by IPs small to big */
92 config.reverse_order = false;
94 /* Parse command line options */
96 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
97 long_options, &option_index);
107 if (optarg != NULL) {
108 strncpy(config.dhcpdconf_file, optarg,
109 (size_t) MAXLEN - 1);
112 ("main: for argument configuration file parameter not set");
118 if (optarg != NULL) {
119 strncpy(config.dhcpdlease_file, optarg,
120 (size_t) MAXLEN - 1);
123 ("main: for argument lease file parameter not set");
129 if (optarg != NULL) {
130 strncpy(config.output_format, optarg,
134 ("main: for argument output format parameter not set");
139 /* Output sorting option */
140 if (optarg != NULL) {
141 sorts = strlen(optarg);
144 ("main: only 5 first sort orders will be used");
145 strncpy(config.sort, optarg,
149 strncpy(config.sort, optarg,
154 ("main: for argument sort order parameter not set");
159 /* What ever sort in reverse order */
160 config.reverse_order = true;
164 if (optarg != NULL) {
165 strncpy(config.output_file, optarg,
166 (size_t) MAXLEN - 1);
169 ("main: for argument output file parameter not set");
174 /* Specification what will be printed */
175 if (optarg != NULL) {
176 if (optarg[0] >= '0' && optarg[0] < '8') {
177 config.output_limit[0] =
178 (int) optarg[0] - '0';
181 ("main: output mask %s illegal",
185 if (optarg[1] >= '0' && optarg[1] < '8') {
186 config.output_limit[1] =
187 (int) optarg[1] - '0';
190 ("main: output mask %s illegal",
196 ("main: for argument output mask parameter not set");
212 /* Output function selection */
213 switch (config.output_format[0]) {
215 output_analysis = output_txt;
218 output_analysis = output_html;
221 output_analysis = output_html;
225 output_analysis = output_xml;
228 output_analysis = output_xml;
231 output_analysis = output_csv;
234 /* output_analysis = output_snmp; */
235 output_analysis = output_txt;
238 eprintf("main: unknown ouput format");
244 parse_config(true, config.dhcpdconf_file, shared_net_names,
245 shared_net_names + strlen(shared_net_names) + 1,
248 if ((config.output_format[0] == 'x')
249 || (config.output_format[0] == 'X')) {
250 printf("<dhcpstatus>\n");
256 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
258 mergesort_ranges(ranges, num_ranges, tmp_ranges);
260 if (config.reverse_order == true) {
261 flip_ranges(ranges, tmp_ranges);
266 if ((config.output_format[0] == 'x')
267 || (config.output_format[0] == 'X')) {
268 printf("</dhcpstatus>\n");
274 /* Global allocations, counter resets etc */
277 num_ranges = num_shared_networks = 0;
279 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
281 safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
283 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
285 /* First shared network entry is all networks */
286 strcpy(shared_net_names, "All networks");