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/>.
25 #else /* Not STDC_HEADERS */
26 extern char *malloc();
27 #endif /* STDC_HEADERS */
38 #include "dhcpd-pools.h"
40 int main(int argc, char **argv)
45 struct range_t *tmp_ranges;
47 /* Options for getopt_long */
48 static struct option const long_options[] = {
49 {"config", required_argument, 0, (int) 'c'},
50 {"leases", required_argument, 0, (int) 'l'},
51 {"format", required_argument, 0, (int) 'f'},
52 {"sort", required_argument, 0, (int) 's'},
53 {"reverse", no_argument, 0, (int) 'r'},
54 {"output", required_argument, 0, (int) 'o'},
55 {"limit", required_argument, 0, (int) 'L'},
56 {"version", no_argument, 0, (int) 'v'},
57 {"help", no_argument, 0, (int) 'h'},
61 /* FIXME: make these allocations dynamic up on need. */
62 config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
63 config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
64 config.output_file = safe_malloc(sizeof(char) * MAXLEN);
66 /* Make sure string has zero lenght if there is no
67 * command line option */
68 config.output_file[0] = '\0';
70 /* File location defaults */
71 strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
73 strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
76 config.output_limit[0] = (int) (*tmp - '0');
78 config.output_limit[1] = (int) (*tmp - '0');
81 /* Make sure some output format is selected by default */
82 strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
84 /* Default sort order is by IPs small to big */
85 config.reverse_order = false;
87 /* Parse command line options */
89 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
90 long_options, &option_index);
98 strncpy(config.dhcpdconf_file, optarg,
103 strncpy(config.dhcpdlease_file, optarg,
104 (size_t) MAXLEN - 1);
108 strncpy(config.output_format, optarg, (size_t) 1);
111 /* Output sorting option */
112 sorts = strlen(optarg);
115 ("main: only first 5 sort orders will be used");
116 strncpy(config.sort, optarg, (size_t) 5);
119 strncpy(config.sort, optarg,
122 for (i = 0; i < sorts; i++) {
123 field_selector(config.sort[i]);
127 /* What ever sort in reverse order */
128 config.reverse_order = true;
132 strncpy(config.output_file, optarg,
133 (size_t) MAXLEN - 1);
136 /* Specification what will be printed */
137 for (i = 0; i < 2; i++) {
138 if (optarg[i] >= '0' && optarg[i] < '8') {
139 config.output_limit[i] =
140 (int) optarg[i] - '0';
143 "main: output mask `%s' is illegal",
156 "Try `%s --help' for more information.",
157 program_invocation_short_name);
161 /* Output function selection */
162 switch (config.output_format[0]) {
164 output_analysis = output_txt;
167 output_analysis = output_html;
170 output_analysis = output_html;
174 output_analysis = output_xml;
177 output_analysis = output_xml;
180 output_analysis = output_csv;
183 errx(EXIT_FAILURE, "main: unknown output format `%c'",
184 config.output_format[0]);
189 parse_config(true, config.dhcpdconf_file, shared_networks);
194 tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
196 mergesort_ranges(ranges, num_ranges, tmp_ranges);
198 if (config.reverse_order == true) {
199 flip_ranges(ranges, tmp_ranges);
205 return (EXIT_SUCCESS);
208 /* Global allocations, counter resets etc */
212 num_ranges = num_shared_networks = 0;
214 safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
216 ranges = safe_malloc(sizeof(struct range_t) * RANGES);
219 /* First shared network entry is all networks */
220 shared_networks->name = safe_strdup("All networks");