res_pjsip_caller_id.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_session</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_ua.h>
  27. #include "asterisk/res_pjsip.h"
  28. #include "asterisk/res_pjsip_session.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/callerid.h"
  32. /*!
  33. * \internal
  34. * \brief Set an ast_party_id name and number based on an identity header.
  35. * \param hdr From, P-Asserted-Identity, or Remote-Party-ID header on incoming message
  36. * \param[out] id The ID to set data on
  37. */
  38. static void set_id_from_hdr(pjsip_fromto_hdr *hdr, struct ast_party_id *id)
  39. {
  40. char cid_name[AST_CHANNEL_NAME];
  41. char cid_num[AST_CHANNEL_NAME];
  42. pjsip_sip_uri *uri;
  43. pjsip_name_addr *id_name_addr = (pjsip_name_addr *) hdr->uri;
  44. uri = pjsip_uri_get_uri(id_name_addr);
  45. ast_copy_pj_str(cid_name, &id_name_addr->display, sizeof(cid_name));
  46. ast_copy_pj_str(cid_num, &uri->user, sizeof(cid_num));
  47. ast_free(id->name.str);
  48. id->name.str = ast_strdup(cid_name);
  49. if (!ast_strlen_zero(cid_name)) {
  50. id->name.valid = 1;
  51. }
  52. ast_free(id->number.str);
  53. id->number.str = ast_strdup(cid_num);
  54. if (!ast_strlen_zero(cid_num)) {
  55. id->number.valid = 1;
  56. }
  57. }
  58. /*!
  59. * \internal
  60. * \brief Get a P-Asserted-Identity or Remote-Party-ID header from an incoming message
  61. *
  62. * This function will parse the header as if it were a From header. This allows for us
  63. * to easily manipulate the URI, as well as add, modify, or remove parameters from the
  64. * header
  65. *
  66. * \param rdata The incoming message
  67. * \param header_name The name of the ID header to find
  68. * \retval NULL No ID header present or unable to parse ID header
  69. * \retval non-NULL The parsed ID header
  70. */
  71. static pjsip_fromto_hdr *get_id_header(pjsip_rx_data *rdata, const pj_str_t *header_name)
  72. {
  73. static const pj_str_t from = { "From", 4 };
  74. pj_str_t header_content;
  75. pjsip_fromto_hdr *parsed_hdr;
  76. pjsip_generic_string_hdr *ident = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
  77. header_name, NULL);
  78. int parsed_len;
  79. if (!ident) {
  80. return NULL;
  81. }
  82. pj_strdup_with_null(rdata->tp_info.pool, &header_content, &ident->hvalue);
  83. parsed_hdr = pjsip_parse_hdr(rdata->tp_info.pool, &from, header_content.ptr,
  84. pj_strlen(&header_content), &parsed_len);
  85. if (!parsed_hdr) {
  86. return NULL;
  87. }
  88. return parsed_hdr;
  89. }
  90. /*!
  91. * \internal
  92. * \brief Set an ast_party_id structure based on data in a P-Asserted-Identity header
  93. *
  94. * This makes use of \ref set_id_from_hdr for setting name and number. It uses
  95. * the contents of a Privacy header in order to set presentation information.
  96. *
  97. * \param rdata The incoming message
  98. * \param[out] id The ID to set
  99. * \retval 0 Successfully set the party ID
  100. * \retval non-zero Could not set the party ID
  101. */
  102. static int set_id_from_pai(pjsip_rx_data *rdata, struct ast_party_id *id)
  103. {
  104. static const pj_str_t pai_str = { "P-Asserted-Identity", 19 };
  105. static const pj_str_t privacy_str = { "Privacy", 7 };
  106. pjsip_fromto_hdr *pai_hdr = get_id_header(rdata, &pai_str);
  107. pjsip_generic_string_hdr *privacy;
  108. if (!pai_hdr) {
  109. return -1;
  110. }
  111. set_id_from_hdr(pai_hdr, id);
  112. if (!id->number.valid) {
  113. return -1;
  114. }
  115. privacy = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &privacy_str, NULL);
  116. if (!privacy) {
  117. return 0;
  118. }
  119. if (!pj_stricmp2(&privacy->hvalue, "id")) {
  120. id->number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
  121. id->name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
  122. }
  123. return 0;
  124. }
  125. /*!
  126. * \internal
  127. * \brief Set an ast_party_id structure based on data in a Remote-Party-ID header
  128. *
  129. * This makes use of \ref set_id_from_hdr for setting name and number. It uses
  130. * the privacy and screen parameters in order to set presentation information.
  131. *
  132. * \param rdata The incoming message
  133. * \param[out] id The ID to set
  134. * \retval 0 Succesfully set the party ID
  135. * \retval non-zero Could not set the party ID
  136. */
  137. static int set_id_from_rpid(pjsip_rx_data *rdata, struct ast_party_id *id)
  138. {
  139. static const pj_str_t rpid_str = { "Remote-Party-ID", 15 };
  140. static const pj_str_t privacy_str = { "privacy", 7 };
  141. static const pj_str_t screen_str = { "screen", 6 };
  142. pjsip_fromto_hdr *rpid_hdr = get_id_header(rdata, &rpid_str);
  143. pjsip_param *screen;
  144. pjsip_param *privacy;
  145. if (!rpid_hdr) {
  146. return -1;
  147. }
  148. set_id_from_hdr(rpid_hdr, id);
  149. if (!id->number.valid) {
  150. return -1;
  151. }
  152. privacy = pjsip_param_find(&rpid_hdr->other_param, &privacy_str);
  153. screen = pjsip_param_find(&rpid_hdr->other_param, &screen_str);
  154. if (privacy && !pj_stricmp2(&privacy->value, "full")) {
  155. id->number.presentation |= AST_PRES_RESTRICTED;
  156. id->name.presentation |= AST_PRES_RESTRICTED;
  157. }
  158. if (screen && !pj_stricmp2(&screen->value, "yes")) {
  159. id->number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
  160. id->name.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
  161. }
  162. return 0;
  163. }
  164. /*!
  165. * \internal
  166. * \brief Set an ast_party_id structure based on data in a From
  167. *
  168. * This makes use of \ref set_id_from_hdr for setting name and number. It uses
  169. * no information from the message in order to set privacy. It relies on endpoint
  170. * configuration for privacy information.
  171. *
  172. * \param rdata The incoming message
  173. * \param[out] id The ID to set
  174. * \retval 0 Succesfully set the party ID
  175. * \retval non-zero Could not set the party ID
  176. */
  177. static int set_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
  178. {
  179. pjsip_fromto_hdr *from = pjsip_msg_find_hdr(rdata->msg_info.msg,
  180. PJSIP_H_FROM, rdata->msg_info.msg->hdr.next);
  181. if (!from) {
  182. /* This had better not happen */
  183. return -1;
  184. }
  185. set_id_from_hdr(from, id);
  186. if (!id->number.valid) {
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. /*!
  192. * \internal
  193. * \brief Determine if a connected line update should be queued
  194. *
  195. * This uses information about the session and the ID that would be queued
  196. * in the connected line update in order to determine if we should queue
  197. * a connected line update.
  198. *
  199. * \param session The session whose channel we wish to queue the connected line update on
  200. * \param id The identification information that would be queued on the connected line update
  201. * \retval 0 We should not queue a connected line update
  202. * \retval non-zero We should queue a connected line update
  203. */
  204. static int should_queue_connected_line_update(const struct ast_sip_session *session, const struct ast_party_id *id)
  205. {
  206. /* Invalid number means no update */
  207. if (!id->number.valid) {
  208. return 0;
  209. }
  210. /* If the session has never communicated an update or if the
  211. * new ID has a different number than the session, then we
  212. * should queue an update
  213. */
  214. if (ast_strlen_zero(session->id.number.str) ||
  215. strcmp(session->id.number.str, id->number.str)) {
  216. return 1;
  217. }
  218. /* By making it to this point, it means the number is not enough
  219. * to determine if an update should be sent. Now we look at
  220. * the name
  221. */
  222. /* If the number couldn't warrant an update and the name is
  223. * invalid, then no update
  224. */
  225. if (!id->name.valid) {
  226. return 0;
  227. }
  228. /* If the name has changed or we don't have a name set for the
  229. * session, then we should send an update
  230. */
  231. if (ast_strlen_zero(session->id.name.str) ||
  232. strcmp(session->id.name.str, id->name.str)) {
  233. return 1;
  234. }
  235. /* Neither the name nor the number have changed. No update */
  236. return 0;
  237. }
  238. /*!
  239. * \internal
  240. * \brief Queue a connected line update on a session's channel.
  241. * \param session The session whose channel should have the connected line update queued upon.
  242. * \param id The identification information to place in the connected line update
  243. */
  244. static void queue_connected_line_update(struct ast_sip_session *session, const struct ast_party_id *id)
  245. {
  246. struct ast_party_connected_line connected;
  247. struct ast_party_caller caller;
  248. /* Fill connected line information */
  249. ast_party_connected_line_init(&connected);
  250. connected.id = *id;
  251. connected.id.tag = session->endpoint->id.self.tag;
  252. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  253. /* Save to channel driver copy */
  254. ast_party_id_copy(&session->id, &connected.id);
  255. /* Update our channel CALLERID() */
  256. ast_party_caller_init(&caller);
  257. caller.id = connected.id;
  258. caller.ani = connected.id;
  259. caller.ani2 = ast_channel_caller(session->channel)->ani2;
  260. ast_channel_set_caller_event(session->channel, &caller, NULL);
  261. /* Tell peer about the new connected line information. */
  262. ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
  263. }
  264. /*!
  265. * \internal
  266. * \brief Make updates to connected line information based on an incoming request.
  267. *
  268. * This will get identity information from an incoming request. Once the identification is
  269. * retrieved, we will check if the new information warrants a connected line update and queue
  270. * a connected line update if so.
  271. *
  272. * \param session The session on which we received an incoming request
  273. * \param rdata The incoming request
  274. */
  275. static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
  276. {
  277. struct ast_party_id id;
  278. if (!session->endpoint->id.trust_inbound) {
  279. return;
  280. }
  281. ast_party_id_init(&id);
  282. if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) {
  283. if (should_queue_connected_line_update(session, &id)) {
  284. queue_connected_line_update(session, &id);
  285. }
  286. }
  287. ast_party_id_free(&id);
  288. }
  289. /*!
  290. * \internal
  291. * \brief Session supplement callback on an incoming INVITE request
  292. *
  293. * If we are receiving an initial INVITE, then we will set the session's identity
  294. * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
  295. * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
  296. * function
  297. *
  298. * \param session The session that has received an INVITE
  299. * \param rdata The incoming INVITE
  300. */
  301. static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  302. {
  303. if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED) {
  304. /*
  305. * Initial inbound INVITE. Set the session ID directly
  306. * because the channel has not been created yet.
  307. */
  308. if (session->endpoint->id.trust_inbound
  309. && (!set_id_from_pai(rdata, &session->id)
  310. || !set_id_from_rpid(rdata, &session->id))) {
  311. ast_free(session->id.tag);
  312. session->id.tag = ast_strdup(session->endpoint->id.self.tag);
  313. return 0;
  314. }
  315. ast_party_id_copy(&session->id, &session->endpoint->id.self);
  316. if (!session->endpoint->id.self.number.valid) {
  317. set_id_from_from(rdata, &session->id);
  318. }
  319. } else {
  320. /* Reinvite. Check for changes to the ID and queue a connected line
  321. * update if necessary
  322. */
  323. update_incoming_connected_line(session, rdata);
  324. }
  325. return 0;
  326. }
  327. /*!
  328. * \internal
  329. * \brief Session supplement callback on INVITE response
  330. *
  331. * INVITE responses could result in queuing connected line updates.
  332. *
  333. * \param session The session on which communication is happening
  334. * \param rdata The incoming INVITE response
  335. */
  336. static void caller_id_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
  337. {
  338. if (!session->channel) {
  339. return;
  340. }
  341. update_incoming_connected_line(session, rdata);
  342. }
  343. /*!
  344. * \internal
  345. * \brief Set name and number information on an identity header.
  346. * \param pool Memory pool to use for string duplication
  347. * \param id_hdr A From, P-Asserted-Identity, or Remote-Party-ID header to modify
  348. * \param id The identity information to apply to the header
  349. */
  350. static void modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const struct ast_party_id *id)
  351. {
  352. pjsip_name_addr *id_name_addr;
  353. pjsip_sip_uri *id_uri;
  354. id_name_addr = (pjsip_name_addr *) id_hdr->uri;
  355. id_uri = pjsip_uri_get_uri(id_name_addr->uri);
  356. if (id->name.valid) {
  357. int name_buf_len = strlen(id->name.str) * 2 + 1;
  358. char *name_buf = ast_alloca(name_buf_len);
  359. ast_escape_quoted(id->name.str, name_buf, name_buf_len);
  360. pj_strdup2(pool, &id_name_addr->display, name_buf);
  361. }
  362. if (id->number.valid) {
  363. pj_strdup2(pool, &id_uri->user, id->number.str);
  364. }
  365. }
  366. /*!
  367. * \internal
  368. * \brief Create an identity header for an outgoing message
  369. * \param hdr_name The name of the header to create
  370. * \param tdata The message to place the header on
  371. * \param id The identification information for the new header
  372. * \return newly-created header
  373. */
  374. static pjsip_fromto_hdr *create_new_id_hdr(const pj_str_t *hdr_name, pjsip_tx_data *tdata, const struct ast_party_id *id)
  375. {
  376. pjsip_fromto_hdr *id_hdr;
  377. pjsip_fromto_hdr *base;
  378. pjsip_name_addr *id_name_addr;
  379. pjsip_sip_uri *id_uri;
  380. base = tdata->msg->type == PJSIP_REQUEST_MSG ? PJSIP_MSG_FROM_HDR(tdata->msg) :
  381. PJSIP_MSG_TO_HDR(tdata->msg);
  382. id_hdr = pjsip_from_hdr_create(tdata->pool);
  383. id_hdr->type = PJSIP_H_OTHER;
  384. pj_strdup(tdata->pool, &id_hdr->name, hdr_name);
  385. id_hdr->sname.slen = 0;
  386. id_name_addr = pjsip_uri_clone(tdata->pool, base->uri);
  387. id_uri = pjsip_uri_get_uri(id_name_addr->uri);
  388. if (id->name.valid) {
  389. int name_buf_len = strlen(id->name.str) * 2 + 1;
  390. char *name_buf = ast_alloca(name_buf_len);
  391. ast_escape_quoted(id->name.str, name_buf, name_buf_len);
  392. pj_strdup2(tdata->pool, &id_name_addr->display, name_buf);
  393. }
  394. pj_strdup2(tdata->pool, &id_uri->user, id->number.str);
  395. id_hdr->uri = (pjsip_uri *) id_name_addr;
  396. return id_hdr;
  397. }
  398. /*!
  399. * \internal
  400. * \brief Add a Privacy header to an outbound message
  401. *
  402. * When sending a P-Asserted-Identity header, if privacy is requested, then we
  403. * will need to indicate such by adding a Privacy header. Similarly, if no
  404. * privacy is requested, and a Privacy header already exists on the message,
  405. * then the old Privacy header should be removed.
  406. *
  407. * \param tdata The outbound message to add the Privacy header to
  408. * \param id The id information used to determine privacy
  409. */
  410. static void add_privacy_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
  411. {
  412. static const pj_str_t pj_privacy_name = { "Privacy", 7 };
  413. static const pj_str_t pj_privacy_value = { "id", 2 };
  414. pjsip_hdr *old_privacy;
  415. old_privacy = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_privacy_name, NULL);
  416. if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
  417. (id->number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
  418. if (old_privacy) {
  419. pj_list_erase(old_privacy);
  420. }
  421. } else if (!old_privacy) {
  422. pjsip_generic_string_hdr *privacy_hdr = pjsip_generic_string_hdr_create(
  423. tdata->pool, &pj_privacy_name, &pj_privacy_value);
  424. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)privacy_hdr);
  425. }
  426. }
  427. /*!
  428. * \internal
  429. * \brief Add a P-Asserted-Identity header to an outbound message
  430. * \param tdata The message to add the header to
  431. * \param id The identification information used to populate the header
  432. */
  433. static void add_pai_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
  434. {
  435. static const pj_str_t pj_pai_name = { "P-Asserted-Identity", 19 };
  436. pjsip_fromto_hdr *pai_hdr;
  437. pjsip_fromto_hdr *old_pai;
  438. if (!id->number.valid) {
  439. return;
  440. }
  441. /* Since inv_session reuses responses, we have to make sure there's not already
  442. * a P-Asserted-Identity present. If there is, we just modify the old one.
  443. */
  444. old_pai = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_pai_name, NULL);
  445. if (old_pai) {
  446. modify_id_header(tdata->pool, old_pai, id);
  447. add_privacy_header(tdata, id);
  448. return;
  449. }
  450. pai_hdr = create_new_id_hdr(&pj_pai_name, tdata, id);
  451. if (!pai_hdr) {
  452. return;
  453. }
  454. add_privacy_header(tdata, id);
  455. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pai_hdr);
  456. }
  457. /*!
  458. * \internal
  459. * \brief Add privacy and screen parameters to a Remote-Party-ID header.
  460. *
  461. * If privacy is requested, then the privacy and screen parameters need to
  462. * reflect this. Similarly, if no privacy or screening is to be communicated,
  463. * we need to make sure that any previously set values are updated.
  464. *
  465. * \param tdata The message where the Remote-Party-ID header is
  466. * \param hdr The header on which the parameters are being added
  467. * \param id The identification information used to determine privacy
  468. */
  469. static void add_privacy_params(pjsip_tx_data *tdata, pjsip_fromto_hdr *hdr, const struct ast_party_id *id)
  470. {
  471. static const pj_str_t privacy_str = { "privacy", 7 };
  472. static const pj_str_t screen_str = { "screen", 6 };
  473. static const pj_str_t privacy_full_str = { "full", 4 };
  474. static const pj_str_t privacy_off_str = { "off", 3 };
  475. static const pj_str_t screen_yes_str = { "yes", 3 };
  476. static const pj_str_t screen_no_str = { "no", 2 };
  477. pjsip_param *old_privacy;
  478. pjsip_param *old_screen;
  479. pjsip_param *privacy;
  480. pjsip_param *screen;
  481. old_privacy = pjsip_param_find(&hdr->other_param, &privacy_str);
  482. old_screen = pjsip_param_find(&hdr->other_param, &screen_str);
  483. if (!old_privacy) {
  484. privacy = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
  485. privacy->name = privacy_str;
  486. pj_list_insert_before(&hdr->other_param, privacy);
  487. } else {
  488. privacy = old_privacy;
  489. }
  490. if (!old_screen) {
  491. screen = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
  492. screen->name = screen_str;
  493. pj_list_insert_before(&hdr->other_param, screen);
  494. } else {
  495. screen = old_screen;
  496. }
  497. if ((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
  498. (id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
  499. privacy->value = privacy_off_str;
  500. } else {
  501. privacy->value = privacy_full_str;
  502. }
  503. if ((id->name.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN &&
  504. (id->number.presentation & AST_PRES_NUMBER_TYPE) == AST_PRES_USER_NUMBER_PASSED_SCREEN) {
  505. screen->value = screen_yes_str;
  506. } else {
  507. screen->value = screen_no_str;
  508. }
  509. }
  510. /*!
  511. * \internal
  512. * \brief Add a Remote-Party-ID header to an outbound message
  513. * \param tdata The message to add the header to
  514. * \param id The identification information used to populate the header
  515. */
  516. static void add_rpid_header(pjsip_tx_data *tdata, const struct ast_party_id *id)
  517. {
  518. static const pj_str_t pj_rpid_name = { "Remote-Party-ID", 15 };
  519. pjsip_fromto_hdr *rpid_hdr;
  520. pjsip_fromto_hdr *old_rpid;
  521. if (!id->number.valid) {
  522. return;
  523. }
  524. /* Since inv_session reuses responses, we have to make sure there's not already
  525. * a P-Asserted-Identity present. If there is, we just modify the old one.
  526. */
  527. old_rpid = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_rpid_name, NULL);
  528. if (old_rpid) {
  529. modify_id_header(tdata->pool, old_rpid, id);
  530. add_privacy_params(tdata, old_rpid, id);
  531. return;
  532. }
  533. rpid_hdr = create_new_id_hdr(&pj_rpid_name, tdata, id);
  534. if (!rpid_hdr) {
  535. return;
  536. }
  537. add_privacy_params(tdata, rpid_hdr, id);
  538. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)rpid_hdr);
  539. }
  540. /*!
  541. * \internal
  542. * \brief Add any appropriate identification headers to an outbound SIP message
  543. *
  544. * This will determine if an outbound message should have identification headers and
  545. * will add the appropriately configured headers
  546. *
  547. * \param session The session on which we will be sending the message
  548. * \param tdata The outbound message
  549. * \param The identity information to place on the message
  550. */
  551. static void add_id_headers(const struct ast_sip_session *session, pjsip_tx_data *tdata, const struct ast_party_id *id)
  552. {
  553. if (((id->name.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED ||
  554. (id->number.presentation & AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED) &&
  555. !session->endpoint->id.trust_outbound) {
  556. return;
  557. }
  558. if (session->endpoint->id.send_pai) {
  559. add_pai_header(tdata, id);
  560. }
  561. if (session->endpoint->id.send_rpid) {
  562. add_rpid_header(tdata, id);
  563. }
  564. }
  565. /*!
  566. * \internal
  567. * \brief Session supplement callback for outgoing INVITE requests
  568. *
  569. * For an initial INVITE request, we may change the From header to appropriately
  570. * reflect the identity information. On all INVITEs (initial and reinvite) we may
  571. * add other identity headers such as P-Asserted-Identity and Remote-Party-ID based
  572. * on configuration and privacy settings
  573. *
  574. * \param session The session on which the INVITE will be sent
  575. * \param tdata The outbound INVITE request
  576. */
  577. static void caller_id_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  578. {
  579. struct ast_party_id effective_id;
  580. struct ast_party_id connected_id;
  581. if (!session->channel) {
  582. return;
  583. }
  584. /* Must do a deep copy unless we hold the channel lock the entire time. */
  585. ast_party_id_init(&connected_id);
  586. ast_channel_lock(session->channel);
  587. effective_id = ast_channel_connected_effective_id(session->channel);
  588. ast_party_id_copy(&connected_id, &effective_id);
  589. ast_channel_unlock(session->channel);
  590. if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED &&
  591. ast_strlen_zero(session->endpoint->fromuser) &&
  592. (session->endpoint->id.trust_outbound ||
  593. ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
  594. (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
  595. /* Only change the From header on the initial outbound INVITE. Switching it
  596. * mid-call might confuse some UAs.
  597. */
  598. pjsip_fromto_hdr *from;
  599. pjsip_dialog *dlg;
  600. from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, tdata->msg->hdr.next);
  601. dlg = session->inv_session->dlg;
  602. modify_id_header(tdata->pool, from, &connected_id);
  603. modify_id_header(dlg->pool, dlg->local.info, &connected_id);
  604. }
  605. add_id_headers(session, tdata, &connected_id);
  606. ast_party_id_free(&connected_id);
  607. }
  608. /*!
  609. * \internal
  610. * \brief Session supplement for outgoing INVITE response
  611. *
  612. * This will add P-Asserted-Identity and Remote-Party-ID headers if necessary
  613. *
  614. * \param session The session on which the INVITE response is to be sent
  615. * \param tdata The outbound INVITE response
  616. */
  617. static void caller_id_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  618. {
  619. struct ast_party_id effective_id;
  620. struct ast_party_id connected_id;
  621. if (!session->channel) {
  622. return;
  623. }
  624. /* Must do a deep copy unless we hold the channel lock the entire time. */
  625. ast_party_id_init(&connected_id);
  626. ast_channel_lock(session->channel);
  627. effective_id = ast_channel_connected_effective_id(session->channel);
  628. ast_party_id_copy(&connected_id, &effective_id);
  629. ast_channel_unlock(session->channel);
  630. add_id_headers(session, tdata, &connected_id);
  631. ast_party_id_free(&connected_id);
  632. }
  633. static struct ast_sip_session_supplement caller_id_supplement = {
  634. .method = "INVITE,UPDATE",
  635. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
  636. .incoming_request = caller_id_incoming_request,
  637. .incoming_response = caller_id_incoming_response,
  638. .outgoing_request = caller_id_outgoing_request,
  639. .outgoing_response = caller_id_outgoing_response,
  640. };
  641. static int load_module(void)
  642. {
  643. CHECK_PJSIP_SESSION_MODULE_LOADED();
  644. ast_sip_session_register_supplement(&caller_id_supplement);
  645. return AST_MODULE_LOAD_SUCCESS;
  646. }
  647. static int unload_module(void)
  648. {
  649. ast_sip_session_unregister_supplement(&caller_id_supplement);
  650. return 0;
  651. }
  652. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Caller ID Support",
  653. .support_level = AST_MODULE_SUPPORT_CORE,
  654. .load = load_module,
  655. .unload = unload_module,
  656. .load_pri = AST_MODPRI_APP_DEPEND,
  657. );