AC_INIT(configure.in)
AM_CONFIG_HEADER(config.h)
-AM_INIT_AUTOMAKE(libi2ncommon, 1.6)
+AM_INIT_AUTOMAKE(libi2ncommon, 1.8)
LIBI2NCOMMON_LIB_VERSION=2:0:0
// Don't complain about bitset<7>::set return value
-esym(534, std::bitset<*>::set)
-esym(534, std::bitset<*>::reset)
-
-// Allow enum to int conversion
-+fie
using namespace std;
using namespace ip_type;
+IP_RANGE& IP_RANGE::operator=(const IP_RANGE& other)
+{
+ IP_RANGE temp(other);
+ temp.swap(*this);
+ return *this;
+}
+
+void IP_RANGE::swap(IP_RANGE& other)
+{
+ std::swap(ip,other.ip);
+ std::swap(mask,other.mask);
+ std::swap(end,other.end);
+ std::swap(t,other.t);
+}
+
// can decode IP, IP-IP (as range) and IP/MASK (as network)
void IP_RANGE::load(const std::string& ip)
{
- int delim_pos;
+ string::size_type delim_pos;
struct in_addr ia_ip1, ia_ip2;
if ((delim_pos=ip.find('/')) != string::npos ||
}
else if (t==RANGE)
{
- if(!inet_aton(ip.c_str(),&ia_ip1) || !inet_aton(mask_or_end.c_str(),&ia_ip2))
+ if(!inet_aton(ip.c_str(),&ia_ip1))
throw runtime_error("invalid IP given: "+ip);
-
+
+ if(!inet_aton(mask_or_end.c_str(),&ia_ip2))
+ throw runtime_error("invalid IP given: "+mask_or_end);
+
this->ip=ia_ip1.s_addr;
this->end=ia_ip2.s_addr;
return false;
}
+/** @brief map this IP_RANGE to another network (resembles iptables NETMAP-target)
+ @param original_net the original net that is netmapped. *this needs to be within it.
+ @param target_net the net that original_net is netmapped to.
+ @retval true if the netmap was successful, false in case of an error
+ @note no support for overlapping nets or ranges
+*/
+bool IP_RANGE::netmap(const IP_RANGE& original_net, const IP_RANGE& target_net)
+{
+ // input validation
+ if (original_net.t != ip_type::NETWORK ||
+ target_net.t != ip_type::NETWORK)
+ return false;
+
+ if (original_net.mask != target_net.mask)
+ return false;
+
+ // only map if we are within the original net
+ if (!is_within(original_net))
+ return false;
+
+ // everything ok, ready to go
+
+ // difference to start of original net
+ int ipno=turn_ip(ip)-turn_ip(original_net.ip);
+
+ // add that difference to the target net
+ ip=turn_ip(turn_ip(target_net.ip)+ipno);
+
+ // turn the end too
+ ipno=turn_ip(end)-turn_ip(original_net.ip);
+ end=turn_ip(turn_ip(target_net.ip)+ipno);
+
+ return true;
+}
+
IP_RANGE operator+(const IP_RANGE &base, const int &ips)
{
IP_RANGE ret=base;
{ load(t,ip,mask_or_end); }
IP_RANGE(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0)
{ load(t,ip,mask_or_end); }
-
+
+ IP_RANGE& operator=(const IP_RANGE& other);
+ void swap(IP_RANGE& other);
+
void load(const std::string& ip); // can decode IP-IP (as range) and IP/MASK (as network)
void load(ip_type::type t, const std::string& ip, const std::string& mask_or_end="");
void load(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0);
bool is_within(const IP_RANGE& a) const;
bool overlapping(const IP_RANGE& a) const;
+
+ bool netmap(const IP_RANGE& original_net, const IP_RANGE& target_net);
int operator-(const IP_RANGE &other);
*/
int set_log_level(int level)
{
- int result = g_max_level;
- g_max_level = std::max( LOG_CRIT, level );
- return result;
+ int previous = g_max_level;
+
+ // Sanity check
+ if (level < LogLevel::Emergency)
+ level = LogLevel::Emergency;
+ else if (level > LogLevel::Debug)
+ level = LogLevel::Debug;
+
+ g_max_level = level;
+
+ return previous;
} // eo set_log_level(int)
public:
inpipestream(const std::string& command)
- : buf(command), std::istream(&buf)
+ : std::istream(&buf), buf(command)
{}
void store_exit_status(bool *_status_set, int *_exit_status)
outpipebuf buf;
public:
outpipestream(const std::string& command)
- : buf(command), std::ostream(&buf)
+ : std::ostream(&buf), buf(command)
{}
void store_exit_status(bool *_status_set, int *_exit_status)
{
// Normal chars
replace_all (str, "&", "&");
- replace_all (str, "\"", """);
replace_all (str, "<", "<");
replace_all (str, ">", ">");
+ replace_all (str, "\"", """);
+ replace_all (str, "'", "'");
+ replace_all (str, "/", "/");
// Umlauts
replace_all (str, "\xC3\xA4", "ä");
* @brief Constructs an unopened tmpfstreamTempl.
*/
tmpfstreamTempl()
- : unlinked(false), bio::stream<Device, Tr, Alloc>()
+ : unlinked(false), fd(-1), bio::stream<Device, Tr, Alloc>()
{ }
/**
tmpfstreamTempl(const std::string& tmpnametemplate,
std::ios_base::open_mode mode = std::ios_base::out,
int buffer_size = -1 , int pback_size = -1)
- : unlinked(false), bio::stream<Device, Tr, Alloc>()
+ : unlinked(false), fd(-1), bio::stream<Device, Tr, Alloc>()
{
open(tmpnametemplate,mode,buffer_size,pback_size);
}
if (fd==-1)
return false;
- boost::iostreams::stream<Device,Tr,Alloc>::open(Device(fd,true),
+ boost::iostreams::stream<Device,Tr,Alloc>::open(Device(fd, boost::iostreams::close_handle),
buffer_size,pback_size);
return tmpfstreamTempl<Device,Tr,Alloc>::is_open();
return os;
}
+
+template <class charT, class traits>
+inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os,
+ const IP_RANGE& x)
+{
+ os << x.to_string();
+ return os;
+}
+
} // eo namespace std
BOOST_CHECK_EQUAL(expected,IP_RANGE::ip_num_string(myip.get_base()));
}
+BOOST_AUTO_TEST_CASE(NetmapValidation1)
+{
+ IP_RANGE myip("192.168.1.11");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.0.0");
+
+ BOOST_CHECK_EQUAL(false,myip.netmap(original,target));
+}
+
+BOOST_AUTO_TEST_CASE(NetmapValidation2)
+{
+ IP_RANGE myip("192.168.3.11");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.255.0");
+
+ BOOST_CHECK_EQUAL(false,myip.netmap(original,target));
+}
+
+BOOST_AUTO_TEST_CASE(NetmapValidation3)
+{
+ IP_RANGE myip("192.168.1.70-192.168.2.20");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.255.0");
+
+ BOOST_CHECK_EQUAL(false,myip.netmap(original,target));
+}
+
+BOOST_AUTO_TEST_CASE(Netmap1)
+{
+ IP_RANGE myip("192.168.1.11");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.255.0");
+
+ BOOST_CHECK_EQUAL(true,myip.netmap(original,target));
+ BOOST_CHECK_EQUAL(IP_RANGE("192.168.2.11"),myip);
+}
+
+BOOST_AUTO_TEST_CASE(Netmap2)
+{
+ IP_RANGE myip("192.168.1.11-192.168.1.22");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.255.0");
+
+ BOOST_CHECK_EQUAL(true,myip.netmap(original,target));
+ BOOST_CHECK_EQUAL(IP_RANGE("192.168.2.11-192.168.2.22"),myip);
+}
+
+BOOST_AUTO_TEST_CASE(Netmap3)
+{
+ IP_RANGE myip("192.168.1.0/255.255.255.0");
+ IP_RANGE original("192.168.1.0/255.255.255.0");
+ IP_RANGE target("192.168.2.0/255.255.255.0");
+
+ BOOST_CHECK_EQUAL(true,myip.netmap(original,target));
+ BOOST_CHECK_EQUAL(IP_RANGE("192.168.2.0/255.255.255.0"),myip);
+}
+
BOOST_AUTO_TEST_SUITE_END()