Debian Codegraph @

src/dhcpd-pools.h

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 dhcpd-pools.h
37 * \brief Global definitions of structures, enums, and function prototypes.
38 */
39
40 #ifndef DHCPD_POOLS_H
41 # define DHCPD_POOLS_H 1
42
43 # include <config.h>
44 # include <arpa/inet.h>
45 # include <stddef.h>
46 # include <stdio.h>
47 # include <string.h>
48 # include <uthash.h>
49
50 /*! \def likely(x)
51 * \brief Symbolic call to __builtin_expect'ed branch.
52 */
53 /*! \def unlikely(x)
54 * \brief Symbolic call to not-__builtin_expect'ed branch.
55 */
56 # ifdef HAVE_BUILTIN_EXPECT
57 # define likely(x) __builtin_expect(!!(x), 1)
58 # define unlikely(x) __builtin_expect(!!(x), 0)
59 # else
60 # define likely(x) (x)
61 # define unlikely(x) (x)
62 # endif
63
64 /*! \def _DP_ATTRIBUTE_HOT
65 * \brief The function attribute __hot__ was added in gcc 4.3. See gnu
66 * documentation for further information.
67 * https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-hot-function-attribute
68 */
69 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
70 # define _DP_ATTRIBUTE_HOT __attribute__ ((__hot__))
71 # else
72 # define _DP_ATTRIBUTE_HOT /* empty */
73 # endif
74
75 /*! \union ipaddr_t
76 * \brief Memory space for a binary IP address saving. */
77 union ipaddr_t {
78 uint32_t v4;
79 unsigned char v6[16];
80 };
81
82 /*! \enum dhcp_version
83 * \brief The IP version, IPv4 or IPv6, served by the dhcpd.
84 */
85 enum dhcp_version {
86 IPvUNKNOWN,
87 IPv4,
88 IPv6
89 };
90
91 /*! \enum prefix_t
92 * \brief Enumeration of interesting data in dhcpd.leases file, that has to
93 * be further examined, and saved. Functions xstrstr_v4() and xstrstr_v6()
94 * return one of these values to parse_leases().
95 */
96 enum prefix_t {
97 PREFIX_LEASE,
98 PREFIX_BINDING_STATE_FREE,
99 PREFIX_BINDING_STATE_ABANDONED,
100 PREFIX_BINDING_STATE_EXPIRED,
101 PREFIX_BINDING_STATE_RELEASED,
102 PREFIX_BINDING_STATE_ACTIVE,
103 PREFIX_BINDING_STATE_BACKUP,
104 PREFIX_HARDWARE_ETHERNET,
105 NUM_OF_PREFIX
106 };
107
108 /*! \enum color_mode
109 * \brief Enumeration whether to use or not color output.
110 */
111 enum color_mode {
112 color_unknown,
113 color_off,
114 color_on,
115 color_auto /*!< Default, use colors when output terminal is interactive. */
116 };
117
118 /*! \struct shared_network_t
119 * \brief Counters for an individual shared network. This data entry is
120 * also used for 'all networks' counting.
121 */
122 struct shared_network_t {
123 char *name;
124 double available;
125 double used;
126 double touched;
127 double backups;
128 struct shared_network_t *next;
129 int netmask;
130 };
131
132 /*! \struct range_t
133 * \brief Counters for an individual range.
134 */
135 struct range_t {
136 struct shared_network_t *shared_net;
137 union ipaddr_t first_ip;
138 union ipaddr_t last_ip;
139 double count;
140 double touched;
141 double backups;
142 };
143
144 /*! \struct output_helper_t
145 * \brief Various per range and shared net temporary calculation results.
146 */
147 struct output_helper_t {
148 double range_size;
149 double percent;
150 double tc;
151 double tcp;
152 double bup;
153 int status;
154 };
155
156 /*! \struct status_counts_t
157 * \brief Range and shared network alarming status counts.
158 */
159 struct status_counts_t {
160 unsigned int warning;
161 unsigned int critical;
162 unsigned int ok;
163 unsigned int ignored;
164 };
165
166 /*! \enum ltype
167 * \brief Lease state types. These are the possible values in struct leases_t.
168 */
169 enum ltype {
170 ACTIVE,
171 FREE,
172 BACKUP
173 };
174
175 /*! \struct leases_t
176 * \brief An individual lease. These leaases are hashed.
177 */
178 struct leases_t {
179 union ipaddr_t ip; /* ip as key */
180 char *ethernet;
181 UT_hash_handle hh;
182 enum ltype type;
183 };
184
185 /*! \enum limbits
186 * \brief Output limit bits.
187 */
188 enum limbits {
189 R_BIT = (1 << 0), /*!< Range limit. */
190 S_BIT = (1 << 1), /*!< Shared networks limit. */
191 A_BIT = (1 << 2) /*!< All networks summary limit. */
192 };
193
194 /*! \def STATE_OK
195 * \brief Nagios alarm exit value.
196 */
197 # define STATE_OK 0
198 # define STATE_WARNING 1
199 # define STATE_CRITICAL 2
200
201 /*! \var comparer_t
202 * \brief Function pointer holding sort algorithm.
203 */
204 typedef int (*comparer_t) (struct range_t *r1, struct range_t *r2);
205
206 /*! \struct output_sort
207 * \brief Linked list of sort functions.
208 */
209 struct output_sort {
210 comparer_t func;
211 struct output_sort *next;
212 };
213
214 /*! \struct conf_t
215 * \brief Runtime configuration state.
216 */
217 struct conf_t {
218 struct shared_network_t *shared_net_root; /*!< First entry in shared network linked list, that is the 'all networks', */
219 struct shared_network_t *shared_net_head; /*!< Last entry in shared network linked list. */
220 struct range_t *ranges; /*!< Array of ranges. */
221 unsigned int num_ranges; /*!< Number of ranges in the ranges array. */
222 enum dhcp_version ip_version; /*!< Designator if the dhcpd is running in IPv4 or IPv6 mode. */
223 size_t ranges_size; /*!< Size of the ranges array. */
224 struct leases_t *leases; /*!< An array of individual leases from dhcpd.leases file. */
225 const char *dhcpdconf_file; /*!< Path to dhcpd.conf file. */
226 const char *dhcpdlease_file; /*!< Path to dhcpd.leases file. */
227 struct output_sort *sorts; /*!< Linked list how to sort ranges. */
228 const char *output_file; /*!< Output file path. */
229 const char *mustach_template; /*!< Mustach template file path. */
230 double warning; /*!< Warning percent threshold. */
231 double critical; /*!< Critical percent threshold. */
232 double warn_count; /*!< Maximum number of free IP's before warning. */
233 double crit_count; /*!< Maximum number of free IP's before critical. */
234 double minsize; /*!< Minimum size of range or shared network to be considered exceeding threshold. */
235 int color_format; /*!< Column to use in color_tags array. */
236 char output_format; /*!< Output format, such as text, json, xml, .... */
237 uint32_t
238 print_mac_addreses:1, /*!< Print mac address in xml or json. */
239 reverse_order:1, /*!< Reverse sort order. */
240 backups_found:1, /*!< Indicator if dhcpd.leases file has leases in backup state. */
241 snet_alarms:1, /*!< Suppress alarming thresholds for ranges that are part of a shared network. */
242 perfdata:1, /*!< Include performance statistics when using Nagios alarm output format. */
243 all_as_shared:1, /*!< Treat stand-alone subnets as a shared network. */
244 header_limit:4, /*!< Bits to suppress header output. */
245 number_limit:3, /*!< Bits to suppress value output. */
246 skip_ok:1, /*!< Skip none-alarming values from output. */
247 skip_warning:1, /*!< Skip warning values from output. */
248 skip_critical:1, /*!< Skip critical values from output. */
249 skip_minsize:1, /*!< Skip alarming values that are below minsize from output. */
250 skip_suppressed:1, /*!< Skip alarming values that are suppressed with --snet-alarms option, or they are shared networks without IP availability. */
251 color_mode:2; /*!< Indicator if colors should be used in output. */
252 };
253
254 /* Function prototypes */
255
256 /* analyze.c */
257 extern void prepare_data(struct conf_t *state);
258 extern void do_counting(struct conf_t *state);
259
260 /* getdata.c */
261 extern int parse_leases(struct conf_t *state);
262 extern void parse_config(struct conf_t *state, const int is_include,
263 const char *restrict config_file,
264 struct shared_network_t *restrict shared_p);
265
266 /* hash.c */
267 extern void (*add_lease) (struct conf_t *state, union ipaddr_t *addr, enum ltype type);
268 extern void add_lease_init(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
269 extern void add_lease_v4(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
270 extern void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
271
272 extern struct leases_t *(*find_lease) (struct conf_t *state, union ipaddr_t *addr);
273 extern struct leases_t *find_lease_init(struct conf_t *state, union ipaddr_t *addr);
274 extern struct leases_t *find_lease_v4(struct conf_t *state, union ipaddr_t *addr);
275 extern struct leases_t *find_lease_v6(struct conf_t *state, union ipaddr_t *addr);
276
277 extern void delete_lease(struct conf_t *state, struct leases_t *lease);
278 extern void delete_all_leases(struct conf_t *state);
279
280 /* mustach-dhcpd-pools.c */
281 extern int mustach_dhcpd_pools(struct conf_t *state);
282
283 /* other.c */
284 extern void set_ipv_functions(struct conf_t *state, int version);
285 extern void flip_ranges(struct conf_t *state);
286 extern void clean_up(struct conf_t *state);
287 extern void parse_cidr(struct conf_t *state, struct range_t *range_p, const char *word);
288 extern int parse_color_mode(const char *restrict arg);
289 extern double strtod_or_err(const char *restrict str, const char *restrict errmesg);
290 extern void __attribute__ ((noreturn)) print_version(void);
291 extern void __attribute__ ((noreturn)) usage(int status);
292 extern void dp_time_tool(FILE *file, const char *path, int epoch);
293
294 extern int (*parse_ipaddr) (struct conf_t *state, const char *restrict src,
295 union ipaddr_t *restrict dst);
296 extern int parse_ipaddr_init(struct conf_t *state, const char *restrict src,
297 union ipaddr_t *restrict dst);
298 extern int parse_ipaddr_v4(struct conf_t *state, const char *restrict src,
299 union ipaddr_t *restrict dst);
300 extern int parse_ipaddr_v6(struct conf_t *state, const char *restrict src,
301 union ipaddr_t *restrict dst);
302
303 extern int (*xstrstr) (struct conf_t *state, const char *restrict str);
304 extern int xstrstr_init(struct conf_t *state, const char *restrict str);
305 extern int xstrstr_v4(struct conf_t *state, const char *restrict str);
306 extern int xstrstr_v6(struct conf_t *state, const char *restrict str);
307
308 extern void (*copy_ipaddr) (union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
309 extern void copy_ipaddr_init(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
310 extern void copy_ipaddr_v4(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
311 extern void copy_ipaddr_v6(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
312
313 extern const char *(*ntop_ipaddr) (const union ipaddr_t *ip);
314 extern const char *ntop_ipaddr_init(const union ipaddr_t *ip);
315 extern const char *ntop_ipaddr_v4(const union ipaddr_t *ip);
316 extern const char *ntop_ipaddr_v6(const union ipaddr_t *ip);
317
318 extern double (*get_range_size) (const struct range_t *r);
319 extern double get_range_size_init(const struct range_t *r);
320 extern double get_range_size_v4(const struct range_t *r);
321 extern double get_range_size_v6(const struct range_t *r);
322
323 /* output.c */
324 extern int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
325 struct range_t *range_p);
326 extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
327 struct shared_network_t *shared_p);
328 extern int output_analysis(struct conf_t *state);
329 extern void range_alarms(struct conf_t *state, struct status_counts_t *rangstat);
330 extern void shared_net_alarms(struct conf_t *state, struct status_counts_t *sharstat);
331
332 /* sort.c */
333 extern void mergesort_ranges(struct conf_t *state,
334 struct range_t *restrict orig, unsigned int size,
335 struct range_t *restrict temp, const int root_call);
336
337 extern int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b);
338 extern int leasecomp_init(const struct leases_t *restrict a
339 __attribute__ ((unused)),
340 const struct leases_t *restrict b __attribute__ ((unused)));
341 extern int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b);
342 extern int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b);
343
344 extern int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
345 extern int ipcomp_init(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
346 extern int ipcomp_v4(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
347 extern int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
348
349 extern int rangecomp(const void *restrict r1, const void *restrict r2)
350 __attribute__ ((nonnull(1, 2)));
351
352 extern int comp_cur(struct range_t *r1, struct range_t *r2);
353 extern int comp_double(double f1, double f2);
354 extern int comp_ip(struct range_t *r1, struct range_t *r2);
355 extern int comp_max(struct range_t *r1, struct range_t *r2);
356 extern int comp_percent(struct range_t *r1, struct range_t *r2);
357 extern int comp_tc(struct range_t *r1, struct range_t *r2);
358 extern int comp_tcperc(struct range_t *r1, struct range_t *r2);
359 extern int comp_touched(struct range_t *r1, struct range_t *r2);
360
361 extern comparer_t field_selector(char c);
362 extern double ret_percent(struct range_t r);
363 extern double ret_tc(struct range_t r);
364 extern double ret_tcperc(struct range_t r);
365
366 #endif /* DHCPD_POOLS_H */