tests: Add hnb-test to connect to hnbgw and tests its functions

Only connects currently
diff --git a/src/tests/Makefile b/src/tests/Makefile
index 1c9f63d..0cedab9 100644
--- a/src/tests/Makefile
+++ b/src/tests/Makefile
@@ -3,10 +3,22 @@
 PKG_LDFLAGS:=$(shell pkg-config --libs libosmocore libosmovty libosmogsm libasn1c)
 
 CFLAGS:=-g -Wall $(PKG_INCLUDES) -I.. -I../hnbap
-LDFLAGS:=$(PKG_LDFLAGS)
+LDFLAGS:=$(PKG_LDFLAGS) -lsctp
 
 test-helpers: ../iu_helpers.o ../asn1helpers.o test-helpers.c
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
 
 test-hnbap: ../iu_helpers.o ../asn1helpers.o ../hnbap_common.o ../hnbap_decoder.o test-hnbap.c ../hnbap/libosmo-asn1-hnbap.a
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
+
+HNBAP_OBJS=../hnbap_encoder.o ../hnbap_decoder.o ../hnbap_common.o
+RUA_OBJS=../rua_encoder.o ../rua_decoder.o ../rua_common.o
+RANAP_OBJS=../ranap_common.o #ranap_encoder.o ranap_decoder.o
+
+LIBS=../hnbap/libosmo-asn1-hnbap.a ../rua/libosmo-asn1-rua.a ../ranap/libosmo-asn1-ranap.a
+
+hnb-test: 	$(HNBAP_OBJS) $(RUA_OBJS) $(RANAP_OBJS) ../iu_helpers.o ../asn1helpers.o hnb-test.o $(LIBS)
+	$(CC) $(LDFLAGS) -o $@ $^
+
+clean:
+	@rm -f hnb-test test-helpers test-hnbap *.o
diff --git a/src/tests/hnb-test.c b/src/tests/hnb-test.c
new file mode 100644
index 0000000..583222a
--- /dev/null
+++ b/src/tests/hnb-test.c
@@ -0,0 +1,179 @@
+/* Test HNB */
+
+/* (C) 2015 by Daniel Willmann <dwillmann@sysmocom.de>
+ * (C) 2015 by Sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/sctp.h>
+#include <arpa/inet.h>
+
+#include <osmocom/core/application.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/write_queue.h>
+
+#include <osmocom/vty/telnet_interface.h>
+#include <osmocom/vty/logging.h>
+
+#include "hnb-test.h"
+
+static void *tall_hnb_ctx;
+void *talloc_asn1_ctx;
+
+struct hnb_test g_hnb_test = {
+	.gw_port = IUH_DEFAULT_SCTP_PORT,
+};
+
+static int hnb_read_cb(struct osmo_fd *fd)
+{
+	struct hnb_test *hnb_test = fd->data;
+	struct sctp_sndrcvinfo sinfo;
+	struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
+	int flags = 0;
+	int rc;
+
+	if (!msg)
+		return -ENOMEM;
+
+	rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
+			  NULL, NULL, &sinfo, &flags);
+	if (rc < 0) {
+		LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
+		/* FIXME: clean up after disappeared HNB */
+		return rc;
+	} else
+		msgb_put(msg, rc);
+
+	if (flags & MSG_NOTIFICATION) {
+		LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
+		msgb_free(msg);
+		return 0;
+	}
+
+	sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
+
+	switch (sinfo.sinfo_ppid) {
+	case IUH_PPI_HNBAP:
+		printf("HNBAP mesage received\n");
+//		rc = hnbgw_hnbap_rx(hnb, msg);
+		break;
+	case IUH_PPI_RUA:
+		printf("RUA mesage received\n");
+//		rc = hnbgw_rua_rx(hnb, msg);
+		break;
+	case IUH_PPI_SABP:
+	case IUH_PPI_RNA:
+	case IUH_PPI_PUA:
+		LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
+		     sinfo.sinfo_ppid);
+		rc = 0;
+		break;
+	default:
+		LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
+		     sinfo.sinfo_ppid);
+		rc = 0;
+		break;
+	}
+
+	msgb_free(msg);
+	return rc;
+}
+
+static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
+{
+	struct hnb_test *ctx = fd->data;
+	struct sctp_sndrcvinfo sinfo = {
+		.sinfo_ppid = htonl(msgb_ppid(msg)),
+		.sinfo_stream = 0,
+	};
+	int rc;
+
+	rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
+			&sinfo, 0);
+	/* we don't need to msgb_free(), write_queue does this for us */
+	return rc;
+}
+
+static const struct log_info_cat log_cat[] = {
+	[DMAIN] = {
+		.name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
+		.color = "",
+		.description = "Main program",
+	},
+};
+
+static const struct log_info hnb_test_log_info = {
+	.cat = log_cat,
+	.num_cat = ARRAY_SIZE(log_cat),
+};
+
+static struct vty_app_info vty_info = {
+	.name		= "OsmoHNB-Test",
+	.version	= "0",
+};
+
+int main(int argc, const char *argv)
+{
+	int rc;
+
+	tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
+	talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
+
+	rc = osmo_init_logging(&hnb_test_log_info);
+	if (rc < 0)
+		exit(1);
+
+	vty_init(&vty_info);
+
+	osmo_wqueue_init(&g_hnb_test.wqueue, 16);
+	g_hnb_test.wqueue.bfd.data = &g_hnb_test;
+	g_hnb_test.wqueue.read_cb = hnb_read_cb;
+	g_hnb_test.wqueue.write_cb = hnb_write_cb;
+
+	rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
+			   IPPROTO_SCTP, "127.0.0.1",
+			   g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
+	if (rc < 0) {
+		perror("Error connecting to Iuh port");
+		exit(1);
+	}
+//	sctp_sock_init(g_hnb_test.conn_fd);
+
+	while (1) {
+		rc = osmo_select_main(0);
+		if (rc < 0)
+			exit(3);
+	}
+
+	/* not reached */
+	exit(0);
+}
diff --git a/src/tests/hnb-test.h b/src/tests/hnb-test.h
new file mode 100644
index 0000000..6a8491e
--- /dev/null
+++ b/src/tests/hnb-test.h
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <osmocom/core/select.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/write_queue.h>
+
+#define DEBUG
+#include <osmocom/core/logging.h>
+
+#define msgb_ppid(msg)		(msg)->cb[0]
+
+enum {
+	DMAIN,
+};
+
+
+/* 25.467 Section 7.1 */
+#define IUH_DEFAULT_SCTP_PORT	29169
+#define RNA_DEFAULT_SCTP_PORT	25471
+
+#define IUH_PPI_RUA		19
+#define IUH_PPI_HNBAP		20
+#define IUH_PPI_SABP		31
+#define IUH_PPI_RNA		42
+#define IUH_PPI_PUA		55
+
+#define IUH_MSGB_SIZE	2048
+
+struct umts_cell_id {
+	uint16_t mcc;	/*!< Mobile Country Code */
+	uint16_t mnc;	/*!< Mobile Network Code */
+	uint16_t lac;	/*!< Locaton Area Code */
+	uint16_t rac;	/*!< Routing Area Code */
+	uint16_t sac;	/*!< Service Area Code */
+	uint32_t cid;	/*!< Cell ID */
+};
+
+struct ue_context {
+	/*! Entry in the HNB-global list of UE */
+	struct llist_head list;
+	/*! Unique Context ID for this UE */
+	uint32_t context_id;
+	char imsi[16+1];
+};
+
+struct hnb_test {
+	uint16_t gw_port;
+	/*! SCTP listen socket for incoming connections */
+	struct osmo_fd conn_fd;
+
+	/*! SCTP socket + write queue for Iuh to this specific HNB */
+	struct osmo_wqueue wqueue;
+	/*! copied from HNB-Identity-Info IE */
+	char identity_info[256];
+	/*! copied from Cell Identity IE */
+	struct umts_cell_id id;
+
+	/*! SCTP stream ID for HNBAP */
+	uint16_t hnbap_stream;
+	/*! SCTP stream ID for RUA */
+	uint16_t rua_stream;
+};
+
+extern struct hnb_test g_hnb_test;