From 2c5c3d0150d35a4b5a976340a13baf4e65e749fa Mon Sep 17 00:00:00 2001 From: cnbatch Date: Sun, 29 Oct 2023 22:42:21 +0800 Subject: [PATCH] update stun function of ipv6 --- src/main.cpp | 2 +- src/networks/stun.cpp | 39 ++++++++++++--------------------------- src/networks/stun.hpp | 13 ++++--------- src/networks/tcp_mode.cpp | 2 +- src/networks/udp_mode.cpp | 4 +++- 5 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d2f8ddd..e185c53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) if (argc <= 1) { char app_name[] = "punchnat"; - printf("%s version 20230426\n", app_name); + printf("%s version 20231029\n", app_name); printf("Usage: %s config1.conf\n", app_name); printf(" %s config1.conf config2.conf...\n", app_name); return 0; diff --git a/src/networks/stun.cpp b/src/networks/stun.cpp index 9c279c1..b72c51b 100644 --- a/src/networks/stun.cpp +++ b/src/networks/stun.cpp @@ -16,15 +16,16 @@ namespace rfc3489 return header_data; } - bool unpack_address_port(const uint8_t *data, uint64_t transaction_id_part_1, uint64_t transaction_id_part_2, uint32_t &ip_address, uint16_t &port) + bool unpack_address_port(const uint8_t *data, const stun_header *current_header, uint32_t &ip_address, uint16_t &port) { const uint8_t *ptr = data; const stun_header *header = (const stun_header *)ptr; - if(ntohs(header->message_type) != message_type::binding_response) + if (ntohs(header->message_type) != message_type::binding_response) return false; uint16_t attrbutes_size = ntohs(header->message_length); - if (header->transaction_id_part_1 != transaction_id_part_1 || header->transaction_id_part_2 != transaction_id_part_2) + if (header->transaction_id_part_1 != current_header->transaction_id_part_1 || + header->transaction_id_part_2 != current_header->transaction_id_part_2) return false; const stun_attributes *attribute_ptr = (const stun_attributes *)(ptr + sizeof(stun_header)); @@ -66,7 +67,7 @@ namespace rfc8489 return header_data; } - bool unpack_address_port(const uint8_t *data, uint32_t transaction_id_part_1, uint64_t transaction_id_part_2, + bool unpack_address_port(const uint8_t *data, const stun_header *current_header, uint32_t &ipv4_address, uint16_t &ipv4_port, std::array &ipv6_address, uint16_t &ipv6_port) { bool address_has_found = false; @@ -78,7 +79,8 @@ namespace rfc8489 return false; uint16_t attrbutes_size = ntohs(header->message_length); - if (header->transaction_id_part_1 != transaction_id_part_1 || header->transaction_id_part_2 != transaction_id_part_2) + if (header->transaction_id_part_1 != current_header->transaction_id_part_1 || + header->transaction_id_part_2 != current_header->transaction_id_part_2) return false; const stun_attributes *attribute_ptr = (const stun_attributes *)(ptr + sizeof(stun_header)); @@ -113,29 +115,12 @@ namespace rfc8489 if (ipv6->family == ip_family::ipv6) { std::copy(std::begin(ipv6->x_ip_address), std::end(ipv6->x_ip_address), ipv6_address.begin()); - uint8_t *ptr = ipv6_address.data(); + uint32_t *byte_array_32 = (uint32_t *)ipv6_address.data(); + uint64_t *byte_array_64 = (uint64_t *)ipv6_address.data(); uint32_t n_cookie = htonl(magic_cookie_value); - - for (uint8_t *u8_ptr = (uint8_t *)&n_cookie; - u8_ptr < (uint8_t *)&n_cookie + sizeof(n_cookie); - u8_ptr++, ptr++) - { - *ptr ^= *u8_ptr; - } - - for (uint8_t *u8_ptr = (uint8_t *)&transaction_id_part_1; - u8_ptr < (uint8_t *)&transaction_id_part_1 + sizeof(transaction_id_part_1); - u8_ptr++, ptr++) - { - *ptr ^= *u8_ptr; - } - - for (uint8_t *u8_ptr = (uint8_t *)&transaction_id_part_2; - u8_ptr < (uint8_t *)&transaction_id_part_2 + sizeof(transaction_id_part_2); - u8_ptr++, ptr++) - { - *ptr ^= *u8_ptr; - } + byte_array_32[0] ^= n_cookie; + byte_array_32[1] ^= current_header->transaction_id_part_1; + byte_array_64[1] ^= current_header->transaction_id_part_2; ipv6_port = ntohs(ipv6->x_port) ^ magic_cookie_front16; address_has_found = true; diff --git a/src/networks/stun.hpp b/src/networks/stun.hpp index 3cc2095..4c32112 100644 --- a/src/networks/stun.hpp +++ b/src/networks/stun.hpp @@ -60,8 +60,7 @@ namespace rfc3489 #pragma pack(pop) std::unique_ptr create_stun_header(uint64_t id); - bool unpack_address_port(const uint8_t *data, uint64_t transaction_id_part_1, - uint64_t transaction_id_part_2, uint32_t &ip_address, uint16_t &port); + bool unpack_address_port(const uint8_t *data, const stun_header *current_header, uint32_t &ip_address, uint16_t &port); } namespace rfc8489 @@ -146,13 +145,9 @@ namespace rfc8489 #pragma pack(pop) std::unique_ptr create_stun_header(uint64_t id); - bool unpack_address_port(const uint8_t *data, - uint32_t transaction_id_part_1, uint64_t transaction_id_part_2, - uint32_t &ipv4_address, uint16_t &ipv4_port, - std::array &ipv6_address, uint16_t &ipv6_port); - - - + bool unpack_address_port(const uint8_t *data, const stun_header *current_header, + uint32_t &ipv4_address, uint16_t &ipv4_port, + std::array &ipv6_address, uint16_t &ipv6_port); } #endif // !__STUN_HPP__ diff --git a/src/networks/tcp_mode.cpp b/src/networks/tcp_mode.cpp index c8b021c..1e89bfc 100644 --- a/src/networks/tcp_mode.cpp +++ b/src/networks/tcp_mode.cpp @@ -249,7 +249,7 @@ void tcp_mode::extract_stun_data(std::unique_ptr input_data, size_t d uint16_t ipv4_port = 0; std::array ipv6_address{}; uint16_t ipv6_port = 0; - if (rfc8489::unpack_address_port(input_data.get(), stun_header->transaction_id_part_1, stun_header->transaction_id_part_2, ipv4_address, ipv4_port, ipv6_address, ipv6_port)) + if (rfc8489::unpack_address_port(input_data.get(), stun_header.get(), ipv4_address, ipv4_port, ipv6_address, ipv6_port)) { save_external_ip_address(ipv4_address, ipv4_port, ipv6_address, ipv6_port); return; diff --git a/src/networks/udp_mode.cpp b/src/networks/udp_mode.cpp index 5da73c6..564f9e7 100644 --- a/src/networks/udp_mode.cpp +++ b/src/networks/udp_mode.cpp @@ -83,7 +83,7 @@ void udp_mode::udp_server_incoming(std::unique_ptr data, size_t data_ uint16_t ipv4_port = 0; std::array ipv6_address{}; uint16_t ipv6_port = 0; - if (rfc8489::unpack_address_port(data.get(), stun_header->transaction_id_part_1, stun_header->transaction_id_part_2, ipv4_address, ipv4_port, ipv6_address, ipv6_port)) + if (rfc8489::unpack_address_port(data.get(), stun_header.get(), ipv4_address, ipv4_port, ipv6_address, ipv6_port)) { save_external_ip_address(ipv4_address, ipv4_port, ipv6_address, ipv6_port); return; @@ -255,6 +255,7 @@ void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por std::stringstream ss; ss << "UDP Mode - External IPv4 Address: " << asio::ip::make_address_v4(ipv4_address) << "\n"; ss << "UDP Mode - External IPv4 Port: " << ipv4_port << "\n"; + std::cout << ss.str(); if (!current_settings.log_ip_address.empty()) v4_info = ss.str(); } @@ -270,6 +271,7 @@ void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por std::stringstream ss; ss << "UDP Mode - External IPv6 Address: " << asio::ip::make_address_v6(ipv6_address) << "\n"; ss << "UDP Mode - External IPv6 Port: " << ipv6_port << "\n"; + std::cout << ss.str(); if (!current_settings.log_ip_address.empty()) v6_info = ss.str(); }