mgcp_protocol: Avoid code duplication between virtual + other trunks

There were two code paths that were supposed to do exactly the same,
but then in Change-Id I3994af016fb96427263edbba05f560743f85fdd4 only
one of the two was modified, resulting in OS#4034

Let's
* dynamically allocate the virtual trunk
* rename mgcp_config.trunk to mgcp_config.virt_trunk to clarify
* as a result, abolish copy+pasted code for trunk initialization

Change-Id: I54762af6d417b849a24b6e71b6c5c996a5cb3fa6
Related: OS#4034
diff --git a/src/libosmo-mgcp/mgcp_msg.c b/src/libosmo-mgcp/mgcp_msg.c
index 3e95ed1..b0d1a9f 100644
--- a/src/libosmo-mgcp/mgcp_msg.c
+++ b/src/libosmo-mgcp/mgcp_msg.c
@@ -235,6 +235,7 @@
 	unsigned int gw = INT_MAX;
 	const char *endpoint_number_str;
 	struct mgcp_endpoint *endp;
+	struct mgcp_trunk_config *virt_trunk = cfg->virt_trunk;
 
 	*cause = 0;
 
@@ -259,15 +260,15 @@
 		endpoint_number_str =
 		    mgcp + strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
 		if (endpoint_number_str[0] == '*') {
-			endp = find_free_endpoint(cfg->trunk.endpoints,
-						  cfg->trunk.number_endpoints);
+			endp = find_free_endpoint(virt_trunk->endpoints,
+						  virt_trunk->number_endpoints);
 			if (!endp)
 				*cause = -403;
 			return endp;
 		}
 		gw = strtoul(endpoint_number_str, &endptr, 16);
-		if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') {
-			endp = &cfg->trunk.endpoints[gw];
+		if (gw < virt_trunk->number_endpoints && endptr[0] == '@') {
+			endp = &virt_trunk->endpoints[gw];
 			endp->wildcarded_req = false;
 			return endp;
 		}
@@ -278,8 +279,8 @@
 	     "Addressing virtual trunk without prefix (deprecated), please use %s: '%s'\n",
 	     MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, mgcp);
 	gw = strtoul(mgcp, &endptr, 16);
-	if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') {
-		endp = &cfg->trunk.endpoints[gw];
+	if (gw < virt_trunk->number_endpoints && endptr[0] == '@') {
+		endp = &virt_trunk->endpoints[gw];
 		endp->wildcarded_req = false;
 		return endp;
 	}
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 608a93b..3b8e736 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -1440,7 +1440,7 @@
 {
 	/* NOTE: The port that is used for RTCP is the RTP port incremented by one
 	 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
-	 struct mgcp_endpoint *endp = &cfg->trunk.endpoints[endpno];
+	 struct mgcp_endpoint *endp = &cfg->virt_trunk->endpoints[endpno];
 
 	if (mgcp_create_bind(source_addr, &rtp_end->rtp,
 			     rtp_end->local_port) != 0) {
diff --git a/src/libosmo-mgcp/mgcp_osmux.c b/src/libosmo-mgcp/mgcp_osmux.c
index a1121de..8da7361 100644
--- a/src/libosmo-mgcp/mgcp_osmux.c
+++ b/src/libosmo-mgcp/mgcp_osmux.c
@@ -202,9 +202,9 @@
 	struct mgcp_conn_rtp * conn_rtp;
 	int i;
 
-	for (i=0; i<cfg->trunk.number_endpoints; i++) {
+	for (i=0; i<cfg->virt_trunk->number_endpoints; i++) {
 
-		endp = &cfg->trunk.endpoints[i];
+		endp = &cfg->virt_trunk->endpoints[i];
 
 		llist_for_each_entry(conn, &endp->conns, entry) {
 			if (conn->type != MGCP_CONN_TYPE_RTP)
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index 6b8eae2..caed0b7 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -379,7 +379,7 @@
  *   - or a response (three numbers, space, transaction id) */
 struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
 {
-	struct mgcp_trunk_config *tcfg = &cfg->trunk;
+	struct mgcp_trunk_config *tcfg = cfg->virt_trunk;
 	struct rate_ctr_group *rate_ctrs = tcfg->mgcp_general_ctr_group;
 	struct mgcp_parse_data pdata;
 	int rc, i, code, handled = 0;
@@ -1657,33 +1657,26 @@
 
 	cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
 
-	/* default trunk handling; TODO: avoid duplication with mgcp_trunk_alloc() below */
-	cfg->trunk.cfg = cfg;
-	cfg->trunk.trunk_nr = 0;
-	cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
-	cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
-	cfg->trunk.audio_payload = 126;
-	cfg->trunk.audio_send_ptime = 1;
-	cfg->trunk.audio_send_name = 1;
-	cfg->trunk.vty_number_endpoints = 33;
-	cfg->trunk.omit_rtcp = 0;
-	mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
-	if (alloc_mgcp_rate_counters(&cfg->trunk, cfg) < 0) {
+	INIT_LLIST_HEAD(&cfg->trunks);
+
+	/* default trunk handling */
+	cfg->virt_trunk = mgcp_trunk_alloc(cfg, MGCP_TRUNK_VIRTUAL, 0);
+	if (!cfg->virt_trunk) {
 		talloc_free(cfg);
 		return NULL;
 	}
-
-	INIT_LLIST_HEAD(&cfg->trunks);
+	/* virtual trunk is not part of the list! */
+	llist_del(&cfg->virt_trunk->entry);
 
 	return cfg;
 }
 
-/*! allocate configuration with default values.
+/*! allocate configuration with default values. Do not link it into global list yet!
  *  (called once at startup by VTY)
  *  \param[in] cfg mgcp configuration
  *  \param[in] nr trunk number
  *  \returns pointer to allocated trunk configuration */
-struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
+struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, enum mgcp_trunk_type ttype, int nr)
 {
 	struct mgcp_trunk_config *trunk;
 
@@ -1694,16 +1687,19 @@
 	}
 
 	trunk->cfg = cfg;
-	trunk->trunk_type = MGCP_TRUNK_E1;
+	trunk->trunk_type = ttype;
 	trunk->trunk_nr = nr;
-	trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
+	trunk->audio_name = talloc_strdup(trunk, "AMR/8000");
 	trunk->audio_payload = 126;
 	trunk->audio_send_ptime = 1;
 	trunk->audio_send_name = 1;
 	trunk->vty_number_endpoints = 33;
 	trunk->omit_rtcp = 0;
 	mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
-	alloc_mgcp_rate_counters(trunk, trunk);
+	if (alloc_mgcp_rate_counters(trunk, trunk) < 0) {
+		talloc_free(trunk);
+		return NULL;
+	}
 	llist_add_tail(&trunk->entry, &cfg->trunks);
 
 	return trunk;
diff --git a/src/libosmo-mgcp/mgcp_vty.c b/src/libosmo-mgcp/mgcp_vty.c
index 76d674f..6a431e5 100644
--- a/src/libosmo-mgcp/mgcp_vty.c
+++ b/src/libosmo-mgcp/mgcp_vty.c
@@ -47,7 +47,7 @@
 	struct mgcp_trunk_config *trunk;
 
 	if (nr == 0)
-		trunk = &cfg->trunk;
+		trunk = cfg->virt_trunk;
 	else
 		trunk = mgcp_trunk_num(cfg, nr);
 
@@ -68,6 +68,8 @@
 
 static int config_write_mgcp(struct vty *vty)
 {
+	struct mgcp_trunk_config *trunk = g_cfg->virt_trunk;
+
 	vty_out(vty, "mgcp%s", VTY_NEWLINE);
 	vty_out(vty, "  domain %s%s", g_cfg->domain, VTY_NEWLINE);
 	if (g_cfg->local_ip)
@@ -85,50 +87,50 @@
 	else
 		vty_out(vty, "  no rtp ip-probing%s", VTY_NEWLINE);
 	vty_out(vty, "  rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
-	if (g_cfg->trunk.keepalive_interval == MGCP_KEEPALIVE_ONCE)
+	if (trunk->keepalive_interval == MGCP_KEEPALIVE_ONCE)
 		vty_out(vty, "  rtp keep-alive once%s", VTY_NEWLINE);
-	else if (g_cfg->trunk.keepalive_interval)
+	else if (trunk->keepalive_interval)
 		vty_out(vty, "  rtp keep-alive %d%s",
-			g_cfg->trunk.keepalive_interval, VTY_NEWLINE);
+			trunk->keepalive_interval, VTY_NEWLINE);
 	else
 		vty_out(vty, "  no rtp keep-alive%s", VTY_NEWLINE);
 
-	if (g_cfg->trunk.omit_rtcp)
+	if (trunk->omit_rtcp)
 		vty_out(vty, "  rtcp-omit%s", VTY_NEWLINE);
 	else
 		vty_out(vty, "  no rtcp-omit%s", VTY_NEWLINE);
-	if (g_cfg->trunk.force_constant_ssrc
-	    || g_cfg->trunk.force_aligned_timing
-	    || g_cfg->trunk.rfc5993_hr_convert) {
+	if (trunk->force_constant_ssrc
+	    || trunk->force_aligned_timing
+	    || trunk->rfc5993_hr_convert) {
 		vty_out(vty, "  %srtp-patch ssrc%s",
-			g_cfg->trunk.force_constant_ssrc ? "" : "no ",
+			trunk->force_constant_ssrc ? "" : "no ",
 			VTY_NEWLINE);
 		vty_out(vty, "  %srtp-patch timestamp%s",
-			g_cfg->trunk.force_aligned_timing ? "" : "no ",
+			trunk->force_aligned_timing ? "" : "no ",
 			VTY_NEWLINE);
 		vty_out(vty, "  %srtp-patch rfc5993hr%s",
-			g_cfg->trunk.rfc5993_hr_convert ? "" : "no ",
+			trunk->rfc5993_hr_convert ? "" : "no ",
 			VTY_NEWLINE);
 	} else
 		vty_out(vty, "  no rtp-patch%s", VTY_NEWLINE);
-	if (g_cfg->trunk.audio_payload != -1)
+	if (trunk->audio_payload != -1)
 		vty_out(vty, "  sdp audio-payload number %d%s",
-			g_cfg->trunk.audio_payload, VTY_NEWLINE);
-	if (g_cfg->trunk.audio_name)
+			trunk->audio_payload, VTY_NEWLINE);
+	if (trunk->audio_name)
 		vty_out(vty, "  sdp audio-payload name %s%s",
-			g_cfg->trunk.audio_name, VTY_NEWLINE);
-	if (g_cfg->trunk.audio_fmtp_extra)
+			trunk->audio_name, VTY_NEWLINE);
+	if (trunk->audio_fmtp_extra)
 		vty_out(vty, "  sdp audio fmtp-extra %s%s",
-			g_cfg->trunk.audio_fmtp_extra, VTY_NEWLINE);
+			trunk->audio_fmtp_extra, VTY_NEWLINE);
 	vty_out(vty, "  %ssdp audio-payload send-ptime%s",
-		g_cfg->trunk.audio_send_ptime ? "" : "no ", VTY_NEWLINE);
+		trunk->audio_send_ptime ? "" : "no ", VTY_NEWLINE);
 	vty_out(vty, "  %ssdp audio-payload send-name%s",
-		g_cfg->trunk.audio_send_name ? "" : "no ", VTY_NEWLINE);
-	vty_out(vty, "  loop %u%s", ! !g_cfg->trunk.audio_loop, VTY_NEWLINE);
+		trunk->audio_send_name ? "" : "no ", VTY_NEWLINE);
+	vty_out(vty, "  loop %u%s", ! !trunk->audio_loop, VTY_NEWLINE);
 	vty_out(vty, "  number endpoints %u%s",
-		g_cfg->trunk.vty_number_endpoints - 1, VTY_NEWLINE);
+		trunk->vty_number_endpoints - 1, VTY_NEWLINE);
 	vty_out(vty, "  %sallow-transcoding%s",
-		g_cfg->trunk.no_audio_transcoding ? "no " : "", VTY_NEWLINE);
+		trunk->no_audio_transcoding ? "no " : "", VTY_NEWLINE);
 	if (g_cfg->call_agent_addr)
 		vty_out(vty, "  call-agent ip %s%s", g_cfg->call_agent_addr,
 			VTY_NEWLINE);
@@ -299,7 +301,7 @@
 	struct mgcp_trunk_config *trunk;
 	int show_stats = argc >= 1;
 
-	dump_trunk(vty, &g_cfg->trunk, show_stats);
+	dump_trunk(vty, g_cfg->virt_trunk, show_stats);
 
 	llist_for_each_entry(trunk, &g_cfg->trunks, entry)
 	    dump_trunk(vty, trunk, show_stats);
@@ -350,7 +352,7 @@
 {
 	struct mgcp_trunk_config *trunk;
 
-	dump_mgcp_endpoint(vty, &g_cfg->trunk, argv[0]);
+	dump_mgcp_endpoint(vty, g_cfg->virt_trunk, argv[0]);
 	llist_for_each_entry(trunk, &g_cfg->trunks, entry)
 		dump_mgcp_endpoint(vty, trunk, argv[0]);
 
@@ -561,7 +563,7 @@
 	if (!txt)
 		return CMD_WARNING;
 
-	osmo_talloc_replace_string(g_cfg, &g_cfg->trunk.audio_fmtp_extra, txt);
+	osmo_talloc_replace_string(g_cfg, &g_cfg->virt_trunk->audio_fmtp_extra, txt);
 	talloc_free(txt);
 	return CMD_SUCCESS;
 }
@@ -570,7 +572,7 @@
       cfg_mgcp_allow_transcoding_cmd,
       "allow-transcoding", "Allow transcoding\n")
 {
-	g_cfg->trunk.no_audio_transcoding = 0;
+	g_cfg->virt_trunk->no_audio_transcoding = 0;
 	return CMD_SUCCESS;
 }
 
@@ -578,7 +580,7 @@
       cfg_mgcp_no_allow_transcoding_cmd,
       "no allow-transcoding", NO_STR "Allow transcoding\n")
 {
-	g_cfg->trunk.no_audio_transcoding = 1;
+	g_cfg->virt_trunk->no_audio_transcoding = 1;
 	return CMD_SUCCESS;
 }
 
@@ -590,7 +592,7 @@
       SDP_STR AUDIO_STR "Number\n" "Payload number\n")
 {
 	unsigned int payload = atoi(argv[0]);
-	g_cfg->trunk.audio_payload = payload;
+	g_cfg->virt_trunk->audio_payload = payload;
 	return CMD_SUCCESS;
 }
 
@@ -604,7 +606,7 @@
       "sdp audio-payload name NAME",
       SDP_STR AUDIO_STR "Name\n" "Payload name\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->trunk.audio_name, argv[0]);
+	osmo_talloc_replace_string(g_cfg, &g_cfg->virt_trunk->audio_name, argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -617,7 +619,7 @@
       "sdp audio-payload send-ptime",
       SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
 {
-	g_cfg->trunk.audio_send_ptime = 1;
+	g_cfg->virt_trunk->audio_send_ptime = 1;
 	return CMD_SUCCESS;
 }
 
@@ -626,7 +628,7 @@
       "no sdp audio-payload send-ptime",
       NO_STR SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
 {
-	g_cfg->trunk.audio_send_ptime = 0;
+	g_cfg->virt_trunk->audio_send_ptime = 0;
 	return CMD_SUCCESS;
 }
 
@@ -635,7 +637,7 @@
       "sdp audio-payload send-name",
       SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
 {
-	g_cfg->trunk.audio_send_name = 1;
+	g_cfg->virt_trunk->audio_send_name = 1;
 	return CMD_SUCCESS;
 }
 
@@ -644,7 +646,7 @@
       "no sdp audio-payload send-name",
       NO_STR SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
 {
-	g_cfg->trunk.audio_send_name = 0;
+	g_cfg->virt_trunk->audio_send_name = 0;
 	return CMD_SUCCESS;
 }
 
@@ -657,7 +659,7 @@
 		vty_out(vty, "Cannot use `loop' with `osmux'.%s", VTY_NEWLINE);
 		return CMD_WARNING;
 	}
-	g_cfg->trunk.audio_loop = atoi(argv[0]);
+	g_cfg->virt_trunk->audio_loop = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -667,7 +669,7 @@
       "Force endpoint reallocation when the endpoint is still seized\n"
       "Don't force reallocation\n" "force reallocation\n")
 {
-	g_cfg->trunk.force_realloc = atoi(argv[0]);
+	g_cfg->virt_trunk->force_realloc = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -677,7 +679,7 @@
       "Accept all RTP packets, even when the originating IP/Port does not match\n"
       "enable filter\n" "disable filter\n")
 {
-	g_cfg->trunk.rtp_accept_all = atoi(argv[0]);
+	g_cfg->virt_trunk->rtp_accept_all = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -687,20 +689,20 @@
       "Number options\n" "Endpoints available\n" "Number endpoints\n")
 {
 	/* + 1 as we start counting at one */
-	g_cfg->trunk.vty_number_endpoints = atoi(argv[0]) + 1;
+	g_cfg->virt_trunk->vty_number_endpoints = atoi(argv[0]) + 1;
 	return CMD_SUCCESS;
 }
 
 DEFUN(cfg_mgcp_omit_rtcp, cfg_mgcp_omit_rtcp_cmd, "rtcp-omit", RTCP_OMIT_STR)
 {
-	g_cfg->trunk.omit_rtcp = 1;
+	g_cfg->virt_trunk->omit_rtcp = 1;
 	return CMD_SUCCESS;
 }
 
 DEFUN(cfg_mgcp_no_omit_rtcp,
       cfg_mgcp_no_omit_rtcp_cmd, "no rtcp-omit", NO_STR RTCP_OMIT_STR)
 {
-	g_cfg->trunk.omit_rtcp = 0;
+	g_cfg->virt_trunk->omit_rtcp = 0;
 	return CMD_SUCCESS;
 }
 
@@ -708,7 +710,7 @@
       cfg_mgcp_patch_rtp_ssrc_cmd,
       "rtp-patch ssrc", RTP_PATCH_STR "Force a fixed SSRC\n")
 {
-	g_cfg->trunk.force_constant_ssrc = 1;
+	g_cfg->virt_trunk->force_constant_ssrc = 1;
 	return CMD_SUCCESS;
 }
 
@@ -716,7 +718,7 @@
       cfg_mgcp_no_patch_rtp_ssrc_cmd,
       "no rtp-patch ssrc", NO_STR RTP_PATCH_STR "Force a fixed SSRC\n")
 {
-	g_cfg->trunk.force_constant_ssrc = 0;
+	g_cfg->virt_trunk->force_constant_ssrc = 0;
 	return CMD_SUCCESS;
 }
 
@@ -724,7 +726,7 @@
       cfg_mgcp_patch_rtp_ts_cmd,
       "rtp-patch timestamp", RTP_PATCH_STR "Adjust RTP timestamp\n")
 {
-	g_cfg->trunk.force_aligned_timing = 1;
+	g_cfg->virt_trunk->force_aligned_timing = 1;
 	return CMD_SUCCESS;
 }
 
@@ -732,7 +734,7 @@
       cfg_mgcp_no_patch_rtp_ts_cmd,
       "no rtp-patch timestamp", NO_STR RTP_PATCH_STR "Adjust RTP timestamp\n")
 {
-	g_cfg->trunk.force_aligned_timing = 0;
+	g_cfg->virt_trunk->force_aligned_timing = 0;
 	return CMD_SUCCESS;
 }
 
@@ -740,7 +742,7 @@
       cfg_mgcp_patch_rtp_rfc5993hr_cmd,
       "rtp-patch rfc5993hr", RTP_PATCH_STR RTP_TS101318_RFC5993_CONV_STR)
 {
-	g_cfg->trunk.rfc5993_hr_convert = true;
+	g_cfg->virt_trunk->rfc5993_hr_convert = true;
 	return CMD_SUCCESS;
 }
 
@@ -748,16 +750,16 @@
       cfg_mgcp_no_patch_rtp_rfc5993hr_cmd,
       "no rtp-patch rfc5993hr", NO_STR RTP_PATCH_STR RTP_TS101318_RFC5993_CONV_STR)
 {
-	g_cfg->trunk.rfc5993_hr_convert = false;
+	g_cfg->virt_trunk->rfc5993_hr_convert = false;
 	return CMD_SUCCESS;
 }
 
 DEFUN(cfg_mgcp_no_patch_rtp,
       cfg_mgcp_no_patch_rtp_cmd, "no rtp-patch", NO_STR RTP_PATCH_STR)
 {
-	g_cfg->trunk.force_constant_ssrc = 0;
-	g_cfg->trunk.force_aligned_timing = 0;
-	g_cfg->trunk.rfc5993_hr_convert = false;
+	g_cfg->virt_trunk->force_constant_ssrc = 0;
+	g_cfg->virt_trunk->force_aligned_timing = 0;
+	g_cfg->virt_trunk->rfc5993_hr_convert = false;
 	return CMD_SUCCESS;
 }
 
@@ -766,7 +768,7 @@
       "rtp keep-alive <1-120>",
       RTP_STR RTP_KEEPALIVE_STR "Keep alive interval in secs\n")
 {
-	mgcp_trunk_set_keepalive(&g_cfg->trunk, atoi(argv[0]));
+	mgcp_trunk_set_keepalive(g_cfg->virt_trunk, atoi(argv[0]));
 	return CMD_SUCCESS;
 }
 
@@ -775,7 +777,7 @@
       "rtp keep-alive once",
       RTP_STR RTP_KEEPALIVE_STR "Send dummy packet only once after CRCX/MDCX\n")
 {
-	mgcp_trunk_set_keepalive(&g_cfg->trunk, MGCP_KEEPALIVE_ONCE);
+	mgcp_trunk_set_keepalive(g_cfg->virt_trunk, MGCP_KEEPALIVE_ONCE);
 	return CMD_SUCCESS;
 }
 
@@ -783,7 +785,7 @@
       cfg_mgcp_no_rtp_keepalive_cmd,
       "no rtp keep-alive", NO_STR RTP_STR RTP_KEEPALIVE_STR)
 {
-	mgcp_trunk_set_keepalive(&g_cfg->trunk, MGCP_KEEPALIVE_NEVER);
+	mgcp_trunk_set_keepalive(g_cfg->virt_trunk, MGCP_KEEPALIVE_NEVER);
 	return CMD_SUCCESS;
 }
 
@@ -809,8 +811,11 @@
 	int index = atoi(argv[0]);
 
 	trunk = mgcp_trunk_num(g_cfg, index);
-	if (!trunk)
-		trunk = mgcp_trunk_alloc(g_cfg, index);
+	if (!trunk) {
+		trunk = mgcp_trunk_alloc(g_cfg, MGCP_TRUNK_E1, index);
+		if (!trunk)
+			return CMD_WARNING;
+	}
 
 	if (!trunk) {
 		vty_out(vty, "%%Unable to allocate trunk %u.%s",
@@ -855,7 +860,7 @@
 		else
 			vty_out(vty, "  no rtcp-omit%s", VTY_NEWLINE);
 		if (trunk->force_constant_ssrc || trunk->force_aligned_timing
-		    || g_cfg->trunk.rfc5993_hr_convert) {
+		    || g_cfg->virt_trunk->rfc5993_hr_convert) {
 			vty_out(vty, "  %srtp-patch ssrc%s",
 				trunk->force_constant_ssrc ? "" : "no ",
 				VTY_NEWLINE);
@@ -1318,7 +1323,7 @@
 	else if (strcmp(argv[0], "only") == 0)
 		g_cfg->osmux = OSMUX_USAGE_ONLY;
 
-	if (g_cfg->trunk.audio_loop) {
+	if (g_cfg->virt_trunk->audio_loop) {
 		vty_out(vty, "Cannot use `loop' with `osmux'.%s", VTY_NEWLINE);
 		return CMD_WARNING;
 	}
@@ -1519,10 +1524,10 @@
 		return -1;
 	}
 
-	if (mgcp_endpoints_allocate(&g_cfg->trunk) != 0) {
+	if (mgcp_endpoints_allocate(g_cfg->virt_trunk) != 0) {
 		LOGP(DLMGCP, LOGL_ERROR,
 		     "Failed to initialize the virtual trunk (%d endpoints)\n",
-		     g_cfg->trunk.number_endpoints);
+		     g_cfg->virt_trunk->number_endpoints);
 		return -1;
 	}