Essentially, I have two 4 byte IP addresses:
u_int32_t daddr; // in the packet
u_int32_t entry; // in the forwarding table
I also have a prefix that goes with entry in the forwarding table:
unsigned short prefix; // in forwarding table corresponding to entry
I need to match the daddr to the entry based on the prefix. I am pretty sure what this means is: if e.g the prefix is 23, then I have to match the first 23 bits of the entry with the daddr. I honestly don’t even know where to start because I don’t know how to match individual bits.
I have a forwarding table with lots of entries which each have a different prefix. I am not sure how to match the da with the correct entry.. Any help would be much appreciated.
My daddr is stored in a standard ip header that I got from netinet ip.h file.
EDIT: I have find the “longest” match. So I am not comparing the entries to only check if they are equal, I am comparing them to determine how many bits are same. The best match is obviously when all of the bits are the same.
To compare only the top
nbits ofaandbof some unsigned typeUInt:To compare the bottom
mbits:Some explanation: The type
UInthassizeof(UInt)bytes, and thusNBITSbits. To compare the topnbits, we simply shift both numbers to the right so that onlynbits remain (the new top bits are filled in with zeros because the type is unsigned). To compare the bottommbits, we shift both numbers to the left until all butmbits have fallen off the left (and zeros are filled in on the right):