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 = NULL;
65 struct stat lease_file_stats;
66 struct macaddr_t *macaddr_p = NULL;
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) {
166 fclose(dhcpd_leases);
170 /* Like strcpy but for field which is separated by white spaces. Number of
171 * first field is 1 and not 0 like C programs should have. Question of
172 * semantics, send mail to author if this annoys. All performance boosts for
173 * this function are well come. */
174 int nth_field(int n, char *dest, const char *src)
176 int i, j = 0, wordn = 0, len;
180 for (i = 0; i < len; i++) {
181 if (isspace(src[i])) {
200 /* dhcpd.conf interesting words */
201 int is_interesting_config_clause(char *s)
203 if (strstr(s, "range")) {
205 } else if (strstr(s, "shared-network")) {
207 } else if (strstr(s, "include")) {
214 /* FIXME: This spagetti monster function need to be rewrote at least ones. */
215 void parse_config(int is_include, char *config_file,
216 struct shared_network_t *shared_p)
219 int i = 0, newclause = true, argument = false, comment =
220 false, braces = 0, quote = false;
222 int braces_shared = 1000;
224 struct range_t *range_p;
226 word = safe_malloc(sizeof(char) * MAXLEN);
229 /* Default place holder for ranges "All networks". */
230 shared_p->name = shared_networks->name;
233 /* Open configuration file */
234 dhcpd_config = fopen(config_file, "r");
235 if (dhcpd_config == NULL) {
236 err(EXIT_FAILURE, "parse_config: %s", config_file);
238 #ifdef POSIX_FADV_WILLNEED
239 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED);
241 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
243 #endif /* POSIX_FADV_WILLNEED */
244 #ifdef POSIX_FADV_SEQUENTIAL
245 posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
247 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
249 #endif /* POSIX_FADV_SEQUENTIAL */
251 /* Very hairy stuff begins. */
252 while (!feof(dhcpd_config)) {
253 c = fgetc(dhcpd_config);
254 /* Certain characters are magical */
256 /* Handle comments if they are not quoted */
258 if (quote == false) {
263 if (comment == false) {
265 /* Either one or zero */
270 /* New line resets comment section, but
272 if (quote == false) {
277 /* Quoted colon does not mean new clause */
281 if (comment == false && argument != 2
285 } else if (argument == 2) {
286 /* Range ends to ; and this hair in code
287 * make two ranges wrote to gether like...
289 * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
291 * ...to be interpreted correctly. */
299 if (comment == false) {
302 /* i == 0 detects word that ends to brace like:
304 * shared-network DSL{ ... */
313 if (comment == false) {
315 /* End of shared-network */
316 if (braces_shared == braces) {
317 /* FIXME: Using 1000 is lame, but
319 braces_shared = 1000;
320 shared_p = shared_networks;
322 /* Not literally true, but works for this
331 /* Either inside comment or Nth word of clause. */
333 || (newclause == false && argument == 0)) {
336 /* Strip white spaces before new clause word. */
337 if ((newclause == true || argument != 0) && isspace(c)
341 /* Save to word which clause this is. */
342 if ((newclause == true || argument != 0)
343 && (!isspace(c) || quote == true)) {
346 /* Long word which is almost causing overflow. None
347 * of words are this long which the program is
355 /* See if clause is something that parser is looking for. */
356 else if (newclause == true) {
357 /* Insert string end & set state */
362 argument = is_interesting_config_clause(word);
364 /* words after range, shared-network or include */
365 else if (argument != 0) {
372 /* printf ("range 2nd ip: %s\n", word); */
373 range_p = ranges + num_ranges;
374 inet_aton(word, &inp);
376 range_p->last_ip = htonl(inp.s_addr) + 1;
378 range_p->touched = 0;
379 range_p->backups = 0;
380 range_p->shared_net = shared_p;
382 if (RANGES < num_ranges + 1) {
389 range_p = ranges + num_ranges;
394 /* printf ("range 1nd ip: %s\n", word); */
395 range_p = ranges + num_ranges;
396 if (!(inet_aton(word, &inp))) {
397 /* word was not ip, try
401 range_p->first_ip = htonl(inp.s_addr) - 1;
405 /* printf ("shared-network named: %s\n", word); */
406 num_shared_networks++;
408 shared_networks + num_shared_networks;
409 shared_p->name = safe_strdup(word);
410 shared_p->available = 0;
412 shared_p->touched = 0;
413 shared_p->backups = 0;
414 if (SHARED_NETWORKS <
415 num_shared_networks + 2) {
417 * away by reallocationg
420 "parse_config: increase default.h SHARED_NETWORKS and recompile");
423 braces_shared = braces;
426 /* printf ("include file: %s\n", word); */
428 parse_config(false, word, shared_p);
432 /* printf ("nothing interesting: %s\n", word); */
436 warnx("impossible occurred, report a bug");
442 fclose(dhcpd_config);