parse_leases: exit at read error
[debian/dhcpd-pools.git] / src / dhcpd-pools.h
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 <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.
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 #ifndef DHCPD_POOLS_H
37 # define DHCPD_POOLS_H 1
38
39 #include <config.h>
40 #include <stddef.h>
41
42 /* Feature test switches */
43 #define _POSIX_SOURCE 1
44 #define POSIXLY_CORRECT 1
45
46 #ifdef  HAVE_STDLIB_H
47 #else
48 extern void exit();
49 extern char *malloc();
50 #define EXIT_FAILURE    1
51 #define EXIT_SUCCESS    0
52 #endif                          /* STDC_HEADERS */
53
54 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
55 #  ifdef HAVE___PROGNAME
56 extern char *__progname;
57 #    define program_invocation_short_name __progname
58 #  else                         /* HAVE___PROGNAME */
59 #    ifdef HAVE_GETEXECNAME
60 #      include <stdlib.h>
61 #      define program_invocation_short_name \
62        prog_inv_sh_nm_from_file(getexecname(), 0)
63 #    else                       /* HAVE_GETEXECNAME */
64 #      define program_invocation_short_name \
65        prog_inv_sh_nm_from_file(__FILE__, 1)
66 #    endif                      /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
67 static char prog_inv_sh_nm_buf[256];
68 static inline char *prog_inv_sh_nm_from_file(char *f, char stripext)
69 {
70         char *t;
71         if ((t = strrchr(f, '/')) != NULL) {
72                 t++;
73         } else {
74                 t = f;
75         }
76         strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
77         prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
78
79         if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) {
80                 *t = '\0';
81         }
82         return prog_inv_sh_nm_buf;
83 }
84 #  endif
85 #endif
86
87 /* Structures and unions */
88 struct configuration_t {
89         char *dhcpdconf_file;
90         char *dhcpdlease_file;
91         char output_format[2];
92         char sort[6];
93         int reverse_order;
94         char *output_file;
95         int output_limit[2];
96 };
97 struct shared_network_t {
98         char *name;
99         unsigned long int available;
100         unsigned long int used;
101         unsigned long int touched;
102         unsigned long int backups;
103 };
104 struct range_t {
105         struct shared_network_t *shared_net;
106         unsigned long int first_ip;
107         unsigned long int last_ip;
108         unsigned long int count;
109         unsigned long int touched;
110         unsigned long int backups;
111 };
112 struct macaddr_t {
113         char *ethernet;
114         char *ip;
115         struct macaddr_t *next;
116 };
117
118 /* Global variables */
119 static int const true = 1;
120 static int const false = 0;
121
122 struct configuration_t config;
123
124 static int const output_limit_bit_1 = 1;
125 static int const output_limit_bit_2 = 2;
126 static int const output_limit_bit_3 = 4;
127 unsigned int fullhtml;
128
129 struct shared_network_t *shared_networks;
130 unsigned int num_shared_networks;
131
132 struct range_t *ranges;
133 unsigned int num_ranges;
134
135 unsigned long int *leases;
136 unsigned long int num_leases;
137
138 unsigned long int *touches;
139 unsigned long int num_touches;
140
141 unsigned long int *backups;
142 unsigned long int num_backups;
143
144 struct macaddr_t *macaddr;
145
146 /* Function prototypes */
147 int prepare_memory(void);
148 int parse_leases(void);
149 void parse_config(int, char *, struct shared_network_t *)
150     __attribute__ ((nonnull(2, 3)));
151 int nth_field(int n, char *dest, const char *src)
152     __attribute__ ((nonnull(2, 3)))
153 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
154     __attribute__ ((__hot__))
155 #endif
156     ;
157 int prepare_data(void);
158 int do_counting(void);
159 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
160     __attribute__ ((nonnull(1, 2)));
161 /* General support functions */
162 void *safe_malloc(const size_t size)
163 #if __GNUC__ >= 3
164     __attribute__ ((__malloc__))
165 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
166     __attribute__ ((__alloc_size__((1))))
167 #endif
168 #endif
169     ;
170 void *safe_realloc(void *ptr, const size_t size);
171 char *safe_strdup(const char *str) __attribute__ ((nonnull(1)));
172 void print_version(void) __attribute__ ((noreturn));
173 void usage(int status) __attribute__ ((noreturn));
174 /* qsort required functions... */
175 /* ...for ranges and... */
176 int intcomp(const void *x, const void *y) __attribute__ ((nonnull(1, 2)));
177 int rangecomp(const void *r1, const void *r2)
178     __attribute__ ((nonnull(1, 2)));
179 /* sort function pointer and functions */
180 int sort_name(void);
181 unsigned long int (*returner) (struct range_t r);
182 unsigned long int ret_ip(struct range_t r);
183 unsigned long int ret_cur(struct range_t r);
184 unsigned long int ret_max(struct range_t r);
185 unsigned long int ret_percent(struct range_t r);
186 unsigned long int ret_touched(struct range_t r);
187 unsigned long int ret_tc(struct range_t r);
188 unsigned long int ret_tcperc(struct range_t r);
189 void field_selector(char c);
190 int get_order(struct range_t *left, struct range_t *right)
191     __attribute__ ((nonnull(1, 2)));
192 void mergesort_ranges(struct range_t *orig, int size, struct range_t *temp)
193     __attribute__ ((nonnull(1, 3)));
194 /* output function pointer and functions */
195 int (*output_analysis) (void);
196 int output_txt(void);
197 int output_html(void);
198 int output_xml(void);
199 int output_csv(void);
200 /* Memory release, file closing etc */
201 void clean_up(void);
202
203 #endif                          /* DHCPD_POOLS_H */