This is a short guide on how to check if an ip address is inside a certain ip / mask combination. An example would be to ensure that an ip address like "192.168.0.10" is inside "192.168.0.0/255.255.255.0"
One of the problems that you will face is that the standard functions for converting ip addresses to an unsigned int typically will also convert the address to network byte order which will not make. You could use the ntohl function to convert it back or a simple parsing functions like below to convert it to an unsigned long using sscanf.
uint32_t IPToUInt(const std::string ip) {
int a, b, c, d;
uint32_t addr = 0;
if (sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
return 0;
addr = a << 24;
addr |= b << 16;
addr |= c << 8;
addr |= d;
return addr;
}
The rest is the very straight forward since an ip address is really just a 32bit number if you convert the ip address the subnet and the mask a straight check can be performed like this.
bool IsIPInRange(const std::string ip, const std::string network, const std::string mask) {
uint32_t ip_addr = IPToUInt(ip);
uint32_t network_addr = IPToUInt(network);
uint32_t mask_addr = IPToUInt(mask);
uint32_t net_lower = (network_addr & mask_addr);
uint32_t net_upper = (net_lower | (~mask_addr));
if (ip_addr >= net_lower &&
ip_addr <= net_upper)
return true;
return false;
}
And just to make sure that it works here is some test code.
void test(const std::string ip, const std::string network, const std::string mask, bool expected) {
if (IsIPInRange(ip, network, mask) != expected) {
printf("Failed! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");
} else {
printf("Success! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");
}
}
int main(int argc, char **argv) {
std::string ip(argv[1]);
test("192.168.1.1", "192.168.1.0", "255.255.255.0", true);
test("192.168.1.1", "192.168.1.2", "255.255.255.255", false);
test("192.168.1.3", "192.168.1.2", "255.255.255.255", false);
test("220.1.1.22", "192.168.1.0", "255.255.255.0", false);
test("220.1.1.22", "220.1.1.22", "255.255.255.255", true);
test("220.1.1.22", "220.1.1.23", "255.255.255.255", false);
test("220.1.1.22", "220.1.1.21", "255.255.255.255", false);
test("0.0.0.1", "0.0.0.0", "0.0.0.0", true);
test("192.168.1.2", "10.0.0.1", "255.255.255.255", false);
return 0;
}
Did You find this page useful?
Yes
No