Add IPv6 support for STUN and TURN.

This commit is contained in:
bossiel 2010-02-04 18:41:11 +00:00
parent 7bb18c980b
commit 68cf4ff791
7 changed files with 77 additions and 65 deletions

View File

@ -385,13 +385,25 @@ static void* tnet_stun_attribute_mapped_addr_create(void * self, va_list * app)
memcpy(attribute->address, &addr, 4);
payloadPtr+=4;
}
else if(attribute->family == stun_ipv6)
{
TSK_DEBUG_ERROR("IPv6 not supported yet.");
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY.");
{ /*=== Compute IP address */
size_t addr_size = (attribute->family == stun_ipv6) ? 16 : (attribute->family == stun_ipv4 ? 4 : 0);
if(addr_size)
{
size_t i;
uint32_t addr;
for(i=0; i<addr_size; i+=4)
{
addr = ntohl(*((uint32_t*)payloadPtr));
memcpy(&attribute->address[i], &addr, 4);
payloadPtr+=4;
}
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY [%u].", attribute->family);
}
}
}
}
@ -444,29 +456,35 @@ static void* tnet_stun_attribute_xmapped_addr_create(void * self, va_list * app)
then the converting the result to network byte order.
*/
attribute->xport = ntohs(*((uint16_t*)payloadPtr));
//attribute->xport ^= 0x2112;
attribute->xport ^= 0x2112;
payloadPtr+=2;
{ /*=== Compute IP address */
if(attribute->family == stun_ipv4)
{
/* RFC 5389 - 15.2. XOR-MAPPED-ADDRESS
If the IP address family is IPv4, X-Address is computed by taking the mapped IP
address in host byte order, XOR'ing it with the magic cookie, and
converting the result to network byte order.
*/
uint32_t addr = ntohl(*((uint32_t*)payloadPtr));
//addr ^= TNET_STUN_MAGIC_COOKIE;
memcpy(attribute->xaddress, &addr, 4);
payloadPtr+=4;
}
else if(attribute->family == stun_ipv6)
{
TSK_DEBUG_ERROR("IPv6 not supported yet.");
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY.");
size_t addr_size = (attribute->family == stun_ipv6) ? 16 : (attribute->family == stun_ipv4 ? 4 : 0);
if(addr_size)
{
size_t i;
uint32_t addr;
for(i=0; i<addr_size; i+=4)
{
addr = ntohl(*((uint32_t*)payloadPtr));
addr ^= TNET_STUN_MAGIC_COOKIE;
memcpy(&attribute->xaddress[i], &addr, 4);
payloadPtr+=4;
}
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY [%u].", attribute->family);
}
}
}

View File

@ -65,7 +65,7 @@ TNET_BEGIN_DECLS
**/
#define TNET_IS_STUN2(PU8) \
(((PU8)[0] & 0xc0) == 0x00) && \
((*((uint32_t *)(PU8))+1) == htonl(TNET_STUN_MAGIC_COOKIE))
( (*(((uint32_t *)(PU8))+1)) == htonl(TNET_STUN_MAGIC_COOKIE) )
/**
* @def TNET_STUN_TRANSACID_SIZE

View File

@ -131,22 +131,20 @@ int __pred_find_turn_channel_binding(const tsk_list_item_t* item, const void* id
*
* @return Zero if current list item hold a binding with the same id and -1 otherwise.
**/
int tnet_stun_address_tostring(const uint8_t in_ip[16], tnet_stun_addr_family_t family, int XORed, char** out_ip)
int tnet_stun_address_tostring(const uint8_t in_ip[16], tnet_stun_addr_family_t family, char** out_ip)
{
if(family == stun_ipv6)
{
TSK_DEBUG_ERROR("IPv6 not supported yet.");
tsk_sprintf(out_ip, "%x:%x:%x:%x:%x:%x:%x:%x",
ntohs(*((uint16_t*)&in_ip[0])), ntohs(*((uint16_t*)&in_ip[2])), ntohs(*((uint16_t*)&in_ip[4])), ntohs(*((uint16_t*)&in_ip[6])),
ntohs(*((uint16_t*)&in_ip[8])), ntohs(*((uint16_t*)&in_ip[10])), ntohs(*((uint16_t*)&in_ip[12])), ntohs(*((uint16_t*)&in_ip[14])));
}
else if(family == stun_ipv4)
{
uint32_t address = *((uint32_t*)in_ip);
address = /*ntohl*/(address);
if(XORed)
{
address ^= TNET_STUN_MAGIC_COOKIE;
}
tsk_sprintf(out_ip, "%u.%u.%u.%u", (address>>24)&0xFF, (address>>16)&0xFF, (address>>8)&0xFF, (address>>0)&0xFF);
return 0;
}
else
@ -269,15 +267,15 @@ int tnet_nat_stun_get_reflexive_address(const tnet_nat_context_handle_t* self, t
/*STUN2: XOR-MAPPED-ADDRESS */
if(binding->xmaddr)
{
int ret = tnet_stun_address_tostring(binding->xmaddr->xaddress, binding->xmaddr->family, 1, ipaddress);
*port = /*ntohs*/(binding->xmaddr->xport) ^ 0x2112;
int ret = tnet_stun_address_tostring(binding->xmaddr->xaddress, binding->xmaddr->family, ipaddress);
*port = /*ntohs*/(binding->xmaddr->xport);
return ret;
}
/*STUN1: MAPPED-ADDRESS*/
if(binding->maddr)
{
int ret = tnet_stun_address_tostring(binding->maddr->address, binding->maddr->family, 0, ipaddress);
int ret = tnet_stun_address_tostring(binding->maddr->address, binding->maddr->family, ipaddress);
*port = /*ntohs*/(binding->maddr->port);
return ret;
}
@ -387,15 +385,15 @@ int tnet_nat_turn_get_reflexive_address(const tnet_nat_context_handle_t* self, t
/*STUN2: XOR-MAPPED-ADDRESS */
if(allocation->xmaddr)
{
int ret = tnet_stun_address_tostring(allocation->xmaddr->xaddress, allocation->xmaddr->family, 1, ipaddress);
*port = /*ntohs*/(allocation->xmaddr->xport) ^ 0x2112;
int ret = tnet_stun_address_tostring(allocation->xmaddr->xaddress, allocation->xmaddr->family, ipaddress);
*port = /*ntohs*/(allocation->xmaddr->xport);
return ret;
}
/*STUN1: MAPPED-ADDRESS*/
if(allocation->maddr)
{
int ret = tnet_stun_address_tostring(allocation->maddr->address, allocation->maddr->family, 0, ipaddress);
int ret = tnet_stun_address_tostring(allocation->maddr->address, allocation->maddr->family, ipaddress);
*port = /*ntohs*/(allocation->maddr->port);
return ret;
}

View File

@ -421,25 +421,29 @@ static void* tnet_turn_attribute_xrelayed_addr_create(void * self, va_list * app
attribute->family = (tnet_stun_addr_family_t)(*(payloadPtr++));
attribute->xport = ntohs(*((uint16_t*)payloadPtr));
//attribute->xport ^= 0x2112;
attribute->xport ^= 0x2112;
payloadPtr+=2;
if(attribute->family == stun_ipv4)
{
uint32_t addr = ntohl(*((uint32_t*)payloadPtr));
//addr ^= TNET_STUN_MAGIC_COOKIE;
memcpy(attribute->xaddress, &addr, 4);
payloadPtr+=4;
}
else if(attribute->family == stun_ipv6)
{
TSK_DEBUG_ERROR("IPv6 not supported yet.");
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY.");
}
{ /*=== Compute IP address */
size_t addr_size = (attribute->family == stun_ipv6) ? 16 : (attribute->family == stun_ipv4 ? 4 : 0);
if(addr_size)
{
size_t i;
uint32_t addr;
for(i=0; i<addr_size; i+=4)
{
addr = ntohl(*((uint32_t*)payloadPtr));
addr ^= TNET_STUN_MAGIC_COOKIE;
memcpy(&attribute->xaddress[i], &addr, 4);
payloadPtr+=4;
}
}
else
{
TSK_DEBUG_ERROR("UNKNOWN FAMILY [%u].", attribute->family);
}
}
}
}
return self;

View File

@ -42,12 +42,13 @@
#define RUN_TEST_ALL 0
#define RUN_TEST_SOCKETS 0 /* FIXME: Android */
#define RUN_TEST_TRANSPORT 1
#define RUN_TEST_TRANSPORT 0
#define RUN_TEST_AUTH 0
#define RUN_TEST_STUN 0
#define RUN_TEST_NAT 0
#define RUN_TEST_IFACES 0
#define RUN_TEST_DNS 0
#define RUN_TEST_DHCP 0
#ifdef _WIN32_WCE
int _tmain(int argc, _TCHAR* argv[])

View File

@ -24,7 +24,7 @@
void test_dns_query()
{
tnet_dns_ctx_t *ctx = TNET_DNS_CREATE();
tnet_dns_ctx_t *ctx = TNET_DNS_CTX_CREATE();
tnet_dns_response_t *response = 0;
if((response = tnet_dns_resolve(ctx, "_stun._udp.ims.inexbee.com", qclass_in, qtype_srv)))

View File

@ -209,16 +209,7 @@ bail:
void test_nat()
{
uint8_t addr[4];
const char* str = "192.168.16.104";
memset(addr, 0, sizeof(addr));
//sscanf(str,"%64[0-9.]:%32[^,]%n",addr);
//sscanf(str,"%3u.%3u.%3u.%3u",&addr[0], &addr[1], &addr[2], &addr[3]);
//sscanf(str,"%3u.{1,4}",addr);
//test_nat_stun();
test_nat_stun();
test_nat_turn();
tsk_thread_sleep(1000);
}