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 
11 #include <libcork/core.h>
12 
13 #include "ipset/bdd/nodes.h"
14 #include "ipset/ipset.h"
15 
16 bool
ipmap_is_empty(const struct ip_map * map)17 ipmap_is_empty(const struct ip_map *map)
18 {
19     /* Since BDDs are unique, any map that maps all addresses to the
20      * default value is “empty”. */
21     return (map->map_bdd == map->default_bdd);
22 }
23 
24 bool
ipmap_is_equal(const struct ip_map * map1,const struct ip_map * map2)25 ipmap_is_equal(const struct ip_map *map1, const struct ip_map *map2)
26 {
27     return ipset_node_cache_nodes_equal
28         (map1->cache, map1->map_bdd, map2->cache, map2->map_bdd);
29 }
30 
31 size_t
ipmap_memory_size(const struct ip_map * map)32 ipmap_memory_size(const struct ip_map *map)
33 {
34     return ipset_node_memory_size(map->cache, map->map_bdd);
35 }
36 
37 
38 void
ipmap_ip_set(struct ip_map * map,struct cork_ip * addr,int value)39 ipmap_ip_set(struct ip_map *map, struct cork_ip *addr, int value)
40 {
41     if (addr->version == 4) {
42         ipmap_ipv4_set(map, &addr->ip.v4, value);
43     } else {
44         ipmap_ipv6_set(map, &addr->ip.v6, value);
45     }
46 }
47 
48 
49 void
ipmap_ip_set_network(struct ip_map * map,struct cork_ip * addr,unsigned int cidr_prefix,int value)50 ipmap_ip_set_network(struct ip_map *map, struct cork_ip *addr,
51                      unsigned int cidr_prefix, int value)
52 {
53     if (addr->version == 4) {
54         ipmap_ipv4_set_network(map, &addr->ip.v4, cidr_prefix, value);
55     } else {
56         ipmap_ipv6_set_network(map, &addr->ip.v6, cidr_prefix, value);
57     }
58 }
59 
60 
61 int
ipmap_ip_get(struct ip_map * map,struct cork_ip * addr)62 ipmap_ip_get(struct ip_map *map, struct cork_ip *addr)
63 {
64     if (addr->version == 4) {
65         return ipmap_ipv4_get(map, &addr->ip.v4);
66     } else {
67         return ipmap_ipv6_get(map, &addr->ip.v6);
68     }
69 }
70