if (!sub->is_within(*this))
throw runtime_error("IP_RANGE::substract error: "+ sub->to_string() + " is not within " + to_string());
- // Get beginn of network/range/IP
+ // Get begin of network/range/IP
unsigned int sub_begin = turn_ip(sub->get_base());
// Got a fragment?
- if (ip_pos < sub_begin)
- rtn.push_back(IP_RANGE(ip_type::RANGE, turn_ip(ip_pos), turn_ip(sub_begin-1)));
-
+ if (ip_pos < sub_begin) {
+ if (ip_pos == sub_begin-1)
+ rtn.push_back(IP_RANGE(ip_type::IP, turn_ip(ip_pos)));
+ else
+ rtn.push_back(IP_RANGE(ip_type::RANGE, turn_ip(ip_pos), turn_ip(sub_begin-1)));
+ }
+
// Set position to end if end is higher than current position
if (ip_pos < turn_ip(sub->get_broadcast())+1)
ip_pos = turn_ip(sub->get_broadcast())+1;
CPPUNIT_TEST(Substract12);
CPPUNIT_TEST(Substract13);
CPPUNIT_TEST(Substract14);
+ CPPUNIT_TEST(Substract15);
CPPUNIT_TEST_SUITE_END();
CPPUNIT_ASSERT_EQUAL(check_range,new_range);
}
+
+ void Substract15()
+ {
+ IP_RANGE range("192.168.1.1-192.168.1.100");
+ set<IP_RANGE> to_sub;
+ to_sub.insert(IP_RANGE("192.168.1.1-192.168.1.60"));
+ to_sub.insert(IP_RANGE("192.168.1.62-192.168.1.100"));
+
+ vector<IP_RANGE> new_range = range.substract(to_sub);
+
+ vector<IP_RANGE> check_range;
+ check_range.push_back("192.168.1.61");
+
+ vector<IP_RANGE>::const_iterator it, it_end = new_range.end();
+ for (it = new_range.begin(); it != it_end; it++) {
+ vector<IP_RANGE> cidrs = it->to_cidr();
+ vector<IP_RANGE>::const_iterator cidr, cidrs_end = cidrs.end();
+ for (cidr = cidrs.begin(); cidr != cidrs_end; cidr++)
+ cerr << "Range as CIDR: " << cidr->to_string() << endl;
+ }
+
+ CPPUNIT_ASSERT_EQUAL(check_range,new_range);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(ip_range);