libi2ncommon: (gerd) add ip_num_string function needed for ip accounting table names
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Sun, 5 Sep 2004 23:35:25 +0000 (23:35 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Sun, 5 Sep 2004 23:35:25 +0000 (23:35 +0000)
src/ipfunc.cpp
src/ipfunc.hxx
test/ip_range.cpp

index a9cd704..2000a23 100644 (file)
@@ -211,6 +211,14 @@ bool operator<(const IP_RANGE& a, const IP_RANGE& b)
             ((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;
@@ -527,3 +535,21 @@ std::string IP_RANGE::ip_string(unsigned int 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;
+}
index 00f24bc..2eea18b 100644 (file)
@@ -70,12 +70,14 @@ class IP_RANGE
             { 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
index 92e76a0..3048507 100644 (file)
@@ -116,6 +116,9 @@ class ip_range : public TestFixture
     CPPUNIT_TEST(Substract13);
     CPPUNIT_TEST(Substract14);
     CPPUNIT_TEST(Substract15);
+    CPPUNIT_TEST(IpNumString1);
+    CPPUNIT_TEST(IpNumString2);
+    CPPUNIT_TEST(IpNumString3);
     
     CPPUNIT_TEST_SUITE_END();
     
@@ -834,6 +837,31 @@ class ip_range : public TestFixture
             
             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);