e3835ad5c2f1bf2ca81157fce51a30cdc30210d5
[debian/dhcpd-pools.git] / src / dhcpd-pools.h
1 /* http://dhcpd-pools.sourceforge.net/
2 ** Copyright 2006- Sami Kerola <kerolasa@iki.fi>
3 **
4 ** This program is free software: you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation, either version 3 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.   
16 */
17
18 #ifndef DHCPD_POOLS_H
19 # define DHCPD_POOLS_H 1
20
21 #include <config.h>
22
23 /* Feature test switches */
24 #define _POSIX_SOURCE 1
25 #define POSIXLY_CORRECT 1
26
27 #ifdef  HAVE_STDLIB_H
28 #include <stdlib.h>
29 #else
30 extern void exit();
31 extern char *malloc();
32 #endif                          /* STDC_HEADERS */
33
34 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
35 #  ifdef HAVE___PROGNAME
36 extern char *__progname;
37 #    define program_invocation_short_name __progname
38 #  else                         /* HAVE___PROGNAME */
39 #    ifdef HAVE_GETEXECNAME
40 #      include <stdlib.h>
41 #      define program_invocation_short_name \
42        prog_inv_sh_nm_from_file(getexecname(), 0)
43 #    else                       /* HAVE_GETEXECNAME */
44 #      define program_invocation_short_name \
45        prog_inv_sh_nm_from_file(__FILE__, 1)
46 #    endif                      /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
47 static char prog_inv_sh_nm_buf[256];
48 static inline char *prog_inv_sh_nm_from_file(char *f, char stripext)
49 {
50         char *t;
51         if ((t = strrchr(f, '/')) != NULL) {
52                 t++;
53         } else {
54                 t = f;
55         }
56         strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
57         prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
58
59         if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) {
60                 *t = '\0';
61         }
62         return prog_inv_sh_nm_buf;
63 }
64 #  endif
65 #endif
66
67 /* Structures and unions */
68 struct configuration_t {
69         char *dhcpdconf_file;
70         char *dhcpdlease_file;
71         char output_format[2];
72         char sort[6];
73         int reverse_order;
74         char *output_file;
75         int output_limit[2];
76 };
77 struct shared_network_t {
78         char *name;
79         unsigned long int available;
80         unsigned long int used;
81         unsigned long int touched;
82         unsigned long int backups;
83 };
84 struct range_t {
85         struct shared_network_t *shared_net;
86         unsigned long int first_ip;
87         unsigned long int last_ip;
88         unsigned long int count;
89         unsigned long int touched;
90         unsigned long int backups;
91 };
92 struct macaddr_t {
93         char *ethernet;
94         char *ip;
95         struct macaddr_t *next;
96 };
97
98 /* Global variables */
99 static int const true = 1;
100 static int const false = 0;
101
102 struct configuration_t config;
103
104 static int const output_limit_bit_1 = 1;
105 static int const output_limit_bit_2 = 2;
106 static int const output_limit_bit_3 = 4;
107 unsigned int fullhtml;
108
109 struct shared_network_t *shared_networks;
110 char *shared_net_names;
111 unsigned int num_shared_networks;
112
113 struct range_t *ranges;
114 unsigned int num_ranges;
115
116 unsigned long int *leases;
117 unsigned long int num_leases;
118
119 unsigned long int *touches;
120 unsigned long int num_touches;
121
122 unsigned long int *backups;
123 unsigned long int num_backups;
124
125 struct macaddr_t *macaddr;
126
127 /* Function prototypes */
128 int prepare_memory(void);
129 int parse_leases(void);
130 char *parse_config(int, char *, char *, char *, struct shared_network_t *)
131     __attribute__ ((nonnull(2, 3, 4)));
132 int nth_field(int n, char *dest, const char *src)
133     __attribute__ ((nonnull(2, 3)))
134 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
135     __attribute__ ((__hot__))
136 #endif
137     ;
138 int prepare_data(void);
139 int do_counting(void);
140 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
141     __attribute__ ((nonnull(1, 2)));
142 /* General support functions */
143 void *safe_malloc(const size_t size)
144 #if __GNUC__ >= 3
145     __attribute__ ((__malloc__))
146 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
147     __attribute__ ((__alloc_size__((1))))
148 #endif
149 #endif
150     ;
151 void *safe_realloc(void *ptr, const size_t size);
152 char *safe_strdup(const char *str) __attribute__ ((nonnull(1)));
153 void print_version(void) __attribute__ ((noreturn));
154 void usage(int status) __attribute__ ((noreturn));
155 /* qsort required functions... */
156 /* ...for ranges and... */
157 int intcomp(const void *x, const void *y) __attribute__ ((nonnull(1, 2)));
158 int rangecomp(const void *r1, const void *r2)
159     __attribute__ ((nonnull(1, 2)));
160 /* sort function pointer and functions */
161 int sort_name(void);
162 unsigned long int (*returner) (struct range_t r);
163 unsigned long int ret_ip(struct range_t r);
164 unsigned long int ret_cur(struct range_t r);
165 unsigned long int ret_max(struct range_t r);
166 unsigned long int ret_percent(struct range_t r);
167 unsigned long int ret_touched(struct range_t r);
168 unsigned long int ret_tc(struct range_t r);
169 unsigned long int ret_tcperc(struct range_t r);
170 void field_selector(char c);
171 int get_order(struct range_t *left, struct range_t *right)
172     __attribute__ ((nonnull(1, 2)));
173 void mergesort_ranges(struct range_t *orig, int size, struct range_t *temp)
174     __attribute__ ((nonnull(1, 3)));
175 /* output function pointer and functions */
176 int (*output_analysis) (void);
177 int output_txt(void);
178 int output_html(void);
179 int output_xml(void);
180 int output_csv(void);
181 /* Memory release, file closing etc */
182 void clean_up(void);
183
184 #endif                          /* DHCPD_POOLS_H */