nat: Add code to find a BSC connection by the given msc multiplex
diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h
index 6f79106..412d9a8 100644
--- a/openbsc/include/openbsc/bsc_nat.h
+++ b/openbsc/include/openbsc/bsc_nat.h
@@ -183,4 +183,6 @@
 void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
 int bsc_mgcp_init(struct bsc_nat *nat);
 
+struct bsc_connection *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
+
 #endif
diff --git a/openbsc/src/nat/bsc_mgcp_utils.c b/openbsc/src/nat/bsc_mgcp_utils.c
index e190105..0f45a91 100644
--- a/openbsc/src/nat/bsc_mgcp_utils.c
+++ b/openbsc/src/nat/bsc_mgcp_utils.c
@@ -78,6 +78,23 @@
 		mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
 }
 
+struct bsc_connection *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
+{
+	struct sccp_connections *sccp;
+
+	llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
+		if (sccp->msc_timeslot == -1)
+			continue;
+		if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
+			continue;
+
+		return sccp->bsc;
+	}
+
+	LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
+	return NULL;
+}
+
 static int mgcp_do_read(struct bsc_fd *fd)
 {
 	struct bsc_nat *nat;
diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c
index 70e98d7..30f1490 100644
--- a/openbsc/tests/bsc-nat/bsc_nat_test.c
+++ b/openbsc/tests/bsc-nat/bsc_nat_test.c
@@ -335,7 +335,7 @@
 	talloc_free(parsed);
 }
 
-static void test_mgcp(void)
+static void test_mgcp_ass_tracking(void)
 {
 	struct sccp_connections con;
 	struct bsc_nat_parsed *parsed;
@@ -370,6 +370,46 @@
 	}
 }
 
+/* test the code to find a given connection */
+static void test_mgcp_find(void)
+{
+	struct bsc_nat *nat;
+	struct bsc_connection *con;
+	struct sccp_connections *sccp_con;
+
+	fprintf(stderr, "Testing finding of a BSC Connection\n");
+
+	nat = bsc_nat_alloc();
+	con = bsc_connection_alloc(nat);
+	llist_add(&con->list_entry, &nat->bsc_connections);
+
+	sccp_con = talloc_zero(con, struct sccp_connections);
+	sccp_con->msc_timeslot = 12;
+	sccp_con->bsc_timeslot = 12;
+	sccp_con->bsc = con;
+	llist_add(&sccp_con->list_entry, &nat->sccp_connections);
+
+	if (bsc_mgcp_find_con(nat, 11) != NULL) {
+		fprintf(stderr, "Found the wrong connection.\n");
+		abort();
+	}
+
+	if (bsc_mgcp_find_con(nat, 12) != con) {
+		fprintf(stderr, "Didn't find the connection\n");
+		abort();
+	}
+
+	sccp_con->msc_timeslot = 0;
+	sccp_con->bsc_timeslot = 0;
+	if (bsc_mgcp_find_con(nat, 1) != con) {
+		fprintf(stderr, "Didn't find the connection\n");
+		abort();
+	}
+
+	/* free everything */
+	talloc_free(nat);
+}
+
 int main(int argc, char **argv)
 {
 	struct debug_target *stderr_target;
@@ -381,7 +421,8 @@
 	test_filter();
 	test_contrack();
 	test_paging();
-	test_mgcp();
+	test_mgcp_ass_tracking();
+	test_mgcp_find();
 	return 0;
 }