Merge branch 'on-waves/sccp'
diff --git a/openbsc/Makefile.am b/openbsc/Makefile.am
index deaba0f..7dcd956 100644
--- a/openbsc/Makefile.am
+++ b/openbsc/Makefile.am
@@ -4,7 +4,7 @@
 SUBDIRS = include src tests
 
 pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = openbsc.pc
+pkgconfig_DATA = openbsc.pc liblaf0rge1.pc libsccp.pc
 
 #dist-hook:
 #	rm -rf `find $(distdir) -name .svn`
diff --git a/openbsc/configure.in b/openbsc/configure.in
index cba6c6c..88a3f61 100644
--- a/openbsc/configure.in
+++ b/openbsc/configure.in
@@ -38,6 +38,8 @@
 
 AC_OUTPUT(
     openbsc.pc
+    liblaf0rge1.pc
+    libsccp.pc
     include/openbsc/Makefile
     include/vty/Makefile
     include/sccp/Makefile
diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am
index b476025..8aa90c3 100644
--- a/openbsc/include/openbsc/Makefile.am
+++ b/openbsc/include/openbsc/Makefile.am
@@ -1,8 +1,15 @@
-noinst_HEADERS = abis_nm.h abis_rsl.h debug.h db.h gsm_04_08.h gsm_data.h \
-		 gsm_subscriber.h linuxlist.h msgb.h select.h tlv.h gsm_04_11.h \
+noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \
+		 gsm_subscriber.h gsm_04_11.h \
 		 timer.h misdn.h chan_alloc.h telnet_interface.h paging.h \
-		 subchan_demux.h trau_frame.h e1_input.h trau_mux.h signal.h \
+		 subchan_demux.h trau_frame.h e1_input.h trau_mux.h \
 		 gsm_utils.h ipaccess.h rs232.h openbscdefines.h rtp_proxy.h \
-		 bsc_rll.h mncc.h talloc.h transaction.h ussd.h gsm_04_80.h \
+		 bsc_rll.h mncc.h transaction.h ussd.h gsm_04_80.h \
 		 silent_call.h mgcp.h meas_rep.h bitvec.h rest_octets.h \
 		 system_information.h handover.h statistics.h
+
+laforge_HEADERS = signal.h linuxlist.h timer.h talloc.h msgb.h select.h tlv.h \
+		  debug.h
+laforgedir= $(includedir)/laf0rge1
+
+openbsc_HEADERS = gsm_04_08.h meas_rep.h
+openbscdir = $(includedir)/openbsc
diff --git a/openbsc/include/openbsc/ipaccess.h b/openbsc/include/openbsc/ipaccess.h
index 8c12e59..e101428 100644
--- a/openbsc/include/openbsc/ipaccess.h
+++ b/openbsc/include/openbsc/ipaccess.h
@@ -48,6 +48,11 @@
 int ipaccess_rcvmsg_base(struct msgb *msg, struct bsc_fd *bfd);
 struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error);
 void ipaccess_prepend_header(struct msgb *msg, int proto);
+int ipaccess_send_id_ack(int fd);
+int ipaccess_send_id_req(int fd);
+
+int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len);
+
 
 /*
  * Firmware specific header
diff --git a/openbsc/include/openbsc/msgb.h b/openbsc/include/openbsc/msgb.h
index ab3c033..d0efc90 100644
--- a/openbsc/include/openbsc/msgb.h
+++ b/openbsc/include/openbsc/msgb.h
@@ -20,7 +20,8 @@
  *
  */
 
-#include <openbsc/linuxlist.h>
+#include <sys/types.h>
+#include "linuxlist.h"
 
 struct bts_link;
 
diff --git a/openbsc/include/openbsc/select.h b/openbsc/include/openbsc/select.h
index c2af1d7..2d8b3ec 100644
--- a/openbsc/include/openbsc/select.h
+++ b/openbsc/include/openbsc/select.h
@@ -1,7 +1,7 @@
 #ifndef _BSC_SELECT_H
 #define _BSC_SELECT_H
 
-#include <openbsc/linuxlist.h>
+#include "linuxlist.h"
 
 #define BSC_FD_READ	0x0001
 #define BSC_FD_WRITE	0x0002
diff --git a/openbsc/include/openbsc/telnet_interface.h b/openbsc/include/openbsc/telnet_interface.h
index f4e976f..8e0e910 100644
--- a/openbsc/include/openbsc/telnet_interface.h
+++ b/openbsc/include/openbsc/telnet_interface.h
@@ -27,9 +27,6 @@
 
 #include <vty/vty.h>
 
-#define TELNET_COMMAND_48	1
-#define TELNET_COMMAND_11	2
-
 struct telnet_connection {
 	struct llist_head entry;
 	struct gsm_network *network;
diff --git a/openbsc/include/openbsc/tlv.h b/openbsc/include/openbsc/tlv.h
index c90643e..6141b1f 100644
--- a/openbsc/include/openbsc/tlv.h
+++ b/openbsc/include/openbsc/tlv.h
@@ -22,6 +22,7 @@
 #define TLV_GROSS_LEN(x)	(x+2)
 #define TLV16_GROSS_LEN(x)	((2*x)+2)
 #define TL16V_GROSS_LEN(x)	(x+3)
+#define L16TV_GROSS_LEN(x)	(x+3)
 
 #define TVLV_MAX_ONEBYTE	0x7f
 
@@ -105,6 +106,18 @@
 	return tvlv_put(buf, tag, len, val);
 }
 
+static inline u_int8_t *msgb_l16tv_put(struct msgb *msg, u_int16_t len, u_int8_t tag,
+                                       const u_int8_t *val)
+{
+	u_int8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
+
+	*buf++ = len >> 8;
+	*buf++ = len & 0xff;
+	*buf++ = tag;
+	memcpy(buf, val, len);
+	return buf + len;
+}
+
 static inline u_int8_t *v_put(u_int8_t *buf, u_int8_t val)
 {
 	*buf++ = val;
diff --git a/openbsc/include/sccp/Makefile.am b/openbsc/include/sccp/Makefile.am
index 42fd310..6c8a517 100644
--- a/openbsc/include/sccp/Makefile.am
+++ b/openbsc/include/sccp/Makefile.am
@@ -1 +1,2 @@
-noinst_HEADERS = sccp_types.h sccp.h
+sccp_HEADERS = sccp_types.h sccp.h
+sccpdir = $(includedir)/sccp
diff --git a/openbsc/include/sccp/sccp.h b/openbsc/include/sccp/sccp.h
index e5e8b81..643479a 100644
--- a/openbsc/include/sccp/sccp.h
+++ b/openbsc/include/sccp/sccp.h
@@ -27,11 +27,11 @@
 #include <stdlib.h>
 
 #include <sys/socket.h>
-
-#include <openbsc/msgb.h>
+#include <sys/types.h>
 
 #include "sccp_types.h"
 
+struct msgb;
 struct sccp_system;
 
 enum {
diff --git a/openbsc/liblaf0rge1.pc.in b/openbsc/liblaf0rge1.pc.in
new file mode 100644
index 0000000..9539101
--- /dev/null
+++ b/openbsc/liblaf0rge1.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: LaF0rge Lib
+Description: C Utility Library
+Version: @VERSION@
+Libs: -L${libdir} -llaf0rge1
+Cflags: -I${includedir}/
+
diff --git a/openbsc/libsccp.pc.in b/openbsc/libsccp.pc.in
new file mode 100644
index 0000000..eda8d49
--- /dev/null
+++ b/openbsc/libsccp.pc.in
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: OpenBSC SCCP Lib
+Description: OpenBSC SCCP Lib
+Version: @VERSION@
+Libs: -L${libdir} -lsccp
+Cflags: -I${includedir}/
diff --git a/openbsc/openbsc.pc.in b/openbsc/openbsc.pc.in
index 0d00831..aba07e2 100644
--- a/openbsc/openbsc.pc.in
+++ b/openbsc/openbsc.pc.in
@@ -1,7 +1,7 @@
 prefix=@prefix@
 exec_prefix=@exec_prefix@
 libdir=@libdir@
-includedir=@includedir@/openbsc-1.0
+includedir=@includedir@/
 
 Name: OpenBSC
 Description: OpenBSC base station controller
diff --git a/openbsc/src/Makefile.am b/openbsc/src/Makefile.am
index 161c283..ba3d280 100644
--- a/openbsc/src/Makefile.am
+++ b/openbsc/src/Makefile.am
@@ -3,14 +3,19 @@
 
 sbin_PROGRAMS = bsc_hack bs11_config ipaccess-find ipaccess-config \
                 isdnsync bsc_mgcp ipaccess-proxy
-noinst_LIBRARIES = libbsc.a libmsc.a libvty.a libsccp.a
+noinst_LIBRARIES = libbsc.a libmsc.a libvty.a
 noinst_HEADERS = vty/cardshell.h
 
+bscdir = $(libdir)
+bsc_LIBRARIES = liblaf0rge1.a libsccp.a
+
+liblaf0rge1_a_SOURCES = msgb.c timer.c talloc.c select.c signal.c debug.c
+
 libbsc_a_SOURCES = abis_rsl.c abis_nm.c gsm_data.c gsm_04_08_utils.c \
-		msgb.c select.c chan_alloc.c timer.c debug.c \
+		chan_alloc.c \
 		gsm_subscriber_base.c subchan_demux.c bsc_rll.c transaction.c \
 		trau_frame.c trau_mux.c paging.c e1_config.c e1_input.c tlv_parser.c \
-		input/misdn.c input/ipaccess.c signal.c gsm_utils.c talloc.c \
+		input/misdn.c input/ipaccess.c signal.c gsm_utils.c \
 		talloc_ctx.c system_information.c bitvec.c rest_octets.c \
 		rtp_proxy.c statistics.c bts_siemens_bs11.c bts_ipaccess_nanobts.c \
 		bts_unknown.c
@@ -25,7 +30,7 @@
 libsccp_a_SOURCES = sccp/sccp.c
 
 bsc_hack_SOURCES = bsc_hack.c bsc_init.c vty_interface.c vty_interface_layer3.c
-bsc_hack_LDADD = libmsc.a libbsc.a libmsc.a libvty.a -ldl -ldbi $(LIBCRYPT)
+bsc_hack_LDADD = libmsc.a libbsc.a libmsc.a libvty.a liblaf0rge1.a -ldl -ldbi $(LIBCRYPT)
 
 bs11_config_SOURCES = bs11_config.c abis_nm.c gsm_data.c msgb.c debug.c \
 		      select.c timer.c rs232.c tlv_parser.c signal.c talloc.c \
@@ -34,11 +39,11 @@
 ipaccess_find_SOURCES = ipaccess/ipaccess-find.c select.c timer.c
 
 ipaccess_config_SOURCES = ipaccess/ipaccess-config.c ipaccess/ipaccess-firmware.c
-ipaccess_config_LDADD = libbsc.a libmsc.a libbsc.a libvty.a -ldl -ldbi $(LIBCRYPT)
+ipaccess_config_LDADD = libbsc.a libmsc.a libbsc.a libvty.a liblaf0rge1.a -ldl -ldbi $(LIBCRYPT)
 
 isdnsync_SOURCES = isdnsync.c
 
 bsc_mgcp_SOURCES = mgcp/mgcp_main.c mgcp/mgcp_protocol.c msgb.c talloc.c debug.c select.c timer.c telnet_interface.c
-bsc_mgcp_LDADD = libvty.a
+bsc_mgcp_LDADD = libvty.a liblaf0rge1.a
 
 ipaccess_proxy_SOURCES = ipaccess/ipaccess-proxy.c msgb.c select.c talloc.c debug.c timer.c
diff --git a/openbsc/src/bsc_init.c b/openbsc/src/bsc_init.c
index 622fb98..617d4ae 100644
--- a/openbsc/src/bsc_init.c
+++ b/openbsc/src/bsc_init.c
@@ -802,8 +802,10 @@
 		}
 		break;
 	case GSM_BAND_900:
-		if (bts->c0->arfcn < 1 || bts->c0->arfcn > 124) {
-			LOGP(DNM, LOGL_ERROR, "GSM900 channel must be between 1-124.\n");
+		if (bts->c0->arfcn < 1 ||
+		   (bts->c0->arfcn > 124 && bts->c0->arfcn < 955) ||
+		    bts->c0->arfcn > 1023)  {
+			LOGP(DNM, LOGL_ERROR, "GSM900 channel must be between 1-124, 955-1023.\n");
 			return -EINVAL;
 		}
 		break;
diff --git a/openbsc/src/input/ipaccess.c b/openbsc/src/input/ipaccess.c
index 90d7cea..73d798f 100644
--- a/openbsc/src/input/ipaccess.c
+++ b/openbsc/src/input/ipaccess.c
@@ -89,7 +89,7 @@
 	return idtag_names[tag];
 }
 
-static int ipac_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
+int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
 {
 	u_int8_t t_len;
 	u_int8_t t_tag;
@@ -164,6 +164,17 @@
 	return 0;
 }
 
+/* send the id ack */
+int ipaccess_send_id_ack(int fd)
+{
+	return write(fd, id_ack, sizeof(id_ack));
+}
+
+int ipaccess_send_id_req(int fd)
+{
+	return write(fd, id_req, sizeof(id_req));
+}
+
 /* base handling of the ip.access protocol */
 int ipaccess_rcvmsg_base(struct msgb *msg,
 			 struct bsc_fd *bfd)
@@ -180,7 +191,7 @@
 		break;
 	case IPAC_MSGT_ID_ACK:
 		DEBUGP(DMI, "ID_ACK? -> ACK!\n");
-		ret = write(bfd->fd, id_ack, sizeof(id_ack));
+		ret = ipaccess_send_id_ack(bfd->fd);
 		break;
 	}
 	return 0;
@@ -201,7 +212,7 @@
 	case IPAC_MSGT_ID_RESP:
 		DEBUGP(DMI, "ID_RESP ");
 		/* parse tags, search for Unit ID */
-		ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
+		ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
 				 msgb_l2len(msg)-2);
 		DEBUGP(DMI, "\n");
 
@@ -530,7 +541,7 @@
 	}
 
 	/* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
-	ret = write(bfd->fd, id_req, sizeof(id_req));
+	ret = ipaccess_send_id_req(bfd->fd);
 
         return ret;
 	//return e1inp_line_register(line);
@@ -587,6 +598,11 @@
 	bfd->when = BSC_FD_READ;
 	//bfd->data = line;
 
+	if (bfd->fd < 0) {
+		LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
+		return -EIO;
+	}
+
 	memset(&addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
 	addr.sin_port = htons(port);
@@ -598,18 +614,21 @@
 	if (ret < 0) {
 		LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n",
 			strerror(errno));
+		close(bfd->fd);
 		return -EIO;
 	}
 
 	ret = listen(bfd->fd, 1);
 	if (ret < 0) {
 		perror("listen");
+		close(bfd->fd);
 		return ret;
 	}
 	
 	ret = bsc_register_fd(bfd);
 	if (ret < 0) {
 		perror("register_listen_fd");
+		close(bfd->fd);
 		return ret;
 	}
 	return 0;
@@ -628,6 +647,11 @@
 	bfd->data = line;
 	bfd->priv_nr = 1;
 
+	if (bfd->fd < 0) {
+		LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
+		return -EIO;
+	}
+
 	setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
 	ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
diff --git a/openbsc/src/sccp/sccp.c b/openbsc/src/sccp/sccp.c
index 94fd017..b0de239 100644
--- a/openbsc/src/sccp/sccp.c
+++ b/openbsc/src/sccp/sccp.c
@@ -24,11 +24,12 @@
 
 #include <string.h>
 
-#include <sccp/sccp.h>
-
+#include <openbsc/msgb.h>
 #include <openbsc/debug.h>
 #include <openbsc/talloc.h>
-#include <openbsc/linuxlist.h>
+
+#include <sccp/sccp.h>
+
 
 static void *tall_sccp_ctx;
 static LLIST_HEAD(sccp_connections);
diff --git a/openbsc/tests/db/Makefile.am b/openbsc/tests/db/Makefile.am
index 8ce7e3c..310763a 100644
--- a/openbsc/tests/db/Makefile.am
+++ b/openbsc/tests/db/Makefile.am
@@ -4,5 +4,5 @@
 noinst_PROGRAMS = db_test
 
 db_test_SOURCES = db_test.c
-db_test_LDADD = $(top_builddir)/src/libbsc.a $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a -ldl -ldbi
+db_test_LDADD = $(top_builddir)/src/libbsc.a $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a $(top_builddir)/src/liblaf0rge1.a -ldl -ldbi
 
diff --git a/openbsc/tests/gsm0408/Makefile.am b/openbsc/tests/gsm0408/Makefile.am
index ff8caf9..69f1e4e 100644
--- a/openbsc/tests/gsm0408/Makefile.am
+++ b/openbsc/tests/gsm0408/Makefile.am
@@ -2,4 +2,4 @@
 noinst_PROGRAMS = gsm0408_test
 
 gsm0408_test_SOURCES = gsm0408_test.c
-gsm0408_test_LDADD = $(top_builddir)/src/libbsc.a $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a -ldbi
+gsm0408_test_LDADD = $(top_builddir)/src/libbsc.a $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a $(top_builddir)/src/liblaf0rge1.a -ldbi
diff --git a/openbsc/tests/sccp/Makefile.am b/openbsc/tests/sccp/Makefile.am
index 5a275fc..3e35014 100644
--- a/openbsc/tests/sccp/Makefile.am
+++ b/openbsc/tests/sccp/Makefile.am
@@ -4,5 +4,5 @@
 noinst_PROGRAMS = sccp_test
 
 sccp_test_SOURCES = sccp_test.c
-sccp_test_LDADD = $(top_builddir)/src/libsccp.a $(top_builddir)/src/libbsc.a
+sccp_test_LDADD = $(top_builddir)/src/libsccp.a $(top_builddir)/src/libbsc.a $(top_builddir)/src/liblaf0rge1.a
 
diff --git a/openbsc/tests/sccp/sccp_test.c b/openbsc/tests/sccp/sccp_test.c
index 553544d..562e134 100644
--- a/openbsc/tests/sccp/sccp_test.c
+++ b/openbsc/tests/sccp/sccp_test.c
@@ -26,9 +26,11 @@
 
 #include <arpa/inet.h>
 
-#include <sccp/sccp.h>
 #include <openbsc/gsm_data.h>
 #include <openbsc/debug.h>
+#include <openbsc/msgb.h>
+
+#include <sccp/sccp.h>
 
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
 
diff --git a/openbsc/tests/sms/Makefile.am b/openbsc/tests/sms/Makefile.am
index 807c674..c1eeec7 100644
--- a/openbsc/tests/sms/Makefile.am
+++ b/openbsc/tests/sms/Makefile.am
@@ -2,4 +2,4 @@
 noinst_PROGRAMS = sms_test
 
 sms_test_SOURCES = sms_test.c
-sms_test_LDADD = $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a -ldl -ldbi
+sms_test_LDADD = $(top_builddir)/src/libmsc.a $(top_builddir)/src/libbsc.a $(top_builddir)/src/liblaf0rge1.a -ldl -ldbi