forked from lthn/blockchain
p2p: check_remote_client_version() moved to more appropriate place
This commit is contained in:
parent
02c04aa300
commit
609969799e
5 changed files with 43 additions and 41 deletions
|
|
@ -658,4 +658,41 @@ std::string get_nix_version_display_string()
|
|||
return static_cast<uint64_t>(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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]"));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue