1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Implementation for a set of addresses.
8 * This module was first written on a semi-emergency basis to improve the
9 * robustness of the anti-DoS module. As such, it's written in a pretty
10 * conservative way, and should be susceptible to improvement later on.
14 #include "core/or/address_set.h"
15 #include "lib/net/address.h"
16 #include "lib/container/bloomfilt.h"
17 #include "lib/crypt_ops/crypto_rand.h"
19 /** Wrap our hash function to have the signature that the bloom filter
22 bloomfilt_addr_hash(const struct sipkey
*key
,
25 return tor_addr_keyed_hash(key
, item
);
29 * Allocate and return an address_set, suitable for holding up to
30 * <b>max_address_guess</b> distinct values.
33 address_set_new(int max_addresses_guess
)
35 uint8_t k
[BLOOMFILT_KEY_LEN
];
36 crypto_rand((void*)k
, sizeof(k
));
37 return bloomfilt_new(max_addresses_guess
, bloomfilt_addr_hash
, k
);
41 * Add <b>addr</b> to <b>set</b>.
43 * All future queries for <b>addr</b> in set will return true. Removing
44 * items is not possible.
47 address_set_add(address_set_t
*set
, const struct tor_addr_t
*addr
)
49 bloomfilt_add(set
, addr
);
52 /** As address_set_add(), but take an ipv4 address in host order. */
54 address_set_add_ipv4h(address_set_t
*set
, uint32_t addr
)
57 tor_addr_from_ipv4h(&a
, addr
);
58 address_set_add(set
, &a
);
62 * Return true if <b>addr</b> is a member of <b>set</b>. (And probably,
63 * return false if <b>addr</b> is not a member of set.)
66 address_set_probably_contains(const address_set_t
*set
,
67 const struct tor_addr_t
*addr
)
69 return bloomfilt_probably_contains(set
, addr
);