]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Move parseIPAndPort() to iputils and add unit test
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 1 Sep 2020 10:01:09 +0000 (12:01 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 5 Oct 2020 11:25:54 +0000 (13:25 +0200)
pdns/iputils.cc
pdns/iputils.hh
pdns/reczones.cc
pdns/syncres.hh
pdns/test-iputils_hh.cc

index 624746db80375abc9769d3c0116c438c85380b5e..19b84833e3ed4541365911b323d746818eac7df3 100644 (file)
@@ -436,3 +436,36 @@ bool isTCPSocketUsable(int sock)
 
   return false;
 }
+/* mission in life: parse three cases
+   1) 1.2.3.4
+   2) 1.2.3.4:5300
+   3) 2001::1
+   4) [2002::1]:53
+*/
+
+ComboAddress parseIPAndPort(const std::string& input, uint16_t port)
+{
+  if (input.find(':') == string::npos || input.empty()) { // common case
+    return ComboAddress(input, port);
+  }
+
+  pair<string,string> both;
+  try { // case 2
+    string::size_type cpos = input.rfind(':');
+    both.first = input.substr(0, cpos);
+    both.second = input.substr(cpos + 1);
+
+    uint16_t newport = static_cast<uint16_t>(pdns_stou(both.second));
+    return ComboAddress(both.first, newport);
+  }
+  catch(...) {
+  }
+
+  if (input[0] == '[') { // case 4
+    both = splitField(input.substr(1), ']');
+    return ComboAddress(both.first, both.second.empty() ? port : static_cast<uint16_t>(pdns_stou(both.second.substr(1))));
+  }
+
+  return ComboAddress(input, port); // case 3
+}
+
index 06ce5f2c5e1920eadff2be6bf0b0ba1b5fafc12a..355f61b672b4b2ac56313ab2f342fa7740bdf606 100644 (file)
@@ -1440,3 +1440,5 @@ size_t sendMsgWithOptions(int fd, const char* buffer, size_t len, const ComboAdd
 bool isTCPSocketUsable(int sock);
 
 extern template class NetmaskTree<bool>;
+ComboAddress parseIPAndPort(const std::string& input, uint16_t port);
+
index 487781b2893f580d6d455a421b0332a415318b08..5c049b1c3afb5dd614ce772f55a277d539e74227 100644 (file)
@@ -250,42 +250,6 @@ static void makeIPToNamesZone(std::shared_ptr<SyncRes::domainmap_t> newMap, cons
   }
 }
 
-
-
-/* mission in life: parse three cases
-   1) 1.2.3.4
-   2) 1.2.3.4:5300
-   3) 2001::1
-   4) [2002::1]:53
-*/
-
-ComboAddress parseIPAndPort(const std::string& input, uint16_t port)
-{
-  if (input.find(':') == string::npos || input.empty()) { // common case
-    return ComboAddress(input, port);
-  }
-
-  pair<string,string> both;
-  try { // case 2
-    string::size_type cpos = input.rfind(':');
-    both.first = input.substr(0, cpos);
-    both.second = input.substr(cpos + 1);
-
-    uint16_t newport = static_cast<uint16_t>(pdns_stou(both.second));
-    return ComboAddress(both.first, newport);
-  }
-  catch(...) {
-  }
-
-  if (input[0] == '[') { // case 4
-    both = splitField(input.substr(1), ']');
-    return ComboAddress(both.first, both.second.empty() ? port : static_cast<uint16_t>(pdns_stou(both.second.substr(1))));
-  }
-
-  return ComboAddress(input, port); // case 3
-}
-
-
 static void convertServersForAD(const std::string& input, SyncRes::AuthDomain& ad, const char* sepa, bool verbose=true)
 {
   vector<string> servers;
index 616ebd0a84f4f8aba8c72724b6c018852544f659..69d28966060c83cea7e3b1ab3ff576347f3781e2 100644 (file)
@@ -1080,7 +1080,6 @@ extern bool g_lowercaseOutgoing;
 
 
 std::string reloadAuthAndForwards();
-ComboAddress parseIPAndPort(const std::string& input, uint16_t port);
 typedef boost::function<void*(void)> pipefunc_t;
 void broadcastFunction(const pipefunc_t& func);
 void distributeAsyncFunction(const std::string& question, const pipefunc_t& func);
index cc543929535a21bf9e81585359196739b9775599..06fdc69d3b453b8dc5e6ad9d0c8b08cf8f87c4d4 100644 (file)
@@ -820,4 +820,35 @@ BOOST_AUTO_TEST_CASE(test_ComboAddress_caContainerToString) {
   BOOST_CHECK_EQUAL(caVectorStr, "192.0.2.1:53,192.0.2.2:5300,[2001:db8:53::3]:53,[2001:db8:53::4]:5300");
 }
 
+BOOST_AUTO_TEST_CASE(test_parseIPAndPort)
+{
+  struct {
+    std::string str;
+    uint16_t port;
+    std::string result;
+    bool ex;
+  } tests[] = {
+    { "", 0, "", true },
+    { "1.2.3.a", 53, "", true },
+    { "1::g3", 99, "", true },
+    { "1.2.3.4", 0, "1.2.3.4:0", false },
+    { "1.2.3.4", 999, "1.2.3.4:999", false },
+    { "1::", 999, "[1::]:999", false },
+    { "1::33:99", 0, "[1::33]:99", false },
+    { "[1::33]:99", 0, "[1::33]:99", false },
+    { "1:33::99", 0, "1:33::99", false },
+    { "[1:33::]:99", 0, "[1:33::]:99", false },
+    { "2003:1234::f561", 53, "[2003:1234::f561]:53", false },
+  };
+
+  for (const auto& t : tests) {
+    if (t.ex) {
+      BOOST_CHECK_THROW(parseIPAndPort(t.str, t.port), PDNSException);
+    } else {
+      ComboAddress a = parseIPAndPort(t.str, t.port);
+      BOOST_CHECK_EQUAL(a.toString(), ComboAddress(t.result).toString());
+    }
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END()