Version 2.1

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2006-03-13 00:00:00 +02:00
parent 74aef1c34e
commit 61b6acf2f5
52 changed files with 41412 additions and 9588 deletions

View file

@ -1,5 +1,5 @@
/*
** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
** 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
@ -25,137 +25,94 @@
#include "dhcpd-pools.h"
/* Clean up data */
int prepare_data(void)
/* A simple (predicate-like) integer comparison function (for qsort) */
int
intcomp (const void *x, const void *y)
{
unsigned long int i, j, k;
if (*(unsigned long int *) x < *(unsigned long int *) y)
return -1;
else if (*(unsigned long int *) y < *(unsigned long int *) x)
return 1;
else
return 0;
}
/* Sort leases */
qsort(leases, (size_t) num_leases, sizeof(long int), &intcomp);
/* A simple (predicate-like) range structure comparison function
* (for qsort) */
int
rangecomp (const void *r1, const void *r2)
{
if ((((struct range_t *) r1)->first_ip) <
(((struct range_t *) r2)->first_ip))
return -1;
else if ((((struct range_t *) r2)->first_ip) <
(((struct range_t *) r1)->first_ip))
return 1;
else
return 0;
}
/* Get rid of lease dublicates */
for (k = j = i = 0; i < num_leases; i++) {
if (j != leases[i]) {
leases[k] = leases[i];
j = leases[i];
k++;
}
/* Clean up data */
int
prepare_data (void)
{
unsigned long int i, j, k;
/* Sort leases */
qsort (leases, (size_t) num_leases, sizeof (long int), &intcomp);
/* Get rid of dublicates */
for (k = j = i = 0; i < num_leases; i++)
{
if (j != leases[i])
{
leases[k] = leases[i];
j = leases[i];
k++;
}
num_leases = k;
}
num_leases = k;
/* Delete touched IPs that are in use. */
j = 0;
for (i = 0; i < num_touches; i++) {
if (bsearch
(&touches[i], leases, (size_t) num_leases,
sizeof(long int), &intcomp) == NULL) {
touches[j] = touches[i];
j++;
}
}
num_touches = j;
qsort(touches, (size_t) num_touches, sizeof(long int), &intcomp);
/* Sort ranges */
qsort (ranges, (size_t) num_ranges, sizeof (struct range_t), &rangecomp);
/* Sort ranges */
qsort(ranges, (size_t) num_ranges, sizeof(struct range_t),
&rangecomp);
/* Sort backups */
if (num_backups > 0) {
qsort(backups, (size_t) num_backups, sizeof(long int),
&intcomp);
}
return 0;
return 0;
}
/* Join leases and ranges into couter structs */
int do_counting(void)
int
do_counting (void)
{
struct range_t *range_p;
unsigned int i, j, k, m, block_size;
struct range_t *range_p;
unsigned int i, block_size;
i = j = m = 0;
range_p = ranges;
range_p = ranges;
/* Walk through ranges */
for (k = 0; k < num_ranges; k++) {
/* Count IPs in use */
for (; range_p->last_ip > leases[i]
&& (unsigned long) i < num_leases; i++) {
if (range_p->first_ip > leases[i]) {
continue;
}
/* IP with in range */
range_p->count++;
if (range_p->shared_net) {
range_p->shared_net->used++;
}
}
/* Count touched IPs */
for (; range_p->last_ip > touches[j]
&& (unsigned long) j < num_touches; j++) {
if (range_p->first_ip > touches[j]) {
continue;
}
/* IP with in range */
range_p->touched++;
if (range_p->shared_net) {
range_p->shared_net->touched++;
}
}
/* Count backup IPs */
if (num_backups > 0) {
for (; range_p->last_ip > backups[m]
&& (unsigned long) m < num_touches; m++) {
if (range_p->first_ip > touches[m]) {
continue;
}
/* IP with in range */
range_p->backups++;
if (range_p->shared_net) {
range_p->shared_net->backups++;
}
}
}
/* Size of range, shared net & all networks */
block_size =
(unsigned int) (range_p->last_ip - range_p->first_ip -
1);
if (range_p->shared_net) {
range_p->shared_net->available += block_size;
}
/* Reverse so that not even a one IP will be missed. */
if (i) {
i--;
}
if (j) {
j--;
}
range_p++;
for (i = 0; i < num_leases; i++)
{
/* IP with in range */
if (range_p->first_ip < leases[i] && range_p->last_ip > leases[i])
{
range_p->count++;
if (range_p->shared_net)
{
range_p->shared_net->used++;
}
shared_networks->used++;
}
/* IP out of range */
if (range_p->last_ip < leases[i])
{
range_p++;
block_size = range_p->last_ip - range_p->first_ip - 1;
if (range_p->shared_net)
{
range_p->shared_net->available += block_size;
}
shared_networks->available += block_size;
i--;
}
/* During count of other shared networks default network and
* all networks got mixed to gether semantically. This fixes
* the problem, but is not elegant. TODO: fix semantics of all
* and default share_network. */
shared_networks->available = 0;
shared_networks->used = 0;
shared_networks->touched = 0;
range_p = ranges;
for (k = 0; k < num_ranges; k++) {
shared_networks->available +=
range_p->last_ip - range_p->first_ip - 1;
shared_networks->used += range_p->count;
shared_networks->touched += range_p->touched;
shared_networks->backups += range_p->backups;
range_p++;
}
return 0;
}
return 0;
}