From b83b6917146148a4d271d3a1a383419b885a2809 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 11 Sep 2023 19:43:28 +0200 Subject: [PATCH] TCPServer: cleaned up IPv4 address handling --- src/p2p_server.cpp | 6 +++--- src/tcp_server.cpp | 14 ++++++-------- src/util.cpp | 6 ++---- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index a863193..bd6079a 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -461,7 +461,7 @@ void P2PServer::save_peer_list() } else { 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)); if (addr_str) { f << addr_str << ':' << p.m_port << '\n'; @@ -2269,7 +2269,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*) peers[0] = {}; *reinterpret_cast(peers[0].m_addr.data) = SUPPORTED_PROTOCOL_VERSION; *reinterpret_cast(peers[0].m_addr.data + 4) = (P2POOL_VERSION_MAJOR << 16) | P2POOL_VERSION_MINOR; - *reinterpret_cast(peers[0].m_addr.data + 12) = 0xFFFFFFFFU; + *reinterpret_cast(peers[0].m_addr.data + sizeof(raw_ip::ipv4_prefix)) = 0xFFFFFFFFU; peers[0].m_port = 0xFFFF; 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) // Check for protocol version message - if ((*reinterpret_cast(ip.data + 12) == 0xFFFFFFFFU) && (port == 0xFFFF)) { + if ((*reinterpret_cast(ip.data + sizeof(raw_ip::ipv4_prefix)) == 0xFFFFFFFFU) && (port == 0xFFFF)) { const uint32_t version = *reinterpret_cast(ip.data); // Clients with different major protocol versions communicate using v1.0 protocol diff --git a/src/tcp_server.cpp b/src/tcp_server.cpp index d1e2292..e276a74 100644 --- a/src/tcp_server.cpp +++ b/src/tcp_server.cpp @@ -381,7 +381,7 @@ bool TCPServer::connect_to_peer(Client* client) else { sockaddr_in* addr4 = reinterpret_cast(&addr); 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(client->m_port)); } } @@ -395,7 +395,7 @@ bool TCPServer::connect_to_peer(Client* client) else { sockaddr_in* addr4 = reinterpret_cast(&addr); 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(m_socks5ProxyPort)); } } @@ -782,10 +782,8 @@ void TCPServer::on_new_client(uv_stream_t* server, Client* client) client->m_port = ntohs(reinterpret_cast(&peer_addr)->sin6_port); } else { - client->m_addr = {}; - client->m_addr.data[10] = 0xFF; - client->m_addr.data[11] = 0xFF; - memcpy(client->m_addr.data + 12, &reinterpret_cast(&peer_addr)->sin_addr, sizeof(in_addr)); + memcpy(client->m_addr.data, raw_ip::ipv4_prefix, sizeof(raw_ip::ipv4_prefix)); + memcpy(client->m_addr.data + sizeof(raw_ip::ipv4_prefix), &reinterpret_cast(&peer_addr)->sin_addr, sizeof(in_addr)); client->m_port = ntohs(reinterpret_cast(&peer_addr)->sin_port); } @@ -1109,7 +1107,7 @@ bool TCPServer::Client::on_proxy_handshake(char* data, uint32_t size) } else { 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(m_port >> 8); buf[9] = static_cast(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)); } 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) { diff --git a/src/util.cpp b/src/util.cpp index bc19f6b..2d9b2c4 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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)); return false; } - result = {}; - result.data[10] = 0xFF; - result.data[11] = 0xFF; - memcpy(result.data + 12, &addr4->sin_addr, sizeof(in_addr)); + memcpy(result.data, raw_ip::ipv4_prefix, sizeof(raw_ip::ipv4_prefix)); + memcpy(result.data + sizeof(raw_ip::ipv4_prefix), &addr4->sin_addr, sizeof(in_addr)); } return true;