diff --git a/channels/chan_pjsip.c b/channels/chan_pjsip.c index c958f8086441bbdbb215f408b89a9c845672ea41..ac7543c3e4230faa7367f60bbe7f1b6baf835624 100644 --- a/channels/chan_pjsip.c +++ b/channels/chan_pjsip.c @@ -381,11 +381,13 @@ static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int s return NULL; } - chan = ast_channel_alloc_with_endpoint(1, state, S_OR(session->id.number.str, ""), - S_OR(session->id.name.str, ""), session->endpoint->accountcode, "", - "", assignedids, requestor, 0, session->endpoint->persistent, - "PJSIP/%s-%08x", ast_sorcery_object_get_id(session->endpoint), - (unsigned)ast_atomic_fetchadd_int((int *)&chan_idx, +1)); + chan = ast_channel_alloc_with_endpoint(1, state, + S_COR(session->id.number.valid, session->id.number.str, ""), + S_COR(session->id.name.valid, session->id.name.str, ""), + session->endpoint->accountcode, "", "", assignedids, requestor, 0, + session->endpoint->persistent, "PJSIP/%s-%08x", + ast_sorcery_object_get_id(session->endpoint), + (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1)); if (!chan) { ao2_ref(caps, -1); return NULL; @@ -432,6 +434,9 @@ static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int s ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE); + ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id); + ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id); + ast_channel_context_set(chan, session->endpoint->context); ast_channel_exten_set(chan, S_OR(exten, "s")); ast_channel_priority_set(chan, 1); @@ -1042,7 +1047,6 @@ static int transmit_info_with_vidupdate(void *data) static int update_connected_line_information(void *data) { RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup); - struct ast_party_id connected_id; if ((ast_channel_state(session->channel) != AST_STATE_UP) && (session->inv_session->role == PJSIP_UAS_ROLE)) { int response_code = 0; @@ -1062,12 +1066,20 @@ static int update_connected_line_information(void *data) } } else { enum ast_sip_session_refresh_method method = session->endpoint->id.refresh_method; + struct ast_party_id connected_id; if (session->inv_session->invite_tsx && (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE)) { method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE; } + /* + * We can get away with a shallow copy here because we are + * not looking at strings. + */ + ast_channel_lock(session->channel); connected_id = ast_channel_connected_effective_id(session->channel); + ast_channel_unlock(session->channel); + if ((session->endpoint->id.send_pai || session->endpoint->id.send_rpid) && (session->endpoint->id.trust_outbound || ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED && @@ -1492,33 +1504,26 @@ static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned in static void update_initial_connected_line(struct ast_sip_session *session) { struct ast_party_connected_line connected; - struct ast_set_party_connected_line update_connected; - struct ast_sip_endpoint_id_configuration *id = &session->endpoint->id; - if (!id->self.number.valid && !id->self.name.valid) { + /* + * Use the channel CALLERID() as the initial connected line data. + * The core or a predial handler may have supplied missing values + * from the session->endpoint->id.self about who we are calling. + */ + ast_channel_lock(session->channel); + ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id); + ast_channel_unlock(session->channel); + + /* Supply initial connected line information if available. */ + if (!session->id.number.valid && !session->id.name.valid) { return; } - /* Supply initial connected line information if available. */ - memset(&update_connected, 0, sizeof(update_connected)); ast_party_connected_line_init(&connected); - connected.id.number = id->self.number; - connected.id.name = id->self.name; - connected.id.tag = id->self.tag; + connected.id = session->id; connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER; - if (connected.id.number.valid) { - update_connected.id.number = 1; - } - - if (connected.id.name.valid) { - update_connected.id.name = 1; - } - - /* Invalidate any earlier private connected id representation */ - ast_set_party_id_all(&update_connected.priv); - - ast_channel_queue_connected_line_update(session->channel, &connected, &update_connected); + ast_channel_queue_connected_line_update(session->channel, &connected, NULL); } static int call(void *data) @@ -1549,7 +1554,7 @@ static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeou ao2_ref(channel, +1); if (ast_sip_push_task(channel->session->serializer, call, channel)) { - ast_log(LOG_WARNING, "Error attempting to place outbound call to call '%s'\n", dest); + ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest); ao2_cleanup(channel); return -1; } diff --git a/res/res_pjsip_caller_id.c b/res/res_pjsip_caller_id.c index 7ab5b8c043174e62977bdc66aa60fd28bdfa2e22..cffb45342ac6dab8b87f6745de1c2bb297354bd2 100644 --- a/res/res_pjsip_caller_id.c +++ b/res/res_pjsip_caller_id.c @@ -281,21 +281,26 @@ static int should_queue_connected_line_update(const struct ast_sip_session *sess static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id) { struct ast_party_connected_line connected; - struct ast_set_party_connected_line update_connected; + struct ast_party_caller caller; + /* Fill connected line information */ ast_party_connected_line_init(&connected); - ast_party_id_copy(&connected.id, id); - - memset(&update_connected, 0, sizeof(update_connected)); - update_connected.id.number = 1; - update_connected.id.name = 1; - - ast_set_party_id_all(&update_connected.priv); + connected.id = *id; + connected.id.tag = session->endpoint->id.self.tag; connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER; + + /* Save to channel driver copy */ ast_party_id_copy(&session->id, &connected.id); - ast_channel_queue_connected_line_update(session->channel, &connected, &update_connected); - ast_party_connected_line_free(&connected); + /* Update our channel CALLERID() */ + ast_party_caller_init(&caller); + caller.id = connected.id; + caller.ani = connected.id; + caller.ani2 = ast_channel_caller(session->channel)->ani2; + ast_channel_set_caller_event(session->channel, &caller, NULL); + + /* Tell peer about the new connected line information. */ + ast_channel_queue_connected_line_update(session->channel, &connected, NULL); } /*! @@ -318,13 +323,11 @@ static void update_incoming_connected_line(struct ast_sip_session *session, pjsi } ast_party_id_init(&id); - if (set_id_from_pai(rdata, &id) && set_id_from_rpid(rdata, &id)) { - return; - } - if (should_queue_connected_line_update(session, &id)) { - queue_connected_line_update(session, &id); + if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) { + if (should_queue_connected_line_update(session, &id)) { + queue_connected_line_update(session, &id); + } } - ast_party_id_free(&id); } @@ -343,9 +346,15 @@ static void update_incoming_connected_line(struct ast_sip_session *session, pjsi static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata) { if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED) { - /* Initial inbound INVITE. Set the session ID directly */ - if (session->endpoint->id.trust_inbound && - (!set_id_from_pai(rdata, &session->id) || !set_id_from_rpid(rdata, &session->id))) { + /* + * Initial inbound INVITE. Set the session ID directly + * because the channel has not been created yet. + */ + if (session->endpoint->id.trust_inbound + && (!set_id_from_pai(rdata, &session->id) + || !set_id_from_rpid(rdata, &session->id))) { + ast_free(session->id.tag); + session->id.tag = ast_strdup(session->endpoint->id.self.tag); return 0; } ast_party_id_copy(&session->id, &session->endpoint->id.self); @@ -640,13 +649,20 @@ static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data */ static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata) { + struct ast_party_id effective_id; struct ast_party_id connected_id; if (!session->channel) { return; } - connected_id = ast_channel_connected_effective_id(session->channel); + /* Must do a deep copy unless we hold the channel lock the entire time. */ + ast_party_id_init(&connected_id); + ast_channel_lock(session->channel); + effective_id = ast_channel_connected_effective_id(session->channel); + ast_party_id_copy(&connected_id, &effective_id); + ast_channel_unlock(session->channel); + if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED && ast_strlen_zero(session->endpoint->fromuser) && (session->endpoint->id.trust_outbound || @@ -665,6 +681,7 @@ static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx modify_id_header(dlg->pool, dlg->local.info, &connected_id); } add_id_headers(session, tdata, &connected_id); + ast_party_id_free(&connected_id); } /*! @@ -678,13 +695,22 @@ static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx */ static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata) { + struct ast_party_id effective_id; struct ast_party_id connected_id; if (!session->channel) { return; } - connected_id = ast_channel_connected_effective_id(session->channel); + + /* Must do a deep copy unless we hold the channel lock the entire time. */ + ast_party_id_init(&connected_id); + ast_channel_lock(session->channel); + effective_id = ast_channel_connected_effective_id(session->channel); + ast_party_id_copy(&connected_id, &effective_id); + ast_channel_unlock(session->channel); + add_id_headers(session, tdata, &connected_id); + ast_party_id_free(&connected_id); } static struct ast_sip_session_supplement caller_id_supplement = { diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c index b161f0361f639d2b53ce579509134bfd32100a44..b85b457c70842bfb985747f3e912e474bf25fc17 100644 --- a/res/res_pjsip_session.c +++ b/res/res_pjsip_session.c @@ -1272,6 +1272,7 @@ struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint pjsip_inv_terminate(inv_session, 500, PJ_FALSE); return NULL; } + ast_party_id_copy(&session->id, &endpoint->id.self); if (ast_format_cap_count(req_caps)) { /* get joint caps between req_caps and endpoint caps */