diff --git a/src/common/util.cpp b/src/common/util.cpp index 999c5401..6fa15854 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -658,4 +658,41 @@ std::string get_nix_version_display_string() return static_cast(in.tellg()); } + bool check_remote_client_version(const std::string& client_ver) + { + std::string v = client_ver.substr(0, client_ver.find('[')); // remove commit id + v = v.substr(0, v.rfind('.')); // remove build number + + int v_major = 0, v_minor = 0, v_revision = 0; + + size_t dot_pos = v.find('.'); + if (dot_pos == std::string::npos || !epee::string_tools::string_to_num_fast(v.substr(0, dot_pos), v_major)) + return false; + + v = v.substr(dot_pos + 1); + dot_pos = v.find('.'); + if (!epee::string_tools::string_to_num_fast(v.substr(0, dot_pos), v_minor)) + return false; + + if (dot_pos != std::string::npos) + { + // revision + v = v.substr(dot_pos + 1); + if (!epee::string_tools::string_to_num_fast(v, v_revision)) + return false; + } + + // got v_major, v_minor, v_revision + + // allow 1.1.x and greater + + if (v_major < 1) + return false; + + if (v_major == 1 && v_minor < 1) + return false; + + return true; + } + } // namespace tools diff --git a/src/common/util.h b/src/common/util.h index 3360c345..87138ef0 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -30,6 +30,8 @@ namespace tools std::string get_default_user_dir(); std::string get_current_username(); std::string get_os_version_string(); + bool check_remote_client_version(const std::string& client_ver); + bool create_directories_if_necessary(const std::string& path); std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name); diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 72cadef1..93ee7a61 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -483,7 +483,7 @@ namespace nodetool return; } - if (!check_remote_client_version(rsp.payload_data.client_version)) + if (!tools::check_remote_client_version(rsp.payload_data.client_version)) { LOG_PRINT_CCONTEXT_L2("COMMAND_HANDSHAKE Failed, wrong client version: " << rsp.payload_data.client_version << ", closing connection."); return; diff --git a/src/version.h.in b/src/version.h.in index 61d98d65..bbd5c6c5 100644 --- a/src/version.h.in +++ b/src/version.h.in @@ -11,42 +11,3 @@ #define PROJECT_VERSION_BUILD_NO 67 #define PROJECT_VERSION_BUILD_NO_STR STRINGIFY_EXPAND(PROJECT_VERSION_BUILD_NO) #define PROJECT_VERSION_LONG PROJECT_VERSION "." PROJECT_VERSION_BUILD_NO_STR "[" BUILD_COMMIT_ID "]" - - - -inline bool check_remote_client_version(const std::string& client_ver) -{ - std::string v = client_ver.substr(0, client_ver.find('[')); // remove commit id - v = v.substr(0, v.rfind('.')); // remove build number - - int v_major = 0, v_minor = 0, v_revision = 0; - - size_t dot_pos = v.find('.'); - if (dot_pos == std::string::npos || !epee::string_tools::string_to_num_fast(v.substr(0, dot_pos), v_major)) - return false; - - v = v.substr(dot_pos + 1); - dot_pos = v.find('.'); - if (!epee::string_tools::string_to_num_fast(v.substr(0, dot_pos), v_minor)) - return false; - - if (dot_pos != std::string::npos) - { - // revision - v = v.substr(dot_pos + 1); - if (!epee::string_tools::string_to_num_fast(v, v_revision)) - return false; - } - - // got v_major, v_minor, v_revision - - // allow 1.1.x and greater - - if (v_major < 1) - return false; - - if (v_major == 1 && v_minor < 1) - return false; - - return true; -} diff --git a/tests/unit_tests/p2p_client_version.cpp b/tests/unit_tests/p2p_client_version.cpp index 73b44559..3761e36a 100644 --- a/tests/unit_tests/p2p_client_version.cpp +++ b/tests/unit_tests/p2p_client_version.cpp @@ -3,10 +3,12 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "gtest/gtest.h" -#include "version.h" +#include "common/util.h" TEST(p2p_client_version, test_1) { + using namespace tools; + // good ASSERT_TRUE(check_remote_client_version("10.101.999.28391[deadbeef31337-dirty]"));