pjsip_outbound_auth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #undef bzero
  20. #define bzero bzero
  21. #include <pjsip.h>
  22. #include "asterisk/res_pjsip.h"
  23. #include "asterisk/module.h"
  24. #include "include/res_pjsip_private.h"
  25. static pj_bool_t outbound_auth(pjsip_rx_data *rdata);
  26. static pjsip_module outbound_auth_mod = {
  27. .name = {"Outbound Authentication", 19},
  28. .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
  29. .on_rx_response = outbound_auth,
  30. };
  31. struct outbound_auth_cb_data {
  32. ast_sip_dialog_outbound_auth_cb cb;
  33. void *user_data;
  34. };
  35. static pj_bool_t outbound_auth(pjsip_rx_data *rdata)
  36. {
  37. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  38. pjsip_transaction *tsx;
  39. pjsip_dialog *dlg;
  40. struct outbound_auth_cb_data *cb_data;
  41. pjsip_tx_data *tdata;
  42. if (rdata->msg_info.msg->line.status.code != 401 &&
  43. rdata->msg_info.msg->line.status.code != 407) {
  44. /* Doesn't pertain to us. Move on */
  45. return PJ_FALSE;
  46. }
  47. tsx = pjsip_rdata_get_tsx(rdata);
  48. dlg = pjsip_rdata_get_dlg(rdata);
  49. if (!dlg || !tsx) {
  50. return PJ_FALSE;
  51. }
  52. endpoint = ast_sip_dialog_get_endpoint(dlg);
  53. if (!endpoint) {
  54. return PJ_FALSE;
  55. }
  56. if (ast_sip_create_request_with_auth(&endpoint->outbound_auths, rdata, tsx, &tdata)) {
  57. return PJ_FALSE;
  58. }
  59. cb_data = dlg->mod_data[outbound_auth_mod.id];
  60. if (cb_data) {
  61. cb_data->cb(dlg, tdata, cb_data->user_data);
  62. return PJ_TRUE;
  63. }
  64. pjsip_dlg_send_request(dlg, tdata, -1, NULL);
  65. return PJ_TRUE;
  66. }
  67. int ast_sip_dialog_setup_outbound_authentication(pjsip_dialog *dlg, const struct ast_sip_endpoint *endpoint,
  68. ast_sip_dialog_outbound_auth_cb cb, void *user_data)
  69. {
  70. struct outbound_auth_cb_data *cb_data = PJ_POOL_ZALLOC_T(dlg->pool, struct outbound_auth_cb_data);
  71. cb_data->cb = cb;
  72. cb_data->user_data = user_data;
  73. dlg->sess_count++;
  74. pjsip_dlg_add_usage(dlg, &outbound_auth_mod, cb_data);
  75. dlg->sess_count--;
  76. return 0;
  77. }
  78. int internal_sip_initialize_outbound_authentication(void) {
  79. return internal_sip_register_service(&outbound_auth_mod);
  80. }
  81. void internal_sip_destroy_outbound_authentication(void) {
  82. internal_sip_unregister_service(&outbound_auth_mod);
  83. }