res_pjsip: Use reasonable buffer lengths for endpoint identification
[asterisk/asterisk.git] / res / res_pjsip_caller_id.c
index 283ec94..64191a7 100644 (file)
@@ -46,11 +46,29 @@ static void set_id_from_hdr(pjsip_fromto_hdr *hdr, struct ast_party_id *id)
        char cid_num[AST_CHANNEL_NAME];
        pjsip_sip_uri *uri;
        pjsip_name_addr *id_name_addr = (pjsip_name_addr *) hdr->uri;
+       char *semi;
 
        uri = pjsip_uri_get_uri(id_name_addr);
        ast_copy_pj_str(cid_name, &id_name_addr->display, sizeof(cid_name));
        ast_copy_pj_str(cid_num, &uri->user, sizeof(cid_num));
 
+       /* Always truncate caller-id number at a semicolon. */
+       semi = strchr(cid_num, ';');
+       if (semi) {
+               /*
+                * We need to be able to handle URI's looking like
+                * "sip:1235557890;phone-context=national@x.x.x.x;user=phone"
+                *
+                * Where the uri->user field will result in:
+                * "1235557890;phone-context=national"
+                *
+                * People don't care about anything after the semicolon
+                * showing up on their displays even though the RFC
+                * allows the semicolon.
+                */
+               *semi = '\0';
+       }
+
        ast_free(id->name.str);
        id->name.str = ast_strdup(cid_name);
        if (!ast_strlen_zero(cid_name)) {
@@ -131,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;
@@ -413,12 +431,12 @@ static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_fromt
        id_hdr = pjsip_from_hdr_create(tdata->pool);
        id_hdr->type = PJSIP_H_OTHER;
        pj_strdup(tdata->pool, &id_hdr->name, hdr_name);
-       id_hdr->sname.slen = 0;
+       id_hdr->sname = id_hdr->name;
 
        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);
 
@@ -432,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;
@@ -505,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) {
@@ -611,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) {