res_pjsip_rfc3326.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  32. {
  33. const pj_str_t str_reason = { "Reason", 6 };
  34. pjsip_generic_string_hdr *header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
  35. char buf[20], *cause, *text;
  36. int code;
  37. if (!header) {
  38. return;
  39. }
  40. ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
  41. cause = ast_skip_blanks(buf);
  42. if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
  43. return;
  44. }
  45. /* If text is present get rid of it */
  46. if ((text = strstr(cause, ";"))) {
  47. *text = '\0';
  48. }
  49. if (sscanf(cause, "cause=%30d", &code) != 1) {
  50. return;
  51. }
  52. ast_channel_hangupcause_set(session->channel, code & 0x7f);
  53. }
  54. static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  55. {
  56. if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
  57. pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
  58. !session->channel) {
  59. return 0;
  60. }
  61. rfc3326_use_reason_header(session, rdata);
  62. return 0;
  63. }
  64. static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  65. {
  66. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  67. if ((status.code < 300) || !session->channel) {
  68. return;
  69. }
  70. rfc3326_use_reason_header(session, rdata);
  71. }
  72. static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  73. {
  74. char buf[20];
  75. snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
  76. ast_sip_add_header(tdata, "Reason", buf);
  77. if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
  78. ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
  79. }
  80. }
  81. static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  82. {
  83. if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method) &&
  84. pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method)) ||
  85. !session->channel) {
  86. return;
  87. }
  88. rfc3326_add_reason_header(session, tdata);
  89. }
  90. static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  91. {
  92. struct pjsip_status_line status = tdata->msg->line.status;
  93. if ((status.code < 300) || !session->channel) {
  94. return;
  95. }
  96. rfc3326_add_reason_header(session, tdata);
  97. }
  98. static struct ast_sip_session_supplement rfc3326_supplement = {
  99. .incoming_request = rfc3326_incoming_request,
  100. .incoming_response = rfc3326_incoming_response,
  101. .outgoing_request = rfc3326_outgoing_request,
  102. .outgoing_response = rfc3326_outgoing_response,
  103. };
  104. static int load_module(void)
  105. {
  106. CHECK_PJSIP_SESSION_MODULE_LOADED();
  107. ast_sip_session_register_supplement(&rfc3326_supplement);
  108. return AST_MODULE_LOAD_SUCCESS;
  109. }
  110. static int unload_module(void)
  111. {
  112. ast_sip_session_unregister_supplement(&rfc3326_supplement);
  113. return 0;
  114. }
  115. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
  116. .support_level = AST_MODULE_SUPPORT_CORE,
  117. .load = load_module,
  118. .unload = unload_module,
  119. .load_pri = AST_MODPRI_APP_DEPEND,
  120. );