((a.ip==b.ip) && IP_RANGE::turn_ip(a.end) < IP_RANGE::turn_ip(b.end)));
}
+unsigned int IP_RANGE::get_netmask() const
+{
+ if (t==ip_type::RANGE)
+ throw out_of_range("no netmask for ip range");
+
+ return mask;
+}
+
std::string IP_RANGE::to_string(bool always_mask) const
{
struct in_addr ia_ip;
else
return buffer;
}
+
+std::string IP_RANGE::ip_num_string(unsigned int ip)
+{
+ string target;
+ static const int bufsize=16;
+ unsigned char a0,a1,a2,a3;
+ char buf[bufsize];
+
+ a0=((char*)&ip)[0];
+ a1=((char*)&ip)[1];
+ a2=((char*)&ip)[2];
+ a3=((char*)&ip)[3];
+ snprintf(buf,13,"%03u%03u%03u%03u",a0,a1,a2,a3);
+ buf[12]=0;
+ target=buf;
+
+ return target;
+}
{ return ip; }
ip_type::type get_type() const
{ return t; }
+ unsigned int get_netmask() const;
// static IP utility functions
static unsigned int turn_ip(unsigned int src);
static unsigned int calc_netmask_bits(unsigned int mask);
static unsigned int calc_netmask_from_cidr(unsigned int cidr);
static std::string ip_string(unsigned int ip);
+ static std::string ip_num_string(unsigned int ip);
};
// DEPRECATED!!! use IP_RANGE instead
CPPUNIT_TEST(Substract13);
CPPUNIT_TEST(Substract14);
CPPUNIT_TEST(Substract15);
+ CPPUNIT_TEST(IpNumString1);
+ CPPUNIT_TEST(IpNumString2);
+ CPPUNIT_TEST(IpNumString3);
CPPUNIT_TEST_SUITE_END();
CPPUNIT_ASSERT_EQUAL(check_range,new_range);
}
+
+ void IpNumString1()
+ {
+ IP_RANGE myip("192.168.1.50");
+ string expected("192168001050");
+
+ CPPUNIT_ASSERT_EQUAL(expected,IP_RANGE::ip_num_string(myip.get_base()));
+ }
+
+ void IpNumString2()
+ {
+ IP_RANGE myip("0.0.0.0");
+ string expected("000000000000");
+
+ CPPUNIT_ASSERT_EQUAL(expected,IP_RANGE::ip_num_string(myip.get_base()));
+ }
+
+ void IpNumString3()
+ {
+ IP_RANGE myip("255.255.255.255");
+ string expected("255255255255");
+
+ CPPUNIT_ASSERT_EQUAL(expected,IP_RANGE::ip_num_string(myip.get_base()));
+ }
+
};
CPPUNIT_TEST_SUITE_REGISTRATION(ip_range);