tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

subnet calculations



Hi

What is the path of least resistance on NetBSD to determine if an
address falls into a subnet given as net_address/cidr_mask.

IPv6 support would be a nice bonus. Standard function would be even
better, but I doubt it exists.

I understand I can use strchr()  to cut the "/", then getaddrinfo() to
store the mask in binary while taking IPv6 into account. Then I
understand inet_net_pton() on the original mask could extract the number
of bits in the mask, but then? 

For an IPv4 address, I guess I can turn the cidr mask into a binary
address with thing like  ~((1 << 32-cidr) - 1) on an uint64_t, but for
IPv6 it will be more subbtle. Similarly, doing bit-wise AND on IPv4
address is just working on uint32_t, but it is more complex with IPv6.

-- 
Emmanuel Dreyfus
http://hcpnet.free.fr/pubz
manu%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index