| 1 | /* Useful functions for dealing with collections of IP addresses */ |
| 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 3 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 | */ |
| 20 | |
| 21 | #ifndef ipaddr_h |
| 22 | #define ipaddr_h |
| 23 | |
| 24 | struct subnet { |
| 25 | uint32_t prefix; |
| 26 | uint32_t mask; |
| 27 | int len; |
| 28 | }; |
| 29 | |
| 30 | struct subnet_list { |
| 31 | int32_t entries; |
| 32 | int32_t alloc; |
| 33 | struct subnet *list; |
| 34 | }; |
| 35 | |
| 36 | struct iprange { |
| 37 | uint32_t a,b; |
| 38 | }; |
| 39 | |
| 40 | struct ipset { |
| 41 | int32_t l; /* Number of entries in list */ |
| 42 | int32_t a; /* Allocated space in list */ |
| 43 | struct iprange *d; |
| 44 | }; |
| 45 | |
| 46 | extern struct subnet_list *subnet_list_new(void); |
| 47 | extern void subnet_list_free(struct subnet_list *a); |
| 48 | extern void subnet_list_append(struct subnet_list *a, uint32_t prefix, int len); |
| 49 | |
| 50 | static inline bool_t subnet_match(struct subnet s, uint32_t address) |
| 51 | { |
| 52 | return (s.prefix==(address&s.mask)); |
| 53 | } |
| 54 | |
| 55 | extern struct ipset *ipset_new(void); |
| 56 | extern void ipset_free(struct ipset *a); |
| 57 | extern struct ipset *ipset_from_subnet(struct subnet s); |
| 58 | extern struct ipset *ipset_from_subnet_list(struct subnet_list *l); |
| 59 | extern struct ipset *ipset_union(struct ipset *a, struct ipset *b); |
| 60 | extern struct ipset *ipset_intersection(struct ipset *a, struct ipset *b); |
| 61 | extern struct ipset *ipset_complement(struct ipset *a); |
| 62 | extern struct ipset *ipset_subtract(struct ipset *a, struct ipset *b); |
| 63 | extern bool_t ipset_is_empty(struct ipset *a); |
| 64 | extern bool_t ipset_contains_addr(struct ipset *a, uint32_t addr); |
| 65 | extern bool_t ipset_is_subset(struct ipset *super, struct ipset *sub); |
| 66 | extern struct subnet_list *ipset_to_subnet_list(struct ipset *is); |
| 67 | |
| 68 | extern string_t ipaddr_to_string(uint32_t addr); |
| 69 | extern string_t subnet_to_string(struct subnet sn); |
| 70 | |
| 71 | extern struct ipset *string_list_to_ipset(list_t *l,struct cloc loc, |
| 72 | cstring_t module, cstring_t param); |
| 73 | |
| 74 | extern uint32_t string_item_to_ipaddr(const item_t *i, cstring_t desc); |
| 75 | |
| 76 | #endif /* ipaddr_h */ |