summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJeremy Sowden <jeremy@azazel.net>2023-08-21 20:42:37 +0100
committerFlorian Westphal <fw@strlen.de>2023-09-14 14:22:49 +0200
commit9aa04788a317f0bc34050cd07ee81d66fda4cc8c (patch)
tree160c28717cf7779a254e8731bfa6e199d4a717ae /include
parent083d56967d3940a3a29d11c881d1b78e6033c24f (diff)
db: insert ipv6 addresses in the same format as ip2binHEADmaster
Move a `ULOGD_RET_BOOL` case for consistency. Signed-off-by: Jeremy Sowden <jeremy@azazel.net> Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'include')
-rw-r--r--include/ulogd/ulogd.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/include/ulogd/ulogd.h b/include/ulogd/ulogd.h
index cb01740..c7cf402 100644
--- a/include/ulogd/ulogd.h
+++ b/include/ulogd/ulogd.h
@@ -18,6 +18,7 @@
#include <signal.h> /* need this because of extension-sighandler */
#include <sys/types.h>
#include <inttypes.h>
+#include <netinet/in.h>
#include <string.h>
#include <config.h>
@@ -208,6 +209,46 @@ static inline void *ikey_get_ptr(struct ulogd_key *key)
return key->u.source->u.value.ptr;
}
+/**
+ * Convert IPv4 address (as 32-bit unsigned integer) to IPv6 address: add 96-bit
+ * prefix "::ffff" to get IPv6 address "::ffff:a.b.c.d".
+ */
+static inline struct in6_addr *
+uint32_to_ipv6(uint32_t ipv4, struct in6_addr *ipv6)
+{
+ static const uint8_t IPV4_IN_IPV6_PREFIX[12] = {
+ [10] = 0xff,
+ [11] = 0xff,
+ };
+ uint8_t *p = ipv6->s6_addr;
+
+ memcpy(p, IPV4_IN_IPV6_PREFIX, sizeof(IPV4_IN_IPV6_PREFIX));
+ p += sizeof(IPV4_IN_IPV6_PREFIX);
+ memcpy(p, &ipv4, sizeof(ipv4));
+
+ return ipv6;
+}
+
+static inline void
+format_ipv6(char *buf, size_t size, const struct in6_addr *ipv6)
+{
+ unsigned i = 0;
+
+ if (size > 2 + sizeof (*ipv6) * 2) {
+ buf[i++] = '0';
+ buf[i++] = 'x';
+
+ for (unsigned j = 0; i < sizeof(*ipv6); j += 4, i += 8) {
+ sprintf(buf + i, "%02hhx%02hhx%02hhx%02hhx",
+ ipv6->s6_addr[j + 0],
+ ipv6->s6_addr[j + 1],
+ ipv6->s6_addr[j + 2],
+ ipv6->s6_addr[j + 3]);
+ }
+ }
+ buf[i] = '\0';
+}
+
struct ulogd_pluginstance_stack;
struct ulogd_pluginstance;