Optimization on basis of gcov
[debian/dhcpd-pools.git] / src / getdata.c
index 0356649..d887f0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-** Copyright (C) 2006- Sami Kerola <   >
+** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
 **  
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
@@ -48,6 +48,7 @@ extern char *malloc();
 #define _XOPEN_SOURCE 600
 #include <fcntl.h>
 #include <errno.h>
+#include <err.h>
 #include <ctype.h>
 
 #include "dhcpd-pools.h"
@@ -120,10 +121,16 @@ int parse_leases(void)
                else if (strstr(line, "binding state active")) {
                        leases[num_leases] = htonl(inp.s_addr);
                        num_leases++;
+                       if (leasesmallocsize < num_leases) {
+                               errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug");
+                       }
                        sw_active_lease = 1;
                } else if (strstr(line, "  binding state free")) {
                        touches[num_touches] = htonl(inp.s_addr);
                        num_touches++;
+                       if (touchesmallocsize < num_touches) {
+                               errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug");
+                       }
                } else if (strstr(line, "  binding state backup")) {
                        if (num_backups == 0) {
                                backups =
@@ -132,35 +139,23 @@ int parse_leases(void)
                        }
                        backups[num_backups] = htonl(inp.s_addr);
                        num_backups++;
+                       if (backupsmallocsize < num_backups) {
+                               errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug");
+                       }
                }
 
-               if ((sw_active_lease == 1)
+               /* FIXME: move to output.c and use FILE *outfile */
+               if ((config.output_format[0] == 'X')
+                   && (sw_active_lease == 1)
                    && (strstr(line, "hardware ethernet"))) {
                        nth_field(3, macstring, line);
                        macstring[strlen(macstring) - 1] = '\0';
 
-                       if (config.output_format[0] == 'X') {
-                               printf
-                                   ("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
-                                    ipstring, macstring);
-                       };
-               }
-
-               if ((num_leases > leasesmallocsize) ||
-                   (num_touches > touchesmallocsize) ||
-                   (num_backups > backupsmallocsize)) {
-                       printf("WARNING: running out of memory\n");
-                       printf("\tlease/touch/backup = %lu/%lu/%lu\n",
-                              leasesmallocsize, touchesmallocsize,
-                              backupsmallocsize);
-                       printf("\tlease/touch/backup = %lu/%lu/%lu\n",
-                              num_leases, num_touches, num_backups);
-                       printf
-                           ("Code should realloc() and init new memory, but no time to write that now!\n");
-                       exit(EXIT_FAILURE);
+                        printf
+                           ("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
+                            ipstring, macstring);
                }
        }
-
        return 0;
 }
 
@@ -198,10 +193,10 @@ int nth_field(int n, char *dest, const char *src)
 /* dhcpd.conf interesting words */
 int is_interesting_config_clause(char *s)
 {
-       if (strstr(s, "shared-network")) {
-               return 1;
-       } else if (strstr(s, "range")) {
+       if (strstr(s, "range")) {
                return 3;
+       } else if (strstr(s, "shared-network")) {
+               return 1;
        } else if (strstr(s, "include")) {
                return 4;
        } else {
@@ -350,7 +345,7 @@ char *parse_config(int is_include, char *config_file,
                        i++;
                        /* Long word which is almost causing overflow. Not any of words
                         * this program is looking for are this long. */
-                       if (i > MAXLEN) {
+                       if (MAXLEN < i) {
                                newclause = false;
                                i = 0;
                                continue;
@@ -372,9 +367,33 @@ char *parse_config(int is_include, char *config_file,
                        i = 0;
 
                        switch (argument) {
-                       case 0:
-                               /* printf ("nothing interesting: %s\n", word); */
+                       case 2:
+                               /* printf ("range 2nd ip: %s\n", word); */
+                               range_p = ranges + num_ranges;
+                               inet_aton(word, &inp);
                                argument = 0;
+                               range_p->last_ip = htonl(inp.s_addr) + 1;
+                               range_p->count = 0;
+                               range_p->touched = 0;
+                               range_p->backups = 0;
+                               range_p->shared_net = shared_p;
+                               num_ranges++;
+                               if (RANGES < num_ranges) {
+                                       eprintf
+                                           ("parse_config: Range space full! Increase RANGES and recompile.");
+                                       exit(EXIT_FAILURE);
+                               }
+                               newclause = true;
+                               break;
+                       case 3:
+                               /* printf ("range 1nd ip: %s\n", word); */
+                               range_p = ranges + num_ranges;
+                               if (!(inet_aton(word, &inp))) {
+                                       /* word was not ip, try again */
+                                       break;
+                               }
+                               range_p->first_ip = htonl(inp.s_addr) - 1;
+                               argument = 2;
                                break;
                        case 1:
                                /* printf ("shared-network named: %s\n", word); */
@@ -403,43 +422,20 @@ char *parse_config(int is_include, char *config_file,
                                argument = 0;
                                braces_shared = braces;
                                break;
-                       case 2:
-                               /* printf ("range 2nd ip: %s\n", word); */
-                               range_p = ranges + num_ranges;
-                               inet_aton(word, &inp);
-                               argument = 0;
-                               range_p->last_ip = htonl(inp.s_addr) + 1;
-                               range_p->count = 0;
-                               range_p->touched = 0;
-                               range_p->backups = 0;
-                               range_p->shared_net = shared_p;
-                               num_ranges++;
-                               if (num_ranges > RANGES) {
-                                       eprintf
-                                           ("parse_config: Range space full! Increase RANGES and recompile.");
-                                       exit(EXIT_FAILURE);
-                               }
-                               newclause = true;
-                               break;
-                       case 3:
-                               /* printf ("range 1nd ip: %s\n", word); */
-                               range_p = ranges + num_ranges;
-                               if (!(inet_aton(word, &inp))) {
-                                       /* word was not ip, try again */
-                                       break;
-                               }
-                               range_p->first_ip = htonl(inp.s_addr) - 1;
-                               argument = 2;
-                               break;
                        case 4:
                                /* printf ("include file: %s\n", word); */
                                argument = 0;
                                next_free_shared_name =
-                                   parse_config(false, word, current_shared_name,
+                                   parse_config(false, word,
+                                                current_shared_name,
                                                 next_free_shared_name,
                                                 shared_p);
                                newclause = true;
                                break;
+                       case 0:
+                               /* printf ("nothing interesting: %s\n", word); */
+                               argument = 0;
+                               break;
                        default:
                                eprintf
                                    ("parse_config: This cannot happen, report a bug!");