1 /* http://dhcpd-pools.sourceforge.net/
2 ** Copyright 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 3 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, see <http://www.gnu.org/licenses/>.
28 #else /* Not STDC_HEADERS */
29 extern char *malloc();
30 #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 /* FIXME: make these allocations dynamic up on need. */
65 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
66 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
67 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
69 /* Make sure string has zero lenght if there is no
70 * command line option */
71 config.output_file[0] = '\0';
73 /* File location defaults */
74 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
76 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
79 config.output_limit[0] = (int) (*tmp - '0');
81 config.output_limit[1] = (int) (*tmp - '0');
84 /* Make sure some output format is selected by default */
85 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
87 /* Default sort order is by IPs small to big */
88 config.reverse_order = false;
90 /* Parse command line options */
92 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
93 long_options, &option_index);
101 strncpy(config.dhcpdconf_file, optarg,
102 (size_t) MAXLEN - 1);
106 strncpy(config.dhcpdlease_file, optarg,
107 (size_t) MAXLEN - 1);
111 strncpy(config.output_format, optarg, (size_t) 1);
114 /* Output sorting option */
115 sorts = strlen(optarg);
118 ("main: only first 5 sort orders will be used");
119 strncpy(config.sort, optarg, (size_t) 5);
122 strncpy(config.sort, optarg,
125 for (i = 0; i < sorts; i++) {
126 field_selector(config.sort[i]);
130 /* What ever sort in reverse order */
131 config.reverse_order = true;
135 strncpy(config.output_file, optarg,
136 (size_t) MAXLEN - 1);
139 /* Specification what will be printed */
140 for (i = 0; i < 2; i++) {
141 if (optarg[i] >= '0' && optarg[i] < '8') {
142 config.output_limit[i] =
143 (int) optarg[i] - '0';
146 "main: output mask `%s' is illegal",
154 return (EXIT_SUCCESS);
160 "Try `%s --help' for more information.",
161 program_invocation_short_name);
165 /* Output function selection */
166 switch (config.output_format[0]) {
168 output_analysis = output_txt;
171 output_analysis = output_html;
174 output_analysis = output_html;
178 output_analysis = output_xml;
181 output_analysis = output_xml;
184 output_analysis = output_csv;
187 errx(EXIT_FAILURE, "main: unknown output format `%c'",
188 config.output_format[0]);
193 parse_config(true, config.dhcpdconf_file, shared_net_names,
194 shared_net_names + strlen(shared_net_names) + 1,
200 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
202 mergesort_ranges(ranges, num_ranges, tmp_ranges);
204 if (config.reverse_order == true) {
205 flip_ranges(ranges, tmp_ranges);
209 /* After fopen in output ioctl does like /dev/null which
210 * cause ENOTTY, and clean_up will see that without this
211 * reset. At least linux does this, and possibly some
212 * other systems. There's a report from FreeBSD 8.0 which
213 * matches quite well with this symptom. */
218 return (EXIT_SUCCESS);
221 /* Global allocations, counter resets etc */
224 num_ranges = num_shared_networks = 0;
226 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
228 safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
230 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
233 /* First shared network entry is all networks */
234 strcpy(shared_net_names, "All networks");