| 1 |
/* -*- coding: utf-8 -*- |
| 2 |
* ---------------------------------------------------------------------- |
| 3 |
* Copyright © 2009, libcorkipset authors |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Please see the COPYING file in this distribution for license details. |
| 7 |
* ---------------------------------------------------------------------- |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef LIBCORK_IPSET_H |
| 11 |
#define LIBCORK_IPSET_H |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
#include <libcork/core.h> |
| 16 |
#include <libcork/ds.h> |
| 17 |
#include <libcork/ipset/nodes.h> |
| 18 |
|
| 19 |
|
| 20 |
struct ip_set { |
| 21 |
struct ipset_node_cache *cache; |
| 22 |
ipset_node_id set_bdd; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
struct ip_map { |
| 27 |
struct ipset_node_cache *cache; |
| 28 |
ipset_node_id map_bdd; |
| 29 |
ipset_node_id default_bdd; |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
/*--------------------------------------------------------------------- |
| 34 |
* General functions |
| 35 |
*/ |
| 36 |
|
| 37 |
int |
| 38 |
ipset_init_library(void); |
| 39 |
|
| 40 |
|
| 41 |
/*--------------------------------------------------------------------- |
| 42 |
* IP set functions |
| 43 |
*/ |
| 44 |
|
| 45 |
void |
| 46 |
ipset_init(struct ip_set *set); |
| 47 |
|
| 48 |
void |
| 49 |
ipset_done(struct ip_set *set); |
| 50 |
|
| 51 |
struct ip_set * |
| 52 |
ipset_new(void); |
| 53 |
|
| 54 |
void |
| 55 |
ipset_free(struct ip_set *set); |
| 56 |
|
| 57 |
bool |
| 58 |
ipset_is_empty(const struct ip_set *set); |
| 59 |
|
| 60 |
bool |
| 61 |
ipset_is_equal(const struct ip_set *set1, const struct ip_set *set2); |
| 62 |
|
| 63 |
size_t |
| 64 |
ipset_memory_size(const struct ip_set *set); |
| 65 |
|
| 66 |
int |
| 67 |
ipset_save(FILE *stream, const struct ip_set *set); |
| 68 |
|
| 69 |
int |
| 70 |
ipset_save_to_stream(struct cork_stream_consumer *stream, |
| 71 |
const struct ip_set *set); |
| 72 |
|
| 73 |
int |
| 74 |
ipset_save_dot(FILE *stream, const struct ip_set *set); |
| 75 |
|
| 76 |
struct ip_set * |
| 77 |
ipset_load(FILE *stream); |
| 78 |
|
| 79 |
bool |
| 80 |
ipset_ipv4_add(struct ip_set *set, struct cork_ipv4 *elem); |
| 81 |
|
| 82 |
bool |
| 83 |
ipset_ipv4_add_network(struct ip_set *set, struct cork_ipv4 *elem, |
| 84 |
unsigned int cidr_prefix); |
| 85 |
|
| 86 |
bool |
| 87 |
ipset_ipv4_remove(struct ip_set *set, struct cork_ipv4 *elem); |
| 88 |
|
| 89 |
bool |
| 90 |
ipset_ipv4_remove_network(struct ip_set *set, struct cork_ipv4 *elem, |
| 91 |
unsigned int cidr_prefix); |
| 92 |
|
| 93 |
bool |
| 94 |
ipset_contains_ipv4(const struct ip_set *set, struct cork_ipv4 *elem); |
| 95 |
|
| 96 |
bool |
| 97 |
ipset_ipv6_add(struct ip_set *set, struct cork_ipv6 *elem); |
| 98 |
|
| 99 |
bool |
| 100 |
ipset_ipv6_add_network(struct ip_set *set, struct cork_ipv6 *elem, |
| 101 |
unsigned int cidr_prefix); |
| 102 |
|
| 103 |
bool |
| 104 |
ipset_ipv6_remove(struct ip_set *set, struct cork_ipv6 *elem); |
| 105 |
|
| 106 |
bool |
| 107 |
ipset_ipv6_remove_network(struct ip_set *set, struct cork_ipv6 *elem, |
| 108 |
unsigned int cidr_prefix); |
| 109 |
|
| 110 |
bool |
| 111 |
ipset_contains_ipv6(const struct ip_set *set, struct cork_ipv6 *elem); |
| 112 |
|
| 113 |
bool |
| 114 |
ipset_ip_add(struct ip_set *set, struct cork_ip *addr); |
| 115 |
|
| 116 |
bool |
| 117 |
ipset_ip_add_network(struct ip_set *set, struct cork_ip *addr, |
| 118 |
unsigned int cidr_prefix); |
| 119 |
|
| 120 |
bool |
| 121 |
ipset_ip_remove(struct ip_set *set, struct cork_ip *addr); |
| 122 |
|
| 123 |
bool |
| 124 |
ipset_ip_remove_network(struct ip_set *set, struct cork_ip *addr, |
| 125 |
unsigned int cidr_prefix); |
| 126 |
|
| 127 |
bool |
| 128 |
ipset_contains_ip(const struct ip_set *set, struct cork_ip *elem); |
| 129 |
|
| 130 |
|
| 131 |
/* An internal state type used by the ipset_iterator_multiple_expansion_state |
| 132 |
* field. */ |
| 133 |
enum ipset_iterator_state { |
| 134 |
IPSET_ITERATOR_NORMAL = 0, |
| 135 |
IPSET_ITERATOR_MULTIPLE_IPV4, |
| 136 |
IPSET_ITERATOR_MULTIPLE_IPV6 |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/* An iterator that returns all of the IP addresses that have a given value in |
| 141 |
* an IP set or map. */ |
| 142 |
struct ipset_iterator { |
| 143 |
/* The address of the current IP network in the iterator. */ |
| 144 |
struct cork_ip addr; |
| 145 |
|
| 146 |
/* The netmask of the current IP network in the iterator, given as a |
| 147 |
* CIDR prefix. For a single IP address, this will be 32 or 128. */ |
| 148 |
unsigned int cidr_prefix; |
| 149 |
|
| 150 |
/* Whether the current assignment needs to be expanded a second |
| 151 |
* time. |
| 152 |
* |
| 153 |
* We have to expand IPv4 and IPv6 assignments separately, since the |
| 154 |
* set of variables to turn into address bits is different. |
| 155 |
* Unfortunately, a BDD assignment can contain both IPv4 and IPv6 |
| 156 |
* addresses, if variable 0 is EITHER. (This is trivially true for |
| 157 |
* the empty set, for instance.) In this case, we have to |
| 158 |
* explicitly set variable 0 to TRUE, expand it as IPv4, and then |
| 159 |
* set it to FALSE, and expand it as IPv6. This variable tells us |
| 160 |
* whether we're in an assignment that needs to be expanded twice, |
| 161 |
* and if so, which expansion we're currently in. |
| 162 |
*/ |
| 163 |
enum ipset_iterator_state multiple_expansion_state; |
| 164 |
|
| 165 |
/* An iterator for retrieving each assignment in the set's BDD. */ |
| 166 |
struct ipset_bdd_iterator *bdd_iterator; |
| 167 |
|
| 168 |
/* An iterator for expanding each assignment into individual IP |
| 169 |
* addresses. */ |
| 170 |
struct ipset_expanded_assignment *assignment_iterator; |
| 171 |
|
| 172 |
/* Whether there are any more IP addresses in this iterator. */ |
| 173 |
bool finished; |
| 174 |
|
| 175 |
/* The desired value for each IP address. */ |
| 176 |
bool desired_value; |
| 177 |
|
| 178 |
/* Whether to summarize the contents of the IP set as networks, |
| 179 |
* where possible. */ |
| 180 |
bool summarize; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
struct ipset_iterator * |
| 185 |
ipset_iterate(struct ip_set *set, bool desired_value); |
| 186 |
|
| 187 |
struct ipset_iterator * |
| 188 |
ipset_iterate_networks(struct ip_set *set, bool desired_value); |
| 189 |
|
| 190 |
void |
| 191 |
ipset_iterator_free(struct ipset_iterator *iterator); |
| 192 |
|
| 193 |
void |
| 194 |
ipset_iterator_advance(struct ipset_iterator *iterator); |
| 195 |
|
| 196 |
|
| 197 |
/*--------------------------------------------------------------------- |
| 198 |
* IP map functions |
| 199 |
*/ |
| 200 |
|
| 201 |
void |
| 202 |
ipmap_init(struct ip_map *map, int default_value); |
| 203 |
|
| 204 |
void |
| 205 |
ipmap_done(struct ip_map *map); |
| 206 |
|
| 207 |
struct ip_map * |
| 208 |
ipmap_new(int default_value); |
| 209 |
|
| 210 |
void |
| 211 |
ipmap_free(struct ip_map *map); |
| 212 |
|
| 213 |
bool |
| 214 |
ipmap_is_empty(const struct ip_map *map); |
| 215 |
|
| 216 |
bool |
| 217 |
ipmap_is_equal(const struct ip_map *map1, const struct ip_map *map2); |
| 218 |
|
| 219 |
size_t |
| 220 |
ipmap_memory_size(const struct ip_map *map); |
| 221 |
|
| 222 |
int |
| 223 |
ipmap_save(FILE *stream, const struct ip_map *map); |
| 224 |
|
| 225 |
int |
| 226 |
ipmap_save_to_stream(struct cork_stream_consumer *stream, |
| 227 |
const struct ip_map *map); |
| 228 |
|
| 229 |
struct ip_map * |
| 230 |
ipmap_load(FILE *stream); |
| 231 |
|
| 232 |
void |
| 233 |
ipmap_ipv4_set(struct ip_map *map, struct cork_ipv4 *elem, int value); |
| 234 |
|
| 235 |
void |
| 236 |
ipmap_ipv4_set_network(struct ip_map *map, struct cork_ipv4 *elem, |
| 237 |
unsigned int cidr_prefix, int value); |
| 238 |
|
| 239 |
int |
| 240 |
ipmap_ipv4_get(struct ip_map *map, struct cork_ipv4 *elem); |
| 241 |
|
| 242 |
void |
| 243 |
ipmap_ipv6_set(struct ip_map *map, struct cork_ipv6 *elem, int value); |
| 244 |
|
| 245 |
void |
| 246 |
ipmap_ipv6_set_network(struct ip_map *map, struct cork_ipv6 *elem, |
| 247 |
unsigned int cidr_prefix, int value); |
| 248 |
|
| 249 |
int |
| 250 |
ipmap_ipv6_get(struct ip_map *map, struct cork_ipv6 *elem); |
| 251 |
|
| 252 |
void |
| 253 |
ipmap_ip_set(struct ip_map *map, struct cork_ip *addr, int value); |
| 254 |
|
| 255 |
void |
| 256 |
ipmap_ip_set_network(struct ip_map *map, struct cork_ip *addr, |
| 257 |
unsigned int cidr_prefix, int value); |
| 258 |
|
| 259 |
int |
| 260 |
ipmap_ip_get(struct ip_map *map, struct cork_ip *addr); |
| 261 |
|
| 262 |
|
| 263 |
#endif /* LIBCORK_IPSET_H */ |