danicoin/src/P2p/P2pProtocolDefinitions.h

296 lines
7.1 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-03-03 22:07:58 +00:00
#pragma once
2015-07-30 15:22:07 +00:00
#include "P2pProtocolTypes.h"
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
#include "crypto/crypto.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteConfig.h"
#include "CryptoNoteCore/CryptoNoteStatInfo.h"
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
// new serialization
2015-07-30 15:22:07 +00:00
#include "Serialization/ISerializer.h"
#include "Serialization/SerializationOverloads.h"
#include "CryptoNoteCore/CryptoNoteSerialization.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;
2015-07-30 15:22:07 +00:00
PeerIdType peer_id;
2014-03-03 22:07:58 +00:00
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
{
2015-07-30 15:22:07 +00:00
uint32_t current_height;
Crypto::Hash top_id;
2015-05-27 12:08:46 +00:00
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
{
2015-07-30 15:22:07 +00:00
enum { 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;
2015-07-30 15:22:07 +00:00
std::list<PeerlistEntry> local_peerlist;
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)
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
{
2015-07-30 15:22:07 +00:00
enum { ID = P2P_COMMANDS_POOL_BASE + 2 };
2014-03-03 22:07:58 +00:00
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;
2015-07-30 15:22:07 +00:00
std::list<PeerlistEntry> local_peerlist;
2014-03-03 22:07:58 +00:00
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.
*/
2015-07-30 15:22:07 +00:00
enum { ID = P2P_COMMANDS_POOL_BASE + 3 };
2014-03-03 22:07:58 +00:00
#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;
2015-07-30 15:22:07 +00:00
PeerIdType peer_id;
2014-03-03 22:07:58 +00:00
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
{
2015-07-30 15:22:07 +00:00
PeerIdType peer_id;
2014-03-03 22:07:58 +00:00
uint64_t time;
2015-07-30 15:22:07 +00:00
Crypto::Signature sign;
2014-03-03 22:07:58 +00:00
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-07-30 15:22:07 +00:00
inline Crypto::Hash get_proof_of_trust_hash(const proof_of_trust& pot) {
2015-05-27 12:08:46 +00:00
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));
2015-07-30 15:22:07 +00:00
return Crypto::cn_fast_hash(s.data(), s.size());
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_REQUEST_STAT_INFO
2014-03-03 22:07:58 +00:00
{
2015-07-30 15:22:07 +00:00
enum { ID = P2P_COMMANDS_POOL_BASE + 4 };
2014-03-03 22:07:58 +00:00
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
{
2015-07-30 15:22:07 +00:00
enum { ID = P2P_COMMANDS_POOL_BASE + 5 };
2014-03-03 22:07:58 +00:00
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-30 15:22:07 +00:00
std::list<PeerlistEntry> local_peerlist_white;
std::list<PeerlistEntry> local_peerlist_gray;
2015-07-15 12:23:00 +00:00
std::list<connection_entry> connections_list;
2015-07-30 15:22:07 +00:00
PeerIdType 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
{
2015-07-30 15:22:07 +00:00
enum { ID = P2P_COMMANDS_POOL_BASE + 6 };
2014-03-03 22:07:58 +00:00
struct request
{
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {}
2014-03-03 22:07:58 +00:00
};
struct response
{
2015-07-30 15:22:07 +00:00
PeerIdType my_id;
2014-03-03 22:07:58 +00:00
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
}