55 #define SDP_HANDLER_BUCKETS 11 57 #define MOD_DATA_ON_RESPONSE "on_response" 58 #define MOD_DATA_NAT_HOOK "nat_hook" 61 #define DEFAULT_NUM_SESSION_MEDIA 2 118 return "(null session)";
148 if (!strcmp(iter->
id, handler->
id)) {
149 ast_log(
LOG_WARNING,
"Handler '%s' already registered for stream type '%s'.\n", handler->
id, stream_type);
154 ast_debug(1,
"Registered SDP stream handler '%s' for stream type '%s'\n", handler->
id, stream_type);
160 handler_list =
ao2_alloc(
sizeof(*handler_list) + strlen(stream_type),
NULL);
165 strcpy(handler_list->stream_type, stream_type);
168 if (!
ao2_link(sdp_handlers, handler_list)) {
171 ast_debug(1,
"Registered SDP stream handler '%s' for stream type '%s'\n", handler->
id, stream_type);
184 if (!strcmp(iter->
id, handler->
id)) {
186 ast_debug(1,
"Unregistered SDP stream handler '%s' for stream type '%s'\n", handler->
id, stream_type);
192 ast_debug(3,
"No more handlers exist for stream type '%s'\n", stream_type);
215 size_t sessions,
size_t read_callbacks)
219 media_state =
ast_calloc(1,
sizeof(*media_state));
229 if (
AST_VECTOR_INIT(&media_state->read_callbacks, read_callbacks) < 0) {
249 if (!media_state || !sip_session) {
256 if (!media || !media->
rtp) {
321 for (index = 0; index <
AST_VECTOR_SIZE(&media_state->sessions); ++index) {
335 for (index = 0; index <
AST_VECTOR_SIZE(&media_state->read_callbacks); ++index) {
390 .read_callback = callback,
391 .session = session_media,
419 return session_media;
429 return bundle_group_session_media;
433 return session_media;
480 if (session_media->
srtp) {
501 current_session_media =
AST_VECTOR_GET(&media_state->sessions, position);
502 if (current_session_media && current_session_media->
type == type) {
511 if (session_media->
type == type) {
513 ast_trace(1,
"Reusing existing media session\n");
529 ast_trace(1,
"Can't reuse existing media session because the types are different. %s <> %s\n",
531 session_media =
NULL;
535 if (!session_media) {
538 if (!session_media) {
541 ast_trace(1,
"Creating new media session\n");
601 return (type_streams[type] > 0);
617 for (index = 0; index < sdp->attr_count; ++index) {
618 pjmedia_sdp_attr *attr = sdp->attr[index];
619 char value[pj_strlen(&attr->value) + 1], *mids =
value, *attr_mid;
621 if (pj_strcmp2(&attr->name,
"group") || pj_strncmp2(&attr->value,
"BUNDLE", 6)) {
630 while ((attr_mid =
strsep(&mids,
" "))) {
631 if (!strcmp(attr_mid, mid)) {
648 const pjmedia_sdp_session *sdp,
649 const struct pjmedia_sdp_media *stream)
651 pjmedia_sdp_attr *attr;
664 attr = pjmedia_sdp_media_find_attr2(stream,
"mid",
NULL);
670 if (!session_media->
mid) {
686 const pjmedia_sdp_session *sdp,
687 const struct pjmedia_sdp_media *stream,
697 for (index = 0; index < stream->attr_count; ++index) {
698 pjmedia_sdp_attr *attr = stream->attr[index];
699 char attr_value[pj_strlen(&attr->value) + 1];
700 char *ssrc_attribute_name, *ssrc_attribute_value =
NULL;
701 char *msid, *
tmp = attr_value;
702 static const pj_str_t STR_msid = {
"msid", 4 };
703 static const pj_str_t STR_ssrc = {
"ssrc", 4 };
704 static const pj_str_t STR_label = {
"label", 5 };
706 if (!pj_strcmp(&attr->name, &STR_label)) {
709 }
else if (!pj_strcmp(&attr->name, &STR_msid)) {
714 }
else if (!pj_strcmp(&attr->name, &STR_ssrc)) {
717 if ((ssrc_attribute_name = strchr(attr_value,
' '))) {
719 *ssrc_attribute_name++ =
'\0';
720 ssrc_attribute_value = strchr(ssrc_attribute_name,
':');
721 if (ssrc_attribute_value) {
723 *ssrc_attribute_value++ =
'\0';
726 if (!strcasecmp(ssrc_attribute_name,
"mslabel") && !
ast_strlen_zero(ssrc_attribute_value)) {
785 for (i = 0; i < sdp->media_count; ++i) {
794 pjmedia_sdp_media *remote_stream = sdp->media[i];
809 char *stream_name =
NULL, *stream_name_allocated =
NULL;
810 const char *stream_label =
NULL;
831 stream_name = stream_name_allocated;
860 if (pjmedia_sdp_media_find_attr2(remote_stream,
"sendonly",
NULL)) {
866 }
else if (pjmedia_sdp_media_find_attr2(remote_stream,
"recvonly",
NULL)) {
868 }
else if (pjmedia_sdp_media_find_attr2(remote_stream,
"inactive",
NULL)) {
881 if (!session_media) {
889 SCOPE_EXIT_EXPR(
continue,
"%s: Declining incoming SDP media stream %s'\n",
897 handler = session_media->
handler;
898 ast_trace(-1,
"%s: Negotiating incoming SDP media stream %s using %s SDP handler\n",
906 }
else if (res == 0) {
908 SCOPE_EXIT_EXPR(
continue,
"%s: Declining incoming SDP media stream %s\n",
910 }
else if (res > 0) {
912 ++type_streams[
type];
922 SCOPE_EXIT_EXPR(
continue,
"%s: Media stream %s has no registered handlers\n",
926 if (handler == session_media->
handler) {
929 ast_trace(-1,
"%s: Negotiating incoming SDP media stream %s using %s SDP handler\n",
939 }
else if (res == 0) {
941 ast_trace(-1,
"%s: Declining incoming SDP media stream %s\n",
944 }
else if (res > 0) {
947 ++type_streams[
type];
948 ast_trace(-1,
"%s: Media stream %s handled by %s\n",
961 handled ?
"yes" :
"no");
966 const pjmedia_sdp_session *remote,
int index,
struct ast_stream *asterisk_stream)
969 struct pjmedia_sdp_media *local_stream = local->media[index];
977 ast_copy_pj_str(media, &local->media[index]->desc.media,
sizeof(media));
987 if (pjmedia_sdp_media_find_attr2(local_stream,
"sendonly",
NULL)) {
989 }
else if (pjmedia_sdp_media_find_attr2(local_stream,
"recvonly",
NULL)) {
991 }
else if (pjmedia_sdp_media_find_attr2(local_stream,
"inactive",
NULL)) {
1003 handler = session_media->
handler;
1005 ast_debug(4,
"%s: Applying negotiated SDP media stream '%s' using %s SDP handler\n",
1010 ast_debug(4,
"%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
1017 SCOPE_EXIT_RTN_VALUE(-1,
"%s: Failed to apply negotiated SDP media stream '%s' using %s SDP handler\n",
1023 if (!handler_list) {
1028 if (handler == session_media->
handler) {
1031 ast_debug(4,
"%s: Applying negotiated SDP media stream '%s' using %s SDP handler\n",
1041 ast_debug(4,
"%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
1053 ast_debug(4,
"%s: Stopping SDP media stream '%s' as it is not currently negotiated\n",
1060 res ?
"not negotiated. Stopped" :
"handled");
1067 unsigned int changed = 0;
1079 active_media_state_clone =
1081 if (!active_media_state_clone) {
1103 ast_log(
LOG_WARNING,
"%s: Local SDP contains %d media streams while we expected it to contain %u\n",
1109 for (i = 0; i < local->media_count; ++i) {
1113 if (!remote->media[i]) {
1124 if (!remote->media[i]->desc.port) {
1146 changed |= session_media->
changed;
1154 for (i = 0; i < local->media_count; ++i) {
1158 if (!remote->media[i]) {
1184 if (pjmedia_sdp_neg_was_answer_remote(session->
inv_session->neg) == PJ_FALSE &&
1217 }
else if (changed == 2) {
1226 #define DATASTORE_BUCKETS 53 1227 #define MEDIA_BUCKETS 7 1247 const char *uid_ptr =
uid;
1258 datastore->info =
info;
1265 if (!datastore->uid) {
1312 const char *
str =
"<unknown>";
1397 SCOPE_ENTER(3,
"%s: sending delayed %s request\n",
1502 if (!timer_running) {
1583 int (*cb)(
void *vsession))
1595 int generate_new_sdp,
1602 on_request, on_sdp_creation, on_response, generate_new_sdp, pending_media_state,
1603 active_media_state);
1623 pjsip_inv_session *inv_session = session->
inv_session;
1624 const pjmedia_sdp_session *previous_sdp =
NULL;
1627 if (inv_session->neg) {
1628 if (pjmedia_sdp_neg_was_answer_remote(inv_session->neg)) {
1629 pjmedia_sdp_neg_get_active_remote(inv_session->neg, &previous_sdp);
1631 pjmedia_sdp_neg_get_active_local(inv_session->neg, &previous_sdp);
1641 pj_pool_t *dlg_pool;
1642 pjsip_fromto_hdr *dlg_info;
1643 pjsip_contact_hdr *dlg_contact;
1644 pjsip_name_addr *dlg_info_name_addr;
1645 pjsip_sip_uri *dlg_info_uri;
1646 pjsip_sip_uri *dlg_contact_uri;
1648 const char *pjsip_from_domain;
1668 dlg_contact = session->
inv_session->dlg->local.contact;
1669 dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1670 dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1671 dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
1683 dlg_info_name_addr->display.ptr =
NULL;
1684 dlg_info_name_addr->display.slen = 0;
1698 ast_debug(3,
"%s: From header domain reset by channel variable SIPFROMDOMAIN (%s)\n",
1700 pj_strdup2(dlg_pool, &dlg_info_uri->host, pjsip_from_domain);
1713 pj_strdup2(dlg_pool, &dlg_info_name_addr->display,
"Anonymous");
1716 pj_strdup2(dlg_pool, &dlg_info_uri->user,
"anonymous");
1720 pj_strdup2(dlg_pool, &dlg_contact_uri->user,
"anonymous");
1724 pj_strdup2(dlg_pool, &dlg_info_uri->host,
"anonymous.invalid");
1734 #define STREAM_REMOVED(_stream) (ast_stream_get_state(_stream) == AST_STREAM_STATE_REMOVED) 1735 #define STATE_REMOVED(_stream_state) (_stream_state == AST_STREAM_STATE_REMOVED) 1736 #define STATE_NONE(_stream_state) (_stream_state == AST_STREAM_STATE_END) 1737 #define GET_STREAM_SAFE(_topology, _i) (_i < ast_stream_topology_get_count(_topology) ? ast_stream_topology_get_stream(_topology, _i) : NULL) 1738 #define GET_STREAM_STATE_SAFE(_stream) (_stream ? ast_stream_get_state(_stream) : AST_STREAM_STATE_END) 1739 #define GET_STREAM_NAME_SAFE(_stream) (_stream ? ast_stream_get_name(_stream) : "") 1757 SCOPE_ENTER(3,
"%s: Topology: %s\n", session_name,
1760 if (session_count != stream_count) {
1762 session_count, stream_count);
1765 for (i = 0; i < stream_count; i++) {
1768 const char *stream_name =
NULL;
1777 for (j = 0; j < stream_count; j++) {
1779 if (j == i || !possible_dup) {
1795 if (j == i || !possible_dup) {
1801 i, media->
label, j);
1811 SCOPE_EXIT_EXPR(
goto end,
"%s: media %d has type %s but stream has type %s\n", stream_name,
1836 const char *session_name,
1840 int run_post_validation)
1849 int max_stream_count;
1851 SCOPE_ENTER(2,
"%s: DP: %s DA: %s CA: %s\n", session_name,
1866 if (!new_pending_state) {
1869 new_pending = new_pending_state->topology;
1871 for (i = 0; i < max_stream_count; i++) {
1888 const char *found_da_name __attribute__((unused)) =
"";
1889 const char *found_np_name __attribute__((unused)) =
"";
1890 int found_da_slot __attribute__((unused)) = -1;
1891 int found_np_slot = -1;
1892 int removed_np_slot = -1;
1894 SCOPE_ENTER(3,
"%s: slot: %d DP: %s DA: %s CA: %s\n", session_name, i,
1924 SCOPE_EXIT_EXPR(
continue,
"%s: No DP stream so use CA stream as is\n", session_name);
1932 ast_trace(-1,
"%s: Same stream in all 3 states\n", session_name);
1933 if (dp_state == da_state && da_state == ca_state) {
1935 SCOPE_EXIT_EXPR(
continue,
"%s: All in the same state so nothing to do\n", session_name);
1937 if (da_state != ca_state) {
1942 SCOPE_EXIT_EXPR(
continue,
"%s: Ignoring request to change state from %s to %s\n",
1945 if (dp_state != da_state) {
1948 SCOPE_EXIT_EXPR(
continue,
"%s: Changed NP stream state from %s to %s\n",
1973 ast_trace(-1,
"%s: Checking if DP is already in NP somewhere\n", session_name);
1978 ast_trace(-1,
"%s: Checking %s against %s\n", session_name, dp_name, possible_existing_name);
1979 if (found_np_slot == -1 &&
ast_strings_equal(dp_name, possible_existing_name)) {
1980 ast_trace(-1,
"%s: Pending stream %s slot %d is in NP slot %d\n", session_name,
1983 found_np_stream = possible_existing;
1987 if (
STREAM_REMOVED(possible_existing) && removed_np_slot == -1) {
1988 removed_np_slot = j;
1990 if (removed_np_slot >= 0 && found_np_slot >= 0) {
1997 found_np_stream = np_stream;
1998 found_np_state = np_state;
1999 found_np_name = np_name;
2010 ast_trace(-1,
"%s: Checking if DP is already in DA somewhere\n", session_name);
2015 ast_trace(-1,
"%s: Checking %s against %s\n", session_name, dp_name, possible_existing_name);
2017 ast_trace(-1,
"%s: Pending stream %s slot %d is already in delayed active slot %d\n",
2018 session_name, dp_name, i, j);
2020 found_da_stream = possible_existing;
2029 found_da_stream = da_stream;
2030 found_da_state = da_state;
2031 found_da_name = da_name;
2034 ast_trace(-1,
"%s: Found NP slot: %d Found removed NP slot: %d Found DA slot: %d\n",
2035 session_name, found_np_slot, removed_np_slot, found_da_slot);
2042 if (!found_da_stream) {
2049 ast_trace(-1,
"%s: There was no corresponding DA stream so the request was to add a stream\n", session_name);
2051 if (found_np_stream) {
2056 SCOPE_EXIT_EXPR(
continue,
"%s: New stream requested but it's already in CA\n", session_name);
2061 ast_trace(-1,
"%s: There was no corresponding NP stream\n", session_name);
2070 "%s: Attempting to remove stream %d:%s but it doesn't exist anywhere.\n", session_name, i, dp_name);
2077 ast_trace(-1,
"%s: Checking for open slot\n", session_name);
2078 if (removed_np_slot >= 0) {
2096 session_name, removed_np_slot);
2108 session_name, new_slot);
2116 ast_trace(-1,
"%s: There was a corresponding DA stream so the request was to change/remove a stream\n", session_name);
2117 if (dp_state == found_da_state) {
2119 if (!found_np_stream) {
2127 SCOPE_EXIT_EXPR(
continue,
"%s: Stream doesn't exist in CA so nothing to do\n", session_name);
2128 }
else if (dp_state == found_np_state) {
2129 SCOPE_EXIT_EXPR(
continue,
"%s: States are the same all around so nothing to do\n", session_name);
2131 SCOPE_EXIT_EXPR(
continue,
"%s: Something changed the CA state so we're going to leave it as is\n", session_name);
2136 if (!found_np_stream) {
2137 SCOPE_EXIT_EXPR(
continue,
"%s: Stream doesn't exist in CA so nothing to do\n", session_name);
2138 }
else if (da_state == found_np_state) {
2140 SCOPE_EXIT_EXPR(
continue,
"%s: Changed NP stream state from %s to %s\n",
2143 SCOPE_EXIT_EXPR(
continue,
"%s: Something changed the CA state so we're going to leave it as is\n",
2149 SCOPE_EXIT(
"%s: Done with slot %d\n", session_name, i);
2152 ast_trace(-1,
"%s: Resetting default media states\n", session_name);
2155 new_pending_state->default_session[i] =
NULL;
2161 new_pending_state->default_session[i] = media;
2167 if (run_post_validation) {
2168 ast_trace(-1,
"%s: Running post-validation\n", session_name);
2178 returned_media_state = new_pending_state;
2179 new_pending_state =
NULL;
2193 pjsip_inv_session *inv_session = session->
inv_session;
2194 pjmedia_sdp_session *new_sdp =
NULL;
2195 pjsip_tx_data *tdata;
2198 generate_new_sdp ?
"yes" :
"no", queued ?
"yes" :
"no",
2202 if (pending_media_state && (!pending_media_state->
topology || !generate_new_sdp)) {
2207 pending_media_state->
topology ==
NULL ?
"pending topology is null " :
"",
2208 !generate_new_sdp ?
"generate_new_sdp is false" :
"");
2211 if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2220 if (inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
2221 res =
delay_request(session, on_request_creation, on_sdp_creation, on_response,
2226 SCOPE_EXIT_RTN_VALUE(res,
"%s: Delay sending reinvite because dialog has not been established\n",
2231 if (inv_session->invite_tsx) {
2233 res =
delay_request(session, on_request_creation, on_sdp_creation,
2238 }
else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
2249 if (generate_new_sdp) {
2251 if (inv_session->neg
2252 && pjmedia_sdp_neg_get_state(inv_session->neg)
2253 != PJMEDIA_SDP_NEG_STATE_DONE) {
2254 res =
delay_request(session, on_request_creation, on_sdp_creation,
2255 on_response, generate_new_sdp,
2259 SCOPE_EXIT_RTN_VALUE(res,
"%s: Delay session refresh with new SDP because SDP negotiation is not yet done\n",
2264 if (pending_media_state) {
2276 res =
delay_request(session, on_request_creation, on_sdp_creation,
2277 on_response, generate_new_sdp,
2289 if (active_media_state && active_media_state->
topology &&
2304 if (new_pending_state) {
2307 pending_media_state = new_pending_state;
2355 if (!existing_stream) {
2381 if (!existing_stream) {
2450 SCOPE_EXIT(
"%s: Appended empty stream in position %d to make counts match\n",
2483 if (on_sdp_creation) {
2484 if (on_sdp_creation(session, new_sdp)) {
2493 if (pjsip_inv_reinvite(inv_session,
NULL, new_sdp, &tdata)) {
2494 if (generate_new_sdp) {
2500 }
else if (pjsip_inv_update(inv_session,
NULL, new_sdp, &tdata)) {
2501 if (generate_new_sdp) {
2507 if (on_request_creation) {
2508 if (on_request_creation(session, tdata)) {
2509 if (generate_new_sdp) {
2532 on_response, method, generate_new_sdp, media_state,
NULL, 0);
2538 pjsip_inv_session *inv_session = session->
inv_session;
2539 pjmedia_sdp_session *new_answer =
NULL;
2540 const pjmedia_sdp_session *previous_offer =
NULL;
2544 if (!inv_session->neg || (pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
2545 pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)) {
2546 ast_log(
LOG_WARNING,
"Requested to regenerate local SDP answer for channel '%s' but negotiation in state '%s'\n",
2551 pjmedia_sdp_neg_get_neg_remote(inv_session->neg, &previous_offer);
2552 if (pjmedia_sdp_neg_get_state(inv_session->neg) == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
2554 pjmedia_sdp_neg_negotiate(inv_session->pool, inv_session->neg, 0);
2555 pjmedia_sdp_neg_set_remote_offer(inv_session->pool, inv_session->neg, previous_offer);
2565 if (on_sdp_creation) {
2566 if (on_sdp_creation(session, new_answer)) {
2571 pjsip_inv_set_sdp_answer(inv_session, new_answer);
2588 .name = {
"Session Module", 14},
2589 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
2611 for (i = 0; i < sdp->media_count; ++i) {
2621 pjmedia_sdp_media *remote_stream = sdp->media[i];
2645 if (existing_stream) {
2654 if (!session_media) {
2660 if (pjmedia_sdp_media_find_attr2(remote_stream,
"sendonly",
NULL)) {
2666 }
else if (pjmedia_sdp_media_find_attr2(remote_stream,
"recvonly",
NULL)) {
2668 }
else if (pjmedia_sdp_media_find_attr2(remote_stream,
"inactive",
NULL)) {
2678 handler = session_media->
handler;
2698 if (!handler_list) {
2703 if (handler == session_media->
handler) {
2737 pjsip_rdata_sdp_info *sdp_info;
2740 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
2741 !(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
2753 pj_str_t key, deferred_key;
2754 pjsip_tx_data *tdata;
2757 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
2758 pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->
deferred_reinvite->msg_info.cseq->method,
2762 if (!pj_strcmp(&key, &deferred_key)) {
2767 if (pjsip_dlg_create_response(dlg, rdata, 491,
NULL, &tdata) == PJ_SUCCESS) {
2769 pjsip_tx_data_dec_ref(tdata);
2776 if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
2777 (sdp_info->sdp_err != PJ_SUCCESS)) {
2781 if (!sdp_info->sdp) {
2786 if (deferred == -1) {
2789 }
else if (!deferred) {
2813 .name = {
"Session Re-Invite Module", 24 },
2814 .priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
2821 pjsip_inv_session *inv_session = session->
inv_session;
2828 if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED &&
2829 tdata->msg->line.req.method.id != PJSIP_BYE_METHOD) {
2849 pjmedia_sdp_session *offer;
2853 pjsip_inv_terminate(session->
inv_session, 500, PJ_FALSE);
2857 pjsip_inv_set_local_sdp(session->
inv_session, offer);
2858 pjmedia_sdp_neg_set_prefer_remote_codec_order(session->
inv_session->neg, PJ_FALSE);
2859 #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS 2861 pjmedia_sdp_neg_set_answer_multiple_codecs(session->
inv_session->neg, PJ_TRUE);
2871 if (pjsip_inv_invite(session->
inv_session, tdata) != PJ_SUCCESS) {
2892 const char *uid2 = flags &
OBJ_KEY ? arg : datastore2->
uid;
2905 #ifdef TEST_FRAMEWORK 2907 const char *endpoint_name = session->
endpoint ?
2950 struct pjsip_dialog *dlg = session->
inv_session->dlg;
2956 #ifdef HAVE_PJSIP_INV_SESSION_REF 2959 pjsip_dlg_dec_session(dlg, &session_module);
2995 int dsp_features = 0;
3035 if (!session->
dsp) {
3078 #ifdef HAVE_PJSIP_INV_SESSION_REF 3079 if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {
3085 pjsip_dlg_inc_session(inv_session->dlg, &session_module);
3086 inv_session->mod_data[session_module.id] =
ao2_bump(session);
3233 pjsip_transaction *tsx;
3235 pjsip_inv_session *inv;
3236 pjsip_tx_data *tdata;
3239 if (rdata->msg_info.msg->line.status.code != 401
3240 && rdata->msg_info.msg->line.status.code != 407) {
3245 tsx = pjsip_rdata_get_tsx(rdata);
3246 dlg = pjsip_rdata_get_dlg(rdata);
3251 if (tsx->method.id != PJSIP_INVITE_METHOD) {
3256 inv = pjsip_dlg_get_inv_session(dlg);
3257 session = inv->mod_data[session_module.id];
3259 if (PJSIP_INV_STATE_CONFIRMED <= inv->
state) {
3276 tsx->last_tx, &tdata)) {
3284 pjsip_inv_uac_restart(inv, PJ_FALSE);
3291 .name = {
"Outbound INVITE Auth", 20},
3292 .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
3311 status = pjsip_dlg_add_usage(dlg, &outbound_invite_auth_module,
NULL);
3314 return status != PJ_SUCCESS ? -1 : 0;
3321 const char *uri =
NULL;
3324 pjsip_timer_setting
timer;
3326 struct pjsip_inv_session *inv_session;
3333 if (location || !contact) {
3334 location =
S_OR(location, endpoint->
aors);
3337 &found_aor, &found_contact);
3341 uri = found_contact->uri;
3349 ast_log(
LOG_ERROR,
"Endpoint '%s': No URI available. Is endpoint registered?\n",
3359 pjsip_dlg_terminate(dlg);
3363 if (pjsip_inv_create_uac(dlg,
NULL, endpoint->
extensions.
flags, &inv_session) != PJ_SUCCESS) {
3364 pjsip_dlg_terminate(dlg);
3367 #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE) 3368 inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
3371 pjsip_timer_setting_default(&timer);
3374 pjsip_timer_init_session(inv_session, &timer);
3379 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3410 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3427 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3433 if (pjsip_dlg_add_usage(dlg, &session_module,
NULL) != PJ_SUCCESS) {
3434 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3454 pjsip_tx_data *packet =
NULL;
3477 case PJSIP_INV_STATE_NULL:
3487 pjsip_inv_terminate(session->
inv_session, response, PJ_TRUE);
3495 pjsip_inv_terminate(session->
inv_session, response, PJ_TRUE);
3498 case PJSIP_INV_STATE_CONFIRMED:
3500 ast_debug(3,
"%s: Delay sending BYE because of outstanding transaction...\n",
3510 status = pjsip_inv_end_session(session->
inv_session, response,
NULL, &packet);
3511 if (status == PJ_SUCCESS && packet) {
3519 if (packet->msg->type == PJSIP_RESPONSE_MSG) {
3556 pj_time_val delay = { .sec = 60, };
3632 pjsip_inv_session *
inv_session = pjsip_dlg_get_inv_session(dlg);
3636 !(session = inv_session->mod_data[session_module.id])) {
3668 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
3669 pjsip_sip_uri *sip_ruri;
3673 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
3677 sip_ruri = pjsip_uri_get_uri(ruri);
3688 ast_log(
LOG_ERROR,
"%s: Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n",
3696 if (!strcmp(session->
exten, pickupexten) ||
3711 !strncmp(session->
exten, pickupexten, strlen(session->
exten)) ||
3743 int answer_code,
int terminate_code, pj_bool_t notify)
3745 pjsip_tx_data *tdata =
NULL;
3748 if (inv_session->state != PJSIP_INV_STATE_DISCONNECTED) {
3749 if (pjsip_inv_initial_answer(
3750 inv_session, rdata, answer_code,
NULL,
NULL, &tdata) != PJ_SUCCESS) {
3752 pjsip_inv_terminate(inv_session, terminate_code ? terminate_code : answer_code, notify);
3755 pjsip_inv_send_msg(inv_session, tdata);
3759 if (answer_code >= 300) {
3764 pjsip_dlg_dec_lock(inv_session->dlg);
3791 pjsip_tx_data *tdata;
3793 pjsip_inv_session *inv_session;
3795 pj_status_t dlg_status = PJ_EUNKNOWN;
3800 pjsip_tx_data_dec_ref(tdata);
3810 if (dlg_status != PJ_EEXISTS) {
3821 if (pjsip_inv_create_uas(dlg, rdata,
NULL, options, &inv_session) != PJ_SUCCESS) {
3828 pjsip_dlg_dec_lock(dlg);
3829 pjsip_dlg_terminate(dlg);
3833 #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE) 3834 inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
3836 if (pjsip_dlg_add_usage(dlg, &session_module,
NULL) != PJ_SUCCESS) {
3840 pjsip_dlg_dec_session(inv_session->dlg, &session_module);
3857 pjsip_media_type app_sdp;
3858 pjsip_media_type_init2(&app_sdp,
"application",
"sdp");
3860 if (!pjsip_media_type_cmp(content_type, &app_sdp, 0)) {
3869 pjsip_hdr *hdr = part->hdr.next;
3870 static const pj_str_t str_handling_required = {
"handling=required", 16};
3872 while (hdr != &part->hdr) {
3873 if (hdr->type == PJSIP_H_OTHER) {
3874 pjsip_generic_string_hdr *generic_hdr = (pjsip_generic_string_hdr*)hdr;
3876 if (!pj_stricmp2(&hdr->name,
"Content-Disposition") &&
3877 pj_stristr(&generic_hdr->hvalue, &str_handling_required) &&
3893 pjsip_msg_body *body = rdata->msg_info.msg->body;
3894 pjsip_ctype_hdr *ctype_hdr = rdata->msg_info.ctype;
3896 if (body && ctype_hdr &&
3897 !pj_stricmp2(&ctype_hdr->media.type,
"multipart") &&
3898 (!pj_stricmp2(&ctype_hdr->media.subtype,
"mixed") ||
3899 !pj_stricmp2(&ctype_hdr->media.subtype,
"alternative"))) {
3900 pjsip_multipart_part *part = pjsip_multipart_get_first_part(body);
3901 while (part !=
NULL) {
3905 part = pjsip_multipart_get_next_part(body, part);
3913 pjsip_tx_data *tdata =
NULL;
3914 pjsip_timer_setting
timer;
3915 pjsip_rdata_sdp_info *sdp_info;
3916 pjmedia_sdp_session *local =
NULL;
3938 ast_trace(-1,
"%s: Call (%s:%s) to extension '%s' - unsupported uri\n",
3940 invite->
rdata->tp_info.transport->type_name,
3941 pj_sockaddr_print(&invite->
rdata->pkt_info.src_addr, buffer,
sizeof(buffer), 3),
3950 ast_trace(-1,
"%s: Call (%s:%s) to extension '%s' - partial match\n",
3952 invite->
rdata->tp_info.transport->type_name,
3953 pj_sockaddr_print(&invite->
rdata->pkt_info.src_addr, buffer,
sizeof(buffer), 3),
3964 ast_trace_log(-1,
LOG_NOTICE,
"%s: Call (%s:%s) to extension '%s' rejected because extension not found in context '%s'.\n",
3966 invite->
rdata->tp_info.transport->type_name,
3967 pj_sockaddr_print(&invite->
rdata->pkt_info.src_addr, buffer,
sizeof(buffer), 3),
3988 pjsip_timer_setting_default(&timer);
3998 ast_trace(-1,
"%s: Call (%s:%s) to extension '%s' sending 100 Trying\n",
4000 invite->
rdata->tp_info.transport->type_name,
4001 pj_sockaddr_print(&invite->
rdata->pkt_info.src_addr, buffer,
sizeof(buffer), 3),
4009 sdp_info = pjsip_rdata_get_sdp_info(invite->
rdata);
4010 if (sdp_info && (sdp_info->sdp_err == PJ_SUCCESS) && sdp_info->sdp) {
4037 pjmedia_sdp_neg_set_prefer_remote_codec_order(invite->
session->
inv_session->neg, PJ_FALSE);
4038 #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS 4040 pjmedia_sdp_neg_set_answer_multiple_codecs(invite->
session->
inv_session->neg, PJ_TRUE);
4058 int res =
TRACE_ATLEAST(1) ? pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, req_uri, 256) : 0;
4059 SCOPE_ENTER(1,
"Request: %s\n", res ? req_uri :
"");
4097 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
4123 pjsip_dlg_dec_lock(inv_session->dlg);
4137 pj_cstr(&method, supplement_method);
4139 return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
4145 struct pjsip_method *
method = &rdata->msg_info.msg->line.req.method;
4166 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
4167 pjsip_inv_session *inv_session = (dlg ? pjsip_dlg_get_inv_session(dlg) :
NULL);
4168 struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] :
NULL);
4170 pjsip_tsx_state_str(tsx->state), inv_session ? pjsip_inv_state_name(inv_session->state) :
"unknown");
4173 ast_trace(2,
"Topology: Pending: %s Active: %s\n",
4188 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
4189 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
4190 pjsip_inv_session *inv_session = dlg ? pjsip_dlg_get_inv_session(dlg) :
NULL;
4191 struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] :
NULL);
4193 (
int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
4217 pj_status_t handled = PJ_FALSE;
4218 struct pjsip_request_line req = rdata->msg_info.msg->line.req;
4219 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
4220 pjsip_inv_session *inv_session = (dlg ? pjsip_dlg_get_inv_session(dlg) :
NULL);
4221 struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] :
NULL);
4223 int res =
TRACE_ATLEAST(1) ? pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, req_uri, 256) : 0;
4225 (
int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name), res ? req_uri :
"");
4227 switch (req.method.id) {
4228 case PJSIP_INVITE_METHOD:
4238 handled = dlg && (inv_session = pjsip_dlg_get_inv_session(dlg)) &&
4244 (
int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name), req_uri,
4245 handled == PJ_TRUE ?
"yes" :
"no");
4252 ast_debug(3,
"%s: re-INVITE collision timer expired.\n",
4276 int use_pending = 0;
4304 if (!pending_media_state) {
4311 if (!active_media_state) {
4318 active_media_state, 1)) {
4330 if (inv->role == PJSIP_ROLE_UAC) {
4349 int id = session_module.id;
4358 function, pjsip_event_str(e->type));
4360 ast_log(
LOG_DEBUG,
"Transaction %p does not belong to an inv_session?\n", tsx);
4362 pjsip_tsx_state_str(tsx->state));
4366 session = inv->mod_data[session_module.id];
4375 if (inv->invite_tsx) {
4382 ast_log(
LOG_DEBUG,
"The %s %.*s transaction involved in this state change is %p\n",
4383 pjsip_role_name(tsx->role),
4384 (
int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
4387 pjsip_tsx_state_str(tsx->state));
4389 pjsip_event_str(e->body.tsx_state.type));
4391 ast_log(
LOG_DEBUG,
"There is no transaction involved in this state change\n");
4393 ast_log(
LOG_DEBUG,
"The current inv state is %s\n", pjsip_inv_state_name(inv->state));
4396 #define print_debug_details(inv, tsx, e) __print_debug_details(__PRETTY_FUNCTION__, (inv), (tsx), (e)) 4401 struct pjsip_request_line req = rdata->msg_info.msg->line.req;
4453 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
4455 status.code, (
int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
4472 if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
4484 struct pjsip_request_line req = tdata->msg->line.req;
4486 (
int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
4501 struct pjsip_status_line status = tdata->msg->line.status;
4502 pjsip_cseq_hdr *cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ,
NULL);
4504 (
int) pj_strlen(&cseq->method.name),
4505 pj_strbuf(&cseq->method.name), status.code, (
int) pj_strlen(&status.reason),
4506 pj_strbuf(&status.reason));
4563 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4565 if (tsx->status_code != 503 && tsx->status_code != 408) {
4573 pjsip_inv_uac_restart(inv, PJ_FALSE);
4578 pjsip_tx_data_add_ref(tsx->last_tx);
4588 "request" :
"response");
4592 msg = rdata->msg_info.msg;
4593 if (msg->type == PJSIP_REQUEST_MSG
4594 && msg->line.req.method.id == PJSIP_ACK_METHOD
4595 && pjmedia_sdp_neg_get_state(inv->neg) != PJMEDIA_SDP_NEG_STATE_DONE) {
4596 pjsip_tx_data *tdata;
4605 "%s: Ending session due to incomplete SDP negotiation. %s\n",
4607 pjsip_rx_data_get_info(rdata));
4608 if (pjsip_inv_end_session(inv, 400,
NULL, &tdata) == PJ_SUCCESS
4617 pjsip_event_id_e
type;
4620 pjsip_event_str(e->type), pjsip_inv_state_name(inv->state));
4630 type = PJSIP_EVENT_UNKNOWN;
4633 session = inv->mod_data[session_module.id];
4639 case PJSIP_EVENT_TX_MSG:
4641 case PJSIP_EVENT_RX_MSG:
4644 case PJSIP_EVENT_TSX_STATE:
4646 pjsip_event_str(e->body.tsx_state.type));
4648 switch(e->body.tsx_state.type) {
4649 case PJSIP_EVENT_TX_MSG:
4651 case PJSIP_EVENT_RX_MSG:
4656 case PJSIP_EVENT_TRANSPORT_ERROR:
4657 case PJSIP_EVENT_TIMER:
4665 case PJSIP_EVENT_USER:
4666 case PJSIP_EVENT_UNKNOWN:
4667 case PJSIP_EVENT_TSX_STATE:
4672 case PJSIP_EVENT_TRANSPORT_ERROR:
4673 case PJSIP_EVENT_TIMER:
4674 case PJSIP_EVENT_UNKNOWN:
4675 case PJSIP_EVENT_USER:
4680 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
4705 if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
4713 pjsip_dlg_inc_lock(inv->dlg);
4714 session = inv->mod_data[
id];
4715 inv->mod_data[
id] =
NULL;
4716 pjsip_dlg_dec_lock(inv->dlg);
4734 int id = session_module.id;
4735 pjsip_tx_data *tdata;
4738 pjsip_tsx_state_str(tsx->state), pjsip_inv_state_name(inv->state));
4744 session = inv->mod_data[
id];
4762 switch (e->body.tsx_state.type) {
4763 case PJSIP_EVENT_TX_MSG:
4769 tsx->mod_data[
id] = e->body.tsx_state.src.tdata->mod_data[
id];
4771 case PJSIP_EVENT_RX_MSG:
4778 if ((e->body.tsx_state.src.rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) ||
4779 (tsx->method.id != PJSIP_BYE_METHOD)) {
4783 if (tsx->method.id == PJSIP_INVITE_METHOD) {
4784 if (tsx->role == PJSIP_ROLE_UAC) {
4785 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
4787 if (tsx->status_code == PJSIP_SC_REQUEST_PENDING) {
4791 if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
4792 ast_debug(1,
"%s: reINVITE received final response code %d\n",
4795 if ((tsx->status_code == 401 || tsx->status_code == 407)
4799 e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
4808 if (tsx->status_code == 481 || tsx->status_code == 408) {
4809 if (pjsip_inv_end_session(inv, 500,
NULL, &tdata) == PJ_SUCCESS
4815 }
else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
4816 if (!inv->cancelling
4817 && inv->role == PJSIP_ROLE_UAC
4818 && inv->state == PJSIP_INV_STATE_CONFIRMED
4819 && pjmedia_sdp_neg_was_answer_remote(inv->neg)
4820 && pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE
4830 ast_debug(1,
"Endpoint '%s(%s)': Ending session due to 200 OK with incomplete SDP negotiation. %s\n",
4833 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
4838 if (inv->cancelling && tsx->status_code == PJSIP_SC_OK) {
4839 int sdp_negotiation_done =
4840 pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE;
4864 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata),
4865 sdp_negotiation_done ?
"complete" :
"incomplete");
4866 if (!sdp_negotiation_done) {
4867 ast_debug(1,
"%s: Incomplete SDP negotiation cancelled session. %s\n",
4869 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
4870 }
else if (pjsip_inv_end_session(inv, 500,
NULL, &tdata) == PJ_SUCCESS
4872 ast_debug(1,
"%s: Ending session due to RFC5407 race condition. %s\n",
4874 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
4882 if (tsx->role == PJSIP_ROLE_UAC) {
4883 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
4885 ast_debug(1,
"%s: %.*s received final response code %d\n",
4887 (
int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
4889 if ((tsx->status_code == 401 || tsx->status_code == 407)
4893 e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
4897 (
int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name));
4903 cb(session, e->body.tsx_state.src.rdata);
4906 case PJSIP_EVENT_TRANSPORT_ERROR:
4907 case PJSIP_EVENT_TIMER:
4916 case PJSIP_EVENT_USER:
4917 case PJSIP_EVENT_UNKNOWN:
4918 case PJSIP_EVENT_TSX_STATE:
4928 if (tsx->method.id == PJSIP_INVITE_METHOD) {
4929 if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
4930 ast_debug(3,
"%s: INVITE delay check. tsx-state:%s\n",
4932 pjsip_tsx_state_str(tsx->state));
4934 }
else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
4940 ast_debug(3,
"%s: INVITE delay check. tsx-state:%s\n",
4942 pjsip_tsx_state_str(tsx->state));
4945 }
else if (tsx->role == PJSIP_ROLE_UAC
4946 && tsx->state == PJSIP_TSX_STATE_COMPLETED
4947 && !pj_strcmp2(&tsx->method.name,
"UPDATE")) {
4948 ast_debug(3,
"%s: UPDATE delay check. tsx-state:%s\n",
4950 pjsip_tsx_state_str(tsx->state));
4959 const struct pjmedia_sdp_session *remote,