Merge remote-tracking branch 'mdw/mdw/powm-sec'
[secnet] / ipaddr.h
CommitLineData
794f2398 1/* Useful functions for dealing with collections of IP addresses */
c215a4bc
IJ
2/*
3 * This file is part of secnet.
4 * See README for full list of copyright holders.
5 *
6 * secnet is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version d of the License, or
9 * (at your option) any later version.
10 *
11 * secnet is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 3 along with secnet; if not, see
18 * https://www.gnu.org/licenses/gpl.html.
19 */
794f2398 20
7138d0c5
SE
21#ifndef ipaddr_h
22#define ipaddr_h
23
794f2398
SE
24struct subnet {
25 uint32_t prefix;
26 uint32_t mask;
1caa23ff 27 int len;
794f2398
SE
28};
29
30struct subnet_list {
1caa23ff
IJ
31 int32_t entries;
32 int32_t alloc;
794f2398
SE
33 struct subnet *list;
34};
35
36struct iprange {
37 uint32_t a,b;
38};
39
40struct ipset {
1caa23ff
IJ
41 int32_t l; /* Number of entries in list */
42 int32_t a; /* Allocated space in list */
794f2398
SE
43 struct iprange *d;
44};
7138d0c5 45
794f2398
SE
46extern struct subnet_list *subnet_list_new(void);
47extern void subnet_list_free(struct subnet_list *a);
1caa23ff 48extern void subnet_list_append(struct subnet_list *a, uint32_t prefix, int len);
794f2398
SE
49
50static inline bool_t subnet_match(struct subnet s, uint32_t address)
51{
52 return (s.prefix==(address&s.mask));
53}
54
55extern struct ipset *ipset_new(void);
56extern void ipset_free(struct ipset *a);
57extern struct ipset *ipset_from_subnet(struct subnet s);
58extern struct ipset *ipset_from_subnet_list(struct subnet_list *l);
59extern struct ipset *ipset_union(struct ipset *a, struct ipset *b);
60extern struct ipset *ipset_intersection(struct ipset *a, struct ipset *b);
61extern struct ipset *ipset_complement(struct ipset *a);
62extern struct ipset *ipset_subtract(struct ipset *a, struct ipset *b);
63extern bool_t ipset_is_empty(struct ipset *a);
64extern bool_t ipset_contains_addr(struct ipset *a, uint32_t addr);
65extern bool_t ipset_is_subset(struct ipset *super, struct ipset *sub);
66extern struct subnet_list *ipset_to_subnet_list(struct ipset *is);
7138d0c5
SE
67
68extern string_t ipaddr_to_string(uint32_t addr);
794f2398
SE
69extern string_t subnet_to_string(struct subnet sn);
70
71extern struct ipset *string_list_to_ipset(list_t *l,struct cloc loc,
fe5e9cc4 72 cstring_t module, cstring_t param);
794f2398 73
a094a1ba 74extern uint32_t string_item_to_ipaddr(const item_t *i, cstring_t desc);
7138d0c5
SE
75
76#endif /* ipaddr_h */