nat: Start using a write_queue for the BSC connection

We are still writing to the BSC directly and don't make real
use of this feature right now but we will need to do it.
diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h
index d42e489..5ac60df 100644
--- a/openbsc/include/openbsc/bsc_nat.h
+++ b/openbsc/include/openbsc/bsc_nat.h
@@ -28,6 +28,7 @@
 #include <osmocore/select.h>
 #include <osmocore/msgb.h>
 #include <osmocore/timer.h>
+#include <osmocore/write_queue.h>
 
 #define DIR_BSC 1
 #define DIR_MSC 2
@@ -77,7 +78,7 @@
 	int authenticated;
 
 	/* the fd we use to communicate */
-	struct bsc_fd bsc_fd;
+	struct write_queue write_queue;
 
 	/* the LAC assigned to this connection */
 	unsigned int lac;
diff --git a/openbsc/src/nat/bsc_nat.c b/openbsc/src/nat/bsc_nat.c
index 63263fa..0972208 100644
--- a/openbsc/src/nat/bsc_nat.c
+++ b/openbsc/src/nat/bsc_nat.c
@@ -44,7 +44,6 @@
 #include <openbsc/telnet_interface.h>
 
 #include <osmocore/talloc.h>
-#include <osmocore/write_queue.h>
 
 #include <vty/vty.h>
 
@@ -315,7 +314,7 @@
 		return -1;
 	}
 
-	return write(bsc->bsc_fd.fd, msg->data, msg->len);
+	return write(bsc->write_queue.bfd.fd, msg->data, msg->len);
 
 send_to_all:
 	/*
@@ -349,7 +348,7 @@
 				if (!bsc->authenticated || _lac != bsc->lac)
 					continue;
 
-				rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
+				rc = write(bsc->write_queue.bfd.fd, msg->data, msg->len);
 				if (rc < msg->len)
 					LOGP(DNAT, LOGL_ERROR,
 					     "Failed to write message to BTS: %d\n", rc);
@@ -363,7 +362,7 @@
 		if (!bsc->authenticated)
 			continue;
 
-		rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
+		rc = write(bsc->write_queue.bfd.fd, msg->data, msg->len);
 
 		/* try the next one */
 		if (rc < msg->len)
@@ -434,8 +433,8 @@
 static void remove_bsc_connection(struct bsc_connection *connection)
 {
 	struct sccp_connections *sccp_patch, *tmp;
-	bsc_unregister_fd(&connection->bsc_fd);
-	close(connection->bsc_fd.fd);
+	bsc_unregister_fd(&connection->write_queue.bfd);
+	close(connection->write_queue.bfd.fd);
 	llist_del(&connection->list_entry);
 
 	/* stop the timeout timer */
@@ -564,7 +563,7 @@
 	return -1;
 }
 
-static int ipaccess_bsc_cb(struct bsc_fd *bfd, unsigned int what)
+static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
 {
 	int error;
 	struct msgb *msg = ipaccess_read_msg(bfd, &error);
@@ -620,11 +619,12 @@
 	}
 
 	bsc->nat = nat;
-	bsc->bsc_fd.data = bsc;
-	bsc->bsc_fd.fd = ret;
-	bsc->bsc_fd.cb = ipaccess_bsc_cb;
-	bsc->bsc_fd.when = BSC_FD_READ;
-	if (bsc_register_fd(&bsc->bsc_fd) < 0) {
+	write_queue_init(&bsc->write_queue, 100);
+	bsc->write_queue.bfd.data = bsc;
+	bsc->write_queue.bfd.fd = ret;
+	bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
+	bsc->write_queue.bfd.when = BSC_FD_READ;
+	if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
 		LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
 		close(ret);
 		talloc_free(bsc);
@@ -633,7 +633,7 @@
 
 	LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
 	llist_add(&bsc->list_entry, &nat->bsc_connections);
-	ipaccess_send_id_ack(bsc->bsc_fd.fd);
+	ipaccess_send_id_ack(bsc->write_queue.bfd.fd);
 	ipaccess_send_id_req(ret);
 
 	/*
diff --git a/openbsc/src/nat/bsc_nat_vty.c b/openbsc/src/nat/bsc_nat_vty.c
index 5430670..6959037 100644
--- a/openbsc/src/nat/bsc_nat_vty.c
+++ b/openbsc/src/nat/bsc_nat_vty.c
@@ -89,7 +89,7 @@
 	struct bsc_connection *con;
 	llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
 		vty_out(vty, "BSC lac: %d auth: %d fd: %d%s",
-			con->lac, con->authenticated, con->bsc_fd.fd, VTY_NEWLINE);
+			con->lac, con->authenticated, con->write_queue.bfd.fd, VTY_NEWLINE);
 	}
 
 	return CMD_SUCCESS;