Impossible to reach sections removed
[debian/dhcpd-pools.git] / src / dhcpd-pools.c
1 /*
2 ** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
3 **  
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.
8 ** 
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.
13 ** 
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.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #ifdef  HAVE_STDLIB_H
28 #include <stdlib.h>
29 #else                           /* Not STDC_HEADERS */
30 extern char *malloc();
31 #endif                          /* STDC_HEADERS */
32 #ifdef  HAVE_STRING_H
33 #include <string.h>
34 #else
35 #include <strings.h>
36 #endif
37 #include <getopt.h>
38 #include <errno.h>
39 #include <err.h>
40
41 #include "dhcpd-pools.h"
42 #include "defaults.h"
43
44 int main(int argc, char **argv)
45 {
46         int i, c, sorts = 0;
47         int option_index = 0;
48         char *tmp;
49         struct range_t *tmp_ranges;
50
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'},
62                 {0, 0, 0, 0}
63         };
64
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);
69
70         /* Make sure string has zero lenght if there is no
71          * command line option */
72         config.output_file[0] = '\0';
73
74         /* File location defaults */
75         strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
76                 (size_t) MAXLEN - 1);
77         strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
78                 (size_t) MAXLEN - 1);
79         tmp = OUTPUT_LIMIT;
80         config.output_limit[0] = (int) (*tmp - '0');
81         tmp++;
82         config.output_limit[1] = (int) (*tmp - '0');
83         fullhtml = false;
84
85         /* Make sure some output format is selected by default */
86         strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
87
88         /* Default sort order is by IPs small to big */
89         config.reverse_order = false;
90
91         /* Parse command line options */
92         while (1) {
93                 c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
94                                 long_options, &option_index);
95
96                 if (c == EOF)
97                         break;
98
99                 switch (c) {
100                 case 'c':
101                         /* config file */
102                         strncpy(config.dhcpdconf_file, optarg,
103                                 (size_t) MAXLEN - 1);
104                         break;
105                 case 'l':
106                         /* lease file */
107                         strncpy(config.dhcpdlease_file, optarg,
108                                 (size_t) MAXLEN - 1);
109                         break;
110                 case 'f':
111                         /* Output format */
112                         strncpy(config.output_format, optarg, (size_t) 1);
113                         break;
114                 case 's':
115                         /* Output sorting option */
116                         sorts = strlen(optarg);
117                         if (5 < sorts) {
118                                 warnx("main: only first 5 sort orders will be used");
119                                 strncpy(config.sort, optarg, (size_t) 5);
120                                 sorts = 5;
121                         } else {
122                                 strncpy(config.sort, optarg,
123                                         (size_t) sorts);
124                         }
125                         break;
126                 case 'r':
127                         /* What ever sort in reverse order */
128                         config.reverse_order = true;
129                         break;
130                 case 'o':
131                         /* Output file */
132                         strncpy(config.output_file, optarg,
133                                 (size_t) MAXLEN - 1);
134                         break;
135                 case 'L':
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';
141                                 } else {
142                                         errx(EXIT_FAILURE,
143                                              "main: output mask `%s' is illegal",
144                                              optarg);
145                                 }
146                         }
147                         break;
148                 case 'v':
149                         /* Print version */
150                         print_version();
151                         return (EXIT_SUCCESS);
152                 case 'h':
153                         /* Print help */
154                         usage(EXIT_SUCCESS);
155                 default:
156                         errx(EXIT_FAILURE,
157                              "Try `%s --help' for more information.",
158                              program_invocation_short_name);
159                 }
160         }
161
162         /* Output function selection */
163         switch (config.output_format[0]) {
164         case 't':
165                 output_analysis = output_txt;
166                 break;
167         case 'h':
168                 output_analysis = output_html;
169                 break;
170         case 'H':
171                 output_analysis = output_html;
172                 fullhtml = true;
173                 break;
174         case 'x':
175                 output_analysis = output_xml;
176                 break;
177         case 'X':
178                 output_analysis = output_xml;
179                 break;
180         case 'c':
181                 output_analysis = output_csv;
182                 break;
183         default:
184                 errx(EXIT_FAILURE, "main: unknown ouput format `%c'",
185                      config.output_format[0]);
186         }
187
188         /* Do the job */
189         prepare_memory();
190         parse_config(true, config.dhcpdconf_file, shared_net_names,
191                      shared_net_names + strlen(shared_net_names) + 1,
192                      shared_networks);
193
194         /* FIXME: move to output.c and use FILE *outfile */
195         if ((config.output_format[0] == 'x')
196             || (config.output_format[0] == 'X')) {
197                 printf("<dhcpstatus>\n");
198         };
199
200         parse_leases();
201         prepare_data();
202         do_counting();
203         tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
204         if (sorts != 0) {
205                 mergesort_ranges(ranges, num_ranges, tmp_ranges);
206         }
207         if (config.reverse_order == true) {
208                 flip_ranges(ranges, tmp_ranges);
209         }
210         free(tmp_ranges);
211         output_analysis();
212         /* After fopen in ouput ioctl does like /dev/null which
213          * cause ENOTTY, and clean_up will see that without this
214          * reset. At least linux does this, and possibly some
215          * other systems. There's a report from FreeBSD 8.0 which
216          * matches quite well with the symptom. */
217         if (errno == 25)
218                 errno = 0;
219
220         /* FIXME: move to output.c and use FILE *outfile */
221         if ((config.output_format[0] == 'x')
222             || (config.output_format[0] == 'X')) {
223                 printf("</dhcpstatus>\n");
224         };
225
226         clean_up();
227         return (EXIT_SUCCESS);
228 }
229
230 /* Global allocations, counter resets etc */
231 int prepare_memory()
232 {
233         num_ranges = num_shared_networks = 0;
234         shared_networks =
235             safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
236         shared_net_names =
237             safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
238
239         ranges = safe_malloc(sizeof(struct range_t) * RANGES);
240
241         /* First shared network entry is all networks */
242         strcpy(shared_net_names, "All networks");
243         return 0;
244 }