nat: Allocate endpoints from multiple multiplexes.

This code allocates endpoints from multiple multiplexes but
will always leave 0x0 and 0x1f unassigned in the multiplex.
diff --git a/openbsc/src/nat/bsc_mgcp_utils.c b/openbsc/src/nat/bsc_mgcp_utils.c
index f002926..4df5621 100644
--- a/openbsc/src/nat/bsc_mgcp_utils.c
+++ b/openbsc/src/nat/bsc_mgcp_utils.c
@@ -48,7 +48,7 @@
 	if (!con->cfg)
 		return -1;
 
-	con->number_endpoints = 31 * con->cfg->number_multiplexes;
+	con->number_multiplexes = con->cfg->number_multiplexes;
 	con->_endpoint_status = talloc_zero_array(con, char,
 						  (32 * con->cfg->number_multiplexes) + 1);
 	return con->_endpoint_status == NULL;
@@ -56,13 +56,31 @@
 
 static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
 {
-	const int number_endpoints = bsc->number_endpoints;
+	int multiplex;
+	int timeslot;
+	const int number_endpoints = 32 * bsc->number_multiplexes;
 	int i;
 
-	for (i = 1; i <= number_endpoints; ++i) {
-		int endpoint = (bsc->last_endpoint + i) % number_endpoints;
-		if (endpoint == 0)
-			endpoint = 1;
+	mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, &timeslot);
+	timeslot += 1;
+
+	for (i = 0; i < number_endpoints; ++i) {
+		int endpoint;
+
+		/* Wrap around timeslots */
+		if (timeslot == 0)
+			timeslot = 1;
+
+		if (timeslot == 0x1f) {
+			timeslot = 1;
+			multiplex += 1;
+		}
+
+		/* Wrap around the multiplex */
+		if (multiplex >= bsc->number_multiplexes)
+			multiplex = 0;
+
+		endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
 
 		if (bsc->_endpoint_status[endpoint] == 0) {
 			bsc->_endpoint_status[endpoint] = 1;
@@ -70,6 +88,8 @@
 			bsc->last_endpoint = endpoint;
 			return 0;
 		}
+
+		timeslot += 1;
 	}
 
 	return -1;
diff --git a/openbsc/src/nat/bsc_nat_vty.c b/openbsc/src/nat/bsc_nat_vty.c
index 2aefe9b..5366dd3 100644
--- a/openbsc/src/nat/bsc_nat_vty.c
+++ b/openbsc/src/nat/bsc_nat_vty.c
@@ -170,10 +170,9 @@
 {
 	struct bsc_connection *con;
 	int nr = atoi(argv[0]);
-	int i;
+	int i, j, endp;
 
 	llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
-		int endpoints;
 		if (!con->cfg)
 			continue;
 		if (con->cfg->nr != nr)
@@ -184,11 +183,15 @@
 			continue;
 
 		vty_out(vty, "MGCP Status for %d%s", con->cfg->nr, VTY_NEWLINE);
-		endpoints = con->number_endpoints;
-		for (i = 1; i <= endpoints; ++i)
-			vty_out(vty, " Endpoint 0x%x %s%s", i,
-				con->_endpoint_status[i] == 0 ? "free" : "allocated",
+		for (i = 0; i < con->number_multiplexes; ++i) {
+			for (j = 0; j < 32; ++j) {
+				endp = mgcp_timeslot_to_endpoint(i, j);
+				vty_out(vty, " Endpoint 0x%x %s%s", endp,
+					con->_endpoint_status[endp] == 0 
+						? "free" : "allocated",
 				VTY_NEWLINE);
+			}
+		}
 		break;
 	}
 
@@ -632,7 +635,7 @@
 }
 
 DEFUN(cfg_bsc_nr_multip, cfg_bsc_nr_multip_cmd,
-      "number-multiplexes <1-1>",
+      "number-multiplexes <1-64>",
       "Number of multiplexes on a BSC\n" "Number of ports\n")
 {
 	struct bsc_config *conf = vty->index;