parse error response for code to only change roles on 487

This commit is contained in:
Anthony Minessale 2013-06-27 13:00:20 -05:00
parent a26ab6e3e0
commit cd56d774a0
3 changed files with 51 additions and 23 deletions

View File

@ -142,6 +142,27 @@ typedef struct {
} switch_stun_ip_t;
#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
typedef struct {
unsigned padding:21;
unsigned code:3;
unsigned number:8;
char reason[764];
} switch_stun_error_code_t;
#else
typedef struct {
unsigned number:8;
unsigned code:3;
unsigned padding:21;
char reason[764];
} switch_stun_error_code_t;
#endif
/*!
\brief Writes random characters into a buffer
\param buf the buffer

View File

@ -227,7 +227,6 @@ typedef struct {
uint8_t ready;
uint8_t rready;
int missed_count;
int flips;
char last_sent_id[12];
} switch_rtp_ice_t;
@ -784,27 +783,6 @@ static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *d
}
if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
if ((ice->type & ICE_VANILLA)) {
if (ice->flips < 4) {
if ((ice->type & ICE_CONTROLLED)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Changing role to CONTROLLING\n");
ice->type &= ~ICE_CONTROLLED;
ice->flips++;
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Changing role to CONTROLLED\n");
ice->type |= ICE_CONTROLLED;
ice->flips++;
}
packet->header.type = SWITCH_STUN_BINDING_RESPONSE;
}
}
} else {
ice->flips = 0;
}
end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
rtp_session->last_stun = switch_micro_time_now();
@ -813,6 +791,30 @@ static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *d
do {
switch (attr->type) {
case SWITCH_STUN_ATTR_ERROR_CODE:
{
switch_stun_error_code_t *err = (switch_stun_error_code_t *) attr->value;
uint32_t code = (err->code * 100) + err->number;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s got stun binding response %u %s\n",
switch_core_session_get_name(rtp_session->session),
code,
err->reason
);
if ((ice->type & ICE_VANILLA) && code == 487) {
if ((ice->type & ICE_CONTROLLED)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Changing role to CONTROLLING\n");
ice->type &= ~ICE_CONTROLLED;
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Changing role to CONTROLLED\n");
ice->type |= ICE_CONTROLLED;
}
packet->header.type = SWITCH_STUN_BINDING_RESPONSE;
}
}
break;
case SWITCH_STUN_ATTR_MAPPED_ADDRESS:
if (attr->type) {
char ip[16];

View File

@ -257,11 +257,16 @@ SWITCH_DECLARE(switch_stun_packet_t *) switch_stun_packet_parse(uint8_t *buf, ui
*val = ntohl(*val); /* should we do this here? */
}
break;
case SWITCH_STUN_ATTR_ERROR_CODE: /* ErrorCode */
{
uint32_t *u = (uint32_t *) attr->value;
*u = htonl(*u);
}
break;
case SWITCH_STUN_ATTR_USERNAME: /* ByteString, multiple of 4 bytes */
case SWITCH_STUN_ATTR_PASSWORD: /* ByteString, multiple of 4 bytes */
case SWITCH_STUN_ATTR_DATA: /* ByteString */
case SWITCH_STUN_ATTR_ERROR_CODE: /* ErrorCode */
case SWITCH_STUN_ATTR_TRANSPORT_PREFERENCES: /* TransportPrefs */
/*
* No length checking here, since we already checked against the padded length