[PATCH 1/3] lib: Add ip6_common_octets function
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@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
Christian Tacke <Christian.Tacke+bird.network.cz@cosmokey.com> writes:
Hi,
just out of curiosity:
On Mon, Jun 05, 2017 at 23:49:52 +0200, Toke Høiland-Jørgensen wrote: [...]
+int +ip6_common_octets(ip6_addr a, ip6_addr b) +{ + int i, j, common = 0; [...]
Why aren't you using an unsigned int here?
No particular reason; I suppose muscle memory kicked in and gave me an 'int'. You are right, of course, that it should probably be uint... :) -Toke
On Tue, Jun 06, 2017 at 01:00:38PM +0200, Toke Høiland-Jørgensen wrote:
Christian Tacke <Christian.Tacke+bird.network.cz@cosmokey.com> writes:
Hi,
just out of curiosity:
On Mon, Jun 05, 2017 at 23:49:52 +0200, Toke Høiland-Jørgensen wrote: [...]
+int +ip6_common_octets(ip6_addr a, ip6_addr b) +{ + int i, j, common = 0; [...]
Why aren't you using an unsigned int here?
No particular reason; I suppose muscle memory kicked in and gave me an 'int'. You are right, of course, that it should probably be uint... :)
Well, beware of down-iteration in for(): for (j=3; j>=0; j--) That would not work with uint. -- Elen sila lumenn' omentielvo Ondrej 'Santiago' Zajicek (email: santiago@crfreenet.org) OpenPGP encrypted e-mails preferred (KeyID 0x11DEADC3, wwwkeys.pgp.net) "To err is human -- to blame it on a computer is even more so."
participants (3)
-
Christian Tacke -
Ondrej Zajicek -
Toke Høiland-Jørgensen