mgcp: Parse a Digital Trunk endpoint name.
diff --git a/openbsc/configure.in b/openbsc/configure.in
index 8bbfcf8..6aa68d8 100644
--- a/openbsc/configure.in
+++ b/openbsc/configure.in
@@ -95,4 +95,5 @@
     tests/db/Makefile
     tests/channel/Makefile
     tests/bsc-nat/Makefile
+    tests/mgcp/Makefile
     Makefile)
diff --git a/openbsc/src/mgcp/mgcp_protocol.c b/openbsc/src/mgcp/mgcp_protocol.c
index dba4870..f9d723a 100644
--- a/openbsc/src/mgcp/mgcp_protocol.c
+++ b/openbsc/src/mgcp/mgcp_protocol.c
@@ -260,18 +260,56 @@
 	return found;
 }
 
+/**
+ * We have a null terminated string with the endpoint name here. We only
+ * support two kinds. Simple ones as seen on the BSC level and the ones
+ * seen on the trunk side.
+ */
+static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
+					     const char *mgcp)
+{
+	char *rest = NULL;
+	int trunk, endp, mgcp_endp;
+
+	trunk = strtoul(mgcp + 6, &rest, 10);
+	if (rest == NULL || rest[0] != '/') {
+		LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
+		return NULL;
+	}
+
+	endp = strtoul(rest + 1, &rest, 10);
+	if (rest == NULL || rest[0] != '@') {
+		LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
+		return NULL;
+	}
+
+	/* signalling is on timeslot 1 */
+	if (endp == 1)
+		return NULL;
+
+	mgcp_endp = mgcp_timeslot_to_endpoint(trunk, endp);
+	if (mgcp_endp < 1 || mgcp_endp >= cfg->number_endpoints) {
+		LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
+	}
+
+	return &cfg->endpoints[mgcp_endp];
+}
+
 static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
 {
 	char *endptr = NULL;
 	unsigned int gw = INT_MAX;
 
-	gw = strtoul(mgcp, &endptr, 16);
-	if (gw == 0 || gw >= cfg->number_endpoints || strcmp(endptr, "@mgw") != 0) {
-		LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
-		return NULL;
+	if (strncmp(mgcp, "ds/e1", 5) == 0) {
+		return find_e1_endpoint(cfg, mgcp);
+	} else {
+		gw = strtoul(mgcp, &endptr, 16);
+		if (gw > 0 && gw < cfg->number_endpoints && strcmp(endptr, "@mgw") == 0)
+			return &cfg->endpoints[gw];
 	}
 
-	return &cfg->endpoints[gw];
+	LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
+	return NULL;
 }
 
 int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg,
diff --git a/openbsc/tests/Makefile.am b/openbsc/tests/Makefile.am
index 99ec296..1968119 100644
--- a/openbsc/tests/Makefile.am
+++ b/openbsc/tests/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = debug gsm0408 db channel
+SUBDIRS = debug gsm0408 db channel mgcp
 
 if BUILD_NAT
 SUBDIRS += bsc-nat
diff --git a/openbsc/tests/mgcp/Makefile.am b/openbsc/tests/mgcp/Makefile.am
new file mode 100644
index 0000000..7595e3b
--- /dev/null
+++ b/openbsc/tests/mgcp/Makefile.am
@@ -0,0 +1,12 @@
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(COVERAGE_CFLAGS)
+AM_LDFLAGS = $(COVERAGE_LDFLAGS)
+
+EXTRA_DIST = bsc_data.c
+
+noinst_PROGRAMS = mgcp_test
+
+mgcp_test_SOURCES = mgcp_test.c \
+			$(top_srcdir)/src/mgcp/mgcp_protocol.c \
+			$(top_srcdir)/src/mgcp/mgcp_network.c
+mgcp_test_LDADD = $(top_builddir)/src/libbsc.a $(LIBOSMOCORE_LIBS) -lrt $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS)
diff --git a/openbsc/tests/mgcp/mgcp_test.c b/openbsc/tests/mgcp/mgcp_test.c
new file mode 100644
index 0000000..558fbff
--- /dev/null
+++ b/openbsc/tests/mgcp/mgcp_test.c
@@ -0,0 +1,82 @@
+/*
+ * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2011 by On-Waves
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <openbsc/mgcp.h>
+
+#include <osmocore/talloc.h>
+#include <string.h>
+
+static struct msgb *create_auep1()
+{
+	struct msgb *msg;
+
+	msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
+	int len = sprintf((char *)msg->data, "AUEP 158663169 ds/e1-0/2@172.16.6.66 MGCP 1.0\r\n");
+	msg->l2h = msgb_put(msg, len);
+	return msg;
+}
+
+static struct msgb *create_auep2()
+{
+	struct msgb *msg;
+
+	msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
+	int len = sprintf((char *)msg->data, "AUEP 18983213 ds/e1-1/1@172.16.6.66 MGCP 1.0\r\n");
+	msg->l2h = msgb_put(msg, len);
+	return msg;
+}
+
+static void test_auep(void)
+{
+	struct msgb *inp;
+	struct msgb *msg;
+	struct mgcp_config *cfg = mgcp_config_alloc();
+	cfg->number_endpoints = 64;
+	mgcp_endpoints_allocate(cfg);
+
+	inp = create_auep1();
+	msg = mgcp_handle_message(cfg, inp);
+	msgb_free(inp);
+	if (strcmp((char *) msg->data, "200 158663169 OK\r\n") != 0)
+		printf("Result1 failed '%s'\n", (char *) msg->data);
+	/* Verify that the endpoint is fine */
+	msgb_free(msg);
+
+	inp = create_auep2();
+	msg = mgcp_handle_message(cfg, inp);
+	msgb_free(inp);
+	/* Verify that the endpoint is not fine */
+	if (strcmp((char *) msg->data, "500 18983213 FAIL\r\n") != 0)
+		printf("Result2 failed '%s'\n", (char *) msg->data);
+	msgb_free(msg);
+
+	talloc_free(cfg);
+}
+
+int main(int argc, char **argv)
+{
+	struct log_target *stderr_target;
+	log_init(&log_info);
+	stderr_target = log_target_create_stderr();
+	log_add_target(stderr_target);
+	log_set_all_filter(stderr_target, 1);
+
+	test_auep();
+	return 0;
+}