res_pjsip_header_funcs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.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/pbx.h"
  31. #include "asterisk/app.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/utils.h"
  34. /*** DOCUMENTATION
  35. <function name="PJSIP_HEADER" language="en_US">
  36. <synopsis>
  37. Gets headers from an inbound PJSIP channel. Adds, updates or removes the
  38. specified SIP header from an outbound PJSIP channel.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="action" required="true">
  42. <enumlist>
  43. <enum name="read"><para>Returns instance <replaceable>number</replaceable>
  44. of header <replaceable>name</replaceable>.</para></enum>
  45. <enum name="add"><para>Adds a new header <replaceable>name</replaceable>
  46. to this session.</para></enum>
  47. <enum name="update"><para>Updates instance <replaceable>number</replaceable>
  48. of header <replaceable>name</replaceable> to a new value.
  49. The header must already exist.</para></enum>
  50. <enum name="remove"><para>Removes all instances of previously added headers
  51. whose names match <replaceable>name</replaceable>. A <literal>*</literal>
  52. may be appended to <replaceable>name</replaceable> to remove all headers
  53. <emphasis>beginning with</emphasis> <replaceable>name</replaceable>.
  54. <replaceable>name</replaceable> may be set to a single <literal>*</literal>
  55. to clear <emphasis>all</emphasis> previously added headers. In all cases,
  56. the number of headers actually removed is returned.</para></enum>
  57. </enumlist>
  58. </parameter>
  59. <parameter name="name" required="true"><para>The name of the header.</para></parameter>
  60. <parameter name="number" required="false">
  61. <para>If there's more than 1 header with the same name, this specifies which header
  62. to read or update. If not specified, defaults to <literal>1</literal> meaning
  63. the first matching header. Not valid for <literal>add</literal> or
  64. <literal>remove</literal>.</para>
  65. </parameter>
  66. </syntax>
  67. <description>
  68. <para>PJSIP_HEADER allows you to read specific SIP headers from the inbound
  69. PJSIP channel as well as write(add, update, remove) headers on the outbound
  70. channel. One exception is that you can read headers that you have already
  71. added on the outbound channel.</para>
  72. <para>Examples:</para>
  73. <para>;</para>
  74. <para>; Set 'somevar' to the value of the 'From' header.</para>
  75. <para>exten => 1,1,Set(somevar=${PJSIP_HEADER(read,From)})</para>
  76. <para>;</para>
  77. <para>; Set 'via2' to the value of the 2nd 'Via' header.</para>
  78. <para>exten => 1,1,Set(via2=${PJSIP_HEADER(read,Via,2)})</para>
  79. <para>;</para>
  80. <para>; Add an 'X-Myheader' header with the value of 'myvalue'.</para>
  81. <para>exten => 1,1,Set(PJSIP_HEADER(add,X-MyHeader)=myvalue)</para>
  82. <para>;</para>
  83. <para>; Add an 'X-Myheader' header with an empty value.</para>
  84. <para>exten => 1,1,Set(PJSIP_HEADER(add,X-MyHeader)=)</para>
  85. <para>;</para>
  86. <para>; Update the value of the header named 'X-Myheader' to 'newvalue'.</para>
  87. <para>; 'X-Myheader' must already exist or the call will fail.</para>
  88. <para>exten => 1,1,Set(PJSIP_HEADER(update,X-MyHeader)=newvalue)</para>
  89. <para>;</para>
  90. <para>; Remove all headers whose names exactly match 'X-MyHeader'.</para>
  91. <para>exten => 1,1,Set(PJSIP_HEADER(remove,X-MyHeader)=)</para>
  92. <para>;</para>
  93. <para>; Remove all headers that begin with 'X-My'.</para>
  94. <para>exten => 1,1,Set(PJSIP_HEADER(remove,X-My*)=)</para>
  95. <para>;</para>
  96. <para>; Remove all previously added headers.</para>
  97. <para>exten => 1,1,Set(PJSIP_HEADER(remove,*)=)</para>
  98. <para>;</para>
  99. <note><para>The <literal>remove</literal> action can be called by reading
  100. <emphasis>or</emphasis> writing PJSIP_HEADER.</para>
  101. <para>;</para>
  102. <para>; Display the number of headers removed</para>
  103. <para>exten => 1,1,Verbose( Removed ${PJSIP_HEADER(remove,X-MyHeader)} headers)</para>
  104. <para>;</para>
  105. <para>; Set a variable to the number of headers removed</para>
  106. <para>exten => 1,1,Set(count=${PJSIP_HEADER(remove,X-MyHeader)})</para>
  107. <para>;</para>
  108. <para>; Just remove them ignoring any count</para>
  109. <para>exten => 1,1,Set(=${PJSIP_HEADER(remove,X-MyHeader)})</para>
  110. <para>exten => 1,1,Set(PJSIP_HEADER(remove,X-MyHeader)=)</para>
  111. <para>;</para>
  112. </note>
  113. <note><para>If you call PJSIP_HEADER in a normal dialplan context you'll be
  114. operating on the <emphasis>caller's (incoming)</emphasis> channel which
  115. may not be what you want. To operate on the <emphasis>callee's (outgoing)</emphasis>
  116. channel call PJSIP_HEADER in a pre-dial handler. </para>
  117. <para>Example:</para>
  118. <para>;</para>
  119. <para>[handler]</para>
  120. <para>exten => addheader,1,Set(PJSIP_HEADER(add,X-MyHeader)=myvalue)</para>
  121. <para>exten => addheader,2,Set(PJSIP_HEADER(add,X-MyHeader2)=myvalue2)</para>
  122. <para>;</para>
  123. <para>[somecontext]</para>
  124. <para>exten => 1,1,Dial(PJSIP/${EXTEN},,b(handler^addheader^1))</para>
  125. <para>;</para>
  126. </note>
  127. </description>
  128. </function>
  129. ***/
  130. /*! \brief Linked list for accumulating headers */
  131. struct hdr_list_entry {
  132. pjsip_hdr *hdr;
  133. AST_LIST_ENTRY(hdr_list_entry) nextptr;
  134. };
  135. AST_LIST_HEAD_NOLOCK(hdr_list, hdr_list_entry);
  136. /*! \brief Datastore for saving headers */
  137. static const struct ast_datastore_info header_datastore = {
  138. .type = "header_datastore",
  139. };
  140. /*! \brief Data structure used for ast_sip_push_task_wait_serializer */
  141. struct header_data {
  142. struct ast_sip_channel_pvt *channel;
  143. char *header_name;
  144. const char *header_value;
  145. char *buf;
  146. int header_number;
  147. size_t len;
  148. };
  149. /*!
  150. * \internal
  151. * \brief Insert the header pointers into the linked list.
  152. *
  153. * For each header in the message, allocate a list entry,
  154. * clone the header, then insert the entry.
  155. */
  156. static int insert_headers(pj_pool_t * pool, struct hdr_list *list, pjsip_msg * msg)
  157. {
  158. pjsip_hdr *hdr = msg->hdr.next;
  159. struct hdr_list_entry *le;
  160. while (hdr && hdr != &msg->hdr) {
  161. le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
  162. le->hdr = pjsip_hdr_clone(pool, hdr);
  163. AST_LIST_INSERT_TAIL(list, le, nextptr);
  164. hdr = hdr->next;
  165. }
  166. return 0;
  167. }
  168. /*!
  169. * \internal
  170. * \brief Session supplement callback on an incoming INVITE request
  171. *
  172. * Retrieve the header_datastore from the session or create one if it doesn't exist.
  173. * Create and initialize the list if needed.
  174. * Insert the headers.
  175. */
  176. static int incoming_request(struct ast_sip_session *session, pjsip_rx_data * rdata)
  177. {
  178. pj_pool_t *pool = session->inv_session->dlg->pool;
  179. RAII_VAR(struct ast_datastore *, datastore,
  180. ast_sip_session_get_datastore(session, header_datastore.type), ao2_cleanup);
  181. if (!datastore) {
  182. if (!(datastore =
  183. ast_sip_session_alloc_datastore(&header_datastore, header_datastore.type))
  184. ||
  185. !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
  186. ast_sip_session_add_datastore(session, datastore)) {
  187. ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
  188. return 0;
  189. }
  190. AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
  191. }
  192. insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
  193. return 0;
  194. }
  195. /*!
  196. * \internal
  197. * \brief Search list for nth occurrence of specific header.
  198. */
  199. static pjsip_hdr *find_header(struct hdr_list *list, const char *header_name,
  200. int header_number)
  201. {
  202. struct hdr_list_entry *le;
  203. pjsip_hdr *hdr = NULL;
  204. int i = 1;
  205. if (!list || ast_strlen_zero(header_name) || header_number < 1) {
  206. return NULL;
  207. }
  208. AST_LIST_TRAVERSE(list, le, nextptr) {
  209. if (pj_stricmp2(&le->hdr->name, header_name) == 0 && i++ == header_number) {
  210. hdr = le->hdr;
  211. break;
  212. }
  213. }
  214. return hdr;
  215. }
  216. /*!
  217. * \internal
  218. * \brief Implements PJSIP_HEADER 'read' by searching the for the requested header.
  219. *
  220. * Retrieve the header_datastore.
  221. * Search for the nth matching header.
  222. * Validate the pjsip_hdr found.
  223. * Parse pjsip_hdr into a name and value.
  224. * Return the value.
  225. */
  226. static int read_header(void *obj)
  227. {
  228. struct header_data *data = obj;
  229. pjsip_hdr *hdr = NULL;
  230. char *pj_hdr_string;
  231. size_t pj_hdr_string_len;
  232. char *p;
  233. size_t plen;
  234. RAII_VAR(struct ast_datastore *, datastore,
  235. ast_sip_session_get_datastore(data->channel->session, header_datastore.type),
  236. ao2_cleanup);
  237. if (!datastore || !datastore->data) {
  238. ast_debug(1, "There was no datastore from which to read headers.\n");
  239. return -1;
  240. }
  241. hdr = find_header((struct hdr_list *) datastore->data, data->header_name,
  242. data->header_number);
  243. if (!hdr) {
  244. ast_debug(1, "There was no header named %s.\n", data->header_name);
  245. return -1;
  246. }
  247. pj_hdr_string = ast_alloca(data->len);
  248. pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len);
  249. pj_hdr_string[pj_hdr_string_len] = '\0';
  250. p = strchr(pj_hdr_string, ':');
  251. if (!p) {
  252. ast_log(AST_LOG_ERROR,
  253. "A malformed header was returned from pjsip_hdr_print_on.\n");
  254. return -1;
  255. }
  256. ++p;
  257. p = ast_strip(p);
  258. plen = strlen(p);
  259. if (plen + 1 > data->len) {
  260. ast_log(AST_LOG_ERROR,
  261. "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,
  262. data->len);
  263. return -1;
  264. }
  265. ast_copy_string(data->buf, p, data->len);
  266. return 0;
  267. }
  268. /*!
  269. * \internal
  270. * \brief Implements PJSIP_HEADER 'add' by inserting the specified header into thge list.
  271. *
  272. * Retrieve the header_datastore from the session or create one if it doesn't exist.
  273. * Create and initialize the list if needed.
  274. * Create the pj_strs for name and value.
  275. * Create pjsip_msg and hdr_list_entry.
  276. * Add the entry to the list.
  277. */
  278. static int add_header(void *obj)
  279. {
  280. struct header_data *data = obj;
  281. struct ast_sip_session *session = data->channel->session;
  282. pj_pool_t *pool = session->inv_session->dlg->pool;
  283. pj_str_t pj_header_name;
  284. pj_str_t pj_header_value;
  285. struct hdr_list_entry *le;
  286. struct hdr_list *list;
  287. RAII_VAR(struct ast_datastore *, datastore,
  288. ast_sip_session_get_datastore(session, header_datastore.type), ao2_cleanup);
  289. if (!datastore) {
  290. if (!(datastore = ast_sip_session_alloc_datastore(&header_datastore,
  291. header_datastore.type))
  292. || !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list)))
  293. || ast_sip_session_add_datastore(session, datastore)) {
  294. ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
  295. return -1;
  296. }
  297. AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
  298. }
  299. ast_debug(1, "Adding header %s with value %s\n", data->header_name,
  300. data->header_value);
  301. pj_cstr(&pj_header_name, data->header_name);
  302. pj_cstr(&pj_header_value, data->header_value);
  303. le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
  304. le->hdr = (pjsip_hdr *) pjsip_generic_string_hdr_create(pool, &pj_header_name,
  305. &pj_header_value);
  306. list = datastore->data;
  307. AST_LIST_INSERT_TAIL(list, le, nextptr);
  308. return 0;
  309. }
  310. /*!
  311. * \internal
  312. * \brief Implements PJSIP_HEADER 'update' by finding the specified header and updating it.
  313. *
  314. * Retrieve the header_datastore from the session or create one if it doesn't exist.
  315. * Create and initialize the list if needed.
  316. * Create the pj_strs for name and value.
  317. * Create pjsip_msg and hdr_list_entry.
  318. * Add the entry to the list.
  319. */
  320. static int update_header(void *obj)
  321. {
  322. struct header_data *data = obj;
  323. pjsip_hdr *hdr = NULL;
  324. RAII_VAR(struct ast_datastore *, datastore,
  325. ast_sip_session_get_datastore(data->channel->session, header_datastore.type),
  326. ao2_cleanup);
  327. if (!datastore || !datastore->data) {
  328. ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
  329. return -1;
  330. }
  331. hdr = find_header((struct hdr_list *) datastore->data, data->header_name,
  332. data->header_number);
  333. if (!hdr) {
  334. ast_log(AST_LOG_ERROR, "There was no header named %s.\n", data->header_name);
  335. return -1;
  336. }
  337. pj_strcpy2(&((pjsip_generic_string_hdr *) hdr)->hvalue, data->header_value);
  338. return 0;
  339. }
  340. /*!
  341. * \internal
  342. * \brief Implements PJSIP_HEADER 'remove' by finding the specified header and removing it.
  343. *
  344. * Retrieve the header_datastore from the session. Fail if it doesn't exist.
  345. * If the header_name is exactly '*', the entire list is simply destroyed.
  346. * Otherwise search the list for the matching header name which may be a partial name.
  347. */
  348. static int remove_header(void *obj)
  349. {
  350. struct header_data *data = obj;
  351. size_t len = strlen(data->header_name);
  352. struct hdr_list *list;
  353. struct hdr_list_entry *le;
  354. int removed_count = 0;
  355. RAII_VAR(struct ast_datastore *, datastore,
  356. ast_sip_session_get_datastore(data->channel->session, header_datastore.type),
  357. ao2_cleanup);
  358. if (!datastore || !datastore->data) {
  359. ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
  360. return -1;
  361. }
  362. list = datastore->data;
  363. AST_LIST_TRAVERSE_SAFE_BEGIN(list, le, nextptr) {
  364. if (data->header_name[len - 1] == '*') {
  365. if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0) {
  366. AST_LIST_REMOVE_CURRENT(nextptr);
  367. removed_count++;
  368. }
  369. } else {
  370. if (pj_stricmp2(&le->hdr->name, data->header_name) == 0) {
  371. AST_LIST_REMOVE_CURRENT(nextptr);
  372. removed_count++;
  373. }
  374. }
  375. }
  376. AST_LIST_TRAVERSE_SAFE_END;
  377. if (data->buf && data->len) {
  378. snprintf(data->buf, data->len, "%d", removed_count);
  379. }
  380. return 0;
  381. }
  382. /*!
  383. * \brief Implements function 'read' callback.
  384. *
  385. * Valid actions are 'read' and 'remove'.
  386. */
  387. static int func_read_header(struct ast_channel *chan, const char *function, char *data,
  388. char *buf, size_t len)
  389. {
  390. struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
  391. struct header_data header_data;
  392. int number;
  393. AST_DECLARE_APP_ARGS(args,
  394. AST_APP_ARG(action);
  395. AST_APP_ARG(header_name); AST_APP_ARG(header_number););
  396. AST_STANDARD_APP_ARGS(args, data);
  397. if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
  398. ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
  399. return -1;
  400. }
  401. if (ast_strlen_zero(args.action)) {
  402. ast_log(AST_LOG_ERROR, "This function requires an action.\n");
  403. return -1;
  404. }
  405. if (ast_strlen_zero(args.header_name)) {
  406. ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
  407. return -1;
  408. }
  409. if (!args.header_number) {
  410. number = 1;
  411. } else {
  412. sscanf(args.header_number, "%30d", &number);
  413. if (number < 1) {
  414. number = 1;
  415. }
  416. }
  417. header_data.channel = channel;
  418. header_data.header_name = args.header_name;
  419. header_data.header_number = number;
  420. header_data.header_value = NULL;
  421. header_data.buf = buf;
  422. header_data.len = len;
  423. if (!strcasecmp(args.action, "read")) {
  424. return ast_sip_push_task_wait_serializer(channel->session->serializer,
  425. read_header, &header_data);
  426. } else if (!strcasecmp(args.action, "remove")) {
  427. return ast_sip_push_task_wait_serializer(channel->session->serializer,
  428. remove_header, &header_data);
  429. } else {
  430. ast_log(AST_LOG_ERROR,
  431. "Unknown action '%s' is not valid, must be 'read' or 'remove'.\n",
  432. args.action);
  433. return -1;
  434. }
  435. }
  436. /*!
  437. * \brief Implements function 'write' callback.
  438. *
  439. * Valid actions are 'add', 'update' and 'remove'.
  440. */
  441. static int func_write_header(struct ast_channel *chan, const char *cmd, char *data,
  442. const char *value)
  443. {
  444. struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
  445. struct header_data header_data;
  446. int header_number;
  447. AST_DECLARE_APP_ARGS(args,
  448. AST_APP_ARG(action);
  449. AST_APP_ARG(header_name); AST_APP_ARG(header_number););
  450. AST_STANDARD_APP_ARGS(args, data);
  451. if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
  452. ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
  453. return -1;
  454. }
  455. if (ast_strlen_zero(args.action)) {
  456. ast_log(AST_LOG_ERROR, "This function requires an action.\n");
  457. return -1;
  458. }
  459. if (ast_strlen_zero(args.header_name)) {
  460. ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
  461. return -1;
  462. }
  463. if (!args.header_number) {
  464. header_number = 1;
  465. } else {
  466. sscanf(args.header_number, "%30d", &header_number);
  467. if (header_number < 1) {
  468. header_number = 1;
  469. }
  470. }
  471. header_data.channel = channel;
  472. header_data.header_name = args.header_name;
  473. header_data.header_number = header_number;
  474. header_data.header_value = value;
  475. header_data.buf = NULL;
  476. header_data.len = 0;
  477. if (!strcasecmp(args.action, "add")) {
  478. return ast_sip_push_task_wait_serializer(channel->session->serializer,
  479. add_header, &header_data);
  480. } else if (!strcasecmp(args.action, "update")) {
  481. return ast_sip_push_task_wait_serializer(channel->session->serializer,
  482. update_header, &header_data);
  483. } else if (!strcasecmp(args.action, "remove")) {
  484. return ast_sip_push_task_wait_serializer(channel->session->serializer,
  485. remove_header, &header_data);
  486. } else {
  487. ast_log(AST_LOG_ERROR,
  488. "Unknown action '%s' is not valid, must be 'add', 'update', or 'remove'.\n",
  489. args.action);
  490. return -1;
  491. }
  492. }
  493. static struct ast_custom_function pjsip_header_function = {
  494. .name = "PJSIP_HEADER",
  495. .read = func_read_header,
  496. .write = func_write_header,
  497. };
  498. /*!
  499. * \internal
  500. * \brief Session supplement callback for outgoing INVITE requests
  501. *
  502. * Retrieve the header_datastore from the session.
  503. * Add each header in the list to the outgoing message.
  504. *
  505. * These pjsip_hdr structures will have been created by add_header.
  506. * Because outgoing_request may be called more than once with the same header
  507. * list (as in the case of an authentication exchange), each pjsip_hdr structure
  508. * MUST be newly cloned for each outgoing message.
  509. */
  510. static void outgoing_request(struct ast_sip_session *session, pjsip_tx_data * tdata)
  511. {
  512. pj_pool_t *pool = session->inv_session->dlg->pool;
  513. struct hdr_list *list;
  514. struct hdr_list_entry *le;
  515. RAII_VAR(struct ast_datastore *, datastore,
  516. ast_sip_session_get_datastore(session, header_datastore.type), ao2_cleanup);
  517. if (!datastore || !datastore->data ||
  518. (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED)) {
  519. return;
  520. }
  521. list = datastore->data;
  522. AST_LIST_TRAVERSE(list, le, nextptr) {
  523. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) pjsip_hdr_clone(pool, le->hdr));
  524. }
  525. ast_sip_session_remove_datastore(session, datastore->uid);
  526. }
  527. static struct ast_sip_session_supplement header_funcs_supplement = {
  528. .method = "INVITE",
  529. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
  530. .incoming_request = incoming_request,
  531. .outgoing_request = outgoing_request,
  532. };
  533. static int load_module(void)
  534. {
  535. CHECK_PJSIP_SESSION_MODULE_LOADED();
  536. ast_sip_session_register_supplement(&header_funcs_supplement);
  537. ast_custom_function_register(&pjsip_header_function);
  538. return AST_MODULE_LOAD_SUCCESS;
  539. }
  540. static int unload_module(void)
  541. {
  542. ast_custom_function_unregister(&pjsip_header_function);
  543. ast_sip_session_unregister_supplement(&header_funcs_supplement);
  544. return 0;
  545. }
  546. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Header Functions",
  547. .support_level = AST_MODULE_SUPPORT_CORE,
  548. .load = load_module,
  549. .unload = unload_module,
  550. .load_pri = AST_MODPRI_APP_DEPEND,);