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, *macstring2;
65 struct stat lease_file_stats;
66 unsigned long leasesmallocsize;
67 unsigned long touchesmallocsize;
68 unsigned long backupsmallocsize;
69 int sw_active_lease = 0;
71 num_touches = num_leases = num_backups = 0;
73 dhcpd_leases = fopen(config.dhcpdlease_file, "r");
74 if (dhcpd_leases == NULL) {
75 err(EXIT_FAILURE, "parse_leases: %s",
76 config.dhcpdlease_file);
78 #ifdef POSIX_FADV_WILLNEED
79 posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_WILLNEED);
81 err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file);
83 #endif /* POSIX_FADV_WILLNEED */
84 #ifdef POSIX_FADV_SEQUENTIAL
85 posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
87 err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file);
89 #endif /* POSIX_FADV_SEQUENTIAL */
91 /* I found out that there's one lease address per 300 bytes in
92 * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
93 * If someone has higher density in lease file I'm interested to
95 if (stat(config.dhcpdlease_file, &lease_file_stats)) {
96 err(EXIT_FAILURE, "parse_leases: %s",
97 config.dhcpdlease_file);
99 leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
100 touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
101 backupsmallocsize = (lease_file_stats.st_size / 120) + MAXLEN - 2;
102 leases = safe_malloc(sizeof(long int) * leasesmallocsize);
104 safe_malloc((size_t) sizeof(long int) * touchesmallocsize);
106 line = safe_malloc(sizeof(long int) * MAXLEN);
107 ipstring = safe_malloc(sizeof(long int) * MAXLEN);
108 macstring = safe_malloc(sizeof(long int) * MAXLEN);
109 macstring2 = safe_malloc(sizeof(long int) * MAXLEN);
111 while (!feof(dhcpd_leases)) {
112 fgets(line, MAXLEN, dhcpd_leases);
113 /* It's a lease, save IP */
114 if (strstr(line, "lease") == line) {
115 strncpy(ipstring, line, (size_t) MAXLEN);
116 nth_field(2, ipstring, ipstring);
117 inet_aton(ipstring, &inp);
120 /* Copy IP to correct array */
121 else if (strstr(line, "binding state active")) {
122 leases[num_leases] = htonl(inp.s_addr);
124 assert(!(leasesmallocsize < num_leases));
126 } else if (strstr(line, " binding state free")) {
127 touches[num_touches] = htonl(inp.s_addr);
129 assert(!(touchesmallocsize < num_touches));
130 } else if (strstr(line, " binding state backup")) {
131 if (num_backups == 0) {
133 safe_malloc((size_t) sizeof(long int) *
136 backups[num_backups] = htonl(inp.s_addr);
138 assert(!(backupsmallocsize < num_backups));
141 /* FIXME: move to output.c and use the FILE
143 if ((config.output_format[0] == 'X')
144 && (sw_active_lease == 1)
145 && (strstr(line, "hardware ethernet"))) {
146 nth_field(3, macstring, line);
147 macstring[strlen(macstring) - 1] = '\0';
150 ("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
151 ipstring, macstring);
157 /* Like strcpy but for field which is separated by white spaces. Number of
158 * first field is 1 and not 0 like C programs should have. Question of
159 * semantics, send mail to author if this annoys. All performance boosts for
160 * this function are well come. */
161 int nth_field(int n, char *dest, const char *src)
163 int i, j = 0, wordn = 0, len;
167 for (i = 0; i < len; i++) {
168 if (isspace(src[i])) {
187 /* dhcpd.conf interesting words */
188 int is_interesting_config_clause(char *s)
190 if (strstr(s, "range")) {
192 } else if (strstr(s, "shared-network")) {
194 } else if (strstr(s, "include")) {
201 /* FIXME: This spagetti monster function need to be rewrote at least ones. */
202 char *parse_config(int is_include, char *config_file,
203 char *current_shared_name,
204 char *next_free_shared_name,
205 struct shared_network_t *shared_p)
208 int i = 0, newclause = true, argument = false, comment =
209 false, braces = 0, quote = false;
211 int braces_shared = 1000;
213 struct range_t *range_p;
215 char *last_shared_name;
216 last_shared_name = SHARED_NETWORKS_NAMES + shared_net_names;
218 word = safe_malloc(sizeof(char) * MAXLEN);
221 /* Default place holder for ranges "All networks". */
222 shared_p->name = current_shared_name;
225 /* Open configuration file */
226 dhcpd_config = fopen(config_file, "r");
227 if (dhcpd_config == NULL) {
228 err(EXIT_FAILURE, "parse_config: %s", config_file);
230 #ifdef POSIX_FADV_WILLNEED
231 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED);
233 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
235 #endif /* POSIX_FADV_WILLNEED */
236 #ifdef POSIX_FADV_SEQUENTIAL
237 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
239 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
241 #endif /* POSIX_FADV_SEQUENTIAL */
243 /* Very hairy stuff begins. */
244 while (!feof(dhcpd_config)) {
245 c = fgetc(dhcpd_config);
246 /* Certain characters are magical */
248 /* Handle comments if they are not quoted */
250 if (quote == false) {
255 if (comment == false) {
257 /* Either one or zero */
262 /* New line resets comment section, but
264 if (quote == false) {
269 /* Quoted colon does not mean new clause */
273 if (comment == false && argument != 2
277 } else if (argument == 2) {
278 /* Range ends to ; and this hair in code
279 * make two ranges wrote to gether like...
281 * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
283 * ...to be interpreted correctly. */
291 if (comment == false) {
294 /* i == 0 detects word that ends to brace like:
296 * shared-network DSL{ ... */
305 if (comment == false) {
307 /* End of shared-network */
308 if (braces_shared == braces) {
309 current_shared_name =
311 /* FIXME: Using 1000 is lame, but
313 braces_shared = 1000;
314 shared_p = shared_networks;
316 /* Not literally true, but works for this
325 /* Either inside comment or Nth word of clause. */
327 || (newclause == false && argument == 0)) {
330 /* Strip white spaces before new clause word. */
331 if ((newclause == true || argument != 0) && isspace(c)
335 /* Save to word which clause this is. */
336 if ((newclause == true || argument != 0)
337 && (!isspace(c) || quote == true)) {
340 /* Long word which is almost causing overflow. None
341 * of words are this long which the program is
349 /* See if clause is something that parser is looking for. */
350 else if (newclause == true) {
351 /* Insert string end & set state */
356 argument = is_interesting_config_clause(word);
358 /* words after range, shared-network or include */
359 else if (argument != 0) {
366 /* printf ("range 2nd ip: %s\n", word); */
367 range_p = ranges + num_ranges;
368 inet_aton(word, &inp);
370 range_p->last_ip = htonl(inp.s_addr) + 1;
372 range_p->touched = 0;
373 range_p->backups = 0;
374 range_p->shared_net = shared_p;
376 if (RANGES < num_ranges) {
378 "parse_config: Range space full! Increase RANGES and recompile.");
383 /* printf ("range 1nd ip: %s\n", word); */
384 range_p = ranges + num_ranges;
385 if (!(inet_aton(word, &inp))) {
386 /* word was not ip, try
390 range_p->first_ip = htonl(inp.s_addr) - 1;
394 /* printf ("shared-network named: %s\n", word); */
395 strcpy(next_free_shared_name, word);
397 shared_networks + num_shared_networks;
398 num_shared_networks++;
400 shared_p->name = next_free_shared_name;
401 shared_p->available = 0;
403 shared_p->touched = 0;
404 shared_p->backups = 0;
405 /* Temporary abuse of argument
408 strlen(next_free_shared_name) + 1;
409 if (next_free_shared_name + argument <
411 next_free_shared_name += argument;
413 /* FIXME: make this go
414 * away by reallocationg
417 "parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
420 braces_shared = braces;
423 /* printf ("include file: %s\n", word); */
425 next_free_shared_name =
426 parse_config(false, word,
428 next_free_shared_name,
433 /* printf ("nothing interesting: %s\n", word); */
437 warnx("impossible occurred, report a bug");
443 return next_free_shared_name;