[asterisk-commits] mmichelson: branch mmichelson/outbound_auth r383116 - in /team/mmichelson/out...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 14 18:09:04 CDT 2013


Author: mmichelson
Date: Thu Mar 14 18:09:01 2013
New Revision: 383116

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383116
Log:
Add framework for allowing easier in-dialog authentication.

Nothing uses this yet, but I'm about to add it in the session module
as a proof of concept.


Modified:
    team/mmichelson/outbound_auth/include/asterisk/res_sip.h
    team/mmichelson/outbound_auth/res/res_sip/sip_outbound_auth.c

Modified: team/mmichelson/outbound_auth/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/include/asterisk/res_sip.h?view=diff&rev=383116&r1=383115&r2=383116
==============================================================================
--- team/mmichelson/outbound_auth/include/asterisk/res_sip.h (original)
+++ team/mmichelson/outbound_auth/include/asterisk/res_sip.h Thu Mar 14 18:09:01 2013
@@ -483,6 +483,10 @@
  */
 int ast_sip_initialize_outbound_authentication(void);
 
+typedef int (*outbound_auth_cb)(pjsip_tx_data *tdata, void *user_data);
+
+int ast_sip_setup_outbound_authentication(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint,
+		outbound_auth_cb cb, void *user_data);
 /*!
  * \brief Initialize the distributor module
  *

Modified: team/mmichelson/outbound_auth/res/res_sip/sip_outbound_auth.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/res/res_sip/sip_outbound_auth.c?view=diff&rev=383116&r1=383115&r2=383116
==============================================================================
--- team/mmichelson/outbound_auth/res/res_sip/sip_outbound_auth.c (original)
+++ team/mmichelson/outbound_auth/res/res_sip/sip_outbound_auth.c Thu Mar 14 18:09:01 2013
@@ -23,16 +23,51 @@
 
 #include "asterisk/res_sip.h"
 
+static pj_bool_t outbound_auth(pjsip_rx_data *rdata);
+
+static pjsip_module outbound_auth_mod = {
+	.name = {"Outbound Authentication", 19},
+	.priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
+	.on_rx_response = outbound_auth,
+};
+
+struct outbound_auth_cb_data {
+	outbound_auth_cb cb;
+	void *user_data;
+};
+
+static void dialog_outbound_auth(pjsip_dialog *dlg, pjsip_transaction *tsx, pjsip_rx_data *rdata)
+{
+	struct outbound_auth_cb_data *cb_data = dlg->mod_data[outbound_auth_mod.id];
+	pjsip_tx_data *tdata;
+	pjsip_auth_clt_reinit_req(&dlg->auth_sess, rdata, tsx->last_tx, &tdata);
+
+	if (cb_data) {
+		cb_data->cb(tdata, cb_data->user_data);
+		return;
+	}
+
+	pjsip_dlg_send_request(dlg, tdata, -1, NULL);
+}
+
 static pj_bool_t outbound_auth(pjsip_rx_data *rdata)
 {
 	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
 	pjsip_transaction *tsx;
+	pjsip_dialog *dlg;
 	pjsip_auth_clt_sess auth_sess;
 	pjsip_tx_data *tdata;
 	if (rdata->msg_info.msg->line.status.code != 401 &&
 			rdata->msg_info.msg->line.status.code != 407) {
 		/* Doesn't pertain to us. Move on */
 		return PJ_FALSE;
+	}
+
+	tsx = pjsip_rdata_get_tsx(rdata);
+	dlg = pjsip_rdata_get_dlg(rdata);
+	if (dlg) {
+		dialog_outbound_auth(dlg, tsx, rdata);
+		return PJ_TRUE;
 	}
 
 	/* Endpoint identification is not automatically done on responses,
@@ -45,28 +80,25 @@
 		return PJ_FALSE;
 	}
 
-	tsx = pjsip_rdata_get_tsx(rdata);
-	if (!tsx) {
-		ast_log(LOG_WARNING, "Cannot respond to authentication challenge because"
-				"initial request was sent statelessly\n");
-		return PJ_FALSE;
-	}
 	ast_sip_set_outbound_authentication_credentials(&auth_sess, endpoint);
 	pjsip_auth_clt_reinit_req(&auth_sess, rdata, tsx->last_tx, &tdata);
 	pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
 	return PJ_TRUE;
 }
 
-/* XXX This module is intended to be a temporary solution for doing
- * out-of-dialog outbound authentication. Once endpoint location
- * is merged, then out of dialog outbound authentication will be
- * handled entirely within the ast_sip_send_request() function.
- */
-static pjsip_module outbound_auth_mod = {
-	.name = {"Outbound Authentication", 19},
-	.priority = PJSIP_MOD_PRIORITY_APPLICATION - 1,
-	.on_rx_response = outbound_auth,
-};
+int ast_sip_setup_outbound_authentication(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint,
+		outbound_auth_cb cb, void *user_data)
+{
+	struct outbound_auth_cb_data *cb_data = ast_calloc(1, sizeof(*cb_data));
+	cb_data->cb = cb;
+	cb_data->user_data = user_data;
+
+	dlg->sess_count++;
+	pjsip_dlg_add_usage(dlg, &outbound_auth_mod, cb_data);
+	dlg->sess_count--;
+
+	return ast_sip_set_outbound_authentication_credentials(&dlg->auth_sess, endpoint);
+}
 
 int ast_sip_initialize_outbound_authentication(void) {
 	return ast_sip_register_service(&outbound_auth_mod);




More information about the asterisk-commits mailing list