res_pjsip_dlg_options.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Yaron Nahum <nachum.yaron@gmail.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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include <pjsip.h>
  27. #include <pjsip_ua.h>
  28. #include <pjlib.h>
  29. #include "asterisk/module.h"
  30. #include "asterisk/res_pjsip.h"
  31. #include "asterisk/res_pjsip_session.h"
  32. #define DEFAULT_LANGUAGE "en"
  33. #define DEFAULT_ENCODING "text/plain"
  34. static int options_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  35. {
  36. pjsip_tx_data *tdata;
  37. pj_status_t status;
  38. const pjsip_hdr *hdr;
  39. pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
  40. status = pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL,&tdata);
  41. if (status != PJ_SUCCESS) {
  42. ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
  43. return status;
  44. }
  45. /* Add appropriate headers */
  46. if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ACCEPT, NULL))) {
  47. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
  48. }
  49. if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ALLOW, NULL))) {
  50. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
  51. }
  52. if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, NULL))) {
  53. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
  54. }
  55. /*
  56. * XXX TODO: pjsip doesn't care a lot about either of these headers -
  57. * while it provides specific methods to create them, they are defined
  58. * to be the standard string header creation. We never did add them
  59. * in chan_sip, although RFC 3261 says they SHOULD. Hard coded here.
  60. */
  61. ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING);
  62. ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE);
  63. status = pjsip_dlg_send_response(session->inv_session->dlg, pjsip_rdata_get_tsx(rdata), tdata);
  64. if (status != PJ_SUCCESS) {
  65. ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
  66. }
  67. return status;
  68. }
  69. static struct ast_sip_session_supplement dlg_options_supplement = {
  70. .method = "OPTIONS",
  71. .incoming_request = options_incoming_request,
  72. };
  73. static int load_module(void)
  74. {
  75. CHECK_PJSIP_MODULE_LOADED();
  76. if (ast_sip_session_register_supplement(&dlg_options_supplement)) {
  77. return AST_MODULE_LOAD_DECLINE;
  78. }
  79. return AST_MODULE_LOAD_SUCCESS;
  80. }
  81. static int unload_module(void)
  82. {
  83. ast_sip_session_unregister_supplement(&dlg_options_supplement);
  84. return 0;
  85. }
  86. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP OPTIONS in dialog handler",
  87. .support_level = AST_MODULE_SUPPORT_CORE,
  88. .load = load_module,
  89. .unload = unload_module,
  90. .load_pri = AST_MODPRI_APP_DEPEND,
  91. );