res_pjsip_rfc3326.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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/module.h"
  30. #include "asterisk/causes.h"
  31. #include "asterisk/threadpool.h"
  32. static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  33. {
  34. static const pj_str_t str_reason = { "Reason", 6 };
  35. pjsip_generic_string_hdr *header;
  36. char buf[20];
  37. char *cause;
  38. char *text;
  39. int code;
  40. header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
  41. for (; header;
  42. header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, header->next)) {
  43. ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
  44. cause = ast_skip_blanks(buf);
  45. if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
  46. continue;
  47. }
  48. /* If text is present get rid of it */
  49. if ((text = strstr(cause, ";"))) {
  50. *text = '\0';
  51. }
  52. if (sscanf(cause, "cause=%30d", &code) != 1) {
  53. continue;
  54. }
  55. ast_channel_hangupcause_set(session->channel, code & 0x7f);
  56. break;
  57. }
  58. }
  59. static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  60. {
  61. if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
  62. pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
  63. !session->channel) {
  64. return 0;
  65. }
  66. rfc3326_use_reason_header(session, rdata);
  67. return 0;
  68. }
  69. static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  70. {
  71. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  72. if ((status.code < 300) || !session->channel) {
  73. return;
  74. }
  75. rfc3326_use_reason_header(session, rdata);
  76. }
  77. static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  78. {
  79. char buf[20];
  80. if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
  81. ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
  82. }
  83. if (session->endpoint && session->endpoint->suppress_q850_reason_headers) {
  84. ast_debug(1, "A Q.850 '%s'(%i) Reason header was suppresed for endpoint '%s'\n",
  85. ast_cause2str((ast_channel_hangupcause(session->channel) & 0x7f)),
  86. (ast_channel_hangupcause(session->channel) & 0x7f),
  87. ast_sorcery_object_get_id(session->endpoint));
  88. } else {
  89. snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
  90. ast_sip_add_header(tdata, "Reason", buf);
  91. }
  92. }
  93. static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  94. {
  95. if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
  96. && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
  97. || !session->channel
  98. /*
  99. * The session->channel has been seen to go away on us between
  100. * checks so we must also be running under the call's serializer
  101. * thread.
  102. */
  103. || session->serializer != ast_threadpool_serializer_get_current()) {
  104. return;
  105. }
  106. rfc3326_add_reason_header(session, tdata);
  107. }
  108. static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  109. {
  110. struct pjsip_status_line status = tdata->msg->line.status;
  111. if (status.code < 300
  112. || !session->channel
  113. || session->serializer != ast_threadpool_serializer_get_current()) {
  114. return;
  115. }
  116. rfc3326_add_reason_header(session, tdata);
  117. }
  118. static struct ast_sip_session_supplement rfc3326_supplement = {
  119. .incoming_request = rfc3326_incoming_request,
  120. .incoming_response = rfc3326_incoming_response,
  121. .outgoing_request = rfc3326_outgoing_request,
  122. .outgoing_response = rfc3326_outgoing_response,
  123. };
  124. static int load_module(void)
  125. {
  126. CHECK_PJSIP_SESSION_MODULE_LOADED();
  127. ast_sip_session_register_supplement(&rfc3326_supplement);
  128. return AST_MODULE_LOAD_SUCCESS;
  129. }
  130. static int unload_module(void)
  131. {
  132. ast_sip_session_unregister_supplement(&rfc3326_supplement);
  133. return 0;
  134. }
  135. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
  136. .support_level = AST_MODULE_SUPPORT_CORE,
  137. .load = load_module,
  138. .unload = unload_module,
  139. .load_pri = AST_MODPRI_APP_DEPEND,
  140. );