mgcp: Allocate the endpoints for the E1 trunks as well.
diff --git a/openbsc/include/openbsc/mgcp.h b/openbsc/include/openbsc/mgcp.h
index 54a3a6e..88e5230 100644
--- a/openbsc/include/openbsc/mgcp.h
+++ b/openbsc/include/openbsc/mgcp.h
@@ -159,7 +159,7 @@
 struct mgcp_config *mgcp_config_alloc(void);
 int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg);
 int mgcp_vty_init(void);
-int mgcp_endpoints_allocate(struct mgcp_config *cfg);
+int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
 void mgcp_free_endp(struct mgcp_endpoint *endp);
 int mgcp_reset_transcoder(struct mgcp_config *cfg);
 
diff --git a/openbsc/src/mgcp/mgcp_protocol.c b/openbsc/src/mgcp/mgcp_protocol.c
index 46214a5..b95cc81 100644
--- a/openbsc/src/mgcp/mgcp_protocol.c
+++ b/openbsc/src/mgcp/mgcp_protocol.c
@@ -942,15 +942,12 @@
 	end->rtcp.fd = -1;
 }
 
-int mgcp_endpoints_allocate(struct mgcp_config *cfg)
+int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
 {
-	struct mgcp_trunk_config *tcfg;
 	int i;
 
-	tcfg = &cfg->trunk;
-
 	/* Initialize all endpoints */
-	tcfg->endpoints = _talloc_zero_array(cfg,
+	tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
 				       sizeof(struct mgcp_endpoint),
 				       tcfg->number_endpoints, "endpoints");
 	if (!tcfg->endpoints)
@@ -958,7 +955,7 @@
 
 	for (i = 0; i < tcfg->number_endpoints; ++i) {
 		tcfg->endpoints[i].ci = CI_UNUSED;
-		tcfg->endpoints[i].cfg = cfg;
+		tcfg->endpoints[i].cfg = tcfg->cfg;
 		tcfg->endpoints[i].tcfg = tcfg;
 		mgcp_rtp_end_init(&tcfg->endpoints[i].net_end);
 		mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end);
diff --git a/openbsc/src/mgcp/mgcp_vty.c b/openbsc/src/mgcp/mgcp_vty.c
index 81cc68b..f955498 100644
--- a/openbsc/src/mgcp/mgcp_vty.c
+++ b/openbsc/src/mgcp/mgcp_vty.c
@@ -636,9 +636,71 @@
 	return 0;
 }
 
+static int allocate_trunk(struct mgcp_trunk_config *trunk)
+{
+	int i;
+	struct mgcp_config *cfg = trunk->cfg;
+
+	if (mgcp_endpoints_allocate(trunk) != 0) {
+		LOGP(DMGCP, LOGL_ERROR,
+		     "Failed to allocate %d endpoints on trunk %d.\n",
+		     trunk->number_endpoints, trunk->trunk_nr);
+		return -1;
+	}
+
+	/* early bind */
+	for (i = 1; i < trunk->number_endpoints; ++i) {
+		struct mgcp_endpoint *endp = &trunk->endpoints[i];
+		int rtp_port;
+
+		if (cfg->bts_ports.mode == PORT_ALLOC_STATIC) {
+			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
+						      cfg->bts_ports.base_port);
+			if (mgcp_bind_bts_rtp_port(endp, rtp_port) != 0) {
+				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
+				return -1;
+			}
+			endp->bts_end.local_alloc = PORT_ALLOC_STATIC;
+		}
+
+		if (cfg->net_ports.mode == PORT_ALLOC_STATIC) {
+			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
+						      cfg->net_ports.base_port);
+			if (mgcp_bind_net_rtp_port(endp, rtp_port) != 0) {
+				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
+				return -1;
+			}
+			endp->net_end.local_alloc = PORT_ALLOC_STATIC;
+		}
+
+		if (cfg->transcoder_ip && cfg->transcoder_ports.mode == PORT_ALLOC_STATIC) {
+			/* network side */
+			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
+						      cfg->transcoder_ports.base_port);
+			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)),
+						      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;
+		}
+	}
+
+	return 0;
+}
+
 int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
 {
-	int i, rc;
+	int rc;
+	struct mgcp_trunk_config *trunk;
 
 	g_cfg = cfg;
 	rc = vty_read_config_file(config_file, NULL);
@@ -656,55 +718,16 @@
 		return -1;
 	}
 
-	if (mgcp_endpoints_allocate(g_cfg) != 0) {
-		fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n",
-		g_cfg->trunk.number_endpoints);
+	if (allocate_trunk(&g_cfg->trunk) != 0) {
+		LOGP(DMGCP, LOGL_ERROR, "Failed to initialize the virtual trunk.\n");
 		return -1;
 	}
 
-	/* early bind */
-	for (i = 1; i < g_cfg->trunk.number_endpoints; ++i) {
-		struct mgcp_endpoint *endp = &g_cfg->trunk.endpoints[i];
-		int rtp_port;
-
-		if (g_cfg->bts_ports.mode == PORT_ALLOC_STATIC) {
-			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
-						      g_cfg->bts_ports.base_port);
-			if (mgcp_bind_bts_rtp_port(endp, rtp_port) != 0) {
-				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
-				return -1;
-			}
-			endp->bts_end.local_alloc = PORT_ALLOC_STATIC;
-		}
-
-		if (g_cfg->net_ports.mode == PORT_ALLOC_STATIC) {
-			rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
-						      g_cfg->net_ports.base_port);
-			if (mgcp_bind_net_rtp_port(endp, rtp_port) != 0) {
-				LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
-				return -1;
-			}
-			endp->net_end.local_alloc = PORT_ALLOC_STATIC;
-		}
-
-		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_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;
+	llist_for_each_entry(trunk, &g_cfg->trunks, entry) {
+		if (allocate_trunk(trunk) != 0) {
+			LOGP(DMGCP, LOGL_ERROR,
+			     "Failed to initialize E1 trunk %d.\n", trunk->trunk_nr);
+			return -1;
 		}
 	}
 
diff --git a/openbsc/tests/mgcp/mgcp_test.c b/openbsc/tests/mgcp/mgcp_test.c
index c63a786..4052377 100644
--- a/openbsc/tests/mgcp/mgcp_test.c
+++ b/openbsc/tests/mgcp/mgcp_test.c
@@ -18,6 +18,7 @@
  */
 
 #include <openbsc/mgcp.h>
+#include <openbsc/mgcp_internal.h>
 
 #include <osmocore/talloc.h>
 #include <string.h>
@@ -48,7 +49,9 @@
 	struct msgb *msg;
 	struct mgcp_config *cfg = mgcp_config_alloc();
 	cfg->trunk.number_endpoints = 64;
-	mgcp_endpoints_allocate(cfg);
+	mgcp_endpoints_allocate(&cfg->trunk);
+
+	mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
 
 	inp = create_auep1();
 	msg = mgcp_handle_message(cfg, inp);