tcpdump mailing list archives
Re: IP range implementation given an IP address and subnet mask
From: Andrej van der Zee <andrejvanderzee () gmail com>
Date: Thu, 20 Oct 2011 05:58:39 +0200
I would like to implement an IP address range filter similar to the BPF capture filter that takes an IP address and subnet mask (like in "net 192.168.0.0 and mask 255.255.255.0"). What would be the best way to calculate this in C? Or in which file can I find the BPF implementation of this expression?
Ok, pretty straightforward using the CIDR notation:
bool in_cidr_subnet(unsigned long ip_addr,
unsigned long cidr_network_addr,
unsigned short cidr_bits)
{
const unsigned long mask = ntohl(((unsigned long) -1) << (32-cidr_bits));
assert(cidr_network_addr & mask == cidr_network_addr);
const unsigned long ip_network_addr = ip_addr & mask;
return cidr_network_addr == ip_network_addr;
}
-
This is the tcpdump-workers list.
Visit https://cod.sandelman.ca/ to unsubscribe.
Current thread:
- IP range implementation given an IP address and subnet mask Andrej van der Zee (Nov 27)
- Re: IP range implementation given an IP address and subnet mask Andrej van der Zee (Nov 27)
