From b02971fea76bf92bfe8cf98c375a5d3465117251 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 21 May 2008 16:46:48 +0000 Subject: [PATCH] next chapter in the never-ending nat saga git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8502 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/endpoints/mod_sofia/mod_sofia.c | 2 +- src/mod/endpoints/mod_sofia/mod_sofia.h | 3 ++- src/mod/endpoints/mod_sofia/sofia.c | 32 ++++++++++++++++-------- src/mod/endpoints/mod_sofia/sofia_glue.c | 16 +++++------- src/mod/endpoints/mod_sofia/sofia_reg.c | 4 +-- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index d268aec289..6724aa3d67 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -1856,7 +1856,7 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session tech_pvt->dest_to = switch_core_session_sprintf(nsession, "sip:%s@%s", dest_to, host); } } - + if (!tech_pvt->dest_to) { tech_pvt->dest_to = tech_pvt->dest; diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 1b4c6d8ba7..df413013dc 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -145,7 +145,8 @@ typedef enum { PFLAG_WORKER_RUNNING = (1 << 17), PFLAG_UNREG_OPTIONS_FAIL = (1 << 18), PFLAG_DISABLE_TIMER = (1 << 19), - PFLAG_DISABLE_100REL = (1 << 20) + PFLAG_DISABLE_100REL = (1 << 20), + PFLAG_AGGRESSIVE_NAT_DETECTION = (1 << 21) } PFLAGS; typedef enum { diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 9f2367912f..f5b7359906 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -1117,6 +1117,8 @@ switch_status_t config_sofia(int reload, char *profile_name) switch_set_flag(profile, TFLAG_LATE_NEGOTIATION); } else if (!strcasecmp(var, "inbound-proxy-media") && switch_true(val)) { switch_set_flag(profile, TFLAG_PROXY_MEDIA); + } else if (!strcasecmp(var, "aggressive-nat-detection") && switch_true(val)) { + profile->pflags |= PFLAG_AGGRESSIVE_NAT_DETECTION; } else if (!strcasecmp(var, "rfc2833-pt")) { profile->te = (switch_payload_t) atoi(val); } else if (!strcasecmp(var, "cng-pt")) { @@ -2647,7 +2649,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ int is_auth = 0, calling_myself = 0; su_addrinfo_t *my_addrinfo = msg_addrinfo(nua_current_request(nua)); int network_port = 0; - char *sticky = NULL, *is_nat = NULL; + char *is_nat = NULL; if (sess_count >= sess_max || !(profile->pflags & PFLAG_RUNNING)) { nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END()); @@ -2659,12 +2661,24 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ nua_respond(nh, SIP_503_SERVICE_UNAVAILABLE, TAG_END()); return; } - get_addr(network_ip, sizeof(network_ip), &((struct sockaddr_in *) my_addrinfo->ai_addr)->sin_addr); network_port = ntohs(((struct sockaddr_in *) msg_addrinfo(nua_current_request(nua))->ai_addr)->sin_port); - - if (profile->nat_acl_count) { + + if ((profile->pflags & PFLAG_AGGRESSIVE_NAT_DETECTION)) { + if (sip && sip->sip_via) { + const char *port = sip->sip_via->v_port; + const char *host = sip->sip_via->v_host; + + if (host && strcmp(network_ip, host)) { + is_nat = "via host"; + } else if (port && atoi(port) != network_port) { + is_nat = "via port"; + } + } + } + + if (!is_nat && profile->nat_acl_count) { uint32_t x = 0; int ok = 1; char *last_acl = NULL; @@ -2687,7 +2701,6 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ } } } - if (profile->acl_count) { uint32_t x = 0; @@ -2718,7 +2731,6 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ } } } - if (!is_auth && ((profile->pflags & PFLAG_AUTH_CALLS) || (!(profile->pflags & PFLAG_BLIND_AUTH) && (sip->sip_proxy_authorization || sip->sip_authorization)))) { @@ -2729,22 +2741,18 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ if (v_event) { switch_event_destroy(&v_event); } - switch_safe_free(sticky); return; } - switch_safe_free(sticky); } is_auth++; } - if (!(sip->sip_contact && sip->sip_contact->m_url)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO CONTACT!\n"); nua_respond(nh, 400, "Missing Contact Header", TAG_END()); return; } - if (!sofia_endpoint_interface || !(session = switch_core_session_request(sofia_endpoint_interface, NULL))) { nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END()); return; @@ -2756,6 +2764,8 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ switch_core_session_destroy(&session); return; } + + switch_mutex_init(&tech_pvt->flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session)); tech_pvt->remote_ip = switch_core_session_strdup(session, network_ip); @@ -3189,7 +3199,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ if (is_nat) { switch_set_flag(tech_pvt, TFLAG_NAT); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setting NAT mode based on acl %s\n", is_nat); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setting NAT mode based on %s\n", is_nat); switch_channel_set_variable(channel, "sip_nat_detected", "true"); } diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index 54d2854061..e52a4d7a13 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -868,7 +868,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) uint32_t session_timeout = 0; const char *val; const char *rep; - char *sticky; + char *sticky = NULL; rep = switch_channel_get_variable(channel, SOFIA_REPLACES_HEADER); @@ -1001,19 +1001,15 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) SIPTAG_CONTACT_STR(invite_contact), TAG_END()); - - if (switch_test_flag(tech_pvt, TFLAG_NAT) || - (val = switch_channel_get_variable(channel, "sip-force-contact")) || - ((val = switch_channel_get_variable(channel, "sip_sticky_contact")) && switch_true(val))) { + + if (strstr(tech_pvt->dest, ";nat") || ((val = switch_channel_get_variable(channel, "sip_sticky_contact")) && switch_true(val))) { + switch_set_flag(tech_pvt, TFLAG_NAT); tech_pvt->record_route = switch_core_session_strdup(tech_pvt->session, url_str); sticky = tech_pvt->record_route; session_timeout = SOFIA_NAT_SESSION_TIMEOUT; switch_channel_set_variable(channel, "sip_nat_detected", "true"); } - - - - + /* TODO: We should use the new tags for making an rpid and add profile options to turn this on/off */ if (switch_test_flag(caller_profile, SWITCH_CPF_HIDE_NAME)) { priv = "name"; @@ -1104,7 +1100,6 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) sofia_glue_tech_patch_sdp(tech_pvt); } - nua_invite(tech_pvt->nh, NUTAG_AUTOANSWER(0), NUTAG_SESSION_TIMER(session_timeout), @@ -1112,6 +1107,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) TAG_IF(!switch_strlen_zero(alert_info), SIPTAG_HEADER_STR(alert_info)), TAG_IF(!switch_strlen_zero(extra_headers), SIPTAG_HEADER_STR(extra_headers)), TAG_IF(!switch_strlen_zero(max_forwards), SIPTAG_MAX_FORWARDS_STR(max_forwards)), + TAG_IF(sticky, NUTAG_PROXY(tech_pvt->record_route)), SOATAG_USER_SDP_STR(tech_pvt->local_sdp_str), SOATAG_REUSE_REJECTED(1), SOATAG_ORDERED_USER(1), diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index e95fa5e812..cf38438fda 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -529,10 +529,10 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han if (!strcasecmp(v_contact_str, "nat-connectile-dysfunction") || !strcasecmp(v_contact_str, "NDLB-connectile-dysfunction") || !strcasecmp(v_contact_str, "NDLB-tls-connectile-dysfunction")) { if (contact->m_url->url_params) { - switch_snprintf(contact_str, sizeof(contact_str), "%s ", + switch_snprintf(contact_str, sizeof(contact_str), "%s ;nat", display, contact->m_url->url_user, network_ip, network_port, contact->m_url->url_params); } else { - switch_snprintf(contact_str, sizeof(contact_str), "%s ", display, contact->m_url->url_user, network_ip, network_port); + switch_snprintf(contact_str, sizeof(contact_str), "%s ;nat", display, contact->m_url->url_user, network_ip, network_port); } if (strstr(v_contact_str, "tls")) { reg_desc = "Registered(TLSHACK)";