TCPServer: cleaned up IPv4 address handling

This commit is contained in:
SChernykh 2023-09-11 19:43:28 +02:00
parent 887611c9d8
commit b83b691714
3 changed files with 11 additions and 15 deletions

View file

@ -461,7 +461,7 @@ void P2PServer::save_peer_list()
} }
else { else {
in_addr addr{}; in_addr addr{};
memcpy(&addr.s_addr, p.m_addr.data + 12, sizeof(addr.s_addr)); memcpy(&addr.s_addr, p.m_addr.data + sizeof(raw_ip::ipv4_prefix), sizeof(addr.s_addr));
addr_str = inet_ntop(AF_INET, &addr, addr_str_buf, sizeof(addr_str_buf)); addr_str = inet_ntop(AF_INET, &addr, addr_str_buf, sizeof(addr_str_buf));
if (addr_str) { if (addr_str) {
f << addr_str << ':' << p.m_port << '\n'; f << addr_str << ':' << p.m_port << '\n';
@ -2269,7 +2269,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
peers[0] = {}; peers[0] = {};
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data) = SUPPORTED_PROTOCOL_VERSION; *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 + 4) = (P2POOL_VERSION_MAJOR << 16) | P2POOL_VERSION_MINOR;
*reinterpret_cast<uint32_t*>(peers[0].m_addr.data + 12) = 0xFFFFFFFFU; *reinterpret_cast<uint32_t*>(peers[0].m_addr.data + sizeof(raw_ip::ipv4_prefix)) = 0xFFFFFFFFU;
peers[0].m_port = 0xFFFF; peers[0].m_port = 0xFFFF;
if (num_selected_peers == 0) { if (num_selected_peers == 0) {
@ -2338,7 +2338,7 @@ void P2PServer::P2PClient::on_peer_list_response(const uint8_t* buf)
// Ignore 0.0.0.0/8 (special-purpose range for "this network") and 224.0.0.0/3 (IP multicast and reserved ranges) // Ignore 0.0.0.0/8 (special-purpose range for "this network") and 224.0.0.0/3 (IP multicast and reserved ranges)
// Check for protocol version message // Check for protocol version message
if ((*reinterpret_cast<uint32_t*>(ip.data + 12) == 0xFFFFFFFFU) && (port == 0xFFFF)) { if ((*reinterpret_cast<uint32_t*>(ip.data + sizeof(raw_ip::ipv4_prefix)) == 0xFFFFFFFFU) && (port == 0xFFFF)) {
const uint32_t version = *reinterpret_cast<uint32_t*>(ip.data); const uint32_t version = *reinterpret_cast<uint32_t*>(ip.data);
// Clients with different major protocol versions communicate using v1.0 protocol // Clients with different major protocol versions communicate using v1.0 protocol

View file

@ -381,7 +381,7 @@ bool TCPServer::connect_to_peer(Client* client)
else { else {
sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(&addr); sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(&addr);
addr4->sin_family = AF_INET; addr4->sin_family = AF_INET;
memcpy(&addr4->sin_addr, client->m_addr.data + 12, sizeof(in_addr)); memcpy(&addr4->sin_addr, client->m_addr.data + sizeof(raw_ip::ipv4_prefix), sizeof(in_addr));
addr4->sin_port = htons(static_cast<uint16_t>(client->m_port)); addr4->sin_port = htons(static_cast<uint16_t>(client->m_port));
} }
} }
@ -395,7 +395,7 @@ bool TCPServer::connect_to_peer(Client* client)
else { else {
sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(&addr); sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(&addr);
addr4->sin_family = AF_INET; addr4->sin_family = AF_INET;
memcpy(&addr4->sin_addr, m_socks5ProxyIP.data + 12, sizeof(in_addr)); memcpy(&addr4->sin_addr, m_socks5ProxyIP.data + sizeof(raw_ip::ipv4_prefix), sizeof(in_addr));
addr4->sin_port = htons(static_cast<uint16_t>(m_socks5ProxyPort)); addr4->sin_port = htons(static_cast<uint16_t>(m_socks5ProxyPort));
} }
} }
@ -782,10 +782,8 @@ void TCPServer::on_new_client(uv_stream_t* server, Client* client)
client->m_port = ntohs(reinterpret_cast<sockaddr_in6*>(&peer_addr)->sin6_port); client->m_port = ntohs(reinterpret_cast<sockaddr_in6*>(&peer_addr)->sin6_port);
} }
else { else {
client->m_addr = {}; memcpy(client->m_addr.data, raw_ip::ipv4_prefix, sizeof(raw_ip::ipv4_prefix));
client->m_addr.data[10] = 0xFF; memcpy(client->m_addr.data + sizeof(raw_ip::ipv4_prefix), &reinterpret_cast<sockaddr_in*>(&peer_addr)->sin_addr, sizeof(in_addr));
client->m_addr.data[11] = 0xFF;
memcpy(client->m_addr.data + 12, &reinterpret_cast<sockaddr_in*>(&peer_addr)->sin_addr, sizeof(in_addr));
client->m_port = ntohs(reinterpret_cast<sockaddr_in*>(&peer_addr)->sin_port); client->m_port = ntohs(reinterpret_cast<sockaddr_in*>(&peer_addr)->sin_port);
} }
@ -1109,7 +1107,7 @@ bool TCPServer::Client::on_proxy_handshake(char* data, uint32_t size)
} }
else { else {
buf[3] = 1; // ATYP buf[3] = 1; // ATYP
memcpy(buf + 4, m_addr.data + 12, 4); memcpy(buf + 4, m_addr.data + sizeof(raw_ip::ipv4_prefix), 4);
buf[8] = static_cast<uint8_t>(m_port >> 8); buf[8] = static_cast<uint8_t>(m_port >> 8);
buf[9] = static_cast<uint8_t>(m_port & 0xFF); buf[9] = static_cast<uint8_t>(m_port & 0xFF);
} }
@ -1248,7 +1246,7 @@ void TCPServer::Client::init_addr_string()
addr_str = inet_ntop(AF_INET6, m_addr.data, addr_str_buf, sizeof(addr_str_buf)); addr_str = inet_ntop(AF_INET6, m_addr.data, addr_str_buf, sizeof(addr_str_buf));
} }
else { else {
addr_str = inet_ntop(AF_INET, m_addr.data + 12, addr_str_buf, sizeof(addr_str_buf)); addr_str = inet_ntop(AF_INET, m_addr.data + sizeof(raw_ip::ipv4_prefix), addr_str_buf, sizeof(addr_str_buf));
} }
if (addr_str) { if (addr_str) {

View file

@ -608,10 +608,8 @@ bool str_to_ip(bool is_v6, const char* ip, raw_ip& result)
LOGERR(1, "failed to parse IPv4 address " << ip << ", error " << uv_err_name(err)); LOGERR(1, "failed to parse IPv4 address " << ip << ", error " << uv_err_name(err));
return false; return false;
} }
result = {}; memcpy(result.data, raw_ip::ipv4_prefix, sizeof(raw_ip::ipv4_prefix));
result.data[10] = 0xFF; memcpy(result.data + sizeof(raw_ip::ipv4_prefix), &addr4->sin_addr, sizeof(in_addr));
result.data[11] = 0xFF;
memcpy(result.data + 12, &addr4->sin_addr, sizeof(in_addr));
} }
return true; return true;