[PATCH 1/3] lib: Add ip6_common_octets function

Toke Høiland-Jørgensen toke at toke.dk
Mon Jun 5 23:49:52 CEST 2017


This function computes the number of common octets between two IPv6 addresses,
for use in Babel prefix compression.

Signed-off-by: Toke Høiland-Jørgensen <toke at toke.dk>
---
 lib/ip.c | 14 ++++++++++++++
 lib/ip.h |  1 +
 2 files changed, 15 insertions(+)

diff --git a/lib/ip.c b/lib/ip.c
index 9497248c..af81704b 100644
--- a/lib/ip.c
+++ b/lib/ip.c
@@ -33,6 +33,20 @@ ip6_compare(ip6_addr a, ip6_addr b)
   return 0;
 }
 
+int
+ip6_common_octets(ip6_addr a, ip6_addr b)
+{
+  int i, j, common = 0;
+  for (i=0; i<4; i++)
+    for (j=3; j>=0; j--)
+      if ((0xff & (a.addr[i] >> (8*j))) == (0xff & (b.addr[i] >> (8*j))))
+	common++;
+      else
+	return common;
+
+  return common;
+}
+
 ip6_addr
 ip6_mkmask(uint n)
 {
diff --git a/lib/ip.h b/lib/ip.h
index 5cfce1f1..b4909984 100644
--- a/lib/ip.h
+++ b/lib/ip.h
@@ -205,6 +205,7 @@ static inline int ip4_compare(ip4_addr a, ip4_addr b)
 { return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
 
 int ip6_compare(ip6_addr a, ip6_addr b);
+int ip6_common_octets(ip6_addr a, ip6_addr b);
 
 #define ipa_hash(x) ip6_hash(x)
 #define ipa_compare(x,y) ip6_compare(x,y)
-- 
2.13.0



More information about the Bird-users mailing list