From 5f7ce286824f19b142bf5f7a408d03d114cefcd2 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 24 May 2023 13:46:05 +0200 Subject: [PATCH] TCPServer: fixed send callback arguments --- src/p2p_server.cpp | 60 ++++++++++++++++++------------------------ src/stratum_server.cpp | 10 +++---- src/tcp_server.cpp | 34 +++++++++++------------- 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 869c836..67bd3e8 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -376,7 +376,7 @@ void P2PServer::send_peer_list_request(P2PClient* client, uint64_t cur_time) client->m_nextOutgoingPeerListRequest = cur_time + (60 + (get_random64() % 61)); const bool result = send(client, - [client](void* buf, size_t buf_size) + [client](uint8_t* buf, size_t buf_size) { LOGINFO(6, "sending PEER_LIST_REQUEST to " << static_cast(client->m_addrString)); @@ -384,7 +384,7 @@ void P2PServer::send_peer_list_request(P2PClient* client, uint64_t cur_time) return 0; } - *reinterpret_cast(buf) = static_cast(MessageId::PEER_LIST_REQUEST); + *buf = static_cast(MessageId::PEER_LIST_REQUEST); return 1; }); @@ -878,10 +878,9 @@ void P2PServer::on_broadcast() } for (Broadcast* data : broadcast_queue) { - const bool result = send(client, [client, data](void* buf, size_t buf_size) -> size_t + const bool result = send(client, [client, data](uint8_t* buf, size_t buf_size) -> size_t { - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; bool send_pruned = true; bool send_compact = (client->m_protocolVersion >= PROTOCOL_VERSION_1_1) && !data->compact_blob.empty() && (data->compact_blob.size() < data->pruned_blob.size()); @@ -935,7 +934,7 @@ void P2PServer::on_broadcast() } } - return p - p0; + return p - buf; }); if (!result) { LOGWARN(5, "failed to broadcast to " << static_cast(client->m_addrString) << ", disconnecting"); @@ -1138,7 +1137,7 @@ void P2PServer::download_missing_blocks() } const bool result = send(client, - [&id, client](void* buf, size_t buf_size) -> size_t + [&id, client](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending BLOCK_REQUEST for id = " << id << " to " << static_cast(client->m_addrString)); @@ -1146,15 +1145,14 @@ void P2PServer::download_missing_blocks() return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::BLOCK_REQUEST); memcpy(p, id.h, HASH_SIZE); p += HASH_SIZE; - return p - p0; + return p - buf; }); if (result) { @@ -1618,7 +1616,7 @@ bool P2PServer::P2PClient::send_handshake_challenge() m_handshakeChallenge = owner->get_random64(); return owner->send(this, - [this, owner](void* buf, size_t buf_size) -> size_t + [this, owner](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending HANDSHAKE_CHALLENGE to " << static_cast(m_addrString)); @@ -1626,8 +1624,7 @@ bool P2PServer::P2PClient::send_handshake_challenge() return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::HANDSHAKE_CHALLENGE); @@ -1641,7 +1638,7 @@ bool P2PServer::P2PClient::send_handshake_challenge() memcpy(p, &k, sizeof(uint64_t)); p += sizeof(uint64_t); - return p - p0; + return p - buf; }); } @@ -1742,7 +1739,7 @@ void P2PServer::P2PClient::send_handshake_solution(const uint8_t (&challenge)[CH } const bool result = work->server->send(work->client, - [work](void* buf, size_t buf_size) -> size_t + [work](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending HANDSHAKE_SOLUTION to " << static_cast(work->client->m_addrString)); @@ -1750,8 +1747,7 @@ void P2PServer::P2PClient::send_handshake_solution(const uint8_t (&challenge)[CH return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::HANDSHAKE_SOLUTION); @@ -1765,7 +1761,7 @@ void P2PServer::P2PClient::send_handshake_solution(const uint8_t (&challenge)[CH work->client->on_after_handshake(p); } - return p - p0; + return p - buf; }); if (result) { @@ -1889,7 +1885,7 @@ bool P2PServer::P2PClient::on_handshake_solution(const uint8_t* buf) } return m_owner->send(this, - [this](void* buf, size_t buf_size) -> size_t + [this](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending LISTEN_PORT and BLOCK_REQUEST for the chain tip to " << static_cast(m_addrString)); @@ -1897,10 +1893,9 @@ bool P2PServer::P2PClient::on_handshake_solution(const uint8_t* buf) return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; on_after_handshake(p); - return p - p0; + return p - buf; }); } @@ -1958,7 +1953,7 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf) } return server->send(this, - [this, &blob](void* buf, size_t buf_size) -> size_t + [this, &blob](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending BLOCK_RESPONSE to " << static_cast(m_addrString)); @@ -1968,8 +1963,7 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf) return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::BLOCK_RESPONSE); @@ -1981,7 +1975,7 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf) p += len; } - return p - p0; + return p - buf; }); } @@ -2167,7 +2161,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*) } return server->send(this, - [this, &peers, num_selected_peers](void* buf, size_t buf_size) -> size_t + [this, &peers, num_selected_peers](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(6, "sending PEER_LIST_RESPONSE to " << static_cast(m_addrString)); @@ -2175,8 +2169,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*) return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::PEER_LIST_RESPONSE); *(p++) = static_cast(num_selected_peers); @@ -2193,7 +2186,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*) p += 2; } - return p - p0; + return p - buf; }); } @@ -2408,7 +2401,7 @@ void P2PServer::P2PClient::post_handle_incoming_block(const uint32_t reset_count } const bool result = server->send(this, - [this, &id](void* buf, size_t buf_size) -> size_t + [this, &id](uint8_t* buf, size_t buf_size) -> size_t { LOGINFO(5, "sending BLOCK_REQUEST for id = " << id << " to " << static_cast(m_addrString)); @@ -2416,15 +2409,14 @@ void P2PServer::P2PClient::post_handle_incoming_block(const uint32_t reset_count return 0; } - uint8_t* p0 = reinterpret_cast(buf); - uint8_t* p = p0; + uint8_t* p = buf; *(p++) = static_cast(MessageId::BLOCK_REQUEST); memcpy(p, id.h, HASH_SIZE); p += HASH_SIZE; - return p - p0; + return p - buf; }); if (!result) { diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 9c02319..2caa7f9 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -287,7 +287,7 @@ bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* log client->m_lastJobTarget = target; const bool result = send(client, - [client, id, &hashing_blob, job_id, blob_size, target, height, &seed_hash](void* buf, size_t buf_size) + [client, id, &hashing_blob, job_id, blob_size, target, height, &seed_hash](uint8_t* buf, size_t buf_size) { do { client->m_rpcId = static_cast(client->m_owner)->get_random32(); @@ -377,7 +377,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo if (!block.get_difficulties(template_id, height, sidechain_height, mainchain_diff, sidechain_diff)) { LOGWARN(4, "client " << static_cast(client->m_addrString) << " got a stale share"); return send(client, - [id](void* buf, size_t buf_size) + [id](uint8_t* buf, size_t buf_size) { log::Stream s(buf, buf_size); s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Stale share\"}}\n"; @@ -463,7 +463,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo LOGWARN(4, "client " << static_cast(client->m_addrString) << " got a share with invalid job id " << job_id << " (latest job sent has id " << client->m_perConnectionJobId << ')'); const bool result = send(client, - [id](void* buf, size_t buf_size) + [id](uint8_t* buf, size_t buf_size) { log::Stream s(buf, buf_size); s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Invalid job id\"}}\n"; @@ -773,7 +773,7 @@ void StratumServer::on_blobs_ready() client->m_lastJobTarget = target; const bool result = send(client, - [data, target, hashing_blob, job_id](void* buf, size_t buf_size) + [data, target, hashing_blob, job_id](uint8_t* buf, size_t buf_size) { log::hex_buf target_hex(reinterpret_cast(&target), sizeof(uint64_t)); @@ -985,7 +985,7 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/) if ((client->m_resetCounter.load() == share->m_clientResetCounter) && (client->m_rpcId == share->m_rpcId)) { const bool result = server->send(client, - [share](void* buf, size_t buf_size) + [share](uint8_t* buf, size_t buf_size) { log::Stream s(buf, buf_size); switch (share->m_result) { diff --git a/src/tcp_server.cpp b/src/tcp_server.cpp index 2181475..68186cc 100644 --- a/src/tcp_server.cpp +++ b/src/tcp_server.cpp @@ -809,16 +809,15 @@ void TCPServer::on_new_client(uv_stream_t* server, Client* client) } else { const bool result = owner->send(client, - [](void* buf, size_t buf_size) -> size_t + [](uint8_t* buf, size_t buf_size) -> size_t { if (buf_size < 3) { return 0; } - uint8_t* p = reinterpret_cast(buf); - p[0] = 5; // Protocol version (SOCKS5) - p[1] = 1; // NMETHODS - p[2] = 0; // Method 0 (no authentication) + buf[0] = 5; // Protocol version (SOCKS5) + buf[1] = 1; // NMETHODS + buf[2] = 0; // Method 0 (no authentication) return 3; }); @@ -1082,27 +1081,26 @@ bool TCPServer::Client::on_proxy_handshake(char* data, uint32_t size) n = 2; const bool result = m_owner->send(this, - [this](void* buf, size_t buf_size) -> size_t + [this](uint8_t* buf, size_t buf_size) -> size_t { if (buf_size < 22) { return 0; } - uint8_t* p = reinterpret_cast(buf); - p[0] = 5; // Protocol version (SOCKS5) - p[1] = 1; // CONNECT - p[2] = 0; // RESERVED + buf[0] = 5; // Protocol version (SOCKS5) + buf[1] = 1; // CONNECT + buf[2] = 0; // RESERVED if (m_isV6) { - p[3] = 4; // ATYP - memcpy(p + 4, m_addr.data, 16); - p[20] = static_cast(m_port >> 8); - p[21] = static_cast(m_port & 0xFF); + buf[3] = 4; // ATYP + memcpy(buf + 4, m_addr.data, 16); + buf[20] = static_cast(m_port >> 8); + buf[21] = static_cast(m_port & 0xFF); } else { - p[3] = 1; // ATYP - memcpy(p + 4, m_addr.data + 12, 4); - p[8] = static_cast(m_port >> 8); - p[9] = static_cast(m_port & 0xFF); + buf[3] = 1; // ATYP + memcpy(buf + 4, m_addr.data + 12, 4); + buf[8] = static_cast(m_port >> 8); + buf[9] = static_cast(m_port & 0xFF); } return m_isV6 ? 22 : 10;