mgw: Announce and rebind new local address if change required during MDCX

MDCX may provide a new remote address, which means we may need to update
our announced IP addr and re-bind our local end. This can happen for
instance if MGW initially provided an IPv4 during CRCX ACK, and now MDCX
tells us the remote has an IPv6 address.

Change-Id: Iaed424e2c209e1753e1f579752fc684aaad7a512
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index 1e7ccbf..4c00140 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -1006,6 +1006,7 @@
 {
 	struct mgcp_endpoint *endp = p->endp;
 	struct rate_ctr_group *rate_ctrs = endp->trunk->ratectr.mgcp_mdcx_ctr_group;
+	char new_local_addr[INET6_ADDRSTRLEN];
 	int error_code = 500;
 	int silent = 0;
 	int have_sdp = 0;
@@ -1170,6 +1171,20 @@
 		   that conn. */
 	}
 
+	/* MDCX may have provided a new remote address, which means we may need
+	   to update our announced IP addr and re-bind our local end. This can
+	   happen for instance if MGW initially provided an IPv4 during CRCX
+	   ACK, and now MDCX tells us the remote has an IPv6 address. */
+	mgcp_get_local_addr(new_local_addr, conn);
+	if (strcmp(new_local_addr, conn->end.local_addr)) {
+		osmo_strlcpy(conn->end.local_addr, new_local_addr, sizeof(conn->end.local_addr));
+		mgcp_free_rtp_port(&conn->end);
+		if (allocate_port(endp, conn) != 0) {
+			rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
+			goto error3;
+		}
+	}
+
 	if (setup_rtp_processing(endp, conn) != 0) {
 		rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
 		goto error3;