2 * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3 * BSD License" or "FreeBSD License".
5 * Copyright 2006- Sami Kerola. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing
33 * official policies, either expressed or implied, of Sami Kerola.
39 #include "dhcpd-pools.h"
42 int prepare_data(void)
44 unsigned long int i, j, k;
47 qsort(leases, (size_t) num_leases, sizeof(long int), &intcomp);
49 /* Get rid of lease dublicates */
50 for (k = j = i = 0; i < num_leases; i++) {
52 leases[k] = leases[i];
59 /* Delete touched IPs that are in use. */
61 for (i = 0; i < num_touches; i++) {
63 (&touches[i], leases, (size_t) num_leases,
64 sizeof(long int), &intcomp) == NULL) {
65 touches[j] = touches[i];
70 qsort(touches, (size_t) num_touches, sizeof(long int), &intcomp);
73 qsort(ranges, (size_t) num_ranges, sizeof(struct range_t),
77 if (0 < num_backups) {
78 qsort(backups, (size_t) num_backups, sizeof(long int),
85 /* Join leases and ranges into couter structs */
88 struct range_t *range_p;
89 unsigned int i, j, k, m, block_size;
94 /* Walk through ranges */
95 for (k = 0; k < num_ranges; k++) {
96 /* Count IPs in use */
97 for (; leases[i] < range_p->last_ip
98 && (unsigned long) i < num_leases; i++) {
99 if (leases[i] < range_p->first_ip) {
102 /* IP with in range */
104 if (range_p->shared_net) {
105 range_p->shared_net->used++;
109 /* Count touched IPs */
110 for (; touches[j] < range_p->last_ip
111 && (unsigned long) j < num_touches; j++) {
112 if (touches[j] < range_p->first_ip) {
115 /* IP with in range */
117 if (range_p->shared_net) {
118 range_p->shared_net->touched++;
122 /* Count backup IPs */
123 if (0 < num_backups) {
124 for (; backups[m] < range_p->last_ip
125 && (unsigned long) m < num_touches; m++) {
126 if (touches[m] < range_p->first_ip) {
129 /* IP with in range */
131 if (range_p->shared_net) {
132 range_p->shared_net->backups++;
137 /* Size of range, shared net & all networks */
139 (unsigned int) (range_p->last_ip - range_p->first_ip -
141 if (range_p->shared_net) {
142 range_p->shared_net->available += block_size;
145 /* Go backwards one step so that not even a one IP will be
146 * missed. This is possibly always unnecessary. */
157 /* FIXME: During count of other shared networks default network and
158 * all networks got mixed to gether semantically. This fixes the
159 * problem, but is not elegant. */
160 shared_networks->available = 0;
161 shared_networks->used = 0;
162 shared_networks->touched = 0;
164 for (k = 0; k < num_ranges; k++) {
165 shared_networks->available +=
166 range_p->last_ip - range_p->first_ip - 1;
167 shared_networks->used += range_p->count;
168 shared_networks->touched += range_p->touched;
169 shared_networks->backups += range_p->backups;