pjsip_global_headers.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "asterisk.h"
  19. #include <pjsip.h>
  20. #include <pjlib.h>
  21. #include "asterisk/res_pjsip.h"
  22. #include "asterisk/linkedlists.h"
  23. #include "include/res_pjsip_private.h"
  24. static pj_status_t add_request_headers(pjsip_tx_data *tdata);
  25. static pj_status_t add_response_headers(pjsip_tx_data *tdata);
  26. /*!
  27. * \brief Indicator we've already handled a specific request/response
  28. *
  29. * PJSIP tends to reuse requests and responses. If we already have added
  30. * headers to a request or response, we mark the message with this value
  31. * so that we know not to re-add the headers again.
  32. */
  33. static unsigned int handled_id = 0xCA115785;
  34. static pjsip_module global_header_mod = {
  35. .name = {"Global headers", 13},
  36. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  37. .on_tx_request = add_request_headers,
  38. .on_tx_response = add_response_headers,
  39. };
  40. struct header {
  41. AST_DECLARE_STRING_FIELDS(
  42. AST_STRING_FIELD(name);
  43. AST_STRING_FIELD(value);
  44. );
  45. AST_LIST_ENTRY(header) next;
  46. };
  47. static struct header *alloc_header(const char *name, const char *value)
  48. {
  49. struct header *alloc;
  50. alloc = ast_calloc_with_stringfields(1, struct header, 32);
  51. if (!alloc) {
  52. return NULL;
  53. }
  54. ast_string_field_set(alloc, name, name);
  55. ast_string_field_set(alloc, value, value);
  56. return alloc;
  57. }
  58. static void destroy_header(struct header *to_destroy)
  59. {
  60. ast_string_field_free_memory(to_destroy);
  61. ast_free(to_destroy);
  62. }
  63. AST_RWLIST_HEAD(header_list, header);
  64. static struct header_list request_headers;
  65. static struct header_list response_headers;
  66. static void add_headers_to_message(struct header_list *headers, pjsip_tx_data *tdata)
  67. {
  68. struct header *iter;
  69. SCOPED_LOCK(lock, headers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  70. if (tdata->mod_data[global_header_mod.id] == &handled_id) {
  71. return;
  72. }
  73. AST_LIST_TRAVERSE(headers, iter, next) {
  74. pj_str_t name;
  75. pjsip_generic_string_hdr *hdr;
  76. hdr = pjsip_msg_find_hdr_by_name(tdata->msg, pj_cstr(&name, iter->name), NULL);
  77. if (hdr) {
  78. continue;
  79. }
  80. ast_sip_add_header(tdata, iter->name, iter->value);
  81. };
  82. tdata->mod_data[global_header_mod.id] = &handled_id;
  83. }
  84. static pj_status_t add_request_headers(pjsip_tx_data *tdata)
  85. {
  86. add_headers_to_message(&request_headers, tdata);
  87. return PJ_SUCCESS;
  88. }
  89. static pj_status_t add_response_headers(pjsip_tx_data *tdata)
  90. {
  91. add_headers_to_message(&response_headers, tdata);
  92. return PJ_SUCCESS;
  93. }
  94. static void remove_header(struct header_list *headers, const char *to_remove)
  95. {
  96. struct header *iter;
  97. AST_LIST_TRAVERSE_SAFE_BEGIN(headers, iter, next) {
  98. if (!strcasecmp(iter->name, to_remove)) {
  99. AST_LIST_REMOVE_CURRENT(next);
  100. destroy_header(iter);
  101. break;
  102. }
  103. }
  104. AST_LIST_TRAVERSE_SAFE_END;
  105. }
  106. static int add_header(struct header_list *headers, const char *name, const char *value, int replace)
  107. {
  108. struct header *to_add = NULL;
  109. if (!ast_strlen_zero(value)) {
  110. to_add = alloc_header(name, value);
  111. if (!to_add) {
  112. return -1;
  113. }
  114. }
  115. AST_RWLIST_WRLOCK(headers);
  116. if (replace) {
  117. remove_header(headers, name);
  118. }
  119. if (to_add) {
  120. AST_LIST_INSERT_TAIL(headers, to_add, next);
  121. }
  122. AST_RWLIST_UNLOCK(headers);
  123. return 0;
  124. }
  125. int ast_sip_add_global_request_header(const char *name, const char *value, int replace)
  126. {
  127. return add_header(&request_headers, name, value, replace);
  128. }
  129. int ast_sip_add_global_response_header(const char *name, const char *value, int replace)
  130. {
  131. return add_header(&response_headers, name, value, replace);
  132. }
  133. void ast_sip_initialize_global_headers(void)
  134. {
  135. AST_RWLIST_HEAD_INIT(&request_headers);
  136. AST_RWLIST_HEAD_INIT(&response_headers);
  137. internal_sip_register_service(&global_header_mod);
  138. }
  139. static void destroy_headers(struct header_list *headers)
  140. {
  141. struct header *iter;
  142. while ((iter = AST_RWLIST_REMOVE_HEAD(headers, next))) {
  143. destroy_header(iter);
  144. }
  145. AST_RWLIST_HEAD_DESTROY(headers);
  146. }
  147. void ast_sip_destroy_global_headers(void)
  148. {
  149. destroy_headers(&request_headers);
  150. destroy_headers(&response_headers);
  151. internal_sip_unregister_service(&global_header_mod);
  152. }