[Asterisk-code-review] res pjsip: Symmetric transports (asterisk[13])

George Joseph asteriskteam at digium.com
Fri Mar 10 20:22:12 CST 2017


George Joseph has uploaded a new change for review. ( https://gerrit.asterisk.org/5156 )

Change subject: res_pjsip:  Symmetric transports
......................................................................

res_pjsip:  Symmetric transports

A new transport parameter 'symmetric_transport' has been added.

If set to yes, when a request from a dynamic contact comes in on
this transport, the transport name will be saved and used for
subsequent outgoing requests like OPTIONS and NOTIFY.  It's saved
as a contact uri parameter named 'x-ast-txp' and will display with
the contact uri in CLI, AMI, and ARI output.  On the outgoing
request, if a transport wasn't explicitly set on the endpoint AND
the request URI is not a hostname, the saved transport will be used
and the 'x-ast-txp' parameter stripped from the outgoing packet.

* config_transport was modified to accept and store the new parameter.

* config_transport/transport_apply was updated to store the transport
  name in the pjsip_transport->info field using the pjsip_transport->pool
  on UDP transports.

* A 'multihomed_on_rx_message' function was added to
  pjsip_message_ip_updater that, for incoming requests, retrieves the
  transport name from pjsip_transport->info and retrieves the transport.
  If transport->symmetric_transport is set, an 'x-ast-txp' uri parameter
  containing the transport name is added to the incoming Contact header.

* An 'ast_sip_get_transport_name' function was added to res_pjsip.
  It takes an ast_sip_endpoint and a pjsip_sip_uri and returns a
  transport name if endpoint->transport is set or if there's an
  'x-ast-txp' parameter on the uri and the uri host is an ipv4 or
  ipv6 address.  Otherwise it returns NULL.

* An 'ast_sip_dlg_set_transport' function was added to res_pjsip
  which takes an ast_sip_endpoint, a pjsip_dialog, and an optional
  pjsip_tpselector.  It calls ast_sip_get_transport_name() and if
  a non-NULL is returned, sets the selector and sets the transport
  on the dialog.  If a selector was passed in, it's updated.

* res_pjsip/ast_sip_create_dialog_uac and ast_sip_create_dialog_uas
  were modified to call ast_sip_dlg_set_transport() instead of their
  original logic.

* res_pjsip/create_out_of_dialog_request was modified to call
  ast_sip_get_transport_name() and pjsip_tx_data_set_transport()
  instead of its original logic.

* Existing transport logic was removed from endpt_send_request
  since that can only be called after a create_out_of_dialog_request.

* res_pjsip/ast_sip_create_rdata was converted to a wrapper around
  a new 'ast_sip_create_rdata_with_contact' function which allows
  a contact_uri to be specified in addition to the existing
  parameters.  (See below)

* res_pjsip_pubsub/internal_pjsip_evsub_send_request was eliminated
  since all it did was transport selection and that is now done in
  ast_sip_create_dialog_uac and ast_sip_create_dialog_uas.

* 'contact_uri' was added to subscription_persistence.  This was
  necessary because although the parsed rdata contact header has the
  x-ast-txp parameter added (if appropriate),
  subscription_persistence_update stores the raw packet which
  doesn't have it.  subscription_persistence_recreate was then
  updated to call ast_sip_create_rdata_with_contact with the
  persisted contact_uri so the recreated subscription has the
  correct transport info to send the NOTIFYs.

* res_pjsip_session/internal_pjsip_inv_send_msg was eliminated since
  all it did was transport selection and that is now done in
  ast_sip_create_dialog_uac.

* pjsip_message_ip_updater/multihomed_on_tx_message was updated
  to remove all traces of the x-ast-txp parameter from the
  outgoing headers.

NOTE:  This change does NOT modify the behavior of permanent
contacts specified on an aor.  To do so would require that the
permanent contact's contact uri be updated with the x-ast-txp
parameter and the aor sorcery object updated.  If we need to
persue this, we need to think about cloning permanent contacts into
the same store as the dynamic ones on an aor load so they can be
updated without disturbing the originally configured value.

You CAN add the x-ast-txp parameter to a permanent contact's uri
but it would be much simpler to just set endpoint->transport.

Change-Id: I4ee1f51473da32ca54b877cd158523efcef9655f
---
M CHANGES
M configs/samples/pjsip.conf.sample
A contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py
M include/asterisk/res_pjsip.h
M res/res_pjsip.c
M res/res_pjsip/config_transport.c
M res/res_pjsip/pjsip_message_ip_updater.c
M res/res_pjsip_pubsub.c
M res/res_pjsip_session.c
9 files changed, 374 insertions(+), 102 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/56/5156/1

diff --git a/CHANGES b/CHANGES
index 9668c03..18ca20b 100644
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,22 @@
  * The 'Comedian Mail' prompts can now be overriden using the 'vm-login' and
    'vm-newuser' configuration options in voicemail.conf.
 
+res_pjsip
+------------------
+ * A new transport parameter 'symmetric_transport' has been added.
+   If set to yes, when a request from a dynamic contact comes in on this
+   transport, the transport name will be saved and used for subsequent
+   outgoing requests like OPTIONS and NOTIFY.  It's saved as a contact uri
+   parameter named 'x-ast-txp' and will display with the contact uri in CLI,
+   AMI, and ARI output.  On the outgoing request, if a transport wasn't
+   explicitly set on the endpoint AND the request URI is not a hostname, the
+   saved transport will be used and the 'x-ast-txp' parameter stripped from
+   the outgoing packet.  To facilitate recreation of subscriptions on
+   asterisk restart, a new column 'contact_uri' needed to be added to the
+   ps_subcsription_persistence table.  Since new columns were added to both
+   transport and subscription_persistence, an alembic upgrade should be run
+   to bring the database tables up to date.
+
  * Added 'fromstring' field to the voicemail boxes. If set, it will override
    the global 'fromstring' field on a per-mailbox basis.
 
diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index f661613..72f9812 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -841,6 +841,17 @@
                     ; this option is set to 'no' (the default) changes to the
                     ; particular transport will be ignored. If set to 'yes',
                     ; changes (if any) will be applied.
+;symmetric_transport=no ; If set to yes, when a request from a dynamic contact
+                        ; comes in on this transport, the transport name will be
+                        ; saved and used for subsequent outgoing requests like
+                        ; OPTIONS and NOTIFY.  It's saved as a contact uri
+                        ; parameter named 'x-ast-txp' and will display with
+                        ; the contact uri in CLI, AMI, and ARI output.  On the
+                        ; outgoing request, if a transport wasn't explicitly set
+                        ; on the endpoint AND the request URI is not a hostname,
+                        ; the saved transport will be used and the 'x-ast-txp'
+                        ; parameter stripped from the outgoing packet.
+
 
 ;==========================AOR SECTION OPTIONS=========================
 ;[aor]
diff --git a/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py b/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py
new file mode 100644
index 0000000..e585806
--- /dev/null
+++ b/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py
@@ -0,0 +1,29 @@
+"""symmetric_transport
+
+Revision ID: f638dbe2eb23
+Revises: 28ab27a7826d
+Create Date: 2017-03-09 09:38:59.513479
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = 'f638dbe2eb23'
+down_revision = '28ab27a7826d'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    ############################# Enums ##############################
+
+    # yesno_values have already been created, so use postgres enum object
+    # type to get around "already created" issue - works okay with mysql
+    yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
+
+    op.add_column('ps_transports', sa.Column('symmetric_transport', yesno_values))
+    op.add_column('ps_subscription_persistence', sa.Column('contact_uri', sa.String(256)))
+
+def downgrade():
+    op.drop_column('ps_subscription_persistence', 'contact_uri')
+    op.drop_column('ps_transports', 'symmetric_transport')
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index d8e172f..a60cf0d 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -194,6 +194,8 @@
 	int write_timeout;
 	/*! Allow reload */
 	int allow_reload;
+	/*! Automatically send requests out the same transport requests have come in on */
+	int symmetric_transport;
 };
 
 #define SIP_SORCERY_DOMAIN_ALIAS_TYPE "domain_alias"
@@ -1700,6 +1702,26 @@
 
 /*!
  * \brief General purpose method for creating an rdata structure using specific information
+ * \since 13.15.0
+ *
+ * \param rdata[out] The rdata structure that will be populated
+ * \param packet A SIP message
+ * \param src_name The source IP address of the message
+ * \param src_port The source port of the message
+ * \param transport_type The type of transport the message was received on
+ * \param local_name The local IP address the message was received on
+ * \param local_port The local port the message was received on
+ * \param contact_uri The contact URI of the message
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet,
+	const char *src_name, int src_port, char *transport_type, const char *local_name,
+	int local_port, const char *contact_uri);
+
+/*!
+ * \brief General purpose method for creating an rdata structure using specific information
  *
  * \param rdata[out] The rdata structure that will be populated
  * \param packet A SIP message
@@ -1712,8 +1734,8 @@
  * \retval 0 success
  * \retval -1 failure
  */
-int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, char *transport_type,
-	const char *local_name, int local_port);
+int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name,
+	int src_port, char *transport_type, const char *local_name, int local_port);
 
 /*!
  * \brief General purpose method for creating a SIP request
@@ -2752,4 +2774,40 @@
 void ast_sip_get_unidentified_request_thresholds(unsigned int *count, unsigned int *period,
 	unsigned int *prune_interval);
 
+/*!
+ * \brief Get the transport name from an endpoint or request uri
+ * \since 13.15.0
+ *
+ * \param endpoint
+ * \param sip_uri
+ *
+ * \return strdup'd transport name or NULL
+ *
+ * \note
+ * If endpoint->transport is not NULL, it is returned.
+ * Otherwise if sip_uri has an 'x-ast-txp' parameter AND the sip_uri host is
+ * an ip4 or ip6 address, its value is returned,
+ * Otherwise NULL is returned.
+ *
+ * If a non-NULL is returned, it must be freed with ast_free.
+ */
+char *ast_sip_get_transport_name(const struct ast_sip_endpoint *endpoint,
+	pjsip_sip_uri *sip_uri);
+
+/*!
+ * \brief Set the transport on a dialog
+ * \since 13.15.0
+ *
+ * \param endpoint
+ * \param dlg
+ * \param selector (optional)
+ *
+ * \note
+ * This API calls ast_sip_get_transport_name(endpoint, dlg->target) and if the result is
+ * non-NULL, calls pjsip_dlg_set_transport.  If 'selector' is non-NULL, it is updated with
+ * the selector used.
+ */
+int ast_sip_dlg_set_transport(const struct ast_sip_endpoint *endpoint, pjsip_dialog *dlg,
+	pjsip_tpselector *selector);
+
 #endif /* _RES_PJSIP_H */
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 54a0a5f..d5e6fbe 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -1177,6 +1177,22 @@
 						in-progress calls.</para>
 					</description>
 				</configOption>
+				<configOption name="symmetric_transport" default="no">
+					<synopsis>Use the same transport for outgoing reqyests as incoming ones.</synopsis>
+					<description>
+						<para>If set to yes, when a request from a dynamic contact
+							comes in on this transport, the transport name will be
+							saved and used for subsequent outgoing requests like
+							OPTIONS and NOTIFY.  It's saved as a contact uri
+							parameter named 'x-ast-txp' and will display with
+							the contact uri in CLI, AMI, and ARI output.  On the
+							outgoing request, if a transport wasn't explicitly set
+							on the endpoint AND the request URI is not a hostname,
+							the saved transport will be used and the 'x-ast-txp'
+							parameter stripped from the outgoing packet.
+						</para>
+					</description>
+				</configOption>
 			</configObject>
 			<configObject name="contact">
 				<synopsis>A way of creating an aliased name to a SIP URI</synopsis>
@@ -2750,7 +2766,66 @@
 	return ast_pjsip_endpoint;
 }
 
-static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *user, const char *domain, const pj_str_t *target, pjsip_tpselector *selector)
+char *ast_sip_get_transport_name(const struct ast_sip_endpoint *endpoint,
+	pjsip_sip_uri *sip_uri)
+{
+	char *transport_name = NULL;
+	char *host = NULL;
+	pj_str_t name = { "x-ast-txp", 9};
+	pjsip_param *x_transport;
+
+	if (!ast_strlen_zero(endpoint->transport)) {
+		return ast_strdup(endpoint->transport);
+	}
+
+	x_transport = pjsip_param_find(&sip_uri->other_param, &name);
+	if (!x_transport) {
+		return NULL;
+	}
+
+	/* Only use x_transport if the uri host is an ip (4 or 6) address */
+	host = ast_alloca(sip_uri->host.slen + 1);
+	ast_copy_pj_str(host, &sip_uri->host, sip_uri->host.slen + 1);
+	if (!ast_sockaddr_parse(NULL, host, PARSE_PORT_FORBID)) {
+		return NULL;
+	}
+
+	transport_name = ast_malloc(x_transport->value.slen + 1);
+	ast_copy_pj_str(transport_name, &x_transport->value, x_transport->value.slen + 1);
+
+	return transport_name;
+}
+
+int ast_sip_dlg_set_transport(const struct ast_sip_endpoint *endpoint, pjsip_dialog *dlg,
+	pjsip_tpselector *selector)
+{
+	pjsip_sip_uri *uri;
+	char *transport_name = NULL;
+	pjsip_tpselector sel = { .type = PJSIP_TPSELECTOR_NONE, };
+
+	uri = pjsip_uri_get_uri(dlg->target);
+	transport_name = ast_sip_get_transport_name(endpoint, uri);
+	if (ast_strlen_zero(transport_name)) {
+		return 0;
+	}
+
+	if (!selector) {
+		selector = &sel;
+	}
+
+	ast_sip_set_tpselector_from_transport_name(transport_name, selector);
+	pjsip_dlg_set_transport(dlg, selector);
+
+	ast_debug(1, "Set transport '%s' on dialog %p with %.*s:%d\n", transport_name, dlg,
+		(int)uri->host.slen, uri->host.ptr, uri->port);
+
+	ast_free(transport_name);
+
+	return 0;
+}
+
+static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *user,
+	const char *domain, const pj_str_t *target, pjsip_tpselector *selector)
 {
 	pj_str_t tmp, local_addr;
 	pjsip_uri *uri;
@@ -2880,17 +2955,6 @@
 	return ast_sip_set_tpselector_from_transport(transport, selector);
 }
 
-static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector)
-{
-	const char *transport_name = endpoint->transport;
-
-	if (ast_strlen_zero(transport_name)) {
-		return 0;
-	}
-
-	return ast_sip_set_tpselector_from_transport_name(endpoint->transport, selector);
-}
-
 void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t *pool, pjsip_uri *uri)
 {
 	pjsip_sip_uri *sip_uri;
@@ -2930,7 +2994,8 @@
 	pj_list_insert_before(&sip_uri->other_param, param);
 }
 
-pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
+pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
+	const char *uri, const char *request_user)
 {
 	char enclosed_uri[PJSIP_MAX_URL_SIZE];
 	pj_str_t local_uri = { "sip:temp at temp", 13 }, remote_uri, target_uri;
@@ -2955,15 +3020,16 @@
 		return NULL;
 	}
 
-	if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
-		pjsip_dlg_terminate(dlg);
-		return NULL;
-	}
+	/* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
+	dlg->sess_count++;
+
+	ast_sip_dlg_set_transport(endpoint, dlg, &selector);
 
 	if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
 		pjsip_dlg_terminate(dlg);
 		return NULL;
 	}
+
 
 	/* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
 	pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
@@ -2995,11 +3061,6 @@
 	/* Add the user=phone parameter if applicable */
 	ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->target);
 	ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->remote.info->uri);
-
-	/* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
-	dlg->sess_count++;
-
-	pjsip_dlg_set_transport(dlg, &selector);
 
 	if (!ast_strlen_zero(outbound_proxy)) {
 		pjsip_route_hdr route_set, *route;
@@ -3069,12 +3130,17 @@
 	pjsip_transport_type_e type = rdata->tp_info.transport->key.type;
 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 	pjsip_transport *transport;
+	char *transport_name = NULL;
+	pjsip_contact_hdr *contact_hdr;
 
 	ast_assert(status != NULL);
 
-	if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
+	contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+	transport_name = ast_sip_get_transport_name(endpoint, pjsip_uri_get_uri(contact_hdr->uri));
+	if (ast_sip_set_tpselector_from_transport_name(transport_name, &selector)) {
 		return NULL;
 	}
+	ast_free(transport_name);
 
 	transport = rdata->tp_info.transport;
 	if (selector.type == PJSIP_TPSELECTOR_TRANSPORT) {
@@ -3118,8 +3184,8 @@
 	return dlg;
 }
 
-int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
-	char *transport_type, const char *local_name, int local_port)
+int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
+	char *transport_type, const char *local_name, int local_port, const char *contact)
 {
 	pj_str_t tmp;
 
@@ -3143,6 +3209,16 @@
 		return -1;
 	}
 
+	if (!ast_strlen_zero(contact)) {
+		pjsip_contact_hdr *contact_hdr;
+
+		contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+		if (contact_hdr) {
+			contact_hdr->uri = pjsip_parse_uri(rdata->tp_info.pool, (char *)contact,
+				strlen(contact), PJSIP_PARSE_URI_AS_NAMEADDR);
+		}
+	}
+
 	pj_strdup2(rdata->tp_info.pool, &rdata->msg_info.via->recvd_param, rdata->pkt_info.src_name);
 	rdata->msg_info.via->rport_param = -1;
 
@@ -3152,6 +3228,13 @@
 	rdata->tp_info.transport->local_name.port = local_port;
 
 	return 0;
+}
+
+int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
+	char *transport_type, const char *local_name, int local_port)
+{
+	return ast_sip_create_rdata_with_contact(rdata, packet, src_name, src_port, transport_type,
+		local_name, local_port, NULL);
 }
 
 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
@@ -3214,6 +3297,7 @@
 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 	pjsip_uri *sip_uri;
 	const char *fromuser;
+	char *transport_name;
 
 	if (ast_strlen_zero(uri)) {
 		if (!endpoint && (!contact || ast_strlen_zero(contact->uri))) {
@@ -3235,14 +3319,6 @@
 		pj_cstr(&remote_uri, uri);
 	}
 
-	if (endpoint) {
-		if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
-			ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
-				ast_sorcery_object_get_id(endpoint));
-			return -1;
-		}
-	}
-
 	pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
 
 	if (!pool) {
@@ -3259,6 +3335,9 @@
 		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
 		return -1;
 	}
+
+	transport_name = ast_sip_get_transport_name(endpoint, pjsip_uri_get_uri(sip_uri));
+	ast_sip_set_tpselector_from_transport_name(transport_name, &selector);
 
 	fromuser = endpoint ? (!ast_strlen_zero(endpoint->fromuser) ? endpoint->fromuser : ast_sorcery_object_get_id(endpoint)) : NULL;
 	if (sip_dialog_create_from(pool, &from, fromuser,
@@ -3278,6 +3357,8 @@
 		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
 		return -1;
 	}
+
+	pjsip_tx_data_set_transport(*tdata, &selector);
 
 	if (endpoint && !ast_strlen_zero(endpoint->contact_user)){
 		pjsip_contact_hdr *contact_hdr;
@@ -3319,6 +3400,8 @@
 		struct ast_sip_contact *contact, pjsip_tx_data **tdata)
 {
 	const pjsip_method *pmethod = get_pjsip_method(method);
+
+	ast_assert(endpoint != NULL);
 
 	if (!pmethod) {
 		ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
@@ -3584,7 +3667,6 @@
 	struct send_request_wrapper *req_wrapper;
 	pj_status_t ret_val;
 	pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
-	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 
 	if (!cb && token) {
 		/* Silly.  Without a callback we cannot do anything with token. */
@@ -3608,11 +3690,6 @@
 	req_wrapper->tdata = tdata;
 	/* Add a reference to tdata.  The wrapper destructor cleans it up. */
 	pjsip_tx_data_add_ref(tdata);
-
-	if (endpoint) {
-		sip_get_tpselector_from_endpoint(endpoint, &selector);
-		pjsip_tx_data_set_transport(tdata, &selector);
-	}
 
 	if (timeout > 0) {
 		pj_time_val timeout_timer_val = { timeout / 1000, timeout % 1000 };
diff --git a/res/res_pjsip/config_transport.c b/res/res_pjsip/config_transport.c
index 60b4507..540b180 100644
--- a/res/res_pjsip/config_transport.c
+++ b/res/res_pjsip/config_transport.c
@@ -552,13 +552,20 @@
 			}
 		}
 
-		if (res == PJ_SUCCESS && (transport->tos || transport->cos)) {
-			pj_sock_t sock;
-			pj_qos_params qos_params;
-			sock = pjsip_udp_transport_get_socket(temp_state->state->transport);
-			pj_sock_get_qos_params(sock, &qos_params);
-			set_qos(transport, &qos_params);
-			pj_sock_set_qos_params(sock, &qos_params);
+		if (res == PJ_SUCCESS) {
+			temp_state->state->transport->info = pj_pool_alloc(temp_state->state->transport->pool,
+				(strlen("x-ast-txp:") + strlen(transport_id) + 1));
+
+			sprintf(temp_state->state->transport->info, "x-ast-txp:%s", transport_id);
+
+			if (transport->tos || transport->cos) {
+				pj_sock_t sock;
+				pj_qos_params qos_params;
+				sock = pjsip_udp_transport_get_socket(temp_state->state->transport);
+				pj_sock_get_qos_params(sock, &qos_params);
+				set_qos(transport, &qos_params);
+				pj_sock_set_qos_params(sock, &qos_params);
+			}
 		}
 	} else if (transport->type == AST_TRANSPORT_TCP) {
 		pjsip_tcp_transport_cfg cfg;
@@ -1375,6 +1382,7 @@
 	ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
 	ast_sorcery_object_field_register(sorcery, "transport", "websocket_write_timeout", AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR, OPT_INT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_transport, write_timeout), 1, INT_MAX);
 	ast_sorcery_object_field_register(sorcery, "transport", "allow_reload", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, allow_reload));
+	ast_sorcery_object_field_register(sorcery, "transport", "symmetric_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, symmetric_transport));
 
 	internal_sip_register_endpoint_formatter(&endpoint_transport_formatter);
 
diff --git a/res/res_pjsip/pjsip_message_ip_updater.c b/res/res_pjsip/pjsip_message_ip_updater.c
index 7671ad0..c8304a1 100644
--- a/res/res_pjsip/pjsip_message_ip_updater.c
+++ b/res/res_pjsip/pjsip_message_ip_updater.c
@@ -28,6 +28,7 @@
 #define MOD_DATA_RESTRICTIONS "restrictions"
 
 static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata);
+static pj_bool_t multihomed_on_rx_message(pjsip_rx_data *rdata);
 
 /*! \brief Outgoing message modification restrictions */
 struct multihomed_message_restrictions {
@@ -41,6 +42,7 @@
 	.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 1,
 	.on_tx_request = multihomed_on_tx_message,
 	.on_tx_response = multihomed_on_tx_message,
+	.on_rx_request = multihomed_on_rx_message,
 };
 
 /*! \brief Helper function to get (or allocate if not already present) restrictions on a message */
@@ -151,6 +153,44 @@
 	return 0;
 }
 
+static void sanitize_tdata(pjsip_tx_data *tdata)
+{
+	pj_str_t name = { "x-ast-txp", 9};
+	pjsip_param *x_transport;
+	pjsip_sip_uri *uri;
+	pjsip_fromto_hdr *fromto;
+	pjsip_contact_hdr *contact;
+	pjsip_hdr *hdr;
+
+	if (tdata->msg->type == PJSIP_REQUEST_MSG) {
+		uri = pjsip_uri_get_uri(tdata->msg->line.req.uri);
+		x_transport = pjsip_param_find(&uri->other_param, &name);
+		if (x_transport) {
+			pj_list_erase(x_transport);
+		}
+	}
+
+	for (hdr = tdata->msg->hdr.next; hdr != &tdata->msg->hdr; hdr = hdr->next) {
+		if (hdr->type == PJSIP_H_TO || hdr->type == PJSIP_H_FROM) {
+			fromto = (pjsip_fromto_hdr *) hdr;
+			uri = pjsip_uri_get_uri(fromto->uri);
+			x_transport = pjsip_param_find(&uri->other_param, &name);
+			if (x_transport) {
+				pj_list_erase(x_transport);
+			}
+		} else if (hdr->type == PJSIP_H_CONTACT) {
+			contact = (pjsip_contact_hdr *) hdr;
+			uri = pjsip_uri_get_uri(contact->uri);
+			x_transport = pjsip_param_find(&uri->other_param, &name);
+			if (x_transport) {
+				pj_list_erase(x_transport);
+			}
+		}
+	}
+
+	pjsip_tx_data_invalidate_msg(tdata);
+}
+
 static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata)
 {
 	struct multihomed_message_restrictions *restrictions = ast_sip_mod_data_get(tdata->mod_data, multihomed_module.id, MOD_DATA_RESTRICTIONS);
@@ -158,6 +198,8 @@
 	pjsip_cseq_hdr *cseq;
 	pjsip_via_hdr *via;
 	pjsip_fromto_hdr *from;
+
+	sanitize_tdata(tdata);
 
 	/* Use the destination information to determine what local interface this message will go out on */
 	pjsip_tpmgr_fla2_param_default(&prm);
@@ -273,6 +315,49 @@
 	return PJ_SUCCESS;
 }
 
+#define TRANSPORT_PREFIX_LENGTH 10
+
+static pj_bool_t multihomed_on_rx_message(pjsip_rx_data *rdata)
+{
+	pjsip_contact_hdr *contact;
+	pjsip_sip_uri *uri;
+	const char *transport_id;
+	struct ast_sip_transport *transport;
+	pjsip_param *x_transport;
+
+	if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) {
+		return PJ_FALSE;
+	}
+
+	contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+	if (!(contact && contact->uri
+		&& ast_begins_with(rdata->tp_info.transport->info, "x-ast-txp:"))) {
+		return PJ_FALSE;
+	}
+
+	uri = pjsip_uri_get_uri(contact->uri);
+
+	transport_id = rdata->tp_info.transport->info + TRANSPORT_PREFIX_LENGTH;
+	transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_id);
+
+	if (!(transport && transport->symmetric_transport)) {
+		return PJ_FALSE;
+	}
+
+	x_transport = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param);
+	x_transport->name = pj_strdup3(rdata->tp_info.pool, "x-ast-txp");
+	x_transport->value = pj_strdup3(rdata->tp_info.pool, transport_id);
+
+	pj_list_insert_before(&uri->other_param, x_transport);
+
+	ast_debug(1, "Set transport '%s' on %.*s from %.*s:%d\n", transport_id,
+		(int)rdata->msg_info.msg->line.req.method.name.slen,
+		rdata->msg_info.msg->line.req.method.name.ptr,
+		(int)uri->host.slen, uri->host.ptr, uri->port);
+
+	return PJ_FALSE;
+}
+
 void ast_res_pjsip_cleanup_message_ip_updater(void)
 {
 	ast_sip_unregister_service(&multihomed_module);
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index 1892a20..4a1ac0c 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -123,6 +123,9 @@
 				<configOption name="expires">
 					<synopsis>The time at which the subscription expires</synopsis>
 				</configOption>
+				<configOption name="contact_uri">
+					<synopsis>The Contact URI of the dialog for the subscription</synopsis>
+				</configOption>
 			</configObject>
 			<configObject name="resource_list">
 				<synopsis>Resource list configuration parameters.</synopsis>
@@ -376,6 +379,8 @@
 	char *tag;
 	/*! When this subscription expires */
 	struct timeval expires;
+	/*! Contact URI */
+	char *contact_uri;
 };
 
 /*!
@@ -548,6 +553,7 @@
 
 	ast_free(persistence->endpoint);
 	ast_free(persistence->tag);
+	ast_free(persistence->contact_uri);
 }
 
 /*! \brief Allocator for subscription persistence */
@@ -600,9 +606,16 @@
 	if (rdata) {
 		int expires;
 		pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
+		pjsip_contact_hdr *contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+		char contact_uri[PJSIP_MAX_URL_SIZE];
 
 		expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
 		sub_tree->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1));
+
+		if (pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, contact_hdr->uri, contact_uri,
+			sizeof(contact_uri)) > 0) {
+			sub_tree->persistence->contact_uri = ast_strdup(contact_uri);
+		}
 
 		/* When receiving a packet on an streaming transport, it's possible to receive more than one SIP
 		 * message at a time into the rdata->pkt_info.packet buffer. However, the rdata->msg_info.msg_buf
@@ -1572,8 +1585,9 @@
 	pj_pool_reset(pool);
 	rdata.tp_info.pool = pool;
 
-	if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port,
-		persistence->transport_key, persistence->local_name, persistence->local_port)) {
+	if (ast_sip_create_rdata_with_contact(&rdata, persistence->packet, persistence->src_name,
+		persistence->src_port, persistence->transport_key, persistence->local_name,
+		persistence->local_port, persistence->contact_uri)) {
 		ast_log(LOG_WARNING, "Failed recreating '%s' subscription: The message could not be parsed\n",
 			persistence->endpoint);
 		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
@@ -1725,28 +1739,6 @@
 	return pjsip_msg_find_hdr_by_name(msg, &name, NULL);
 }
 
-/*!
- * \internal
- * \brief Wrapper for pjsip_evsub_send_request
- *
- * This function (re)sets the transport before sending to catch cases
- * where the transport might have changed.
- *
- * If pjproject gives us the ability to resend, we'll only reset the transport
- * if PJSIP_ETPNOTAVAIL is returned from send.
- *
- * \returns pj_status_t
- */
-static pj_status_t internal_pjsip_evsub_send_request(struct sip_subscription_tree *sub_tree, pjsip_tx_data *tdata)
-{
-	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
-
-	ast_sip_set_tpselector_from_transport_name(sub_tree->endpoint->transport, &selector);
-	pjsip_dlg_set_transport(sub_tree->dlg, &selector);
-
-	return pjsip_evsub_send_request(sub_tree->evsub, tdata);
-}
-
 /* XXX This function is not used. */
 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
 		struct ast_sip_endpoint *endpoint, const char *resource)
@@ -1794,7 +1786,7 @@
 	evsub = sub_tree->evsub;
 
 	if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
-		internal_pjsip_evsub_send_request(sub_tree, tdata);
+		pjsip_evsub_send_request(sub_tree->evsub, tdata);
 	} else {
 		/* pjsip_evsub_terminate will result in pubsub_on_evsub_state,
 		 * being called and terminating the subscription. Therefore, we don't
@@ -1891,7 +1883,7 @@
 		return -1;
 	}
 
-	res = internal_pjsip_evsub_send_request(sub_tree, tdata);
+	res = pjsip_evsub_send_request(sub_tree->evsub, tdata);
 
 	subscription_persistence_update(sub_tree, NULL, SUBSCRIPTION_PERSISTENCE_SEND_REQUEST);
 
@@ -4468,6 +4460,22 @@
 	return 0;
 }
 
+static int persistence_contact_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct subscription_persistence *persistence = obj;
+
+	persistence->contact_uri = ast_strdup(var->value);
+	return 0;
+}
+
+static int persistence_contact_struct2str(const void *obj, const intptr_t *args, char **buf)
+{
+	const struct subscription_persistence *persistence = obj;
+
+	*buf = ast_strdup(persistence->contact_uri);
+	return 0;
+}
+
 static int persistence_expires_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
 {
 	struct subscription_persistence *persistence = obj;
@@ -5343,6 +5351,8 @@
 		persistence_tag_str2struct, persistence_tag_struct2str, NULL, 0, 0);
 	ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "expires", "",
 		persistence_expires_str2struct, persistence_expires_struct2str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "contact_uri", "",
+		persistence_contact_str2struct, persistence_contact_struct2str, NULL, 0, 0);
 
 	if (apply_list_configuration(sorcery)) {
 		ast_sip_unregister_service(&pubsub_module);
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 3c4f102..98ee872 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -973,32 +973,10 @@
 	return 0;
 }
 
-/*!
- * \internal
- * \brief Wrapper for pjsip_inv_send_msg
- *
- * This function (re)sets the transport before sending to catch cases
- * where the transport might have changed.
- *
- * If pjproject gives us the ability to resend, we'll only reset the transport
- * if PJSIP_ETPNOTAVAIL is returned from send.
- *
- * \returns pj_status_t
- */
-static pj_status_t internal_pjsip_inv_send_msg(pjsip_inv_session *inv, const char *transport_name, pjsip_tx_data *tdata)
-{
-	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
-
-	ast_sip_set_tpselector_from_transport_name(transport_name, &selector);
-	pjsip_dlg_set_transport(inv->dlg, &selector);
-
-	return pjsip_inv_send_msg(inv, tdata);
-}
-
 void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
 {
 	handle_outgoing_response(session, tdata);
-	internal_pjsip_inv_send_msg(session->inv_session, session->endpoint->transport, tdata);
+	pjsip_inv_send_msg(session->inv_session, tdata);
 	return;
 }
 
@@ -1229,7 +1207,7 @@
 			     MOD_DATA_ON_RESPONSE, on_response);
 
 	handle_outgoing_request(session, tdata);
-	internal_pjsip_inv_send_msg(session->inv_session, session->endpoint->transport, tdata);
+	pjsip_inv_send_msg(session->inv_session, tdata);
 
 	return;
 }
@@ -2049,7 +2027,7 @@
 		if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) != PJ_SUCCESS) {
 			pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
 		}
-		internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata);
+		pjsip_inv_send_msg(inv_session, tdata);
 		return NULL;
 	}
 	return inv_session;
@@ -2218,7 +2196,7 @@
 			if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
 				pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
 			} else {
-				internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata);
+				pjsip_inv_send_msg(inv_session, tdata);
 			}
 		}
 		return;
@@ -2230,7 +2208,7 @@
 		if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
 			pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
 		} else {
-			internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata);
+			pjsip_inv_send_msg(inv_session, tdata);
 		}
 #ifdef HAVE_PJSIP_INV_SESSION_REF
 		pjsip_inv_dec_ref(inv_session);
@@ -2243,7 +2221,7 @@
 		if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
 			pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
 		} else {
-			internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata);
+			pjsip_inv_send_msg(inv_session, tdata);
 		}
 #ifdef HAVE_PJSIP_INV_SESSION_REF
 		pjsip_inv_dec_ref(inv_session);

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4ee1f51473da32ca54b877cd158523efcef9655f
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <gjoseph at digium.com>



More information about the asterisk-code-review mailing list