chan_pjsip: Fix crash on reINVITE before initial INVITE completes.
[asterisk/asterisk.git] / res / res_pjsip_caller_id.c
index 5996f13..07b1266 100644 (file)
@@ -131,12 +131,12 @@ static int set_id_from_pai(pjsip_rx_data *rdata, struct ast_party_id *id)
        }
 
        privacy = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &privacy_str, NULL);
-       if (!privacy) {
-               return 0;
-       }
-       if (!pj_stricmp2(&privacy->hvalue, "id")) {
+       if (privacy && !pj_stricmp2(&privacy->hvalue, "id")) {
                id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
                id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
+       } else {
+               id->number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
+               id->name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
        }
 
        return 0;
@@ -176,12 +176,18 @@ static int set_id_from_rpid(pjsip_rx_data *rdata, struct ast_party_id *id)
        privacy = pjsip_param_find(&rpid_hdr->other_param, &privacy_str);
        screen = pjsip_param_find(&rpid_hdr->other_param, &screen_str);
        if (privacy && !pj_stricmp2(&privacy->value, "full")) {
-               id->number.presentation |= AST_PRES_RESTRICTED;
-               id->name.presentation |= AST_PRES_RESTRICTED;
+               id->number.presentation = AST_PRES_RESTRICTED;
+               id->name.presentation = AST_PRES_RESTRICTED;
+       } else {
+               id->number.presentation = AST_PRES_ALLOWED;
+               id->name.presentation = AST_PRES_ALLOWED;
        }
        if (screen && !pj_stricmp2(&screen->value, "yes")) {
                id->number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
                id->name.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
+       } else {
+               id->number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
+               id->name.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
        }
 
        return 0;
@@ -281,21 +287,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 +329,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);
 }
 
@@ -342,10 +351,17 @@ 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))) {
+       if (!session->channel) {
+               /*
+                * Since we have no channel this must be the 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);
@@ -353,8 +369,9 @@ static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_
                        set_id_from_from(rdata, &session->id);
                }
        } else {
-               /* Reinvite. Check for changes to the ID and queue a connected line
-                * update if necessary
+               /*
+                * ReINVITE or UPDATE.  Check for changes to the ID and queue
+                * a connected line update if necessary.
                 */
                update_incoming_connected_line(session, rdata);
        }
@@ -395,7 +412,11 @@ static void modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const st
        id_uri = pjsip_uri_get_uri(id_name_addr->uri);
 
        if (id->name.valid) {
-               pj_strdup2(pool, &id_name_addr->display, id->name.str);
+               int name_buf_len = strlen(id->name.str) * 2 + 1;
+               char *name_buf = ast_alloca(name_buf_len);
+
+               ast_escape_quoted(id->name.str, name_buf, name_buf_len);
+               pj_strdup2(pool, &id_name_addr->display, name_buf);
        }
 
        if (id->number.valid) {
@@ -429,7 +450,11 @@ static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_tx_da
        id_uri = pjsip_uri_get_uri(id_name_addr->uri);
 
        if (id->name.valid) {
-               pj_strdup2(tdata->pool, &id_name_addr->display, id->name.str);
+               int name_buf_len = strlen(id->name.str) * 2 + 1;
+               char *name_buf = ast_alloca(name_buf_len);
+
+               ast_escape_quoted(id->name.str, name_buf, name_buf_len);
+               pj_strdup2(tdata->pool, &id_name_addr->display, name_buf);
        }
 
        pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
@@ -458,17 +483,14 @@ static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *
 
        old_privacy = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_privacy_name, NULL);
 
-       if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
-                       (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) {
-               if (!old_privacy) {
-                       pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
-                                       tdata->pool, &pj_privacy_name, &pj_privacy_value);
-                       pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
-               }
-       } else {
+       if ((ast_party_id_presentation(id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
                if (old_privacy) {
                        pj_list_erase(old_privacy);
                }
+       } else if (!old_privacy) {
+               pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
+                               tdata->pool, &pj_privacy_name, &pj_privacy_value);
+               pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
        }
 }
 
@@ -484,10 +506,6 @@ static void add_pai_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
        pjsip_fromto_hdr *pai_hdr;
        pjsip_fromto_hdr *old_pai;
 
-       if (!id->number.valid) {
-               return;
-       }
-
        /* Since inv_session reuses responses, we have to make sure there's not already
         * a P-Asserted-Identity present. If there is, we just modify the old one.
         */
@@ -531,6 +549,7 @@ static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, cons
        pjsip_param *old_screen;
        pjsip_param *privacy;
        pjsip_param *screen;
+       int presentation;
 
        old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
        old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
@@ -551,15 +570,13 @@ static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, cons
                screen = old_screen;
        }
 
-       if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
-                       (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
+       presentation = ast_party_id_presentation(id);
+       if ((presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
                privacy->value = privacy_off_str;
        } else {
                privacy->value = privacy_full_str;
        }
-
-       if ((id->name.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN &&
-                       (id->number.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN) {
+       if ((presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN) {
                screen->value = screen_yes_str;
        } else {
                screen->value = screen_no_str;
@@ -578,10 +595,6 @@ static void add_rpid_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
        pjsip_fromto_hdr *rpid_hdr;
        pjsip_fromto_hdr *old_rpid;
 
-       if (!id->number.valid) {
-               return;
-       }
-
        /* Since inv_session reuses responses, we have to make sure there's not already
         * a P-Asserted-Identity present. If there is, we just modify the old one.
         */
@@ -613,9 +626,9 @@ static void add_rpid_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
  */
 static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
 {
-       if (((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
-                       (id->number.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) &&
-                       !session->endpoint->id.trust_outbound) {
+       if (!id->number.valid
+               || (!session->endpoint->id.trust_outbound
+                       && (ast_party_id_presentation(id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
                return;
        }
        if (session->endpoint->id.send_pai) {
@@ -640,18 +653,21 @@ 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);
-       if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED &&
-                       ast_strlen_zero(session->endpoint->fromuser) &&
-                       (session->endpoint->id.trust_outbound ||
-                       ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
-                       (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
+       /* 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) {
                /* Only change the From header on the initial outbound INVITE. Switching it
                 * mid-call might confuse some UAs.
                 */
@@ -661,10 +677,18 @@ static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx
                from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, tdata->msg->hdr.next);
                dlg = session->inv_session->dlg;
 
-               modify_id_header(tdata->pool, from, &connected_id);
-               modify_id_header(dlg->pool, dlg->local.info, &connected_id);
+               if (ast_strlen_zero(session->endpoint->fromuser)
+                       && (session->endpoint->id.trust_outbound
+                               || (ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED)) {
+                       modify_id_header(tdata->pool, from, &connected_id);
+                       modify_id_header(dlg->pool, dlg->local.info, &connected_id);
+               }
+
+               ast_sip_add_usereqphone(session->endpoint, tdata->pool, from->uri);
+               ast_sip_add_usereqphone(session->endpoint, dlg->pool, dlg->local.info->uri);
        }
        add_id_headers(session, tdata, &connected_id);
+       ast_party_id_free(&connected_id);
 }
 
 /*!
@@ -678,13 +702,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 = {
@@ -698,6 +731,8 @@ static struct ast_sip_session_supplement caller_id_supplement = {
 
 static int load_module(void)
 {
+       CHECK_PJSIP_SESSION_MODULE_LOADED();
+
        ast_sip_session_register_supplement(&caller_id_supplement);
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -709,7 +744,8 @@ static int unload_module(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_APP_DEPEND,
-              );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND,
+);