[asterisk-commits] file: trunk r403511 - in /trunk: ./ res/res_pjsip_nat.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 9 10:41:44 CST 2013


Author: file
Date: Mon Dec  9 10:41:43 2013
New Revision: 403511

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=403511
Log:
res_pjsip_nat: Add NAT module to session dialogs.

Due to the way pjproject internally works it was possible for the
NAT module to not be invoked on messages with-in a session dialog.
This means that the various parts of the message would not get rewritten
with the source IP address and port.

This change uses a session supplement to add the NAT module
to the dialog on the first incoming or outgoing INVITE.

(closes issue ASTERISK-22941)
Reported by: Leif Madsen
........

Merged revisions 403510 from http://svn.asterisk.org/svn/asterisk/branches/12

Modified:
    trunk/   (props changed)
    trunk/res/res_pjsip_nat.c

Propchange: trunk/
------------------------------------------------------------------------------
--- branch-12-merged (original)
+++ branch-12-merged Mon Dec  9 10:41:43 2013
@@ -1,1 +1,1 @@
-/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-403290,403292-403398,403435,403458
+/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-403290,403292-403398,403435,403458,403510

Modified: trunk/res/res_pjsip_nat.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_nat.c?view=diff&rev=403511&r1=403510&r2=403511
==============================================================================
--- trunk/res/res_pjsip_nat.c (original)
+++ trunk/res/res_pjsip_nat.c Mon Dec  9 10:41:43 2013
@@ -28,10 +28,11 @@
 #include <pjsip_ua.h>
 
 #include "asterisk/res_pjsip.h"
+#include "asterisk/res_pjsip_session.h"
 #include "asterisk/module.h"
 #include "asterisk/acl.h"
 
-static pj_bool_t nat_on_rx_request(pjsip_rx_data *rdata)
+static pj_bool_t nat_on_rx_message(pjsip_rx_data *rdata)
 {
 	RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
 	pjsip_contact_hdr *contact;
@@ -213,21 +214,60 @@
 	.name = { "NAT", 3 },
 	.id = -1,
 	.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 2,
-	.on_rx_request = nat_on_rx_request,
+	.on_rx_request = nat_on_rx_message,
+	.on_rx_response = nat_on_rx_message,
 	.on_tx_request = nat_on_tx_message,
 	.on_tx_response = nat_on_tx_message,
 };
 
+/*! \brief Function called when an INVITE goes out */
+static int nat_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
+{
+	if (session->inv_session->state == PJSIP_INV_STATE_INCOMING) {
+		pjsip_dlg_add_usage(session->inv_session->dlg, &nat_module, NULL);
+	}
+
+	return 0;
+}
+
+/*! \brief Function called when an INVITE comes in */
+static void nat_outgoing_invite_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
+{
+	if (session->inv_session->state == PJSIP_INV_STATE_NULL) {
+		pjsip_dlg_add_usage(session->inv_session->dlg, &nat_module, NULL);
+	}
+}
+
+/*! \brief Supplement for adding NAT functionality to dialog */
+static struct ast_sip_session_supplement nat_supplement = {
+	.method = "INVITE",
+	.priority = AST_SIP_SESSION_SUPPLEMENT_PRIORITY_FIRST + 1,
+	.incoming_request = nat_incoming_invite_request,
+	.outgoing_request = nat_outgoing_invite_request,
+};
+
+
+static int unload_module(void)
+{
+	ast_sip_session_unregister_supplement(&nat_supplement);
+	ast_sip_unregister_service(&nat_module);
+	return 0;
+}
+
 static int load_module(void)
 {
-	ast_sip_register_service(&nat_module);
+	if (ast_sip_register_service(&nat_module)) {
+		ast_log(LOG_ERROR, "Could not register NAT module for incoming and outgoing requests\n");
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	if (ast_sip_session_register_supplement(&nat_supplement)) {
+		ast_log(LOG_ERROR, "Could not register NAT session supplement for incoming and outgoing INVITE requests\n");
+		unload_module();
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
 	return AST_MODULE_LOAD_SUCCESS;
-}
-
-static int unload_module(void)
-{
-	ast_sip_unregister_service(&nat_module);
-	return 0;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP NAT Support",




More information about the asterisk-commits mailing list