Optimization on basis of gcov
[debian/dhcpd-pools.git] / src / getdata.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 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef  HAVE_STDLIB_H
28 #include <stdlib.h>
29 #else                           /* Not STDC_HEADERS */
30 extern void exit();
31 extern char *malloc();
32 #define EXIT_FAILURE    1       /* Failing exit status.  */
33 #define EXIT_SUCCESS    0       /* Successful exit status.  */
34 #endif                          /* STDC_HEADERS */
35
36 #ifdef  HAVE_STRING_H
37 #include <string.h>
38 #else
39 #include <strings.h>
40 #endif
41
42 #include <stdio.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #define _XOPEN_SOURCE 600
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <ctype.h>
53
54 #include "dhcpd-pools.h"
55 #include "defaults.h"
56
57 /* Parse dhcpd.leases file. All performance boosts for this
58  * function are wellcome */
59 int parse_leases(void)
60 {
61         FILE *dhcpd_leases;
62         char *line, *ipstring, *macstring, *macstring2;
63         struct in_addr inp;
64         struct stat lease_file_stats;
65         unsigned long leasesmallocsize;
66         unsigned long touchesmallocsize;
67         unsigned long backupsmallocsize;
68         int sw_active_lease = 0;
69
70         num_touches = num_leases = num_backups = 0;
71
72         dhcpd_leases = fopen(config.dhcpdlease_file, "r");
73         if (dhcpd_leases == NULL) {
74                 eprintf("parse_leases: %s:", config.dhcpdlease_file);
75                 exit(EXIT_FAILURE);
76         }
77 #ifdef POSIX_FADV_NOREUSE
78         posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_NOREUSE);
79         if (errno) {
80                 eprintf("parse_leases: fadvise:");
81                 exit(EXIT_FAILURE);
82         }
83 #endif                          /* POSIX_FADV_NOREUSE */
84 #ifdef POSIX_FADV_SEQUENTIAL
85         posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
86         if (errno) {
87                 eprintf("parse_leases: fadvise:");
88                 exit(EXIT_FAILURE);
89         }
90 #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
93          * 250. If someone has higher density in lease file I'm
94          * interested to hear about that. */
95         if (stat(config.dhcpdlease_file, &lease_file_stats)) {
96                 eprintf("parse_leases: %s:", config.dhcpdlease_file);
97                 exit(EXIT_FAILURE);
98         }
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);
103         touches =
104             safe_malloc((size_t) sizeof(long int) * touchesmallocsize);
105
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);
110
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);
118                         sw_active_lease = 0;
119                 }
120                 /* Copy IP to correct array */
121                 else if (strstr(line, "binding state active")) {
122                         leases[num_leases] = htonl(inp.s_addr);
123                         num_leases++;
124                         if (leasesmallocsize < num_leases) {
125                                 errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug");
126                         }
127                         sw_active_lease = 1;
128                 } else if (strstr(line, "  binding state free")) {
129                         touches[num_touches] = htonl(inp.s_addr);
130                         num_touches++;
131                         if (touchesmallocsize < num_touches) {
132                                 errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug");
133                         }
134                 } else if (strstr(line, "  binding state backup")) {
135                         if (num_backups == 0) {
136                                 backups =
137                                     safe_malloc((size_t) sizeof(long int) *
138                                                 backupsmallocsize);
139                         }
140                         backups[num_backups] = htonl(inp.s_addr);
141                         num_backups++;
142                         if (backupsmallocsize < num_backups) {
143                                 errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug");
144                         }
145                 }
146
147                 /* FIXME: move to output.c and use FILE *outfile */
148                 if ((config.output_format[0] == 'X')
149                     && (sw_active_lease == 1)
150                     && (strstr(line, "hardware ethernet"))) {
151                         nth_field(3, macstring, line);
152                         macstring[strlen(macstring) - 1] = '\0';
153
154                         printf
155                             ("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
156                              ipstring, macstring);
157                 }
158         }
159         return 0;
160 }
161
162 /* Like strcpy but for field which is separated by white spaces.
163  * Number of first field is 1 and not 0 like C programs should
164  * have. Question of semantics, send mail to author if this
165  * annoys. All performance boosts for this function are well
166  * come. */
167 int nth_field(int n, char *dest, const char *src)
168 {
169         int i, j = 0, wordn = 0, len;
170
171         len = strlen(src);
172
173         for (i = 0; i < len; i++) {
174                 if (isspace(src[i])) {
175                         if (!(wordn < n)) {
176                                 dest[j] = '\0';
177                                 break;
178                         }
179                         j = 0;
180                 } else {
181                         if (j == 0) {
182                                 wordn++;
183                         }
184                         if (wordn == n) {
185                                 dest[j] = src[i];
186                         }
187                         j++;
188                 }
189         }
190         return 0;
191 }
192
193 /* dhcpd.conf interesting words */
194 int is_interesting_config_clause(char *s)
195 {
196         if (strstr(s, "range")) {
197                 return 3;
198         } else if (strstr(s, "shared-network")) {
199                 return 1;
200         } else if (strstr(s, "include")) {
201                 return 4;
202         } else {
203                 return 0;
204         }
205 }
206
207 /* TODO: This spagetti monster function need to be rewrote at
208  * least ones. */
209 char *parse_config(int is_include, char *config_file,
210                    char *current_shared_name,
211                    char *next_free_shared_name,
212                    struct shared_network_t *shared_p)
213 {
214         FILE *dhcpd_config;
215         int i = 0, newclause = true, argument = false, comment =
216             false, braces = 0, quote = false;
217         char *word, c;
218         int braces_shared = 1000;
219         struct in_addr inp;
220         struct range_t *range_p;
221
222         char *last_shared_name;
223         last_shared_name = SHARED_NETWORKS_NAMES + shared_net_names;
224
225         word = safe_malloc(sizeof(char) * MAXLEN);
226
227         if (is_include) {
228                 /* Default place holder for ranges "All networks". */
229                 shared_p->name = current_shared_name;
230         }
231
232         /* Open configuration file */
233         dhcpd_config = fopen(config_file, "r");
234         if (dhcpd_config == NULL) {
235                 eprintf("parse_config: %s:", config_file);
236                 exit(EXIT_FAILURE);
237         }
238 #ifdef POSIX_FADV_NOREUSE
239         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_NOREUSE);
240         if (errno) {
241                 eprintf("parse_config: fadvise:");
242                 exit(EXIT_FAILURE);
243         }
244 #endif                          /* POSIX_FADV_NOREUSE */
245 #ifdef POSIX_FADV_SEQUENTIAL
246         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
247         if (errno) {
248                 eprintf("parse_config: fadvise:");
249                 exit(EXIT_FAILURE);
250         }
251 #endif                          /* POSIX_FADV_SEQUENTIAL */
252         /* Very hairy stuff begins. */
253         while (!feof(dhcpd_config)) {
254                 c = fgetc(dhcpd_config);
255                 /* Certain characters are magical */
256                 switch (c) {
257                         /* Handle comments if they are not quoted */
258                 case '#':
259                         if (quote == false) {
260                                 comment = true;
261                         }
262                         continue;
263                 case '"':
264                         if (comment == false) {
265                                 quote++;
266                                 /* Either one or zero */
267                                 quote = quote % 2;
268                         }
269                         continue;
270                 case '\n':
271                         /* New line resets comment section, but not if quoted */
272                         if (quote == false) {
273                                 comment = false;
274                         }
275                         break;
276                 case ';':
277                         /* Quoted colon does not mean new clause */
278                         if (quote == true) {
279                                 break;
280                         }
281                         if (comment == false && argument != 2
282                             && argument != 4) {
283                                 newclause = true;
284                                 i = 0;
285                         } else if (argument == 2) {
286                                 /* Range ends to ; and this hair in code make two
287                                  * ranges wrote to gether like...
288                                  *
289                                  * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
290                                  * 
291                                  * ...to be interpreted correctly. */
292                                 c = ' ';
293                         }
294                         continue;
295                 case '{':
296                         if (quote == true) {
297                                 break;
298                         }
299                         if (comment == false) {
300                                 braces++;
301                         }
302                         /* i == 0 detects word that ends to brace like:
303                          *
304                          * shared-network DSL{ ... */
305                         if (i == 0) {
306                                 newclause = true;
307                         }
308                         continue;
309                 case '}':
310                         if (quote == true) {
311                                 break;
312                         }
313                         if (comment == false) {
314                                 braces--;
315                                 /* End of shared-network */
316                                 if (braces_shared == braces) {
317                                         current_shared_name =
318                                             shared_net_names;
319                                         /* TODO: Using 1000 is lame, but works. */
320                                         braces_shared = 1000;
321                                         shared_p = shared_networks;
322                                 }
323                                 /* Not literally true, but works for this program */
324                                 newclause = true;
325                         }
326                         continue;
327                 default:
328                         break;
329                 }
330
331                 /* Either inside comment or Nth word of clause. */
332                 if (comment == true
333                     || (newclause == false && argument == 0)) {
334                         continue;
335                 }
336                 /* Strip white spaces before new clause word. */
337                 if ((newclause == true || argument != 0) && isspace(c)
338                     && i == 0) {
339                         continue;
340                 }
341                 /* Save to word which clause this is. */
342                 if ((newclause == true || argument != 0)
343                     && (!isspace(c) || quote == true)) {
344                         word[i] = c;
345                         i++;
346                         /* Long word which is almost causing overflow. Not any of words
347                          * this program is looking for are this long. */
348                         if (MAXLEN < i) {
349                                 newclause = false;
350                                 i = 0;
351                                 continue;
352                         }
353                 }
354                 /* See if clause is something that parser is looking for. */
355                 else if (newclause == true) {
356                         /* Insert string end & set state */
357                         word[i] = '\0';
358                         newclause = false;
359                         i = 0;
360
361                         argument = is_interesting_config_clause(word);
362                 }
363                 /* words after range, shared-network or include */
364                 else if (argument != 0) {
365                         word[i] = '\0';
366                         newclause = false;
367                         i = 0;
368
369                         switch (argument) {
370                         case 2:
371                                 /* printf ("range 2nd ip: %s\n", word); */
372                                 range_p = ranges + num_ranges;
373                                 inet_aton(word, &inp);
374                                 argument = 0;
375                                 range_p->last_ip = htonl(inp.s_addr) + 1;
376                                 range_p->count = 0;
377                                 range_p->touched = 0;
378                                 range_p->backups = 0;
379                                 range_p->shared_net = shared_p;
380                                 num_ranges++;
381                                 if (RANGES < num_ranges) {
382                                         eprintf
383                                             ("parse_config: Range space full! Increase RANGES and recompile.");
384                                         exit(EXIT_FAILURE);
385                                 }
386                                 newclause = true;
387                                 break;
388                         case 3:
389                                 /* printf ("range 1nd ip: %s\n", word); */
390                                 range_p = ranges + num_ranges;
391                                 if (!(inet_aton(word, &inp))) {
392                                         /* word was not ip, try again */
393                                         break;
394                                 }
395                                 range_p->first_ip = htonl(inp.s_addr) - 1;
396                                 argument = 2;
397                                 break;
398                         case 1:
399                                 /* printf ("shared-network named: %s\n", word); */
400                                 strcpy(next_free_shared_name, word);
401                                 shared_p =
402                                     shared_networks + num_shared_networks;
403                                 num_shared_networks++;
404                                 shared_p++;
405                                 shared_p->name = next_free_shared_name;
406                                 shared_p->available = 0;
407                                 shared_p->used = 0;
408                                 shared_p->touched = 0;
409                                 shared_p->backups = 0;
410                                 /* Temporary abuse of argument variable */
411                                 argument =
412                                     strlen(next_free_shared_name) + 1;
413                                 if (last_shared_name >
414                                     next_free_shared_name + argument) {
415                                         next_free_shared_name += argument;
416                                 } else {
417                                         /* TODO: make this go away by reallocationg more space. */
418                                         eprintf
419                                             ("parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
420                                         exit(EXIT_FAILURE);
421                                 }
422                                 argument = 0;
423                                 braces_shared = braces;
424                                 break;
425                         case 4:
426                                 /* printf ("include file: %s\n", word); */
427                                 argument = 0;
428                                 next_free_shared_name =
429                                     parse_config(false, word,
430                                                  current_shared_name,
431                                                  next_free_shared_name,
432                                                  shared_p);
433                                 newclause = true;
434                                 break;
435                         case 0:
436                                 /* printf ("nothing interesting: %s\n", word); */
437                                 argument = 0;
438                                 break;
439                         default:
440                                 eprintf
441                                     ("parse_config: This cannot happen, report a bug!");
442                                 exit(EXIT_FAILURE);
443                         }
444                 }
445         }
446         free(word);
447         return next_free_shared_name;
448 }