ell.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH 01/17] net: Add net_prefix_from_ipv6 utility
@ 2022-05-13 22:46 Andrew Zaborowski
  0 siblings, 0 replies; only message in thread
From: Andrew Zaborowski @ 2022-05-13 22:46 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 999 bytes --]

Add function that zeroes bits after prefix_len'th bit in an address.  Put
it in net-private.h so it can be used by both netconfig.c and icmp6.c,
potentially others.
---
 ell/net-private.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/ell/net-private.h b/ell/net-private.h
index cc395bb..39d4d98 100644
--- a/ell/net-private.h
+++ b/ell/net-private.h
@@ -22,3 +22,21 @@
 
 char *net_domain_name_parse(const uint8_t *raw, size_t raw_len);
 char **net_domain_list_parse(const uint8_t *raw, size_t raw_len);
+
+static inline const void *net_prefix_from_ipv6(const uint8_t *address,
+						uint8_t prefix_len)
+{
+	uint8_t last_byte = prefix_len / 8;
+	uint8_t bits = prefix_len & 7;
+	static uint8_t prefix[16];
+
+	memcpy(prefix, address, last_byte);
+
+	if (prefix_len & 7) {
+		prefix[last_byte] = address[last_byte] & (0xff00 >> bits);
+		last_byte++;
+	}
+
+	memset(prefix + last_byte, 0, 16 - last_byte);
+	return prefix;
+}
-- 
2.32.0

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-13 22:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13 22:46 [PATCH 01/17] net: Add net_prefix_from_ipv6 utility Andrew Zaborowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).