Re: Getting prefix from address?

Stig Venaas <[email protected]>
Newsgroups gmane.network.ipv6.general
Message-ID <[email protected]>
On Sat, Dec 10, 2005 at 11:42:05AM +0900, JINMEI Tatuya / ?$B?@L@C#:H wrote:
> >>>>> On Fri, 9 Dec 2005 18:28:14 -0500, 
> >>>>> Roy Smith <[email protected]> said:
> 
> > Is there a standard API for getting the prefix given an address and a 
> > prefix length?  I.e., I've got 
> > "FFEE:0200:0045:0000:0000:0000:0123:0012" and 64 and I want to get 
> > "FFEE:0200:0045:0000:0000:0000:0000:0000".
> 
> > I looked in RFC 3493 and 3542 and didn't see anything in either one 
> > that looked like it did this.  Did I just miss it, or does it not 
> > exist?
> 
> There's no standard API for this as far as I know.

Pretty sure there isn't. I don't see the need for it myself, it
is easy enough to implement. I'm sure there are people willing to
share code for this.

For a reasonably quick implementation, you could use memset() for
the 0-bytes, and have an array with the bitmasks for the remainder.

Something like:

struct in6_addr *addr;
uint8_t plen, div8;
uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
...
div8 = plen / 8;
if (div8 < 15)
	memset(&addr->s6_addr[1 + div8], 0, 15 - div8);
if (div8 < 16)
	addr->s6_addr[div8] = addr->s6_addr[div8] & mask[plen % 8];

The above is not tested, but should be ok...

Stig
---------------------------------------------------------------------
The IPv6 Users Mailing List
Unsubscribe by sending "unsubscribe users" to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.