From a1a5824869eb04ed7973ff9c6797282d7d436df5 Mon Sep 17 00:00:00 2001
From: Luigi Rizzo <rizzo@icir.org>
Date: Sat, 7 Oct 2006 12:20:16 +0000
Subject: [PATCH] put repeated code to set nat mode in a function.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@44674 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_sip.c | 84 ++++++++++++++++-----------------------------
 1 file changed, 29 insertions(+), 55 deletions(-)

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 0f4a1e50e3..62e9d7b6e4 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -2501,13 +2501,33 @@ static struct sip_user *find_user(const char *name, int realtime)
 	return u;
 }
 
+/*! \brief Set nat mode on the various data sockets */
+static void do_setnat(struct sip_pvt *p, int natflags)
+{
+	const char *mode = natflags ? "On" : "Off";
+
+	if (p->rtp) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
+		ast_rtp_setnat(p->rtp, natflags);
+	}
+	if (p->vrtp) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
+		ast_rtp_setnat(p->vrtp, natflags);
+	}
+	if (p->udptl) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
+		ast_udptl_setnat(p->udptl, natflags);
+	}
+}
+
 /*! \brief Create address structure from peer reference.
  *  return -1 on error, 0 on success.
  */
 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
 {
-	int natflags;
-
 	if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
 	    (!peer->maxms || ((peer->lastms >= 0)  && (peer->lastms <= peer->maxms)))) {
 		dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
@@ -2541,26 +2561,17 @@ static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
 		ast_udptl_destroy(dialog->udptl);
 		dialog->udptl = NULL;
 	}
-	natflags = ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
+	do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
+
 	if (dialog->rtp) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", natflags ? "On" : "Off");
-		ast_rtp_setnat(dialog->rtp, natflags);
 		ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) != SIP_DTMF_INFO);
 		ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
 	}
 	if (dialog->vrtp) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", natflags ? "On" : "Off");
-		ast_rtp_setnat(dialog->vrtp, natflags);
 		ast_rtp_setdtmf(dialog->vrtp, 0);
 		ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
 	}
-	if (dialog->udptl) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", natflags ? "On" : "Off");
-		ast_udptl_setnat(dialog->udptl, natflags);
-	}
+
 	/* Set Frame packetization */
 	if (dialog->rtp) {
 		ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
@@ -4131,17 +4142,10 @@ static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *si
 	}
 
 	if (useglobal_nat && sin) {
-		int natflags;
 		/* Setup NAT structure according to global settings if we have an address */
 		ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
 		p->recv = *sin;
-		natflags = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
-		if (p->rtp)
-			ast_rtp_setnat(p->rtp, natflags);
-		if (p->vrtp)
-			ast_rtp_setnat(p->vrtp, natflags);
-		if (p->udptl)
-			ast_udptl_setnat(p->udptl, natflags);
+		do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
 	}
 
 	if (p->method != SIP_REGISTER)
@@ -8687,7 +8691,6 @@ static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_requ
 	char calleridname[50];
 	int debug=sip_debug_test_addr(sin);
 	struct ast_variable *tmpvar = NULL, *v = NULL;
-	int usenatroute;
 	char *uri2 = ast_strdupa(uri);
 
 	/* Terminate URI */
@@ -8772,23 +8775,8 @@ static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_requ
 			ast_string_field_set(p, cid_num, tmp);
 		}
 		
-		usenatroute = ast_test_flag(&p->flags[0], SIP_NAT_ROUTE);
+		do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
 
-		if (p->rtp) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", usenatroute ? "On" : "Off");
-			ast_rtp_setnat(p->rtp, usenatroute);
-		}
-		if (p->vrtp) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", usenatroute ? "On" : "Off");
-			ast_rtp_setnat(p->vrtp, usenatroute);
-		}
-		if (p->udptl) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", usenatroute ? "On" : "Off");
-			ast_udptl_setnat(p->udptl, usenatroute);
-		}
 		if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
 			sip_cancel_destroy(p);
 			ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
@@ -8894,22 +8882,8 @@ static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_requ
 					ast_shrink_phone_number(tmp);
 				ast_string_field_set(p, cid_num, tmp);
 			}
-			usenatroute = ast_test_flag(&p->flags[0], SIP_NAT_ROUTE);
-			if (p->rtp) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", usenatroute ? "On" : "Off");
-				ast_rtp_setnat(p->rtp, usenatroute);
-			}
-			if (p->vrtp) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", usenatroute ? "On" : "Off");
-				ast_rtp_setnat(p->vrtp, usenatroute);
-			}
-			if (p->udptl) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", usenatroute ? "On" : "Off");
-				ast_udptl_setnat(p->udptl, usenatroute);
-			}
+			do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
+
 			ast_string_field_set(p, peersecret, peer->secret);
 			ast_string_field_set(p, peermd5secret, peer->md5secret);
 			ast_string_field_set(p, subscribecontext, peer->subscribecontext);
-- 
GitLab