Skip to content

Commit 6c39e02

Browse files
committed
basic: introduce in_addr_data_hash_ops
1 parent 1c57fa9 commit 6c39e02

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/basic/in-addr-util.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,34 @@ int in_addr_prefix_from_string_auto(
571571
return 0;
572572

573573
}
574+
575+
void in_addr_data_hash_func(const void *p, struct siphash *state) {
576+
const struct in_addr_data *a = p;
577+
578+
siphash24_compress(&a->family, sizeof(a->family), state);
579+
580+
if (a->family == AF_INET)
581+
siphash24_compress(&a->address.in, sizeof(a->address.in), state);
582+
else if (a->family == AF_INET6)
583+
siphash24_compress(&a->address.in6, sizeof(a->address.in6), state);
584+
}
585+
586+
int in_addr_data_compare_func(const void *a, const void *b) {
587+
const struct in_addr_data *x = a, *y = b;
588+
589+
if (x->family != y->family)
590+
return x->family - y->family;
591+
592+
if (x->family == AF_INET)
593+
return memcmp(&x->address.in.s_addr, &y->address.in.s_addr, sizeof(struct in_addr));
594+
595+
if (x->family == AF_INET6)
596+
return memcmp(&x->address.in6.s6_addr, &y->address.in6.s6_addr, sizeof(struct in6_addr));
597+
598+
return trivial_compare_func(a, b);
599+
}
600+
601+
const struct hash_ops in_addr_data_hash_ops = {
602+
.hash = in_addr_data_hash_func,
603+
.compare = in_addr_data_compare_func,
604+
};

src/basic/in-addr-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stddef.h>
66
#include <sys/socket.h>
77

8+
#include "hash-funcs.h"
89
#include "macro.h"
910
#include "util.h"
1011

@@ -53,3 +54,7 @@ static inline size_t FAMILY_ADDRESS_SIZE(int family) {
5354
}
5455

5556
#define IN_ADDR_NULL ((union in_addr_union) {})
57+
58+
void in_addr_data_hash_func(const void *p, struct siphash *state);
59+
int in_addr_data_compare_func(const void *a, const void *b);
60+
extern const struct hash_ops in_addr_data_hash_ops;

0 commit comments

Comments
 (0)