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
+}
+
bool isTCPSocketUsable(int sock);
extern template class NetmaskTree<bool>;
+ComboAddress parseIPAndPort(const std::string& input, uint16_t port);
+
}
}
-
-
-/* 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;
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);
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()