Debian Codegraph @

src/analyze.c

raw symbols SCIP
1 /*
2 * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3 * BSD License" or "FreeBSD License".
4 *
5 * Copyright 2006- Sami Kerola. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
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.
34 */
35
36 /*! \file analyze.c
37 * \brief Data analysis functions.
38 */
39
40 #include <config.h>
41
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <stdlib.h>
45
46 #include "dhcpd-pools.h"
47
48 /*! \brief Prepare data for analysis. The function will sort leases and
49 * ranges. */
50 void prepare_data(struct conf_t *state)
51 {
52 /* Sort leases */
53 HASH_SORT(state->leases, leasecomp);
54 /* Sort ranges */
55 qsort(state->ranges, state->num_ranges, sizeof(struct range_t), &rangecomp);
56 }
57
58 /*!\brief Perform counting. Join leases with ranges, and update range and
59 * shared network counters. */
60 void do_counting(struct conf_t *state)
61 {
62 struct range_t *restrict range_p = state->ranges;
63 const struct leases_t *restrict l = state->leases;
64 unsigned long i;
65 double block_size;
66
67 /* Walk through ranges */
68 for (i = 0; i < state->num_ranges; i++, range_p++) {
69 while (l != NULL && ipcomp(&range_p->first_ip, &l->ip) < 0)
70 l = l->hh.prev; /* rewind */
71 if (l == NULL)
72 l = state->leases;
73 for (; l != NULL && ipcomp(&l->ip, &range_p->last_ip) <= 0; l = l->hh.next) {
74 if (unlikely(ipcomp(&l->ip, &range_p->first_ip) < 0))
75 continue; /* cannot happen? */
76 /* IP in range */
77 switch (l->type) {
78 case FREE:
79 range_p->touched++;
80 break;
81 case ACTIVE:
82 range_p->count++;
83 break;
84 case BACKUP:
85 range_p->backups++;
86 break;
87 default:
88 abort();
89 }
90 }
91 /* Size of range size. */
92 block_size = get_range_size(range_p);
93 /* Count together ranges within shared network block. */
94 range_p->shared_net->available += block_size;
95 range_p->shared_net->used += range_p->count;
96 range_p->shared_net->touched += range_p->touched;
97 range_p->shared_net->backups += range_p->backups;
98 /* When shared network is not 'all networks' add it as well. */
99 if (range_p->shared_net != state->shared_net_root) {
100 state->shared_net_root->available += block_size;
101 state->shared_net_root->used += range_p->count;
102 state->shared_net_root->touched += range_p->touched;
103 state->shared_net_root->backups += range_p->backups;
104 }
105 }
106 }