Commit 25440b75 authored by Youness Alaoui's avatar Youness Alaoui

Use the proper magic cookie for ICE in msn compatibility mode and process the...

Use the proper magic cookie for ICE in msn compatibility mode and process the ice message without relying on stun/usages/bind.c
parent 1b76e28f
...@@ -42,7 +42,6 @@ ...@@ -42,7 +42,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "stun/usages/bind.h"
#include "stunagent.h" #include "stunagent.h"
#include <errno.h> #include <errno.h>
...@@ -96,30 +95,83 @@ stun_usage_ice_conncheck_create (StunAgent *agent, StunMessage *msg, ...@@ -96,30 +95,83 @@ stun_usage_ice_conncheck_create (StunAgent *agent, StunMessage *msg,
StunUsageIceReturn stun_usage_ice_conncheck_process (StunMessage *msg, StunUsageIceReturn stun_usage_ice_conncheck_process (StunMessage *msg,
struct sockaddr *addr, socklen_t *addrlen, struct sockaddr *addr, socklen_t *addrlen,
struct sockaddr *alternate_server, socklen_t *alternate_server_len) struct sockaddr *alternate_server, socklen_t *alternate_server_len,
StunUsageIceCompatibility compatibility)
{ {
int code = -1; int val, code = -1;
StunUsageBindReturn res;
switch (stun_message_get_class (msg))
res = stun_usage_bind_process (msg, addr, addrlen, {
alternate_server, alternate_server_len); case STUN_REQUEST:
switch (res) { case STUN_INDICATION:
case STUN_USAGE_BIND_RETURN_ERROR: return STUN_USAGE_ICE_RETURN_RETRY;
if (stun_message_get_class (msg) == STUN_ERROR) {
if (stun_message_find_error (msg, &code) == 0 && case STUN_RESPONSE:
code == STUN_ERROR_ROLE_CONFLICT) break;
case STUN_ERROR:
if (stun_message_find_error (msg, &code) != 0) {
/* missing ERROR-CODE: ignore message */
return STUN_USAGE_ICE_RETURN_RETRY;
}
if (code == STUN_ERROR_ROLE_CONFLICT)
return STUN_USAGE_ICE_RETURN_ROLE_CONFLICT; return STUN_USAGE_ICE_RETURN_ROLE_CONFLICT;
/* NOTE: currently we ignore unauthenticated messages if the context
* is authenticated, for security reasons. */
stun_debug (" STUN error message received (code: %d)\n", code);
/* ALTERNATE-SERVER mechanism */
if ((code / 100) == 3) {
if (alternate_server && alternate_server_len) {
if (stun_message_find_addr (msg, STUN_ATTRIBUTE_ALTERNATE_SERVER,
alternate_server, alternate_server_len)) {
stun_debug (" Unexpectedly missing ALTERNATE-SERVER attribute\n");
return STUN_USAGE_ICE_RETURN_ERROR;
} }
} else {
if (!stun_message_has_attribute (msg, STUN_ATTRIBUTE_ALTERNATE_SERVER)) {
stun_debug (" Unexpectedly missing ALTERNATE-SERVER attribute\n");
return STUN_USAGE_ICE_RETURN_ERROR; return STUN_USAGE_ICE_RETURN_ERROR;
case STUN_USAGE_BIND_RETURN_RETRY: }
return STUN_USAGE_ICE_RETURN_RETRY; }
case STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER:
stun_debug ("Found alternate server\n");
return STUN_USAGE_ICE_RETURN_ALTERNATE_SERVER; return STUN_USAGE_ICE_RETURN_ALTERNATE_SERVER;
default:
return STUN_USAGE_ICE_RETURN_SUCCESS;
} }
return STUN_USAGE_ICE_RETURN_ERROR;
}
stun_debug ("Received %u-bytes STUN message\n", stun_message_length (msg));
if (compatibility == STUN_USAGE_ICE_COMPATIBILITY_MSN) {
stun_transid_t transid;
uint32_t magic_cookie;
stun_message_id (msg, transid);
magic_cookie = *((uint32_t *) transid);
val = stun_message_find_xor_addr_full (msg,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, addr, addrlen, htonl (magic_cookie));
} else {
val = stun_message_find_xor_addr (msg,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, addr, addrlen);
}
if (val)
{
stun_debug (" No XOR-MAPPED-ADDRESS: %s\n", strerror (val));
val = stun_message_find_addr (msg,
STUN_ATTRIBUTE_MAPPED_ADDRESS, addr, addrlen);
if (val)
{
stun_debug (" No MAPPED-ADDRESS: %s\n", strerror (val));
return STUN_USAGE_ICE_RETURN_ERROR;
}
}
stun_debug ("Mapped address found!\n");
return STUN_USAGE_ICE_RETURN_SUCCESS;
} }
static int static int
...@@ -151,7 +203,8 @@ int ...@@ -151,7 +203,8 @@ int
stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req, stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req,
StunMessage *msg, uint8_t *buf, size_t *plen, StunMessage *msg, uint8_t *buf, size_t *plen,
const struct sockaddr *src, socklen_t srclen, const struct sockaddr *src, socklen_t srclen,
bool *restrict control, uint64_t tie) bool *restrict control, uint64_t tie,
StunUsageIceCompatibility compatibility)
{ {
const char *username = NULL; const char *username = NULL;
uint16_t username_len; uint16_t username_len;
...@@ -216,11 +269,20 @@ stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req, ...@@ -216,11 +269,20 @@ stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req,
stun_debug ("Unable to create response\n"); stun_debug ("Unable to create response\n");
goto failure; goto failure;
} }
if (!stun_has_cookie (msg)) { if (compatibility == STUN_USAGE_ICE_COMPATIBILITY_MSN) {
val = stun_message_append_addr (msg, STUN_ATTRIBUTE_MAPPED_ADDRESS, src, srclen); stun_transid_t transid;
} else { uint32_t magic_cookie;
stun_message_id (msg, transid);
magic_cookie = *((uint32_t *) transid);
val = stun_message_append_xor_addr_full (msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
src, srclen, htonl (magic_cookie));
} else if (stun_has_cookie (msg)) {
val = stun_message_append_xor_addr (msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, val = stun_message_append_xor_addr (msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
src, srclen); src, srclen);
} else {
val = stun_message_append_addr (msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
src, srclen);
} }
if (val) { if (val) {
......
...@@ -74,7 +74,8 @@ stun_usage_ice_conncheck_create (StunAgent *agent, StunMessage *msg, ...@@ -74,7 +74,8 @@ stun_usage_ice_conncheck_create (StunAgent *agent, StunMessage *msg,
StunUsageIceReturn stun_usage_ice_conncheck_process (StunMessage *msg, StunUsageIceReturn stun_usage_ice_conncheck_process (StunMessage *msg,
struct sockaddr *addr, socklen_t *addrlen, struct sockaddr *addr, socklen_t *addrlen,
struct sockaddr *alternate_server, socklen_t *alternate_server_len); struct sockaddr *alternate_server, socklen_t *alternate_server_len,
StunUsageIceCompatibility compatibility);
/** /**
* Tries to parse a STUN connectivity check (Binding request) and format a * Tries to parse a STUN connectivity check (Binding request) and format a
...@@ -105,7 +106,8 @@ int ...@@ -105,7 +106,8 @@ int
stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req, stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req,
StunMessage *msg, uint8_t *buf, size_t *plen, StunMessage *msg, uint8_t *buf, size_t *plen,
const struct sockaddr *restrict src, socklen_t srclen, const struct sockaddr *restrict src, socklen_t srclen,
bool *restrict control, uint64_t tie); bool *restrict control, uint64_t tie,
StunUsageIceCompatibility compatibility);
/** /**
* Extracts the priority from a STUN message. * Extracts the priority from a STUN message.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment