Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_caller_id.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <[email protected]>
7  *
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.
13  *
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.
17  */
18 
19 /*** MODULEINFO
20  <depend>pjproject</depend>
21  <depend>res_pjsip</depend>
22  <depend>res_pjsip_session</depend>
23  <support_level>core</support_level>
24  ***/
25 
26 #include "asterisk.h"
27 
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30 
31 #include "asterisk/res_pjsip.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/module.h"
35 #include "asterisk/callerid.h"
36 #include "asterisk/conversions.h"
37 
38 /*!
39  * \internal
40  * \brief Set an ast_party_id name and number based on an identity header.
41  * \param hdr From, P-Asserted-Identity, or Remote-Party-ID header on incoming message
42  * \param[out] id The ID to set data on
43  */
44 static void set_id_from_hdr(pjsip_fromto_hdr *hdr, struct ast_party_id *id)
45 {
48  pjsip_sip_uri *uri;
49  pjsip_name_addr *id_name_addr = (pjsip_name_addr *) hdr->uri;
50  char *semi;
51 
52  uri = pjsip_uri_get_uri(id_name_addr);
53  ast_copy_pj_str(cid_name, &id_name_addr->display, sizeof(cid_name));
54  ast_copy_pj_str(cid_num, &uri->user, sizeof(cid_num));
55 
56  /* Always truncate caller-id number at a semicolon. */
57  semi = strchr(cid_num, ';');
58  if (semi) {
59  /*
60  * We need to be able to handle URI's looking like
61  * "sip:1235557890;[email protected];user=phone"
62  *
63  * Where the uri->user field will result in:
64  * "1235557890;phone-context=national"
65  *
66  * People don't care about anything after the semicolon
67  * showing up on their displays even though the RFC
68  * allows the semicolon.
69  */
70  *semi = '\0';
71  }
72 
73  ast_free(id->name.str);
74  id->name.str = ast_strdup(cid_name);
75  if (!ast_strlen_zero(cid_name)) {
76  id->name.valid = 1;
77  }
78  ast_free(id->number.str);
79  id->number.str = ast_strdup(cid_num);
80  if (!ast_strlen_zero(cid_num)) {
81  id->number.valid = 1;
82  }
83 }
84 
85 /*!
86  * \internal
87  * \brief Get a P-Asserted-Identity or Remote-Party-ID header from an incoming message
88  *
89  * This function will parse the header as if it were a From header. This allows for us
90  * to easily manipulate the URI, as well as add, modify, or remove parameters from the
91  * header
92  *
93  * \param rdata The incoming message
94  * \param header_name The name of the ID header to find
95  * \retval NULL No ID header present or unable to parse ID header
96  * \retval non-NULL The parsed ID header
97  */
98 static pjsip_fromto_hdr *get_id_header(pjsip_rx_data *rdata, const pj_str_t *header_name)
99 {
100  static const pj_str_t from = { "From", 4 };
101  pj_str_t header_content;
102  pjsip_fromto_hdr *parsed_hdr;
103  pjsip_generic_string_hdr *ident = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
104  header_name, NULL);
105  int parsed_len;
106 
107  if (!ident) {
108  return NULL;
109  }
110 
111  ast_log(LOG_WARNING, "Doing checks now\n");
112 
113  if (!(rdata)) {
114  ast_log(LOG_WARNING, "NULL 1\n");
115  }
116  if (!ident) {
117  ast_log(LOG_WARNING, "NULL 3\n");
118  }
119 
120  if (!rdata->tp_info.pool) {
121  ast_log(LOG_WARNING, "NULL 11\n");
122  }
123  if (!&ident->hvalue) {
124  ast_log(LOG_WARNING, "NULL 13\n");
125  }
126 
127  ast_log(LOG_WARNING, "strlens %lu %lu\n", pj_strlen(&header_content), pj_strlen(&ident->hvalue));
128 
129 
130  pj_strdup_with_null(rdata->tp_info.pool, &header_content, &ident->hvalue);
131 
132  parsed_hdr = pjsip_parse_hdr(rdata->tp_info.pool, &from, header_content.ptr,
133  pj_strlen(&header_content), &parsed_len);
134 
135  if (!parsed_hdr) {
136  return NULL;
137  }
138 
139  return parsed_hdr;
140 }
141 
142 /*!
143  * \internal
144  * \brief Set an ANI2 integer based on OLI data in a From header
145  *
146  * This uses the contents of a From header in order to set Originating Line information.
147  *
148  * \param rdata The incoming message
149  * \param ani2 The ANI2 field to set
150  * \retval 0 Successfully parsed From header
151  * \retval non-zero Could not parse From header
152  */
153 static int set_id_from_oli(pjsip_rx_data *rdata, int *ani2)
154 {
155  #define MAX_OLI_DIGITS 2
156  char oli[MAX_OLI_DIGITS + 1];
157  static const pj_str_t from = { "From", 4 };
158  static const pj_str_t oli_str1 = { "oli", 3 };
159  static const pj_str_t oli_str2 = { "isup-oli", 8 };
160  static const pj_str_t oli_str3 = { "ss7-oli", 7 };
161  const pjsip_param *oli1, *oli2, *oli3;
162  pjsip_fromto_hdr *from_hdr = get_id_header(rdata, &from);
163  if (!from_hdr) {
164  return 0;
165  }
166  if ((oli1 = pjsip_param_find(&from_hdr->other_param, &oli_str1))) {
167  ast_copy_pj_str(oli, &oli1->value, MAX_OLI_DIGITS);
168  return ast_str_to_int(oli, ani2);
169  } else if ((oli2 = pjsip_param_find(&from_hdr->other_param, &oli_str2))) {
170  ast_copy_pj_str(oli, &oli2->value, MAX_OLI_DIGITS);
171  return ast_str_to_int(oli, ani2);
172  } else if ((oli3 = pjsip_param_find(&from_hdr->other_param, &oli_str3))) {
173  ast_copy_pj_str(oli, &oli3->value, MAX_OLI_DIGITS);
174  return ast_str_to_int(oli, ani2);
175  }
176  return 0;
177 }
178 
179 /*!
180  * \internal
181  * \brief Set an ast_party_id structure based on data in a P-Asserted-Identity header
182  *
183  * This makes use of \ref set_id_from_hdr for setting name and number. It uses
184  * the contents of a Privacy header in order to set presentation information.
185  *
186  * \param rdata The incoming message
187  * \param[out] id The ID to set
188  * \retval 0 Successfully set the party ID
189  * \retval non-zero Could not set the party ID
190  */
191 static int set_id_from_pai(pjsip_rx_data *rdata, struct ast_party_id *id)
192 {
193  static const pj_str_t pai_str = { "P-Asserted-Identity", 19 };
194  static const pj_str_t privacy_str = { "Privacy", 7 };
195  pjsip_fromto_hdr *pai_hdr = get_id_header(rdata, &pai_str);
196  pjsip_generic_string_hdr *privacy;
197 
198  if (!pai_hdr) {
199  return -1;
200  }
201 
202  set_id_from_hdr(pai_hdr, id);
203 
204  if (!id->number.valid) {
205  return -1;
206  }
207 
208  privacy = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &privacy_str, NULL);
209  if (!privacy || !pj_stricmp2(&privacy->hvalue, "none")) {
210  id->number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
211  id->name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
212  } else {
213  id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
214  id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
215  }
216 
217  return 0;
218 }
219 
220 /*!
221  * \internal
222  * \brief Set an ast_party_id structure based on data in a Remote-Party-ID header
223  *
224  * This makes use of \ref set_id_from_hdr for setting name and number. It uses
225  * the privacy and screen parameters in order to set presentation information.
226  *
227  * \param rdata The incoming message
228  * \param[out] id The ID to set
229  * \retval 0 Succesfully set the party ID
230  * \retval non-zero Could not set the party ID
231  */
232 static int set_id_from_rpid(pjsip_rx_data *rdata, struct ast_party_id *id)
233 {
234  static const pj_str_t rpid_str = { "Remote-Party-ID", 15 };
235  static const pj_str_t privacy_str = { "privacy", 7 };
236  static const pj_str_t screen_str = { "screen", 6 };
237  pjsip_fromto_hdr *rpid_hdr = get_id_header(rdata, &rpid_str);
238  pjsip_param *screen;
239  pjsip_param *privacy;
240 
241  if (!rpid_hdr) {
242  return -1;
243  }
244 
245  set_id_from_hdr(rpid_hdr, id);
246 
247  if (!id->number.valid) {
248  return -1;
249  }
250 
251  privacy = pjsip_param_find(&rpid_hdr->other_param, &privacy_str);
252  screen = pjsip_param_find(&rpid_hdr->other_param, &screen_str);
253  if (privacy && !pj_stricmp2(&privacy->value, "full")) {
254  id->number.presentation = AST_PRES_RESTRICTED;
255  id->name.presentation = AST_PRES_RESTRICTED;
256  } else {
257  id->number.presentation = AST_PRES_ALLOWED;
258  id->name.presentation = AST_PRES_ALLOWED;
259  }
260  if (screen && !pj_stricmp2(&screen->value, "yes")) {
261  id->number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
262  id->name.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
263  } else {
264  id->number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
265  id->name.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
266  }
267 
268  return 0;
269 }
270 
271 /*!
272  * \internal
273  * \brief Set an ast_party_id structure based on data in a From
274  *
275  * This makes use of \ref set_id_from_hdr for setting name and number. It uses
276  * no information from the message in order to set privacy. It relies on endpoint
277  * configuration for privacy information.
278  *
279  * \param rdata The incoming message
280  * \param[out] id The ID to set
281  * \retval 0 Succesfully set the party ID
282  * \retval non-zero Could not set the party ID
283  */
284 static int set_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
285 {
286  pjsip_fromto_hdr *from = pjsip_msg_find_hdr(rdata->msg_info.msg,
287  PJSIP_H_FROM, rdata->msg_info.msg->hdr.next);
288 
289  if (!from) {
290  /* This had better not happen */
291  return -1;
292  }
293 
294  set_id_from_hdr(from, id);
295 
296  if (!id->number.valid) {
297  return -1;
298  }
299 
300  return 0;
301 }
302 
303 /*!
304  * \internal
305  * \brief Determine if a connected line update should be queued
306  *
307  * This uses information about the session and the ID that would be queued
308  * in the connected line update in order to determine if we should queue
309  * a connected line update.
310  *
311  * \param session The session whose channel we wish to queue the connected line update on
312  * \param id The identification information that would be queued on the connected line update
313  * \retval 0 We should not queue a connected line update
314  * \retval non-zero We should queue a connected line update
315  */
316 static int should_queue_connected_line_update(const struct ast_sip_session *session, const struct ast_party_id *id)
317 {
318  /* Invalid number means no update */
319  if (!id->number.valid) {
320  return 0;
321  }
322 
323  /* If the session has never communicated an update or if the
324  * new ID has a different number than the session, then we
325  * should queue an update
326  */
327  if (ast_strlen_zero(session->id.number.str) ||
328  strcmp(session->id.number.str, id->number.str)) {
329  return 1;
330  }
331 
332  /* By making it to this point, it means the number is not enough
333  * to determine if an update should be sent. Now we look at
334  * the name
335  */
336 
337  /* If the number couldn't warrant an update and the name is
338  * invalid, then no update
339  */
340  if (!id->name.valid) {
341  return 0;
342  }
343 
344  /* If the name has changed or we don't have a name set for the
345  * session, then we should send an update
346  */
347  if (ast_strlen_zero(session->id.name.str) ||
348  strcmp(session->id.name.str, id->name.str)) {
349  return 1;
350  }
351 
352  /* Neither the name nor the number have changed. No update */
353  return 0;
354 }
355 
356 /*!
357  * \internal
358  * \brief Queue a connected line update on a session's channel.
359  * \param session The session whose channel should have the connected line update queued upon.
360  * \param id The identification information to place in the connected line update
361  */
362 static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id)
363 {
364  struct ast_party_connected_line connected;
365  struct ast_party_caller caller;
366 
367  /* Fill connected line information */
368  ast_party_connected_line_init(&connected);
369  connected.id = *id;
370  connected.id.tag = session->endpoint->id.self.tag;
372 
373  /* Save to channel driver copy */
374  ast_party_id_copy(&session->id, &connected.id);
375 
376  /* Update our channel CALLERID() */
377  ast_party_caller_init(&caller);
378  caller.id = connected.id;
379  caller.ani = connected.id;
380  caller.ani2 = ast_channel_caller(session->channel)->ani2;
381  ast_channel_set_caller_event(session->channel, &caller, NULL);
382 
383  /* Tell peer about the new connected line information. */
385 }
386 
387 /*!
388  * \internal
389  * \brief Make updates to connected line information based on an incoming request.
390  *
391  * This will get identity information from an incoming request. Once the identification is
392  * retrieved, we will check if the new information warrants a connected line update and queue
393  * a connected line update if so.
394  *
395  * \param session The session on which we received an incoming request
396  * \param rdata The incoming request
397  */
398 static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
399 {
400  struct ast_party_id id;
401 
402  if (!session->endpoint->id.trust_connected_line
403  || !session->endpoint->id.trust_inbound) {
404  return;
405  }
406 
407  ast_party_id_init(&id);
408  if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) {
409  if (should_queue_connected_line_update(session, &id)) {
410  queue_connected_line_update(session, &id);
411  }
412  }
413  ast_party_id_free(&id);
414 }
415 
416 /*!
417  * \internal
418  * \brief Session supplement callback on an incoming INVITE request
419  *
420  * If we are receiving an initial INVITE, then we will set the session's identity
421  * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
422  * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
423  * function
424  *
425  * \param session The session that has received an INVITE
426  * \param rdata The incoming INVITE
427  */
428 static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
429 {
430  if (!session->channel) {
431  int ani2;
432  /*
433  * Since we have no channel this must be the initial inbound
434  * INVITE. Set the session ID directly because the channel
435  * has not been created yet.
436  */
437 
438  if (session->endpoint->id.trust_inbound
439  && (!set_id_from_pai(rdata, &session->id)
440  || !set_id_from_rpid(rdata, &session->id))) {
441  ast_free(session->id.tag);
442  session->id.tag = ast_strdup(session->endpoint->id.self.tag);
443  return 0;
444  }
445  ast_party_id_copy(&session->id, &session->endpoint->id.self);
446  if (!session->endpoint->id.self.number.valid) {
447  set_id_from_from(rdata, &session->id);
448  }
449  if (!set_id_from_oli(rdata, &ani2)) {
450  session->ani2 = 0;
451  }
452  } else {
453  /*
454  * ReINVITE or UPDATE. Check for changes to the ID and queue
455  * a connected line update if necessary.
456  */
457  update_incoming_connected_line(session, rdata);
458  }
459  return 0;
460 }
461 
462 /*!
463  * \internal
464  * \brief Session supplement callback on INVITE response
465  *
466  * INVITE responses could result in queuing connected line updates.
467  *
468  * \param session The session on which communication is happening
469  * \param rdata The incoming INVITE response
470  */
471 static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
472 {
473  if (!session->channel) {
474  return;
475  }
476 
477  update_incoming_connected_line(session, rdata);
478 }
479 
480 /*!
481  * \internal
482  * \brief Create an identity header for an outgoing message
483  * \param hdr_name The name of the header to create
484  * \param tdata The message to place the header on
485  * \param id The identification information for the new header
486  * \return newly-created header
487  */
488 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)
489 {
490  pjsip_fromto_hdr *id_hdr;
491  pjsip_name_addr *id_name_addr;
492  pjsip_sip_uri *id_uri;
493 
494  id_hdr = pjsip_from_hdr_create(tdata->pool);
495  id_hdr->type = PJSIP_H_OTHER;
496  id_hdr->sname = id_hdr->name = *hdr_name;
497 
498  id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
499  id_uri = pjsip_uri_get_uri(id_name_addr->uri);
500 
501  if (id->name.valid && !ast_strlen_zero(id->name.str)) {
502  int name_buf_len = strlen(id->name.str) * 2 + 1;
503  char *name_buf = ast_alloca(name_buf_len);
504 
505  ast_escape_quoted(id->name.str, name_buf, name_buf_len);
506  pj_strdup2(tdata->pool, &id_name_addr->display, name_buf);
507  } else {
508  /*
509  * We need to clear the remnants of the clone or it'll be left set.
510  * pj_strdup2 is safe to call with a NULL src and it resets both slen and ptr.
511  */
512  pj_strdup2(tdata->pool, &id_name_addr->display, NULL);
513  }
514 
515  if (id->number.valid) {
516  pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
517  } else {
518  /* Similar to name, make sure the number is also cleared when invalid */
519  pj_strdup2(tdata->pool, &id_uri->user, NULL);
520  }
521 
522  id_hdr->uri = (pjsip_uri *) id_name_addr;
523  return id_hdr;
524 }
525 
526 /*!
527  * \internal
528  * \brief Add a Privacy header to an outbound message
529  *
530  * When sending a P-Asserted-Identity header, if privacy is requested, then we
531  * will need to indicate such by adding a Privacy header. Similarly, if no
532  * privacy is requested, and a Privacy header already exists on the message,
533  * then the old Privacy header should be removed.
534  *
535  * \param tdata The outbound message to add the Privacy header to
536  * \param id The id information used to determine privacy
537  */
538 static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
539 {
540  static const pj_str_t pj_privacy_name = { "Privacy", 7 };
541  static const pj_str_t pj_privacy_value = { "id", 2 };
542  pjsip_hdr *old_privacy;
543 
544  old_privacy = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_privacy_name, NULL);
545 
547  if (old_privacy) {
548  pj_list_erase(old_privacy);
549  }
550  } else if (!old_privacy) {
551  pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
552  tdata->pool, &pj_privacy_name, &pj_privacy_value);
553  pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
554  }
555 }
556 
557 /*!
558  * \internal
559  * \brief Add a P-Asserted-Identity header to an outbound message
560  * \param tdata The message to add the header to
561  * \param id The identification information used to populate the header
562  */
563 static void add_pai_header(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
564 {
565  static const pj_str_t pj_pai_name = { "P-Asserted-Identity", 19 };
566  pjsip_fromto_hdr *base;
567  pjsip_fromto_hdr *pai_hdr;
568  pjsip_fromto_hdr *old_pai;
569 
570  /* Since inv_session reuses responses, we have to make sure there's not already
571  * a P-Asserted-Identity present. If there is, we just modify the old one.
572  */
573  old_pai = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_pai_name, NULL);
574  if (old_pai) {
575  /* If type is OTHER, then the existing header was most likely
576  * added by the PJSIP_HEADER dial plan function as a simple
577  * name/value pair. We can't pass this to modify_id_header because
578  * there are no virtual functions to get the uri. We could parse
579  * it into a pjsip_fromto_hdr but it isn't worth it since
580  * modify_id_header is just going to overwrite the name and number
581  * anyway. We'll just remove it from the header list instead
582  * and create a new one.
583  */
584  if (old_pai->type == PJSIP_H_OTHER) {
585  pj_list_erase(old_pai);
586  } else {
587  ast_sip_modify_id_header(tdata->pool, old_pai, id);
588  add_privacy_header(tdata, id);
589  return;
590  }
591  }
592 
593  if (tdata->msg->type == PJSIP_REQUEST_MSG) {
594  base = session->saved_from_hdr ? session->saved_from_hdr : PJSIP_MSG_FROM_HDR(tdata->msg);
595  } else {
596  base = PJSIP_MSG_TO_HDR(tdata->msg);
597  }
598 
599  pai_hdr = create_new_id_hdr(&pj_pai_name, base, tdata, id);
600  if (!pai_hdr) {
601  return;
602  }
603  add_privacy_header(tdata, id);
604 
605  pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pai_hdr);
606 }
607 
608 /*!
609  * \internal
610  * \brief Add party parameter to a Remote-Party-ID header.
611  *
612  * \param tdata The message where the Remote-Party-ID header is
613  * \param hdr The header on which the parameters are being added
614  * \param session The session involved
615  */
616 static void add_party_param(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_sip_session *session)
617 {
618  static const pj_str_t party_str = { "party", 5 };
619  static const pj_str_t calling_str = { "calling", 7 };
620  static const pj_str_t called_str = { "called", 6 };
621  pjsip_param *party;
622 
623  /* The party value can't change throughout the lifetime, so it is set only once */
624  party = pjsip_param_find(&hdr->other_param, &party_str);
625  if (party) {
626  return;
627  }
628 
629  party = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
630  party->name = party_str;
631  party->value = (session->inv_session->role == PJSIP_ROLE_UAC) ? calling_str : called_str;
632  pj_list_insert_before(&hdr->other_param, party);
633 }
634 
635 /*!
636  * \internal
637  * \brief Add privacy and screen parameters to a Remote-Party-ID header.
638  *
639  * If privacy is requested, then the privacy and screen parameters need to
640  * reflect this. Similarly, if no privacy or screening is to be communicated,
641  * we need to make sure that any previously set values are updated.
642  *
643  * \param tdata The message where the Remote-Party-ID header is
644  * \param hdr The header on which the parameters are being added
645  * \param id The identification information used to determine privacy
646  */
647 static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
648 {
649  static const pj_str_t privacy_str = { "privacy", 7 };
650  static const pj_str_t screen_str = { "screen", 6 };
651  static const pj_str_t privacy_full_str = { "full", 4 };
652  static const pj_str_t privacy_off_str = { "off", 3 };
653  static const pj_str_t screen_yes_str = { "yes", 3 };
654  static const pj_str_t screen_no_str = { "no", 2 };
655  pjsip_param *old_privacy;
656  pjsip_param *old_screen;
657  pjsip_param *privacy;
658  pjsip_param *screen;
659  int presentation;
660 
661  old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
662  old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
663 
664  if (!old_privacy) {
665  privacy = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
666  privacy->name = privacy_str;
667  pj_list_insert_before(&hdr->other_param, privacy);
668  } else {
669  privacy = old_privacy;
670  }
671 
672  if (!old_screen) {
673  screen = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
674  screen->name = screen_str;
675  pj_list_insert_before(&hdr->other_param, screen);
676  } else {
677  screen = old_screen;
678  }
679 
680  presentation = ast_party_id_presentation(id);
681  if ((presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
682  privacy->value = privacy_off_str;
683  } else {
684  privacy->value = privacy_full_str;
685  }
687  screen->value = screen_yes_str;
688  } else {
689  screen->value = screen_no_str;
690  }
691 }
692 
693 /*!
694  * \internal
695  * \brief Add a Remote-Party-ID header to an outbound message
696  * \param tdata The message to add the header to
697  * \param id The identification information used to populate the header
698  */
699 static void add_rpid_header(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
700 {
701  static const pj_str_t pj_rpid_name = { "Remote-Party-ID", 15 };
702  pjsip_fromto_hdr *base;
703  pjsip_fromto_hdr *rpid_hdr;
704  pjsip_fromto_hdr *old_rpid;
705 
706  /* Since inv_session reuses responses, we have to make sure there's not already
707  * a P-Asserted-Identity present. If there is, we just modify the old one.
708  */
709  old_rpid = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_rpid_name, NULL);
710  if (old_rpid) {
711  /* If type is OTHER, then the existing header was most likely
712  * added by the PJSIP_HEADER dial plan function as a simple
713  * name/value pair. We can't pass this to modify_id_header because
714  * there are no virtual functions to get the uri. We could parse
715  * it into a pjsip_fromto_hdr but it isn't worth it since
716  * modify_id_header is just going to overwrite the name and number
717  * anyway. We'll just remove it from the header list instead
718  * and create a new one.
719  */
720  if (old_rpid->type == PJSIP_H_OTHER) {
721  pj_list_erase(old_rpid);
722  } else {
723  ast_sip_modify_id_header(tdata->pool, old_rpid, id);
724  add_party_param(tdata, old_rpid, session);
725  add_privacy_params(tdata, old_rpid, id);
726  return;
727  }
728  }
729 
730  if (tdata->msg->type == PJSIP_REQUEST_MSG) {
731  base = session->saved_from_hdr ? session->saved_from_hdr : PJSIP_MSG_FROM_HDR(tdata->msg);
732  } else {
733  base = PJSIP_MSG_TO_HDR(tdata->msg);
734  }
735 
736  rpid_hdr = create_new_id_hdr(&pj_rpid_name, base, tdata, id);
737  if (!rpid_hdr) {
738  return;
739  }
740  add_party_param(tdata, rpid_hdr, session);
741  add_privacy_params(tdata, rpid_hdr, id);
742  pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)rpid_hdr);
743 }
744 
745 /*!
746  * \internal
747  * \brief Add any appropriate identification headers to an outbound SIP message
748  *
749  * This will determine if an outbound message should have identification headers and
750  * will add the appropriately configured headers
751  *
752  * \param session The session on which we will be sending the message
753  * \param tdata The outbound message
754  * \param The identity information to place on the message
755  */
756 static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
757 {
758  if (!id->number.valid
759  || (!session->endpoint->id.trust_outbound
761  return;
762  }
763  if (session->endpoint->id.send_pai) {
764  add_pai_header(session, tdata, id);
765  }
766  if (session->endpoint->id.send_rpid) {
767  add_rpid_header(session, tdata, id);
768  }
769 }
770 
771 /*!
772  * \internal
773  * \brief Session supplement callback for outgoing INVITE requests
774  *
775  * On all INVITEs (initial and reinvite) we may add other identity headers
776  * such as P-Asserted-Identity and Remote-Party-ID based on configuration
777  * and privacy settings
778  *
779  * \param session The session on which the INVITE will be sent
780  * \param tdata The outbound INVITE request
781  */
782 static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
783 {
784  struct ast_party_id effective_id;
785  struct ast_party_id connected_id;
786 
787  if (!session->channel) {
788  return;
789  }
790 
791  ast_party_id_init(&connected_id);
792  ast_channel_lock(session->channel);
793  effective_id = ast_channel_connected_effective_id(session->channel);
794  ast_party_id_copy(&connected_id, &effective_id);
795  ast_channel_unlock(session->channel);
796 
797  add_id_headers(session, tdata, &connected_id);
798  ast_party_id_free(&connected_id);
799 }
800 
801 /*!
802  * \internal
803  * \brief Session supplement for outgoing INVITE response
804  *
805  * This will add P-Asserted-Identity and Remote-Party-ID headers if necessary
806  *
807  * \param session The session on which the INVITE response is to be sent
808  * \param tdata The outbound INVITE response
809  */
810 static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
811 {
812  struct ast_party_id effective_id;
813  struct ast_party_id connected_id;
814 
815  if (!session->channel
816  || (!session->endpoint->id.send_connected_line
817  && session->inv_session
818  && session->inv_session->state >= PJSIP_INV_STATE_EARLY)) {
819  return;
820  }
821 
822  /* Must do a deep copy unless we hold the channel lock the entire time. */
823  ast_party_id_init(&connected_id);
824  ast_channel_lock(session->channel);
825  effective_id = ast_channel_connected_effective_id(session->channel);
826  ast_party_id_copy(&connected_id, &effective_id);
827  ast_channel_unlock(session->channel);
828 
829  add_id_headers(session, tdata, &connected_id);
830  ast_party_id_free(&connected_id);
831 }
832 
834  .method = "INVITE,UPDATE",
835  .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
836  .incoming_request = caller_id_incoming_request,
837  .incoming_response = caller_id_incoming_response,
838  .outgoing_request = caller_id_outgoing_request,
839  .outgoing_response = caller_id_outgoing_response,
840 };
841 
842 static int load_module(void)
843 {
844  ast_module_shutdown_ref(AST_MODULE_SELF);
845  ast_sip_session_register_supplement(&caller_id_supplement);
847 }
848 
849 static int unload_module(void)
850 {
851  ast_sip_session_unregister_supplement(&caller_id_supplement);
852  return 0;
853 }
854 
855 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
856  .support_level = AST_MODULE_SUPPORT_CORE,
857  .load = load_module,
858  .unload = unload_module,
859  .load_pri = AST_MODPRI_APP_DEPEND,
860  .requires = "res_pjsip,res_pjsip_session",
861 );
static pjsip_fromto_hdr * get_id_header(pjsip_rx_data *rdata, const pj_str_t *header_name)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
Information needed to identify an endpoint in a call.
Definition: channel.h:339
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
static int set_id_from_oli(pjsip_rx_data *rdata, int *ani2)
#define ast_channel_lock(chan)
Definition: channel.h:2945
static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
struct ast_sip_endpoint * endpoint
char * str
Subscriber phone number (Malloced)
Definition: channel.h:292
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)
static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
Asterisk main include file. File version handling, generic pbx functions.
static int unload_module(void)
void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
Set the caller id information in the Asterisk channel and generate an AMI event if the caller id name...
Definition: channel.c:7472
static int set_id_from_rpid(pjsip_rx_data *rdata, struct ast_party_id *id)
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
struct ast_party_id id
Connected party ID.
Definition: channel.h:459
struct ast_party_name name
Subscriber name.
Definition: channel.h:341
static void set_id_from_hdr(pjsip_fromto_hdr *hdr, struct ast_party_id *id)
#define LOG_WARNING
Definition: logger.h:274
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
#define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED
Definition: callerid.h:341
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
Copy the source party id information to the destination party id.
Definition: channel.c:1765
static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
static int load_module(void)
char * str
Subscriber name (Malloced)
Definition: channel.h:265
static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
void ast_party_id_free(struct ast_party_id *doomed)
Destroy the party id contents.
Definition: channel.c:1811
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5240
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
static struct ast_sip_session_supplement caller_id_supplement
#define NULL
Definition: resample.c:96
struct pjsip_inv_session * inv_session
#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED
Definition: callerid.h:329
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:164
A structure describing a SIP session.
#define AST_PRES_RESTRICTED
Definition: callerid.h:325
#define ast_strlen_zero(foo)
Definition: strings.h:52
static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id)
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
#define ast_log
Definition: astobj2.c:42
void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Queue a connected line update frame on a channel.
Definition: channel.c:9202
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:428
General Asterisk PBX channel definitions.
#define AST_PRES_USER_NUMBER_UNSCREENED
Definition: callerid.h:318
static int should_queue_connected_line_update(const struct ast_sip_session *session, const struct ast_party_id *id)
static struct ast_mansession session
Caller Party information.
Definition: channel.h:419
Conversion utility functions.
struct ast_channel * channel
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
struct ast_sip_endpoint_id_configuration id
Definition: res_pjsip.h:847
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
static int set_id_from_pai(pjsip_rx_data *rdata, struct ast_party_id *id)
#define AST_PRES_NUMBER_TYPE
Definition: callerid.h:317
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:464
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:434
Connected Line/Party information.
Definition: channel.h:457
static int set_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
int source
Information about the source of an update.
Definition: channel.h:483
#define ast_free(a)
Definition: astmm.h:182
char * ast_escape_quoted(const char *string, char *outbuf, int buflen)
Escape characters found in a quoted string.
Definition: main/utils.c:635
#define AST_CHANNEL_NAME
Definition: channel.h:172
void ast_party_id_init(struct ast_party_id *init)
Initialize the given party id structure.
Definition: channel.c:1757
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:165
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
void ast_sip_modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const struct ast_party_id *id)
Set name and number information on an identity header.
Definition: res_pjsip.c:5587
A supplement to SIP message processing.
#define AST_PRES_RESTRICTION
Definition: callerid.h:323
#define AST_PRES_USER_NUMBER_PASSED_SCREEN
Definition: callerid.h:319
char * tag
User-set "tag".
Definition: channel.h:355
struct ast_party_id self
Definition: res_pjsip.h:629
#define AST_PRES_ALLOWED
Definition: callerid.h:324
static void add_rpid_header(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
enum queue_result id
Definition: app_queue.c:1507
#define MAX_OLI_DIGITS
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:280
pjsip_fromto_hdr * saved_from_hdr
void ast_party_caller_init(struct ast_party_caller *init)
Initialize the given caller structure.
Definition: channel.c:1978
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
struct ast_party_id id
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:298
static void add_party_param(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_sip_session *session)
static void add_pai_header(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
#define ast_sip_session_register_supplement(supplement)
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:343