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