diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 4017042410..cf11601626 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -485,6 +485,7 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session) char *bye_headers = sofia_glue_get_extra_headers(channel, SOFIA_SIP_BYE_HEADER_PREFIX); const char *val = NULL; const char *max_forwards = switch_channel_get_variable(channel, SWITCH_MAX_FORWARDS_VARIABLE); + const char *call_info = switch_channel_get_variable(channel, "presence_call_info_full"); val = switch_channel_get_variable(tech_pvt->channel, "disable_q850_reason"); @@ -503,8 +504,6 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session) } if (switch_channel_test_flag(channel, CF_ANSWERED) || sofia_test_flag(tech_pvt, TFLAG_ANS)) { - const char *call_info = switch_channel_get_variable(channel, "presence_call_info_full"); - if (!tech_pvt->got_bye) { switch_channel_set_variable(channel, "sip_hangup_disposition", "send_bye"); } @@ -524,6 +523,7 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session) } if (!sofia_test_flag(tech_pvt, TFLAG_BYE)) { nua_cancel(tech_pvt->nh, + TAG_IF(call_info, SIPTAG_CALL_INFO_STR(call_info)), TAG_IF(!zstr(reason), SIPTAG_REASON_STR(reason)), TAG_IF(!zstr(bye_headers), SIPTAG_HEADER_STR(bye_headers)), TAG_END()); } } else { @@ -1592,6 +1592,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi case SWITCH_MESSAGE_INDICATE_DISPLAY: { const char *name = msg->string_array_arg[0], *number = msg->string_array_arg[1]; + const char *call_info = switch_channel_get_variable(channel, "presence_call_info_full"); if (!zstr(name)) { char message[256] = ""; @@ -1668,6 +1669,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi nua_update(tech_pvt->nh, NUTAG_SESSION_TIMER(tech_pvt->session_timeout), NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(call_info, SIPTAG_CALL_INFO_STR(call_info)), TAG_IF(!zstr(tech_pvt->route_uri), NUTAG_PROXY(tech_pvt->route_uri)), TAG_IF(!zstr_buf(message), SIPTAG_HEADER_STR(message)), TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), TAG_END()); @@ -1678,6 +1680,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi nua_update(tech_pvt->nh, NUTAG_SESSION_TIMER(tech_pvt->session_timeout), NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(call_info, SIPTAG_CALL_INFO_STR(call_info)), TAG_IF(!zstr(tech_pvt->route_uri), NUTAG_PROXY(tech_pvt->route_uri)), TAG_IF(!zstr_buf(message), SIPTAG_HEADER_STR(message)), TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), TAG_END()); @@ -1688,6 +1691,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi nua_update(tech_pvt->nh, NUTAG_SESSION_TIMER(tech_pvt->session_timeout), NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(call_info, SIPTAG_CALL_INFO_STR(call_info)), TAG_IF(!zstr(tech_pvt->route_uri), NUTAG_PROXY(tech_pvt->route_uri)), TAG_IF(!zstr_buf(message), SIPTAG_HEADER_STR(message)), TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), TAG_END()); @@ -1698,6 +1702,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi nua_update(tech_pvt->nh, NUTAG_SESSION_TIMER(tech_pvt->session_timeout), NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(call_info, SIPTAG_CALL_INFO_STR(call_info)), TAG_IF(!zstr(tech_pvt->route_uri), NUTAG_PROXY(tech_pvt->route_uri)), TAG_IF(!zstr_buf(message), SIPTAG_HEADER_STR(message)), TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), TAG_END()); diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 77a3a64e0c..f7ee7b64cc 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -8512,9 +8512,15 @@ void sofia_handle_sip_i_invite(switch_core_session_t *session, nua_t *nua, sofia switch_channel_t *b_channel = switch_core_session_get_channel(b_session); const char *bridge_uuid; switch_caller_profile_t *orig_cp; - const char *sent_name, *sent_number; + //const char *sent_name, *sent_number; orig_cp = switch_channel_get_caller_profile(b_channel); - + tech_pvt->caller_profile->callee_id_name = switch_core_strdup(tech_pvt->caller_profile->pool, orig_cp->callee_id_name); + tech_pvt->caller_profile->callee_id_number = switch_core_strdup(tech_pvt->caller_profile->pool, orig_cp->callee_id_number); + tech_pvt->caller_profile->caller_id_name = switch_core_strdup(tech_pvt->caller_profile->pool, orig_cp->caller_id_name); + tech_pvt->caller_profile->caller_id_number = switch_core_strdup(tech_pvt->caller_profile->pool, orig_cp->caller_id_number); + + +#if 0 sent_name = switch_channel_get_variable(b_channel, "last_sent_callee_id_name"); sent_number = switch_channel_get_variable(b_channel, "last_sent_callee_id_number"); @@ -8530,6 +8536,7 @@ void sofia_handle_sip_i_invite(switch_core_session_t *session, nua_t *nua, sofia tech_pvt->caller_profile->callee_id_number = switch_core_strdup(tech_pvt->caller_profile->pool, orig_cp->caller_id_number); } } +#endif if (is_nat) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Setting NAT mode based on %s\n", is_nat); @@ -8558,13 +8565,25 @@ void sofia_handle_sip_i_invite(switch_core_session_t *session, nua_t *nua, sofia codec_str = switch_core_session_sprintf(session, "set:absolute_codec_string=%s@%di,", read_impl.iananame, read_impl.microseconds_per_packet / 1000); } - - if (!zstr(bridge_uuid) && switch_channel_test_flag(b_channel, CF_LEG_HOLDING)) { tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "%sanswer,intercept:%s", codec_str, bridge_uuid); } else { + const char *name = NULL, *num = NULL; + switch_caller_profile_t *bcp = switch_channel_get_caller_profile(b_channel); + + if (switch_channel_test_flag(b_channel, CF_BRIDGE_ORIGINATOR) || !switch_channel_test_flag(b_channel, CF_BRIDGED)) { + name = bcp->callee_id_name; + num = bcp->callee_id_number; + } else { + name = bcp->caller_id_name; + num = bcp->caller_id_number; + } + + tech_pvt->caller_profile->callee_id_name = switch_core_strdup(tech_pvt->caller_profile->pool, name); + tech_pvt->caller_profile->callee_id_number = switch_core_strdup(tech_pvt->caller_profile->pool, num); + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "%sanswer,sofia_sla:%s", codec_str, b_private->uuid); } diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index 8b9982ccc8..3a1fc44d00 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -788,6 +788,2946 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) sipip = tech_pvt->profile->extsipip; } + if ((status = switch_stun_lookup(ip, port, stun_ip, stun_port, &error, pool)) != SWITCH_STATUS_SUCCESS) { + switch_yield(100000); + } else { + break; + } + } + if (status != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STUN Failed! %s:%d [%s]\n", stun_ip, stun_port, error); + goto out; + } + if (!*ip) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STUN Failed! No IP returned\n"); + goto out; + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "STUN Success [%s]:[%d]\n", *ip, *port); + status = SWITCH_STATUS_SUCCESS; + if (tech_pvt) { + if (myport == *port && !strcmp(*ip, tech_pvt->rtpip)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "STUN Not Required ip and port match. [%s]:[%d]\n", *ip, *port); + if (sofia_test_pflag(profile, PFLAG_STUN_AUTO_DISABLE)) { + sofia_clear_pflag(profile, PFLAG_STUN_ENABLED); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "STUN completely disabled.\n"); + } + } else { + tech_pvt->stun_ip = switch_core_session_strdup(tech_pvt->session, stun_ip); + tech_pvt->stun_port = stun_port; + tech_pvt->stun_flags |= STUN_FLAG_SET; + if (funny) { + tech_pvt->stun_flags |= STUN_FLAG_FUNNY; + } + } + } + } else { + *ip = (char *) sourceip; + status = SWITCH_STATUS_SUCCESS; + } + + out: + + switch_safe_free(stun_ip); + + return status; +} + + +const char *sofia_glue_get_unknown_header(sip_t const *sip, const char *name) +{ + sip_unknown_t *un; + for (un = sip->sip_unknown; un; un = un->un_next) { + if (!strcasecmp(un->un_name, name)) { + if (!zstr(un->un_value)) { + return un->un_value; + } + } + } + return NULL; +} + +switch_status_t sofia_glue_tech_choose_port(private_object_t *tech_pvt, int force) +{ + char *lookup_rtpip = tech_pvt->rtpip; /* Pointer to externally looked up address */ + switch_port_t sdp_port, rtcp_port; /* The external port to be sent in the SDP */ + const char *use_ip = NULL; /* The external IP to be sent in the SDP */ + + /* Don't do anything if we're in proxy mode or if a (remote) port already has been found */ + if (!force) { + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE) || + switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA) || tech_pvt->adv_sdp_audio_port) { + return SWITCH_STATUS_SUCCESS; + } + } + + /* Release the local sdp port */ + if (tech_pvt->local_sdp_audio_port) { + switch_rtp_release_port(tech_pvt->rtpip, tech_pvt->local_sdp_audio_port); + } + + /* Request a local port from the core's allocator */ + if (!(tech_pvt->local_sdp_audio_port = switch_rtp_request_port(tech_pvt->rtpip))) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_CRIT, "No RTP ports available!\n"); + return SWITCH_STATUS_FALSE; + } + + tech_pvt->local_sdp_audio_ip = tech_pvt->rtpip; + + sdp_port = tech_pvt->local_sdp_audio_port; + + /* Check if NAT is detected */ + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + /* Yes, map the port through switch_nat */ + switch_nat_add_mapping(tech_pvt->local_sdp_audio_port, SWITCH_NAT_UDP, &sdp_port, SWITCH_FALSE); + switch_nat_add_mapping(tech_pvt->local_sdp_audio_port + 1, SWITCH_NAT_UDP, &rtcp_port, SWITCH_FALSE); + + /* Find an IP address to use */ + if (!(use_ip = switch_channel_get_variable(tech_pvt->channel, "rtp_adv_audio_ip")) + && !zstr(tech_pvt->profile->extrtpip)) { + use_ip = tech_pvt->profile->extrtpip; + } + + if (use_ip) { + if (sofia_glue_ext_address_lookup(tech_pvt->profile, tech_pvt, &lookup_rtpip, &sdp_port, + use_ip, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + /* Address lookup was required and fail (external ip was "host:..." or "stun:...") */ + return SWITCH_STATUS_FALSE; + } else { + /* Address properly resolved, use it as external ip */ + use_ip = lookup_rtpip; + } + } else { + /* No external ip found, use the profile's rtp ip */ + use_ip = tech_pvt->rtpip; + } + } else { + /* No NAT traversal required, use the profile's rtp ip */ + use_ip = tech_pvt->rtpip; + } + + tech_pvt->adv_sdp_audio_port = sdp_port; + tech_pvt->adv_sdp_audio_ip = tech_pvt->extrtpip = switch_core_session_strdup(tech_pvt->session, use_ip); + + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE, tech_pvt->local_sdp_audio_ip); + switch_channel_set_variable_printf(tech_pvt->channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE, "%d", sdp_port); + switch_channel_set_variable(tech_pvt->channel, SWITCH_ADVERTISED_MEDIA_IP_VARIABLE, tech_pvt->adv_sdp_audio_ip); + + return SWITCH_STATUS_SUCCESS; +} + + +switch_status_t sofia_glue_tech_choose_video_port(private_object_t *tech_pvt, int force) +{ + char *lookup_rtpip = tech_pvt->rtpip; /* Pointer to externally looked up address */ + switch_port_t sdp_port; /* The external port to be sent in the SDP */ + const char *use_ip = NULL; /* The external IP to be sent in the SDP */ + + /* Don't do anything if we're in proxy mode or if a (remote) port already has been found */ + if (!force) { + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE) || + switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA) || tech_pvt->adv_sdp_video_port) { + return SWITCH_STATUS_SUCCESS; + } + } + + /* Release the local sdp port */ + if (tech_pvt->local_sdp_video_port) { + switch_rtp_release_port(tech_pvt->rtpip, tech_pvt->local_sdp_video_port); + } + + /* Request a local port from the core's allocator */ + if (!(tech_pvt->local_sdp_video_port = switch_rtp_request_port(tech_pvt->rtpip))) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_CRIT, "No RTP ports available!\n"); + return SWITCH_STATUS_FALSE; + } + + sdp_port = tech_pvt->local_sdp_video_port; + + /* Check if NAT is detected */ + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + /* Yes, map the port through switch_nat */ + switch_nat_add_mapping(tech_pvt->local_sdp_video_port, SWITCH_NAT_UDP, &sdp_port, SWITCH_FALSE); + + /* Find an IP address to use */ + if (!(use_ip = switch_channel_get_variable(tech_pvt->channel, "rtp_adv_video_ip")) + && !zstr(tech_pvt->profile->extrtpip)) { + use_ip = tech_pvt->profile->extrtpip; + } + + if (use_ip) { + if (sofia_glue_ext_address_lookup(tech_pvt->profile, tech_pvt, &lookup_rtpip, &sdp_port, + use_ip, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + /* Address lookup was required and fail (external ip was "host:..." or "stun:...") */ + return SWITCH_STATUS_FALSE; + } else { + /* Address properly resolved, use it as external ip */ + use_ip = lookup_rtpip; + } + } else { + /* No external ip found, use the profile's rtp ip */ + use_ip = tech_pvt->rtpip; + } + } else { + /* No NAT traversal required, use the profile's rtp ip */ + use_ip = tech_pvt->rtpip; + } + + tech_pvt->adv_sdp_video_port = sdp_port; + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_VIDEO_IP_VARIABLE, tech_pvt->adv_sdp_audio_ip); + switch_channel_set_variable_printf(tech_pvt->channel, SWITCH_LOCAL_VIDEO_PORT_VARIABLE, "%d", sdp_port); + + return SWITCH_STATUS_SUCCESS; +} + +sofia_transport_t sofia_glue_str2transport(const char *str) +{ + if (!strncasecmp(str, "udp", 3)) { + return SOFIA_TRANSPORT_UDP; + } else if (!strncasecmp(str, "tcp", 3)) { + return SOFIA_TRANSPORT_TCP; + } else if (!strncasecmp(str, "sctp", 4)) { + return SOFIA_TRANSPORT_SCTP; + } else if (!strncasecmp(str, "tls", 3)) { + return SOFIA_TRANSPORT_TCP_TLS; + } + + return SOFIA_TRANSPORT_UNKNOWN; +} + +enum tport_tls_verify_policy sofia_glue_str2tls_verify_policy(const char * str){ + char *ptr_next; + int len; + enum tport_tls_verify_policy ret; + char *ptr_cur = (char *) str; + ret = TPTLS_VERIFY_NONE; + + while (ptr_cur) { + if ((ptr_next = strchr(ptr_cur, '|'))) { + len = ptr_next++ - ptr_cur; + } else { + len = strlen(ptr_cur); + } + if (!strncasecmp(ptr_cur, "in",len)) { + ret |= TPTLS_VERIFY_IN; + } else if (!strncasecmp(ptr_cur, "out",len)) { + ret |= TPTLS_VERIFY_OUT; + } else if (!strncasecmp(ptr_cur, "all",len)) { + ret |= TPTLS_VERIFY_ALL; + } else if (!strncasecmp(ptr_cur, "subjects_in",len)) { + ret |= TPTLS_VERIFY_SUBJECTS_IN; + } else if (!strncasecmp(ptr_cur, "subjects_out",len)) { + ret |= TPTLS_VERIFY_SUBJECTS_OUT; + } else if (!strncasecmp(ptr_cur, "subjects_all",len)) { + ret |= TPTLS_VERIFY_SUBJECTS_ALL; + } + ptr_cur = ptr_next; + } + return ret; +} + +char *sofia_glue_find_parameter_value(switch_core_session_t *session, const char *str, const char *param) +{ + const char *param_ptr; + char *param_value; + char *tmp; + switch_size_t param_len; + + if (zstr(str) || zstr(param) || !session) return NULL; + + if (end_of(param) != '=') { + param = switch_core_session_sprintf(session, "%s=", param); + if (zstr(param)) return NULL; + } + + param_len = strlen(param); + param_ptr = sofia_glue_find_parameter(str, param); + + if (zstr(param_ptr)) return NULL; + + param_value = switch_core_session_strdup(session, param_ptr + param_len); + + if (zstr(param_value)) return NULL; + + if ((tmp = strchr(param_value, ';'))) *tmp = '\0'; + + return param_value; +} + +char *sofia_glue_find_parameter(const char *str, const char *param) +{ + char *ptr = NULL; + + ptr = (char *) str; + while (ptr) { + if (!strncasecmp(ptr, param, strlen(param))) + return ptr; + + if ((ptr = strchr(ptr, ';'))) + ptr++; + } + + return NULL; +} + +sofia_transport_t sofia_glue_url2transport(const url_t *url) +{ + char *ptr = NULL; + int tls = 0; + + if (!url) + return SOFIA_TRANSPORT_UNKNOWN; + + if (url->url_scheme && !strcasecmp(url->url_scheme, "sips")) { + tls++; + } + + if ((ptr = sofia_glue_find_parameter(url->url_params, "transport="))) { + return sofia_glue_str2transport(ptr + 10); + } + + return (tls) ? SOFIA_TRANSPORT_TCP_TLS : SOFIA_TRANSPORT_UDP; +} + +sofia_transport_t sofia_glue_via2transport(const sip_via_t * via) +{ + char *ptr = NULL; + + if (!via || !via->v_protocol) + return SOFIA_TRANSPORT_UNKNOWN; + + if ((ptr = strrchr(via->v_protocol, '/'))) { + ptr++; + + if (!strncasecmp(ptr, "udp", 3)) { + return SOFIA_TRANSPORT_UDP; + } else if (!strncasecmp(ptr, "tcp", 3)) { + return SOFIA_TRANSPORT_TCP; + } else if (!strncasecmp(ptr, "tls", 3)) { + return SOFIA_TRANSPORT_TCP_TLS; + } else if (!strncasecmp(ptr, "sctp", 4)) { + return SOFIA_TRANSPORT_SCTP; + } + } + + return SOFIA_TRANSPORT_UNKNOWN; +} + +const char *sofia_glue_transport2str(const sofia_transport_t tp) +{ + switch (tp) { + case SOFIA_TRANSPORT_TCP: + return "tcp"; + + case SOFIA_TRANSPORT_TCP_TLS: + return "tls"; + + case SOFIA_TRANSPORT_SCTP: + return "sctp"; + + default: + return "udp"; + } +} + +char *sofia_glue_create_external_via(switch_core_session_t *session, sofia_profile_t *profile, sofia_transport_t transport) +{ + return sofia_glue_create_via(session, profile->extsipip, (sofia_glue_transport_has_tls(transport)) + ? profile->tls_sip_port : profile->extsipport, transport); +} + +char *sofia_glue_create_via(switch_core_session_t *session, const char *ip, switch_port_t port, sofia_transport_t transport) +{ + if (port && port != 5060) { + if (session) { + return switch_core_session_sprintf(session, "SIP/2.0/%s %s:%d;rport", sofia_glue_transport2str(transport), ip, port); + } else { + return switch_mprintf("SIP/2.0/%s %s:%d;rport", sofia_glue_transport2str(transport), ip, port); + } + } else { + if (session) { + return switch_core_session_sprintf(session, "SIP/2.0/%s %s;rport", sofia_glue_transport2str(transport), ip); + } else { + return switch_mprintf("SIP/2.0/%s %s;rport", sofia_glue_transport2str(transport), ip); + } + } +} + +char *sofia_glue_strip_uri(const char *str) +{ + char *p; + char *r; + + if ((p = strchr(str, '<'))) { + p++; + r = strdup(p); + if ((p = strchr(r, '>'))) { + *p = '\0'; + } + } else { + r = strdup(str); + } + + return r; +} + +int sofia_glue_check_nat(sofia_profile_t *profile, const char *network_ip) +{ + switch_assert(network_ip); + + return (profile->extsipip && + !switch_check_network_list_ip(network_ip, "loopback.auto") && + !switch_check_network_list_ip(network_ip, profile->local_network)); +} + +int sofia_glue_transport_has_tls(const sofia_transport_t tp) +{ + switch (tp) { + case SOFIA_TRANSPORT_TCP_TLS: + return 1; + + default: + return 0; + } +} + +void sofia_glue_get_addr(msg_t *msg, char *buf, size_t buflen, int *port) +{ + su_addrinfo_t *addrinfo = msg_addrinfo(msg); + + if (buf) { + get_addr(buf, buflen, addrinfo->ai_addr, addrinfo->ai_addrlen); + } + + if (port) { + *port = get_port(addrinfo->ai_addr); + } +} + +char *sofia_overcome_sip_uri_weakness(switch_core_session_t *session, const char *uri, const sofia_transport_t transport, switch_bool_t uri_only, + const char *params, const char *invite_tel_params) +{ + char *stripped = switch_core_session_strdup(session, uri); + char *new_uri = NULL; + char *p; + + + stripped = sofia_glue_get_url_from_contact(stripped, 0); + + /* remove our params so we don't make any whiny moronic device piss it's pants and forget who it is for a half-hour */ + if ((p = (char *) switch_stristr(";fs_", stripped))) { + *p = '\0'; + } + + if (transport && transport != SOFIA_TRANSPORT_UDP) { + + if (switch_stristr("port=", stripped)) { + new_uri = switch_core_session_sprintf(session, "%s%s%s", uri_only ? "" : "<", stripped, uri_only ? "" : ">"); + } else { + + if (strchr(stripped, ';')) { + if (params) { + new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s;%s%s", + uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">"); + } else { + new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s%s", + uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">"); + } + } else { + if (params) { + new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s;%s%s", + uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">"); + } else { + new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s%s", + uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">"); + } + } + } + } else { + if (params) { + new_uri = switch_core_session_sprintf(session, "%s%s;%s%s", uri_only ? "" : "<", stripped, params, uri_only ? "" : ">"); + } else { + if (uri_only) { + new_uri = stripped; + } else { + new_uri = switch_core_session_sprintf(session, "<%s>", stripped); + } + } + } + + + + if (!zstr(invite_tel_params)) { + char *lhs, *rhs = strchr(new_uri, '@'); + + if (!zstr(rhs)) { + *rhs++ = '\0'; + lhs = new_uri; + new_uri = switch_core_session_sprintf(session, "%s;%s@%s", lhs, invite_tel_params, rhs); + } + } + + return new_uri; +} + +#define RA_PTR_LEN 512 +switch_status_t sofia_glue_tech_proxy_remote_addr(private_object_t *tech_pvt, const char *sdp_str) +{ + const char *err; + char rip[RA_PTR_LEN] = ""; + char rp[RA_PTR_LEN] = ""; + char rvp[RA_PTR_LEN] = ""; + char *p, *ip_ptr = NULL, *port_ptr = NULL, *vid_port_ptr = NULL, *pe; + int x; + const char *val; + switch_status_t status = SWITCH_STATUS_FALSE; + + if (zstr(sdp_str)) { + sdp_str = tech_pvt->remote_sdp_str; + } + + if (zstr(sdp_str)) { + goto end; + } + + if ((p = (char *) switch_stristr("c=IN IP4 ", sdp_str)) || (p = (char *) switch_stristr("c=IN IP6 ", sdp_str))) { + ip_ptr = p + 9; + } + + if ((p = (char *) switch_stristr("m=audio ", sdp_str))) { + port_ptr = p + 8; + } + + if ((p = (char *) switch_stristr("m=image ", sdp_str))) { + char *tmp = p + 8; + + if (tmp && atoi(tmp)) { + port_ptr = tmp; + } + } + + if ((p = (char *) switch_stristr("m=video ", sdp_str))) { + vid_port_ptr = p + 8; + } + + if (!(ip_ptr && port_ptr)) { + goto end; + } + + p = ip_ptr; + pe = p + strlen(p); + x = 0; + while (x < sizeof(rip) - 1 && p && *p && ((*p >= '0' && *p <= '9') || *p == '.' || *p == ':' || (*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F'))) { + rip[x++] = *p; + p++; + if (p >= pe) { + goto end; + } + } + + p = port_ptr; + x = 0; + while (x < sizeof(rp) - 1 && p && *p && (*p >= '0' && *p <= '9')) { + rp[x++] = *p; + p++; + if (p >= pe) { + goto end; + } + } + + p = vid_port_ptr; + x = 0; + while (x < sizeof(rvp) - 1 && p && *p && (*p >= '0' && *p <= '9')) { + rvp[x++] = *p; + p++; + if (p >= pe) { + goto end; + } + } + + if (!(*rip && *rp)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "invalid SDP\n"); + goto end; + } + + tech_pvt->remote_sdp_audio_ip = switch_core_session_strdup(tech_pvt->session, rip); + tech_pvt->remote_sdp_audio_port = (switch_port_t) atoi(rp); + + if (*rvp) { + tech_pvt->remote_sdp_video_ip = switch_core_session_strdup(tech_pvt->session, rip); + tech_pvt->remote_sdp_video_port = (switch_port_t) atoi(rvp); + } + + if (tech_pvt->remote_sdp_video_ip && tech_pvt->remote_sdp_video_port) { + if (!strcmp(tech_pvt->remote_sdp_video_ip, rip) && atoi(rvp) == tech_pvt->remote_sdp_video_port) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Remote video address:port [%s:%d] has not changed.\n", + tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port); + } else { + sofia_set_flag_locked(tech_pvt, TFLAG_VIDEO); + switch_channel_set_flag(tech_pvt->channel, CF_VIDEO); + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + const char *rport = NULL; + switch_port_t remote_rtcp_port = 0; + + if ((rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_video_rtcp_port"))) { + remote_rtcp_port = (switch_port_t)atoi(rport); + } + + + if (switch_rtp_set_remote_address(tech_pvt->video_rtp_session, tech_pvt->remote_sdp_video_ip, + tech_pvt->remote_sdp_video_port, remote_rtcp_port, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "VIDEO RTP REPORTS ERROR: [%s]\n", err); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "VIDEO RTP CHANGING DEST TO: [%s:%d]\n", + tech_pvt->remote_sdp_video_ip, tech_pvt->remote_sdp_video_port); + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && !switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + /* Reactivate the NAT buster flag. */ + switch_rtp_set_flag(tech_pvt->video_rtp_session, SWITCH_RTP_FLAG_AUTOADJ); + } + if (sofia_test_pflag(tech_pvt->profile, PFLAG_AUTOFIX_TIMING)) { + tech_pvt->check_frames = 0; + } + } + } + } + } + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + char *remote_host = switch_rtp_get_remote_host(tech_pvt->rtp_session); + switch_port_t remote_port = switch_rtp_get_remote_port(tech_pvt->rtp_session); + const char *rport = NULL; + switch_port_t remote_rtcp_port = 0; + + if (remote_host && remote_port && !strcmp(remote_host, tech_pvt->remote_sdp_audio_ip) && remote_port == tech_pvt->remote_sdp_audio_port) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Remote address:port [%s:%d] has not changed.\n", + tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port); + switch_goto_status(SWITCH_STATUS_BREAK, end); + } + + if ((rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_audio_rtcp_port"))) { + remote_rtcp_port = (switch_port_t)atoi(rport); + } + + + if (switch_rtp_set_remote_address(tech_pvt->rtp_session, tech_pvt->remote_sdp_audio_ip, + tech_pvt->remote_sdp_audio_port, remote_rtcp_port, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "AUDIO RTP REPORTS ERROR: [%s]\n", err); + status = SWITCH_STATUS_GENERR; + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "AUDIO RTP CHANGING DEST TO: [%s:%d]\n", + tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port); + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + /* Reactivate the NAT buster flag. */ + switch_rtp_set_flag(tech_pvt->rtp_session, SWITCH_RTP_FLAG_AUTOADJ); + } + if (sofia_test_pflag(tech_pvt->profile, PFLAG_AUTOFIX_TIMING)) { + tech_pvt->check_frames = 0; + } + status = SWITCH_STATUS_SUCCESS; + } + } + + end: + + return status; +} + + +void sofia_glue_tech_patch_sdp(private_object_t *tech_pvt) +{ + switch_size_t len; + char *p, *q, *pe, *qe; + int has_video = 0, has_audio = 0, has_ip = 0; + char port_buf[25] = ""; + char vport_buf[25] = ""; + char *new_sdp; + int bad = 0; + + if (zstr(tech_pvt->local_sdp_str)) { + return; + } + + len = strlen(tech_pvt->local_sdp_str) * 2; + + if (switch_channel_test_flag(tech_pvt->channel, CF_ANSWERED) && + (switch_stristr("sendonly", tech_pvt->local_sdp_str) || switch_stristr("0.0.0.0", tech_pvt->local_sdp_str))) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Skip patch on hold SDP\n"); + return; + } + + if (zstr(tech_pvt->adv_sdp_audio_ip) || !tech_pvt->adv_sdp_audio_port) { + if (sofia_glue_tech_choose_port(tech_pvt, 1) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "%s I/O Error\n", + switch_channel_get_name(tech_pvt->channel)); + return; + } + tech_pvt->iananame = switch_core_session_strdup(tech_pvt->session, "PROXY"); + tech_pvt->rm_rate = 8000; + tech_pvt->codec_ms = 20; + } + + new_sdp = switch_core_session_alloc(tech_pvt->session, len); + switch_snprintf(port_buf, sizeof(port_buf), "%u", tech_pvt->adv_sdp_audio_port); + + + p = tech_pvt->local_sdp_str; + q = new_sdp; + pe = p + strlen(p); + qe = q + len - 1; + + + while (p && *p) { + if (p >= pe) { + bad = 1; + goto end; + } + + if (q >= qe) { + bad = 2; + goto end; + } + + if (tech_pvt->adv_sdp_audio_ip && !strncmp("c=IN IP", p, 7)) { + strncpy(q, p, 7); + p += 7; + q += 7; + strncpy(q, strchr(tech_pvt->adv_sdp_audio_ip, ':') ? "6 " : "4 ", 2); + p +=2; + q +=2; + strncpy(q, tech_pvt->adv_sdp_audio_ip, strlen(tech_pvt->adv_sdp_audio_ip)); + q += strlen(tech_pvt->adv_sdp_audio_ip); + + while (p && *p && ((*p >= '0' && *p <= '9') || *p == '.' || *p == ':' || (*p >= 'A' && *p <= 'F') || (*p >= 'a' && *p <= 'f'))) { + if (p >= pe) { + bad = 3; + goto end; + } + p++; + } + + has_ip++; + + } else if (!strncmp("o=", p, 2)) { + char *oe = strchr(p, '\n'); + switch_size_t len; + + if (oe) { + const char *family = "IP4"; + char o_line[1024] = ""; + + if (oe >= pe) { + bad = 5; + goto end; + } + + len = (oe - p); + p += len; + + + family = strchr(tech_pvt->profile->sipip, ':') ? "IP6" : "IP4"; + + if (!tech_pvt->owner_id) { + tech_pvt->owner_id = (uint32_t) switch_epoch_time_now(NULL) * 31821U + 13849U; + } + + if (!tech_pvt->session_id) { + tech_pvt->session_id = tech_pvt->owner_id; + } + + tech_pvt->session_id++; + + + snprintf(o_line, sizeof(o_line), "o=%s %010u %010u IN %s %s\n", + tech_pvt->profile->username, tech_pvt->owner_id, tech_pvt->session_id, family, tech_pvt->profile->sipip); + + strncpy(q, o_line, strlen(o_line)); + q += strlen(o_line) - 1; + + } + + } else if (!strncmp("s=", p, 2)) { + char *se = strchr(p, '\n'); + switch_size_t len; + + if (se) { + char s_line[1024] = ""; + + if (se >= pe) { + bad = 5; + goto end; + } + + len = (se - p); + p += len; + + snprintf(s_line, sizeof(s_line), "s=%s\n", tech_pvt->profile->username); + + strncpy(q, s_line, strlen(s_line)); + q += strlen(s_line) - 1; + + } + + } else if ((!strncmp("m=audio ", p, 8) && *(p + 8) != '0') || (!strncmp("m=image ", p, 8) && *(p + 8) != '0')) { + strncpy(q, p, 8); + p += 8; + + if (p >= pe) { + bad = 4; + goto end; + } + + + q += 8; + + if (q >= qe) { + bad = 5; + goto end; + } + + + strncpy(q, port_buf, strlen(port_buf)); + q += strlen(port_buf); + + if (q >= qe) { + bad = 6; + goto end; + } + + while (p && *p && (*p >= '0' && *p <= '9')) { + if (p >= pe) { + bad = 7; + goto end; + } + p++; + } + + has_audio++; + + } else if (!strncmp("m=video ", p, 8) && *(p + 8) != '0') { + if (!has_video) { + sofia_glue_tech_choose_video_port(tech_pvt, 1); + tech_pvt->video_rm_encoding = "PROXY-VID"; + tech_pvt->video_rm_rate = 90000; + tech_pvt->video_codec_ms = 0; + switch_snprintf(vport_buf, sizeof(vport_buf), "%u", tech_pvt->adv_sdp_video_port); + if (switch_channel_media_ready(tech_pvt->channel) && !switch_rtp_ready(tech_pvt->video_rtp_session)) { + sofia_set_flag(tech_pvt, TFLAG_VIDEO); + sofia_set_flag(tech_pvt, TFLAG_REINVITE); + sofia_glue_activate_rtp(tech_pvt, 0); + } + } + + strncpy(q, p, 8); + p += 8; + + if (p >= pe) { + bad = 8; + goto end; + } + + q += 8; + + if (q >= qe) { + bad = 9; + goto end; + } + + strncpy(q, vport_buf, strlen(vport_buf)); + q += strlen(vport_buf); + + if (q >= qe) { + bad = 10; + goto end; + } + + while (p && *p && (*p >= '0' && *p <= '9')) { + + if (p >= pe) { + bad = 11; + goto end; + } + + p++; + } + + has_video++; + } + + while (p && *p && *p != '\n') { + + if (p >= pe) { + bad = 12; + goto end; + } + + if (q >= qe) { + bad = 13; + goto end; + } + + *q++ = *p++; + } + + if (p >= pe) { + bad = 14; + goto end; + } + + if (q >= qe) { + bad = 15; + goto end; + } + + *q++ = *p++; + + } + + end: + + if (bad) { + return; + } + + + if (switch_channel_down(tech_pvt->channel) || sofia_test_flag(tech_pvt, TFLAG_BYE)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "%s too late.\n", switch_channel_get_name(tech_pvt->channel)); + return; + } + + + if (!has_ip && !has_audio) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "%s SDP has no audio in it.\n%s\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->local_sdp_str); + return; + } + + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "%s Patched SDP\n---\n%s\n+++\n%s\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->local_sdp_str, new_sdp); + + sofia_glue_tech_set_local_sdp(tech_pvt, new_sdp, SWITCH_FALSE); + +} + + +void sofia_glue_tech_set_local_sdp(private_object_t *tech_pvt, const char *sdp_str, switch_bool_t dup) +{ + switch_mutex_lock(tech_pvt->sofia_mutex); + tech_pvt->local_sdp_str = dup ? switch_core_session_strdup(tech_pvt->session, sdp_str) : (char *) sdp_str; + switch_channel_set_variable(tech_pvt->channel, "sip_local_sdp_str", tech_pvt->local_sdp_str); + switch_mutex_unlock(tech_pvt->sofia_mutex); +} + +char *sofia_glue_get_multipart(switch_core_session_t *session, const char *prefix, const char *sdp, char **mp_type) +{ + char *extra_headers = NULL; + switch_stream_handle_t stream = { 0 }; + switch_event_header_t *hi = NULL; + int x = 0; + switch_channel_t *channel = switch_core_session_get_channel(session); + const char *boundary = switch_core_session_get_uuid(session); + + SWITCH_STANDARD_STREAM(stream); + if ((hi = switch_channel_variable_first(channel))) { + for (; hi; hi = hi->next) { + const char *name = (char *) hi->name; + char *value = (char *) hi->value; + + if (!strncasecmp(name, prefix, strlen(prefix))) { + const char *hname = name + strlen(prefix); + if (*value == '~') { + stream.write_function(&stream, "--%s\nContent-Type: %s\nContent-Length: %d\n%s\n", boundary, hname, strlen(value), value + 1); + } else { + stream.write_function(&stream, "--%s\nContent-Type: %s\nContent-Length: %d\n\n%s\n", boundary, hname, strlen(value) + 1, value); + } + x++; + } + } + switch_channel_variable_last(channel); + } + + if (x) { + *mp_type = switch_core_session_sprintf(session, "multipart/mixed; boundary=%s", boundary); + if (sdp) { + stream.write_function(&stream, "--%s\nContent-Type: application/sdp\nContent-Length: %d\n\n%s\n", boundary, strlen(sdp) + 1, sdp); + } + stream.write_function(&stream, "--%s--\n", boundary); + } + + if (!zstr((char *) stream.data)) { + extra_headers = stream.data; + } else { + switch_safe_free(stream.data); + } + + return extra_headers; +} + + +char *sofia_glue_get_extra_headers(switch_channel_t *channel, const char *prefix) +{ + char *extra_headers = NULL; + switch_stream_handle_t stream = { 0 }; + switch_event_header_t *hi = NULL; + const char *exclude_regex = NULL; + switch_regex_t *re = NULL; + int ovector[30] = {0}; + int proceed; + + exclude_regex = switch_channel_get_variable(channel, "exclude_outgoing_extra_header"); + SWITCH_STANDARD_STREAM(stream); + if ((hi = switch_channel_variable_first(channel))) { + for (; hi; hi = hi->next) { + const char *name = (char *) hi->name; + char *value = (char *) hi->value; + + if (!strncasecmp(name, prefix, strlen(prefix))) { + if ( !exclude_regex || !(proceed = switch_regex_perform(name, exclude_regex, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) { + const char *hname = name + strlen(prefix); + stream.write_function(&stream, "%s: %s\r\n", hname, value); + switch_regex_safe_free(re); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Ignoring Extra Header [%s] , matches exclude_outgoing_extra_header [%s]\n", name, exclude_regex); + } + } + } + switch_channel_variable_last(channel); + } + + if (!zstr((char *) stream.data)) { + extra_headers = stream.data; + } else { + switch_safe_free(stream.data); + } + + return extra_headers; +} + +void sofia_glue_set_extra_headers(switch_core_session_t *session, sip_t const *sip, const char *prefix) +{ + sip_unknown_t *un; + char name[512] = ""; + switch_channel_t *channel = switch_core_session_get_channel(session); + char *pstr; + + + if (!sip || !channel) { + return; + } + + for (un = sip->sip_unknown; un; un = un->un_next) { + if ((!strncasecmp(un->un_name, "X-", 2) && strncasecmp(un->un_name, "X-FS-", 5)) || !strncasecmp(un->un_name, "P-", 2)) { + if (!zstr(un->un_value)) { + switch_snprintf(name, sizeof(name), "%s%s", prefix, un->un_name); + switch_channel_set_variable(channel, name, un->un_value); + } + } + } + + pstr = switch_core_session_sprintf(session, "execute_on_%sprefix", prefix); + switch_channel_execute_on(channel, pstr); + switch_channel_api_on(channel, pstr); + + switch_channel_execute_on(channel, "execute_on_sip_extra_headers"); + switch_channel_api_on(channel, "api_on_sip_extra_headers"); +} + +char *sofia_glue_get_extra_headers_from_event(switch_event_t *event, const char *prefix) +{ + char *extra_headers = NULL; + switch_stream_handle_t stream = { 0 }; + switch_event_header_t *hp; + + SWITCH_STANDARD_STREAM(stream); + for (hp = event->headers; hp; hp = hp->next) { + if (!zstr(hp->name) && !zstr(hp->value) && !strncasecmp(hp->name, prefix, strlen(prefix))) { + char *name = strdup(hp->name); + const char *hname = name + strlen(prefix); + stream.write_function(&stream, "%s: %s\r\n", hname, (char *)hp->value); + free(name); + } + } + + if (!zstr((char *) stream.data)) { + extra_headers = stream.data; + } else { + switch_safe_free(stream.data); + } + + return extra_headers; +} + +switch_status_t sofia_glue_do_invite(switch_core_session_t *session) +{ + char *alert_info = NULL; + const char *max_forwards = NULL; + const char *alertbuf; + private_object_t *tech_pvt = switch_core_session_get_private(session); + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_caller_profile_t *caller_profile; + const char *cid_name, *cid_num; + char *e_dest = NULL; + const char *holdstr = ""; + char *extra_headers = NULL; + switch_status_t status = SWITCH_STATUS_FALSE; + uint32_t session_timeout = 0; + const char *val; + const char *rep; + const char *call_id = NULL; + char *route = NULL; + char *route_uri = NULL; + sofia_destination_t *dst = NULL; + sofia_cid_type_t cid_type = tech_pvt->profile->cid_type; + sip_cseq_t *cseq = NULL; + const char *invite_record_route = switch_channel_get_variable(tech_pvt->channel, "sip_invite_record_route"); + const char *invite_route_uri = switch_channel_get_variable(tech_pvt->channel, "sip_invite_route_uri"); + const char *invite_full_from = switch_channel_get_variable(tech_pvt->channel, "sip_invite_full_from"); + const char *invite_full_to = switch_channel_get_variable(tech_pvt->channel, "sip_invite_full_to"); + const char *handle_full_from = switch_channel_get_variable(tech_pvt->channel, "sip_handle_full_from"); + const char *handle_full_to = switch_channel_get_variable(tech_pvt->channel, "sip_handle_full_to"); + const char *force_full_from = switch_channel_get_variable(tech_pvt->channel, "sip_force_full_from"); + const char *force_full_to = switch_channel_get_variable(tech_pvt->channel, "sip_force_full_to"); + char *mp = NULL, *mp_type = NULL; + char *record_route = NULL; + const char *recover_via = NULL; + int require_timer = 1; + + if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) { + const char *recover_contact = switch_channel_get_variable(tech_pvt->channel, "sip_recover_contact"); + recover_via = switch_channel_get_variable(tech_pvt->channel, "sip_recover_via"); + + if (!zstr(invite_record_route)) { + record_route = switch_core_session_sprintf(session, "Record-Route: %s", invite_record_route); + } + + if (recover_contact) { + char *tmp = switch_core_session_strdup(session, recover_contact); + tech_pvt->redirected = sofia_glue_get_url_from_contact(tmp, 0); + } + } + + + if ((rep = switch_channel_get_variable(channel, SOFIA_REPLACES_HEADER))) { + switch_channel_set_variable(channel, SOFIA_REPLACES_HEADER, NULL); + } + + switch_assert(tech_pvt != NULL); + + sofia_clear_flag_locked(tech_pvt, TFLAG_SDP); + + caller_profile = switch_channel_get_caller_profile(channel); + + if (!caller_profile) { + switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + return SWITCH_STATUS_FALSE; + } + + + if ((val = switch_channel_get_variable_dup(channel, "sip_require_timer", SWITCH_FALSE, -1)) && switch_false(val)) { + require_timer = 0; + } + + + cid_name = caller_profile->caller_id_name; + cid_num = caller_profile->caller_id_number; + sofia_glue_tech_prepare_codecs(tech_pvt); + sofia_glue_check_video_codecs(tech_pvt); + check_decode(cid_name, session); + check_decode(cid_num, session); + + + if ((alertbuf = switch_channel_get_variable(channel, "alert_info"))) { + alert_info = switch_core_session_sprintf(tech_pvt->session, "Alert-Info: %s", alertbuf); + } + + max_forwards = switch_channel_get_variable(channel, SWITCH_MAX_FORWARDS_VARIABLE); + + if ((status = sofia_glue_tech_choose_port(tech_pvt, 0)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Port Error!\n"); + return status; + } + + if (!switch_channel_get_private(tech_pvt->channel, "t38_options") || zstr(tech_pvt->local_sdp_str)) { + sofia_glue_set_local_sdp(tech_pvt, NULL, 0, NULL, 0); + } + + sofia_set_flag_locked(tech_pvt, TFLAG_READY); + + if (!tech_pvt->nh) { + char *d_url = NULL, *url = NULL, *url_str = NULL; + sofia_private_t *sofia_private; + char *invite_contact = NULL, *to_str, *use_from_str, *from_str; + const char *t_var; + char *rpid_domain = NULL, *p; + const char *priv = "off"; + const char *screen = "no"; + const char *invite_params = switch_channel_get_variable(tech_pvt->channel, "sip_invite_params"); + const char *invite_to_params = switch_channel_get_variable(tech_pvt->channel, "sip_invite_to_params"); + const char *invite_tel_params = switch_channel_get_variable(switch_core_session_get_channel(session), "sip_invite_tel_params"); + const char *invite_to_uri = switch_channel_get_variable(tech_pvt->channel, "sip_invite_to_uri"); + const char *invite_from_uri = switch_channel_get_variable(tech_pvt->channel, "sip_invite_from_uri"); + const char *invite_contact_params = switch_channel_get_variable(tech_pvt->channel, "sip_invite_contact_params"); + const char *invite_from_params = switch_channel_get_variable(tech_pvt->channel, "sip_invite_from_params"); + const char *from_var = switch_channel_get_variable(tech_pvt->channel, "sip_from_uri"); + const char *from_display = switch_channel_get_variable(tech_pvt->channel, "sip_from_display"); + const char *invite_req_uri = switch_channel_get_variable(tech_pvt->channel, "sip_invite_req_uri"); + const char *invite_domain = switch_channel_get_variable(tech_pvt->channel, "sip_invite_domain"); + + const char *use_name, *use_number; + + if (zstr(tech_pvt->dest)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "URL Error!\n"); + return SWITCH_STATUS_FALSE; + } + + if ((d_url = sofia_glue_get_url_from_contact(tech_pvt->dest, 1))) { + url = d_url; + } else { + url = tech_pvt->dest; + } + + url_str = url; + + if (!tech_pvt->from_str) { + const char *sipip; + const char *format; + + sipip = tech_pvt->profile->sipip; + + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + sipip = tech_pvt->profile->extsipip; + } + + format = strchr(sipip, ':') ? "\"%s\" " : "\"%s\" "; + + if (!zstr(invite_domain)) { + sipip = invite_domain; + } + + tech_pvt->from_str = switch_core_session_sprintf(tech_pvt->session, format, cid_name, cid_num, !zstr(cid_num) ? "@" : "", sipip); + } + + if (from_var) { + if (strncasecmp(from_var, "sip:", 4) || strncasecmp(from_var, "sips:", 5)) { + use_from_str = switch_core_session_strdup(tech_pvt->session, from_var); + } else { + use_from_str = switch_core_session_sprintf(tech_pvt->session, "sip:%s", from_var); + } + } else if (!zstr(tech_pvt->gateway_from_str)) { + use_from_str = tech_pvt->gateway_from_str; + } else { + use_from_str = tech_pvt->from_str; + } + + if (!zstr(tech_pvt->gateway_from_str)) { + rpid_domain = switch_core_session_strdup(session, tech_pvt->gateway_from_str); + } else if (!zstr(tech_pvt->from_str)) { + rpid_domain = switch_core_session_strdup(session, use_from_str); + } + + sofia_glue_get_url_from_contact(rpid_domain, 0); + if ((rpid_domain = strrchr(rpid_domain, '@'))) { + rpid_domain++; + if ((p = strchr(rpid_domain, ';'))) { + *p = '\0'; + } + } + + if (sofia_test_pflag(tech_pvt->profile, PFLAG_AUTO_NAT)) { + if (!zstr(tech_pvt->remote_ip) && !zstr(tech_pvt->profile->extsipip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + rpid_domain = tech_pvt->profile->extsipip; + } else { + rpid_domain = tech_pvt->profile->sipip; + } + } + + if (!zstr(invite_domain)) { + rpid_domain = (char *)invite_domain; + } + + if (zstr(rpid_domain)) { + rpid_domain = "cluecon.com"; + } + + /* + * Ignore transport chanvar and uri parameter for gateway connections + * since all of them have been already taken care of in mod_sofia.c:sofia_outgoing_channel() + */ + if (tech_pvt->transport == SOFIA_TRANSPORT_UNKNOWN && zstr(tech_pvt->gateway_name)) { + if ((p = (char *) switch_stristr("port=", url))) { + p += 5; + tech_pvt->transport = sofia_glue_str2transport(p); + } else { + if ((t_var = switch_channel_get_variable(channel, "sip_transport"))) { + tech_pvt->transport = sofia_glue_str2transport(t_var); + } + } + + if (tech_pvt->transport == SOFIA_TRANSPORT_UNKNOWN) { + tech_pvt->transport = SOFIA_TRANSPORT_UDP; + } + } + + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + tech_pvt->user_via = sofia_glue_create_external_via(session, tech_pvt->profile, tech_pvt->transport); + } + + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_TLS) && sofia_glue_transport_has_tls(tech_pvt->transport)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "TLS not supported by profile\n"); + return SWITCH_STATUS_FALSE; + } + + if (zstr(tech_pvt->invite_contact)) { + const char *contact; + if ((contact = switch_channel_get_variable(channel, "sip_contact_user"))) { + char *ip_addr = tech_pvt->profile->sipip; + char *ipv6; + + if ( !zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip ) ) { + ip_addr = tech_pvt->profile->extsipip; + } + + ipv6 = strchr(ip_addr, ':'); + + if (sofia_glue_transport_has_tls(tech_pvt->transport)) { + tech_pvt->invite_contact = switch_core_session_sprintf(session, "sip:%s@%s%s%s:%d", contact, + ipv6 ? "[" : "", ip_addr, ipv6 ? "]" : "", tech_pvt->profile->tls_sip_port); + } else { + tech_pvt->invite_contact = switch_core_session_sprintf(session, "sip:%s@%s%s%s:%d", contact, + ipv6 ? "[" : "", ip_addr, ipv6 ? "]" : "", tech_pvt->profile->extsipport); + } + } else { + if (sofia_glue_transport_has_tls(tech_pvt->transport)) { + tech_pvt->invite_contact = tech_pvt->profile->tls_url; + } else { + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + tech_pvt->invite_contact = tech_pvt->profile->public_url; + } else { + tech_pvt->invite_contact = tech_pvt->profile->url; + } + } + } + } + + url_str = sofia_overcome_sip_uri_weakness(session, url, tech_pvt->transport, SWITCH_TRUE, invite_params, invite_tel_params); + invite_contact = sofia_overcome_sip_uri_weakness(session, tech_pvt->invite_contact, tech_pvt->transport, SWITCH_FALSE, invite_contact_params, NULL); + from_str = sofia_overcome_sip_uri_weakness(session, invite_from_uri ? invite_from_uri : use_from_str, 0, SWITCH_TRUE, invite_from_params, NULL); + to_str = sofia_overcome_sip_uri_weakness(session, invite_to_uri ? invite_to_uri : tech_pvt->dest_to, 0, SWITCH_FALSE, invite_to_params, NULL); + + switch_channel_set_variable(channel, "sip_outgoing_contact_uri", invite_contact); + + + /* + Does the "genius" who wanted SIP to be "text-based" so it was "easier to read" even use it now, + or did he just suggest it to make our lives miserable? + */ + use_from_str = from_str; + + if (!switch_stristr("sip:", use_from_str)) { + use_from_str = switch_core_session_sprintf(session, "sip:%s", use_from_str); + } + + if (!from_display && !strcasecmp(tech_pvt->caller_profile->caller_id_name, "_undef_")) { + from_str = switch_core_session_sprintf(session, "<%s>", use_from_str); + } else { + char *name = switch_core_session_strdup(session, from_display ? from_display : tech_pvt->caller_profile->caller_id_name); + check_decode(name, session); + from_str = switch_core_session_sprintf(session, "\"%s\" <%s>", name, use_from_str); + } + + if (!(call_id = switch_channel_get_variable(channel, "sip_invite_call_id"))) { + if (sofia_test_pflag(tech_pvt->profile, PFLAG_UUID_AS_CALLID)) { + call_id = switch_core_session_get_uuid(session); + } + } + + if (handle_full_from) { + from_str = (char *) handle_full_from; + } + + if (handle_full_to) { + to_str = (char *) handle_full_to; + } + + + if (force_full_from) { + from_str = (char *) force_full_from; + } + + if (force_full_to) { + to_str = (char *) force_full_to; + } + + + if (invite_req_uri) { + url_str = (char *) invite_req_uri; + } + + if (url_str) { + char *s = NULL; + if (!strncasecmp(url_str, "sip:", 4)) { + s = url_str + 4; + } + if (!strncasecmp(url_str, "sips:", 5)) { + s = url_str + 5; + } + + /* tel: patch from jaybinks, added by MC + It compiles but I don't have a way to test it + */ + if (!strncasecmp(url_str, "tel:", 4)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), + SWITCH_LOG_ERROR, "URL Error! tel: uri's not supported at this time\n"); + return SWITCH_STATUS_FALSE; + } + if (!s) { + s = url_str; + } + switch_channel_set_variable(channel, "sip_req_uri", s); + } + + switch_channel_set_variable(channel, "sip_to_host", sofia_glue_get_host(to_str, switch_core_session_get_pool(session))); + switch_channel_set_variable(channel, "sip_from_host", sofia_glue_get_host(from_str, switch_core_session_get_pool(session))); + + + if (!(tech_pvt->nh = nua_handle(tech_pvt->profile->nua, NULL, + NUTAG_URL(url_str), + TAG_IF(call_id, SIPTAG_CALL_ID_STR(call_id)), + TAG_IF(!zstr(record_route), SIPTAG_HEADER_STR(record_route)), + SIPTAG_TO_STR(to_str), SIPTAG_FROM_STR(from_str), SIPTAG_CONTACT_STR(invite_contact), TAG_END()))) { + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, + "Error creating HANDLE!\nurl_str=[%s]\ncall_id=[%s]\nto_str=[%s]\nfrom_str=[%s]\ninvite_contact=[%s]\n", + url_str, + call_id ? call_id : "N/A", + to_str, + from_str, + invite_contact); + + switch_safe_free(d_url); + return SWITCH_STATUS_FALSE; + } + + if (tech_pvt->dest && (strstr(tech_pvt->dest, ";fs_nat") || strstr(tech_pvt->dest, ";received") + || ((val = switch_channel_get_variable(channel, "sip_sticky_contact")) && switch_true(val)))) { + sofia_set_flag(tech_pvt, TFLAG_NAT); + tech_pvt->record_route = switch_core_session_strdup(tech_pvt->session, url_str); + route_uri = tech_pvt->record_route; + session_timeout = SOFIA_NAT_SESSION_TIMEOUT; + switch_channel_set_variable(channel, "sip_nat_detected", "true"); + } + + if ((val = switch_channel_get_variable(channel, "sip_cid_type"))) { + cid_type = sofia_cid_name2type(val); + } + + if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING) && switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) { + if (zstr((use_name = switch_channel_get_variable(tech_pvt->channel, "effective_callee_id_name"))) && + zstr((use_name = switch_channel_get_variable(tech_pvt->channel, "sip_callee_id_name")))) { + if (!(use_name = switch_channel_get_variable(tech_pvt->channel, "sip_to_display"))) { + use_name = switch_channel_get_variable(tech_pvt->channel, "sip_to_user"); + } + } + + if (zstr((use_number = switch_channel_get_variable(tech_pvt->channel, "effective_callee_id_number"))) && + zstr((use_number = switch_channel_get_variable(tech_pvt->channel, "sip_callee_id_number")))) { + use_number = switch_channel_get_variable(tech_pvt->channel, "sip_to_user"); + } + + if (zstr(use_name) && zstr(use_name = tech_pvt->caller_profile->callee_id_name)) { + use_name = tech_pvt->caller_profile->caller_id_name; + } + + if (zstr(use_number) && zstr(use_number = tech_pvt->caller_profile->callee_id_number)) { + use_number = tech_pvt->caller_profile->caller_id_number; + } + } else { + use_name = tech_pvt->caller_profile->caller_id_name; + use_number = tech_pvt->caller_profile->caller_id_number; + } + + check_decode(use_name, session); + + switch (cid_type) { + case CID_TYPE_PID: + if (switch_test_flag(caller_profile, SWITCH_CPF_SCREEN)) { + if (zstr(tech_pvt->caller_profile->caller_id_name) || !strcasecmp(tech_pvt->caller_profile->caller_id_name, "_undef_")) { + tech_pvt->asserted_id = switch_core_session_sprintf(tech_pvt->session, "", + use_number, rpid_domain); + } else { + tech_pvt->asserted_id = switch_core_session_sprintf(tech_pvt->session, "\"%s\"", + use_name, use_number, rpid_domain); + } + } else { + if (zstr(tech_pvt->caller_profile->caller_id_name) || !strcasecmp(tech_pvt->caller_profile->caller_id_name, "_undef_")) { + tech_pvt->preferred_id = switch_core_session_sprintf(tech_pvt->session, "", + tech_pvt->caller_profile->caller_id_number, rpid_domain); + } else { + tech_pvt->preferred_id = switch_core_session_sprintf(tech_pvt->session, "\"%s\"", + tech_pvt->caller_profile->caller_id_name, + tech_pvt->caller_profile->caller_id_number, rpid_domain); + } + } + + if (switch_test_flag(caller_profile, SWITCH_CPF_HIDE_NUMBER)) { + tech_pvt->privacy = "id"; + } else { + tech_pvt->privacy = "none"; + } + + break; + case CID_TYPE_RPID: + { + if (switch_test_flag(caller_profile, SWITCH_CPF_HIDE_NAME)) { + priv = "name"; + if (switch_test_flag(caller_profile, SWITCH_CPF_HIDE_NUMBER)) { + priv = "full"; + } + } else if (switch_test_flag(caller_profile, SWITCH_CPF_HIDE_NUMBER)) { + priv = "full"; + } + + if (switch_test_flag(caller_profile, SWITCH_CPF_SCREEN)) { + screen = "yes"; + } + + if (zstr(tech_pvt->caller_profile->caller_id_name) || !strcasecmp(tech_pvt->caller_profile->caller_id_name, "_undef_")) { + tech_pvt->rpid = switch_core_session_sprintf(tech_pvt->session, ";party=calling;screen=%s;privacy=%s", + use_number, rpid_domain, screen, priv); + } else { + tech_pvt->rpid = switch_core_session_sprintf(tech_pvt->session, "\"%s\";party=calling;screen=%s;privacy=%s", + use_name, use_number, rpid_domain, screen, priv); + } + } + break; + default: + break; + } + + + switch_safe_free(d_url); + + if (!(sofia_private = su_alloc(tech_pvt->nh->nh_home, sizeof(*sofia_private)))) { + abort(); + } + + memset(sofia_private, 0, sizeof(*sofia_private)); + sofia_private->is_call = 2; + sofia_private->is_static++; + + if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) { + sofia_private->is_call++; + } + + tech_pvt->sofia_private = sofia_private; + switch_copy_string(tech_pvt->sofia_private->uuid, switch_core_session_get_uuid(session), sizeof(tech_pvt->sofia_private->uuid)); + nua_handle_bind(tech_pvt->nh, tech_pvt->sofia_private); + } + + if (tech_pvt->e_dest && sofia_test_pflag(tech_pvt->profile, PFLAG_IN_DIALOG_CHAT)) { + char *user = NULL, *host = NULL; + char hash_key[256] = ""; + + e_dest = strdup(tech_pvt->e_dest); + switch_assert(e_dest != NULL); + user = e_dest; + + if ((host = strchr(user, '@'))) { + *host++ = '\0'; + } + switch_snprintf(hash_key, sizeof(hash_key), "%s%s%s", user, host, cid_num); + + tech_pvt->chat_from = tech_pvt->from_str; + tech_pvt->chat_to = tech_pvt->dest; + if (tech_pvt->profile->pres_type) { + tech_pvt->hash_key = switch_core_session_strdup(tech_pvt->session, hash_key); + switch_mutex_lock(tech_pvt->profile->flag_mutex); + switch_core_hash_insert(tech_pvt->profile->chat_hash, tech_pvt->hash_key, tech_pvt); + switch_mutex_unlock(tech_pvt->profile->flag_mutex); + } + free(e_dest); + } + + holdstr = sofia_test_flag(tech_pvt, TFLAG_SIP_HOLD) ? "*" : ""; + + if (!switch_channel_get_variable(channel, "sofia_profile_name")) { + switch_channel_set_variable(channel, "sofia_profile_name", tech_pvt->profile->name); + switch_channel_set_variable(channel, "recovery_profile_name", tech_pvt->profile->name); + } + + extra_headers = sofia_glue_get_extra_headers(channel, SOFIA_SIP_HEADER_PREFIX); + + session_timeout = tech_pvt->profile->session_timeout; + + if ((val = switch_channel_get_variable(channel, SOFIA_SESSION_TIMEOUT))) { + int v_session_timeout = atoi(val); + if (v_session_timeout >= 0) { + session_timeout = v_session_timeout; + } + } + + if (switch_channel_test_flag(channel, CF_PROXY_MEDIA)) { + if (switch_rtp_ready(tech_pvt->rtp_session)) { + sofia_glue_tech_proxy_remote_addr(tech_pvt, NULL); + } + sofia_glue_tech_patch_sdp(tech_pvt); + } + + if (!zstr(tech_pvt->dest)) { + dst = sofia_glue_get_destination(tech_pvt->dest); + + if (dst->route_uri) { + route_uri = sofia_overcome_sip_uri_weakness(tech_pvt->session, dst->route_uri, tech_pvt->transport, SWITCH_TRUE, NULL, NULL); + } + + if (dst->route) { + route = dst->route; + } + } + + if ((val = switch_channel_get_variable(channel, "sip_route_uri"))) { + route_uri = switch_core_session_strdup(session, val); + route = NULL; + } + + if (route_uri) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "%s Setting proxy route to %s\n", route_uri, + switch_channel_get_name(channel)); + tech_pvt->route_uri = switch_core_session_strdup(tech_pvt->session, route_uri); + } + + + if ((val = switch_channel_get_variable(tech_pvt->channel, "sip_invite_cseq"))) { + uint32_t callsequence = (uint32_t) strtoul(val, NULL, 10); + cseq = sip_cseq_create(tech_pvt->nh->nh_home, callsequence, SIP_METHOD_INVITE); + } + + + switch_channel_clear_flag(channel, CF_MEDIA_ACK); + + if (handle_full_from) { + tech_pvt->nh->nh_has_invite = 1; + } + + if ((mp = sofia_glue_get_multipart(session, SOFIA_MULTIPART_PREFIX, tech_pvt->local_sdp_str, &mp_type))) { + sofia_clear_flag(tech_pvt, TFLAG_ENABLE_SOA); + } + + if ((tech_pvt->session_timeout = session_timeout)) { + tech_pvt->session_refresher = switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? nua_local_refresher : nua_remote_refresher; + } else { + tech_pvt->session_refresher = nua_no_refresher; + } + + if (tech_pvt->local_sdp_str) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "Local SDP:\n%s\n", tech_pvt->local_sdp_str); + } + + if (sofia_use_soa(tech_pvt)) { + nua_invite(tech_pvt->nh, + NUTAG_AUTOANSWER(0), + //TAG_IF(zstr(tech_pvt->local_sdp_str), NUTAG_AUTOACK(0)), + //TAG_IF(!zstr(tech_pvt->local_sdp_str), NUTAG_AUTOACK(1)), + // The code above is breaking things...... grrr WE need this because we handle our own acks and there are 3pcc cases in there too + NUTAG_AUTOACK(0), + NUTAG_SESSION_TIMER(tech_pvt->session_timeout), + NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(sofia_test_flag(tech_pvt, TFLAG_RECOVERED), NUTAG_INVITE_TIMER(UINT_MAX)), + TAG_IF(invite_full_from, SIPTAG_FROM_STR(invite_full_from)), + TAG_IF(invite_full_to, SIPTAG_TO_STR(invite_full_to)), + TAG_IF(tech_pvt->redirected, NUTAG_URL(tech_pvt->redirected)), + TAG_IF(!zstr(recover_via), SIPTAG_VIA_STR(recover_via)), + TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), + TAG_IF(!zstr(tech_pvt->rpid), SIPTAG_REMOTE_PARTY_ID_STR(tech_pvt->rpid)), + TAG_IF(!zstr(tech_pvt->preferred_id), SIPTAG_P_PREFERRED_IDENTITY_STR(tech_pvt->preferred_id)), + TAG_IF(!zstr(tech_pvt->asserted_id), SIPTAG_P_ASSERTED_IDENTITY_STR(tech_pvt->asserted_id)), + TAG_IF(!zstr(tech_pvt->privacy), SIPTAG_PRIVACY_STR(tech_pvt->privacy)), + TAG_IF(!zstr(alert_info), SIPTAG_HEADER_STR(alert_info)), + TAG_IF(!zstr(extra_headers), SIPTAG_HEADER_STR(extra_headers)), + TAG_IF(sofia_test_pflag(tech_pvt->profile, PFLAG_PASS_CALLEE_ID), SIPTAG_HEADER_STR("X-FS-Support: " FREESWITCH_SUPPORT)), + TAG_IF(!zstr(max_forwards), SIPTAG_MAX_FORWARDS_STR(max_forwards)), + TAG_IF(!zstr(route_uri), NUTAG_PROXY(route_uri)), + TAG_IF(!zstr(invite_route_uri), NUTAG_INITIAL_ROUTE_STR(invite_route_uri)), + TAG_IF(!zstr(route), SIPTAG_ROUTE_STR(route)), + TAG_IF(tech_pvt->profile->minimum_session_expires, NUTAG_MIN_SE(tech_pvt->profile->minimum_session_expires)), + TAG_IF(cseq, SIPTAG_CSEQ(cseq)), + TAG_IF(zstr(tech_pvt->local_sdp_str), SIPTAG_PAYLOAD_STR("")), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_ADDRESS(tech_pvt->adv_sdp_audio_ip)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_USER_SDP_STR(tech_pvt->local_sdp_str)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_REUSE_REJECTED(1)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_ORDERED_USER(1)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_RTP_SORT(SOA_RTP_SORT_REMOTE)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_RTP_SELECT(SOA_RTP_SELECT_ALL)), + TAG_IF(rep, SIPTAG_REPLACES_STR(rep)), + TAG_IF(!require_timer, NUTAG_TIMER_AUTOREQUIRE(0)), + TAG_IF(!zstr(tech_pvt->local_sdp_str), SOATAG_HOLD(holdstr)), TAG_END()); + } else { + nua_invite(tech_pvt->nh, + NUTAG_AUTOANSWER(0), + NUTAG_AUTOACK(0), + NUTAG_SESSION_TIMER(tech_pvt->session_timeout), + NUTAG_SESSION_REFRESHER(tech_pvt->session_refresher), + TAG_IF(sofia_test_flag(tech_pvt, TFLAG_RECOVERED), NUTAG_INVITE_TIMER(UINT_MAX)), + TAG_IF(invite_full_from, SIPTAG_FROM_STR(invite_full_from)), + TAG_IF(invite_full_to, SIPTAG_TO_STR(invite_full_to)), + TAG_IF(tech_pvt->redirected, NUTAG_URL(tech_pvt->redirected)), + TAG_IF(!zstr(recover_via), SIPTAG_VIA_STR(recover_via)), + TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), + TAG_IF(!zstr(tech_pvt->rpid), SIPTAG_REMOTE_PARTY_ID_STR(tech_pvt->rpid)), + TAG_IF(!zstr(tech_pvt->preferred_id), SIPTAG_P_PREFERRED_IDENTITY_STR(tech_pvt->preferred_id)), + TAG_IF(!zstr(tech_pvt->asserted_id), SIPTAG_P_ASSERTED_IDENTITY_STR(tech_pvt->asserted_id)), + TAG_IF(!zstr(tech_pvt->privacy), SIPTAG_PRIVACY_STR(tech_pvt->privacy)), + TAG_IF(!zstr(alert_info), SIPTAG_HEADER_STR(alert_info)), + TAG_IF(!zstr(extra_headers), SIPTAG_HEADER_STR(extra_headers)), + TAG_IF(sofia_test_pflag(tech_pvt->profile, PFLAG_PASS_CALLEE_ID), SIPTAG_HEADER_STR("X-FS-Support: " FREESWITCH_SUPPORT)), + TAG_IF(!zstr(max_forwards), SIPTAG_MAX_FORWARDS_STR(max_forwards)), + TAG_IF(!zstr(route_uri), NUTAG_PROXY(route_uri)), + TAG_IF(!zstr(route), SIPTAG_ROUTE_STR(route)), + TAG_IF(!zstr(invite_route_uri), NUTAG_INITIAL_ROUTE_STR(invite_route_uri)), + TAG_IF(tech_pvt->profile->minimum_session_expires, NUTAG_MIN_SE(tech_pvt->profile->minimum_session_expires)), + TAG_IF(!require_timer, NUTAG_TIMER_AUTOREQUIRE(0)), + TAG_IF(cseq, SIPTAG_CSEQ(cseq)), + NUTAG_MEDIA_ENABLE(0), + SIPTAG_CONTENT_TYPE_STR(mp_type ? mp_type : "application/sdp"), + SIPTAG_PAYLOAD_STR(mp ? mp : tech_pvt->local_sdp_str), TAG_IF(rep, SIPTAG_REPLACES_STR(rep)), SOATAG_HOLD(holdstr), TAG_END()); + } + + sofia_glue_free_destination(dst); + switch_safe_free(extra_headers); + switch_safe_free(mp); + tech_pvt->redirected = NULL; + + return SWITCH_STATUS_SUCCESS; +} + +void sofia_glue_do_xfer_invite(switch_core_session_t *session) +{ + private_object_t *tech_pvt = switch_core_session_get_private(session); + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_caller_profile_t *caller_profile; + const char *sipip, *format, *contact_url; + + switch_assert(tech_pvt != NULL); + switch_mutex_lock(tech_pvt->sofia_mutex); + caller_profile = switch_channel_get_caller_profile(channel); + + if (!zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + sipip = tech_pvt->profile->extsipip; + contact_url = tech_pvt->profile->public_url; + } else { + sipip = tech_pvt->profile->extsipip ? tech_pvt->profile->extsipip : tech_pvt->profile->sipip; + contact_url = tech_pvt->profile->url; + } + + format = strchr(sipip, ':') ? "\"%s\" " : "\"%s\" "; + + if ((tech_pvt->from_str = switch_core_session_sprintf(session, format, caller_profile->caller_id_name, caller_profile->caller_id_number, sipip))) { + + const char *rep = switch_channel_get_variable(channel, SOFIA_REPLACES_HEADER); + + tech_pvt->nh2 = nua_handle(tech_pvt->profile->nua, NULL, + SIPTAG_TO_STR(tech_pvt->dest), SIPTAG_FROM_STR(tech_pvt->from_str), SIPTAG_CONTACT_STR(contact_url), TAG_END()); + + nua_handle_bind(tech_pvt->nh2, tech_pvt->sofia_private); + + nua_invite(tech_pvt->nh2, + SIPTAG_CONTACT_STR(contact_url), + TAG_IF(!zstr(tech_pvt->user_via), SIPTAG_VIA_STR(tech_pvt->user_via)), + SOATAG_ADDRESS(tech_pvt->adv_sdp_audio_ip), + SOATAG_USER_SDP_STR(tech_pvt->local_sdp_str), + SOATAG_REUSE_REJECTED(1), + SOATAG_ORDERED_USER(1), + SOATAG_RTP_SORT(SOA_RTP_SORT_REMOTE), SOATAG_RTP_SELECT(SOA_RTP_SELECT_ALL), TAG_IF(rep, SIPTAG_REPLACES_STR(rep)), TAG_END()); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Memory Error!\n"); + } + switch_mutex_unlock(tech_pvt->sofia_mutex); +} + +void sofia_glue_tech_absorb_sdp(private_object_t *tech_pvt) +{ + const char *sdp_str; + + if ((sdp_str = switch_channel_get_variable(tech_pvt->channel, SWITCH_B_SDP_VARIABLE))) { + sdp_parser_t *parser; + sdp_session_t *sdp; + sdp_media_t *m; + sdp_connection_t *connection; + + if ((parser = sdp_parse(NULL, sdp_str, (int) strlen(sdp_str), 0))) { + if ((sdp = sdp_session(parser))) { + for (m = sdp->sdp_media; m; m = m->m_next) { + if (m->m_type != sdp_media_audio || !m->m_port) { + continue; + } + + connection = sdp->sdp_connection; + if (m->m_connections) { + connection = m->m_connections; + } + + if (connection) { + tech_pvt->proxy_sdp_audio_ip = switch_core_session_strdup(tech_pvt->session, connection->c_address); + } + tech_pvt->proxy_sdp_audio_port = (switch_port_t) m->m_port; + if (tech_pvt->proxy_sdp_audio_ip && tech_pvt->proxy_sdp_audio_port) { + break; + } + } + } + sdp_parser_free(parser); + } + sofia_glue_tech_set_local_sdp(tech_pvt, sdp_str, SWITCH_TRUE); + } +} + + +#define add_stat(_i, _s) \ + switch_snprintf(var_name, sizeof(var_name), "rtp_%s_%s", switch_str_nil(prefix), _s) ; \ + switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_SIZE_T_FMT, _i); \ + switch_channel_set_variable(tech_pvt->channel, var_name, var_val) + +static void set_stats(switch_rtp_t *rtp_session, private_object_t *tech_pvt, const char *prefix) +{ + switch_rtp_stats_t *stats = switch_rtp_get_stats(rtp_session, NULL); + char var_name[256] = "", var_val[35] = ""; + + if (stats) { + + add_stat(stats->inbound.raw_bytes, "in_raw_bytes"); + add_stat(stats->inbound.media_bytes, "in_media_bytes"); + add_stat(stats->inbound.packet_count, "in_packet_count"); + add_stat(stats->inbound.media_packet_count, "in_media_packet_count"); + add_stat(stats->inbound.skip_packet_count, "in_skip_packet_count"); + add_stat(stats->inbound.jb_packet_count, "in_jb_packet_count"); + add_stat(stats->inbound.dtmf_packet_count, "in_dtmf_packet_count"); + add_stat(stats->inbound.cng_packet_count, "in_cng_packet_count"); + add_stat(stats->inbound.flush_packet_count, "in_flush_packet_count"); + add_stat(stats->inbound.largest_jb_size, "in_largest_jb_size"); + + add_stat(stats->outbound.raw_bytes, "out_raw_bytes"); + add_stat(stats->outbound.media_bytes, "out_media_bytes"); + add_stat(stats->outbound.packet_count, "out_packet_count"); + add_stat(stats->outbound.media_packet_count, "out_media_packet_count"); + add_stat(stats->outbound.skip_packet_count, "out_skip_packet_count"); + add_stat(stats->outbound.dtmf_packet_count, "out_dtmf_packet_count"); + add_stat(stats->outbound.cng_packet_count, "out_cng_packet_count"); + + add_stat(stats->rtcp.packet_count, "rtcp_packet_count"); + add_stat(stats->rtcp.octet_count, "rtcp_octet_count"); + + } +} + +void sofia_glue_set_rtp_stats(private_object_t *tech_pvt) +{ + if (tech_pvt->rtp_session) { + set_stats(tech_pvt->rtp_session, tech_pvt, "audio"); + } + + if (tech_pvt->video_rtp_session) { + set_stats(tech_pvt->video_rtp_session, tech_pvt, "video"); + } +} + +void sofia_glue_deactivate_rtp(private_object_t *tech_pvt) +{ + int loops = 0; + if (switch_rtp_ready(tech_pvt->rtp_session)) { + while (loops < 10 && (sofia_test_flag(tech_pvt, TFLAG_READING) || sofia_test_flag(tech_pvt, TFLAG_WRITING))) { + switch_yield(10000); + loops++; + } + } + + if (tech_pvt->video_rtp_session) { + switch_rtp_destroy(&tech_pvt->video_rtp_session); + } else if (tech_pvt->local_sdp_video_port) { + switch_rtp_release_port(tech_pvt->rtpip, tech_pvt->local_sdp_video_port); + } + + + if (tech_pvt->local_sdp_video_port > 0 && !zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + switch_nat_del_mapping((switch_port_t) tech_pvt->local_sdp_video_port, SWITCH_NAT_UDP); + switch_nat_del_mapping((switch_port_t) tech_pvt->local_sdp_video_port + 1, SWITCH_NAT_UDP); + } + + + if (tech_pvt->rtp_session) { + switch_rtp_destroy(&tech_pvt->rtp_session); + } else if (tech_pvt->local_sdp_audio_port) { + switch_rtp_release_port(tech_pvt->rtpip, tech_pvt->local_sdp_audio_port); + } + + if (tech_pvt->local_sdp_audio_port > 0 && !zstr(tech_pvt->remote_ip) && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) { + switch_nat_del_mapping((switch_port_t) tech_pvt->local_sdp_audio_port, SWITCH_NAT_UDP); + switch_nat_del_mapping((switch_port_t) tech_pvt->local_sdp_audio_port + 1, SWITCH_NAT_UDP); + } + +} + +switch_status_t sofia_glue_tech_set_video_codec(private_object_t *tech_pvt, int force) +{ + + if (!tech_pvt->video_rm_encoding) { + return SWITCH_STATUS_FALSE; + } + + if (tech_pvt->video_read_codec.implementation && switch_core_codec_ready(&tech_pvt->video_read_codec)) { + if (!force) { + return SWITCH_STATUS_SUCCESS; + } + if (strcasecmp(tech_pvt->video_read_codec.implementation->iananame, tech_pvt->video_rm_encoding) || + tech_pvt->video_read_codec.implementation->samples_per_second != tech_pvt->video_rm_rate) { + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Changing Codec from %s to %s\n", + tech_pvt->video_read_codec.implementation->iananame, tech_pvt->video_rm_encoding); + switch_core_codec_destroy(&tech_pvt->video_read_codec); + switch_core_codec_destroy(&tech_pvt->video_write_codec); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Already using %s\n", + tech_pvt->video_read_codec.implementation->iananame); + return SWITCH_STATUS_SUCCESS; + } + } + + + + if (switch_core_codec_init(&tech_pvt->video_read_codec, + tech_pvt->video_rm_encoding, + tech_pvt->video_rm_fmtp, + tech_pvt->video_rm_rate, + 0, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); + return SWITCH_STATUS_FALSE; + } else { + if (switch_core_codec_init(&tech_pvt->video_write_codec, + tech_pvt->video_rm_encoding, + tech_pvt->video_rm_fmtp, + tech_pvt->video_rm_rate, + 0, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); + return SWITCH_STATUS_FALSE; + } else { + tech_pvt->video_read_frame.rate = tech_pvt->video_rm_rate; + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set VIDEO Codec %s %s/%ld %d ms\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->video_rm_encoding, tech_pvt->video_rm_rate, tech_pvt->video_codec_ms); + tech_pvt->video_read_frame.codec = &tech_pvt->video_read_codec; + + tech_pvt->video_fmtp_out = switch_core_session_strdup(tech_pvt->session, tech_pvt->video_write_codec.fmtp_out); + + tech_pvt->video_write_codec.agreed_pt = tech_pvt->video_agreed_pt; + tech_pvt->video_read_codec.agreed_pt = tech_pvt->video_agreed_pt; + switch_core_session_set_video_read_codec(tech_pvt->session, &tech_pvt->video_read_codec); + switch_core_session_set_video_write_codec(tech_pvt->session, &tech_pvt->video_write_codec); + + + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + switch_core_session_message_t msg = { 0 }; + + msg.from = __FILE__; + msg.message_id = SWITCH_MESSAGE_INDICATE_VIDEO_REFRESH_REQ; + + switch_rtp_set_default_payload(tech_pvt->video_rtp_session, tech_pvt->video_agreed_pt); + + if (tech_pvt->video_recv_pt != tech_pvt->video_agreed_pt) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set video receive payload to %u\n", switch_channel_get_name(tech_pvt->channel), tech_pvt->video_recv_pt); + + switch_rtp_set_recv_pt(tech_pvt->video_rtp_session, tech_pvt->video_recv_pt); + } else { + switch_rtp_set_recv_pt(tech_pvt->video_rtp_session, tech_pvt->video_agreed_pt); + } + + switch_core_session_receive_message(tech_pvt->session, &msg); + + + } + + switch_channel_set_variable(tech_pvt->channel, "sip_use_video_codec_name", tech_pvt->video_rm_encoding); + switch_channel_set_variable(tech_pvt->channel, "sip_use_video_codec_fmtp", tech_pvt->video_rm_fmtp); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_video_codec_rate", "%d", tech_pvt->video_rm_rate); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_video_codec_ptime", "%d", 0); + + } + } + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t sofia_glue_tech_set_codec(private_object_t *tech_pvt, int force) +{ + switch_status_t status = SWITCH_STATUS_SUCCESS; + int resetting = 0; + + if (!tech_pvt->iananame) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "No audio codec available\n"); + switch_goto_status(SWITCH_STATUS_FALSE, end); + } + + if (switch_core_codec_ready(&tech_pvt->read_codec)) { + if (!force) { + switch_goto_status(SWITCH_STATUS_SUCCESS, end); + } + if (strcasecmp(tech_pvt->read_impl.iananame, tech_pvt->iananame) || + tech_pvt->read_impl.samples_per_second != tech_pvt->rm_rate || + tech_pvt->codec_ms != (uint32_t) tech_pvt->read_impl.microseconds_per_packet / 1000) { + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "Changing Codec from %s@%dms@%dhz to %s@%dms@%luhz\n", + tech_pvt->read_impl.iananame, tech_pvt->read_impl.microseconds_per_packet / 1000, + tech_pvt->read_impl.samples_per_second, + tech_pvt->rm_encoding, + tech_pvt->codec_ms, + tech_pvt->rm_rate); + + switch_yield(tech_pvt->read_impl.microseconds_per_packet); + switch_core_session_lock_codec_write(tech_pvt->session); + switch_core_session_lock_codec_read(tech_pvt->session); + resetting = 1; + switch_yield(tech_pvt->read_impl.microseconds_per_packet); + switch_core_codec_destroy(&tech_pvt->read_codec); + switch_core_codec_destroy(&tech_pvt->write_codec); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Already using %s\n", tech_pvt->read_impl.iananame); + switch_goto_status(SWITCH_STATUS_SUCCESS, end); + } + } + + if (switch_core_codec_init_with_bitrate(&tech_pvt->read_codec, + tech_pvt->iananame, + tech_pvt->rm_fmtp, + tech_pvt->rm_rate, + tech_pvt->codec_ms, + 1, + tech_pvt->bitrate, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE | tech_pvt->profile->codec_flags, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); + switch_goto_status(SWITCH_STATUS_FALSE, end); + } + + tech_pvt->read_codec.session = tech_pvt->session; + + + if (switch_core_codec_init_with_bitrate(&tech_pvt->write_codec, + tech_pvt->iananame, + tech_pvt->rm_fmtp, + tech_pvt->rm_rate, + tech_pvt->codec_ms, + 1, + tech_pvt->bitrate, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE | tech_pvt->profile->codec_flags, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); + switch_goto_status(SWITCH_STATUS_FALSE, end); + } + + tech_pvt->write_codec.session = tech_pvt->session; + + switch_channel_set_variable(tech_pvt->channel, "sip_use_codec_name", tech_pvt->iananame); + switch_channel_set_variable(tech_pvt->channel, "sip_use_codec_fmtp", tech_pvt->rm_fmtp); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_codec_rate", "%d", tech_pvt->rm_rate); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_codec_ptime", "%d", tech_pvt->codec_ms); + + + switch_assert(tech_pvt->read_codec.implementation); + switch_assert(tech_pvt->write_codec.implementation); + + tech_pvt->read_impl = *tech_pvt->read_codec.implementation; + tech_pvt->write_impl = *tech_pvt->write_codec.implementation; + + switch_core_session_set_read_impl(tech_pvt->session, tech_pvt->read_codec.implementation); + switch_core_session_set_write_impl(tech_pvt->session, tech_pvt->write_codec.implementation); + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + switch_assert(tech_pvt->read_codec.implementation); + + if (switch_rtp_change_interval(tech_pvt->rtp_session, + tech_pvt->read_impl.microseconds_per_packet, + tech_pvt->read_impl.samples_per_packet) != SWITCH_STATUS_SUCCESS) { + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + switch_goto_status(SWITCH_STATUS_FALSE, end); + } + } + + tech_pvt->read_frame.rate = tech_pvt->rm_rate; + + if (!switch_core_codec_ready(&tech_pvt->read_codec)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); + switch_goto_status(SWITCH_STATUS_FALSE, end); + } + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set Codec %s %s/%ld %d ms %d samples %d bits\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->iananame, tech_pvt->rm_rate, tech_pvt->codec_ms, + tech_pvt->read_impl.samples_per_packet, tech_pvt->read_impl.bits_per_second); + tech_pvt->read_frame.codec = &tech_pvt->read_codec; + + tech_pvt->write_codec.agreed_pt = tech_pvt->agreed_pt; + tech_pvt->read_codec.agreed_pt = tech_pvt->agreed_pt; + + if (force != 2) { + switch_core_session_set_real_read_codec(tech_pvt->session, &tech_pvt->read_codec); + switch_core_session_set_write_codec(tech_pvt->session, &tech_pvt->write_codec); + } + + tech_pvt->fmtp_out = switch_core_session_strdup(tech_pvt->session, tech_pvt->write_codec.fmtp_out); + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->rtp_session, tech_pvt->pt); + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->agreed_pt); + } + + end: + if (resetting) { + switch_core_session_unlock_codec_write(tech_pvt->session); + switch_core_session_unlock_codec_read(tech_pvt->session); + } + + sofia_glue_tech_set_video_codec(tech_pvt, force); + + return status; +} + + +switch_status_t sofia_glue_build_crypto(private_object_t *tech_pvt, int index, switch_rtp_crypto_key_type_t type, switch_rtp_crypto_direction_t direction) +{ + unsigned char b64_key[512] = ""; + const char *type_str; + unsigned char *key; + const char *val; + + char *p; + + if (type == AES_CM_128_HMAC_SHA1_80) { + type_str = SWITCH_RTP_CRYPTO_KEY_80; + } else { + type_str = SWITCH_RTP_CRYPTO_KEY_32; + } + + if (direction == SWITCH_RTP_CRYPTO_SEND) { + key = tech_pvt->local_raw_key; + } else { + key = tech_pvt->remote_raw_key; + + } + + switch_rtp_get_random(key, SWITCH_RTP_KEY_LEN); + switch_b64_encode(key, SWITCH_RTP_KEY_LEN, b64_key, sizeof(b64_key)); + p = strrchr((char *) b64_key, '='); + + while (p && *p && *p == '=') { + *p-- = '\0'; + } + + tech_pvt->local_crypto_key = switch_core_session_sprintf(tech_pvt->session, "%d %s inline:%s", index, type_str, b64_key); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set Local Key [%s]\n", tech_pvt->local_crypto_key); + + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_SRTP_AUTH) && + !((val = switch_channel_get_variable(tech_pvt->channel, "NDLB_support_asterisk_missing_srtp_auth")) && switch_true(val))) { + tech_pvt->crypto_type = type; + } else { + tech_pvt->crypto_type = AES_CM_128_NULL_AUTH; + } + + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t sofia_glue_add_crypto(private_object_t *tech_pvt, const char *key_str, switch_rtp_crypto_direction_t direction) +{ + unsigned char key[SWITCH_RTP_MAX_CRYPTO_LEN]; + switch_rtp_crypto_key_type_t type; + char *p; + + + if (!switch_rtp_ready(tech_pvt->rtp_session)) { + goto bad; + } + + p = strchr(key_str, ' '); + + if (p && *p && *(p + 1)) { + p++; + if (!strncasecmp(p, SWITCH_RTP_CRYPTO_KEY_32, strlen(SWITCH_RTP_CRYPTO_KEY_32))) { + type = AES_CM_128_HMAC_SHA1_32; + } else if (!strncasecmp(p, SWITCH_RTP_CRYPTO_KEY_80, strlen(SWITCH_RTP_CRYPTO_KEY_80))) { + type = AES_CM_128_HMAC_SHA1_80; + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Parse Error near [%s]\n", p); + goto bad; + } + + p = strchr(p, ' '); + if (p && *p && *(p + 1)) { + p++; + if (strncasecmp(p, "inline:", 7)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Parse Error near [%s]\n", p); + goto bad; + } + + p += 7; + switch_b64_decode(p, (char *) key, sizeof(key)); + + if (direction == SWITCH_RTP_CRYPTO_SEND) { + tech_pvt->crypto_send_type = type; + memcpy(tech_pvt->local_raw_key, key, SWITCH_RTP_KEY_LEN); + } else { + tech_pvt->crypto_recv_type = type; + memcpy(tech_pvt->remote_raw_key, key, SWITCH_RTP_KEY_LEN); + } + return SWITCH_STATUS_SUCCESS; + } + + } + + bad: + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "Error!\n"); + return SWITCH_STATUS_FALSE; + +} + + +switch_status_t sofia_glue_activate_rtp(private_object_t *tech_pvt, switch_rtp_flag_t myflags) +{ + const char *err = NULL; + const char *val = NULL; + switch_rtp_flag_t flags; + switch_status_t status = SWITCH_STATUS_SUCCESS; + char tmp[50]; + uint32_t rtp_timeout_sec = tech_pvt->profile->rtp_timeout_sec; + uint32_t rtp_hold_timeout_sec = tech_pvt->profile->rtp_hold_timeout_sec; + char *timer_name = NULL; + const char *var; + uint32_t delay = tech_pvt->profile->rtp_digit_delay; + + switch_assert(tech_pvt != NULL); + + if (switch_channel_down(tech_pvt->channel) || sofia_test_flag(tech_pvt, TFLAG_BYE)) { + return SWITCH_STATUS_FALSE; + } + + switch_mutex_lock(tech_pvt->sofia_mutex); + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + switch_rtp_reset_media_timer(tech_pvt->rtp_session); + } + + if ((var = switch_channel_get_variable(tech_pvt->channel, SOFIA_SECURE_MEDIA_VARIABLE)) && switch_true(var)) { + sofia_set_flag_locked(tech_pvt, TFLAG_SECURE); + } + + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE)) { + status = SWITCH_STATUS_SUCCESS; + goto end; + } + + + if (!sofia_test_flag(tech_pvt, TFLAG_REINVITE)) { + if (switch_rtp_ready(tech_pvt->rtp_session)) { + if (sofia_test_flag(tech_pvt, TFLAG_VIDEO) && !switch_rtp_ready(tech_pvt->video_rtp_session)) { + goto video; + } + + status = SWITCH_STATUS_SUCCESS; + goto end; + } + } + + if ((status = sofia_glue_tech_set_codec(tech_pvt, 0)) != SWITCH_STATUS_SUCCESS) { + goto end; + } + + + if (myflags) { + flags = myflags; + } else if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_AUTOADJ | SWITCH_RTP_FLAG_DATAWAIT); + } else { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_DATAWAIT); + } + + if (sofia_test_pflag(tech_pvt->profile, PFLAG_PASS_RFC2833) + || ((val = switch_channel_get_variable(tech_pvt->channel, "pass_rfc2833")) && switch_true(val))) { + sofia_set_flag(tech_pvt, TFLAG_PASS_RFC2833); + } + + + if (sofia_test_pflag(tech_pvt->profile, PFLAG_AUTOFLUSH) + || ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_autoflush")) && switch_true(val))) { + flags |= SWITCH_RTP_FLAG_AUTOFLUSH; + } + + if (!(sofia_test_pflag(tech_pvt->profile, PFLAG_REWRITE_TIMESTAMPS) || + ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_rewrite_timestamps")) && switch_true(val)))) { + flags |= SWITCH_RTP_FLAG_RAW_WRITE; + } + + if (sofia_test_pflag(tech_pvt->profile, PFLAG_SUPPRESS_CNG)) { + tech_pvt->cng_pt = 0; + } else if (tech_pvt->cng_pt) { + flags |= SWITCH_RTP_FLAG_AUTO_CNG; + } + +#if __BYTE_ORDER == __LITTLE_ENDIAN + if (!strcasecmp(tech_pvt->read_impl.iananame, "L16")) { + flags |= SWITCH_RTP_FLAG_BYTESWAP; + } +#endif + + if ((flags & SWITCH_RTP_FLAG_BYTESWAP) && (val = switch_channel_get_variable(tech_pvt->channel, "rtp_disable_byteswap")) && switch_true(val)) { + flags &= ~SWITCH_RTP_FLAG_BYTESWAP; + } + + if (tech_pvt->rtp_session && sofia_test_flag(tech_pvt, TFLAG_REINVITE)) { + //const char *ip = switch_channel_get_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE); + //const char *port = switch_channel_get_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE); + char *remote_host = switch_rtp_get_remote_host(tech_pvt->rtp_session); + switch_port_t remote_port = switch_rtp_get_remote_port(tech_pvt->rtp_session); + + if (remote_host && remote_port && !strcmp(remote_host, tech_pvt->remote_sdp_audio_ip) && remote_port == tech_pvt->remote_sdp_audio_port) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Audio params are unchanged for %s.\n", + switch_channel_get_name(tech_pvt->channel)); + if (switch_rtp_ready(tech_pvt->rtp_session)) { + if (tech_pvt->audio_recv_pt != tech_pvt->agreed_pt) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set audio receive payload in Re-INVITE for non-matching dynamic PT to %u\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->audio_recv_pt); + + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->audio_recv_pt); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Setting audio receive payload in Re-INVITE to %u\n", + switch_channel_get_name(tech_pvt->channel), tech_pvt->audio_recv_pt); + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->agreed_pt); + } + + } + goto video; + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Audio params changed for %s from %s:%d to %s:%d\n", + switch_channel_get_name(tech_pvt->channel), + remote_host, remote_port, tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port); + + switch_snprintf(tmp, sizeof(tmp), "%d", tech_pvt->remote_sdp_audio_port); + switch_channel_set_variable(tech_pvt->channel, SWITCH_REMOTE_MEDIA_IP_VARIABLE, tech_pvt->remote_sdp_audio_ip); + switch_channel_set_variable(tech_pvt->channel, SWITCH_REMOTE_MEDIA_PORT_VARIABLE, tmp); + } + } + + if (!switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "AUDIO RTP [%s] %s port %d -> %s port %d codec: %u ms: %d\n", + switch_channel_get_name(tech_pvt->channel), + tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_audio_port, + tech_pvt->remote_sdp_audio_ip, + tech_pvt->remote_sdp_audio_port, tech_pvt->agreed_pt, tech_pvt->read_impl.microseconds_per_packet / 1000); + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->rtp_session, tech_pvt->agreed_pt); + + if (tech_pvt->audio_recv_pt != tech_pvt->agreed_pt) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set audio receive payload to %u\n", switch_channel_get_name(tech_pvt->channel), tech_pvt->audio_recv_pt); + + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->audio_recv_pt); + } else { + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->agreed_pt); + } + + } + } + + switch_snprintf(tmp, sizeof(tmp), "%d", tech_pvt->local_sdp_audio_port); + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE, tech_pvt->local_sdp_audio_ip); + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE, tmp); + switch_channel_set_variable(tech_pvt->channel, SWITCH_ADVERTISED_MEDIA_IP_VARIABLE, tech_pvt->adv_sdp_audio_ip); + + if (tech_pvt->rtp_session && sofia_test_flag(tech_pvt, TFLAG_REINVITE)) { + const char *rport = NULL; + switch_port_t remote_rtcp_port = 0; + + + + if ((rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_audio_rtcp_port"))) { + remote_rtcp_port = (switch_port_t)atoi(rport); + } + + if (switch_rtp_set_remote_address(tech_pvt->rtp_session, tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port, + remote_rtcp_port, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "AUDIO RTP REPORTS ERROR: [%s]\n", err); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "AUDIO RTP CHANGING DEST TO: [%s:%d]\n", + tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port); + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + /* Reactivate the NAT buster flag. */ + switch_rtp_set_flag(tech_pvt->rtp_session, SWITCH_RTP_FLAG_AUTOADJ); + } + } + goto video; + } + + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) { + sofia_glue_tech_proxy_remote_addr(tech_pvt, NULL); + + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_PROXY_MEDIA | SWITCH_RTP_FLAG_AUTOADJ | SWITCH_RTP_FLAG_DATAWAIT); + } else { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_PROXY_MEDIA | SWITCH_RTP_FLAG_DATAWAIT); + } + timer_name = NULL; + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "PROXY AUDIO RTP [%s] %s:%d->%s:%d codec: %u ms: %d\n", + switch_channel_get_name(tech_pvt->channel), + tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_audio_port, + tech_pvt->remote_sdp_audio_ip, + tech_pvt->remote_sdp_audio_port, tech_pvt->agreed_pt, tech_pvt->read_impl.microseconds_per_packet / 1000); + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->rtp_session, tech_pvt->agreed_pt); + } + + } else { + timer_name = tech_pvt->profile->timer_name; + + if ((var = switch_channel_get_variable(tech_pvt->channel, "rtp_timer_name"))) { + timer_name = (char *) var; + } + } + + if (switch_channel_up(tech_pvt->channel) && !sofia_test_flag(tech_pvt, TFLAG_BYE)) { + tech_pvt->rtp_session = switch_rtp_new(tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_audio_port, + tech_pvt->remote_sdp_audio_ip, + tech_pvt->remote_sdp_audio_port, + tech_pvt->agreed_pt, + tech_pvt->read_impl.samples_per_packet, + tech_pvt->codec_ms * 1000, + (switch_rtp_flag_t) flags, timer_name, &err, switch_core_session_get_pool(tech_pvt->session)); + } + + if (switch_rtp_ready(tech_pvt->rtp_session)) { + uint8_t vad_in = sofia_test_flag(tech_pvt, TFLAG_VAD_IN) ? 1 : 0; + uint8_t vad_out = sofia_test_flag(tech_pvt, TFLAG_VAD_OUT) ? 1 : 0; + uint8_t inb = sofia_test_flag(tech_pvt, TFLAG_OUTBOUND) ? 0 : 1; + uint32_t stun_ping = 0; + const char *ssrc; + + if ((ssrc = switch_channel_get_variable(tech_pvt->channel, "rtp_use_ssrc"))) { + uint32_t ssrc_ul = (uint32_t) strtoul(ssrc, NULL, 10); + switch_rtp_set_ssrc(tech_pvt->rtp_session, ssrc_ul); + } + + + switch_channel_set_flag(tech_pvt->channel, CF_FS_RTP); + + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_pt", "%d", tech_pvt->agreed_pt); + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_enable_vad_in")) && switch_true(val)) { + vad_in = 1; + } + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_enable_vad_out")) && switch_true(val)) { + vad_out = 1; + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_disable_vad_in")) && switch_true(val)) { + vad_in = 0; + } + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_disable_vad_out")) && switch_true(val)) { + vad_out = 0; + } + + if ((tech_pvt->stun_flags & STUN_FLAG_SET) && (val = switch_channel_get_variable(tech_pvt->channel, "rtp_stun_ping"))) { + int ival = atoi(val); + + if (ival <= 0) { + if (switch_true(val)) { + ival = 6; + } + } + + stun_ping = (ival * tech_pvt->read_impl.samples_per_second) / tech_pvt->read_impl.samples_per_packet; + } + + tech_pvt->ssrc = switch_rtp_get_ssrc(tech_pvt->rtp_session); + switch_channel_set_variable_printf(tech_pvt->channel, "rtp_use_ssrc", "%u", tech_pvt->ssrc); + + sofia_set_flag(tech_pvt, TFLAG_RTP); + sofia_set_flag(tech_pvt, TFLAG_IO); + + if (tech_pvt->profile->auto_rtp_bugs & RTP_BUG_IGNORE_MARK_BIT) { + tech_pvt->rtp_bugs |= RTP_BUG_IGNORE_MARK_BIT; + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_manual_rtp_bugs"))) { + sofia_glue_parse_rtp_bugs(&tech_pvt->rtp_bugs, val); + } + + switch_rtp_intentional_bugs(tech_pvt->rtp_session, tech_pvt->rtp_bugs | tech_pvt->profile->manual_rtp_bugs); + + if ((vad_in && inb) || (vad_out && !inb)) { + switch_rtp_enable_vad(tech_pvt->rtp_session, tech_pvt->session, &tech_pvt->read_codec, SWITCH_VAD_FLAG_TALKING | SWITCH_VAD_FLAG_EVENTS_TALK | SWITCH_VAD_FLAG_EVENTS_NOTALK); + sofia_set_flag(tech_pvt, TFLAG_VAD); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "AUDIO RTP Engage VAD for %s ( %s %s )\n", + switch_channel_get_name(switch_core_session_get_channel(tech_pvt->session)), vad_in ? "in" : "", vad_out ? "out" : ""); + } + + if (stun_ping) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Setting stun ping to %s:%d\n", tech_pvt->stun_ip, + stun_ping); + switch_rtp_activate_stun_ping(tech_pvt->rtp_session, tech_pvt->stun_ip, tech_pvt->stun_port, stun_ping, + (tech_pvt->stun_flags & STUN_FLAG_FUNNY) ? 1 : 0); + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtcp_audio_interval_msec")) || (val = tech_pvt->profile->rtcp_audio_interval_msec)) { + const char *rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_audio_rtcp_port"); + switch_port_t remote_port = 0; + if (rport) { + remote_port = (switch_port_t)atoi(rport); + } + if (!strcasecmp(val, "passthru")) { + switch_rtp_activate_rtcp(tech_pvt->rtp_session, -1, remote_port); + } else { + int interval = atoi(val); + if (interval < 100 || interval > 5000) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, + "Invalid rtcp interval spec [%d] must be between 100 and 5000\n", interval); + } else { + switch_rtp_activate_rtcp(tech_pvt->rtp_session, interval, remote_port); + } + } + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "jitterbuffer_msec")) || (val = tech_pvt->profile->jb_msec)) { + int jb_msec = atoi(val); + int maxlen = 0, max_drift = 0; + char *p, *q; + + if ((p = strchr(val, ':'))) { + p++; + maxlen = atoi(p); + if ((q = strchr(p, ':'))) { + q++; + max_drift = abs(atoi(q)); + } + } + + if (jb_msec < 20 || jb_msec > 10000) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, + "Invalid Jitterbuffer spec [%d] must be between 20 and 10000\n", jb_msec); + } else { + int qlen, maxqlen = 50; + + qlen = jb_msec / (tech_pvt->read_impl.microseconds_per_packet / 1000); + + if (qlen < 1) { + qlen = 3; + } + + if (maxlen) { + maxqlen = maxlen / (tech_pvt->read_impl.microseconds_per_packet / 1000); + } + + if (maxqlen < qlen) { + maxqlen = qlen * 5; + } + if (switch_rtp_activate_jitter_buffer(tech_pvt->rtp_session, qlen, maxqlen, + tech_pvt->read_impl.samples_per_packet, + tech_pvt->read_impl.samples_per_second, max_drift) == SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), + SWITCH_LOG_DEBUG, "Setting Jitterbuffer to %dms (%d frames)\n", jb_msec, qlen); + switch_channel_set_flag(tech_pvt->channel, CF_JITTERBUFFER); + if (!switch_false(switch_channel_get_variable(tech_pvt->channel, "sip_jitter_buffer_plc"))) { + switch_channel_set_flag(tech_pvt->channel, CF_JITTERBUFFER_PLC); + } + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), + SWITCH_LOG_WARNING, "Error Setting Jitterbuffer to %dms (%d frames)\n", jb_msec, qlen); + } + + } + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_timeout_sec"))) { + int v = atoi(val); + if (v >= 0) { + rtp_timeout_sec = v; + } + } + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_hold_timeout_sec"))) { + int v = atoi(val); + if (v >= 0) { + rtp_hold_timeout_sec = v; + } + } + + if (rtp_timeout_sec) { + tech_pvt->max_missed_packets = (tech_pvt->read_impl.samples_per_second * rtp_timeout_sec) / tech_pvt->read_impl.samples_per_packet; + + switch_rtp_set_max_missed_packets(tech_pvt->rtp_session, tech_pvt->max_missed_packets); + if (!rtp_hold_timeout_sec) { + rtp_hold_timeout_sec = rtp_timeout_sec * 10; + } + } + + if (rtp_hold_timeout_sec) { + tech_pvt->max_missed_hold_packets = (tech_pvt->read_impl.samples_per_second * rtp_hold_timeout_sec) / tech_pvt->read_impl.samples_per_packet; + } + + if (tech_pvt->te) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set 2833 dtmf send payload to %u\n", tech_pvt->te); + switch_rtp_set_telephony_event(tech_pvt->rtp_session, tech_pvt->te); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_2833_send_payload", "%d", tech_pvt->te); + } + + if (tech_pvt->recv_te) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set 2833 dtmf receive payload to %u\n", tech_pvt->recv_te); + switch_rtp_set_telephony_recv_event(tech_pvt->rtp_session, tech_pvt->recv_te); + switch_channel_set_variable_printf(tech_pvt->channel, "sip_2833_recv_payload", "%d", tech_pvt->recv_te); + } + + if (tech_pvt->audio_recv_pt != tech_pvt->agreed_pt) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set audio receive payload to %u\n", switch_channel_get_name(tech_pvt->channel), tech_pvt->audio_recv_pt); + + switch_rtp_set_recv_pt(tech_pvt->rtp_session, tech_pvt->audio_recv_pt); + } + + if (sofia_test_pflag(tech_pvt->profile, PFLAG_SUPPRESS_CNG) || + ((val = switch_channel_get_variable(tech_pvt->channel, "supress_cng")) && switch_true(val)) || + ((val = switch_channel_get_variable(tech_pvt->channel, "suppress_cng")) && switch_true(val))) { + tech_pvt->cng_pt = 0; + } + + if (((val = switch_channel_get_variable(tech_pvt->channel, "rtp_digit_delay")))) { + int delayi = atoi(val); + if (delayi < 0) delayi = 0; + delay = (uint32_t) delayi; + } + + + if (delay) { + switch_rtp_set_interdigit_delay(tech_pvt->rtp_session, delay); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set rtp dtmf delay to %u\n", switch_channel_get_name(tech_pvt->channel), delay); + + } + + if (tech_pvt->cng_pt && !sofia_test_pflag(tech_pvt->profile, PFLAG_SUPPRESS_CNG)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Set comfort noise payload to %u\n", tech_pvt->cng_pt); + switch_rtp_set_cng_pt(tech_pvt->rtp_session, tech_pvt->cng_pt); + } + + if (tech_pvt->remote_crypto_key && sofia_test_flag(tech_pvt, TFLAG_SECURE)) { + sofia_glue_add_crypto(tech_pvt, tech_pvt->remote_crypto_key, SWITCH_RTP_CRYPTO_RECV); + switch_rtp_add_crypto_key(tech_pvt->rtp_session, SWITCH_RTP_CRYPTO_SEND, 1, tech_pvt->crypto_type, tech_pvt->local_raw_key, + SWITCH_RTP_KEY_LEN); + switch_rtp_add_crypto_key(tech_pvt->rtp_session, SWITCH_RTP_CRYPTO_RECV, tech_pvt->crypto_tag, tech_pvt->crypto_type, tech_pvt->remote_raw_key, + SWITCH_RTP_KEY_LEN); + switch_channel_set_variable(tech_pvt->channel, SOFIA_SECURE_MEDIA_CONFIRMED_VARIABLE, "true"); + } + + + switch_snprintf(tmp, sizeof(tmp), "%d", tech_pvt->remote_sdp_audio_port); + switch_channel_set_variable(tech_pvt->channel, SWITCH_REMOTE_MEDIA_IP_VARIABLE, tech_pvt->remote_sdp_audio_ip); + switch_channel_set_variable(tech_pvt->channel, SWITCH_REMOTE_MEDIA_PORT_VARIABLE, tmp); + + + if (switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASSTHRU)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_INFO, "Activating ZRTP PROXY MODE\n"); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Disable NOTIMER_DURING_BRIDGE\n"); + sofia_clear_flag(tech_pvt, TFLAG_NOTIMER_DURING_BRIDGE); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Activating audio UDPTL mode\n"); + switch_rtp_udptl_mode(tech_pvt->rtp_session); + } + + + video: + + sofia_glue_check_video_codecs(tech_pvt); + + if (sofia_test_flag(tech_pvt, TFLAG_VIDEO) && tech_pvt->video_rm_encoding && tech_pvt->remote_sdp_video_port) { + /******************************************************************************************/ + if (tech_pvt->video_rtp_session && sofia_test_flag(tech_pvt, TFLAG_REINVITE)) { + //const char *ip = switch_channel_get_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE); + //const char *port = switch_channel_get_variable(tech_pvt->channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE); + char *remote_host = switch_rtp_get_remote_host(tech_pvt->video_rtp_session); + switch_port_t remote_port = switch_rtp_get_remote_port(tech_pvt->video_rtp_session); + + + + if (remote_host && remote_port && !strcmp(remote_host, tech_pvt->remote_sdp_video_ip) && remote_port == tech_pvt->remote_sdp_video_port) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Video params are unchanged for %s.\n", + switch_channel_get_name(tech_pvt->channel)); + goto video_up; + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Video params changed for %s from %s:%d to %s:%d\n", + switch_channel_get_name(tech_pvt->channel), + remote_host, remote_port, tech_pvt->remote_sdp_video_ip, tech_pvt->remote_sdp_video_port); + } + } + + if (!switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "VIDEO RTP [%s] %s port %d -> %s port %d codec: %u ms: %d\n", switch_channel_get_name(tech_pvt->channel), + tech_pvt->local_sdp_audio_ip, tech_pvt->local_sdp_video_port, tech_pvt->remote_sdp_video_ip, + tech_pvt->remote_sdp_video_port, tech_pvt->video_agreed_pt, tech_pvt->read_impl.microseconds_per_packet / 1000); + + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->video_rtp_session, tech_pvt->video_agreed_pt); + } + } + + switch_snprintf(tmp, sizeof(tmp), "%d", tech_pvt->local_sdp_video_port); + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_VIDEO_IP_VARIABLE, tech_pvt->adv_sdp_audio_ip); + switch_channel_set_variable(tech_pvt->channel, SWITCH_LOCAL_VIDEO_PORT_VARIABLE, tmp); + + + if (tech_pvt->video_rtp_session && sofia_test_flag(tech_pvt, TFLAG_REINVITE)) { + const char *rport = NULL; + switch_port_t remote_rtcp_port = 0; + + sofia_clear_flag_locked(tech_pvt, TFLAG_REINVITE); + + if ((rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_video_rtcp_port"))) { + remote_rtcp_port = (switch_port_t)atoi(rport); + } + + if (switch_rtp_set_remote_address + (tech_pvt->video_rtp_session, tech_pvt->remote_sdp_video_ip, tech_pvt->remote_sdp_video_port, remote_rtcp_port, SWITCH_TRUE, + &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "VIDEO RTP REPORTS ERROR: [%s]\n", err); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "VIDEO RTP CHANGING DEST TO: [%s:%d]\n", + tech_pvt->remote_sdp_video_ip, tech_pvt->remote_sdp_video_port); + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + /* Reactivate the NAT buster flag. */ + switch_rtp_set_flag(tech_pvt->video_rtp_session, SWITCH_RTP_FLAG_AUTOADJ); + } + + } + goto video_up; + } + + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) { + sofia_glue_tech_proxy_remote_addr(tech_pvt, NULL); + + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_PROXY_MEDIA | SWITCH_RTP_FLAG_AUTOADJ | SWITCH_RTP_FLAG_DATAWAIT); + } else { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_PROXY_MEDIA | SWITCH_RTP_FLAG_DATAWAIT); + } + timer_name = NULL; + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "PROXY VIDEO RTP [%s] %s:%d->%s:%d codec: %u ms: %d\n", + switch_channel_get_name(tech_pvt->channel), + tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_video_port, + tech_pvt->remote_sdp_video_ip, + tech_pvt->remote_sdp_video_port, tech_pvt->video_agreed_pt, tech_pvt->read_impl.microseconds_per_packet / 1000); + + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->video_rtp_session, tech_pvt->video_agreed_pt); + } + } else { + timer_name = tech_pvt->profile->timer_name; + + if ((var = switch_channel_get_variable(tech_pvt->channel, "rtp_timer_name"))) { + timer_name = (char *) var; + } + } + + /******************************************************************************************/ + + if (tech_pvt->video_rtp_session) { + goto video_up; + } + + + if (!tech_pvt->local_sdp_video_port) { + sofia_glue_tech_choose_video_port(tech_pvt, 1); + } + + if (!sofia_test_pflag(tech_pvt->profile, PFLAG_DISABLE_RTP_AUTOADJ) && !switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE) && + !((val = switch_channel_get_variable(tech_pvt->channel, "disable_rtp_auto_adjust")) && switch_true(val))) { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_AUTOADJ | SWITCH_RTP_FLAG_DATAWAIT | SWITCH_RTP_FLAG_RAW_WRITE); + } else { + flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_DATAWAIT | SWITCH_RTP_FLAG_RAW_WRITE); + } + + if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) { + flags |= SWITCH_RTP_FLAG_PROXY_MEDIA; + } + sofia_glue_tech_set_video_codec(tech_pvt, 0); + + flags &= ~(SWITCH_RTP_FLAG_USE_TIMER | SWITCH_RTP_FLAG_NOBLOCK); + flags |= SWITCH_RTP_FLAG_VIDEO; + + tech_pvt->video_rtp_session = switch_rtp_new(tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_video_port, + tech_pvt->remote_sdp_video_ip, + tech_pvt->remote_sdp_video_port, + tech_pvt->video_agreed_pt, + 1, 90000, (switch_rtp_flag_t) flags, NULL, &err, switch_core_session_get_pool(tech_pvt->session)); + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "%sVIDEO RTP [%s] %s:%d->%s:%d codec: %u ms: %d [%s]\n", + switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA) ? "PROXY " : "", + switch_channel_get_name(tech_pvt->channel), + tech_pvt->local_sdp_audio_ip, + tech_pvt->local_sdp_video_port, + tech_pvt->remote_sdp_video_ip, + tech_pvt->remote_sdp_video_port, tech_pvt->video_agreed_pt, + 0, switch_rtp_ready(tech_pvt->video_rtp_session) ? "SUCCESS" : err); + + + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + switch_rtp_set_default_payload(tech_pvt->video_rtp_session, tech_pvt->video_agreed_pt); + } + + if (switch_rtp_ready(tech_pvt->video_rtp_session)) { + const char *ssrc; + switch_channel_set_flag(tech_pvt->channel, CF_VIDEO); + if ((ssrc = switch_channel_get_variable(tech_pvt->channel, "rtp_use_video_ssrc"))) { + uint32_t ssrc_ul = (uint32_t) strtoul(ssrc, NULL, 10); + switch_rtp_set_ssrc(tech_pvt->video_rtp_session, ssrc_ul); + } + + + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtp_manual_video_rtp_bugs"))) { + sofia_glue_parse_rtp_bugs(&tech_pvt->video_rtp_bugs, val); + } + + switch_rtp_intentional_bugs(tech_pvt->video_rtp_session, tech_pvt->video_rtp_bugs | tech_pvt->profile->manual_video_rtp_bugs); + + if (tech_pvt->video_recv_pt != tech_pvt->video_agreed_pt) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, + "%s Set video receive payload to %u\n", switch_channel_get_name(tech_pvt->channel), tech_pvt->video_recv_pt); + switch_rtp_set_recv_pt(tech_pvt->video_rtp_session, tech_pvt->video_recv_pt); + } + + switch_channel_set_variable_printf(tech_pvt->channel, "sip_use_video_pt", "%d", tech_pvt->video_agreed_pt); + tech_pvt->video_ssrc = switch_rtp_get_ssrc(tech_pvt->rtp_session); + switch_channel_set_variable_printf(tech_pvt->channel, "rtp_use_video_ssrc", "%u", tech_pvt->ssrc); + + + if ((val = switch_channel_get_variable(tech_pvt->channel, "rtcp_audio_interval_msec")) + || (val = tech_pvt->profile->rtcp_audio_interval_msec)) { + const char *rport = switch_channel_get_variable(tech_pvt->channel, "sip_remote_video_rtcp_port"); + switch_port_t remote_port = 0; + if (rport) { + remote_port = (switch_port_t)atoi(rport); + } + if (!strcasecmp(val, "passthru")) { + switch_rtp_activate_rtcp(tech_pvt->rtp_session, -1, remote_port); + } else { + int interval = atoi(val); + if (interval < 100 || interval > 5000) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, + "Invalid rtcp interval spec [%d] must be between 100 and 5000\n", interval); + } else { + switch_rtp_activate_rtcp(tech_pvt->rtp_session, interval, remote_port); + } + } + } + if (switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASSTHRU)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Activating video UDPTL mode\n"); + switch_rtp_udptl_mode(tech_pvt->video_rtp_session); + } + + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "VIDEO RTP REPORTS ERROR: [%s]\n", switch_str_nil(err)); + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + goto end; + } + } + + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "AUDIO RTP REPORTS ERROR: [%s]\n", switch_str_nil(err)); + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + sofia_clear_flag_locked(tech_pvt, TFLAG_IO); + status = SWITCH_STATUS_FALSE; + goto end; + } + + video_up: + + sofia_set_flag(tech_pvt, TFLAG_IO); + status = SWITCH_STATUS_SUCCESS; + + end: + + sofia_clear_flag_locked(tech_pvt, TFLAG_REINVITE); + switch_core_recovery_track(tech_pvt->session); + + switch_mutex_unlock(tech_pvt->sofia_mutex); + + return status; + +} + +static void add_audio_codec(sdp_rtpmap_t *map, int ptime, char *buf, switch_size_t buflen) +{ + int codec_ms = ptime; + uint32_t map_bit_rate = 0; + char ptstr[20] = ""; + char ratestr[20] = ""; + char bitstr[20] = ""; + switch_codec_fmtp_t codec_fmtp = { 0 }; + + if (!codec_ms) { + codec_ms = switch_default_ptime(map->rm_encoding, map->rm_pt); + } + + map_bit_rate = switch_known_bitrate((switch_payload_t)map->rm_pt); + + if (!ptime && !strcasecmp(map->rm_encoding, "g723")) { + ptime = codec_ms = 30; + } + + if (zstr(map->rm_fmtp)) { + if (!strcasecmp(map->rm_encoding, "ilbc")) { + ptime = codec_ms = 30; + map_bit_rate = 13330; + } + } else { + if ((switch_core_codec_parse_fmtp(map->rm_encoding, map->rm_fmtp, map->rm_rate, &codec_fmtp)) == SWITCH_STATUS_SUCCESS) { + if (codec_fmtp.bits_per_second) { + map_bit_rate = codec_fmtp.bits_per_second; + } + if (codec_fmtp.microseconds_per_packet) { + codec_ms = (codec_fmtp.microseconds_per_packet / 1000); + } + } + } + + if (map->rm_rate) { + switch_snprintf(ratestr, sizeof(ratestr), "@%uh", (unsigned int) map->rm_rate); + } + + if (codec_ms) { + switch_snprintf(ptstr, sizeof(ptstr), "@%di", codec_ms); + } +>>>>>>> 57fb368... sla cid tweaks + format = strchr(sipip, ':') ? "\"%s\" " : "\"%s\" "; if (!zstr(invite_domain)) { diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 8cc93b52b5..d3293e7b74 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -3276,26 +3276,22 @@ static int broadsoft_sla_gather_state_callback(void *pArg, int argc, char **argv data = switch_core_hash_find(sh->hash, key); - if (uuid && (session = switch_core_session_locate(uuid))) { + if (strcasecmp(state, "idle") && uuid && (session = switch_core_session_locate(uuid))) { switch_channel_t *channel = switch_core_session_get_channel(session); - if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) { + if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { + callee_name = switch_channel_get_variable(channel, "callee_id_name"); + callee_number = switch_channel_get_variable(channel, "callee_id_number"); - if (zstr((callee_name = switch_channel_get_variable(channel, "effective_callee_id_name"))) && - zstr((callee_name = switch_channel_get_variable(channel, "sip_callee_id_name")))) { - callee_name = switch_channel_get_variable(channel, "callee_id_name"); - } - - if (zstr((callee_number = switch_channel_get_variable(channel, "effective_callee_id_number"))) && - zstr((callee_number = switch_channel_get_variable(channel, "sip_callee_id_number"))) && - zstr((callee_number = switch_channel_get_variable(channel, "callee_id_number")))) { + if (zstr(callee_number)) { callee_number = switch_channel_get_variable(channel, "destination_number"); } + } else { callee_name = switch_channel_get_variable(channel, "caller_id_name"); callee_number = switch_channel_get_variable(channel, "caller_id_number"); } - + if (zstr(callee_name) && !zstr(callee_number)) { callee_name = callee_number; } @@ -3307,9 +3303,20 @@ static int broadsoft_sla_gather_state_callback(void *pArg, int argc, char **argv if (!zstr(callee_name)) { callee_name = switch_sanitize_number(switch_core_session_strdup(session, callee_name)); } + + + //if (switch_channel_get_state(channel) != CS_EXECUTE) { + //callee_number = NULL; + //} + switch_core_session_rwunlock(session); } + if (data && strstr(data, info)) { + return 0; + } + + if (!zstr(callee_number)) { if (zstr(callee_name)) { callee_name = "unknown"; diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index b545748cb2..ddb6d8c801 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -1473,7 +1473,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session switch_codec_implementation_t tread_impl = { 0 }, read_impl = { 0 }; switch_core_session_message_t msg = { 0 }; char cid_buf[1024] = ""; - switch_caller_profile_t *cp = NULL; + switch_caller_profile_t *cp = NULL, *my_cp = NULL; uint32_t sanity = 600; if (!switch_channel_media_up(channel)) { @@ -1595,26 +1595,29 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session msg.message_id = SWITCH_MESSAGE_INDICATE_BRIDGE; switch_core_session_receive_message(session, &msg); cp = switch_channel_get_caller_profile(tchannel); + my_cp = switch_channel_get_caller_profile(channel); name = cp->caller_id_name; num = cp->caller_id_number; if (flags & ED_COPY_DISPLAY) { - const char *tmp_name = NULL, *tmp_num = NULL; - name = cp->callee_id_name; - num = cp->callee_id_number; - - if (!((tmp_name = switch_channel_get_variable(tchannel, "last_sent_callee_id_name")) - && (tmp_num = switch_channel_get_variable(tchannel, "last_sent_callee_id_number")))) { - - tmp_name = switch_channel_get_variable(tchannel, "callee_id_name"); - tmp_num = switch_channel_get_variable(tchannel, "callee_id_number"); + if (switch_channel_test_flag(tchannel, CF_BRIDGE_ORIGINATOR) || !switch_channel_test_flag(tchannel, CF_BRIDGED)) { + name = cp->callee_id_name; + num = cp->callee_id_number; + } else { + name = cp->caller_id_name; + num = cp->caller_id_number; } - - if (tmp_name) name = tmp_name; - if (tmp_num) num = tmp_num; - + + my_cp->callee_id_name = switch_core_strdup(my_cp->pool, name); + my_cp->callee_id_number = switch_core_strdup(my_cp->pool, num); } + sanity = 300; + while(switch_channel_up(channel) && !switch_channel_media_ack(channel) && --sanity) { + switch_yield(10000); + } + + switch_snprintf(cid_buf, sizeof(cid_buf), "%s|%s", name, num); msg.string_arg = cid_buf; msg.message_id = SWITCH_MESSAGE_INDICATE_DISPLAY;