danicoin/src/p2p/p2p_protocol_defs.h

309 lines
7.7 KiB
C
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-08-13 10:38:35 +00:00
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
2014-03-03 22:07:58 +00:00
#pragma once
2015-05-27 12:08:46 +00:00
#include "p2p_protocol_types.h"
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
#include "crypto/crypto.h"
2015-05-27 12:08:46 +00:00
#include "cryptonote_config.h"
#include "cryptonote_core/cryptonote_stat_info.h"
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
// new serialization
#include "serialization/ISerializer.h"
#include "serialization/SerializationOverloads.h"
#include "cryptonote_core/cryptonote_serialization.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote
{
2015-07-15 12:23:00 +00:00
inline bool serialize(uuid& v, Common::StringView name, ISerializer& s) {
return s.binary(&v, sizeof(v), name);
}
2014-03-03 22:07:58 +00:00
struct network_config
{
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(connections_count)
KV_MEMBER(handshake_interval)
KV_MEMBER(packet_max_size)
KV_MEMBER(config_id)
}
2014-03-03 22:07:58 +00:00
2014-03-20 11:46:11 +00:00
uint32_t connections_count;
uint32_t connection_timeout;
uint32_t ping_connection_timeout;
uint32_t handshake_interval;
uint32_t packet_max_size;
uint32_t config_id;
uint32_t send_peerlist_sz;
2014-03-03 22:07:58 +00:00
};
2015-07-15 12:23:00 +00:00
enum P2PProtocolVersion : uint8_t {
V0 = 0,
V1 = 1,
CURRENT = V1
};
2014-03-03 22:07:58 +00:00
struct basic_node_data
{
2015-07-15 12:23:00 +00:00
uuid network_id;
uint8_t version;
2014-04-25 18:33:24 +00:00
uint64_t local_time;
2014-03-03 22:07:58 +00:00
uint32_t my_port;
peerid_type peer_id;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(network_id)
if (s.type() == ISerializer::INPUT) {
version = 0;
}
KV_MEMBER(version)
KV_MEMBER(peer_id)
KV_MEMBER(local_time)
KV_MEMBER(my_port)
}
2014-03-03 22:07:58 +00:00
};
2015-05-27 12:08:46 +00:00
struct CORE_SYNC_DATA
{
uint64_t current_height;
crypto::hash top_id;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(current_height)
KV_MEMBER(top_id)
}
2015-05-27 12:08:46 +00:00
};
2014-03-03 22:07:58 +00:00
#define P2P_COMMANDS_POOL_BASE 1000
/************************************************************************/
/* */
/************************************************************************/
2015-05-27 12:08:46 +00:00
struct COMMAND_HANDSHAKE
{
const static int ID = P2P_COMMANDS_POOL_BASE + 1;
2014-03-03 22:07:58 +00:00
struct request
{
basic_node_data node_data;
2015-05-27 12:08:46 +00:00
CORE_SYNC_DATA payload_data;
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(node_data)
KV_MEMBER(payload_data)
}
2014-03-03 22:07:58 +00:00
};
struct response
{
basic_node_data node_data;
2015-05-27 12:08:46 +00:00
CORE_SYNC_DATA payload_data;
2014-03-03 22:07:58 +00:00
std::list<peerlist_entry> local_peerlist;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(node_data)
KV_MEMBER(payload_data)
serializeAsBinary(local_peerlist, "local_peerlist", s);
}
2014-03-03 22:07:58 +00:00
};
2015-05-27 12:08:46 +00:00
};
2014-03-03 22:07:58 +00:00
/************************************************************************/
/* */
/************************************************************************/
2015-05-27 12:08:46 +00:00
struct COMMAND_TIMED_SYNC
2014-03-03 22:07:58 +00:00
{
const static int ID = P2P_COMMANDS_POOL_BASE + 2;
struct request
{
2015-05-27 12:08:46 +00:00
CORE_SYNC_DATA payload_data;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(payload_data)
}
2014-03-03 22:07:58 +00:00
};
struct response
{
2014-04-25 18:33:24 +00:00
uint64_t local_time;
2015-05-27 12:08:46 +00:00
CORE_SYNC_DATA payload_data;
2014-03-03 22:07:58 +00:00
std::list<peerlist_entry> local_peerlist;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(local_time)
KV_MEMBER(payload_data)
serializeAsBinary(local_peerlist, "local_peerlist", s);
}
2014-03-03 22:07:58 +00:00
};
};
/************************************************************************/
/* */
/************************************************************************/
struct COMMAND_PING
{
/*
Used to make "callback" connection, to be sure that opponent node
have accessible connection point. Only other nodes can add peer to peerlist,
and ONLY in case when peer has accepted connection and answered to ping.
*/
const static int ID = P2P_COMMANDS_POOL_BASE + 3;
#define PING_OK_RESPONSE_STATUS_TEXT "OK"
struct request
{
/*actually we don't need to send any real data*/
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {}
2014-03-03 22:07:58 +00:00
};
struct response
{
std::string status;
peerid_type peer_id;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(status)
KV_MEMBER(peer_id)
}
2014-03-03 22:07:58 +00:00
};
};
#ifdef ALLOW_DEBUG_COMMANDS
//These commands are considered as insecure, and made in debug purposes for a limited lifetime.
//Anyone who feel unsafe with this commands can disable the ALLOW_GET_STAT_COMMAND macro.
struct proof_of_trust
{
peerid_type peer_id;
uint64_t time;
crypto::signature sign;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(peer_id)
KV_MEMBER(time)
KV_MEMBER(sign)
}
2014-03-03 22:07:58 +00:00
};
2015-05-27 12:08:46 +00:00
inline crypto::hash get_proof_of_trust_hash(const proof_of_trust& pot) {
std::string s;
s.append(reinterpret_cast<const char*>(&pot.peer_id), sizeof(pot.peer_id));
s.append(reinterpret_cast<const char*>(&pot.time), sizeof(pot.time));
return crypto::cn_fast_hash(s.data(), s.size());
}
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
struct COMMAND_REQUEST_STAT_INFO
2014-03-03 22:07:58 +00:00
{
const static int ID = P2P_COMMANDS_POOL_BASE + 4;
struct request
{
proof_of_trust tr;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(tr)
}
2014-03-03 22:07:58 +00:00
};
struct response
{
std::string version;
std::string os_version;
uint64_t connections_count;
uint64_t incoming_connections_count;
2015-05-27 12:08:46 +00:00
core_stat_info payload_info;
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(version)
KV_MEMBER(os_version)
KV_MEMBER(connections_count)
KV_MEMBER(incoming_connections_count)
KV_MEMBER(payload_info)
}
2014-03-03 22:07:58 +00:00
};
};
/************************************************************************/
/* */
/************************************************************************/
struct COMMAND_REQUEST_NETWORK_STATE
{
const static int ID = P2P_COMMANDS_POOL_BASE + 5;
struct request
{
proof_of_trust tr;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(tr)
}
2014-03-03 22:07:58 +00:00
};
struct response
{
2015-07-15 12:23:00 +00:00
std::list<peerlist_entry> local_peerlist_white;
std::list<peerlist_entry> local_peerlist_gray;
std::list<connection_entry> connections_list;
2014-03-03 22:07:58 +00:00
peerid_type my_id;
2015-07-15 12:23:00 +00:00
uint64_t local_time;
void serialize(ISerializer& s) {
serializeAsBinary(local_peerlist_white, "local_peerlist_white", s);
serializeAsBinary(local_peerlist_gray, "local_peerlist_gray", s);
serializeAsBinary(connections_list, "connections_list", s);
KV_MEMBER(my_id)
KV_MEMBER(local_time)
}
2014-03-03 22:07:58 +00:00
};
};
/************************************************************************/
/* */
/************************************************************************/
struct COMMAND_REQUEST_PEER_ID
{
const static int ID = P2P_COMMANDS_POOL_BASE + 6;
struct request
{
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {}
2014-03-03 22:07:58 +00:00
};
struct response
{
peerid_type my_id;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(my_id)
}
2014-03-03 22:07:58 +00:00
};
};
#endif
}