|
| 1 | +/* |
| 2 | +* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application |
| 3 | +* Copyright (C) 2005-2026, Anthony Minessale II <anthm@freeswitch.org> |
| 4 | +* |
| 5 | +* Version: MPL 1.1 |
| 6 | +* |
| 7 | +* The contents of this file are subject to the Mozilla Public License Version |
| 8 | +* 1.1 (the "License"); you may not use this file except in compliance with |
| 9 | +* the License. You may obtain a copy of the License at |
| 10 | +* http://www.mozilla.org/MPL/ |
| 11 | +* |
| 12 | +* Software distributed under the License is distributed on an "AS IS" basis, |
| 13 | +* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 14 | +* for the specific language governing rights and limitations under the |
| 15 | +* License. |
| 16 | +* |
| 17 | +* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application |
| 18 | +* |
| 19 | +* The Initial Developer of the Original Code is |
| 20 | +* Anthony Minessale II <anthm@freeswitch.org> |
| 21 | +* Portions created by the Initial Developer are Copyright (C) |
| 22 | +* the Initial Developer. All Rights Reserved. |
| 23 | +* |
| 24 | +* Contributor(s): |
| 25 | +* Dmitry Verenitsin <morbit85@gmail.com> |
| 26 | +* |
| 27 | +* switch_stun.c -- tests STUN (https://www.rfc-editor.org/rfc/rfc5389). |
| 28 | +*/ |
| 29 | + |
| 30 | + |
| 31 | +#include <switch.h> |
| 32 | +#include <switch_stun.h> |
| 33 | +#include <test/switch_test.h> |
| 34 | + |
| 35 | +FST_CORE_BEGIN("./conf_stun") |
| 36 | +{ |
| 37 | +FST_SUITE_BEGIN(switch_stun) |
| 38 | +{ |
| 39 | +FST_SETUP_BEGIN() |
| 40 | +{ |
| 41 | +} |
| 42 | +FST_SETUP_END() |
| 43 | + |
| 44 | +FST_TEARDOWN_BEGIN() |
| 45 | +{ |
| 46 | +} |
| 47 | +FST_TEARDOWN_END() |
| 48 | + |
| 49 | + FST_TEST_BEGIN(test_stun_add_binded_address_ipv6) |
| 50 | + { |
| 51 | + /* |
| 52 | + * Encode an IPv6 XOR-MAPPED-ADDRESS attribute and verify the |
| 53 | + * attribute type, length, address family, and the raw 16-byte |
| 54 | + * address payload at its expected offset inside the value. |
| 55 | + */ |
| 56 | + uint8_t buf[512]; |
| 57 | + switch_stun_packet_t *packet; |
| 58 | + switch_stun_packet_attribute_t *attr; |
| 59 | + const char *ipv6_str = "2001:db8::dead:beef"; |
| 60 | + uint8_t expected[16]; |
| 61 | + uint8_t *value_bytes; |
| 62 | + |
| 63 | + memset(buf, 0, sizeof(buf)); |
| 64 | + packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, NULL, buf); |
| 65 | + fst_xcheck(inet_pton(AF_INET6, ipv6_str, expected) == 1, "test IPv6 literal parses"); |
| 66 | + |
| 67 | + switch_stun_packet_attribute_add_binded_address(packet, (char *)ipv6_str, 12345, AF_INET6); |
| 68 | + |
| 69 | + attr = (switch_stun_packet_attribute_t *)packet->first_attribute; |
| 70 | + fst_xcheck(ntohs(attr->type) == SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS, "attribute type is XOR_MAPPED_ADDRESS"); |
| 71 | + fst_xcheck(ntohs(attr->length) == 20, "attribute length is 20 for IPv6"); |
| 72 | + |
| 73 | + /* Attribute value layout: wasted(1) + family(1) + port(2) + address(16). */ |
| 74 | + value_bytes = (uint8_t *)attr->value; |
| 75 | + fst_xcheck(value_bytes[1] == 2, "attribute family byte is 2 for IPv6"); |
| 76 | + fst_xcheck(memcmp(value_bytes + 4, expected, 16) == 0, "16-byte IPv6 address written at offset 4 of attribute value"); |
| 77 | + } |
| 78 | + FST_TEST_END() |
| 79 | + |
| 80 | + FST_TEST_BEGIN(test_stun_add_xor_binded_address_ipv6) |
| 81 | + { |
| 82 | + /* |
| 83 | + * Encode then decode an IPv6 XOR-MAPPED-ADDRESS attribute and |
| 84 | + * confirm the round-trip recovers the original IPv6 string — |
| 85 | + * the write path must XOR the address with the transaction ID |
| 86 | + * symmetrically to the read path. |
| 87 | + */ |
| 88 | + uint8_t buf[512]; |
| 89 | + switch_stun_packet_t *packet; |
| 90 | + switch_stun_packet_attribute_t *attr; |
| 91 | + const char *ipv6_str = "2001:db8::dead:beef"; |
| 92 | + char out_ip[64] = { 0 }; |
| 93 | + uint16_t out_port = 0; |
| 94 | + |
| 95 | + memset(buf, 0, sizeof(buf)); |
| 96 | + packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, NULL, buf); |
| 97 | + |
| 98 | + switch_stun_packet_attribute_add_xor_binded_address(packet, (char *)ipv6_str, 12345, AF_INET6); |
| 99 | + |
| 100 | + attr = (switch_stun_packet_attribute_t *)packet->first_attribute; |
| 101 | + fst_xcheck(ntohs(attr->type) == SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS, "attribute type is XOR_MAPPED_ADDRESS"); |
| 102 | + fst_xcheck(ntohs(attr->length) == 20, "attribute length is 20 for IPv6"); |
| 103 | + |
| 104 | + switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, out_ip, sizeof(out_ip), &out_port); |
| 105 | + fst_check_string_equals(out_ip, ipv6_str); |
| 106 | + } |
| 107 | + FST_TEST_END() |
| 108 | + |
| 109 | + FST_TEST_BEGIN(test_stun_add_binded_address_ipv4) |
| 110 | + { |
| 111 | + /* |
| 112 | + * Encode an IPv4 XOR-MAPPED-ADDRESS attribute and verify the |
| 113 | + * attribute type, length, address family, and the raw 4-byte |
| 114 | + * address payload at its expected offset inside the value. |
| 115 | + */ |
| 116 | + uint8_t buf[512]; |
| 117 | + switch_stun_packet_t *packet; |
| 118 | + switch_stun_packet_attribute_t *attr; |
| 119 | + const char *ipv4_str = "192.0.2.42"; |
| 120 | + uint8_t expected[4]; |
| 121 | + uint8_t *value_bytes; |
| 122 | + |
| 123 | + memset(buf, 0, sizeof(buf)); |
| 124 | + packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, NULL, buf); |
| 125 | + fst_xcheck(inet_pton(AF_INET, ipv4_str, expected) == 1, "test IPv4 literal parses"); |
| 126 | + |
| 127 | + switch_stun_packet_attribute_add_binded_address(packet, (char *)ipv4_str, 12345, AF_INET); |
| 128 | + |
| 129 | + attr = (switch_stun_packet_attribute_t *)packet->first_attribute; |
| 130 | + fst_xcheck(ntohs(attr->type) == SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS, "attribute type is XOR_MAPPED_ADDRESS"); |
| 131 | + fst_xcheck(ntohs(attr->length) == 8, "attribute length is 8 for IPv4"); |
| 132 | + |
| 133 | + /* Attribute value layout: wasted(1) + family(1) + port(2) + address(4). */ |
| 134 | + value_bytes = (uint8_t *)attr->value; |
| 135 | + fst_xcheck(value_bytes[1] == 1, "attribute family byte is 1 for IPv4"); |
| 136 | + fst_xcheck(memcmp(value_bytes + 4, expected, 4) == 0, "4-byte IPv4 address written at offset 4 of attribute value"); |
| 137 | + } |
| 138 | + FST_TEST_END() |
| 139 | + |
| 140 | + FST_TEST_BEGIN(test_stun_add_xor_binded_address_ipv4) |
| 141 | + { |
| 142 | + /* |
| 143 | + * Encode then decode an IPv4 XOR-MAPPED-ADDRESS attribute and |
| 144 | + * confirm the round-trip recovers the original IPv4 string — |
| 145 | + * the write path must XOR the address with the magic cookie |
| 146 | + * symmetrically to the read path. |
| 147 | + */ |
| 148 | + uint8_t buf[512]; |
| 149 | + switch_stun_packet_t *packet; |
| 150 | + switch_stun_packet_attribute_t *attr; |
| 151 | + const char *ipv4_str = "192.0.2.42"; |
| 152 | + char out_ip[64] = { 0 }; |
| 153 | + uint16_t out_port = 0; |
| 154 | + |
| 155 | + memset(buf, 0, sizeof(buf)); |
| 156 | + packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, NULL, buf); |
| 157 | + |
| 158 | + switch_stun_packet_attribute_add_xor_binded_address(packet, (char *)ipv4_str, 12345, AF_INET); |
| 159 | + |
| 160 | + attr = (switch_stun_packet_attribute_t *)packet->first_attribute; |
| 161 | + fst_xcheck(ntohs(attr->type) == SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS, "attribute type is XOR_MAPPED_ADDRESS"); |
| 162 | + fst_xcheck(ntohs(attr->length) == 8, "attribute length is 8 for IPv4"); |
| 163 | + |
| 164 | + switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, out_ip, sizeof(out_ip), &out_port); |
| 165 | + fst_check_string_equals(out_ip, ipv4_str); |
| 166 | + } |
| 167 | + FST_TEST_END() |
| 168 | +} |
| 169 | +FST_SUITE_END() |
| 170 | +} |
| 171 | +FST_CORE_END() |
| 172 | + |
0 commit comments