Replace inet_aton() with inet_pton() to parse IPs correctly (#8825) master
authorKejdi Domi <kejdi.domi@intra2net.com>
Wed, 16 Oct 2024 13:29:01 +0000 (15:29 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 17 Oct 2024 13:01:14 +0000 (15:01 +0200)
inet_aton() modifies IPs in an unwanted manner (1.2 -> 1.0.0.2), so
for proper handling of IPs and for future support of IPv6 addresses,
it was changed to inet_pton() and tested accordingly.

src/ipfunc.cpp
test/ip_range.cpp

index 936844f..f52598c 100644 (file)
@@ -85,7 +85,7 @@ void IP_RANGE::load(type t, const std::string& ip, const std::string& mask_or_en
         
     if (t==IP)
     {
-        if(!inet_aton(ip.c_str(),&ia_ip1))
+        if(!inet_pton(AF_INET, ip.c_str(),&ia_ip1))
             throw runtime_error("invalid IP given: "+ip);
     
         this->ip=ia_ip1.s_addr;
@@ -94,8 +94,7 @@ void IP_RANGE::load(type t, const std::string& ip, const std::string& mask_or_en
     }
     else if (t==NETWORK)
     {
-        // TODO: Better IP checks: "16" != "0.0.0.16"        
-        if(!inet_aton(ip.c_str(),&ia_ip1))
+        if(!inet_pton(AF_INET,ip.c_str(),&ia_ip1))
             throw runtime_error("invalid IP given: "+ip);
         
         // Check if mask is in cidr notation
@@ -109,7 +108,7 @@ void IP_RANGE::load(type t, const std::string& ip, const std::string& mask_or_en
             
             ia_ip2.s_addr = calc_netmask_from_cidr(calc_mask);
         } else
-            if (!inet_aton(mask_or_end.c_str(),&ia_ip2))
+            if (!inet_pton(AF_INET,mask_or_end.c_str(),&ia_ip2))
                 throw runtime_error("invalid IP given: "+mask_or_end);
                 
         this->ip=ia_ip1.s_addr;
@@ -123,10 +122,10 @@ 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))
+        if(!inet_pton(AF_INET,ip.c_str(),&ia_ip1))
             throw runtime_error("invalid IP given: "+ip);
 
-        if(!inet_aton(mask_or_end.c_str(),&ia_ip2))
+        if(!inet_pton(AF_INET,mask_or_end.c_str(),&ia_ip2))
             throw runtime_error("invalid IP given: "+mask_or_end);
         
         this->ip=ia_ip1.s_addr;
@@ -639,7 +638,7 @@ string IP_RANGE::resolve_ip(const string &iporname, const bool enable_ipv6)
 {
     struct in_addr ip_adr;
 
-    if (inet_aton(iporname.c_str(),&ip_adr) != 0)
+    if (inet_pton(AF_INET, iporname.c_str(),&ip_adr) != 0)
     {
         // is already a ip
         return iporname;
index 3f4910c..b520a2c 100644 (file)
@@ -874,5 +874,36 @@ BOOST_AUTO_TEST_CASE(ResolveExistingIP)
     BOOST_CHECK_EQUAL("192.168.1.254", test.resolve_ip("192.168.1.254"));
 }
 
+BOOST_AUTO_TEST_CASE(TestValidIP1)
+{
+    std::string test_string = "1.2.3.4";
+    IP_RANGE test(ip_type::IP, test_string);
+
+    BOOST_CHECK_EQUAL(test_string, test.to_string());
+}
+
+BOOST_AUTO_TEST_CASE(TestValidIP2)
+{
+    std::string test_string = "0.0.0.0";
+    IP_RANGE test(ip_type::IP, test_string);
+
+    BOOST_CHECK_EQUAL(test_string, test.to_string());
+}
+
+BOOST_AUTO_TEST_CASE(TestInvalidIP1)
+{
+    std::string test_string="1.2";
+
+    BOOST_REQUIRE_THROW(IP_RANGE(ip_type::IP, test_string),std::runtime_error);
+}
+
+BOOST_AUTO_TEST_CASE(TestInvalidIP2)
+{
+    std::string test_string="1.02.3.4";
+
+    BOOST_REQUIRE_THROW(IP_RANGE(ip_type::IP, test_string),std::runtime_error);
+}
+
+
 
 BOOST_AUTO_TEST_SUITE_END()