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_party_caller caller;
286 /* Fill connected line information */
287 ast_party_connected_line_init(&connected);
289 connected.id.tag = session->endpoint->id.self.tag;
290 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
292 /* Save to channel driver copy */
293 ast_party_id_copy(&session->id, &connected.id);
295 /* Update our channel CALLERID() */
296 ast_party_caller_init(&caller);
297 caller.id = connected.id;
298 caller.ani = connected.id;
299 caller.ani2 = ast_channel_caller(session->channel)->ani2;
300 ast_channel_set_caller_event(session->channel, &caller, NULL);
302 /* Tell peer about the new connected line information. */
303 ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
308 * \brief Make updates to connected line information based on an incoming request.
310 * This will get identity information from an incoming request. Once the identification is
311 * retrieved, we will check if the new information warrants a connected line update and queue
312 * a connected line update if so.
314 * \param session The session on which we received an incoming request
315 * \param rdata The incoming request
317 static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
319 struct ast_party_id id;
321 if (!session->endpoint->id.trust_inbound) {
325 ast_party_id_init(&id);
326 if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) {
327 if (should_queue_connected_line_update(session, &id)) {
328 queue_connected_line_update(session, &id);
331 ast_party_id_free(&id);
336 * \brief Session supplement callback on an incoming INVITE request
338 * If we are receiving an initial INVITE, then we will set the session's identity
339 * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
340 * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
343 * \param session The session that has received an INVITE
344 * \param rdata The incoming INVITE
346 static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
348 if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED) {
350 * Initial inbound INVITE. Set the session ID directly
351 * because the channel has not been created yet.
353 if (session->endpoint->id.trust_inbound
354 && (!set_id_from_pai(rdata, &session->id)
355 || !set_id_from_rpid(rdata, &session->id))) {
356 ast_free(session->id.tag);
357 session->id.tag = ast_strdup(session->endpoint->id.self.tag);
360 ast_party_id_copy(&session->id, &session->endpoint->id.self);
361 if (!session->endpoint->id.self.number.valid) {
362 set_id_from_from(rdata, &session->id);
365 /* Reinvite. Check for changes to the ID and queue a connected line
366 * update if necessary
368 update_incoming_connected_line(session, rdata);
375 * \brief Session supplement callback on INVITE response
377 * INVITE responses could result in queuing connected line updates.
379 * \param session The session on which communication is happening
380 * \param rdata The incoming INVITE response
382 static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
384 if (!session->channel) {
388 update_incoming_connected_line(session, rdata);
393 * \brief Set name and number information on an identity header.
394 * \param pool Memory pool to use for string duplication
395 * \param id_hdr A From, P-Asserted-Identity, or Remote-Party-ID header to modify
396 * \param id The identity information to apply to the header
398 static void modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const struct ast_party_id *id)
400 pjsip_name_addr *id_name_addr;
401 pjsip_sip_uri *id_uri;
403 id_name_addr = (pjsip_name_addr *) id_hdr->uri;
404 id_uri = pjsip_uri_get_uri(id_name_addr->uri);
406 if (id->name.valid) {
407 pj_strdup2(pool, &id_name_addr->display, id->name.str);
410 if (id->number.valid) {
411 pj_strdup2(pool, &id_uri->user, id->number.str);
417 * \brief Create an identity header for an outgoing message
418 * \param hdr_name The name of the header to create
419 * \param tdata The message to place the header on
420 * \param id The identification information for the new header
421 * \return newly-created header
423 static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_tx_data *tdata, const struct ast_party_id *id)
425 pjsip_fromto_hdr *id_hdr;
426 pjsip_fromto_hdr *base;
427 pjsip_name_addr *id_name_addr;
428 pjsip_sip_uri *id_uri;
430 base = tdata->msg->type == PJSIP_REQUEST_MSG ? PJSIP_MSG_FROM_HDR(tdata->msg) :
431 PJSIP_MSG_TO_HDR(tdata->msg);
432 id_hdr = pjsip_from_hdr_create(tdata->pool);
433 id_hdr->type = PJSIP_H_OTHER;
434 pj_strdup(tdata->pool, &id_hdr->name, hdr_name);
435 id_hdr->sname.slen = 0;
437 id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
438 id_uri = pjsip_uri_get_uri(id_name_addr->uri);
440 if (id->name.valid) {
441 pj_strdup2(tdata->pool, &id_name_addr->display, id->name.str);
444 pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
446 id_hdr->uri = (pjsip_uri *) id_name_addr;
452 * \brief Add a Privacy header to an outbound message
454 * When sending a P-Asserted-Identity header, if privacy is requested, then we
455 * will need to indicate such by adding a Privacy header. Similarly, if no
456 * privacy is requested, and a Privacy header already exists on the message,
457 * then the old Privacy header should be removed.
459 * \param tdata The outbound message to add the Privacy header to
460 * \param id The id information used to determine privacy
462 static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
464 static const pj_str_t pj_privacy_name = { "Privacy", 7 };
465 static const pj_str_t pj_privacy_value = { "id", 2 };
466 pjsip_hdr *old_privacy;
468 old_privacy = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_privacy_name, NULL);
470 if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
471 (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) {
473 pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
474 tdata->pool, &pj_privacy_name, &pj_privacy_value);
475 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
479 pj_list_erase(old_privacy);
486 * \brief Add a P-Asserted-Identity header to an outbound message
487 * \param tdata The message to add the header to
488 * \param id The identification information used to populate the header
490 static void add_pai_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
492 static const pj_str_t pj_pai_name = { "P-Asserted-Identity", 19 };
493 pjsip_fromto_hdr *pai_hdr;
494 pjsip_fromto_hdr *old_pai;
496 if (!id->number.valid) {
500 /* Since inv_session reuses responses, we have to make sure there's not already
501 * a P-Asserted-Identity present. If there is, we just modify the old one.
503 old_pai = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_pai_name, NULL);
505 modify_id_header(tdata->pool, old_pai, id);
506 add_privacy_header(tdata, id);
510 pai_hdr = create_new_id_hdr(&pj_pai_name, tdata, id);
514 add_privacy_header(tdata, id);
516 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pai_hdr);
521 * \brief Add privacy and screen parameters to a Remote-Party-ID header.
523 * If privacy is requested, then the privacy and screen parameters need to
524 * reflect this. Similarly, if no privacy or screening is to be communicated,
525 * we need to make sure that any previously set values are updated.
527 * \param tdata The message where the Remote-Party-ID header is
528 * \param hdr The header on which the parameters are being added
529 * \param id The identification information used to determine privacy
531 static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
533 static const pj_str_t privacy_str = { "privacy", 7 };
534 static const pj_str_t screen_str = { "screen", 6 };
535 static const pj_str_t privacy_full_str = { "full", 4 };
536 static const pj_str_t privacy_off_str = { "off", 3 };
537 static const pj_str_t screen_yes_str = { "yes", 3 };
538 static const pj_str_t screen_no_str = { "no", 2 };
539 pjsip_param *old_privacy;
540 pjsip_param *old_screen;
541 pjsip_param *privacy;
544 old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
545 old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
548 privacy = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
549 privacy->name = privacy_str;
550 pj_list_insert_before(&hdr->other_param, privacy);
552 privacy = old_privacy;
556 screen = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
557 screen->name = screen_str;
558 pj_list_insert_before(&hdr->other_param, screen);
563 if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
564 (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
565 privacy->value = privacy_off_str;
567 privacy->value = privacy_full_str;
570 if ((id->name.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN &&
571 (id->number.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN) {
572 screen->value = screen_yes_str;
574 screen->value = screen_no_str;
580 * \brief Add a Remote-Party-ID header to an outbound message
581 * \param tdata The message to add the header to
582 * \param id The identification information used to populate the header
584 static void add_rpid_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
586 static const pj_str_t pj_rpid_name = { "Remote-Party-ID", 15 };
587 pjsip_fromto_hdr *rpid_hdr;
588 pjsip_fromto_hdr *old_rpid;
590 if (!id->number.valid) {
594 /* Since inv_session reuses responses, we have to make sure there's not already
595 * a P-Asserted-Identity present. If there is, we just modify the old one.
597 old_rpid = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_rpid_name, NULL);
599 modify_id_header(tdata->pool, old_rpid, id);
600 add_privacy_params(tdata, old_rpid, id);
604 rpid_hdr = create_new_id_hdr(&pj_rpid_name, tdata, id);
608 add_privacy_params(tdata, rpid_hdr, id);
609 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)rpid_hdr);
614 * \brief Add any appropriate identification headers to an outbound SIP message
616 * This will determine if an outbound message should have identification headers and
617 * will add the appropriately configured headers
619 * \param session The session on which we will be sending the message
620 * \param tdata The outbound message
621 * \param The identity information to place on the message
623 static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
625 if (((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
626 (id->number.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) &&
627 !session->endpoint->id.trust_outbound) {
630 if (session->endpoint->id.send_pai) {
631 add_pai_header(tdata, id);
633 if (session->endpoint->id.send_rpid) {
634 add_rpid_header(tdata, id);
640 * \brief Session supplement callback for outgoing INVITE requests
642 * For an initial INVITE request, we may change the From header to appropriately
643 * reflect the identity information. On all INVITEs (initial and reinvite) we may
644 * add other identity headers such as P-Asserted-Identity and Remote-Party-ID based
645 * on configuration and privacy settings
647 * \param session The session on which the INVITE will be sent
648 * \param tdata The outbound INVITE request
650 static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
652 struct ast_party_id effective_id;
653 struct ast_party_id connected_id;
655 if (!session->channel) {
659 /* Must do a deep copy unless we hold the channel lock the entire time. */
660 ast_party_id_init(&connected_id);
661 ast_channel_lock(session->channel);
662 effective_id = ast_channel_connected_effective_id(session->channel);
663 ast_party_id_copy(&connected_id, &effective_id);
664 ast_channel_unlock(session->channel);
666 if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED &&
667 ast_strlen_zero(session->endpoint->fromuser) &&
668 (session->endpoint->id.trust_outbound ||
669 ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
670 (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
671 /* Only change the From header on the initial outbound INVITE. Switching it
672 * mid-call might confuse some UAs.
674 pjsip_fromto_hdr *from;
677 from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, tdata->msg->hdr.next);
678 dlg = session->inv_session->dlg;
680 modify_id_header(tdata->pool, from, &connected_id);
681 modify_id_header(dlg->pool, dlg->local.info, &connected_id);
683 add_id_headers(session, tdata, &connected_id);
684 ast_party_id_free(&connected_id);
689 * \brief Session supplement for outgoing INVITE response
691 * This will add P-Asserted-Identity and Remote-Party-ID headers if necessary
693 * \param session The session on which the INVITE response is to be sent
694 * \param tdata The outbound INVITE response
696 static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
698 struct ast_party_id effective_id;
699 struct ast_party_id connected_id;
701 if (!session->channel) {
705 /* Must do a deep copy unless we hold the channel lock the entire time. */
706 ast_party_id_init(&connected_id);
707 ast_channel_lock(session->channel);
708 effective_id = ast_channel_connected_effective_id(session->channel);
709 ast_party_id_copy(&connected_id, &effective_id);
710 ast_channel_unlock(session->channel);
712 add_id_headers(session, tdata, &connected_id);
713 ast_party_id_free(&connected_id);
716 static struct ast_sip_session_supplement caller_id_supplement = {
717 .method = "INVITE,UPDATE",
718 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
719 .incoming_request = caller_id_incoming_request,
720 .incoming_response = caller_id_incoming_response,
721 .outgoing_request = caller_id_outgoing_request,
722 .outgoing_response = caller_id_outgoing_response,
725 static int load_module(void)
727 ast_sip_session_register_supplement(&caller_id_supplement);
728 return AST_MODULE_LOAD_SUCCESS;
731 static int unload_module(void)
733 ast_sip_session_unregister_supplement(&caller_id_supplement);
737 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
738 .support_level = AST_MODULE_SUPPORT_CORE,
740 .unload = unload_module,
741 .load_pri = AST_MODPRI_APP_DEPEND,