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 	uint32_t pad;
131 };
132 
133 /*! \struct range_t
134  * \brief Counters for an individual range.
135  */
136 struct range_t {
137 	struct shared_network_t *shared_net;
138 	union ipaddr_t first_ip;
139 	union ipaddr_t last_ip;
140 	double count;
141 	double touched;
142 	double backups;
143 };
144 
145 /*! \struct output_helper_t
146  * \brief Various per range and shared net temporary calculation results.
147  */
148 struct output_helper_t {
149 	double range_size;
150 	double percent;
151 	double tc;
152 	double tcp;
153 	double bup;
154 	int status;
155 	uint32_t pad;
156 };
157 
158 /*! \struct status_counts_t
159  * \brief Range and shared network alarming status counts.
160  */
161 struct status_counts_t {
162 	unsigned int warning;
163 	unsigned int critical;
164 	unsigned int ok;
165 	unsigned int ignored;
166 };
167 
168 /*! \enum ltype
169  * \brief Lease state types.  These are the possible values in struct leases_t.
170  */
171 enum ltype {
172 	ACTIVE,
173 	FREE,
174 	BACKUP
175 };
176 
177 /*! \struct leases_t
178  * \brief An individual lease. These leaases are hashed.
179  */
180 struct leases_t {
181 	union ipaddr_t ip;	/* ip as key */
182 	char *ethernet;
183 	UT_hash_handle hh;
184 	enum ltype type;
185 	uint32_t pad;
186 };
187 
188 /*! \enum limbits
189  * \brief Output limit bits.
190  */
191 enum limbits {
192 	R_BIT = (1 << 0),	/*!< Range limit. */
193 	S_BIT = (1 << 1),	/*!< Shared networks limit. */
194 	A_BIT = (1 << 2)	/*!< All networks summary limit. */
195 };
196 
197 /*! \def STATE_OK
198  * \brief Nagios alarm exit value.
199  */
200 # define STATE_OK 0
201 # define STATE_WARNING 1
202 # define STATE_CRITICAL 2
203 
204 /*! \var comparer_t
205  * \brief Function pointer holding sort algorithm.
206  */
207 typedef int (*comparer_t) (struct range_t *r1, struct range_t *r2);
208 
209 /*! \struct output_sort
210  * \brief Linked list of sort functions.
211  */
212 struct output_sort {
213 	comparer_t func;
214 	struct output_sort *next;
215 };
216 
217 /*! \struct conf_t
218  * \brief Runtime configuration state.
219  */
220 struct conf_t {
221 	struct shared_network_t *shared_net_root;	/*!< First entry in shared network linked list, that is the 'all networks', */
222 	struct shared_network_t *shared_net_head;	/*!< Last entry in shared network linked list.  */
223 	struct range_t *ranges;				/*!< Array of ranges. */
224 	unsigned int num_ranges;			/*!< Number of ranges in the ranges array. */
225 	enum dhcp_version ip_version;			/*!< Designator if the dhcpd is running in IPv4 or IPv6 mode. */
226 	size_t ranges_size;				/*!< Size of the ranges array. */
227 	struct leases_t *leases;			/*!< An array of individual leases from dhcpd.leases file. */
228 	const char *dhcpdconf_file;			/*!< Path to dhcpd.conf file. */
229 	const char *dhcpdlease_file;			/*!< Path to dhcpd.leases file. */
230 	struct output_sort *sorts;			/*!< Linked list how to sort ranges. */
231 	const char *output_file;			/*!< Output file path. */
232 	const char *mustach_template;			/*!< Mustach template file path. */
233 	double warning;					/*!< Warning percent threshold. */
234 	double critical;				/*!< Critical percent threshold. */
235 	double warn_count;				/*!< Maximum number of free IP's before warning. */
236 	double crit_count;				/*!< Maximum number of free IP's before critical. */
237 	double minsize;					/*!< Minimum size of range or shared network to be considered exceeding threshold. */
238 	int color_format;				/*!< Column to use in color_tags array. */
239 	char output_format;				/*!< Output format, such as text, json, xml, .... */
240 	uint32_t
241 		print_mac_addreses:1,			/*!< Print mac address in xml or json. */
242 		reverse_order:1,			/*!< Reverse sort order. */
243 		backups_found:1,			/*!< Indicator if dhcpd.leases file has leases in backup state. */
244 		snet_alarms:1,				/*!< Suppress alarming thresholds for ranges that are part of a shared network. */
245 		perfdata:1,				/*!< Include performance statistics when using Nagios alarm output format. */
246 		all_as_shared:1,			/*!< Treat stand-alone subnets as a shared network. */
247 		header_limit:4,				/*!< Bits to suppress header output. */
248 		number_limit:3,				/*!< Bits to suppress value output. */
249 		skip_ok:1,				/*!< Skip none-alarming values from output. */
250 		skip_warning:1,				/*!< Skip warning values from output. */
251 		skip_critical:1,			/*!< Skip critical values from output. */
252 		skip_minsize:1,				/*!< Skip alarming values that are below minsize from output. */
253 		skip_suppressed:1,			/*!< Skip alarming values that are suppressed with --snet-alarms option, or they are shared networks without IP availability. */
254 		color_mode:2,				/*!< Indicator if colors should be used in output. */
255 		pad_bits:4;
256 };
257 
258 /* Function prototypes */
259 
260 /* analyze.c */
261 extern void prepare_data(struct conf_t *state);
262 extern void do_counting(struct conf_t *state);
263 
264 /* getdata.c */
265 extern int parse_leases(struct conf_t *state);
266 extern void parse_config(struct conf_t *state, const int is_include,
267 			 const char *restrict config_file,
268 			 struct shared_network_t *restrict shared_p);
269 
270 /* hash.c */
271 extern void (*add_lease) (struct conf_t *state, union ipaddr_t *addr, enum ltype type);
272 extern void add_lease_init(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
273 extern void add_lease_v4(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
274 extern void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
275 
276 extern struct leases_t *(*find_lease) (struct conf_t *state, union ipaddr_t *addr);
277 extern struct leases_t *find_lease_init(struct conf_t *state, union ipaddr_t *addr);
278 extern struct leases_t *find_lease_v4(struct conf_t *state, union ipaddr_t *addr);
279 extern struct leases_t *find_lease_v6(struct conf_t *state, union ipaddr_t *addr);
280 
281 extern void delete_lease(struct conf_t *state, struct leases_t *lease);
282 extern void delete_all_leases(struct conf_t *state);
283 
284 /* mustach-dhcpd-pools.c */
285 extern int mustach_dhcpd_pools(struct conf_t *state);
286 
287 /* other.c */
288 extern void set_ipv_functions(struct conf_t *state, int version);
289 extern void flip_ranges(struct conf_t *state);
290 extern void clean_up(struct conf_t *state);
291 extern void parse_cidr(struct conf_t *state, struct range_t *range_p, const char *word);
292 extern int parse_color_mode(const char *restrict arg);
293 extern double strtod_or_err(const char *restrict str, const char *restrict errmesg);
294 extern void __attribute__ ((noreturn)) print_version(void);
295 extern void __attribute__ ((noreturn)) usage(int status);
296 extern void dp_time_tool(FILE *file, const char *path, int epoch);
297 
298 extern int (*parse_ipaddr) (struct conf_t *state, const char *restrict src,
299 			    union ipaddr_t *restrict dst);
300 extern int parse_ipaddr_init(struct conf_t *state, const char *restrict src,
301 			     union ipaddr_t *restrict dst);
302 extern int parse_ipaddr_v4(struct conf_t *state, const char *restrict src,
303 			   union ipaddr_t *restrict dst);
304 extern int parse_ipaddr_v6(struct conf_t *state, const char *restrict src,
305 			   union ipaddr_t *restrict dst);
306 
307 extern int (*xstrstr) (struct conf_t *state, const char *restrict str);
308 extern int xstrstr_init(struct conf_t *state, const char *restrict str);
309 extern int xstrstr_v4(struct conf_t *state, const char *restrict str);
310 extern int xstrstr_v6(struct conf_t *state, const char *restrict str);
311 
312 extern void (*copy_ipaddr) (union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
313 extern void copy_ipaddr_init(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
314 extern void copy_ipaddr_v4(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
315 extern void copy_ipaddr_v6(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
316 
317 extern const char *(*ntop_ipaddr) (const union ipaddr_t *ip);
318 extern const char *ntop_ipaddr_init(const union ipaddr_t *ip);
319 extern const char *ntop_ipaddr_v4(const union ipaddr_t *ip);
320 extern const char *ntop_ipaddr_v6(const union ipaddr_t *ip);
321 
322 extern double (*get_range_size) (const struct range_t *r);
323 extern double get_range_size_init(const struct range_t *r);
324 extern double get_range_size_v4(const struct range_t *r);
325 extern double get_range_size_v6(const struct range_t *r);
326 
327 /* output.c */
328 extern int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
329 			       struct range_t *range_p);
330 extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
331 			       struct shared_network_t *shared_p);
332 extern int output_analysis(struct conf_t *state);
333 extern void range_alarms(struct conf_t *state, struct status_counts_t *rangstat);
334 extern void shared_net_alarms(struct conf_t *state, struct status_counts_t *sharstat);
335 
336 /* sort.c */
337 extern void mergesort_ranges(struct conf_t *state,
338 			     struct range_t *restrict orig, unsigned int size,
339 			     struct range_t *restrict temp, const int root_call);
340 
341 extern int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b);
342 extern int leasecomp_init(const struct leases_t *restrict a
343 			  __attribute__ ((unused)),
344 			  const struct leases_t *restrict b __attribute__ ((unused)));
345 extern int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b);
346 extern int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b);
347 
348 extern int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
349 extern int ipcomp_init(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
350 extern int ipcomp_v4(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
351 extern int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
352 
353 extern int rangecomp(const void *restrict r1, const void *restrict r2)
354     __attribute__ ((nonnull(1, 2)));
355 
356 extern int comp_cur(struct range_t *r1, struct range_t *r2);
357 extern int comp_double(double f1, double f2);
358 extern int comp_ip(struct range_t *r1, struct range_t *r2);
359 extern int comp_max(struct range_t *r1, struct range_t *r2);
360 extern int comp_percent(struct range_t *r1, struct range_t *r2);
361 extern int comp_tc(struct range_t *r1, struct range_t *r2);
362 extern int comp_tcperc(struct range_t *r1, struct range_t *r2);
363 extern int comp_touched(struct range_t *r1, struct range_t *r2);
364 
365 extern comparer_t field_selector(char c);
366 extern double ret_percent(struct range_t r);
367 extern double ret_tc(struct range_t r);
368 extern double ret_tcperc(struct range_t r);
369 
370 #endif /* DHCPD_POOLS_H */
371