[core] Use switch_stun_ipv6_t for STUN IPv6 write paths. (#3037)

Route IPv6 writes in `switch_stun_packet_attribute_add_binded_address`
and `switch_stun_packet_attribute_add_xor_binded_address` through
`switch_stun_ipv6_t` (16-byte `address[]`) instead of `switch_stun_ip_t`
(4-byte `uint32_t address`).

Add IPv4/IPv6 unit tests for both encoders.

Co-authored-by: Andrey Volk <andywolk@gmail.com>
This commit is contained in:
Dmitry Verenitsin
2026-05-26 20:11:11 +05:00
committed by GitHub
parent 9da537a19f
commit bf9c95e890
4 changed files with 265 additions and 33 deletions
+19 -33
View File
@@ -485,31 +485,24 @@ SWITCH_DECLARE(switch_stun_packet_t *) switch_stun_packet_build_header(switch_st
SWITCH_DECLARE(uint8_t) switch_stun_packet_attribute_add_binded_address(switch_stun_packet_t *packet, char *ipstr, uint16_t port, int family)
{
switch_stun_packet_attribute_t *attribute;
switch_stun_ip_t *ip;
attribute = (switch_stun_packet_attribute_t *) ((uint8_t *) & packet->first_attribute + ntohs(packet->header.length));
attribute->type = htons(SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS);
if (family == AF_INET6) {
switch_stun_ipv6_t *ipv6 = (switch_stun_ipv6_t *) attribute->value;
attribute->length = htons(20);
ipv6->family = 2;
ipv6->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
inet_pton(AF_INET6, ipstr, ipv6->address);
} else {
switch_stun_ip_t *ip = (switch_stun_ip_t *) attribute->value;
attribute->length = htons(8);
}
ip = (switch_stun_ip_t *) attribute->value;
ip->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
if (family == AF_INET6) {
ip->family = 2;
} else {
ip->family = 1;
}
if (family == AF_INET6) {
inet_pton(AF_INET6, ipstr, (struct in6_addr *) &ip->address);
} else {
inet_pton(AF_INET, ipstr, (int *) &ip->address);
ip->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
inet_pton(AF_INET, ipstr, &ip->address);
}
packet->header.length += htons(sizeof(switch_stun_packet_attribute_t)) + attribute->length;
@@ -519,32 +512,25 @@ SWITCH_DECLARE(uint8_t) switch_stun_packet_attribute_add_binded_address(switch_s
SWITCH_DECLARE(uint8_t) switch_stun_packet_attribute_add_xor_binded_address(switch_stun_packet_t *packet, char *ipstr, uint16_t port, int family)
{
switch_stun_packet_attribute_t *attribute;
switch_stun_ip_t *ip;
attribute = (switch_stun_packet_attribute_t *) ((uint8_t *) & packet->first_attribute + ntohs(packet->header.length));
attribute->type = htons(SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS);
if (family == AF_INET6) {
switch_stun_ipv6_t *ipv6 = (switch_stun_ipv6_t *) attribute->value;
attribute->length = htons(20);
ipv6->family = 2;
ipv6->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
inet_pton(AF_INET6, ipstr, ipv6->address);
v6_xor(ipv6->address, (uint8_t *)packet->header.id);
} else {
switch_stun_ip_t *ip = (switch_stun_ip_t *) attribute->value;
attribute->length = htons(8);
}
ip = (switch_stun_ip_t *) attribute->value;
ip->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
if (family == AF_INET6) {
ip->family = 2;
} else {
ip->family = 1;
}
if (family == AF_INET6) {
inet_pton(AF_INET6, ipstr, (struct in6_addr *) &ip->address);
v6_xor((uint8_t *)&ip->address, (uint8_t *)packet->header.id);
} else {
inet_pton(AF_INET, ipstr, (int *) &ip->address);
ip->port = htons(port ^ (STUN_MAGIC_COOKIE >> 16));
inet_pton(AF_INET, ipstr, &ip->address);
ip->address = htonl(ntohl(ip->address) ^ STUN_MAGIC_COOKIE);
}