Merge branch 'on-waves/nat-alloc-bsc-endpoints'
diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h
index 5f746f2..0c9af91 100644
--- a/openbsc/include/openbsc/bsc_nat.h
+++ b/openbsc/include/openbsc/bsc_nat.h
@@ -75,6 +75,10 @@
 	struct timer_list ping_timeout;
 	struct timer_list pong_timeout;
 
+	/* mgcp related code */
+	int endpoint_status[32];
+	int last_endpoint;
+
 	/* a back pointer */
 	struct bsc_nat *nat;
 };
@@ -253,14 +257,14 @@
  * MGCP/Audio handling
  */
 int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
-int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg);
+int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg);
 void bsc_mgcp_init(struct sccp_connections *);
 void bsc_mgcp_dlcx(struct sccp_connections *);
 void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
 int bsc_mgcp_nat_init(struct bsc_nat *nat);
 
 struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
-struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port);
+struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port);
 void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
 
 void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
diff --git a/openbsc/include/openbsc/bsc_nat_sccp.h b/openbsc/include/openbsc/bsc_nat_sccp.h
index 73e9c69..4f1afcb 100644
--- a/openbsc/include/openbsc/bsc_nat_sccp.h
+++ b/openbsc/include/openbsc/bsc_nat_sccp.h
@@ -79,10 +79,12 @@
 	int con_type;
 	int con_local;
 
-	/* GSM audio handling. That is 32 * multiplex + ts */
-	int crcx;
-	int msc_timeslot;
-	int bsc_timeslot;
+	/*
+	 * audio handling. Remember if we have ever send a CRCX,
+	 * remember the endpoint used by the MSC and BSC.
+	 */
+	int msc_endp;
+	int bsc_endp;
 
 	/* timeout handling */
 	struct timespec creation_time;
diff --git a/openbsc/include/openbsc/mgcp.h b/openbsc/include/openbsc/mgcp.h
index 38fe504..ed070cd 100644
--- a/openbsc/include/openbsc/mgcp.h
+++ b/openbsc/include/openbsc/mgcp.h
@@ -147,7 +147,16 @@
 {
 	if (timeslot == 0)
 		timeslot = 1;
-	return timeslot + (31 * multiplex);
+	return timeslot + (32 * multiplex);
+}
+
+static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
+{
+	*multiplex = endpoint / 32;
+	*timeslot = endpoint % 32;
+
+	if (*timeslot == 1)
+		*timeslot = 0;
 }
 
 
diff --git a/openbsc/src/nat/bsc_mgcp_utils.c b/openbsc/src/nat/bsc_mgcp_utils.c
index 067df0e..1728a41 100644
--- a/openbsc/src/nat/bsc_mgcp_utils.c
+++ b/openbsc/src/nat/bsc_mgcp_utils.c
@@ -38,14 +38,43 @@
 #include <errno.h>
 #include <unistd.h>
 
-int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
+static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
+{
+	const int number_endpoints = ARRAY_SIZE(bsc->endpoint_status);
+	int i;
+
+	for (i = 1; i < number_endpoints; ++i) {
+		int endpoint = (bsc->last_endpoint + i) % number_endpoints;
+		if (endpoint == 0)
+			endpoint = 1;
+
+		if (bsc->endpoint_status[endpoint] == 0) {
+			bsc->endpoint_status[endpoint] = 1;
+			con->bsc_endp = endpoint;
+			bsc->last_endpoint = endpoint;
+			return 0;
+		}
+	}
+
+	return -1;
+}
+
+static uint16_t create_cic(int endpoint)
+{
+	int timeslot, multiplex;
+
+	mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
+	return (multiplex << 5) | (timeslot & 0x1f);
+}
+
+int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
 {
 	struct sccp_connections *mcon;
 	struct tlv_parsed tp;
 	uint16_t cic;
 	uint8_t timeslot;
 	uint8_t multiplex;
-	int combined;
+	int endp;
 
 	if (!msg->l3h) {
 		LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
@@ -68,22 +97,32 @@
 	multiplex = (cic & ~0x1f) >> 5;
 
 
-	combined = (32 * multiplex) + timeslot;
+	endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
 
 	/* find stale connections using that endpoint */
 	llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
-		if (mcon->msc_timeslot == combined) {
+		if (mcon->msc_endp == endp) {
 			LOGP(DNAT, LOGL_ERROR,
-			     "Timeslot %d was assigned to 0x%x and now 0x%x\n",
-			     combined,
+			     "Endpoint %d was assigned to 0x%x and now 0x%x\n",
+			     endp,
 			     sccp_src_ref_to_int(&mcon->patched_ref),
 			     sccp_src_ref_to_int(&con->patched_ref));
 			bsc_mgcp_dlcx(mcon);
 		}
 	}
 
-	con->msc_timeslot = combined;
-	con->bsc_timeslot = con->msc_timeslot;
+	con->msc_endp = endp;
+	if (bsc_assign_endpoint(con->bsc, con) != 0)
+		return -1;
+
+	/*
+	 * now patch the message for the new CIC...
+	 * still assumed to be one multiplex only
+	 */
+	cic = htons(create_cic(con->bsc_endp));
+	memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
+		&cic, sizeof(cic));
+
 	return 0;
 }
 
@@ -109,7 +148,7 @@
 }
 
 /* send a MDCX where we do not want a response */
-static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, struct mgcp_endpoint *endp)
+static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
 {
 	char buf[2096];
 	int len;
@@ -120,13 +159,15 @@
 		       "\r\n"
 		       "c=IN IP4 %s\r\n"
 		       "m=audio %d RTP/AVP 255\r\n",
-		       ENDPOINT_NUMBER(endp),
+		       port,
 		       bsc->nat->mgcp_cfg->source_addr,
 		       endp->bts_end.local_port);
 	if (len < 0) {
 		LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
 		return;
 	}
+
+	#warning "The MDCX is not send to the BSC. It should"
 }
 
 static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
@@ -135,7 +176,7 @@
 	int len;
 
 	len = snprintf(buf, sizeof(buf),
-		       "DLCX 23 %x@mgw MGCP 1.0\r\n"
+		       "DLCX 26 %x@mgw MGCP 1.0\r\n"
 		       "Z: noanswer\r\n", endpoint);
 	if (len < 0) {
 		LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
@@ -147,18 +188,19 @@
 
 void bsc_mgcp_init(struct sccp_connections *con)
 {
-	con->msc_timeslot = -1;
-	con->bsc_timeslot = -1;
-	con->crcx = 0;
+	con->msc_endp = -1;
+	con->bsc_endp = -1;
 }
 
 void bsc_mgcp_dlcx(struct sccp_connections *con)
 {
 	/* send a DLCX down the stream */
-	if (con->bsc_timeslot != -1 && con->crcx) {
-		int endp = mgcp_timeslot_to_endpoint(0, con->msc_timeslot);
-		bsc_mgcp_send_dlcx(con->bsc, endp);
-		bsc_mgcp_free_endpoint(con->bsc->nat, endp);
+	if (con->bsc_endp != -1) {
+		if (con->bsc->endpoint_status[con->bsc_endp] != 1)
+			LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
+		con->bsc->endpoint_status[con->bsc_endp] = 0;
+		bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
+		bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
 	}
 
 	bsc_mgcp_init(con);
@@ -171,9 +213,9 @@
 	struct sccp_connections *sccp;
 
 	llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
-		if (sccp->msc_timeslot == -1)
+		if (sccp->msc_endp == -1)
 			continue;
-		if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
+		if (sccp->msc_endp != endpoint)
 			continue;
 
 		con = sccp;
@@ -230,7 +272,7 @@
 	}
 
 	/* we need to generate a new and patched message */
-	bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
+	bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
 				   nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
 	if (!bsc_msg) {
 		LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
@@ -254,9 +296,8 @@
 		}
 
 		/* send the message and a fake MDCX to force sending of a dummy packet */
-		sccp->crcx = 1;
 		bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
-		bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
+		bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
 		return MGCP_POLICY_DEFER;
 	} else if (state == MGCP_ENDP_DLCX) {
 		/* we will free the endpoint now and send a DLCX to the BSC */
@@ -275,30 +316,29 @@
 static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
 				 struct bsc_connection *bsc)
 {
-		LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
-			ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
+	LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
+		ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
 
-		/* if a CRCX failed... send a DLCX down the stream */
-		if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
-			struct sccp_connections *con;
-			con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
-			if (!con) {
-				LOGP(DMGCP, LOGL_ERROR,
-					"No SCCP connection for endp 0x%x\n",
-					ENDPOINT_NUMBER(endp));
+	/* if a CRCX failed... send a DLCX down the stream */
+	if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
+		struct sccp_connections *con;
+		con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
+		if (!con) {
+			LOGP(DMGCP, LOGL_ERROR,
+				"No SCCP connection for endp 0x%x\n",
+				ENDPOINT_NUMBER(endp));
+		} else {
+			if (con->bsc == bsc) {
+				bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
 			} else {
-				if (con->bsc == bsc) {
-					bsc_mgcp_send_dlcx(bsc, ENDPOINT_NUMBER(endp));
-					con->crcx = 0;
-				} else {
-					LOGP(DMGCP, LOGL_ERROR,
-						"Endpoint belongs to a different BSC\n");
-				}
+				LOGP(DMGCP, LOGL_ERROR,
+					"Endpoint belongs to a different BSC\n");
 			}
 		}
+	}
 
-		bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
-		mgcp_free_endp(endp);
+	bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
+	mgcp_free_endp(endp);
 }
 
 /*
@@ -364,7 +404,7 @@
 	 * there should be nothing for us to rewrite so putting endp->rtp_port
 	 * with the value of 0 should be no problem.
 	 */
-	output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
+	output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
 				  bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
 
 	if (!output) {
@@ -401,9 +441,28 @@
 	return ci;
 }
 
-/* we need to replace some strings... */
-struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
+static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
+		       int endp, int len, int cr)
 {
+	int slen;
+	int ret;
+	char buf[40];
+
+	buf[0] = buf[39] = '\0';
+	ret = sscanf(tok, "%*s %s", buf);
+
+	slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
+			op, buf, endp, cr ? "\r\n" : "\n");
+	output->l3h = msgb_put(output, slen);
+}
+
+/* we need to replace some strings... */
+struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
+{
+	static const char *crcx_str = "CRCX ";
+	static const char *dlcx_str = "DLCX ";
+	static const char *mdcx_str = "MDCX ";
+
 	static const char *ip_str = "c=IN IP4 ";
 	static const char *aud_str = "m=audio ";
 
@@ -424,11 +483,18 @@
 
 	running = input;
 	output->l2h = output->data;
+	output->l3h = output->l2h;
 	for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
 		int len = strlen(token);
 		int cr = len > 0 && token[len - 1] == '\r';
 
-		if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
+		if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
+			patch_mgcp(output, "CRCX", token, endpoint, len, cr);
+		} else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
+			patch_mgcp(output, "DLCX", token, endpoint, len, cr);
+		} else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
+			patch_mgcp(output, "MDCX", token, endpoint, len, cr);
+		} else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
 			output->l3h = msgb_put(output, strlen(ip_str));
 			memcpy(output->l3h, ip_str, strlen(ip_str));
 			output->l3h = msgb_put(output, strlen(ip));
diff --git a/openbsc/src/nat/bsc_nat.c b/openbsc/src/nat/bsc_nat.c
index 997a57f..a380a61 100644
--- a/openbsc/src/nat/bsc_nat.c
+++ b/openbsc/src/nat/bsc_nat.c
@@ -422,7 +422,7 @@
 					struct rate_ctr_group *ctrg;
 					ctrg = con->bsc->cfg->stats.ctrg;
 					rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
-					if (bsc_mgcp_assign(con, msg) != 0)
+					if (bsc_mgcp_assign_patch(con, msg) != 0)
 						LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
 				} else
 					LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
diff --git a/openbsc/src/nat/bsc_nat_vty.c b/openbsc/src/nat/bsc_nat_vty.c
index d8af1c2..0bbd313 100644
--- a/openbsc/src/nat/bsc_nat_vty.c
+++ b/openbsc/src/nat/bsc_nat_vty.c
@@ -29,6 +29,7 @@
 
 #include <osmocore/talloc.h>
 #include <osmocore/rate_ctr.h>
+#include <osmocore/utils.h>
 
 #include <osmocom/sccp/sccp.h>
 
@@ -121,7 +122,7 @@
 			sccp_src_ref_to_int(&con->patched_ref),
 			con->has_remote_ref,
 			sccp_src_ref_to_int(&con->remote_ref),
-			con->msc_timeslot, con->bsc_timeslot,
+			con->msc_endp, con->bsc_endp,
 			bsc_con_type_to_string(con->con_type),
 			VTY_NEWLINE);
 	}
@@ -148,6 +149,30 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(show_bsc_mgcp, show_bsc_mgcp_cmd, "show bsc mgcp NR",
+      SHOW_STR "Display the MGCP status for a given BSC")
+{
+	struct bsc_connection *con;
+	int nr = atoi(argv[0]);
+	int i;
+
+	llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
+		if (!con->cfg)
+			continue;
+		if (con->cfg->nr != nr)
+			continue;
+
+		vty_out(vty, "MGCP Status for %d%s", con->cfg->nr, VTY_NEWLINE);
+		for (i = 1; i < ARRAY_SIZE(con->endpoint_status); ++i)
+			vty_out(vty, " Endpoint 0x%x %s%s", i,
+				con->endpoint_status[i] == 0 ? "free" : "allocated",
+				VTY_NEWLINE);
+		break;
+	}
+
+	return CMD_SUCCESS;
+}
+
 DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config",
       SHOW_STR "Display information about known BSC configs")
 {
@@ -564,6 +589,7 @@
 	install_element_ve(&close_bsc_cmd);
 	install_element_ve(&show_msc_cmd);
 	install_element_ve(&test_regex_cmd);
+	install_element_ve(&show_bsc_mgcp_cmd);
 
 	/* nat group */
 	install_element(CONFIG_NODE, &cfg_nat_cmd);
diff --git a/openbsc/tests/bsc-nat/bsc_data.c b/openbsc/tests/bsc-nat/bsc_data.c
index 6700b0c..a323979 100644
--- a/openbsc/tests/bsc-nat/bsc_data.c
+++ b/openbsc/tests/bsc-nat/bsc_data.c
@@ -94,7 +94,7 @@
 
 /* nothing to patch */
 static const char crcx[] = "CRCX 23265295 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
-static const char crcx_patched[] = "CRCX 23265295 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
+static const char crcx_patched[] = "CRCX 23265295 1e@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
 
 
 /* patch the ip and port */
@@ -102,8 +102,8 @@
 static const char crcx_resp_patched[] = "200 23265295\r\nI: 1\r\n\r\nv=0\r\nc=IN IP4 10.0.0.1\r\nm=audio 999 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";
 
 /* patch the ip and port */
-static const char mdcx[] = " MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 172.16.18.2\r\nt=0 0\r\nm=audio 4410 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
-static const char mdcx_patched[] = " MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 10.0.0.23\r\nt=0 0\r\nm=audio 6666 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
+static const char mdcx[] = "MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 172.16.18.2\r\nt=0 0\r\nm=audio 4410 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
+static const char mdcx_patched[] = "MDCX 23330829 1e@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 10.0.0.23\r\nt=0 0\r\nm=audio 6666 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
 
 
 static const char mdcx_resp[] = "200 23330829\r\n\r\nv=0\r\nc=IN IP4 172.16.18.2\r\nm=audio 4002 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";
diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c
index c5234aa..8f4e2d1 100644
--- a/openbsc/tests/bsc-nat/bsc_nat_test.c
+++ b/openbsc/tests/bsc-nat/bsc_nat_test.c
@@ -444,24 +444,29 @@
 	msg = msgb_alloc(4096, "foo");
 	copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
 	parsed = bsc_nat_parse(msg);
-	if (bsc_mgcp_assign(&con, msg) != 0) {
+	if (bsc_mgcp_assign_patch(&con, msg) != 0) {
 		fprintf(stderr, "Failed to handle assignment.\n");
 		abort();
 	}
 
-	if (con.msc_timeslot != 21) {
+	if (con.msc_endp != 21) {
 		fprintf(stderr, "Timeslot should be 21.\n");
 		abort();
 	}
 
-	if (con.bsc_timeslot != 21) {
-		fprintf(stderr, "Assigned timeslot should have been 21.\n");
+	if (con.bsc_endp != 1) {
+		fprintf(stderr, "Assigned timeslot should have been 1.\n");
 		abort();
 	}
+	if (con.bsc->endpoint_status[1] != 1) {
+		fprintf(stderr, "The status on the BSC is wrong.\n");
+		abort();
+	}
+
 	talloc_free(parsed);
 
 	bsc_mgcp_dlcx(&con);
-	if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) {
+	if (con.bsc_endp != -1 || con.msc_endp != -1 || con.bsc->endpoint_status[1] != 0) {
 		fprintf(stderr, "Clearing should remove the mapping.\n");
 		abort();
 	}
@@ -483,8 +488,8 @@
 	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->msc_endp = 12;
+	sccp_con->bsc_endp = 12;
 	sccp_con->bsc = con;
 	llist_add(&sccp_con->list_entry, &nat->sccp_connections);
 
@@ -498,13 +503,6 @@
 		abort();
 	}
 
-	sccp_con->msc_timeslot = 0;
-	sccp_con->bsc_timeslot = 0;
-	if (bsc_mgcp_find_con(nat, 1) != sccp_con) {
-		fprintf(stderr, "Didn't find the connection\n");
-		abort();
-	}
-
 	/* free everything */
 	talloc_free(nat);
 }
@@ -523,7 +521,7 @@
 
 		char *input = strdup(orig);
 
-		output = bsc_mgcp_rewrite(input, strlen(input), ip, port);
+		output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, ip, port);
 		if (msgb_l2len(output) != strlen(patc)) {
 			fprintf(stderr, "Wrong sizes for test: %d  %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
 			fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc);