2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_session</depend>
23 <support_level>core</support_level>
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/module.h"
35 #include "asterisk/callerid.h"
39 * \brief Set an ast_party_id name and number based on an identity header.
40 * \param hdr From, P-Asserted-Identity, or Remote-Party-ID header on incoming message
41 * \param[out] id The ID to set data on
43 static void set_id_from_hdr(pjsip_fromto_hdr *hdr, struct ast_party_id *id)
45 char cid_name[AST_CHANNEL_NAME];
46 char cid_num[AST_CHANNEL_NAME];
48 pjsip_name_addr *id_name_addr = (pjsip_name_addr *) hdr->uri;
50 uri = pjsip_uri_get_uri(id_name_addr);
51 ast_copy_pj_str(cid_name, &id_name_addr->display, sizeof(cid_name));
52 ast_copy_pj_str(cid_num, &uri->user, sizeof(cid_num));
54 ast_free(id->name.str);
55 id->name.str = ast_strdup(cid_name);
56 if (!ast_strlen_zero(cid_name)) {
59 ast_free(id->number.str);
60 id->number.str = ast_strdup(cid_num);
61 if (!ast_strlen_zero(cid_num)) {
68 * \brief Get a P-Asserted-Identity or Remote-Party-ID header from an incoming message
70 * This function will parse the header as if it were a From header. This allows for us
71 * to easily manipulate the URI, as well as add, modify, or remove parameters from the
74 * \param rdata The incoming message
75 * \param header_name The name of the ID header to find
76 * \retval NULL No ID header present or unable to parse ID header
77 * \retval non-NULL The parsed ID header
79 static pjsip_fromto_hdr *get_id_header(pjsip_rx_data *rdata, const pj_str_t *header_name)
81 static const pj_str_t from = { "From", 4 };
82 pj_str_t header_content;
83 pjsip_fromto_hdr *parsed_hdr;
84 pjsip_generic_string_hdr *ident = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
92 pj_strdup_with_null(rdata->tp_info.pool, &header_content, &ident->hvalue);
94 parsed_hdr = pjsip_parse_hdr(rdata->tp_info.pool, &from, header_content.ptr,
95 pj_strlen(&header_content), &parsed_len);
106 * \brief Set an ast_party_id structure based on data in a P-Asserted-Identity header
108 * This makes use of \ref set_id_from_hdr for setting name and number. It uses
109 * the contents of a Privacy header in order to set presentation information.
111 * \param rdata The incoming message
112 * \param[out] id The ID to set
113 * \retval 0 Successfully set the party ID
114 * \retval non-zero Could not set the party ID
116 static int set_id_from_pai(pjsip_rx_data *rdata, struct ast_party_id *id)
118 static const pj_str_t pai_str = { "P-Asserted-Identity", 19 };
119 static const pj_str_t privacy_str = { "Privacy", 7 };
120 pjsip_fromto_hdr *pai_hdr = get_id_header(rdata, &pai_str);
121 pjsip_generic_string_hdr *privacy;
127 set_id_from_hdr(pai_hdr, id);
129 if (!id->number.valid) {
133 privacy = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &privacy_str, NULL);
137 if (!pj_stricmp2(&privacy->hvalue, "id")) {
138 id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
139 id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
147 * \brief Set an ast_party_id structure based on data in a Remote-Party-ID header
149 * This makes use of \ref set_id_from_hdr for setting name and number. It uses
150 * the privacy and screen parameters in order to set presentation information.
152 * \param rdata The incoming message
153 * \param[out] id The ID to set
154 * \retval 0 Succesfully set the party ID
155 * \retval non-zero Could not set the party ID
157 static int set_id_from_rpid(pjsip_rx_data *rdata, struct ast_party_id *id)
159 static const pj_str_t rpid_str = { "Remote-Party-ID", 15 };
160 static const pj_str_t privacy_str = { "privacy", 7 };
161 static const pj_str_t screen_str = { "screen", 6 };
162 pjsip_fromto_hdr *rpid_hdr = get_id_header(rdata, &rpid_str);
164 pjsip_param *privacy;
170 set_id_from_hdr(rpid_hdr, id);
172 if (!id->number.valid) {
176 privacy = pjsip_param_find(&rpid_hdr->other_param, &privacy_str);
177 screen = pjsip_param_find(&rpid_hdr->other_param, &screen_str);
178 if (privacy && !pj_stricmp2(&privacy->value, "full")) {
179 id->number.presentation |= AST_PRES_RESTRICTED;
180 id->name.presentation |= AST_PRES_RESTRICTED;
182 if (screen && !pj_stricmp2(&screen->value, "yes")) {
183 id->number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
184 id->name.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
192 * \brief Set an ast_party_id structure based on data in a From
194 * This makes use of \ref set_id_from_hdr for setting name and number. It uses
195 * no information from the message in order to set privacy. It relies on endpoint
196 * configuration for privacy information.
198 * \param rdata The incoming message
199 * \param[out] id The ID to set
200 * \retval 0 Succesfully set the party ID
201 * \retval non-zero Could not set the party ID
203 static int set_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
205 pjsip_fromto_hdr *from = pjsip_msg_find_hdr(rdata->msg_info.msg,
206 PJSIP_H_FROM, rdata->msg_info.msg->hdr.next);
209 /* This had better not happen */
213 set_id_from_hdr(from, id);
215 if (!id->number.valid) {
224 * \brief Determine if a connected line update should be queued
226 * This uses information about the session and the ID that would be queued
227 * in the connected line update in order to determine if we should queue
228 * a connected line update.
230 * \param session The session whose channel we wish to queue the connected line update on
231 * \param id The identification information that would be queued on the connected line update
232 * \retval 0 We should not queue a connected line update
233 * \retval non-zero We should queue a connected line update
235 static int should_queue_connected_line_update(const struct ast_sip_session *session, const struct ast_party_id *id)
237 /* Invalid number means no update */
238 if (!id->number.valid) {
242 /* If the session has never communicated an update or if the
243 * new ID has a different number than the session, then we
244 * should queue an update
246 if (ast_strlen_zero(session->id.number.str) ||
247 strcmp(session->id.number.str, id->number.str)) {
251 /* By making it to this point, it means the number is not enough
252 * to determine if an update should be sent. Now we look at
256 /* If the number couldn't warrant an update and the name is
257 * invalid, then no update
259 if (!id->name.valid) {
263 /* If the name has changed or we don't have a name set for the
264 * session, then we should send an update
266 if (ast_strlen_zero(session->id.name.str) ||
267 strcmp(session->id.name.str, id->name.str)) {
271 /* Neither the name nor the number have changed. No update */
277 * \brief Queue a connected line update on a session's channel.
278 * \param session The session whose channel should have the connected line update queued upon.
279 * \param id The identification information to place in the connected line update
281 static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id)
283 struct ast_party_connected_line connected;
284 struct ast_set_party_connected_line update_connected;
286 ast_party_connected_line_init(&connected);
287 ast_party_id_copy(&connected.id, id);
289 memset(&update_connected, 0, sizeof(update_connected));
290 update_connected.id.number = 1;
291 update_connected.id.name = 1;
293 ast_set_party_id_all(&update_connected.priv);
294 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
295 ast_party_id_copy(&session->id, &connected.id);
296 ast_channel_queue_connected_line_update(session->channel, &connected, &update_connected);
298 ast_party_connected_line_free(&connected);
303 * \brief Make updates to connected line information based on an incoming request.
305 * This will get identity information from an incoming request. Once the identification is
306 * retrieved, we will check if the new information warrants a connected line update and queue
307 * a connected line update if so.
309 * \param session The session on which we received an incoming request
310 * \param rdata The incoming request
312 static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
314 struct ast_party_id id;
316 if (!session->endpoint->id.trust_inbound) {
320 ast_party_id_init(&id);
321 if (set_id_from_pai(rdata, &id) && set_id_from_rpid(rdata, &id)) {
324 if (should_queue_connected_line_update(session, &id)) {
325 queue_connected_line_update(session, &id);
328 ast_party_id_free(&id);
333 * \brief Session supplement callback on an incoming INVITE request
335 * If we are receiving an initial INVITE, then we will set the session's identity
336 * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
337 * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
340 * \param session The session that has received an INVITE
341 * \param rdata The incoming INVITE
343 static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
345 if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED) {
346 /* Initial inbound INVITE. Set the session ID directly */
347 if (session->endpoint->id.trust_inbound &&
348 (!set_id_from_pai(rdata, &session->id) || !set_id_from_rpid(rdata, &session->id))) {
351 ast_party_id_copy(&session->id, &session->endpoint->id.self);
352 if (!session->endpoint->id.self.number.valid) {
353 set_id_from_from(rdata, &session->id);
356 /* Reinvite. Check for changes to the ID and queue a connected line
357 * update if necessary
359 update_incoming_connected_line(session, rdata);
366 * \brief Session supplement callback on INVITE response
368 * INVITE responses could result in queuing connected line updates.
370 * \param session The session on which communication is happening
371 * \param rdata The incoming INVITE response
373 static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
375 if (!session->channel) {
379 update_incoming_connected_line(session, rdata);
384 * \brief Set name and number information on an identity header.
385 * \param pool Memory pool to use for string duplication
386 * \param id_hdr A From, P-Asserted-Identity, or Remote-Party-ID header to modify
387 * \param id The identity information to apply to the header
389 static void modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const struct ast_party_id *id)
391 pjsip_name_addr *id_name_addr;
392 pjsip_sip_uri *id_uri;
394 id_name_addr = (pjsip_name_addr *) id_hdr->uri;
395 id_uri = pjsip_uri_get_uri(id_name_addr->uri);
397 if (id->name.valid) {
398 pj_strdup2(pool, &id_name_addr->display, id->name.str);
401 if (id->number.valid) {
402 pj_strdup2(pool, &id_uri->user, id->number.str);
408 * \brief Create an identity header for an outgoing message
409 * \param hdr_name The name of the header to create
410 * \param tdata The message to place the header on
411 * \param id The identification information for the new header
412 * \return newly-created header
414 static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_tx_data *tdata, const struct ast_party_id *id)
416 pjsip_fromto_hdr *id_hdr;
417 pjsip_fromto_hdr *base;
418 pjsip_name_addr *id_name_addr;
419 pjsip_sip_uri *id_uri;
421 base = tdata->msg->type == PJSIP_REQUEST_MSG ? PJSIP_MSG_FROM_HDR(tdata->msg) :
422 PJSIP_MSG_TO_HDR(tdata->msg);
423 id_hdr = pjsip_from_hdr_create(tdata->pool);
424 id_hdr->type = PJSIP_H_OTHER;
425 pj_strdup(tdata->pool, &id_hdr->name, hdr_name);
426 id_hdr->sname.slen = 0;
428 id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
429 id_uri = pjsip_uri_get_uri(id_name_addr->uri);
431 if (id->name.valid) {
432 pj_strdup2(tdata->pool, &id_name_addr->display, id->name.str);
435 pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
437 id_hdr->uri = (pjsip_uri *) id_name_addr;
443 * \brief Add a Privacy header to an outbound message
445 * When sending a P-Asserted-Identity header, if privacy is requested, then we
446 * will need to indicate such by adding a Privacy header. Similarly, if no
447 * privacy is requested, and a Privacy header already exists on the message,
448 * then the old Privacy header should be removed.
450 * \param tdata The outbound message to add the Privacy header to
451 * \param id The id information used to determine privacy
453 static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
455 static const pj_str_t pj_privacy_name = { "Privacy", 7 };
456 static const pj_str_t pj_privacy_value = { "id", 2 };
457 pjsip_hdr *old_privacy;
459 old_privacy = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_privacy_name, NULL);
461 if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
462 (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) {
464 pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
465 tdata->pool, &pj_privacy_name, &pj_privacy_value);
466 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
470 pj_list_erase(old_privacy);
477 * \brief Add a P-Asserted-Identity header to an outbound message
478 * \param tdata The message to add the header to
479 * \param id The identification information used to populate the header
481 static void add_pai_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
483 static const pj_str_t pj_pai_name = { "P-Asserted-Identity", 19 };
484 pjsip_fromto_hdr *pai_hdr;
485 pjsip_fromto_hdr *old_pai;
487 if (!id->number.valid) {
491 /* Since inv_session reuses responses, we have to make sure there's not already
492 * a P-Asserted-Identity present. If there is, we just modify the old one.
494 old_pai = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_pai_name, NULL);
496 modify_id_header(tdata->pool, old_pai, id);
497 add_privacy_header(tdata, id);
501 pai_hdr = create_new_id_hdr(&pj_pai_name, tdata, id);
505 add_privacy_header(tdata, id);
507 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pai_hdr);
512 * \brief Add privacy and screen parameters to a Remote-Party-ID header.
514 * If privacy is requested, then the privacy and screen parameters need to
515 * reflect this. Similarly, if no privacy or screening is to be communicated,
516 * we need to make sure that any previously set values are updated.
518 * \param tdata The message where the Remote-Party-ID header is
519 * \param hdr The header on which the parameters are being added
520 * \param id The identification information used to determine privacy
522 static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
524 static const pj_str_t privacy_str = { "privacy", 7 };
525 static const pj_str_t screen_str = { "screen", 6 };
526 static const pj_str_t privacy_full_str = { "full", 4 };
527 static const pj_str_t privacy_off_str = { "off", 3 };
528 static const pj_str_t screen_yes_str = { "yes", 3 };
529 static const pj_str_t screen_no_str = { "no", 2 };
530 pjsip_param *old_privacy;
531 pjsip_param *old_screen;
532 pjsip_param *privacy;
535 old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
536 old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
539 privacy = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
540 privacy->name = privacy_str;
541 pj_list_insert_before(&hdr->other_param, privacy);
543 privacy = old_privacy;
547 screen = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
548 screen->name = screen_str;
549 pj_list_insert_before(&hdr->other_param, screen);
554 if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
555 (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
556 privacy->value = privacy_off_str;
558 privacy->value = privacy_full_str;
561 if ((id->name.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN &&
562 (id->number.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN) {
563 screen->value = screen_yes_str;
565 screen->value = screen_no_str;
571 * \brief Add a Remote-Party-ID header to an outbound message
572 * \param tdata The message to add the header to
573 * \param id The identification information used to populate the header
575 static void add_rpid_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
577 static const pj_str_t pj_rpid_name = { "Remote-Party-ID", 15 };
578 pjsip_fromto_hdr *rpid_hdr;
579 pjsip_fromto_hdr *old_rpid;
581 if (!id->number.valid) {
585 /* Since inv_session reuses responses, we have to make sure there's not already
586 * a P-Asserted-Identity present. If there is, we just modify the old one.
588 old_rpid = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_rpid_name, NULL);
590 modify_id_header(tdata->pool, old_rpid, id);
591 add_privacy_params(tdata, old_rpid, id);
595 rpid_hdr = create_new_id_hdr(&pj_rpid_name, tdata, id);
599 add_privacy_params(tdata, rpid_hdr, id);
600 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)rpid_hdr);
605 * \brief Add any appropriate identification headers to an outbound SIP message
607 * This will determine if an outbound message should have identification headers and
608 * will add the appropriately configured headers
610 * \param session The session on which we will be sending the message
611 * \param tdata The outbound message
612 * \param The identity information to place on the message
614 static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
616 if (((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
617 (id->number.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) &&
618 !session->endpoint->id.trust_outbound) {
621 if (session->endpoint->id.send_pai) {
622 add_pai_header(tdata, id);
624 if (session->endpoint->id.send_rpid) {
625 add_rpid_header(tdata, id);
631 * \brief Session supplement callback for outgoing INVITE requests
633 * For an initial INVITE request, we may change the From header to appropriately
634 * reflect the identity information. On all INVITEs (initial and reinvite) we may
635 * add other identity headers such as P-Asserted-Identity and Remote-Party-ID based
636 * on configuration and privacy settings
638 * \param session The session on which the INVITE will be sent
639 * \param tdata The outbound INVITE request
641 static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
643 struct ast_party_id connected_id;
645 if (!session->channel) {
649 connected_id = ast_channel_connected_effective_id(session->channel);
650 if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED &&
651 ast_strlen_zero(session->endpoint->fromuser) &&
652 (session->endpoint->id.trust_outbound ||
653 ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
654 (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
655 /* Only change the From header on the initial outbound INVITE. Switching it
656 * mid-call might confuse some UAs.
658 pjsip_fromto_hdr *from;
661 from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, tdata->msg->hdr.next);
662 dlg = session->inv_session->dlg;
664 modify_id_header(tdata->pool, from, &connected_id);
665 modify_id_header(dlg->pool, dlg->local.info, &connected_id);
667 add_id_headers(session, tdata, &connected_id);
672 * \brief Session supplement for outgoing INVITE response
674 * This will add P-Asserted-Identity and Remote-Party-ID headers if necessary
676 * \param session The session on which the INVITE response is to be sent
677 * \param tdata The outbound INVITE response
679 static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
681 struct ast_party_id connected_id;
683 if (!session->channel) {
686 connected_id = ast_channel_connected_effective_id(session->channel);
687 add_id_headers(session, tdata, &connected_id);
690 static struct ast_sip_session_supplement caller_id_supplement = {
691 .method = "INVITE,UPDATE",
692 .priority = AST_SIP_SESSION_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
693 .incoming_request = caller_id_incoming_request,
694 .incoming_response = caller_id_incoming_response,
695 .outgoing_request = caller_id_outgoing_request,
696 .outgoing_response = caller_id_outgoing_response,
699 static int load_module(void)
701 ast_sip_session_register_supplement(&caller_id_supplement);
702 return AST_MODULE_LOAD_SUCCESS;
705 static int unload_module(void)
707 ast_sip_session_unregister_supplement(&caller_id_supplement);
711 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
713 .unload = unload_module,
714 .load_pri = AST_MODPRI_APP_DEPEND,