mgw: Find and store RTP conn local_addr once during CRCX handling

It doesn't make sense to call the function several times since anyway we
are only binding during
allocate_port()->mgcp_bind_net_rtp_port()->bind_rtp()->mgcp_create_bind()->osmo_sock_init2().

Let's better calculate the local IP addr once and use that stored value.
THis is a previous step towards next commit updating the local IP addr
and re-bindng if encessary.

Change-Id: I803b99c5e5fe0f92a5bf6796d8c25df88d1608e6
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 84f3604..75a4754 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -1620,7 +1620,6 @@
 {
 	char name[512];
 	struct mgcp_rtp_end *end;
-	char local_ip_addr[INET6_ADDRSTRLEN];
 
 	snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
 	end = &conn->end;
@@ -1643,9 +1642,7 @@
 	end->rtcp.data = conn;
 	end->rtcp.cb = rtp_data_net;
 
-	mgcp_get_local_addr(local_ip_addr, conn);
-
-	return bind_rtp(endp->cfg, local_ip_addr, end, endp);
+	return bind_rtp(endp->cfg, conn->end.local_addr, end, endp);
 }
 
 /*! free allocated RTP and RTCP ports.
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index 1269524..1e7ccbf 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -223,24 +223,22 @@
 					     const char *trans_id,
 					     bool add_conn_params)
 {
-	/* TODO: we may want to define another local_ip_osmux var to us for
-	   OSMUX connections. Perhaps adding a new internal API to get it based
-	   on conn type */
-	const char *addr = endp->cfg->local_ip;
+	/* cfg->local_ip allows overwritting the announced IP address with
+	 * regards to the one we actually bind to. Useful in behind-NAT
+	 * scenarios.
+	 * TODO: we may want to define another local_ip_osmux var to
+	 * us for OSMUX connections. Perhaps adding a new internal API to get it
+	 * based on conn type.
+	 */
+	const char *addr = endp->cfg->local_ip ? : conn->end.local_addr;
 	struct msgb *sdp;
 	int rc;
 	struct msgb *result;
-	char local_ip_addr[INET6_ADDRSTRLEN];
 
 	sdp = msgb_alloc_headroom(4096, 128, "sdp record");
 	if (!sdp)
 		return NULL;
 
-	if (!addr) {
-		mgcp_get_local_addr(local_ip_addr, conn);
-		addr = local_ip_addr;
-	}
-
 	/* Attach optional connection parameters */
 	if (add_conn_params) {
 		rc = add_params(sdp, endp, conn);
@@ -943,6 +941,9 @@
 		goto error2;
 	}
 
+	/* Find a local address for conn based on policy and initial SDP remote
+	   information, then find a free port for it */
+	mgcp_get_local_addr(conn->end.local_addr, conn);
 	if (allocate_port(endp, conn) != 0) {
 		rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
 		goto error2;