Reputation: 6358
not sure what I am missing. I am trying to determine if an IP address 172.27.12.32 falls in the range of IP address 20.0.0.0 and 255.255.252.0
What I am doing is the following:
std::string one("200.0.0.0");
std::string two("172.27.12.32");
std::string three("255.255.255.255");
long one_addr = inet_addr(one.c_str());
long two_addr = inet_addr(two.c_str());
long three_addr = inet_addr(three.c_str());
one_addr is equal to 200 two_addr is equal to 537664428 three_addr is equal to 4294967295
two_addr is greater than one_addr but 172.27.12.32 is not in the range if the min IP address is 200.0.0.0
How do I determine if 172.27.12.32 is not in range of 200.0.0.0 and 255.255.255.255?
Upvotes: 0
Views: 3804
Reputation: 96937
The inet_addr()
function returns an in_addr_t
struct instance:
in_addr_t inet_addr(const char *cp);
This is often a typedef for a 32-bit integer, which is not always a long on every architecture.
In any case, consider converting 20.0.0.0
and 255.255.252.0
to in_addr_t
values. Then compare those results with your value of interest. What would you expect?
Upvotes: 0
Reputation: 4085
Hint: =) (http://www.stev.org/post/2012/08/09/C++-Check-an-IP-Address-is-in-a-IPMask-range.aspx)
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;
}
Hope thats enough to answer your question
Upvotes: 4