Harald Welte has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/10113 )

Change subject: import gsup_client.c as new libosmo-gsup-client
......................................................................

import gsup_client.c as new libosmo-gsup-client

This imports the code from osmo-msc 6afef893e17bce67e4d4119acd34d480ed03ba77
with minimal changes to make it compile.  Symbol renaming ot osmo_
prefix is done separately in a follow-up patch to have a as-clean-as-possible
import first.

Requires: libosmocore.git Change-Id Ie36729996abd30b84d1c30a09f62ebc6a9794950
Change-Id: Ief50054ad135551625b684ed8a0486f7af0b2940
---
M .gitignore
M Makefile.am
M configure.ac
A include/Makefile.am
A include/osmocom/gsupclient/gsup_client.h
A libosmo-gsup-client.pc.in
M src/Makefile.am
A src/gsupclient/Makefile.am
A src/gsupclient/gsup_client.c
A src/gsupclient/gsup_test_client.c
10 files changed, 776 insertions(+), 0 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/.gitignore b/.gitignore
index d65cb68..571f5b1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,8 @@
 missing
 .deps

+*.pc
+
 src/db_test
 src/db_bootstrap.h
 src/osmo-hlr
diff --git a/Makefile.am b/Makefile.am
index f5062a2..8e18167 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,6 +3,7 @@
 SUBDIRS = \
        doc \
        src \
+       include \
        sql \
        tests \
        $(NULL)
@@ -11,6 +12,9 @@
        .version \
        $(NULL)

+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = libosmo-gsup-client.pc
+
 @RELMAKE@

 BUILT_SOURCES = $(top_srcdir)/.version
diff --git a/configure.ac b/configure.ac
index cedba45..16c8b51 100644
--- a/configure.ac
+++ b/configure.ac
@@ -99,6 +99,9 @@
        Makefile
        doc/Makefile
        src/Makefile
+       src/gsupclient/Makefile
+       include/Makefile
+       libosmo-gsup-client.pc
        sql/Makefile
        tests/Makefile
        tests/auc/Makefile
diff --git a/include/Makefile.am b/include/Makefile.am
new file mode 100644
index 0000000..dac7011
--- /dev/null
+++ b/include/Makefile.am
@@ -0,0 +1,2 @@
+nobase_include_HEADERS = osmocom/gsupclient/gsup_client.h
+
diff --git a/include/osmocom/gsupclient/gsup_client.h 
b/include/osmocom/gsupclient/gsup_client.h
new file mode 100644
index 0000000..f4362df
--- /dev/null
+++ b/include/osmocom/gsupclient/gsup_client.h
@@ -0,0 +1,66 @@
+/* GPRS Subscriber Update Protocol client */
+
+/* (C) 2014 by Sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Jacob Erlbeck
+ *
+ * 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/>.
+ *
+ */
+#pragma once
+
+#include <osmocom/core/timer.h>
+#include <osmocom/gsm/oap_client.h>
+
+/* a loss of GSUP between MSC and HLR is considered quite serious, let's try 
to recover as quickly as
+ * possible.  Even one new connection attempt per second should be quite 
acceptable until the link is
+ * re-established */
+#define GSUP_CLIENT_RECONNECT_INTERVAL 1
+#define GSUP_CLIENT_PING_INTERVAL 20
+
+struct msgb;
+struct ipa_client_conn;
+struct gsup_client;
+
+/* Expects message in msg->l2h */
+typedef int (*gsup_client_read_cb_t)(struct gsup_client *gsupc,
+                                    struct msgb *msg);
+
+struct gsup_client {
+       const char *unit_name;
+
+       struct ipa_client_conn *link;
+       gsup_client_read_cb_t read_cb;
+       void *data;
+
+       struct osmo_oap_client_state oap_state;
+
+       struct osmo_timer_list ping_timer;
+       struct osmo_timer_list connect_timer;
+       int is_connected;
+       int got_ipa_pong;
+};
+
+struct gsup_client *gsup_client_create(void *talloc_ctx,
+                                      const char *unit_name,
+                                      const char *ip_addr,
+                                      unsigned int tcp_port,
+                                      gsup_client_read_cb_t read_cb,
+                                      struct osmo_oap_client_config 
*oapc_config);
+
+void gsup_client_destroy(struct gsup_client *gsupc);
+int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg);
+struct msgb *gsup_client_msgb_alloc(void);
+
diff --git a/libosmo-gsup-client.pc.in b/libosmo-gsup-client.pc.in
new file mode 100644
index 0000000..4210a4a
--- /dev/null
+++ b/libosmo-gsup-client.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: Osmocom GSUP and OAP Client Library
+Description: C Utility Library
+Version: @VERSION@
+Libs: -L${libdir} @TALLOC_LIBS@ -losmogsm -losmocore
+Cflags: -I${includedir}/
+
diff --git a/src/Makefile.am b/src/Makefile.am
index ea5023d..e4506bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,3 +1,5 @@
+SUBDIRS = gsupclient
+
 AM_CFLAGS = \
        -Wall \
        $(LIBOSMOCORE_CFLAGS) \
diff --git a/src/gsupclient/Makefile.am b/src/gsupclient/Makefile.am
new file mode 100644
index 0000000..4a449ec
--- /dev/null
+++ b/src/gsupclient/Makefile.am
@@ -0,0 +1,20 @@
+# This is _NOT_ the library release version, it's an API version.
+# Please read chapter "Library interface versions" of the libtool documentation
+# before making any modifications: 
https://www.gnu.org/software/libtool/manual/html_node/Versioning.html
+LIBVERSION=0:0:0
+
+AM_CFLAGS = -Wall $(all_includes) -I$(top_srcdir)/include 
-I$(top_builddir)/include \
+           $(TALLOC_CFLAGS) $(LIBOSMOCORE_CFLAGS) $(LIBOSMOABIS_CFLAGS)
+
+lib_LTLIBRARIES = libosmo-gsup-client.la
+
+libosmo_gsup_client_la_SOURCES = gsup_client.c
+
+libosmo_gsup_client_la_LDFLAGS = -version-info $(LIBVERSION) -no-undefined
+libosmo_gsup_client_la_LIBADD = $(TALLOC_LIBS) $(LIBOSMOCORE_LIBS) 
$(LIBOSMOABIS_LIBS)
+
+noinst_PROGRAMS = gsup-test-client
+
+gsup_test_client_SOURCES = gsup_test_client.c
+gsup_test_client_LDADD = $(TALLOC_LIBS) $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) 
\
+                        libosmo-gsup-client.la
diff --git a/src/gsupclient/gsup_client.c b/src/gsupclient/gsup_client.c
new file mode 100644
index 0000000..761e8bd
--- /dev/null
+++ b/src/gsupclient/gsup_client.c
@@ -0,0 +1,345 @@
+/* Generic Subscriber Update Protocol client */
+
+/* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Jacob Erlbeck
+ * Author: Neels Hofmeyr
+ *
+ * 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 <osmocom/gsupclient/gsup_client.h>
+
+#include <osmocom/abis/ipa.h>
+#include <osmocom/gsm/oap_client.h>
+#include <osmocom/gsm/protocol/ipaccess.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/logging.h>
+
+#include <errno.h>
+#include <string.h>
+
+static void start_test_procedure(struct gsup_client *gsupc);
+
+static void gsup_client_send_ping(struct gsup_client *gsupc)
+{
+       struct msgb *msg = gsup_client_msgb_alloc();
+
+       msg->l2h = msgb_put(msg, 1);
+       msg->l2h[0] = IPAC_MSGT_PING;
+       ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
+       ipa_client_conn_send(gsupc->link, msg);
+}
+
+static int gsup_client_connect(struct gsup_client *gsupc)
+{
+       int rc;
+
+       if (gsupc->is_connected)
+               return 0;
+
+       if (osmo_timer_pending(&gsupc->connect_timer)) {
+               LOGP(DLGSUP, LOGL_DEBUG,
+                    "GSUP connect: connect timer already running\n");
+               osmo_timer_del(&gsupc->connect_timer);
+       }
+
+       if (osmo_timer_pending(&gsupc->ping_timer)) {
+               LOGP(DLGSUP, LOGL_DEBUG,
+                    "GSUP connect: ping timer already running\n");
+               osmo_timer_del(&gsupc->ping_timer);
+       }
+
+       if (ipa_client_conn_clear_queue(gsupc->link) > 0)
+               LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored 
messages\n");
+
+       rc = ipa_client_conn_open(gsupc->link);
+
+       if (rc >= 0) {
+               LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n",
+                    gsupc->link->addr, gsupc->link->port);
+               return 0;
+       }
+
+       LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n",
+            gsupc->link->addr, gsupc->link->port, strerror(-rc));
+
+       if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
+           rc == -EINVAL)
+               return rc;
+
+       osmo_timer_schedule(&gsupc->connect_timer,
+                           GSUP_CLIENT_RECONNECT_INTERVAL, 0);
+
+       LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to 
%s:%d\n",
+            gsupc->link->addr, gsupc->link->port);
+
+       return 0;
+}
+
+static void connect_timer_cb(void *gsupc_)
+{
+       struct gsup_client *gsupc = gsupc_;
+
+       if (gsupc->is_connected)
+               return;
+
+       gsup_client_connect(gsupc);
+}
+
+static void client_send(struct gsup_client *gsupc, int proto_ext,
+                       struct msgb *msg_tx)
+{
+       ipa_prepend_header_ext(msg_tx, proto_ext);
+       ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
+       ipa_client_conn_send(gsupc->link, msg_tx);
+       /* msg_tx is now queued and will be freed. */
+}
+
+static void gsup_client_oap_register(struct gsup_client *gsupc)
+{
+       struct msgb *msg_tx;
+       int rc;
+       rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx);
+
+       if ((rc < 0) || (!msg_tx)) {
+               LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot 
register.\n");
+               return;
+       }
+
+       client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
+}
+
+static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
+{
+       struct gsup_client *gsupc = link->data;
+
+       LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
+                    link->addr, link->port, up ? "UP" : "DOWN");
+
+       gsupc->is_connected = up;
+
+       if (up) {
+               start_test_procedure(gsupc);
+
+               if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED)
+                       gsup_client_oap_register(gsupc);
+
+               osmo_timer_del(&gsupc->connect_timer);
+       } else {
+               osmo_timer_del(&gsupc->ping_timer);
+
+               osmo_timer_schedule(&gsupc->connect_timer,
+                                   GSUP_CLIENT_RECONNECT_INTERVAL, 0);
+       }
+}
+
+static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb 
*msg_rx)
+{
+       int rc;
+       struct msgb *msg_tx;
+
+       /* If the oap_state is disabled, this will reject the messages. */
+       rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx);
+       msgb_free(msg_rx);
+       if (rc < 0)
+               return rc;
+
+       if (msg_tx)
+               client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
+
+       return 0;
+}
+
+static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
+{
+       struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
+       struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) 
msgb_l2(msg);
+       struct gsup_client *gsupc = (struct gsup_client *)link->data;
+       int rc;
+       struct ipaccess_unit ipa_dev = {
+               /* see gsup_client_create() on const vs non-const */
+               .unit_name = (char*)gsupc->unit_name,
+       };
+
+       OSMO_ASSERT(ipa_dev.unit_name);
+
+       msg->l2h = &hh->data[0];
+
+       rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
+
+       if (rc < 0) {
+               LOGP(DLGSUP, LOGL_NOTICE,
+                    "GSUP received an invalid IPA/CCM message from %s:%d\n",
+                    link->addr, link->port);
+               /* Link has been closed */
+               gsupc->is_connected = 0;
+               msgb_free(msg);
+               return -1;
+       }
+
+       if (rc == 1) {
+               uint8_t msg_type = *(msg->l2h);
+               /* CCM message */
+               if (msg_type == IPAC_MSGT_PONG) {
+                       LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
+                       gsupc->got_ipa_pong = 1;
+               }
+
+               msgb_free(msg);
+               return 0;
+       }
+
+       if (hh->proto != IPAC_PROTO_OSMO)
+               goto invalid;
+
+       if (!he || msgb_l2len(msg) < sizeof(*he))
+               goto invalid;
+
+       msg->l2h = &he->data[0];
+
+       if (he->proto == IPAC_PROTO_EXT_GSUP) {
+               OSMO_ASSERT(gsupc->read_cb != NULL);
+               gsupc->read_cb(gsupc, msg);
+               /* expecting read_cb() to free msg */
+       } else if (he->proto == IPAC_PROTO_EXT_OAP) {
+               return gsup_client_oap_handle(gsupc, msg);
+               /* gsup_client_oap_handle frees msg */
+       } else
+               goto invalid;
+
+       return 0;
+
+invalid:
+       LOGP(DLGSUP, LOGL_NOTICE,
+            "GSUP received an invalid IPA message from %s:%d, size = %d\n",
+            link->addr, link->port, msgb_length(msg));
+
+       msgb_free(msg);
+       return -1;
+}
+
+static void ping_timer_cb(void *gsupc_)
+{
+       struct gsup_client *gsupc = gsupc_;
+
+       LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
+            gsupc->is_connected ? "connected" : "not connected",
+            gsupc->got_ipa_pong ? "got" : "didn't get");
+
+       if (gsupc->got_ipa_pong) {
+               start_test_procedure(gsupc);
+               return;
+       }
+
+       LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
+       ipa_client_conn_close(gsupc->link);
+       gsupc->is_connected = 0;
+
+       gsup_client_connect(gsupc);
+}
+
+static void start_test_procedure(struct gsup_client *gsupc)
+{
+       osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
+
+       gsupc->got_ipa_pong = 0;
+       osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0);
+       LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
+       gsup_client_send_ping(gsupc);
+}
+
+struct gsup_client *gsup_client_create(void *talloc_ctx,
+                                      const char *unit_name,
+                                      const char *ip_addr,
+                                      unsigned int tcp_port,
+                                      gsup_client_read_cb_t read_cb,
+                                      struct osmo_oap_client_config 
*oapc_config)
+{
+       struct gsup_client *gsupc;
+       int rc;
+
+       gsupc = talloc_zero(talloc_ctx, struct gsup_client);
+       OSMO_ASSERT(gsupc);
+
+       /* struct ipaccess_unit has a non-const unit_name, so let's copy to be
+        * able to have a non-const unit_name here as well. To not taint the
+        * public gsup_client API, let's store it in a const char* anyway. */
+       gsupc->unit_name = talloc_strdup(gsupc, unit_name);
+       OSMO_ASSERT(gsupc->unit_name);
+
+       /* a NULL oapc_config will mark oap_state disabled. */
+       rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state);
+       if (rc != 0)
+               goto failed;
+
+       gsupc->link = ipa_client_conn_create(gsupc,
+                                            /* no e1inp */ NULL,
+                                            0,
+                                            ip_addr, tcp_port,
+                                            gsup_client_updown_cb,
+                                            gsup_client_read_cb,
+                                            /* default write_cb */ NULL,
+                                            gsupc);
+       if (!gsupc->link)
+               goto failed;
+
+       osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
+
+       rc = gsup_client_connect(gsupc);
+
+       if (rc < 0)
+               goto failed;
+
+       gsupc->read_cb = read_cb;
+
+       return gsupc;
+
+failed:
+       gsup_client_destroy(gsupc);
+       return NULL;
+}
+
+void gsup_client_destroy(struct gsup_client *gsupc)
+{
+       osmo_timer_del(&gsupc->connect_timer);
+       osmo_timer_del(&gsupc->ping_timer);
+
+       if (gsupc->link) {
+               ipa_client_conn_close(gsupc->link);
+               ipa_client_conn_destroy(gsupc->link);
+               gsupc->link = NULL;
+       }
+       talloc_free(gsupc);
+}
+
+int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
+{
+       if (!gsupc || !gsupc->is_connected) {
+               LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send 
%s\n", msgb_hexdump(msg));
+               msgb_free(msg);
+               return -ENOTCONN;
+       }
+
+       client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
+
+       return 0;
+}
+
+struct msgb *gsup_client_msgb_alloc(void)
+{
+       return msgb_alloc_headroom(4000, 64, __func__);
+}
diff --git a/src/gsupclient/gsup_test_client.c 
b/src/gsupclient/gsup_test_client.c
new file mode 100644
index 0000000..cfbd55e
--- /dev/null
+++ b/src/gsupclient/gsup_test_client.c
@@ -0,0 +1,321 @@
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/gsm/gsup.h>
+
+#include <osmocom/gsupclient/gsup_client.h>
+
+static struct gsup_client *g_gc;
+
+
+/***********************************************************************
+ * IMSI Operation
+ ***********************************************************************/
+static LLIST_HEAD(g_imsi_ops);
+
+struct imsi_op_stats {
+       uint32_t num_alloc;
+       uint32_t num_released;
+       uint32_t num_rx_success;
+       uint32_t num_rx_error;
+       uint32_t num_timeout;
+};
+
+enum imsi_op_type {
+       IMSI_OP_SAI,
+       IMSI_OP_LU,
+       IMSI_OP_ISD,
+       _NUM_IMSI_OP
+};
+
+static const struct value_string imsi_op_names[] = {
+       { IMSI_OP_SAI, "SAI" },
+       { IMSI_OP_LU, "LU" },
+       { IMSI_OP_ISD, "ISD" },
+       { 0, NULL }
+};
+
+static struct imsi_op_stats imsi_op_stats[_NUM_IMSI_OP];
+
+struct imsi_op {
+       struct llist_head list;
+       char imsi[17];
+       enum imsi_op_type type;
+       struct osmo_timer_list timer;
+};
+
+static struct imsi_op *imsi_op_find(const char *imsi,
+                            enum imsi_op_type type)
+{
+       struct imsi_op *io;
+
+       llist_for_each_entry(io, &g_imsi_ops, list) {
+               if (!strcmp(io->imsi, imsi) && io->type == type)
+                       return io;
+       }
+       return NULL;
+}
+
+static void imsi_op_timer_cb(void *data);
+
+static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi,
+                               enum imsi_op_type type)
+{
+       struct imsi_op *io;
+
+       if (imsi_op_find(imsi, type))
+               return NULL;
+
+       io = talloc_zero(ctx, struct imsi_op);
+       OSMO_STRLCPY_ARRAY(io->imsi, imsi);
+       io->type = type;
+       osmo_timer_setup(&io->timer, imsi_op_timer_cb, io);
+       llist_add(&io->list, &g_imsi_ops);
+       imsi_op_stats[type].num_alloc++;
+
+       return io;
+}
+
+static void imsi_op_release(struct imsi_op *io)
+{
+       osmo_timer_del(&io->timer);
+       llist_del(&io->list);
+       imsi_op_stats[io->type].num_released++;
+       talloc_free(io);
+}
+
+static void imsi_op_timer_cb(void *data)
+{
+       struct imsi_op *io = data;
+       printf("%s: Timer expiration\n", io->imsi);
+       imsi_op_stats[io->type].num_timeout++;
+       imsi_op_release(io);
+}
+
+/* allocate + generate + send Send-Auth-Info */
+static int req_auth_info(const char *imsi)
+{
+       struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_SAI);
+       struct osmo_gsup_message gsup = {0};
+       struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
+       int rc;
+
+       OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
+       gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
+
+       rc = osmo_gsup_encode(msg, &gsup);
+       if (rc < 0) {
+               printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
+               return rc;
+       }
+
+       return gsup_client_send(g_gc, msg);
+}
+
+/* allocate + generate + send Send-Auth-Info */
+static int req_loc_upd(const char *imsi)
+{
+       struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU);
+       struct osmo_gsup_message gsup = {0};
+       struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
+       int rc;
+
+       OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
+       gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
+
+       rc = osmo_gsup_encode(msg, &gsup);
+       if (rc < 0) {
+               printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
+               return rc;
+       }
+
+       return gsup_client_send(g_gc, msg);
+}
+
+static int resp_isd(struct imsi_op *io)
+{
+       struct osmo_gsup_message gsup = {0};
+       struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
+       int rc;
+
+       OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
+       gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
+
+       rc = osmo_gsup_encode(msg, &gsup);
+       if (rc < 0) {
+               printf("%s: encoding failure (%s)\n", io->imsi, strerror(-rc));
+               return rc;
+       }
+
+       imsi_op_release(io);
+
+       return gsup_client_send(g_gc, msg);
+}
+
+/* receive an incoming GSUP message */
+static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message 
*gsup)
+{
+       int is_error = 0, rc;
+
+       if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) {
+               imsi_op_stats[io->type].num_rx_error++;
+               is_error = 1;
+       } else
+               imsi_op_stats[io->type].num_rx_success++;
+
+       switch (io->type) {
+       case IMSI_OP_SAI:
+               printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : 
"");
+               /* now that we have auth tuples, request LU */
+               rc = req_loc_upd(io->imsi);
+               if (rc < 0)
+                       printf("Failed to request Location Update for %s\n", 
io->imsi);
+               imsi_op_release(io);
+               break;
+       case IMSI_OP_LU:
+               printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : 
"");
+               imsi_op_release(io);
+               break;
+       case IMSI_OP_ISD:
+               printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : 
"");
+               rc = resp_isd(io);
+               if (rc < 0)
+                       printf("Failed to insert subscriber data for %s\n", 
io->imsi);
+               break;
+       default:
+               printf("%s: Unknown\n", io->imsi);
+               imsi_op_release(io);
+               break;
+       }
+}
+
+static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type)
+{
+       switch (msg_type) {
+       case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
+       case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
+               return IMSI_OP_SAI;
+       case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
+       case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
+               return IMSI_OP_LU;
+       case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
+               return IMSI_OP_ISD;
+       default:
+               printf("Unknown GSUP msg_type %u\n", msg_type);
+               return -1;
+       }
+}
+
+static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
+{
+       struct osmo_gsup_message gsup_msg = {0};
+       struct imsi_op *io = NULL;
+       int rc;
+
+       DEBUGP(DLGSUP, "Rx GSUP %s\n", msgb_hexdump(msg));
+
+       rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
+       if (rc < 0)
+               return rc;
+
+       if (!gsup_msg.imsi[0])
+               return -1;
+
+       rc = op_type_by_gsup_msgt(gsup_msg.message_type);
+       if (rc < 0)
+               return rc;
+
+       switch (rc) {
+       case IMSI_OP_SAI:
+       case IMSI_OP_LU:
+               io = imsi_op_find(gsup_msg.imsi, rc);
+               break;
+       case IMSI_OP_ISD:
+               /* ISD is an inbound transaction */
+               io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD);
+               break;
+       }
+       if (!io)
+               return -1;
+
+       imsi_op_rx_gsup(io, &gsup_msg);
+       msgb_free(msg);
+
+       return 0;
+}
+
+static void print_report(void)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) {
+               struct imsi_op_stats *st = &imsi_op_stats[i];
+               const char *name = get_value_string(imsi_op_names, i);
+               printf("%s: %u alloc, %u released, %u success, %u error , %u 
tout\n",
+                       name, st->num_alloc, st->num_released, 
st->num_rx_success,
+                       st->num_rx_error, st->num_timeout);
+       }
+}
+
+static void sig_cb(int sig)
+{
+       switch (sig) {
+       case SIGINT:
+               print_report();
+               exit(0);
+               break;
+       }
+}
+
+/* default categories */
+static struct log_info_cat default_categories[] = {
+};
+
+static const struct log_info gsup_test_client_log_info = {
+       .cat = default_categories,
+       .num_cat = ARRAY_SIZE(default_categories),
+};
+
+int main(int argc, char **argv)
+{
+       unsigned long long i;
+       char *server_host = "127.0.0.1";
+       uint16_t server_port = OSMO_GSUP_PORT;
+       void *ctx = talloc_named_const(NULL, 0, "gsup_test_client");
+
+       osmo_init_logging2(ctx, &gsup_test_client_log_info);
+
+       g_gc = gsup_client_create(ctx, "GSUPTEST", server_host, server_port,
+                                 gsupc_read_cb, NULL);
+
+
+       signal(SIGINT, sig_cb);
+
+       for (i = 0; i < 10000; i++) {
+               unsigned long long imsi = 901790000000000 + i;
+               char imsi_buf[17] = { 0 };
+               int rc;
+
+               snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi);
+               rc = req_auth_info(imsi_buf);
+               if (rc < 0)
+                       printf("Failed to request Auth Info for %s\n", 
imsi_buf);
+
+               osmo_select_main(0);
+       }
+
+       while (1) {
+               osmo_select_main(0);
+       }
+
+       print_report();
+       exit(0);
+}

--
To view, visit https://gerrit.osmocom.org/10113
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ief50054ad135551625b684ed8a0486f7af0b2940
Gerrit-Change-Number: 10113
Gerrit-PatchSet: 5
Gerrit-Owner: Harald Welte <[email protected]>
Gerrit-Reviewer: Harald Welte <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-CC: Vadim Yanitskiy <[email protected]>

Reply via email to