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);
134 if (privacy && !pj_stricmp2(&privacy->hvalue, "id")) {
135 id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
136 id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
138 id->number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
139 id->name.presentation = AST_PRES_ALLOWED_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 id->number.presentation = AST_PRES_ALLOWED;
183 id->name.presentation = AST_PRES_ALLOWED;
185 if (screen && !pj_stricmp2(&screen->value, "yes")) {
186 id->number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
187 id->name.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
189 id->number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
190 id->name.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
198 * \brief Set an ast_party_id structure based on data in a From
200 * This makes use of \ref set_id_from_hdr for setting name and number. It uses
201 * no information from the message in order to set privacy. It relies on endpoint
202 * configuration for privacy information.
204 * \param rdata The incoming message
205 * \param[out] id The ID to set
206 * \retval 0 Succesfully set the party ID
207 * \retval non-zero Could not set the party ID
209 static int set_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
211 pjsip_fromto_hdr *from = pjsip_msg_find_hdr(rdata->msg_info.msg,
212 PJSIP_H_FROM, rdata->msg_info.msg->hdr.next);
215 /* This had better not happen */
219 set_id_from_hdr(from, id);
221 if (!id->number.valid) {
230 * \brief Determine if a connected line update should be queued
232 * This uses information about the session and the ID that would be queued
233 * in the connected line update in order to determine if we should queue
234 * a connected line update.
236 * \param session The session whose channel we wish to queue the connected line update on
237 * \param id The identification information that would be queued on the connected line update
238 * \retval 0 We should not queue a connected line update
239 * \retval non-zero We should queue a connected line update
241 static int should_queue_connected_line_update(const struct ast_sip_session *session, const struct ast_party_id *id)
243 /* Invalid number means no update */
244 if (!id->number.valid) {
248 /* If the session has never communicated an update or if the
249 * new ID has a different number than the session, then we
250 * should queue an update
252 if (ast_strlen_zero(session->id.number.str) ||
253 strcmp(session->id.number.str, id->number.str)) {
257 /* By making it to this point, it means the number is not enough
258 * to determine if an update should be sent. Now we look at
262 /* If the number couldn't warrant an update and the name is
263 * invalid, then no update
265 if (!id->name.valid) {
269 /* If the name has changed or we don't have a name set for the
270 * session, then we should send an update
272 if (ast_strlen_zero(session->id.name.str) ||
273 strcmp(session->id.name.str, id->name.str)) {
277 /* Neither the name nor the number have changed. No update */
283 * \brief Queue a connected line update on a session's channel.
284 * \param session The session whose channel should have the connected line update queued upon.
285 * \param id The identification information to place in the connected line update
287 static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id)
289 struct ast_party_connected_line connected;
290 struct ast_party_caller caller;
292 /* Fill connected line information */
293 ast_party_connected_line_init(&connected);
295 connected.id.tag = session->endpoint->id.self.tag;
296 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
298 /* Save to channel driver copy */
299 ast_party_id_copy(&session->id, &connected.id);
301 /* Update our channel CALLERID() */
302 ast_party_caller_init(&caller);
303 caller.id = connected.id;
304 caller.ani = connected.id;
305 caller.ani2 = ast_channel_caller(session->channel)->ani2;
306 ast_channel_set_caller_event(session->channel, &caller, NULL);
308 /* Tell peer about the new connected line information. */
309 ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
314 * \brief Make updates to connected line information based on an incoming request.
316 * This will get identity information from an incoming request. Once the identification is
317 * retrieved, we will check if the new information warrants a connected line update and queue
318 * a connected line update if so.
320 * \param session The session on which we received an incoming request
321 * \param rdata The incoming request
323 static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
325 struct ast_party_id id;
327 if (!session->endpoint->id.trust_inbound) {
331 ast_party_id_init(&id);
332 if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) {
333 if (should_queue_connected_line_update(session, &id)) {
334 queue_connected_line_update(session, &id);
337 ast_party_id_free(&id);
342 * \brief Session supplement callback on an incoming INVITE request
344 * If we are receiving an initial INVITE, then we will set the session's identity
345 * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
346 * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
349 * \param session The session that has received an INVITE
350 * \param rdata The incoming INVITE
352 static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
354 if (!session->channel) {
356 * Since we have no channel this must be the initial inbound
357 * INVITE. Set the session ID directly because the channel
358 * has not been created yet.
360 if (session->endpoint->id.trust_inbound
361 && (!set_id_from_pai(rdata, &session->id)
362 || !set_id_from_rpid(rdata, &session->id))) {
363 ast_free(session->id.tag);
364 session->id.tag = ast_strdup(session->endpoint->id.self.tag);
367 ast_party_id_copy(&session->id, &session->endpoint->id.self);
368 if (!session->endpoint->id.self.number.valid) {
369 set_id_from_from(rdata, &session->id);
373 * ReINVITE or UPDATE. Check for changes to the ID and queue
374 * a connected line update if necessary.
376 update_incoming_connected_line(session, rdata);
383 * \brief Session supplement callback on INVITE response
385 * INVITE responses could result in queuing connected line updates.
387 * \param session The session on which communication is happening
388 * \param rdata The incoming INVITE response
390 static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
392 if (!session->channel) {
396 update_incoming_connected_line(session, rdata);
401 * \brief Create an identity header for an outgoing message
402 * \param hdr_name The name of the header to create
403 * \param tdata The message to place the header on
404 * \param id The identification information for the new header
405 * \return newly-created header
407 static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_fromto_hdr *base, pjsip_tx_data *tdata, const struct ast_party_id *id)
409 pjsip_fromto_hdr *id_hdr;
410 pjsip_name_addr *id_name_addr;
411 pjsip_sip_uri *id_uri;
413 id_hdr = pjsip_from_hdr_create(tdata->pool);
414 id_hdr->type = PJSIP_H_OTHER;
415 pj_strdup(tdata->pool, &id_hdr->name, hdr_name);
416 id_hdr->sname.slen = 0;
418 id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
419 id_uri = pjsip_uri_get_uri(id_name_addr->uri);
421 if (id->name.valid) {
422 int name_buf_len = strlen(id->name.str) * 2 + 1;
423 char *name_buf = ast_alloca(name_buf_len);
425 ast_escape_quoted(id->name.str, name_buf, name_buf_len);
426 pj_strdup2(tdata->pool, &id_name_addr->display, name_buf);
429 * We need to clear the remnants of the clone or it'll be left set.
430 * pj_strdup2 is safe to call with a NULL src and it resets both slen and ptr.
432 pj_strdup2(tdata->pool, &id_name_addr->display, NULL);
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 ((ast_party_id_presentation(id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
463 pj_list_erase(old_privacy);
465 } else if (!old_privacy) {
466 pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
467 tdata->pool, &pj_privacy_name, &pj_privacy_value);
468 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
474 * \brief Add a P-Asserted-Identity header to an outbound message
475 * \param tdata The message to add the header to
476 * \param id The identification information used to populate the header
478 static void add_pai_header(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
480 static const pj_str_t pj_pai_name = { "P-Asserted-Identity", 19 };
481 pjsip_fromto_hdr *base;
482 pjsip_fromto_hdr *pai_hdr;
483 pjsip_fromto_hdr *old_pai;
485 /* Since inv_session reuses responses, we have to make sure there's not already
486 * a P-Asserted-Identity present. If there is, we just modify the old one.
488 old_pai = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_pai_name, NULL);
490 /* If type is OTHER, then the existing header was most likely
491 * added by the PJSIP_HEADER dial plan function as a simple
492 * name/value pair. We can't pass this to modify_id_header because
493 * there are no virtual functions to get the uri. We could parse
494 * it into a pjsip_fromto_hdr but it isn't worth it since
495 * modify_id_header is just going to overwrite the name and number
496 * anyway. We'll just remove it from the header list instead
497 * and create a new one.
499 if (old_pai->type == PJSIP_H_OTHER) {
500 pj_list_erase(old_pai);
502 ast_sip_modify_id_header(tdata->pool, old_pai, id);
503 add_privacy_header(tdata, id);
508 base = tdata->msg->type == PJSIP_REQUEST_MSG ? session->saved_from_hdr :
509 PJSIP_MSG_TO_HDR(tdata->msg);
511 pai_hdr = create_new_id_hdr(&pj_pai_name, base, tdata, id);
515 add_privacy_header(tdata, id);
517 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pai_hdr);
522 * \brief Add privacy and screen parameters to a Remote-Party-ID header.
524 * If privacy is requested, then the privacy and screen parameters need to
525 * reflect this. Similarly, if no privacy or screening is to be communicated,
526 * we need to make sure that any previously set values are updated.
528 * \param tdata The message where the Remote-Party-ID header is
529 * \param hdr The header on which the parameters are being added
530 * \param id The identification information used to determine privacy
532 static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
534 static const pj_str_t privacy_str = { "privacy", 7 };
535 static const pj_str_t screen_str = { "screen", 6 };
536 static const pj_str_t privacy_full_str = { "full", 4 };
537 static const pj_str_t privacy_off_str = { "off", 3 };
538 static const pj_str_t screen_yes_str = { "yes", 3 };
539 static const pj_str_t screen_no_str = { "no", 2 };
540 pjsip_param *old_privacy;
541 pjsip_param *old_screen;
542 pjsip_param *privacy;
546 old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
547 old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
550 privacy = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
551 privacy->name = privacy_str;
552 pj_list_insert_before(&hdr->other_param, privacy);
554 privacy = old_privacy;
558 screen = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
559 screen->name = screen_str;
560 pj_list_insert_before(&hdr->other_param, screen);
565 presentation = ast_party_id_presentation(id);
566 if ((presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
567 privacy->value = privacy_off_str;
569 privacy->value = privacy_full_str;
571 if ((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(const struct ast_sip_session *session, 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 *base;
588 pjsip_fromto_hdr *rpid_hdr;
589 pjsip_fromto_hdr *old_rpid;
591 /* Since inv_session reuses responses, we have to make sure there's not already
592 * a P-Asserted-Identity present. If there is, we just modify the old one.
594 old_rpid = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_rpid_name, NULL);
596 /* If type is OTHER, then the existing header was most likely
597 * added by the PJSIP_HEADER dial plan function as a simple
598 * name/value pair. We can't pass this to modify_id_header because
599 * there are no virtual functions to get the uri. We could parse
600 * it into a pjsip_fromto_hdr but it isn't worth it since
601 * modify_id_header is just going to overwrite the name and number
602 * anyway. We'll just remove it from the header list instead
603 * and create a new one.
605 if (old_rpid->type == PJSIP_H_OTHER) {
606 pj_list_erase(old_rpid);
608 ast_sip_modify_id_header(tdata->pool, old_rpid, id);
609 add_privacy_params(tdata, old_rpid, id);
614 base = tdata->msg->type == PJSIP_REQUEST_MSG ? session->saved_from_hdr :
615 PJSIP_MSG_TO_HDR(tdata->msg);
617 rpid_hdr = create_new_id_hdr(&pj_rpid_name, base, tdata, id);
621 add_privacy_params(tdata, rpid_hdr, id);
622 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)rpid_hdr);
627 * \brief Add any appropriate identification headers to an outbound SIP message
629 * This will determine if an outbound message should have identification headers and
630 * will add the appropriately configured headers
632 * \param session The session on which we will be sending the message
633 * \param tdata The outbound message
634 * \param The identity information to place on the message
636 static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
638 if (!id->number.valid
639 || (!session->endpoint->id.trust_outbound
640 && (ast_party_id_presentation(id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
643 if (session->endpoint->id.send_pai) {
644 add_pai_header(session, tdata, id);
646 if (session->endpoint->id.send_rpid) {
647 add_rpid_header(session, tdata, id);
653 * \brief Session supplement callback for outgoing INVITE requests
655 * On all INVITEs (initial and reinvite) we may add other identity headers
656 * such as P-Asserted-Identity and Remote-Party-ID based on configuration
657 * and privacy settings
659 * \param session The session on which the INVITE will be sent
660 * \param tdata The outbound INVITE request
662 static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
664 struct ast_party_id effective_id;
665 struct ast_party_id connected_id;
667 if (!session->channel) {
671 ast_party_id_init(&connected_id);
672 ast_channel_lock(session->channel);
673 effective_id = ast_channel_connected_effective_id(session->channel);
674 ast_party_id_copy(&connected_id, &effective_id);
675 ast_channel_unlock(session->channel);
677 add_id_headers(session, tdata, &connected_id);
678 ast_party_id_free(&connected_id);
683 * \brief Session supplement for outgoing INVITE response
685 * This will add P-Asserted-Identity and Remote-Party-ID headers if necessary
687 * \param session The session on which the INVITE response is to be sent
688 * \param tdata The outbound INVITE response
690 static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
692 struct ast_party_id effective_id;
693 struct ast_party_id connected_id;
695 if (!session->channel) {
699 /* Must do a deep copy unless we hold the channel lock the entire time. */
700 ast_party_id_init(&connected_id);
701 ast_channel_lock(session->channel);
702 effective_id = ast_channel_connected_effective_id(session->channel);
703 ast_party_id_copy(&connected_id, &effective_id);
704 ast_channel_unlock(session->channel);
706 add_id_headers(session, tdata, &connected_id);
707 ast_party_id_free(&connected_id);
710 static struct ast_sip_session_supplement caller_id_supplement = {
711 .method = "INVITE,UPDATE",
712 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
713 .incoming_request = caller_id_incoming_request,
714 .incoming_response = caller_id_incoming_response,
715 .outgoing_request = caller_id_outgoing_request,
716 .outgoing_response = caller_id_outgoing_response,
719 static int load_module(void)
721 CHECK_PJSIP_SESSION_MODULE_LOADED();
723 ast_sip_session_register_supplement(&caller_id_supplement);
724 return AST_MODULE_LOAD_SUCCESS;
727 static int unload_module(void)
729 ast_sip_session_unregister_supplement(&caller_id_supplement);
733 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
734 .support_level = AST_MODULE_SUPPORT_CORE,
736 .unload = unload_module,
737 .load_pri = AST_MODPRI_APP_DEPEND,