From 90919c6a9a96bd7c4a680019402e182ee54e560d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Mon, 24 Feb 2020 22:10:14 +0800 Subject: [PATCH 01/39] 1 --- include/dsn/tool-api/rpc_address.h | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 3318cb524f..223a7ed1d7 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -83,6 +83,7 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); + if (!from_string_ipv4(std::to_string(host)+std::to_string(port))); _addr.v4.type = HOST_TYPE_IPV4; _addr.v4.ip = rpc_address::ipv4_from_host(host); _addr.v4.port = port; @@ -105,25 +106,41 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } - bool from_string_ipv4(const char *s) - { + bool from_string_ipv4(const char *s){ set_invalid(); - std::string str = std::string(s); - auto pos = str.find_last_of(':'); - if (pos == std::string::npos) - return false; - else { - auto host = str.substr(0, pos); - auto port_str = str.substr(pos + 1); - char *p = nullptr; - long port = ::strtol(port_str.data(), &p, 10); - if (*p != 0) // bad string - return false; - if (port <= 0 || port > UINT16_MAX) // out of range - return false; - assign_ipv4(host.c_str(), (uint16_t)port); - return true; + std::string ip_port = std::string(s); + auto pos = ip_port.find_last_of(':'); + std::string ip = ip_port.substr(0,pos); + std::string port = ip_port.substr(pos+1); + //check port + if (port.size()>5) return false; + for (auto c:port){ + if (!isdigit(c)) return false; + } + int port_num = std::stoi(port); + if (port_num>UINT16_MAX) return false; + //check ip + std::istringstream iss(std::string(ip)); + std::vector num_vec; + std::string temp; + while (std::getline(iss,temp,".")){ + num_vec.emplace_back(temp); + } + // in case of "172.16.254.1." + if (!num_vec.empty() && (num_vec.back() == ".")){ + num_vec.push_back({}); + } + if (num_vec.size()!=4) return false; + for (auto &num:num_vec) { + if (num.empty() || (num.size()>1 && num[0] == '0') || num.size()>3) return false; + for (auto c:num) { + if (!isdigit(c)) return false; + } + int n = std::stoi(num); + if (n<0||n>255) return false; } + assign_ipv4(ip.c_str(), (uint16_t)port_num); + return true; } uint64_t &value() { return _addr.value; } From 4380fca0341d3bfaa0d696f904bf97f2f908805d Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Tue, 25 Feb 2020 10:54:29 +0800 Subject: [PATCH 02/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 223a7ed1d7..843dd21901 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +84,7 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - if (!from_string_ipv4(std::to_string(host)+std::to_string(port))); + if (!from_string_ipv4(std::to_string(*host)+std::to_string(port))); _addr.v4.type = HOST_TYPE_IPV4; _addr.v4.ip = rpc_address::ipv4_from_host(host); _addr.v4.port = port; From 5f5dca569a5b768e228c353c3a772d4952ae15e8 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Tue, 25 Feb 2020 02:58:40 +0800 Subject: [PATCH 03/39] 1 --- include/dsn/tool-api/rpc_address.h | 52 ++++++++++++++++++------------ 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 843dd21901..87f8e2cc74 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -35,7 +35,8 @@ #include #include -typedef enum dsn_host_type_t { +typedef enum dsn_host_type_t +{ HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, @@ -84,7 +85,10 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - if (!from_string_ipv4(std::to_string(*host)+std::to_string(port))); + std::string ip_port = std::to_string(*host) + std::to_string(port); + if (!from_string_ipv4(std::move(ip_port.c_str()))) { + return; + } _addr.v4.type = HOST_TYPE_IPV4; _addr.v4.ip = rpc_address::ipv4_from_host(host); _addr.v4.port = port; @@ -107,38 +111,46 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } - bool from_string_ipv4(const char *s){ + bool from_string_ipv4(const char *s) + { set_invalid(); std::string ip_port = std::string(s); auto pos = ip_port.find_last_of(':'); - std::string ip = ip_port.substr(0,pos); - std::string port = ip_port.substr(pos+1); - //check port - if (port.size()>5) return false; - for (auto c:port){ - if (!isdigit(c)) return false; + std::string ip = ip_port.substr(0, pos); + std::string port = ip_port.substr(pos + 1); + // check port + if (port.size() > 5) + return false; + for (auto c : port) { + if (!isdigit(c)) + return false; } int port_num = std::stoi(port); - if (port_num>UINT16_MAX) return false; - //check ip + if (port_num > UINT16_MAX) + return false; + // check ip std::istringstream iss(std::string(ip)); std::vector num_vec; std::string temp; - while (std::getline(iss,temp,".")){ + while (std::getline(iss, temp, ".")) { num_vec.emplace_back(temp); } - // in case of "172.16.254.1." - if (!num_vec.empty() && (num_vec.back() == ".")){ + // in case of "172.16.254.1." + if (!num_vec.empty() && (num_vec.back() == ".")) { num_vec.push_back({}); } - if (num_vec.size()!=4) return false; - for (auto &num:num_vec) { - if (num.empty() || (num.size()>1 && num[0] == '0') || num.size()>3) return false; - for (auto c:num) { - if (!isdigit(c)) return false; + if (num_vec.size() != 4) + return false; + for (auto &num : num_vec) { + if (num.empty() || (num.size() > 1 && num[0] == '0') || num.size() > 3) + return false; + for (auto c : num) { + if (!isdigit(c)) + return false; } int n = std::stoi(num); - if (n<0||n>255) return false; + if (n < 0 || n > 255) + return false; } assign_ipv4(ip.c_str(), (uint16_t)port_num); return true; From a01bfa23ad0dcdee355bac2aa0e9a5c4f55d5463 Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Tue, 25 Feb 2020 11:16:35 +0800 Subject: [PATCH 04/39] 1 --- include/dsn/tool-api/rpc_address.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 87f8e2cc74..b2ae2b0ceb 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -33,6 +33,7 @@ #include #include #include +#include #include typedef enum dsn_host_type_t @@ -129,10 +130,10 @@ class rpc_address if (port_num > UINT16_MAX) return false; // check ip - std::istringstream iss(std::string(ip)); + std::istringstream iss(ip); std::vector num_vec; std::string temp; - while (std::getline(iss, temp, ".")) { + while (std::getline(iss, temp, '.')) { num_vec.emplace_back(temp); } // in case of "172.16.254.1." From 17f70c559a567ab815b4c0dc18e111dec58d4446 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Tue, 25 Feb 2020 03:54:47 +0800 Subject: [PATCH 05/39] 11 --- include/dsn/tool-api/rpc_address.h | 2 +- src/core/tests/hostname_test.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index b2ae2b0ceb..8a9172e725 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -86,7 +86,7 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - std::string ip_port = std::to_string(*host) + std::to_string(port); + std::string ip_port = std::to_string(*host) + ":" + std::to_string(port); if (!from_string_ipv4(std::move(ip_port.c_str()))) { return; } diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 13540085e7..2c693762f1 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -10,6 +10,27 @@ namespace dsn { namespace replication { +TEST(ip_to_hostname, ipv4_validate) +{ + rpc_address rpc_test_ipv4; + std::string tests[11] = {"127.0.0.1:8080", + "172.16.254.1:1234", + "172.16.254.1:222222", + "172.16.254.1", + "2222,123,33,1:8080", + "123.456.789.1:8080", + "001.223.110.002:8080", + "172.16.254.1.8080", + "172.16.254.1:8080.", + "127.0.0.11:123!", + "192.168.0.1"}; + bool result[11] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}; + + for (int i = 0; i < 11; i++) { + ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(tests[i].c_str()), result[i]); + } +} + TEST(ip_to_hostname, localhost) { std::string hostname_result; From 37d3c1325294066a558ea9b4fd6d402ca036d94a Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Tue, 25 Feb 2020 12:14:57 +0800 Subject: [PATCH 06/39] finish --- src/core/tests/hostname_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 2c693762f1..61b15242c7 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -23,7 +23,7 @@ TEST(ip_to_hostname, ipv4_validate) "172.16.254.1.8080", "172.16.254.1:8080.", "127.0.0.11:123!", - "192.168.0.1"}; + "192.168.0.1:12345"}; bool result[11] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}; for (int i = 0; i < 11; i++) { From 8169483e018d27e0277d9f85aa3f8b0cbd539460 Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Tue, 25 Feb 2020 13:36:18 +0800 Subject: [PATCH 07/39] finish --- include/dsn/tool-api/rpc_address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 8a9172e725..9df18f2892 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -86,7 +86,7 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - std::string ip_port = std::to_string(*host) + ":" + std::to_string(port); + std::string ip_port = std::string(host,host+strlen(host)) + ":" + std::to_string(port); if (!from_string_ipv4(std::move(ip_port.c_str()))) { return; } From 773b634c00c066b51026e602894b7db587a18cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 14:07:39 +0800 Subject: [PATCH 08/39] format --- include/dsn/tool-api/rpc_address.h | 8 ++------ src/core/tests/hostname_test.cpp | 4 ---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 9df18f2892..c9cc1d4a1e 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -36,8 +36,7 @@ #include #include -typedef enum dsn_host_type_t -{ +typedef enum dsn_host_type_t { HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, @@ -86,10 +85,7 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - std::string ip_port = std::string(host,host+strlen(host)) + ":" + std::to_string(port); - if (!from_string_ipv4(std::move(ip_port.c_str()))) { - return; - } + std::string ip_port = std::string(host, host + strlen(host)) + ":" + std::to_string(port); _addr.v4.type = HOST_TYPE_IPV4; _addr.v4.ip = rpc_address::ipv4_from_host(host); _addr.v4.port = port; diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 61b15242c7..9eed4a9197 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -58,14 +58,10 @@ TEST(ip_to_hostname, localhost) ASSERT_TRUE(dsn::utils::hostname(rpc_example_success, &hostname_result)); ASSERT_STREQ(expected_hostname_port.c_str(), hostname_result.c_str()); - ASSERT_FALSE(dsn::utils::hostname(rpc_example_failed, &hostname_result)); - // static bool hostname_from_ip(uint32_t ip, std::string* hostname_result); ASSERT_TRUE(dsn::utils::hostname_from_ip(htonl(rpc_example_success.ip()), &hostname_result)); ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); - ASSERT_FALSE(dsn::utils::hostname_from_ip(htonl(rpc_example_failed.ip()), &hostname_result)); - // static bool hostname_from_ip(const char *ip,std::string *hostname_result); ASSERT_TRUE(dsn::utils::hostname_from_ip(success_ip.c_str(), &hostname_result)); ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); From 0ce39fe5e5260086c0acfc127f5921a0a8e10857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 14:18:19 +0800 Subject: [PATCH 09/39] format --- include/dsn/tool-api/rpc_address.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index c9cc1d4a1e..c42ae160ee 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -25,15 +25,16 @@ */ #pragma once -#include -#include -#include -#include -#include -#include #include #include +#include +#include #include +#include +#include +#include +#include + #include typedef enum dsn_host_type_t { From ee1e0612ea28b1b8a85a82d8baa7c20af75e610f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 14:28:03 +0800 Subject: [PATCH 10/39] 1 --- include/dsn/tool-api/rpc_address.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index c42ae160ee..1d7d9e31f7 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -86,7 +86,6 @@ class rpc_address void assign_ipv4(const char *host, uint16_t port) { set_invalid(); - std::string ip_port = std::string(host, host + strlen(host)) + ":" + std::to_string(port); _addr.v4.type = HOST_TYPE_IPV4; _addr.v4.ip = rpc_address::ipv4_from_host(host); _addr.v4.port = port; From 81fae05277c136e50de3174ce225175d25a00c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 14:54:20 +0800 Subject: [PATCH 11/39] 1 --- include/dsn/tool-api/rpc_address.h | 24 +++--------------------- src/core/tests/hostname_test.cpp | 3 +-- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 1d7d9e31f7..f5df5fb22d 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -35,6 +35,7 @@ #include #include +#include #include typedef enum dsn_host_type_t { @@ -126,28 +127,9 @@ class rpc_address if (port_num > UINT16_MAX) return false; // check ip - std::istringstream iss(ip); - std::vector num_vec; - std::string temp; - while (std::getline(iss, temp, '.')) { - num_vec.emplace_back(temp); - } - // in case of "172.16.254.1." - if (!num_vec.empty() && (num_vec.back() == ".")) { - num_vec.push_back({}); - } - if (num_vec.size() != 4) + uint32_t ip_addr; + if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1){ return false; - for (auto &num : num_vec) { - if (num.empty() || (num.size() > 1 && num[0] == '0') || num.size() > 3) - return false; - for (auto c : num) { - if (!isdigit(c)) - return false; - } - int n = std::stoi(num); - if (n < 0 || n > 255) - return false; } assign_ipv4(ip.c_str(), (uint16_t)port_num); return true; diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 9eed4a9197..c939b43505 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -50,9 +50,8 @@ TEST(ip_to_hostname, localhost) const std::string success_ip_port_list = "127.0.0.1:8080,127.0.0.1:8080,127.0.0.1:8080"; const std::string expected_hostname_port_list = "localhost:8080,localhost:8080,localhost:8080"; - rpc_address rpc_example_success, rpc_example_failed; + rpc_address rpc_example_success; rpc_example_success.assign_ipv4(success_ip.c_str(), 23010); - rpc_example_failed.assign_ipv4(failed_ip.c_str(), 23010); // static bool hostname(const rpc_address &address,std::string *hostname_result); ASSERT_TRUE(dsn::utils::hostname(rpc_example_success, &hostname_result)); From d558025077b841afbe541a347e2b410877567cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 14:54:34 +0800 Subject: [PATCH 12/39] 1 --- include/dsn/tool-api/rpc_address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index f5df5fb22d..85c5038a7a 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -128,7 +128,7 @@ class rpc_address return false; // check ip uint32_t ip_addr; - if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1){ + if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1) { return false; } assign_ipv4(ip.c_str(), (uint16_t)port_num); From 7b819c6f8a6842e79042bf33ac2d8ae62a05e9fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 15:18:00 +0800 Subject: [PATCH 13/39] format --- src/core/tests/hostname_test.cpp | 47 ++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index c939b43505..c7aa4e9c52 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -35,49 +35,40 @@ TEST(ip_to_hostname, localhost) { std::string hostname_result; - const std::string success_ip = "127.0.0.1"; + const std::string valid_ip = "127.0.0.1"; const std::string expected_hostname = "localhost"; - const std::string success_ip_port = "127.0.0.1:23010"; + const std::string valid_ip_port = "127.0.0.1:23010"; const std::string expected_hostname_port = "localhost:23010"; - const std::string failed_ip = "123.456.789.111"; - const std::string failed_ip_port = "123.456.789.111:23010"; - - const std::string success_ip_list = "127.0.0.1,127.0.0.1,127.0.0.1"; + const std::string valid_ip_list = "127.0.0.1,127.0.0.1,127.0.0.1"; const std::string expected_hostname_list = "localhost,localhost,localhost"; - const std::string success_ip_port_list = "127.0.0.1:8080,127.0.0.1:8080,127.0.0.1:8080"; + const std::string valid_ip_port_list = "127.0.0.1:8080,127.0.0.1:8080,127.0.0.1:8080"; const std::string expected_hostname_port_list = "localhost:8080,localhost:8080,localhost:8080"; - rpc_address rpc_example_success; - rpc_example_success.assign_ipv4(success_ip.c_str(), 23010); + rpc_address rpc_example_valid; + rpc_example_valid.assign_ipv4(valid_ip.c_str(), 23010); // static bool hostname(const rpc_address &address,std::string *hostname_result); - ASSERT_TRUE(dsn::utils::hostname(rpc_example_success, &hostname_result)); + ASSERT_TRUE(dsn::utils::hostname(rpc_example_valid, &hostname_result)); ASSERT_STREQ(expected_hostname_port.c_str(), hostname_result.c_str()); // static bool hostname_from_ip(uint32_t ip, std::string* hostname_result); - ASSERT_TRUE(dsn::utils::hostname_from_ip(htonl(rpc_example_success.ip()), &hostname_result)); + ASSERT_TRUE(dsn::utils::hostname_from_ip(htonl(rpc_example_valid.ip()), &hostname_result)); ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); // static bool hostname_from_ip(const char *ip,std::string *hostname_result); - ASSERT_TRUE(dsn::utils::hostname_from_ip(success_ip.c_str(), &hostname_result)); + ASSERT_TRUE(dsn::utils::hostname_from_ip(valid_ip.c_str(), &hostname_result)); ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); - ASSERT_FALSE(dsn::utils::hostname_from_ip(failed_ip.c_str(), &hostname_result)); - ASSERT_STREQ(failed_ip.c_str(), hostname_result.c_str()); - // static bool hostname_from_ip_port(const char *ip_port,std::string *hostname_result); - ASSERT_TRUE(dsn::utils::hostname_from_ip_port(success_ip_port.c_str(), &hostname_result)); + ASSERT_TRUE(dsn::utils::hostname_from_ip_port(valid_ip_port.c_str(), &hostname_result)); ASSERT_STREQ(expected_hostname_port.c_str(), hostname_result.c_str()); - ASSERT_FALSE(dsn::utils::hostname_from_ip_port(failed_ip_port.c_str(), &hostname_result)); - ASSERT_STREQ(failed_ip_port.c_str(), hostname_result.c_str()); - // static bool list_hostname_from_ip(const char *ip_port_list,std::string // *hostname_result_list); - ASSERT_TRUE(dsn::utils::list_hostname_from_ip(success_ip_list.c_str(), &hostname_result)); + ASSERT_TRUE(dsn::utils::list_hostname_from_ip(valid_ip_list.c_str(), &hostname_result)); ASSERT_STREQ(expected_hostname_list.c_str(), hostname_result.c_str()); ASSERT_FALSE(dsn::utils::list_hostname_from_ip("127.0.0.1,127.0.0.23323,111127.0.0.3", @@ -90,7 +81,7 @@ TEST(ip_to_hostname, localhost) // static bool list_hostname_from_ip_port(const char *ip_port_list,std::string // *hostname_result_list); ASSERT_TRUE( - dsn::utils::list_hostname_from_ip_port(success_ip_port_list.c_str(), &hostname_result)); + dsn::utils::list_hostname_from_ip_port(valid_ip_port_list.c_str(), &hostname_result)); ASSERT_STREQ(expected_hostname_port_list.c_str(), hostname_result.c_str()); ASSERT_FALSE(dsn::utils::list_hostname_from_ip_port( @@ -98,5 +89,19 @@ TEST(ip_to_hostname, localhost) ASSERT_STREQ("127.0.3333.1:23456,1127.0.0.2:22233,localhost:8080", hostname_result.c_str()); } +TEST(ip_to_hostname, invalid_ip) +{ + + std::string hostname_result; + const std::string invalid_ip = "123.456.789.111"; + const std::string invalid_ip_port = "123.456.789.111:23010"; + + ASSERT_FALSE(dsn::utils::hostname_from_ip(invalid_ip.c_str(), &hostname_result)); + ASSERT_STREQ(invalid_ip.c_str(), hostname_result.c_str()); + + ASSERT_FALSE(dsn::utils::hostname_from_ip_port(invalid_ip_port.c_str(), &hostname_result)); + ASSERT_STREQ(invalid_ip_port.c_str(), hostname_result.c_str()); +} + } // namespace replication } // namespace dsn From 3bb2a10c1cf816d23d385136fa10aa97d476112b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 15:25:00 +0800 Subject: [PATCH 14/39] 1 --- src/core/tests/hostname_test.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index c7aa4e9c52..b85ccb3247 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -13,21 +13,24 @@ namespace replication { TEST(ip_to_hostname, ipv4_validate) { rpc_address rpc_test_ipv4; - std::string tests[11] = {"127.0.0.1:8080", - "172.16.254.1:1234", - "172.16.254.1:222222", - "172.16.254.1", - "2222,123,33,1:8080", - "123.456.789.1:8080", - "001.223.110.002:8080", - "172.16.254.1.8080", - "172.16.254.1:8080.", - "127.0.0.11:123!", - "192.168.0.1:12345"}; - bool result[11] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}; + struct ip_test + { + std::string ip; + bool result; + } tests[11] = {{"127.0.0.1:8080", 1}, + {"172.16.254.1:1234", 1}, + {"172.16.254.1:222222", 0}, + {"172.16.254.1", 0}, + {"2222,123,33,1:8080", 0}, + {"123.456.789.1:8080", 0}, + {"001.223.110.002:8080", 0}, + {"172.16.254.1.8080", 0}, + {"172.16.254.1:8080.", 0}, + {"127.0.0.11:123!", 0}, + {"127.0.0.11:123!", 1}}; for (int i = 0; i < 11; i++) { - ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(tests[i].c_str()), result[i]); + ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(tests[i].ip.c_str()), tests[i].result); } } From 647be3aa3209ebcbc875e597cd612a6618ec2e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 16:16:18 +0800 Subject: [PATCH 15/39] 1 --- src/core/tests/hostname_test.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index b85ccb3247..0b49d7caa9 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -17,20 +17,20 @@ TEST(ip_to_hostname, ipv4_validate) { std::string ip; bool result; - } tests[11] = {{"127.0.0.1:8080", 1}, - {"172.16.254.1:1234", 1}, - {"172.16.254.1:222222", 0}, - {"172.16.254.1", 0}, - {"2222,123,33,1:8080", 0}, - {"123.456.789.1:8080", 0}, - {"001.223.110.002:8080", 0}, - {"172.16.254.1.8080", 0}, - {"172.16.254.1:8080.", 0}, - {"127.0.0.11:123!", 0}, - {"127.0.0.11:123!", 1}}; - - for (int i = 0; i < 11; i++) { - ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(tests[i].ip.c_str()), tests[i].result); + } tests[] = {{"127.0.0.1:8080", 1}, + {"172.16.254.1:1234", 1}, + {"172.16.254.1:222222", 0}, + {"172.16.254.1", 0}, + {"2222,123,33,1:8080", 0}, + {"123.456.789.1:8080", 0}, + {"001.223.110.002:8080", 0}, + {"172.16.254.1.8080", 0}, + {"172.16.254.1:8080.", 0}, + {"127.0.0.11:123!", 0}, + {"127.0.0.11:123!", 1}}; + + for (auto test : tests) { + ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); } } From c4fcded1e500c3f970371ca5dd9a0d3c14974c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 16:48:17 +0800 Subject: [PATCH 16/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 85c5038a7a..fd54b99dbb 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -114,6 +114,9 @@ class rpc_address set_invalid(); std::string ip_port = std::string(s); auto pos = ip_port.find_last_of(':'); + if (pos == std::string::npos) { + return false; + } std::string ip = ip_port.substr(0, pos); std::string port = ip_port.substr(pos + 1); // check port From cc79a45d78938824e4ed5648648d85a8919c3558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 17:00:26 +0800 Subject: [PATCH 17/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index fd54b99dbb..1589c2b2de 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -119,6 +119,9 @@ class rpc_address } std::string ip = ip_port.substr(0, pos); std::string port = ip_port.substr(pos + 1); + if (pos==std::string::npos){ + return false; + } // check port if (port.size() > 5) return false; From 263ae0a762eb4b1a0e16adbd34d98f63880ccf49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 17:00:46 +0800 Subject: [PATCH 18/39] 1 --- include/dsn/tool-api/rpc_address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 1589c2b2de..45df2fd505 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -119,7 +119,7 @@ class rpc_address } std::string ip = ip_port.substr(0, pos); std::string port = ip_port.substr(pos + 1); - if (pos==std::string::npos){ + if (pos == std::string::npos) { return false; } // check port From 8b4f8d703549ae25810450cdb13b706f28301c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Tue, 25 Feb 2020 17:30:26 +0800 Subject: [PATCH 19/39] 1 --- src/core/tests/hostname_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 0b49d7caa9..2455f661b6 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -27,7 +27,7 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1.8080", 0}, {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, - {"127.0.0.11:123!", 1}}; + {"127.0.0.11:123", 1}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From 2653f6f3b3cd495cb7f161bd81430036e904ef71 Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Wed, 26 Feb 2020 11:11:58 +0800 Subject: [PATCH 20/39] 1 --- include/dsn/tool-api/rpc_address.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 45df2fd505..95c822c583 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -25,15 +25,7 @@ */ #pragma once -#include -#include -#include -#include -#include #include -#include -#include -#include #include #include From 42cb15d6794c4d47bc0322bf4778359a6180aaa4 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Wed, 26 Feb 2020 12:52:51 +0800 Subject: [PATCH 21/39] 1 --- include/dsn/tool-api/rpc_address.h | 24 +++++++++++++++++++++++- src/core/core/utils.cpp | 6 +++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 95c822c583..f8c2a67974 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -30,7 +30,8 @@ #include #include -typedef enum dsn_host_type_t { +typedef enum dsn_host_type_t +{ HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, @@ -102,6 +103,27 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } bool from_string_ipv4(const char *s) + { + set_invalid(); + std::string str = std::string(s); + auto pos = str.find_last_of(':'); + if (pos == std::string::npos) + return false; + else { + auto host = str.substr(0, pos); + auto port_str = str.substr(pos + 1); + char *p = nullptr; + long port = ::strtol(port_str.data(), &p, 10); + if (*p != 0) // bad string + return false; + if (port <= 0 || port > UINT16_MAX) // out of range + return false; + assign_ipv4(host.c_str(), (uint16_t)port); + return true; + } + } + + bool ipv4_validate(const char *s) { set_invalid(); std::string ip_port = std::string(s); diff --git a/src/core/core/utils.cpp b/src/core/core/utils.cpp index 7a3da0dacc..ba77e8d79a 100644 --- a/src/core/core/utils.cpp +++ b/src/core/core/utils.cpp @@ -113,7 +113,7 @@ bool hostname_from_ip(const char *ip, std::string *hostname_result) bool hostname_from_ip_port(const char *ip_port, std::string *hostname_result) { dsn::rpc_address addr; - if (!addr.from_string_ipv4(ip_port)) { + if (!addr.ipv4_validate(ip_port)) { dwarn("invalid ip_port(%s)", ip_port); *hostname_result = ip_port; return false; @@ -190,5 +190,5 @@ bool list_hostname_from_ip_port(const char *ip_port_list, std::string *hostname_ *hostname_result_list = result.str(); return all_ok; } -} -} +} // namespace utils +} // namespace dsn From 08a4b4884a17104b6cbf743d5ee35fb6d252e344 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Wed, 26 Feb 2020 13:15:47 +0800 Subject: [PATCH 22/39] 1 --- src/core/tests/hostname_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 2455f661b6..818dbbe89c 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -30,7 +30,7 @@ TEST(ip_to_hostname, ipv4_validate) {"127.0.0.11:123", 1}}; for (auto test : tests) { - ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); + ASSERT_EQ(rpc_test_ipv4.ipv4_validate(test.ip.c_str()), test.result); } } From 50c0e165742660be737f482b298d9e625e62ecc2 Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Wed, 26 Feb 2020 15:37:59 +0800 Subject: [PATCH 23/39] format --- include/dsn/tool-api/rpc_address.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index f8c2a67974..a5aefa2547 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -30,8 +30,7 @@ #include #include -typedef enum dsn_host_type_t -{ +typedef enum dsn_host_type_t { HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, From 47b7b2a912026e76375b0a662d1359711b0b592c Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Wed, 26 Feb 2020 17:57:49 +0800 Subject: [PATCH 24/39] 1 --- include/dsn/tool-api/rpc_address.h | 21 ------------------- src/core/core/utils.cpp | 2 +- src/core/tests/hostname_test.cpp | 2 +- .../storage_engine/simple_kv/config.ini | 4 ++-- 4 files changed, 4 insertions(+), 25 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index f8c2a67974..99f3153d2e 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -103,27 +103,6 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } bool from_string_ipv4(const char *s) - { - set_invalid(); - std::string str = std::string(s); - auto pos = str.find_last_of(':'); - if (pos == std::string::npos) - return false; - else { - auto host = str.substr(0, pos); - auto port_str = str.substr(pos + 1); - char *p = nullptr; - long port = ::strtol(port_str.data(), &p, 10); - if (*p != 0) // bad string - return false; - if (port <= 0 || port > UINT16_MAX) // out of range - return false; - assign_ipv4(host.c_str(), (uint16_t)port); - return true; - } - } - - bool ipv4_validate(const char *s) { set_invalid(); std::string ip_port = std::string(s); diff --git a/src/core/core/utils.cpp b/src/core/core/utils.cpp index ba77e8d79a..d0ddf27f74 100644 --- a/src/core/core/utils.cpp +++ b/src/core/core/utils.cpp @@ -113,7 +113,7 @@ bool hostname_from_ip(const char *ip, std::string *hostname_result) bool hostname_from_ip_port(const char *ip_port, std::string *hostname_result) { dsn::rpc_address addr; - if (!addr.ipv4_validate(ip_port)) { + if (!addr.from_string_ipv4(ip_port)) { dwarn("invalid ip_port(%s)", ip_port); *hostname_result = ip_port; return false; diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 818dbbe89c..2455f661b6 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -30,7 +30,7 @@ TEST(ip_to_hostname, ipv4_validate) {"127.0.0.11:123", 1}}; for (auto test : tests) { - ASSERT_EQ(rpc_test_ipv4.ipv4_validate(test.ip.c_str()), test.result); + ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); } } diff --git a/src/dist/replication/storage_engine/simple_kv/config.ini b/src/dist/replication/storage_engine/simple_kv/config.ini index d656f1e252..cbdfc58e5f 100644 --- a/src/dist/replication/storage_engine/simple_kv/config.ini +++ b/src/dist/replication/storage_engine/simple_kv/config.ini @@ -24,7 +24,7 @@ pools = THREAD_POOL_DEFAULT,THREAD_POOL_REPLICATION_LONG,THREAD_POOL_REPLICATION [apps.client] type = client -arguments = mycluster localhost:34601 simple_kv.instance0 +arguments = mycluster 127.0.0.1:34601 simple_kv.instance0 run = true count = 1 pools = THREAD_POOL_DEFAULT @@ -117,7 +117,7 @@ rpc_request_resend_timeout_milliseconds = 8000 is_trace = false [meta_server] -server_list = localhost:34601 +server_list = 127.0.0.1:34601 min_live_node_count_for_unfreeze = 1 [replication.app] From 7d2165a6de0aa7f8a04fe659c583930206e0b435 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Wed, 26 Feb 2020 23:06:08 +0800 Subject: [PATCH 25/39] 1 --- include/dsn/tool-api/rpc_address.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 95c822c583..1654a40cd0 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -30,7 +30,8 @@ #include #include -typedef enum dsn_host_type_t { +typedef enum dsn_host_type_t +{ HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, @@ -124,6 +125,11 @@ class rpc_address int port_num = std::stoi(port); if (port_num > UINT16_MAX) return false; + // check localhost + if (ip == "localhost") { + assign_ipv4(ip.c_str(), (uint16_t)port_num); + return true; + } // check ip uint32_t ip_addr; if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1) { From 65baf4c40737c48f24be74ab07e1e354cce6f4b6 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Wed, 26 Feb 2020 23:14:01 +0800 Subject: [PATCH 26/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 1654a40cd0..f50fe19aca 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -102,6 +102,9 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } + // This function is to validate the format of ipv4 like "192.168.0.1:12345" + // Due to historical legacy, we also consider format of "localhost:8080" is valid + // Ip address without port like "127.0.0.1" is invalid here bool from_string_ipv4(const char *s) { set_invalid(); From c835be810e9d79e893f7f62bf53c1514164f4b67 Mon Sep 17 00:00:00 2001 From: tangyanzhao Date: Wed, 26 Feb 2020 23:16:40 +0800 Subject: [PATCH 27/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index f50fe19aca..b8cd0fea9e 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -30,8 +30,7 @@ #include #include -typedef enum dsn_host_type_t -{ +typedef enum dsn_host_type_t { HOST_TYPE_INVALID = 0, HOST_TYPE_IPV4 = 1, HOST_TYPE_GROUP = 2, From 8942420a2339f11527280796ff22760e5aadeff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 10:49:49 +0800 Subject: [PATCH 28/39] 1 --- src/dist/replication/storage_engine/simple_kv/config.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dist/replication/storage_engine/simple_kv/config.ini b/src/dist/replication/storage_engine/simple_kv/config.ini index cbdfc58e5f..d656f1e252 100644 --- a/src/dist/replication/storage_engine/simple_kv/config.ini +++ b/src/dist/replication/storage_engine/simple_kv/config.ini @@ -24,7 +24,7 @@ pools = THREAD_POOL_DEFAULT,THREAD_POOL_REPLICATION_LONG,THREAD_POOL_REPLICATION [apps.client] type = client -arguments = mycluster 127.0.0.1:34601 simple_kv.instance0 +arguments = mycluster localhost:34601 simple_kv.instance0 run = true count = 1 pools = THREAD_POOL_DEFAULT @@ -117,7 +117,7 @@ rpc_request_resend_timeout_milliseconds = 8000 is_trace = false [meta_server] -server_list = 127.0.0.1:34601 +server_list = localhost:34601 min_live_node_count_for_unfreeze = 1 [replication.app] From e3161b9006571dba61b5bff783707273833b082b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 10:51:57 +0800 Subject: [PATCH 29/39] 1 --- src/core/tests/hostname_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 2455f661b6..525399dd6a 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -27,7 +27,8 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1.8080", 0}, {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, - {"127.0.0.11:123", 1}}; + {"127.0.0.11:123", 1}, + {"localhost:34601",1}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From e1936a625e8e5f2c455e788f89d803d4ed24d37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 10:52:10 +0800 Subject: [PATCH 30/39] 1 --- src/core/tests/hostname_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 525399dd6a..3ede43ba74 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -28,7 +28,7 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, - {"localhost:34601",1}}; + {"localhost:34601", 1}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From 301ff5cbcd9b51379e3878c39967fd0f53141a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 15:42:35 +0800 Subject: [PATCH 31/39] 1 --- include/dsn/tool-api/rpc_address.h | 23 +++++++++-------------- src/core/tests/hostname_test.cpp | 3 ++- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index b8cd0fea9e..145817c7a5 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -101,9 +101,9 @@ class rpc_address std::string to_std_string() const { return std::string(to_string()); } - // This function is to validate the format of ipv4 like "192.168.0.1:12345" - // Due to historical legacy, we also consider format of "localhost:8080" is valid - // Ip address without port like "127.0.0.1" is invalid here + // This function is used for validating the format of ipv4 like "192.168.0.1:12345" + // Due to historical legacy, we also consider "localhost:8080" is in a valid format + // IP address without port like "127.0.0.1" is invalid here bool from_string_ipv4(const char *s) { set_invalid(); @@ -114,9 +114,6 @@ class rpc_address } std::string ip = ip_port.substr(0, pos); std::string port = ip_port.substr(pos + 1); - if (pos == std::string::npos) { - return false; - } // check port if (port.size() > 5) return false; @@ -128,14 +125,12 @@ class rpc_address if (port_num > UINT16_MAX) return false; // check localhost - if (ip == "localhost") { - assign_ipv4(ip.c_str(), (uint16_t)port_num); - return true; - } - // check ip - uint32_t ip_addr; - if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1) { - return false; + if (ip != "localhost") { + uint32_t ip_addr; + // check ip + if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1) { + return false; + } } assign_ipv4(ip.c_str(), (uint16_t)port_num); return true; diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 3ede43ba74..a91ae910e5 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -28,7 +28,8 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, - {"localhost:34601", 1}}; + {"localhost:34601", 1} + {"localhost:3460100022212312312213",0}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From 5b03d06cbed45ca224b46481ec1e6955c2ac44cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 15:51:17 +0800 Subject: [PATCH 32/39] 1 --- src/core/tests/hostname_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index a91ae910e5..7ef37bbb54 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -28,8 +28,7 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, - {"localhost:34601", 1} - {"localhost:3460100022212312312213",0}}; + {"localhost:34601", 1} {"localhost:3460100022212312312213", 0}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From 4aea8048508fa00df273eb9e2afde6ca9a77093d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 16:07:11 +0800 Subject: [PATCH 33/39] 1 --- src/core/tests/hostname_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 7ef37bbb54..55f32d73a6 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -28,7 +28,8 @@ TEST(ip_to_hostname, ipv4_validate) {"172.16.254.1:8080.", 0}, {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, - {"localhost:34601", 1} {"localhost:3460100022212312312213", 0}}; + {"localhost:34601", 1}, + {"localhost:3460100022212312312213", 0}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From ac271b97d84f6e61752f327b94cdfdb2e8ae14f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 17:13:42 +0800 Subject: [PATCH 34/39] format --- include/dsn/tool-api/rpc_address.h | 11 ++++------- src/core/tests/hostname_test.cpp | 4 +++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index 145817c7a5..fec8535a67 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -29,6 +29,7 @@ #include #include +#include typedef enum dsn_host_type_t { HOST_TYPE_INVALID = 0, @@ -115,15 +116,11 @@ class rpc_address std::string ip = ip_port.substr(0, pos); std::string port = ip_port.substr(pos + 1); // check port - if (port.size() > 5) + unsigned int port_num; + if (port.size() < 6 && dsn::internal::buf2unsigned(port, port_num) && + port_num > UINT16_MAX) { return false; - for (auto c : port) { - if (!isdigit(c)) - return false; } - int port_num = std::stoi(port); - if (port_num > UINT16_MAX) - return false; // check localhost if (ip != "localhost") { uint32_t ip_addr; diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 55f32d73a6..60819ee9cc 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -29,7 +29,9 @@ TEST(ip_to_hostname, ipv4_validate) {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, {"localhost:34601", 1}, - {"localhost:3460100022212312312213", 0}}; + {"localhost:3460100022212312312213", 0}, + {"localhost:-12", 0}, + {"localhost:1@2", 0}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); From 6bc70b061153fbb34f627a3897011f109d51a337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 17:49:29 +0800 Subject: [PATCH 35/39] 1 --- include/dsn/tool-api/rpc_address.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index fec8535a67..e5421a2339 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -117,20 +117,17 @@ class rpc_address std::string port = ip_port.substr(pos + 1); // check port unsigned int port_num; - if (port.size() < 6 && dsn::internal::buf2unsigned(port, port_num) && - port_num > UINT16_MAX) { + if (!(port.size() < 6 && dsn::internal::buf2unsigned(port, port_num) && + port_num <= UINT16_MAX)) { return false; } // check localhost - if (ip != "localhost") { - uint32_t ip_addr; - // check ip - if (inet_pton(AF_INET, ip.c_str(), &ip_addr) != 1) { - return false; - } + uint32_t ip_addr; + if (ip == "localhost" || inet_pton(AF_INET, ip.c_str(), &ip_addr)) { + assign_ipv4(ip.c_str(), (uint16_t)port_num); + return true; } - assign_ipv4(ip.c_str(), (uint16_t)port_num); - return true; + return false; } uint64_t &value() { return _addr.value; } From e47907f5bb835d0042583053a8e56ce434ae357b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Thu, 27 Feb 2020 17:52:38 +0800 Subject: [PATCH 36/39] 1 --- include/dsn/tool-api/rpc_address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index e5421a2339..d74fa2f700 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -121,7 +121,7 @@ class rpc_address port_num <= UINT16_MAX)) { return false; } - // check localhost + // check localhost & IP uint32_t ip_addr; if (ip == "localhost" || inet_pton(AF_INET, ip.c_str(), &ip_addr)) { assign_ipv4(ip.c_str(), (uint16_t)port_num); From ac7c124701484420cd4cba29a7351e5221621a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Fri, 28 Feb 2020 11:19:06 +0800 Subject: [PATCH 37/39] 1 --- include/dsn/tool-api/rpc_address.h | 3 +-- src/core/tests/hostname_test.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index d74fa2f700..dd64ab2b13 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -117,8 +117,7 @@ class rpc_address std::string port = ip_port.substr(pos + 1); // check port unsigned int port_num; - if (!(port.size() < 6 && dsn::internal::buf2unsigned(port, port_num) && - port_num <= UINT16_MAX)) { + if (!(dsn::internal::buf2unsigned(port, port_num) && port_num <= UINT16_MAX)) { return false; } // check localhost & IP diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 60819ee9cc..9b93a052ca 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -29,7 +29,7 @@ TEST(ip_to_hostname, ipv4_validate) {"127.0.0.11:123!", 0}, {"127.0.0.11:123", 1}, {"localhost:34601", 1}, - {"localhost:3460100022212312312213", 0}, + {"localhost:34601000222123112312312216112312123122312213", 0}, {"localhost:-12", 0}, {"localhost:1@2", 0}}; From 6347d124d71f2944121edb275c6da92a421b8124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Fri, 28 Feb 2020 13:23:51 +0800 Subject: [PATCH 38/39] 1 --- include/dsn/tool-api/rpc_address.h | 2 +- src/core/tests/hostname_test.cpp | 50 +++++++++++++++--------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/dsn/tool-api/rpc_address.h b/include/dsn/tool-api/rpc_address.h index dd64ab2b13..783c2cde1a 100644 --- a/include/dsn/tool-api/rpc_address.h +++ b/include/dsn/tool-api/rpc_address.h @@ -117,7 +117,7 @@ class rpc_address std::string port = ip_port.substr(pos + 1); // check port unsigned int port_num; - if (!(dsn::internal::buf2unsigned(port, port_num) && port_num <= UINT16_MAX)) { + if (!dsn::internal::buf2unsigned(port, port_num) || port_num > UINT16_MAX) { return false; } // check localhost & IP diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 9b93a052ca..8652fdcb3d 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -17,21 +17,21 @@ TEST(ip_to_hostname, ipv4_validate) { std::string ip; bool result; - } tests[] = {{"127.0.0.1:8080", 1}, - {"172.16.254.1:1234", 1}, - {"172.16.254.1:222222", 0}, - {"172.16.254.1", 0}, - {"2222,123,33,1:8080", 0}, - {"123.456.789.1:8080", 0}, - {"001.223.110.002:8080", 0}, - {"172.16.254.1.8080", 0}, - {"172.16.254.1:8080.", 0}, - {"127.0.0.11:123!", 0}, - {"127.0.0.11:123", 1}, - {"localhost:34601", 1}, - {"localhost:34601000222123112312312216112312123122312213", 0}, - {"localhost:-12", 0}, - {"localhost:1@2", 0}}; + } tests[] = {{"127.0.0.1:8080", true}, + {"172.16.254.1:1234", true}, + {"172.16.254.1:222222", false}, + {"172.16.254.1", false}, + {"2222,123,33,1:8080", false}, + {"123.456.789.1:8080", false}, + {"001.223.110.002:8080", false}, + {"172.16.254.1.8080", false}, + {"172.16.254.1:8080.", false}, + {"127.0.0.11:123!", false}, + {"127.0.0.11:123", true}, + {"localhost:34601", true}, + {"localhost:3460100022212312312213", false}, + {"localhost:-12", false}, + {"localhost:1@2", false}}; for (auto test : tests) { ASSERT_EQ(rpc_test_ipv4.from_string_ipv4(test.ip.c_str()), test.result); @@ -59,41 +59,41 @@ TEST(ip_to_hostname, localhost) // static bool hostname(const rpc_address &address,std::string *hostname_result); ASSERT_TRUE(dsn::utils::hostname(rpc_example_valid, &hostname_result)); - ASSERT_STREQ(expected_hostname_port.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname_port, hostname_result); // static bool hostname_from_ip(uint32_t ip, std::string* hostname_result); ASSERT_TRUE(dsn::utils::hostname_from_ip(htonl(rpc_example_valid.ip()), &hostname_result)); - ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname, hostname_result); // static bool hostname_from_ip(const char *ip,std::string *hostname_result); ASSERT_TRUE(dsn::utils::hostname_from_ip(valid_ip.c_str(), &hostname_result)); - ASSERT_STREQ(expected_hostname.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname, hostname_result); // static bool hostname_from_ip_port(const char *ip_port,std::string *hostname_result); ASSERT_TRUE(dsn::utils::hostname_from_ip_port(valid_ip_port.c_str(), &hostname_result)); - ASSERT_STREQ(expected_hostname_port.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname_port, hostname_result); // static bool list_hostname_from_ip(const char *ip_port_list,std::string // *hostname_result_list); ASSERT_TRUE(dsn::utils::list_hostname_from_ip(valid_ip_list.c_str(), &hostname_result)); - ASSERT_STREQ(expected_hostname_list.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname_list, hostname_result); ASSERT_FALSE(dsn::utils::list_hostname_from_ip("127.0.0.1,127.0.0.23323,111127.0.0.3", &hostname_result)); - ASSERT_STREQ("localhost,127.0.0.23323,111127.0.0.3", hostname_result.c_str()); + ASSERT_EQ("localhost,127.0.0.23323,111127.0.0.3", hostname_result); ASSERT_FALSE(dsn::utils::list_hostname_from_ip("123.456.789.111,127.0.0.1", &hostname_result)); - ASSERT_STREQ("123.456.789.111,localhost", hostname_result.c_str()); + ASSERT_EQ("123.456.789.111,localhost", hostname_result); // static bool list_hostname_from_ip_port(const char *ip_port_list,std::string // *hostname_result_list); ASSERT_TRUE( dsn::utils::list_hostname_from_ip_port(valid_ip_port_list.c_str(), &hostname_result)); - ASSERT_STREQ(expected_hostname_port_list.c_str(), hostname_result.c_str()); + ASSERT_EQ(expected_hostname_port_list, hostname_result); ASSERT_FALSE(dsn::utils::list_hostname_from_ip_port( "127.0.3333.1:23456,1127.0.0.2:22233,127.0.0.1:8080", &hostname_result)); - ASSERT_STREQ("127.0.3333.1:23456,1127.0.0.2:22233,localhost:8080", hostname_result.c_str()); + ASSERT_EQ("127.0.3333.1:23456,1127.0.0.2:22233,localhost:8080", hostname_result); } TEST(ip_to_hostname, invalid_ip) @@ -107,7 +107,7 @@ TEST(ip_to_hostname, invalid_ip) ASSERT_STREQ(invalid_ip.c_str(), hostname_result.c_str()); ASSERT_FALSE(dsn::utils::hostname_from_ip_port(invalid_ip_port.c_str(), &hostname_result)); - ASSERT_STREQ(invalid_ip_port.c_str(), hostname_result.c_str()); + ASSERT_EQ(invalid_ip_port, hostname_result); } } // namespace replication From fba7583da38451b4d8d6fc9851101b03487f2095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=BD=A6=E6=98=AD?= Date: Fri, 28 Feb 2020 13:32:35 +0800 Subject: [PATCH 39/39] 1 --- src/core/tests/hostname_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/hostname_test.cpp b/src/core/tests/hostname_test.cpp index 8652fdcb3d..a79a748450 100644 --- a/src/core/tests/hostname_test.cpp +++ b/src/core/tests/hostname_test.cpp @@ -104,7 +104,7 @@ TEST(ip_to_hostname, invalid_ip) const std::string invalid_ip_port = "123.456.789.111:23010"; ASSERT_FALSE(dsn::utils::hostname_from_ip(invalid_ip.c_str(), &hostname_result)); - ASSERT_STREQ(invalid_ip.c_str(), hostname_result.c_str()); + ASSERT_EQ(invalid_ip, hostname_result); ASSERT_FALSE(dsn::utils::hostname_from_ip_port(invalid_ip_port.c_str(), &hostname_result)); ASSERT_EQ(invalid_ip_port, hostname_result);