TCPServer: fixed send callback arguments

This commit is contained in:
SChernykh 2023-05-24 13:46:05 +02:00
parent 9d0f835186
commit 5f7ce28682
3 changed files with 47 additions and 57 deletions

View file

@ -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)); client->m_nextOutgoingPeerListRequest = cur_time + (60 + (get_random64() % 61));
const bool result = send(client, 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<char*>(client->m_addrString)); LOGINFO(6, "sending PEER_LIST_REQUEST to " << static_cast<char*>(client->m_addrString));
@ -384,7 +384,7 @@ void P2PServer::send_peer_list_request(P2PClient* client, uint64_t cur_time)
return 0; return 0;
} }
*reinterpret_cast<uint8_t*>(buf) = static_cast<uint8_t>(MessageId::PEER_LIST_REQUEST); *buf = static_cast<uint8_t>(MessageId::PEER_LIST_REQUEST);
return 1; return 1;
}); });
@ -878,10 +878,9 @@ void P2PServer::on_broadcast()
} }
for (Broadcast* data : broadcast_queue) { 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<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
bool send_pruned = true; 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()); 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) { if (!result) {
LOGWARN(5, "failed to broadcast to " << static_cast<char*>(client->m_addrString) << ", disconnecting"); LOGWARN(5, "failed to broadcast to " << static_cast<char*>(client->m_addrString) << ", disconnecting");
@ -1138,7 +1137,7 @@ void P2PServer::download_missing_blocks()
} }
const bool result = send(client, 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<char*>(client->m_addrString)); LOGINFO(5, "sending BLOCK_REQUEST for id = " << id << " to " << static_cast<char*>(client->m_addrString));
@ -1146,15 +1145,14 @@ void P2PServer::download_missing_blocks()
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_REQUEST); *(p++) = static_cast<uint8_t>(MessageId::BLOCK_REQUEST);
memcpy(p, id.h, HASH_SIZE); memcpy(p, id.h, HASH_SIZE);
p += HASH_SIZE; p += HASH_SIZE;
return p - p0; return p - buf;
}); });
if (result) { if (result) {
@ -1618,7 +1616,7 @@ bool P2PServer::P2PClient::send_handshake_challenge()
m_handshakeChallenge = owner->get_random64(); m_handshakeChallenge = owner->get_random64();
return owner->send(this, 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<char*>(m_addrString)); LOGINFO(5, "sending HANDSHAKE_CHALLENGE to " << static_cast<char*>(m_addrString));
@ -1626,8 +1624,7 @@ bool P2PServer::P2PClient::send_handshake_challenge()
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::HANDSHAKE_CHALLENGE); *(p++) = static_cast<uint8_t>(MessageId::HANDSHAKE_CHALLENGE);
@ -1641,7 +1638,7 @@ bool P2PServer::P2PClient::send_handshake_challenge()
memcpy(p, &k, sizeof(uint64_t)); memcpy(p, &k, sizeof(uint64_t));
p += 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, 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<char*>(work->client->m_addrString)); LOGINFO(5, "sending HANDSHAKE_SOLUTION to " << static_cast<char*>(work->client->m_addrString));
@ -1750,8 +1747,7 @@ void P2PServer::P2PClient::send_handshake_solution(const uint8_t (&challenge)[CH
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::HANDSHAKE_SOLUTION); *(p++) = static_cast<uint8_t>(MessageId::HANDSHAKE_SOLUTION);
@ -1765,7 +1761,7 @@ void P2PServer::P2PClient::send_handshake_solution(const uint8_t (&challenge)[CH
work->client->on_after_handshake(p); work->client->on_after_handshake(p);
} }
return p - p0; return p - buf;
}); });
if (result) { if (result) {
@ -1889,7 +1885,7 @@ bool P2PServer::P2PClient::on_handshake_solution(const uint8_t* buf)
} }
return m_owner->send(this, 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<char*>(m_addrString)); LOGINFO(5, "sending LISTEN_PORT and BLOCK_REQUEST for the chain tip to " << static_cast<char*>(m_addrString));
@ -1897,10 +1893,9 @@ bool P2PServer::P2PClient::on_handshake_solution(const uint8_t* buf)
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
on_after_handshake(p); 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, 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<char*>(m_addrString)); LOGINFO(5, "sending BLOCK_RESPONSE to " << static_cast<char*>(m_addrString));
@ -1968,8 +1963,7 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf)
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_RESPONSE); *(p++) = static_cast<uint8_t>(MessageId::BLOCK_RESPONSE);
@ -1981,7 +1975,7 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf)
p += len; 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, 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<char*>(m_addrString)); LOGINFO(6, "sending PEER_LIST_RESPONSE to " << static_cast<char*>(m_addrString));
@ -2175,8 +2169,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::PEER_LIST_RESPONSE); *(p++) = static_cast<uint8_t>(MessageId::PEER_LIST_RESPONSE);
*(p++) = static_cast<uint8_t>(num_selected_peers); *(p++) = static_cast<uint8_t>(num_selected_peers);
@ -2193,7 +2186,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
p += 2; 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, 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<char*>(m_addrString)); LOGINFO(5, "sending BLOCK_REQUEST for id = " << id << " to " << static_cast<char*>(m_addrString));
@ -2416,15 +2409,14 @@ void P2PServer::P2PClient::post_handle_incoming_block(const uint32_t reset_count
return 0; return 0;
} }
uint8_t* p0 = reinterpret_cast<uint8_t*>(buf); uint8_t* p = buf;
uint8_t* p = p0;
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_REQUEST); *(p++) = static_cast<uint8_t>(MessageId::BLOCK_REQUEST);
memcpy(p, id.h, HASH_SIZE); memcpy(p, id.h, HASH_SIZE);
p += HASH_SIZE; p += HASH_SIZE;
return p - p0; return p - buf;
}); });
if (!result) { if (!result) {

View file

@ -287,7 +287,7 @@ bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* log
client->m_lastJobTarget = target; client->m_lastJobTarget = target;
const bool result = send(client, 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 { do {
client->m_rpcId = static_cast<StratumServer*>(client->m_owner)->get_random32(); client->m_rpcId = static_cast<StratumServer*>(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)) { if (!block.get_difficulties(template_id, height, sidechain_height, mainchain_diff, sidechain_diff)) {
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a stale share"); LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a stale share");
return send(client, return send(client,
[id](void* buf, size_t buf_size) [id](uint8_t* buf, size_t buf_size)
{ {
log::Stream s(buf, buf_size); log::Stream s(buf, buf_size);
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Stale share\"}}\n"; 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<char*>(client->m_addrString) << " got a share with invalid job id " << job_id << " (latest job sent has id " << client->m_perConnectionJobId << ')'); LOGWARN(4, "client " << static_cast<char*>(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, 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); log::Stream s(buf, buf_size);
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Invalid job id\"}}\n"; 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; client->m_lastJobTarget = target;
const bool result = send(client, 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<const uint8_t*>(&target), sizeof(uint64_t)); log::hex_buf target_hex(reinterpret_cast<const uint8_t*>(&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)) { if ((client->m_resetCounter.load() == share->m_clientResetCounter) && (client->m_rpcId == share->m_rpcId)) {
const bool result = server->send(client, 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); log::Stream s(buf, buf_size);
switch (share->m_result) { switch (share->m_result) {

View file

@ -809,16 +809,15 @@ void TCPServer::on_new_client(uv_stream_t* server, Client* client)
} }
else { else {
const bool result = owner->send(client, 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) { if (buf_size < 3) {
return 0; return 0;
} }
uint8_t* p = reinterpret_cast<uint8_t*>(buf); buf[0] = 5; // Protocol version (SOCKS5)
p[0] = 5; // Protocol version (SOCKS5) buf[1] = 1; // NMETHODS
p[1] = 1; // NMETHODS buf[2] = 0; // Method 0 (no authentication)
p[2] = 0; // Method 0 (no authentication)
return 3; return 3;
}); });
@ -1082,27 +1081,26 @@ bool TCPServer::Client::on_proxy_handshake(char* data, uint32_t size)
n = 2; n = 2;
const bool result = m_owner->send(this, 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) { if (buf_size < 22) {
return 0; return 0;
} }
uint8_t* p = reinterpret_cast<uint8_t*>(buf); buf[0] = 5; // Protocol version (SOCKS5)
p[0] = 5; // Protocol version (SOCKS5) buf[1] = 1; // CONNECT
p[1] = 1; // CONNECT buf[2] = 0; // RESERVED
p[2] = 0; // RESERVED
if (m_isV6) { if (m_isV6) {
p[3] = 4; // ATYP buf[3] = 4; // ATYP
memcpy(p + 4, m_addr.data, 16); memcpy(buf + 4, m_addr.data, 16);
p[20] = static_cast<uint8_t>(m_port >> 8); buf[20] = static_cast<uint8_t>(m_port >> 8);
p[21] = static_cast<uint8_t>(m_port & 0xFF); buf[21] = static_cast<uint8_t>(m_port & 0xFF);
} }
else { else {
p[3] = 1; // ATYP buf[3] = 1; // ATYP
memcpy(p + 4, m_addr.data + 12, 4); memcpy(buf + 4, m_addr.data + 12, 4);
p[8] = static_cast<uint8_t>(m_port >> 8); buf[8] = static_cast<uint8_t>(m_port >> 8);
p[9] = static_cast<uint8_t>(m_port & 0xFF); buf[9] = static_cast<uint8_t>(m_port & 0xFF);
} }
return m_isV6 ? 22 : 10; return m_isV6 ? 22 : 10;