res_pjsip_caller_id.c 25 KB

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