From: Kejdi Domi Date: Wed, 16 Oct 2024 13:29:01 +0000 (+0200) Subject: Replace inet_aton() with inet_pton() to parse IPs correctly (#8825) X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=c816e31dfbdd463c6fbcd79f326403d993c32b50;p=libi2ncommon Replace inet_aton() with inet_pton() to parse IPs correctly (#8825) 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. --- diff --git a/src/ipfunc.cpp b/src/ipfunc.cpp index 936844f..f52598c 100644 --- a/src/ipfunc.cpp +++ b/src/ipfunc.cpp @@ -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; diff --git a/test/ip_range.cpp b/test/ip_range.cpp index 3f4910c..b520a2c 100644 --- a/test/ip_range.cpp +++ b/test/ip_range.cpp @@ -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()