Add isc_sockaddr_hash_ex that can be used in incremental hashing

Add a sockaddr hashing function that can be used as part of incremental
hashing.
This commit is contained in:
Ondřej Surý 2023-09-18 09:59:10 +02:00
parent 9f40eee0a8
commit 3230c8e369
No known key found for this signature in database
GPG key ID: 2820F37E873DEA41
2 changed files with 23 additions and 7 deletions

View file

@ -85,6 +85,15 @@ isc_sockaddr_eqaddrprefix(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
* If 'b''s scope is zero then 'a''s scope will be ignored.
*/
void
isc_sockaddr_hash_ex(isc_hash32_t *hash, const isc_sockaddr_t *sockaddr,
bool address_only);
/*%<
* Add the hash of the sockaddr into the hash for incremental hashing
*
* See isc_sockaddr_hash() for details.
*/
uint32_t
isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, bool address_only);
/*%<

View file

@ -187,17 +187,15 @@ isc_sockaddr_format(const isc_sockaddr_t *sa, char *array, unsigned int size) {
}
}
uint32_t
isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, bool address_only) {
void
isc_sockaddr_hash_ex(isc_hash32_t *hash, const isc_sockaddr_t *sockaddr,
bool address_only) {
REQUIRE(sockaddr != NULL);
size_t len = 0;
const uint8_t *s = NULL;
unsigned int p = 0;
const struct in6_addr *in6;
isc_hash32_t hash;
isc_hash32_init(&hash);
switch (sockaddr->type.sa.sa_family) {
case AF_INET:
@ -224,10 +222,19 @@ isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, bool address_only) {
UNREACHABLE();
}
isc_hash32_hash(&hash, s, len, true);
isc_hash32_hash(hash, s, len, true);
if (!address_only) {
isc_hash32_hash(&hash, &p, sizeof(p), true);
isc_hash32_hash(hash, &p, sizeof(p), true);
}
}
uint32_t
isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, bool address_only) {
isc_hash32_t hash;
isc_hash32_init(&hash);
isc_sockaddr_hash_ex(&hash, sockaddr, address_only);
return (isc_hash32_finalize(&hash));
}