Skip to content

Commit

Permalink
update stun function of ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Oct 29, 2023
1 parent 34896d6 commit 2c5c3d0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 12 additions & 27 deletions src/networks/stun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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<uint8_t, 16> &ipv6_address, uint16_t &ipv6_port)
{
bool address_has_found = false;
Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 4 additions & 9 deletions src/networks/stun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ namespace rfc3489
#pragma pack(pop)

std::unique_ptr<stun_header> 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
Expand Down Expand Up @@ -146,13 +145,9 @@ namespace rfc8489
#pragma pack(pop)

std::unique_ptr<stun_header> 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<uint8_t, 16> &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<uint8_t, 16> &ipv6_address, uint16_t &ipv6_port);
}

#endif // !__STUN_HPP__
Expand Down
2 changes: 1 addition & 1 deletion src/networks/tcp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void tcp_mode::extract_stun_data(std::unique_ptr<uint8_t[]> input_data, size_t d
uint16_t ipv4_port = 0;
std::array<uint8_t, 16> 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;
Expand Down
4 changes: 3 additions & 1 deletion src/networks/udp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void udp_mode::udp_server_incoming(std::unique_ptr<uint8_t[]> data, size_t data_
uint16_t ipv4_port = 0;
std::array<uint8_t, 16> 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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 2c5c3d0

Please sign in to comment.