[Asterisk-code-review] res_pjsip_nat: Don't rewrite Contact on REGISTER responses. (asterisk[certified/16.8])

Friendly Automation asteriskteam at digium.com
Thu Aug 12 10:21:57 CDT 2021


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/16279 )

Change subject: res_pjsip_nat: Don't rewrite Contact on REGISTER responses.
......................................................................

res_pjsip_nat: Don't rewrite Contact on REGISTER responses.

When sending a SIP response to an incoming REGISTER request
we don't want to change the Contact header as it will
contain the Contacts registered to the AOR and not our own
Contact URI.

ASTERISK-29235

Change-Id: I35a0723545281dd01fcd5cae497baab58720478c
(cherry picked from commit d65041ebea0d54b5306419866a08e6e171fb444d)
---
M res/res_pjsip_nat.c
1 file changed, 18 insertions(+), 6 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/res/res_pjsip_nat.c b/res/res_pjsip_nat.c
index dc7d10b..9fc6a67 100644
--- a/res/res_pjsip_nat.c
+++ b/res/res_pjsip_nat.c
@@ -406,12 +406,24 @@
 	}
 
 	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_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);
+		pjsip_cseq_hdr *cseq = PJSIP_MSG_CSEQ_HDR(tdata->msg);
+
+		/* Update the Contact header with the external address. We only do this if
+		 * a CSeq is not present (which should not happen - but we are extra safe),
+		 * if a request is being sent, or if a response is sent that is not a response
+		 * to a REGISTER. We specifically don't do this for a response to a REGISTER
+		 * as the Contact headers would contain the registered Contacts, and not our
+		 * own Contact.
+		 */
+		if (!cseq || tdata->msg->type == PJSIP_REQUEST_MSG ||
+			pjsip_method_cmp(&cseq->method, &pjsip_register_method)) {
+			/* We can only rewrite the URI when one is present */
+			if (uri || (uri = nat_get_contact_sip_uri(tdata))) {
+				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);
+				}
 			}
 		}
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16279
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: certified/16.8
Gerrit-Change-Id: I35a0723545281dd01fcd5cae497baab58720478c
Gerrit-Change-Number: 16279
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210812/dff40e56/attachment.html>


More information about the asterisk-code-review mailing list