res_pjsip: Use reasonable buffer lengths for endpoint identification
[asterisk/asterisk.git] / res / res_pjsip_caller_id.c
index 16b19ec..64191a7 100644 (file)
@@ -149,12 +149,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 && !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 {
+       if (!privacy || !pj_stricmp2(&privacy->hvalue, "none")) {
                id->number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
                id->name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
+       } else {
+               id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
+               id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
        }
 
        return 0;
@@ -436,7 +436,7 @@ static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_fromt
        id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
        id_uri = pjsip_uri_get_uri(id_name_addr->uri);
 
-       if (id->name.valid) {
+       if (id->name.valid && !ast_strlen_zero(id->name.str)) {
                int name_buf_len = strlen(id->name.str) * 2 + 1;
                char *name_buf = ast_alloca(name_buf_len);
 
@@ -450,7 +450,12 @@ static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_fromt
                pj_strdup2(tdata->pool, &id_name_addr->display, NULL);
        }
 
-       pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
+       if (id->number.valid) {
+               pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
+       } else {
+               /* Similar to name, make sure the number is also cleared when invalid */
+               pj_strdup2(tdata->pool, &id_uri->user, NULL);
+       }
 
        id_hdr->uri = (pjsip_uri *) id_name_addr;
        return id_hdr;
@@ -523,8 +528,11 @@ static void add_pai_header(const struct ast_sip_session *session, pjsip_tx_data
                }
        }
 
-       base = tdata->msg->type == PJSIP_REQUEST_MSG ? session->saved_from_hdr :
-               PJSIP_MSG_TO_HDR(tdata->msg);
+       if (tdata->msg->type == PJSIP_REQUEST_MSG) {
+               base = session->saved_from_hdr ? session->saved_from_hdr : PJSIP_MSG_FROM_HDR(tdata->msg);
+       } else {
+               base = PJSIP_MSG_TO_HDR(tdata->msg);
+       }
 
        pai_hdr = create_new_id_hdr(&pj_pai_name, base, tdata, id);
        if (!pai_hdr) {
@@ -629,8 +637,11 @@ static void add_rpid_header(const struct ast_sip_session *session, pjsip_tx_data
                }
        }
 
-       base = tdata->msg->type == PJSIP_REQUEST_MSG ? session->saved_from_hdr :
-               PJSIP_MSG_TO_HDR(tdata->msg);
+       if (tdata->msg->type == PJSIP_REQUEST_MSG) {
+               base = session->saved_from_hdr ? session->saved_from_hdr : PJSIP_MSG_FROM_HDR(tdata->msg);
+       } else {
+               base = PJSIP_MSG_TO_HDR(tdata->msg);
+       }
 
        rpid_hdr = create_new_id_hdr(&pj_rpid_name, base, tdata, id);
        if (!rpid_hdr) {