You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
6.4 KiB

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