Version 2.8

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2008-06-21 00:00:00 +02:00
parent 625cde6b9c
commit 40edcd2aba
16 changed files with 303 additions and 76 deletions

View file

@ -59,7 +59,7 @@ parse_leases (void)
struct in_addr inp;
struct stat lease_file_stats;
num_touches = num_leases = 0;
num_touches = num_leases = num_backups = 0;
dhcpd_leases = fopen (config.dhcpdlease_file, "r");
if (dhcpd_leases == NULL)
@ -69,10 +69,9 @@ parse_leases (void)
}
/* I found out that there's one lease address per 300 bytes in
* dhcpd.leases file. Malloc is little bit pessimistic, but I
* think that this way program crashes slightly less frequetly.
* If someone has higher density in lease file I'm interested
* to hear about that. */
* dhcpd.leases file. Malloc is little bit pessimistic and uses
* 250. If someone has higher density in lease file I'm
* interested to hear about that. */
if (stat (config.dhcpdlease_file, &lease_file_stats))
{
eprintf ("parse_leases: %s:", config.dhcpdlease_file);
@ -97,20 +96,33 @@ parse_leases (void)
strncpy (ipstring, line, (size_t) MAXLEN - 1);
}
/* And this is lease state which we are interested about */
if (strstr (line, "binding state active"))
else if (strstr (line, "binding state active"))
{
nth_field (2, ipstring, ipstring);
inet_aton (ipstring, &inp);
leases[num_leases] = htonl (inp.s_addr);
num_leases++;
}
if (strstr (line, " binding state free"))
else if (strstr (line, " binding state free"))
{
nth_field (2, ipstring, ipstring);
inet_aton (ipstring, &inp);
touches[num_touches] = htonl (inp.s_addr);
num_touches++;
}
else if (strstr (line, " binding state backup"))
{
nth_field (2, ipstring, ipstring);
inet_aton (ipstring, &inp);
if (num_backups == 0)
{
backups =
safe_malloc ((size_t) sizeof (long int) *
((lease_file_stats.st_size / 250) + MAXLEN));
}
backups[num_backups] = htonl (inp.s_addr);
num_backups++;
}
}
return 0;
@ -121,7 +133,6 @@ parse_leases (void)
* have. Question of semantics, send mail to author if this
* annoys. All performance boosts for this function are well
* come. */
int
nth_field (int n, char *dest, const char *src)
{
@ -176,13 +187,15 @@ is_interesting_config_clause (char *s)
}
}
/* This function is far too long. */
/* TODO: This spagetti monster function need to be rewrote at
* least ones. */
int
parse_config (char *config_file, char *current_shared_name,
char *next_free_shared_name, struct shared_network_t *shared_p)
{
FILE *dhcpd_config;
int i = 0, newclause = 1, argument = 0, comment = 0, braces = 0, quote = 0;
int i = 0, newclause = true, argument = false, comment = false, braces =
0, quote = false;
char word[MAXLEN], c;
int braces_shared = 1000;
struct in_addr inp;
@ -203,7 +216,7 @@ parse_config (char *config_file, char *current_shared_name,
exit (EXIT_FAILURE);
}
/* Hairy stuff begins. */
/* Very hairy stuff begins. */
while (!feof (dhcpd_config))
{
c = fgetc (dhcpd_config);
@ -243,6 +256,16 @@ parse_config (char *config_file, char *current_shared_name,
newclause = true;
i = 0;
}
else if (argument == 2)
{
/* Range ends to ; and this hair in code make two
* ranges wrote to gether like...
*
* range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
*
* ...to be interpreted correctly. */
c = ' ';
}
continue;
case '{':
if (quote == true)
@ -252,6 +275,12 @@ parse_config (char *config_file, char *current_shared_name,
if (comment == false)
{
braces++;
}
/* i == 0 detects word that ends to brace like:
*
* shared-network DSL{ ... */
if (i == 0)
{
newclause = true;
}
continue;
@ -285,7 +314,7 @@ parse_config (char *config_file, char *current_shared_name,
continue;
}
/* Strip white spaces before new clause word. */
if ((newclause == true || argument != 0) && isspace (c) && i == false)
if ((newclause == true || argument != 0) && isspace (c) && i == 0)
{
continue;
}
@ -337,6 +366,7 @@ parse_config (char *config_file, char *current_shared_name,
shared_p->available = 0;
shared_p->used = 0;
shared_p->touched = 0;
shared_p->backups = 0;
/* Temporary abuse of argument variable */
argument = strlen (next_free_shared_name) + 1;
if (last_shared_name > next_free_shared_name + argument)
@ -359,6 +389,7 @@ parse_config (char *config_file, char *current_shared_name,
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)