From cd56d774a049986e48a95b7481f768442f269232 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 27 Jun 2013 13:00:20 -0500 Subject: [PATCH] parse error response for code to only change roles on 487 --- src/include/switch_stun.h | 21 ++++++++++++++++++ src/switch_rtp.c | 46 ++++++++++++++++++++------------------- src/switch_stun.c | 7 +++++- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/include/switch_stun.h b/src/include/switch_stun.h index fdf1fbd74d..b6de510159 100644 --- a/src/include/switch_stun.h +++ b/src/include/switch_stun.h @@ -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 diff --git a/src/switch_rtp.c b/src/switch_rtp.c index cc73563968..821c375384 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -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]; diff --git a/src/switch_stun.c b/src/switch_stun.c index f2af463f15..8be191e782 100644 --- a/src/switch_stun.c +++ b/src/switch_stun.c @@ -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