From b4d2736ecc0528416bc1c8099950d15f2a6c239d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 22 Dec 2015 23:03:06 +0100 Subject: [PATCH] Add some SUA client and server example code --- configure.ac | 1 + tests/Makefile.am | 2 +- tests/sigtran/Makefile.am | 10 ++++ tests/sigtran/sua_client_test.c | 56 +++++++++++++++++++++ tests/sigtran/sua_server_test.c | 78 +++++++++++++++++++++++++++++ tests/sigtran/sua_test_common.c | 89 +++++++++++++++++++++++++++++++++ tests/sigtran/sua_test_common.h | 31 ++++++++++++ 7 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 tests/sigtran/Makefile.am create mode 100644 tests/sigtran/sua_client_test.c create mode 100644 tests/sigtran/sua_server_test.c create mode 100644 tests/sigtran/sua_test_common.c create mode 100644 tests/sigtran/sua_test_common.h diff --git a/configure.ac b/configure.ac index d7323244..702941a4 100644 --- a/configure.ac +++ b/configure.ac @@ -44,5 +44,6 @@ AC_OUTPUT( tests/sccp/Makefile tests/mtp/Makefile tests/m2ua/Makefile + tests/sigtran/Makefile Makefile) diff --git a/tests/Makefile.am b/tests/Makefile.am index 2bf1089b..6e9b8077 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = sccp mtp m2ua +SUBDIRS = sccp mtp m2ua sigtran # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac diff --git a/tests/sigtran/Makefile.am b/tests/sigtran/Makefile.am new file mode 100644 index 00000000..e2991d42 --- /dev/null +++ b/tests/sigtran/Makefile.am @@ -0,0 +1,10 @@ +AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) +AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) + +noinst_PROGRAMS = sua_server_test sua_client_test + +sua_server_test_SOURCES = sua_server_test.c sua_test_common.c +sua_server_test_LDADD = $(top_builddir)/src/libosmo-sigtran.la $(LIBOSMOCORE_LIBS) $(TALLOC_LIBS) + +sua_client_test_SOURCES = sua_client_test.c sua_test_common.c +sua_client_test_LDADD = $(top_builddir)/src/libosmo-sigtran.la $(LIBOSMOCORE_LIBS) $(TALLOC_LIBS) diff --git a/tests/sigtran/sua_client_test.c b/tests/sigtran/sua_client_test.c new file mode 100644 index 00000000..0f635289 --- /dev/null +++ b/tests/sigtran/sua_client_test.c @@ -0,0 +1,56 @@ +#include "sua_test_common.h" + +struct osmo_sua_user *g_user; +struct sua_link *g_link; + +static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) +{ + struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; + struct osmo_prim_hdr *resp = NULL; + uint8_t payload[] = { 0xa1, 0xa2, 0xa3 }; + + printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); + + switch (OSMO_PRIM_HDR(oph)) { + case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): + printf("N-CONNECT.ind(%u), issuing DATA.req\n", + prim->u.connect.conn_id); + resp = make_dt1_req(prim->u.connect.conn_id, payload, sizeof(payload)); + break; + } + + if (resp) + osmo_sua_user_link_down(link, resp); + + msgb_free(oph->msg); + return 0; +} + + +int main(int argc, char **argv) +{ + void *ctx = talloc_named_const(NULL, 1, "root"); + int rc; + + osmo_sua_set_log_area(DSUA); + + osmo_init_logging(&test_log_info); + + g_user = osmo_sua_user_create(ctx, sccp_sap_up); + + rc = osmo_sua_client_connect(g_user, "127.0.0.1", 2342); + if (rc < 0) { + exit(1); + } + + g_link = osmo_sua_client_get_link(g_user); + + int i = 8000; + + while (1) { + if (i < 8010) + tx_conn_req(g_link, i++); + //tx_unitdata(g_link); + osmo_select_main(0); + } +} diff --git a/tests/sigtran/sua_server_test.c b/tests/sigtran/sua_server_test.c new file mode 100644 index 00000000..1fccf01c --- /dev/null +++ b/tests/sigtran/sua_server_test.c @@ -0,0 +1,78 @@ +#include "sua_test_common.h" + +struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param) +{ + struct msgb *msg = msgb_alloc(1024, "conn_resp"); + struct osmo_scu_prim *prim; + + prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); + osmo_prim_init(&prim->oph, SCCP_SAP_USER, + OSMO_SCU_PRIM_N_CONNECT, + PRIM_OP_RESPONSE, msg); + memcpy(&prim->u.connect, param, sizeof(prim->u.connect)); + return &prim->oph; +} + +static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) +{ + struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; + struct osmo_prim_hdr *resp = NULL; + const uint8_t payload[] = { 0xb1, 0xb2, 0xb3 }; + + printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); + + switch (OSMO_PRIM_HDR(oph)) { + case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): + /* confirmation of outbound connection */ + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): + /* indication of new inbound connection request*/ + printf("N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id); + resp = make_conn_resp(&prim->u.connect); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): + /* indication of disconnect */ + printf("N-DISCONNECT.ind(%u)\n", prim->u.disconnect.conn_id); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): + /* connection-oriented data received */ + printf("N-DATA.ind(%u, %s)\n", prim->u.data.conn_id, + osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); + resp = make_dt1_req(prim->u.data.conn_id, payload, sizeof(payload)); + break; + case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): + /* connection-oriented data received */ + printf("N-UNITDATA.ind(%s)\n", + osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); + tx_unitdata(link); + break; + } + + if (resp) + osmo_sua_user_link_down(link, resp); + + msgb_free(oph->msg); + return 0; +} + +int main(int argc, char **argv) +{ + struct osmo_sua_user *user; + void *ctx = talloc_named_const(NULL, 1, "root"); + int rc; + + osmo_sua_set_log_area(DSUA); + + osmo_init_logging(&test_log_info); + + user = osmo_sua_user_create(ctx, sccp_sap_up); + + rc = osmo_sua_server_listen(user, "127.0.0.1", 2342); + if (rc < 0) { + exit(1); + } + + while (1) { + osmo_select_main(0); + } +} diff --git a/tests/sigtran/sua_test_common.c b/tests/sigtran/sua_test_common.c new file mode 100644 index 00000000..aed48994 --- /dev/null +++ b/tests/sigtran/sua_test_common.c @@ -0,0 +1,89 @@ +#include "sua_test_common.h" + +static const struct log_info_cat log_cat[] = { + [DMAIN] = { + .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "Main program", + }, + [DSUA] = { + .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1, + .color = "", + .description = "SCCP User Adaption", + }, +}; + +const struct log_info test_log_info = { + .cat = log_cat, + .num_cat = ARRAY_SIZE(log_cat), +}; + +int tx_unitdata(struct osmo_sua_link *link) +{ + struct msgb *msg = msgb_alloc(1024, "tx_unitdata"); + struct osmo_scu_prim *prim; + struct osmo_scu_unitdata_param *param; + uint8_t *cur; + + prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); + param = &prim->u.unitdata; + param->calling_addr.presence = OSMO_SCCP_ADDR_T_SSN; + param->called_addr.presence = OSMO_SCCP_ADDR_T_SSN; + osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_REQUEST, msg); + + cur = msg->l2h = msgb_put(msg, 3); + cur[0] = 1; cur[1] = 2; cur[2] = 3; + + return osmo_sua_user_link_down(link, &prim->oph); +} + +static void sccp_make_addr_pc_ssn(struct osmo_sccp_addr *addr, uint32_t pc, uint32_t ssn) +{ + addr->presence = OSMO_SCCP_ADDR_T_SSN | OSMO_SCCP_ADDR_T_PC; + addr->ssn = ssn; + addr->pc = pc; +} + +#define SSN_RANAP 142 + +struct osmo_prim_hdr *make_conn_req(uint32_t conn_id) +{ + struct msgb *msg = msgb_alloc(1024, "conn_req"); + struct osmo_scu_prim *prim; + + prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); + osmo_prim_init(&prim->oph, SCCP_SAP_USER, + OSMO_SCU_PRIM_N_CONNECT, + PRIM_OP_REQUEST, msg); + /* Set SSN for calling and called addr */ + sccp_make_addr_pc_ssn(&prim->u.connect.called_addr, 2, SSN_RANAP); + sccp_make_addr_pc_ssn(&prim->u.connect.calling_addr, 1, SSN_RANAP); + prim->u.connect.sccp_class = 2; + prim->u.connect.conn_id = conn_id; + + return &prim->oph; +} + +int tx_conn_req(struct osmo_sua_link *link, uint32_t conn_id) +{ + struct osmo_prim_hdr *prim = make_conn_req(conn_id); + return osmo_sua_user_link_down(link, prim); +} + +struct osmo_prim_hdr * +make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len) +{ + struct msgb *msg = msgb_alloc(1024, "dt1"); + struct osmo_scu_prim *prim; + + prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); + osmo_prim_init(&prim->oph, SCCP_SAP_USER, + OSMO_SCU_PRIM_N_DATA, + PRIM_OP_REQUEST, msg); + prim->u.data.conn_id = conn_id; + + msg->l2h = msgb_put(msg, len); + memcpy(msg->l2h, data, len); + + return &prim->oph; +} diff --git a/tests/sigtran/sua_test_common.h b/tests/sigtran/sua_test_common.h new file mode 100644 index 00000000..8ec65994 --- /dev/null +++ b/tests/sigtran/sua_test_common.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + + +enum log_cat { + DMAIN, + DSUA, + DXUA, +}; + +extern const struct log_info test_log_info; + +int tx_unitdata(struct osmo_sua_link *link); +int tx_conn_req(struct osmo_sua_link *link, uint32_t conn_id); + +struct osmo_prim_hdr *make_conn_req(uint32_t conn_id); +struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);