From 1bbb57a82c28ba38259f713d55f8aff620a2ade0 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 4 Dec 2007 00:21:32 +0000 Subject: [PATCH] fix SFSIP-37 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6481 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/endpoints/mod_sofia/mod_sofia.h | 5 +- src/mod/endpoints/mod_sofia/sofia.c | 42 +++++----- src/mod/endpoints/mod_sofia/sofia_reg.c | 81 ++----------------- .../mod_event_socket/mod_event_socket.c | 8 +- 4 files changed, 41 insertions(+), 95 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 7bb4942fe0..b3fb4261f7 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -428,8 +428,9 @@ void sofia_reg_auth_challange(nua_t *nua, sofia_profile_t *profile, nua_handle_t auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t const *authorization, const char *regstr, char *np, size_t nplen, char *ip, switch_event_t **v_event, long exptime); void sofia_reg_handle_sip_r_challenge(int status, - char const *phrase, - nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip, tagi_t tags[]); + char const *phrase, + nua_t *nua, sofia_profile_t *profile, + nua_handle_t *nh, switch_core_session_t *session, sofia_gateway_t *gateway, sip_t const *sip, tagi_t tags[]); void sofia_reg_handle_sip_r_register(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]); diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 4de317db8d..ee740e74f7 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -95,18 +95,26 @@ void sofia_event_callback(nua_event_t event, auth_res_t auth_res = AUTH_FORBIDDEN; switch_core_session_t *session = NULL; switch_channel_t *channel = NULL; + sofia_gateway_t *gateway = NULL; - if (sofia_private && !switch_strlen_zero(sofia_private->uuid)) { - if ((session = switch_core_session_locate(sofia_private->uuid))) { - tech_pvt = switch_core_session_get_private(session); - channel = switch_core_session_get_channel(tech_pvt->session); - if (!tech_pvt->call_id && sip && sip->sip_call_id && sip->sip_call_id->i_id) { - tech_pvt->call_id = switch_core_session_strdup(session, sip->sip_call_id->i_id); - switch_channel_set_variable(channel, "sip_call_id", tech_pvt->call_id); + if (sofia_private) { + if ((gateway = sofia_private->gateway)) { + if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name); + return; + } + } else if (!switch_strlen_zero(sofia_private->uuid)) { + if ((session = switch_core_session_locate(sofia_private->uuid))) { + tech_pvt = switch_core_session_get_private(session); + channel = switch_core_session_get_channel(tech_pvt->session); + if (!tech_pvt->call_id && sip && sip->sip_call_id && sip->sip_call_id->i_id) { + tech_pvt->call_id = switch_core_session_strdup(session, sip->sip_call_id->i_id); + switch_channel_set_variable(channel, "sip_call_id", tech_pvt->call_id); + } + } else { + /* too late */ + return; } - } else { - /* too late */ - return; } } @@ -143,7 +151,7 @@ void sofia_event_callback(nua_event_t event, } if (sip && (status == 401 || status == 407)) { - sofia_reg_handle_sip_r_challenge(status, phrase, nua, profile, nh, session, sip, tags); + sofia_reg_handle_sip_r_challenge(status, phrase, nua, profile, nh, session, gateway, sip, tags); goto done; } @@ -251,6 +259,10 @@ void sofia_event_callback(nua_event_t event, done: + if (gateway) { + sofia_reg_release_gateway(gateway); + } + if (session) { switch_core_session_rwunlock(session); @@ -686,18 +698,10 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag) if ((gp = sofia_reg_find_gateway(gateway->name))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring duplicate gateway '%s'\n", gateway->name); sofia_reg_release_gateway(gp); - } else if ((gp=sofia_reg_find_gateway(gateway->register_from))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring duplicate uri '%s'\n", gateway->register_from); - sofia_reg_release_gateway(gp); - } else if ((gp=sofia_reg_find_gateway(gateway->register_contact))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring duplicate contact '%s'\n", gateway->register_contact); - sofia_reg_release_gateway(gp); } else { gateway->next = profile->gateways; profile->gateways = gateway; sofia_reg_add_gateway(gateway->name, gateway); - sofia_reg_add_gateway(gateway->register_from, gateway); - sofia_reg_add_gateway(gateway->register_contact, gateway); } } diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 1dae459cbc..34dd60b0d7 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -660,13 +660,12 @@ void sofia_reg_handle_sip_r_register(int status, } void sofia_reg_handle_sip_r_challenge(int status, - char const *phrase, - nua_t * nua, sofia_profile_t *profile, nua_handle_t * nh, switch_core_session_t *session, sip_t const *sip, tagi_t tags[]) + char const *phrase, + nua_t * nua, sofia_profile_t *profile, nua_handle_t * nh, + switch_core_session_t *session, sofia_gateway_t *gateway, sip_t const *sip, tagi_t tags[]) { - sofia_gateway_t *gateway = NULL; sip_www_authenticate_t const *authenticate = NULL; char const *realm = NULL; - char *p = NULL, *duprealm = NULL, *qrealm = NULL; char const *scheme = NULL; int indexnum; char *cur; @@ -705,68 +704,9 @@ void sofia_reg_handle_sip_r_challenge(int status, return; } - if (profile) { - sofia_gateway_t *gateway_ptr = NULL; - - if ((duprealm = strdup(realm))) { - qrealm = duprealm; - - while (*qrealm && *qrealm == '"') { - qrealm++; - } - - if ((p = strchr(qrealm, '"'))) { - *p = '\0'; - } - - if (sip->sip_from) { - char *from_key = switch_mprintf("sip:%s@%s", - (char *) sip->sip_from->a_url->url_user, - (char *) sip->sip_from->a_url->url_host); - - if (!(gateway = sofia_reg_find_gateway(from_key))) { - gateway = sofia_reg_find_gateway(qrealm); - } - - switch_safe_free(from_key); - } - - if (!gateway) { - switch_mutex_lock(mod_sofia_globals.hash_mutex); - for (gateway_ptr = profile->gateways; gateway_ptr; gateway_ptr = gateway_ptr->next) { - if (scheme && qrealm && !strcasecmp(gateway_ptr->register_scheme, scheme) - && !strcasecmp(gateway_ptr->register_realm, qrealm)) { - gateway = gateway_ptr; - - if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) { -#ifdef SOFIA_DEBUG_RWLOCKS - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name); -#endif - gateway = NULL; - } -#ifdef SOFIA_DEBUG_RWLOCKS - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name); -#endif - break; - } - } - switch_mutex_unlock(mod_sofia_globals.hash_mutex); - } - - if (!gateway) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm); - } - - switch_safe_free(duprealm); - - if (!gateway) { - goto cancel; - } - - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n"); - goto cancel; - } + if (!gateway) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Matching gateway found\n"); + goto cancel; } snprintf(authentication, sizeof(authentication), "%s:%s:%s:%s", scheme, realm, gateway->register_username, gateway->register_password); @@ -779,16 +719,11 @@ void sofia_reg_handle_sip_r_challenge(int status, tl_gets(tags, NUTAG_CALLSTATE_REF(ss_state), SIPTAG_WWW_AUTHENTICATE_REF(authenticate), TAG_END()); nua_authenticate(nh, SIPTAG_EXPIRES_STR(gateway->expires_str), NUTAG_AUTH(authentication), TAG_END()); - if (gateway) { - sofia_reg_release_gateway(gateway); - gateway = NULL; - } + return; cancel: - if (gateway) { - sofia_reg_release_gateway(gateway); - } + if (session) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_hangup(channel, SWITCH_CAUSE_MANDATORY_IE_MISSING); diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index 231b0eb263..77743badc2 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -352,10 +352,16 @@ static switch_status_t read_packet(listener_t * listener, switch_event_t **event char *ptr; uint8_t crcount = 0; uint32_t max_len = sizeof(mbuf); + switch_channel_t *channel = NULL; *event = NULL; start = time(NULL); ptr = mbuf; + + if (listener->session) { + channel = switch_core_session_get_channel(listener->session); + } + while (listener->sock && !prefs.done) { uint8_t do_sleep = 1; mlen = 1; @@ -365,7 +371,7 @@ static switch_status_t read_packet(listener_t * listener, switch_event_t **event return SWITCH_STATUS_FALSE; } - if (listener->session && !switch_channel_ready(switch_core_session_get_channel(listener->session))) { + if (channel && !switch_channel_ready(channel)) { status = SWITCH_STATUS_FALSE; break; }