Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P2p version check #39

Merged
merged 5 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/CryptoNoteConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ const size_t COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT = 1000;
const int P2P_DEFAULT_PORT = 15000;
const int RPC_DEFAULT_PORT = 16000;


/* P2P Network Configuration Section - This defines our current P2P network version
and the minimum version for communication between nodes */
const uint8_t P2P_CURRENT_VERSION = 1;
const uint8_t P2P_MINIMUM_VERSION = 1;
const uint8_t P2P_UPGRADE_WINDOW = 2;

const size_t P2P_LOCAL_WHITE_PEERLIST_LIMIT = 1000;
const size_t P2P_LOCAL_GRAY_PEERLIST_LIMIT = 5000;

Expand Down
43 changes: 18 additions & 25 deletions src/P2p/NetNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Common/Util.h"
#include "crypto/crypto.h"

#include <CryptoNoteConfig.h>
#include "ConnectionContext.h"
#include "LevinProtocol.h"
#include "P2pProtocolDefinitions.h"
Expand Down Expand Up @@ -564,34 +565,11 @@ namespace CryptoNote
COMMAND_HANDSHAKE::response rsp;
get_local_node_data(arg.node_data);
m_payload_handler.get_payload_sync_data(arg.payload_data);
/*
auto logArgAndResp = [this,arg,rsp]() {
logger(Logging::TRACE) << "HANDSHAKE COMM DETAILS " <<
"arg.node_data.local_time=" << arg.node_data.local_time << ", " <<
"arg.node_data.my_port=" << arg.node_data.my_port << ", " <<
"arg.node_data.network_id=" << arg.node_data.network_id << ", " <<
"arg.node_data.peer_id=" << arg.node_data.peer_id << ", " <<
"arg.node_data.version=" << static_cast<uint32_t>(arg.node_data.version) << "; " <<
"arg.payload_data.current_height=" << arg.payload_data.current_height << ", " <<
"arg.payload_data.top_id=" << Common::podToHex(arg.payload_data.top_id) << "; " <<
"rsp.node_data.local_time=" << rsp.node_data.local_time << ", " <<
"rsp.node_data.my_port=" << rsp.node_data.my_port << ", " <<
"rsp.node_data.network_id=" << rsp.node_data.network_id << ", " <<
"rsp.node_data.peer_id=" << rsp.node_data.peer_id << ", " <<
"rsp.node_data.version=" << static_cast<uint32_t>(rsp.node_data.version) << "; " <<
"rsp.payload_data.current_height=" << rsp.payload_data.current_height << ", " <<
"rsp.payload_data.top_id=" << Common::podToHex(rsp.payload_data.top_id) << "; ";
};
*/

if (!proto.invoke(COMMAND_HANDSHAKE::ID, arg, rsp)) {
logger(Logging::ERROR) << context << "Failed to invoke COMMAND_HANDSHAKE, closing connection.";
//logArgAndResp();
return false;
}
//else {
// logArgAndResp();
//}

context.version = rsp.node_data.version;

Expand All @@ -600,6 +578,13 @@ namespace CryptoNote
return false;
}

if (rsp.node_data.version < CryptoNote::P2P_MINIMUM_VERSION) {
logger(Logging::DEBUGGING) << context << "COMMAND_HANDSHAKE Failed, peer is wrong version! (" << std::to_string(rsp.node_data.version) << "), closing connection.";
return false;
} else if ((rsp.node_data.version - CryptoNote::P2P_CURRENT_VERSION) >= CryptoNote::P2P_UPGRADE_WINDOW) {
logger(Logging::WARNING) << context << "COMMAND_HANDSHAKE Warning, your software may be out of date. Please upgrade to the latest version.";
}

if (!handle_remote_peerlist(rsp.local_peerlist, rsp.node_data.local_time, context)) {
logger(Logging::ERROR) << context << "COMMAND_HANDSHAKE: failed to handle_remote_peerlist(...), closing connection.";
return false;
Expand Down Expand Up @@ -756,7 +741,7 @@ namespace CryptoNote
});

if (!handshakeContext.get()) {
logger(WARNING) << "Failed to HANDSHAKE with peer " << na;
logger(DEBUGGING) << "Failed to HANDSHAKE with peer " << na;
return false;
}
} catch (System::InterruptedException&) {
Expand Down Expand Up @@ -969,7 +954,7 @@ namespace CryptoNote

bool NodeServer::get_local_node_data(basic_node_data& node_data)
{
node_data.version = P2PProtocolVersion::CURRENT;
node_data.version = CryptoNote::P2P_CURRENT_VERSION;
time_t local_time;
time(&local_time);
node_data.local_time = local_time;
Expand Down Expand Up @@ -1161,6 +1146,14 @@ namespace CryptoNote
return 1;
}

if (arg.node_data.version < CryptoNote::P2P_MINIMUM_VERSION) {
logger(Logging::DEBUGGING) << context << "UNSUPPORTED NETWORK AGENT VERSION CONNECTED! version=" << std::to_string(arg.node_data.version);
context.m_state = CryptoNoteConnectionContext::state_shutdown;
return 1;
} else if (arg.node_data.version > CryptoNote::P2P_CURRENT_VERSION) {
logger(Logging::WARNING) << context << "Warning, your software may be out of date. Please upgrare to the latest version.";
}

if(!context.m_is_income) {
logger(Logging::ERROR) << context << "COMMAND_HANDSHAKE came not from incoming connection";
context.m_state = CryptoNoteConnectionContext::state_shutdown;
Expand Down
18 changes: 17 additions & 1 deletion src/P2p/P2pNode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2011-2017 The Cryptonote developers
// Copyright (c) 2018, The TurtleCoin Developers
// Copyright (c) 2018 The Circle Foundation
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
Expand All @@ -14,6 +15,7 @@
#include <System/TcpConnection.h>
#include <System/TcpConnector.h>

#include <CryptoNoteConfig.h>
#include "Common/StdInputStream.h"
#include "Common/StdOutputStream.h"
#include "Serialization/BinaryInputStreamSerializer.h"
Expand Down Expand Up @@ -395,6 +397,14 @@ bool P2pNode::fetchPeerList(ContextPtr connection) {
return false;
}

if (response.node_data.version < CryptoNote::P2P_MINIMUM_VERSION) {
logger(DEBUGGING) << *connection << "COMMAND_HANDSHAKE Failed, peer is wrong version: " << std::to_string(response.node_data.version);
return false;
} else if ((response.node_data.version - CryptoNote::P2P_CURRENT_VERSION) >= CryptoNote::P2P_UPGRADE_WINDOW) {
logger(WARNING) << *connection << "COMMAND_HANDSHAKE Warning, your software may be out of date. Please upgrade to the latest version.";
}


return handleRemotePeerList(response.local_peerlist, response.node_data.local_time);
} catch (std::exception& e) {
logger(INFO) << *connection << "Failed to obtain peer list: " << e.what();
Expand Down Expand Up @@ -439,7 +449,7 @@ std::list<PeerlistEntry> P2pNode::getLocalPeerList() const {
basic_node_data P2pNode::getNodeData() const {
basic_node_data nodeData;
nodeData.network_id = m_cfg.getNetworkId();
nodeData.version = P2PProtocolVersion::CURRENT;
nodeData.version = CryptoNote::P2P_CURRENT_VERSION;
nodeData.local_time = time(nullptr);
nodeData.peer_id = m_myPeerId;

Expand Down Expand Up @@ -528,6 +538,12 @@ void P2pNode::handleNodeData(const basic_node_data& node, P2pContext& context) {
throw std::runtime_error(msg.str());
}

if (node.version < CryptoNote::P2P_MINIMUM_VERSION) {
std::ostringstream msg;
msg << context << "COMMAND_HANDSHAKE Failed, peer is wrong version! (" << std::to_string(node.version) << ")";
throw std::runtime_error(msg.str());
}

if (node.peer_id == m_myPeerId) {
throw std::runtime_error("Connection to self detected");
}
Expand Down