2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
24 #include "asterisk/res_pjsip.h"
25 #include "asterisk/module.h"
26 #include "include/res_pjsip_private.h"
28 static pj_bool_t outbound_auth(pjsip_rx_data *rdata);
30 static pjsip_module outbound_auth_mod = {
31 .name = {"Outbound Authentication", 19},
32 .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
33 .on_rx_response = outbound_auth,
36 struct outbound_auth_cb_data {
37 ast_sip_dialog_outbound_auth_cb cb;
41 static pj_bool_t outbound_auth(pjsip_rx_data *rdata)
43 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
44 pjsip_transaction *tsx;
46 struct outbound_auth_cb_data *cb_data;
49 if (rdata->msg_info.msg->line.status.code != 401 &&
50 rdata->msg_info.msg->line.status.code != 407) {
51 /* Doesn't pertain to us. Move on */
55 tsx = pjsip_rdata_get_tsx(rdata);
56 dlg = pjsip_rdata_get_dlg(rdata);
57 ast_assert(dlg != NULL && tsx != NULL);
58 endpoint = ast_sip_dialog_get_endpoint(dlg);
64 if (ast_sip_create_request_with_auth(&endpoint->outbound_auths, rdata, tsx, &tdata)) {
68 cb_data = dlg->mod_data[outbound_auth_mod.id];
70 cb_data->cb(dlg, tdata, cb_data->user_data);
74 pjsip_dlg_send_request(dlg, tdata, -1, NULL);
78 int ast_sip_dialog_setup_outbound_authentication(pjsip_dialog *dlg, const struct ast_sip_endpoint *endpoint,
79 ast_sip_dialog_outbound_auth_cb cb, void *user_data)
81 struct outbound_auth_cb_data *cb_data = PJ_POOL_ZALLOC_T(dlg->pool, struct outbound_auth_cb_data);
83 cb_data->user_data = user_data;
86 pjsip_dlg_add_usage(dlg, &outbound_auth_mod, cb_data);
92 int ast_sip_initialize_outbound_authentication(void) {
93 return ast_sip_register_service(&outbound_auth_mod);