res_pjsip_header_funcs.c 19 KB

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