res_pjsip: Add support for dnsmgr to external_media_address.
authorJoshua Colp <jcolp@digium.com>
Sun, 23 Jul 2017 23:34:32 +0000 (23:34 +0000)
committerGeorge Joseph <gjoseph@digium.com>
Tue, 1 Aug 2017 21:42:38 +0000 (15:42 -0600)
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

CHANGES
include/asterisk/res_pjsip.h
res/res_pjsip/config_transport.c
res/res_pjsip_nat.c
res/res_pjsip_sdp_rtp.c
res/res_pjsip_session.c
res/res_pjsip_t38.c

diff --git a/CHANGES b/CHANGES
index fb54ca6..71166ef 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -26,6 +26,12 @@ app_queue
 --- 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
index cb77fa3..890ce59 100644 (file)
@@ -103,15 +103,25 @@ struct ast_sip_transport_state {
         */
        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;
 };
 
 /*
index 62bc9d6..5f7eafa 100644 (file)
@@ -248,8 +248,11 @@ static int destroy_sip_transport_state(void *data)
        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 @@ static void copy_state_to_transport(struct ast_sip_transport *transport)
        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 @@ static int has_state_changed(struct ast_sip_transport_state *a, struct ast_sip_t
                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 @@ static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
                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++) {
index 9205622..7dfd5ec 100644 (file)
@@ -273,10 +273,10 @@ static pj_status_t nat_on_tx_message(pjsip_tx_data *tdata)
                }
        }
 
-       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 @@ static pj_status_t nat_on_tx_message(pjsip_tx_data *tdata)
 
                /* 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;
                        }
index 4ed1f8a..f79c4cb 100644 (file)
@@ -1820,8 +1820,8 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc
                && 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 */
index 2711efe..bb349a4 100644 (file)
@@ -3896,8 +3896,8 @@ static void session_outgoing_nat_hook(pjsip_tx_data *tdata, struct ast_sip_trans
 
                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));
                }
        }
 
index fbfbd0c..27eff42 100644 (file)
@@ -967,8 +967,8 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc
                && 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 */