mgcp: Introduce trans_bts and send BTS packets from this port

Introduce the trans_bts. Right now only a port is allocated
and the packets from the BTS are sent from this socket.
diff --git a/openbsc/include/openbsc/mgcp_internal.h b/openbsc/include/openbsc/mgcp_internal.h
index 68c8235..c0a1fb1 100644
--- a/openbsc/include/openbsc/mgcp_internal.h
+++ b/openbsc/include/openbsc/mgcp_internal.h
@@ -99,6 +99,12 @@
 	struct mgcp_rtp_end bts_end;
 	struct mgcp_rtp_end net_end;
 
+	/*
+	 * For transcoding we will send from the local_port
+	 * of trans_bts and it will arrive at trans_net from
+	 * where we will forward it to the network.
+	 */
+	struct mgcp_rtp_end trans_bts;
 	struct mgcp_rtp_end trans_net;
 	int is_transcoded;
 
@@ -126,7 +132,15 @@
 int mgcp_send_dummy(struct mgcp_endpoint *endp);
 int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port);
 int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port);
-int mgcp_bind_transcoder_rtp_port(struct mgcp_endpoint *enp, int rtp_port);
+int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *enp, int rtp_port);
+int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *enp, int rtp_port);
 int mgcp_free_rtp_port(struct mgcp_rtp_end *end);
 
+/* For transcoding we need to manage an in and an output that are connected */
+static inline int endp_back_channel(int endpoint)
+{
+	return endpoint + 60;
+}
+
+
 #endif
diff --git a/openbsc/src/mgcp/mgcp_network.c b/openbsc/src/mgcp/mgcp_network.c
index f78e42a..ae0504f 100644
--- a/openbsc/src/mgcp/mgcp_network.c
+++ b/openbsc/src/mgcp/mgcp_network.c
@@ -191,8 +191,8 @@
 	addr.sin_port = htons(port);
 
 	rc = sendto(is_rtp ?
-		endp->bts_end.rtp.fd :
-		endp->bts_end.rtcp.fd, buf, len, 0,
+		endp->trans_bts.rtp.fd :
+		endp->trans_bts.rtcp.fd, buf, len, 0,
 		(struct sockaddr *) &addr, sizeof(addr));
 
 	if (rc != len)
@@ -543,7 +543,7 @@
 	return bind_rtp(endp->cfg, &endp->net_end, ENDPOINT_NUMBER(endp));
 }
 
-int mgcp_bind_transcoder_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
+int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
 {
 	if (endp->trans_net.rtp.fd != -1 || endp->trans_net.rtcp.fd != -1) {
 		LOGP(DMGCP, LOGL_ERROR, "Previous net-port was still bound on %d\n",
@@ -559,6 +559,22 @@
 	return bind_rtp(endp->cfg, &endp->trans_net, ENDPOINT_NUMBER(endp));
 }
 
+int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
+{
+	if (endp->trans_bts.rtp.fd != -1 || endp->trans_bts.rtcp.fd != -1) {
+		LOGP(DMGCP, LOGL_ERROR, "Previous net-port was still bound on %d\n",
+			ENDPOINT_NUMBER(endp));
+		mgcp_free_rtp_port(&endp->trans_bts);
+	}
+
+	endp->trans_bts.local_port = rtp_port;
+	endp->trans_bts.rtp.cb = rtp_data_transcoder;
+	endp->trans_bts.rtp.data = endp;
+	endp->trans_bts.rtcp.data = endp;
+	endp->trans_bts.rtcp.cb = rtp_data_transcoder;
+	return bind_rtp(endp->cfg, &endp->trans_bts, ENDPOINT_NUMBER(endp));
+}
+
 int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
 {
 	if (end->rtp.fd != -1) {
diff --git a/openbsc/src/mgcp/mgcp_protocol.c b/openbsc/src/mgcp/mgcp_protocol.c
index ddcbfaf..84fd155 100644
--- a/openbsc/src/mgcp/mgcp_protocol.c
+++ b/openbsc/src/mgcp/mgcp_protocol.c
@@ -423,12 +423,21 @@
 	if (endp->cfg->transcoder_ip) {
 		if (allocate_port(endp, &endp->trans_net,
 				  &endp->cfg->transcoder_ports,
-				  mgcp_bind_transcoder_rtp_port) != 0) {
+				  mgcp_bind_trans_net_rtp_port) != 0) {
 			mgcp_rtp_end_reset(&endp->net_end);
 			mgcp_rtp_end_reset(&endp->bts_end);
 			return -1;
 		}
 
+		if (allocate_port(endp, &endp->trans_bts,
+				  &endp->cfg->transcoder_ports,
+				  mgcp_bind_trans_bts_rtp_port) != 0) {
+			mgcp_rtp_end_reset(&endp->net_end);
+			mgcp_rtp_end_reset(&endp->bts_end);
+			mgcp_rtp_end_reset(&endp->trans_net);
+			return -1;
+		}
+
 		/* remember that we have set up transcoding */
 		endp->is_transcoded = 1;
 	}
@@ -830,6 +839,7 @@
 		mgcp_rtp_end_init(&cfg->endpoints[i].net_end);
 		mgcp_rtp_end_init(&cfg->endpoints[i].bts_end);
 		mgcp_rtp_end_init(&cfg->endpoints[i].trans_net);
+		mgcp_rtp_end_init(&cfg->endpoints[i].trans_bts);
 	}
 
 	return 0;
@@ -854,6 +864,7 @@
 	mgcp_rtp_end_reset(&endp->bts_end);
 	mgcp_rtp_end_reset(&endp->net_end);
 	mgcp_rtp_end_reset(&endp->trans_net);
+	mgcp_rtp_end_reset(&endp->trans_bts);
 	endp->is_transcoded = 0;
 
 	memset(&endp->net_state, 0, sizeof(endp->net_state));
@@ -865,12 +876,6 @@
 	memset(&endp->taps, 0, sizeof(endp->taps));
 }
 
-/* For transcoding we need to manage an in and an output that are connected */
-static int back_channel(int endpoint)
-{
-	return endpoint + 60;
-}
-
 static int send_trans(struct mgcp_config *cfg, const char *buf, int len)
 {
 	struct sockaddr_in addr;
@@ -932,16 +937,20 @@
 {
 	int port;
 	int in_endp = ENDPOINT_NUMBER(endp);
-	int out_endp = back_channel(in_endp);
+	int out_endp = endp_back_channel(in_endp);
 
 	if (!endp->is_transcoded)
 		return;
 
-	send_msg(endp, in_endp, endp->bts_end.local_port, "CRCX", "recvonly");
-	send_msg(endp, in_endp, endp->bts_end.local_port, "MDCX", "recvonly");
+	send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "recvonly");
+	send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "recvonly");
 	send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv");
 	send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv");
 
+	port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base);
+	endp->trans_bts.rtp_port = htons(port);
+	endp->trans_bts.rtcp_port = htons(port + 1);
+
 	port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base);
 	endp->trans_net.rtp_port = htons(port);
 	endp->trans_net.rtcp_port = htons(port + 1);
@@ -950,7 +959,7 @@
 static void delete_transcoder(struct mgcp_endpoint *endp)
 {
 	int in_endp = ENDPOINT_NUMBER(endp);
-	int out_endp = back_channel(in_endp);
+	int out_endp = endp_back_channel(in_endp);
 
 	if (!endp->is_transcoded)
 		return;
diff --git a/openbsc/src/mgcp/mgcp_vty.c b/openbsc/src/mgcp/mgcp_vty.c
index f173882..b47470d 100644
--- a/openbsc/src/mgcp/mgcp_vty.c
+++ b/openbsc/src/mgcp/mgcp_vty.c
@@ -99,14 +99,14 @@
 	vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
 	for (i = 1; i < g_cfg->number_endpoints; ++i) {
 		struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
-		vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s traffic received bts: %u/%u  remote: %u/%u transcoder: %u%s",
+		vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s traffic received bts: %u/%u  remote: %u/%u transcoder: %u/%u%s",
 			i, endp->ci,
 			ntohs(endp->net_end.rtp_port), ntohs(endp->net_end.rtcp_port),
 			ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port),
 			inet_ntoa(endp->bts_end.addr),
 			endp->bts_end.packets, endp->bts_state.lost_no,
 			endp->net_end.packets, endp->net_state.lost_no,
-			endp->trans_net.packets,
+			endp->trans_net.packets, endp->trans_bts.packets,
 			VTY_NEWLINE);
 	}
 
@@ -523,13 +523,23 @@
 		}
 
 		if (g_cfg->transcoder_ip && g_cfg->transcoder_ports.mode == PORT_ALLOC_STATIC) {
+			/* network side */
 			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
 						      g_cfg->transcoder_ports.base_port);
-			if (mgcp_bind_transcoder_rtp_port(endp, rtp_port) != 0) {
+			if (mgcp_bind_trans_net_rtp_port(endp, rtp_port) != 0) {
 				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
 				return -1;
 			}
 			endp->trans_net.local_alloc = PORT_ALLOC_STATIC;
+
+			/* bts side */
+			rtp_port = rtp_calculate_port(endp_back_channel(ENDPOINT_NUMBER(endp)),
+						      g_cfg->transcoder_ports.base_port);
+			if (mgcp_bind_trans_bts_rtp_port(endp, rtp_port) != 0) {
+				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
+				return -1;
+			}
+			endp->trans_bts.local_alloc = PORT_ALLOC_STATIC;
 		}
 	}