chan_pjsip: Fix attended transfer connected line name update.
authorRichard Mudgett <rmudgett@digium.com>
Tue, 19 Aug 2014 16:16:03 +0000 (16:16 +0000)
committerRichard Mudgett <rmudgett@digium.com>
Tue, 19 Aug 2014 16:16:03 +0000 (16:16 +0000)
A calls B
B answers
B SIP attended transfers to C
C answers, B and C can see each other's connected line information
B completes the transfer
A has number but no name connected line information about C
  while C has the full information about A

I examined the incoming and outgoing party id information handling of
chan_pjsip and found several issues:

* Fixed ast_sip_session_create_outgoing() not setting up the configured
endpoint id as the new channel's caller id.  This is why party A got
default connected line information.

* Made update_initial_connected_line() use the channel's CALLERID(id)
information.  The core, app_dial, or predial routine may have filled in or
changed the endpoint caller id information.

* Fixed chan_pjsip_new() not setting the full party id information
available on the caller id and ANI party id.  This includes the configured
callerid_tag string and other party id fields.

* Fixed accessing channel party id information without the channel lock
held.

* Fixed using the effective connected line id without doing a deep copy
outside of holding the channel lock.  Shallow copy string pointers can
become stale if the channel lock is not held.

* Made queue_connected_line_update() also update the channel's
CALLERID(id) information.  Moving the channel to another bridge would need
the information there for the new bridge peer.

* Fixed off nominal memory leak in update_incoming_connected_line().

* Added pjsip.conf callerid_tag string to party id information from
enabled trust_inbound endpoint in caller_id_incoming_request().

AFS-98 #close
Reported by: Mark Michelson

Review: https://reviewboard.asterisk.org/r/3913/
........

Merged revisions 421400 from http://svn.asterisk.org/svn/asterisk/branches/12
........

Merged revisions 421403 from http://svn.asterisk.org/svn/asterisk/branches/13

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@421404 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_pjsip.c
res/res_pjsip_caller_id.c
res/res_pjsip_session.c

index c958f80..ac7543c 100644 (file)
@@ -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;
        }
index 7ab5b8c..cffb453 100644 (file)
@@ -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 = {
index b161f03..b85b457 100644 (file)
@@ -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 */