[Asterisk-code-review] res pjsip: Add support for dnsmgr to external media address. (asterisk[master])

Joshua Colp asteriskteam at digium.com
Sun Jul 23 18:37:09 CDT 2017


Joshua Colp has uploaded this change for review. ( https://gerrit.asterisk.org/6071


Change subject: res_pjsip: Add support for dnsmgr to external_media_address.
......................................................................

res_pjsip: Add support for dnsmgr to external_media_address.

The "external_media_address" option on transports is now
resolved using dnsmgr. This allows it to be automatically
refreshed regularly if refreshes are enabled in dnsmgr.
If the system is using a dynamic IP address a dynamic DNS
hostname can be provided to keep the IP address up to
date.

Change-Id: Ia54771720dff0105bde55d5bbb81a3ba437e05b2
---
M CHANGES
M include/asterisk/res_pjsip.h
M res/res_pjsip/config_transport.c
M res/res_pjsip_nat.c
M res/res_pjsip_sdp_rtp.c
M res/res_pjsip_session.c
M res/res_pjsip_t38.c
7 files changed, 62 insertions(+), 22 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/71/6071/1

diff --git a/CHANGES b/CHANGES
index 5daa816..b948e5c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -21,6 +21,12 @@
 --- Functionality changes from Asterisk 14.6.0 to Asterisk 14.7.0 ------------
 ------------------------------------------------------------------------------
 
+res_pjsip
+------------------
+ * The "external_media_address" on transports is now resolved using dnsmgr and
+   when dnsmgr refreshes are enabled will be automatically updated with the new
+   IP address of a given hostname.
+
 res_musiconhold
 ------------------
  * By default, when res_musiconhold reloads or unloads, it sends a HUP signal
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index cf366cb..18ea929 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -103,15 +103,25 @@
 	 */
 	struct ast_ha *localnet;
 	/*!
-	 * DNS manager for refreshing the external address
+	 * DNS manager for refreshing the external signaling address
 	 * \since 13.8.0
 	 */
-	struct ast_dnsmgr_entry *external_address_refresher;
+	struct ast_dnsmgr_entry *external_signaling_address_refresher;
 	/*!
-	 * Optional external address information
+	 * Optional external signaling address information
 	 * \since 13.8.0
 	 */
-	struct ast_sockaddr external_address;
+	struct ast_sockaddr external_signaling_address;
+	/*!
+	 * DNS manager for refreshing the external media address
+	 * \since 13.18.0
+	 */
+	struct ast_dnsmgr_entry *external_media_address_refresher;
+	/*!
+	 * Optional external signaling address information
+	 * \since 13.18.0
+	 */
+	struct ast_sockaddr external_media_address;
 };
 
 /*
diff --git a/res/res_pjsip/config_transport.c b/res/res_pjsip/config_transport.c
index 62bc9d6..5f7eafa 100644
--- a/res/res_pjsip/config_transport.c
+++ b/res/res_pjsip/config_transport.c
@@ -248,8 +248,11 @@
 	ast_free(transport_state->id);
 	ast_free_ha(transport_state->localnet);
 
-	if (transport_state->external_address_refresher) {
-		ast_dnsmgr_release(transport_state->external_address_refresher);
+	if (transport_state->external_signaling_address_refresher) {
+		ast_dnsmgr_release(transport_state->external_signaling_address_refresher);
+	}
+	if (transport_state->external_media_address_refresher) {
+		ast_dnsmgr_release(transport_state->external_media_address_refresher);
 	}
 	if (transport_state->transport) {
 		pjsip_transport_shutdown(transport_state->transport);
@@ -399,8 +402,8 @@
 	memcpy(&transport->tls, &transport->state->tls, sizeof(transport->tls));
 	memcpy(&transport->ciphers, &transport->state->ciphers, sizeof(transport->ciphers));
 	transport->localnet = transport->state->localnet;
-	transport->external_address_refresher = transport->state->external_address_refresher;
-	memcpy(&transport->external_address, &transport->state->external_address, sizeof(transport->external_address));
+	transport->external_address_refresher = transport->state->external_signaling_address_refresher;
+	memcpy(&transport->external_address, &transport->state->external_signaling_address, sizeof(transport->external_signaling_address));
 }
 
 static int has_state_changed(struct ast_sip_transport_state *a, struct ast_sip_transport_state *b)
@@ -421,7 +424,11 @@
 		return -1;
 	}
 
-	if (ast_sockaddr_cmp(&a->external_address, &b->external_address)) {
+	if (ast_sockaddr_cmp(&a->external_signaling_address, &b->external_signaling_address)) {
+		return -1;
+	}
+
+	if (ast_sockaddr_cmp(&a->external_media_address, &b->external_media_address)) {
 		return -1;
 	}
 
@@ -515,24 +522,41 @@
 		pj_sockaddr_set_port(&temp_state->state->host, (transport->type == AST_TRANSPORT_TLS) ? 5061 : 5060);
 	}
 
-	/* Now that we know what address family we can set up a dnsmgr refresh for the external media address if present */
+	/* Now that we know what address family we can set up a dnsmgr refresh for the external addresses if present */
 	if (!ast_strlen_zero(transport->external_signaling_address)) {
 		if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
-			temp_state->state->external_address.ss.ss_family = AF_INET;
+			temp_state->state->external_signaling_address.ss.ss_family = AF_INET;
 		} else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
-			temp_state->state->external_address.ss.ss_family = AF_INET6;
+			temp_state->state->external_signaling_address.ss.ss_family = AF_INET6;
 		} else {
 			ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external signaling address\n",
 					transport_id);
 			return -1;
 		}
 
-		if (ast_dnsmgr_lookup(transport->external_signaling_address, &temp_state->state->external_address, &temp_state->state->external_address_refresher, NULL) < 0) {
+		if (ast_dnsmgr_lookup(transport->external_signaling_address, &temp_state->state->external_signaling_address, &temp_state->state->external_signaling_address_refresher, NULL) < 0) {
 			ast_log(LOG_ERROR, "Could not create dnsmgr for external signaling address on '%s'\n", transport_id);
 			return -1;
 		}
 	}
 
+	if (!ast_strlen_zero(transport->external_media_address)) {
+		if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
+			temp_state->state->external_media_address.ss.ss_family = AF_INET;
+		} else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
+			temp_state->state->external_media_address.ss.ss_family = AF_INET6;
+		} else {
+			ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external media address\n",
+					transport_id);
+			return -1;
+		}
+
+		if (ast_dnsmgr_lookup(transport->external_media_address, &temp_state->state->external_media_address, &temp_state->state->external_media_address_refresher, NULL) < 0) {
+			ast_log(LOG_ERROR, "Could not create dnsmgr for external media address on '%s'\n", transport_id);
+			return -1;
+		}
+	}
+
 	if (transport->type == AST_TRANSPORT_UDP) {
 
 		for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
diff --git a/res/res_pjsip_nat.c b/res/res_pjsip_nat.c
index 9205622..7dfd5ec 100644
--- a/res/res_pjsip_nat.c
+++ b/res/res_pjsip_nat.c
@@ -273,10 +273,10 @@
 		}
 	}
 
-	if (!ast_sockaddr_isnull(&transport_state->external_address)) {
+	if (!ast_sockaddr_isnull(&transport_state->external_signaling_address)) {
 		/* Update the contact header with the external address */
 		if (uri || (uri = nat_get_contact_sip_uri(tdata))) {
-			pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport_state->external_address));
+			pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport_state->external_signaling_address));
 			if (transport->external_signaling_port) {
 				uri->port = transport->external_signaling_port;
 				ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port);
@@ -285,7 +285,7 @@
 
 		/* Update the via header if relevant */
 		if ((tdata->msg->type == PJSIP_REQUEST_MSG) && (via || (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL)))) {
-			pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport_state->external_address));
+			pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport_state->external_signaling_address));
 			if (transport->external_signaling_port) {
 				via->sent_by.port = transport->external_signaling_port;
 			}
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index a2e7f8f..0fc580c 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -1820,8 +1820,8 @@
 		&& ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) {
 		return;
 	}
-	ast_debug(5, "Setting media address to %s\n", transport->external_media_address);
-	pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
+	ast_debug(5, "Setting media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
+	pj_strdup2(tdata->pool, &stream->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
 }
 
 /*! \brief Function which stops the RTP instance */
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index fe3680f..40a38bc 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -3888,8 +3888,8 @@
 
 		if (!transport_state->localnet
 			|| ast_apply_ha(transport_state->localnet, &addr) != AST_SENSE_ALLOW) {
-			ast_debug(5, "Setting external media address to %s\n", transport->external_media_address);
-			pj_strdup2(tdata->pool, &sdp->conn->addr, transport->external_media_address);
+			ast_debug(5, "Setting external media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
+			pj_strdup2(tdata->pool, &sdp->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
 		}
 	}
 
diff --git a/res/res_pjsip_t38.c b/res/res_pjsip_t38.c
index fbfbd0c..27eff42 100644
--- a/res/res_pjsip_t38.c
+++ b/res/res_pjsip_t38.c
@@ -967,8 +967,8 @@
 		&& ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) {
 		return;
 	}
-	ast_debug(5, "Setting media address to %s\n", transport->external_media_address);
-	pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
+	ast_debug(5, "Setting media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
+	pj_strdup2(tdata->pool, &stream->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
 }
 
 /*! \brief Function which destroys the UDPTL instance when session ends */

-- 
To view, visit https://gerrit.asterisk.org/6071
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia54771720dff0105bde55d5bbb81a3ba437e05b2
Gerrit-Change-Number: 6071
Gerrit-PatchSet: 1
Gerrit-Owner: Joshua Colp <jcolp at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20170723/dcda8f0d/attachment-0001.html>


More information about the asterisk-code-review mailing list