2 * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3 * BSD License" or "FreeBSD License".
5 * Copyright 2006- Sami Kerola. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing
33 * official policies, either expressed or implied, of Sami Kerola.
43 #else /* Not STDC_HEADERS */
44 extern char *malloc();
45 #endif /* STDC_HEADERS */
56 #include "dhcpd-pools.h"
58 int main(int argc, char **argv)
63 struct range_t *tmp_ranges;
65 /* Options for getopt_long */
66 static struct option const long_options[] = {
67 {"config", required_argument, 0, (int) 'c'},
68 {"leases", required_argument, 0, (int) 'l'},
69 {"format", required_argument, 0, (int) 'f'},
70 {"sort", required_argument, 0, (int) 's'},
71 {"reverse", no_argument, 0, (int) 'r'},
72 {"output", required_argument, 0, (int) 'o'},
73 {"limit", required_argument, 0, (int) 'L'},
74 {"version", no_argument, 0, (int) 'v'},
75 {"help", no_argument, 0, (int) 'h'},
79 /* FIXME: make these allocations dynamic up on need. */
80 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
81 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
82 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
84 /* Make sure string has zero lenght if there is no
85 * command line option */
86 config.output_file[0] = '\0';
88 /* File location defaults */
89 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
91 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
94 config.output_limit[0] = (int) (*tmp - '0');
96 config.output_limit[1] = (int) (*tmp - '0');
99 /* Make sure some output format is selected by default */
100 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
102 /* Default sort order is by IPs small to big */
103 config.reverse_order = false;
105 /* Parse command line options */
107 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
108 long_options, &option_index);
116 strncpy(config.dhcpdconf_file, optarg,
117 (size_t) MAXLEN - 1);
121 strncpy(config.dhcpdlease_file, optarg,
122 (size_t) MAXLEN - 1);
126 strncpy(config.output_format, optarg, (size_t) 1);
129 /* Output sorting option */
130 sorts = strlen(optarg);
133 ("main: only first 5 sort orders will be used");
134 strncpy(config.sort, optarg, (size_t) 5);
137 strncpy(config.sort, optarg,
140 for (i = 0; i < sorts; i++) {
141 field_selector(config.sort[i]);
145 /* What ever sort in reverse order */
146 config.reverse_order = true;
150 strncpy(config.output_file, optarg,
151 (size_t) MAXLEN - 1);
154 /* Specification what will be printed */
155 for (i = 0; i < 2; i++) {
156 if (optarg[i] >= '0' && optarg[i] < '8') {
157 config.output_limit[i] =
158 (int) optarg[i] - '0';
161 "main: output mask `%s' is illegal",
174 "Try `%s --help' for more information.",
175 program_invocation_short_name);
179 /* Output function selection */
180 switch (config.output_format[0]) {
182 output_analysis = output_txt;
185 output_analysis = output_html;
188 output_analysis = output_html;
192 output_analysis = output_xml;
195 output_analysis = output_xml;
198 output_analysis = output_csv;
201 errx(EXIT_FAILURE, "main: unknown output format `%c'",
202 config.output_format[0]);
207 parse_config(true, config.dhcpdconf_file, shared_networks);
212 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
214 mergesort_ranges(ranges, num_ranges, tmp_ranges);
216 if (config.reverse_order == true) {
217 flip_ranges(ranges, tmp_ranges);
223 return (EXIT_SUCCESS);
226 /* Global allocations, counter resets etc */
230 num_ranges = num_shared_networks = 0;
232 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
234 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
237 /* First shared network entry is all networks */
238 shared_networks->name = safe_strdup("All networks");
239 shared_networks->used = 0;
240 shared_networks->touched = 0;
241 shared_networks->backups = 0;