Merge remote branch 'origin/wi2nlog'
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 1 Feb 2011 12:51:39 +0000 (13:51 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 1 Feb 2011 12:51:39 +0000 (13:51 +0100)
configure.in
pclint.lnt [moved from libi2ncommon.lnt with 79% similarity]
src/ipfunc.cpp
src/ipfunc.hxx
src/logfunc.cpp
src/pipestream.hxx
src/stringfunc.cpp
src/tmpfstream.hpp
src/tmpfstream_impl.hpp
test/ip_range.cpp

index 92ccf37..b1c8b82 100644 (file)
@@ -1,7 +1,7 @@
 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
 
similarity index 79%
rename from libi2ncommon.lnt
rename to pclint.lnt
index ce89146..74587e7 100644 (file)
@@ -3,6 +3,3 @@
 // 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
index 7924ef6..6d43bf7 100644 (file)
 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 ||
@@ -90,9 +105,12 @@ void IP_RANGE::load(type t, const std::string& ip, const std::string& mask_or_en
     }
     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;
         
@@ -182,6 +200,41 @@ bool IP_RANGE::overlapping(const IP_RANGE& a) const
     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;
index 505e9a6..06cef4c 100644 (file)
@@ -44,13 +44,18 @@ class IP_RANGE
             { 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);
         
index 15c696f..1a719a2 100644 (file)
@@ -700,9 +700,17 @@ void reopen()
  */
 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)
 
 
index 03ee60e..3dad8b6 100644 (file)
@@ -90,7 +90,7 @@ protected:
 
 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)
@@ -157,7 +157,7 @@ protected:
     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)
index 1fe636a..16cd6bd 100644 (file)
@@ -781,9 +781,11 @@ string html_entities(std::string str)
 {
    // Normal chars
    replace_all (str, "&", "&amp;");
-   replace_all (str, "\"", "&quot;");
    replace_all (str, "<", "&lt;");
    replace_all (str, ">", "&gt;");
+   replace_all (str, "\"", "&quot;");
+   replace_all (str, "'", "&#x27;");
+   replace_all (str, "/", "&#x2F;");
 
    // Umlauts
    replace_all (str, "\xC3\xA4", "&auml;");
index a15df9a..f73924a 100644 (file)
@@ -67,7 +67,7 @@ public:
     * @brief Constructs an unopened tmpfstreamTempl.
     */
     tmpfstreamTempl()
-        : unlinked(false), bio::stream<Device, Tr, Alloc>()
+        : unlinked(false), fd(-1), bio::stream<Device, Tr, Alloc>()
         { }
 
     /**
@@ -83,7 +83,7 @@ public:
     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);
     }
index dafe706..514369b 100644 (file)
@@ -74,7 +74,7 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::open(const std::string& tmpnametemplate,
     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();
index e18cf41..dce227e 100644 (file)
@@ -33,6 +33,15 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
 
     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
 
 
@@ -769,4 +778,61 @@ BOOST_AUTO_TEST_CASE(IpNumString3)
     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()