trunk: parse E1 trunk number

The E1 trunk number is currently not parsed, whenever a trunk prefix is
detected that indicates an E1 trunk, then the entire request is
rejected.

Parse the trunk number and select the trunk accordingly

Related: OS#2547
Change-Id: Ifdaab953544151e73b58cc3e95d21afdb40765f4
diff --git a/src/libosmo-mgcp/mgcp_trunk.c b/src/libosmo-mgcp/mgcp_trunk.c
index bd7bb2a..cc6637b 100644
--- a/src/libosmo-mgcp/mgcp_trunk.c
+++ b/src/libosmo-mgcp/mgcp_trunk.c
@@ -118,6 +118,27 @@
 	return NULL;
 }
 
+/* Made public for unit-testing, do not use from outside this file */
+int e1_trunk_nr_from_epname(const char *epname)
+{
+	unsigned long int trunk_nr;
+	size_t prefix_len;
+	char *str_trunk_nr_end;
+
+	prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
+	if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) != 0)
+		return -EINVAL;
+
+	errno = 0;
+	trunk_nr = strtoul(epname + prefix_len, &str_trunk_nr_end, 10);
+	if (errno == ERANGE || trunk_nr > 64 || trunk_nr == 0
+	    || epname + prefix_len == str_trunk_nr_end
+	    || str_trunk_nr_end[0] != '/')
+		return -EINVAL;
+	else
+		return trunk_nr;
+}
+
 /*! Find a trunk by the trunk prefix in the endpoint name.
  *  \param[in] epname endpoint name with trunk prefix to look up.
  *  \param[in] cfg that contains the trunks where the endpoint is located.
@@ -126,6 +147,7 @@
 {
 	size_t prefix_len;
 	char epname_lc[MGCP_ENDPOINT_MAXLEN];
+	unsigned long int trunk_nr;
 
 	osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
 	epname = epname_lc;
@@ -135,13 +157,12 @@
 		return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
 	}
 
-	/* E1 trunks are not implemented yet, so we deny any request for an
-	 * e1 trunk for now. */
 	prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
 	if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) == 0) {
-		LOGP(DLMGCP, LOGL_ERROR,
-		     "endpoint name \"%s\" suggests an E1 trunk, but E1 trunks are not implemented in this version of osmo-mgw!\n", epname);
-		return NULL;
+		trunk_nr = e1_trunk_nr_from_epname(epname);
+		if (trunk_nr < 0)
+			return NULL;
+		return mgcp_trunk_by_num(cfg, trunk_nr);
 	}
 
 	/* Earlier versions of osmo-mgw were accepting endpoint names