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 #define EXIT_FAILURE 1 /* Failing exit status. */
31 #define EXIT_SUCCESS 0 /* Successful exit status. */
32 #endif /* STDC_HEADERS */
41 #include <arpa/inet.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
45 #include <sys/types.h>
47 #define _XOPEN_SOURCE 600
55 #include "dhcpd-pools.h"
58 /* Parse dhcpd.leases file. All performance boosts for this function are
60 int parse_leases(void)
63 char *line, *ipstring, *macstring;
65 struct stat lease_file_stats;
66 struct macaddr_t *macaddr_p;
67 unsigned long leasesmallocsize;
68 unsigned long touchesmallocsize;
69 unsigned long backupsmallocsize;
70 int sw_active_lease = 0;
72 num_touches = num_leases = num_backups = 0;
74 dhcpd_leases = fopen(config.dhcpdlease_file, "r");
75 if (dhcpd_leases == NULL) {
76 err(EXIT_FAILURE, "parse_leases: %s",
77 config.dhcpdlease_file);
79 #ifdef POSIX_FADV_WILLNEED
80 posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_WILLNEED);
82 err(EXIT_FAILURE, "parse_leases: fadvise %s",
83 config.dhcpdlease_file);
85 #endif /* POSIX_FADV_WILLNEED */
86 #ifdef POSIX_FADV_SEQUENTIAL
87 posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
89 err(EXIT_FAILURE, "parse_leases: fadvise %s",
90 config.dhcpdlease_file);
92 #endif /* POSIX_FADV_SEQUENTIAL */
94 /* I found out that there's one lease address per 300 bytes in
95 * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
96 * If someone has higher density in lease file I'm interested to
98 if (stat(config.dhcpdlease_file, &lease_file_stats)) {
99 err(EXIT_FAILURE, "parse_leases: %s",
100 config.dhcpdlease_file);
102 leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
103 touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
104 backupsmallocsize = (lease_file_stats.st_size / 120) + MAXLEN - 2;
105 leases = safe_malloc(sizeof(long int) * leasesmallocsize);
107 safe_malloc((size_t) sizeof(long int) * touchesmallocsize);
109 line = safe_malloc(sizeof(long int) * MAXLEN);
110 ipstring = safe_malloc(sizeof(long int) * MAXLEN);
111 if (config.output_format[0] == 'X') {
112 macstring = safe_malloc(sizeof(char) * 18);
113 macaddr = safe_malloc(sizeof(struct macaddr_t));
115 macaddr_p->next = NULL;
118 while (!feof(dhcpd_leases)) {
119 fgets(line, MAXLEN, dhcpd_leases);
120 /* It's a lease, save IP */
121 if (strstr(line, "lease") == line) {
122 strncpy(ipstring, line, (size_t) MAXLEN);
123 nth_field(2, ipstring, ipstring);
124 inet_aton(ipstring, &inp);
127 /* Copy IP to correct array */
128 else if (strstr(line, "binding state active")) {
129 leases[num_leases] = htonl(inp.s_addr);
131 assert(!(leasesmallocsize < num_leases));
133 } else if (strstr(line, " binding state free")) {
134 touches[num_touches] = htonl(inp.s_addr);
136 assert(!(touchesmallocsize < num_touches));
137 } else if (strstr(line, " binding state backup")) {
138 if (num_backups == 0) {
140 safe_malloc((size_t) sizeof(long int) *
143 backups[num_backups] = htonl(inp.s_addr);
145 assert(!(backupsmallocsize < num_backups));
148 if ((macaddr != NULL)
149 && (sw_active_lease == 1)
150 && (strstr(line, "hardware ethernet"))) {
151 nth_field(3, macstring, line);
152 macstring[17] = '\0';
153 macaddr_p->ethernet = safe_strdup(macstring);
154 macaddr_p->ip = safe_strdup(ipstring);
156 safe_malloc(sizeof(struct macaddr_t));
157 macaddr_p = macaddr_p->next;
158 macaddr_p->next = NULL;
163 if (macaddr != NULL) {
169 /* Like strcpy but for field which is separated by white spaces. Number of
170 * first field is 1 and not 0 like C programs should have. Question of
171 * semantics, send mail to author if this annoys. All performance boosts for
172 * this function are well come. */
173 int nth_field(int n, char *dest, const char *src)
175 int i, j = 0, wordn = 0, len;
179 for (i = 0; i < len; i++) {
180 if (isspace(src[i])) {
199 /* dhcpd.conf interesting words */
200 int is_interesting_config_clause(char *s)
202 if (strstr(s, "range")) {
204 } else if (strstr(s, "shared-network")) {
206 } else if (strstr(s, "include")) {
213 /* FIXME: This spagetti monster function need to be rewrote at least ones. */
214 char *parse_config(int is_include, char *config_file,
215 char *current_shared_name,
216 char *next_free_shared_name,
217 struct shared_network_t *shared_p)
220 int i = 0, newclause = true, argument = false, comment =
221 false, braces = 0, quote = false;
223 int braces_shared = 1000;
225 struct range_t *range_p;
227 char *last_shared_name;
228 last_shared_name = SHARED_NETWORKS_NAMES + shared_net_names;
230 word = safe_malloc(sizeof(char) * MAXLEN);
233 /* Default place holder for ranges "All networks". */
234 shared_p->name = current_shared_name;
237 /* Open configuration file */
238 dhcpd_config = fopen(config_file, "r");
239 if (dhcpd_config == NULL) {
240 err(EXIT_FAILURE, "parse_config: %s", config_file);
242 #ifdef POSIX_FADV_WILLNEED
243 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED);
245 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
247 #endif /* POSIX_FADV_WILLNEED */
248 #ifdef POSIX_FADV_SEQUENTIAL
249 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
251 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
253 #endif /* POSIX_FADV_SEQUENTIAL */
255 /* Very hairy stuff begins. */
256 while (!feof(dhcpd_config)) {
257 c = fgetc(dhcpd_config);
258 /* Certain characters are magical */
260 /* Handle comments if they are not quoted */
262 if (quote == false) {
267 if (comment == false) {
269 /* Either one or zero */
274 /* New line resets comment section, but
276 if (quote == false) {
281 /* Quoted colon does not mean new clause */
285 if (comment == false && argument != 2
289 } else if (argument == 2) {
290 /* Range ends to ; and this hair in code
291 * make two ranges wrote to gether like...
293 * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
295 * ...to be interpreted correctly. */
303 if (comment == false) {
306 /* i == 0 detects word that ends to brace like:
308 * shared-network DSL{ ... */
317 if (comment == false) {
319 /* End of shared-network */
320 if (braces_shared == braces) {
321 current_shared_name =
323 /* FIXME: Using 1000 is lame, but
325 braces_shared = 1000;
326 shared_p = shared_networks;
328 /* Not literally true, but works for this
337 /* Either inside comment or Nth word of clause. */
339 || (newclause == false && argument == 0)) {
342 /* Strip white spaces before new clause word. */
343 if ((newclause == true || argument != 0) && isspace(c)
347 /* Save to word which clause this is. */
348 if ((newclause == true || argument != 0)
349 && (!isspace(c) || quote == true)) {
352 /* Long word which is almost causing overflow. None
353 * of words are this long which the program is
361 /* See if clause is something that parser is looking for. */
362 else if (newclause == true) {
363 /* Insert string end & set state */
368 argument = is_interesting_config_clause(word);
370 /* words after range, shared-network or include */
371 else if (argument != 0) {
378 /* printf ("range 2nd ip: %s\n", word); */
379 range_p = ranges + num_ranges;
380 inet_aton(word, &inp);
382 range_p->last_ip = htonl(inp.s_addr) + 1;
384 range_p->touched = 0;
385 range_p->backups = 0;
386 range_p->shared_net = shared_p;
388 if (RANGES < num_ranges) {
390 "parse_config: Range space full! Increase RANGES and recompile.");
395 /* printf ("range 1nd ip: %s\n", word); */
396 range_p = ranges + num_ranges;
397 if (!(inet_aton(word, &inp))) {
398 /* word was not ip, try
402 range_p->first_ip = htonl(inp.s_addr) - 1;
406 /* printf ("shared-network named: %s\n", word); */
407 strcpy(next_free_shared_name, word);
409 shared_networks + num_shared_networks;
410 num_shared_networks++;
412 shared_p->name = next_free_shared_name;
413 shared_p->available = 0;
415 shared_p->touched = 0;
416 shared_p->backups = 0;
417 /* Temporary abuse of argument
420 strlen(next_free_shared_name) + 1;
421 if (next_free_shared_name + argument <
423 next_free_shared_name += argument;
425 /* FIXME: make this go
426 * away by reallocationg
429 "parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
432 braces_shared = braces;
435 /* printf ("include file: %s\n", word); */
437 next_free_shared_name =
438 parse_config(false, word,
440 next_free_shared_name,
445 /* printf ("nothing interesting: %s\n", word); */
449 warnx("impossible occurred, report a bug");
455 return next_free_shared_name;