How to check if an IP address falls in a subnet based on subnet and subnet mask
This can be achieved easily with binary AND (&) operation as shown the function below.
typedef unsigned long IPv4Addr;
/* Function returns 1 if the IP falls in the subnet. Otherwise returns 0. */
int IsIpInTheSubnet(IPv4Addr ip, IPv4Addr subnet, IPv4Addr mask)
if((ip & mask) == (subnet & mask)) return 1;
The above function can easily be converted in to a macro to make it simple and more efficient.
This is what preciselyTCP/IP networking stack does for finding a route in the system routing table before sending a packet out.