Refactored software id code

Show a warning when an unknown software id is seen
This commit is contained in:
SChernykh 2023-09-23 17:26:57 +02:00
parent 6ca6f2ed3d
commit b922440fc7
4 changed files with 37 additions and 6 deletions

View file

@ -1272,7 +1272,7 @@ P2PServer::P2PClient::P2PClient()
, m_peerListPendingRequests(0)
, m_protocolVersion(PROTOCOL_VERSION_1_0)
, m_SoftwareVersion(0)
, m_SoftwareID(0)
, m_SoftwareID(SoftwareID::P2Pool)
, m_pingTime(-1)
, m_lastAlive(0)
, m_lastBroadcastTimestamp(0)
@ -1385,7 +1385,7 @@ void P2PServer::P2PClient::reset()
m_peerListPendingRequests = 0;
m_protocolVersion = PROTOCOL_VERSION_1_0;
m_SoftwareVersion = 0;
m_SoftwareID = 0;
m_SoftwareID = SoftwareID::P2Pool;
m_pingTime = -1;
m_blockPendingRequests.clear();
m_lastAlive = 0;
@ -2265,6 +2265,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
peers[0] = {};
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data) = SUPPORTED_PROTOCOL_VERSION;
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data + 4) = (P2POOL_VERSION_MAJOR << 16) | P2POOL_VERSION_MINOR;
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data + 8) = static_cast<uint32_t>(SoftwareID::P2Pool);
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data + sizeof(raw_ip::ipv4_prefix)) = 0xFFFFFFFFU;
peers[0].m_port = 0xFFFF;
@ -2347,11 +2348,19 @@ void P2PServer::P2PClient::on_peer_list_response(const uint8_t* buf)
}
m_SoftwareVersion = *reinterpret_cast<uint32_t*>(ip.data + 4);
m_SoftwareID = *reinterpret_cast<uint32_t*>(ip.data + 8);
const uint32_t id_value = *reinterpret_cast<uint32_t*>(ip.data + 8);
m_SoftwareID = get_software_id(id_value);
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor()
<< " supports protocol version " << (m_protocolVersion >> 16) << '.' << (m_protocolVersion & 0xFFFF)
<< ", runs " << software_name() << " v" << (m_SoftwareVersion >> 16) << '.' << (m_SoftwareVersion & 0xFFFF)
);
if (m_SoftwareID == SoftwareID::Unknown) {
LOGWARN(4, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor()
<< "runs an unknown software with id = " << log::Hex(id_value)
);
}
}
continue;
}
@ -2613,9 +2622,9 @@ void P2PServer::P2PClient::post_handle_incoming_block(p2pool* pool, const PoolBl
const char* P2PServer::P2PClient::software_name() const
{
switch (m_SoftwareID) {
case 0:
case SoftwareID::P2Pool:
return "P2Pool";
case 0x624F6F47UL:
case SoftwareID::GoObserver:
return "GoObserver";
default:
return "Unknown";

View file

@ -142,7 +142,7 @@ public:
uint32_t m_protocolVersion;
uint32_t m_SoftwareVersion;
uint32_t m_SoftwareID;
SoftwareID m_SoftwareID;
int64_t m_pingTime;

View file

@ -60,6 +60,20 @@ const char* VERSION = "v" STR2(P2POOL_VERSION_MAJOR) "." STR2(P2POOL_VERSION_MIN
#endif
" on " __DATE__ ")";
SoftwareID get_software_id(uint32_t value)
{
switch (value) {
case static_cast<uint32_t>(SoftwareID::P2Pool):
return SoftwareID::P2Pool;
case static_cast<uint32_t>(SoftwareID::GoObserver):
return SoftwareID::GoObserver;
default:
return SoftwareID::Unknown;
}
}
const raw_ip raw_ip::localhost_ipv4 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 };
const raw_ip raw_ip::localhost_ipv6 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };

View file

@ -40,6 +40,14 @@ namespace p2pool {
extern const char* VERSION;
enum class SoftwareID : uint32_t {
P2Pool = 0,
GoObserver = 0x624F6F47UL,
Unknown = 0xFFFFFFFFUL,
};
SoftwareID get_software_id(uint32_t value);
template<typename T> struct not_implemented { enum { value = 0 }; };
struct nocopy_nomove