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/>.
25 #include "dhcpd-pools.h"
28 int prepare_data(void)
30 unsigned long int i, j, k;
33 qsort(leases, (size_t) num_leases, sizeof(long int), &intcomp);
35 /* Get rid of lease dublicates */
36 for (k = j = i = 0; i < num_leases; i++) {
38 leases[k] = leases[i];
45 /* Delete touched IPs that are in use. */
47 for (i = 0; i < num_touches; i++) {
49 (&touches[i], leases, (size_t) num_leases,
50 sizeof(long int), &intcomp) == NULL) {
51 touches[j] = touches[i];
56 qsort(touches, (size_t) num_touches, sizeof(long int), &intcomp);
59 qsort(ranges, (size_t) num_ranges, sizeof(struct range_t),
63 if (0 < num_backups) {
64 qsort(backups, (size_t) num_backups, sizeof(long int),
71 /* Join leases and ranges into couter structs */
74 struct range_t *range_p;
75 unsigned int i, j, k, m, block_size;
80 /* Walk through ranges */
81 for (k = 0; k < num_ranges; k++) {
82 /* Count IPs in use */
83 for (; leases[i] < range_p->last_ip
84 && (unsigned long) i < num_leases; i++) {
85 if (leases[i] < range_p->first_ip) {
88 /* IP with in range */
90 if (range_p->shared_net) {
91 range_p->shared_net->used++;
95 /* Count touched IPs */
96 for (; touches[j] < range_p->last_ip
97 && (unsigned long) j < num_touches; j++) {
98 if (touches[j] < range_p->first_ip) {
101 /* IP with in range */
103 if (range_p->shared_net) {
104 range_p->shared_net->touched++;
108 /* Count backup IPs */
109 if (0 < num_backups) {
110 for (; backups[m] < range_p->last_ip
111 && (unsigned long) m < num_touches; m++) {
112 if (touches[m] < range_p->first_ip) {
115 /* IP with in range */
117 if (range_p->shared_net) {
118 range_p->shared_net->backups++;
123 /* Size of range, shared net & all networks */
125 (unsigned int) (range_p->last_ip - range_p->first_ip -
127 if (range_p->shared_net) {
128 range_p->shared_net->available += block_size;
131 /* Go backwards one step so that not even a one IP will be
132 * missed. This is possibly always unnecessary. */
143 /* FIXME: During count of other shared networks default network and
144 * all networks got mixed to gether semantically. This fixes the
145 * problem, but is not elegant. */
146 shared_networks->available = 0;
147 shared_networks->used = 0;
148 shared_networks->touched = 0;
150 for (k = 0; k < num_ranges; k++) {
151 shared_networks->available +=
152 range_p->last_ip - range_p->first_ip - 1;
153 shared_networks->used += range_p->count;
154 shared_networks->touched += range_p->touched;
155 shared_networks->backups += range_p->backups;