summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/include/bscan/garage.h
blob: 0d6377467dcc5b1aa3794ec0f934aa5c51ffca21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* bscan - mod_garage.h - per IP storage functions include file
 *
 * by scut / teso
 */

#ifndef _MOD_GARAGE_H
#define	_MOD_GARAGE_H

#include <sys/types.h>

#define	GARAGE_VERSION	"1.0.1"


typedef struct	ip_elem {
	struct ip_elem *	next;	/* more then one data stored */

	int			data_free;	/* 1 = do free() on destroy */
	size_t			data_len;
	void *			data;	/* must be free() able */
} ip_elem;


typedef struct	ip_list {
	struct ip_list *	next;	/* another IP, always greater then this ! */
	unsigned long int	ip;

	ip_elem *		data;
} ip_list;


/* not for use in other code then garage.c
 */
typedef struct {
	char *			name;
	ip_list	**		garage;

	void			(* cleaner)(ip_list *);

	unsigned long int	ip_count;

	unsigned long int	timeout_max;
	unsigned long int	timeout_idx;
	unsigned long int *	timeout_tbl;
} garage_hdlr;


/* mg_init
 *
 * setup the required structures for the garage
 *
 * return 0 on success
 * return 1 on failure
 */

garage_hdlr *
mg_init (char *name, unsigned long int max_hosts_in_list,
	void (* cleaner)(ip_list *));


/* mg_destroy
 *
 * destroy all data in the garage `g', use the standard handler in case
 * `do_handler' is not zero, otherwise just free.
 */

void
mg_destroy (garage_hdlr *g, int do_handler);


/* mg_write
 *
 * store pointer `data' with len `data_len' to the garage for IP `ip'
 * if `data_free' is non-zero the `data' pointer will be freed if mg_clean
 * or mg_destroy is called.
 */

void
mg_write (garage_hdlr *g, unsigned long int ip, void *data, size_t data_len,
	int data_free);


/* mg_read
 *
 * return first ip_elem for ip `ip' on success (it is not removed from garage)
 * return NULL on failure
 */

ip_elem *
mg_read (garage_hdlr *g, unsigned long int ip);


/* mg_clean
 *
 * clean everything stored in the garage for IP `ip'
 */

void
mg_clean (garage_hdlr *g, unsigned long int ip, void (*cleaner)(ip_list *));


/* mg_show
 *
 * DEBUG function, to show IP distribution in garage
 */

void
mg_show (garage_hdlr *g);


/* mg_count
 *
 * count elements in garage
 */

unsigned long int
mg_count (garage_hdlr *g);


/* mg_ip_isin
 *
 * check whether the ip `ip' is stored in the garage pointed to by `g'.
 *
 * return zero in case it is not
 * return != zero if it is
 */

int
mg_ip_isin (garage_hdlr *g, unsigned long int ip);


/* CIDR routines
 */

/* mg_cidr_getmask
 *
 * convert a netmask (eg 0xfffffc00) or a cidr notation (eg 24) given in
 * `mask' to a netmask.
 */

unsigned long int
mg_cidr_getmask (unsigned long int mask);


/* mg_cidr_maskcount
 *
 * return the number of hosts that are possible using a mask `mask' in
 * either CIDR or netmask notation
 */

unsigned long int
mg_cidr_maskcount (unsigned long int mask);


/* mg_cidr_match
 *
 * check whether `ip1' and `ip2' are in the same network, given the network
 * size by `mask' (CIDR or netmask notation)
 */

int
mg_cidr_match (unsigned long int ip1, unsigned long int ip2,
	unsigned long int mask);


/* mg_cidr_count
 *
 * count elements in garage `g', that are within the CIDR range build from
 * ip `ip' and netmask `mask'. `mask' is either the number of bits, if it's in
 * the range of 0-32, or the real mask, if it is greater then 32. (for the
 * zero case the netmask is equal to the cidr notation).
 */

unsigned long int
mg_cidr_count (garage_hdlr *g, unsigned long int ip, unsigned long int mask);


#endif