res_pjsip_dlg_options.c 3.0 KB

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