mgcp_trunk: pick trunk by number and type

The function mgcp_trunk_by_num() is used to directly pick a specific
trunk that is known by its id number (sometimes called "index").
Traditionally the virtual trunk will reside under id number 0 and all
consecutively created E1 trunks will be created under number 1 to 64.
This works fine, but puts a limitation on us should we ever introduce an
aditional trunk type (e.g. T1). Since the numbers must be unique
regardless of the trunk type one could not have an E1 trunk number 1 and
e.g. a T1 trunk number 1 at the same time. So we should pick the trunk
not only by its number, but also by its type to allow different trunk
types to carry the same number. The trunks will still be distinguishable
by its type along with the respective endpoint prefix.

Change-Id: I7af1e9ce601babd4a51e88201a98319e03945f83
Related: OS#2659
diff --git a/src/libosmo-mgcp/mgcp_trunk.c b/src/libosmo-mgcp/mgcp_trunk.c
index 4f60cc9..e41ed0e 100644
--- a/src/libosmo-mgcp/mgcp_trunk.c
+++ b/src/libosmo-mgcp/mgcp_trunk.c
@@ -105,15 +105,17 @@
 
 /*! get trunk configuration by trunk number (index).
  *  \param[in] cfg mgcp configuration.
- *  \param[in] index trunk number.
+ *  \param[in] ttype trunk type.
+ *  \param[in] nr trunk number.
  *  \returns pointer to trunk configuration, NULL on error. */
-struct mgcp_trunk *mgcp_trunk_by_num(const struct mgcp_config *cfg, int index)
+struct mgcp_trunk *mgcp_trunk_by_num(const struct mgcp_config *cfg, enum mgcp_trunk_type ttype, int nr)
 {
 	struct mgcp_trunk *trunk;
 
-	llist_for_each_entry(trunk, &cfg->trunks, entry)
-	    if (trunk->trunk_nr == index)
-		return trunk;
+	llist_for_each_entry(trunk, &cfg->trunks, entry) {
+		if (trunk->trunk_nr == nr && trunk->trunk_type == ttype)
+			return trunk;
+	}
 
 	return NULL;
 }
@@ -154,7 +156,7 @@
 
 	prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
 	if (strncmp(epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, prefix_len) == 0) {
-		return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
+		return mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
 	}
 
 	prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
@@ -162,7 +164,7 @@
 		trunk_nr = e1_trunk_nr_from_epname(epname);
 		if (trunk_nr < 0)
 			return NULL;
-		return mgcp_trunk_by_num(cfg, trunk_nr);
+		return mgcp_trunk_by_num(cfg, MGCP_TRUNK_E1, trunk_nr);
 	}
 
 	/* Earlier versions of osmo-mgw were accepting endpoint names
@@ -176,7 +178,7 @@
 	if ((epname[0] >= '0' && epname[0] <= '9') || (epname[0] >= 'a' && epname[0] <= 'f')) {
 		LOGP(DLMGCP, LOGL_ERROR, "missing trunk prefix in endpoint name \"%s\", assuming trunk \"%s\"!\n", epname,
 		     MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
-		return  mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
+		return  mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
 	}
 
 	LOGP(DLMGCP, LOGL_ERROR, "unable to find trunk for endpoint name \"%s\"!\n", epname);