add libosmo-mslookup abstract client

mslookup is a key concept in Distributed GSM, which allows querying the current
location of a subscriber in a number of cooperating but independent core
network sites, by arbitrary service names and by MSISDN/IMSI.

Add the abstract mslookup client library. An actual lookup method (besides
mslookup_client_fake.c) is added in a subsequent patch.

For a detailed overview of this and upcoming patches, please see the elaborate
comment at the top of mslookup.c.

Add as separate library, libosmo-mslookup, to allow adding D-GSM capability to
arbitrary client programs.

osmo-hlr will be the only mslookup server implementation, added in a subsequent
patch.

osmo-hlr itself will also use this library and act as an mslookup client, when
requesting the home HLR for locally unknown IMSIs.

Related: OS#4237
Patch-by: osmith, nhofmeyr
Change-Id: I83487ab8aad1611eb02e997dafbcb8344da13df1
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f8591a5..776f8a9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,6 +3,7 @@
 	gsup_server \
 	db \
 	db_upgrade \
+	mslookup \
 	$(NULL)
 
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
diff --git a/tests/mslookup/Makefile.am b/tests/mslookup/Makefile.am
new file mode 100644
index 0000000..71602a3
--- /dev/null
+++ b/tests/mslookup/Makefile.am
@@ -0,0 +1,49 @@
+AM_CPPFLAGS = \
+	$(all_includes) \
+	$(NULL)
+
+AM_CFLAGS = \
+	-Wall \
+	-ggdb3 \
+	-I$(top_srcdir)/include \
+	$(LIBOSMOCORE_CFLAGS) \
+	$(LIBOSMOGSM_CFLAGS) \
+	$(LIBOSMOABIS_CFLAGS) \
+	$(NULL)
+
+AM_LDFLAGS = \
+	-no-install \
+	$(NULL)
+
+EXTRA_DIST = \
+	mslookup_client_test.err \
+	mslookup_test.err \
+	$(NULL)
+
+check_PROGRAMS = \
+	mslookup_client_test \
+	mslookup_test \
+	$(NULL)
+
+mslookup_test_SOURCES = \
+	mslookup_test.c \
+	$(NULL)
+mslookup_test_LDADD = \
+	$(top_builddir)/src/mslookup/libosmo-mslookup.la \
+	$(LIBOSMOGSM_LIBS) \
+	$(NULL)
+
+mslookup_client_test_SOURCES = \
+	mslookup_client_test.c \
+	$(NULL)
+mslookup_client_test_LDADD = \
+	$(top_builddir)/src/mslookup/libosmo-mslookup.la \
+	$(LIBOSMOGSM_LIBS) \
+	$(NULL)
+
+.PHONY: update_exp
+update_exp:
+	for i in $(check_PROGRAMS); do \
+		echo "Updating $$i.err"; \
+		$(builddir)/$$i 2>"$(srcdir)/$$i.err"; \
+	done
diff --git a/tests/mslookup/mslookup_client_test.c b/tests/mslookup/mslookup_client_test.c
new file mode 100644
index 0000000..40be011
--- /dev/null
+++ b/tests/mslookup/mslookup_client_test.c
@@ -0,0 +1,245 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ *
+ * 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 <sys/time.h>
+#include <string.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/select.h>
+#include <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mslookup_client_fake.h>
+#include <osmocom/mslookup/mslookup_client.h>
+
+#define SERVICE_HLR_GSUP "gsup.hlr"
+#define SERVICE_SIP "sip.voice"
+
+void *ctx = NULL;
+
+static struct osmo_mslookup_fake_response fake_lookup_responses[] = {
+	{
+		.time_to_reply = { .tv_sec = 1, },
+		.for_id = {
+			.type = OSMO_MSLOOKUP_ID_IMSI,
+			.imsi = "1234567",
+		},
+		.for_service = SERVICE_HLR_GSUP,
+		.result = {
+			.rc = OSMO_MSLOOKUP_RC_RESULT,
+			.host_v4 = {
+				.af = AF_INET,
+				.ip = "12.34.56.7",
+				.port = 42,
+			},
+			.host_v6 = {
+				.af = AF_INET6,
+				.ip = "be:ef:ed:ca:fe:fa:ce::1",
+				.port = 42,
+			},
+			.age = 0,
+		},
+	},
+	{
+		.time_to_reply = { .tv_usec = 600 * 1000, },
+		.for_id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "112",
+		},
+		.for_service = SERVICE_SIP,
+		.result = {
+			.rc = OSMO_MSLOOKUP_RC_RESULT,
+			.host_v4 = {
+				.af = AF_INET,
+				.ip = "66.66.66.66",
+				.port = 666,
+			},
+			.host_v6 = {
+				.af = AF_INET,
+				.ip = "6666:6666:6666::6",
+				.port = 666,
+			},
+			.age = 423,
+		},
+	},
+	{
+		.time_to_reply = { .tv_usec = 800 * 1000, },
+		.for_id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "112",
+		},
+		.for_service = SERVICE_SIP,
+		.result = {
+			.rc = OSMO_MSLOOKUP_RC_RESULT,
+			.host_v4 = {
+				.af = AF_INET,
+				.ip = "112.112.112.112",
+				.port = 23,
+			},
+			.age = 235,
+		},
+	},
+	{
+		.time_to_reply = { .tv_sec = 1, .tv_usec = 200 * 1000, },
+		.for_id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "112",
+		},
+		.for_service = SERVICE_SIP,
+		.result = {
+			.rc = OSMO_MSLOOKUP_RC_RESULT,
+			.host_v4 = {
+				.af = AF_INET,
+				.ip = "99.99.99.99",
+				.port = 999,
+			},
+			.host_v6 = {
+				.af = AF_INET,
+				.ip = "9999:9999:9999::9",
+				.port = 999,
+			},
+			.age = 335,
+		},
+	},
+	{
+		.time_to_reply = { .tv_sec = 1, .tv_usec = 500 * 1000, },
+		.for_id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "112",
+		},
+		.for_service = SERVICE_SIP,
+		.result = {
+			.rc = OSMO_MSLOOKUP_RC_RESULT,
+			.host_v4 = {
+				.af = AF_INET,
+				.ip = "99.99.99.99",
+				.port = 999,
+			},
+			.age = 999,
+		},
+	},
+};
+
+const struct timeval fake_time_start_time = { 0, 0 };
+
+#define fake_time_passes(secs, usecs) do \
+{ \
+	struct timeval diff; \
+	osmo_gettimeofday_override_add(secs, usecs); \
+	osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
+	timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
+	LOGP(DMSLOOKUP, LOGL_DEBUG, "Total time passed: %d.%06d s\n", \
+	       (int)diff.tv_sec, (int)diff.tv_usec); \
+	osmo_timers_prepare(); \
+	osmo_timers_update(); \
+} while (0)
+
+static void fake_time_start()
+{
+	struct timespec *clock_override;
+
+	osmo_gettimeofday_override_time = fake_time_start_time;
+	osmo_gettimeofday_override = true;
+	clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
+	OSMO_ASSERT(clock_override);
+	clock_override->tv_sec = fake_time_start_time.tv_sec;
+	clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
+	osmo_clock_override_enable(CLOCK_MONOTONIC, true);
+	fake_time_passes(0, 0);
+}
+
+static void result_cb_once(struct osmo_mslookup_client *client,
+			   uint32_t request_handle,
+			   const struct osmo_mslookup_query *query,
+			   const struct osmo_mslookup_result *result)
+{
+	LOGP(DMSLOOKUP, LOGL_DEBUG, "result_cb(): %s\n", osmo_mslookup_result_name_c(ctx, query, result));
+}
+
+int main()
+{
+	ctx = talloc_named_const(NULL, 0, "main");
+	osmo_init_logging2(ctx, NULL);
+
+	log_set_print_filename(osmo_stderr_target, 0);
+	log_set_print_level(osmo_stderr_target, 0);
+	log_set_print_category(osmo_stderr_target, 0);
+	log_set_print_category_hex(osmo_stderr_target, 0);
+	log_set_use_color(osmo_stderr_target, 0);
+	log_set_category_filter(osmo_stderr_target, DMSLOOKUP, true, LOGL_DEBUG);
+
+	fake_time_start();
+
+	struct osmo_mslookup_client *client = osmo_mslookup_client_new(ctx);
+	osmo_mslookup_client_add_fake(client, fake_lookup_responses, ARRAY_SIZE(fake_lookup_responses));
+
+	/* Place some requests to be replied upon asynchronously */
+
+	struct osmo_mslookup_query_handling handling = {
+		.result_timeout_milliseconds = 1, /* set some timeout < min_wait_milliseconds */
+		.min_wait_milliseconds = 2000,
+		.result_cb = result_cb_once,
+	};
+
+	struct osmo_mslookup_query q1 = {
+		.service = SERVICE_HLR_GSUP,
+		.id = {
+			.type = OSMO_MSLOOKUP_ID_IMSI,
+			.imsi = "1234567",
+		},
+	};
+	OSMO_ASSERT(osmo_mslookup_client_request(client, &q1, &handling));
+
+	struct osmo_mslookup_query q2 = {
+		.service = SERVICE_SIP,
+		.id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "112",
+		},
+	};
+	handling.min_wait_milliseconds = 3000;
+	OSMO_ASSERT(osmo_mslookup_client_request(client, &q2, &handling));
+
+	struct osmo_mslookup_query q3 = {
+		.service = "smpp.sms",
+		.id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "00000",
+		},
+	};
+	handling.min_wait_milliseconds = 5000;
+	OSMO_ASSERT(osmo_mslookup_client_request(client, &q3, &handling));
+
+	struct osmo_mslookup_query q4 = {
+		.service = SERVICE_HLR_GSUP,
+		.id = {
+			.type = OSMO_MSLOOKUP_ID_MSISDN,
+			.msisdn = "666",
+		},
+	};
+	handling.min_wait_milliseconds = 10000;
+	uint32_t q4_handle;
+	OSMO_ASSERT((q4_handle = osmo_mslookup_client_request(client, &q4, &handling)));
+
+	while (osmo_gettimeofday_override_time.tv_sec < 6) {
+		log_reset_context();
+		fake_time_passes(0, 1e6 / 5);
+	}
+
+	osmo_mslookup_client_request_cancel(client, q4_handle);
+
+	return 0;
+}
diff --git a/tests/mslookup/mslookup_client_test.err b/tests/mslookup/mslookup_client_test.err
new file mode 100644
index 0000000..c552837
--- /dev/null
+++ b/tests/mslookup/mslookup_client_test.err
@@ -0,0 +1,47 @@
+Total time passed: 0.000000 s
+fake_lookup_request(gsup.hlr.1234567.imsi)
+fake_lookup_request(sip.voice.112.msisdn)
+fake_lookup_request(smpp.sms.00000.msisdn)
+fake_lookup_request(gsup.hlr.666.msisdn)
+Total time passed: 0.200000 s
+Total time passed: 0.400000 s
+Total time passed: 0.600000 s
+osmo_mslookup_client_rx_result(): sip.voice.112.msisdn -> ipv4: 66.66.66.66:666 -> ipv6: 6666:6666:6666::6:666 (age=423) (not-last)
+Total time passed: 0.800000 s
+osmo_mslookup_client_rx_result(): sip.voice.112.msisdn -> ipv4: 112.112.112.112:23 (age=235) (not-last)
+Total time passed: 1.000000 s
+osmo_mslookup_client_rx_result(): gsup.hlr.1234567.imsi -> ipv4: 12.34.56.7:42 -> ipv6: [be:ef:ed:ca:fe:fa:ce::1]:42 (age=0) (not-last)
+result_cb(): gsup.hlr.1234567.imsi -> ipv4: 12.34.56.7:42 -> ipv6: [be:ef:ed:ca:fe:fa:ce::1]:42 (age=0) (last)
+fake_lookup_request_cleanup() ok
+Total time passed: 1.200000 s
+osmo_mslookup_client_rx_result(): sip.voice.112.msisdn -> ipv4: 99.99.99.99:999 -> ipv6: 9999:9999:9999::9:999 (age=335) (not-last)
+Total time passed: 1.400000 s
+Total time passed: 1.600000 s
+osmo_mslookup_client_rx_result(): sip.voice.112.msisdn -> ipv4: 99.99.99.99:999 (age=999) (not-last)
+Total time passed: 1.800000 s
+Total time passed: 2.000000 s
+Total time passed: 2.200000 s
+Total time passed: 2.400000 s
+Total time passed: 2.600000 s
+Total time passed: 2.800000 s
+Total time passed: 3.000000 s
+result_cb(): sip.voice.112.msisdn -> ipv4: 112.112.112.112:23 (age=235) (last)
+fake_lookup_request_cleanup() ok
+Total time passed: 3.200000 s
+Total time passed: 3.400000 s
+Total time passed: 3.600000 s
+Total time passed: 3.800000 s
+Total time passed: 4.000000 s
+Total time passed: 4.200000 s
+Total time passed: 4.400000 s
+Total time passed: 4.600000 s
+Total time passed: 4.800000 s
+Total time passed: 5.000000 s
+result_cb(): smpp.sms.00000.msisdn not-found (last)
+fake_lookup_request_cleanup() ok
+Total time passed: 5.200000 s
+Total time passed: 5.400000 s
+Total time passed: 5.600000 s
+Total time passed: 5.800000 s
+Total time passed: 6.000000 s
+fake_lookup_request_cleanup() ok
diff --git a/tests/mslookup/mslookup_test.c b/tests/mslookup/mslookup_test.c
new file mode 100644
index 0000000..1672bd0
--- /dev/null
+++ b/tests/mslookup/mslookup_test.c
@@ -0,0 +1,88 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ *
+ * 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 <string.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/select.h>
+#include <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mslookup_client.h>
+
+void *ctx;
+
+const char *domains[] = {
+	"gsup.hlr.123456789012345.imsi",
+	"gsup.hlr.1.imsi",
+	"sip.voice.1.msisdn",
+	"a.b.c.imsi",
+	"",
+	".",
+	"...",
+	".....",
+	".....1.msisdn",
+	"fofdndsf. d.ads ofdsf. ads.kj.1243455132.msisdn",
+	"foo.12345678901234567890.imsi",
+	"gsup.hlr.123456789012345.what",
+	NULL,
+	"blarg",
+	"blarg.",
+	"blarg.1.",
+	"blarg.1.msisdn",
+	"blarg.1.msisdn.",
+	".1.msisdn",
+	"1.msisdn",
+	"qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmm.1.msisdn",
+	"qwerty.1.qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmm",
+};
+
+void test_osmo_mslookup_query_init_from_domain_str()
+{
+	int i;
+	for (i = 0; i < ARRAY_SIZE(domains); i++) {
+		const char *d = domains[i];
+		struct osmo_mslookup_query q;
+
+		int rc = osmo_mslookup_query_init_from_domain_str(&q, d);
+		if (rc)
+			fprintf(stderr, "%s -> rc = %d\n", osmo_quote_str(d, -1), rc);
+		else
+			fprintf(stderr, "%s -> %s %s %s\n", osmo_quote_str(d, -1),
+			       osmo_quote_str_c(ctx, q.service, -1),
+			       osmo_quote_str_c(ctx, q.id.imsi, -1),
+			       osmo_mslookup_id_type_name(q.id.type));
+	}
+}
+
+int main()
+{
+	ctx = talloc_named_const(NULL, 0, "main");
+	osmo_init_logging2(ctx, NULL);
+
+	log_set_print_filename(osmo_stderr_target, 0);
+	log_set_print_level(osmo_stderr_target, 0);
+	log_set_print_category(osmo_stderr_target, 0);
+	log_set_print_category_hex(osmo_stderr_target, 0);
+	log_set_use_color(osmo_stderr_target, 0);
+	log_set_category_filter(osmo_stderr_target, DMSLOOKUP, true, LOGL_DEBUG);
+
+	test_osmo_mslookup_query_init_from_domain_str();
+
+	talloc_free(ctx);
+
+	return 0;
+}
diff --git a/tests/mslookup/mslookup_test.err b/tests/mslookup/mslookup_test.err
new file mode 100644
index 0000000..ee5ff21
--- /dev/null
+++ b/tests/mslookup/mslookup_test.err
@@ -0,0 +1,22 @@
+"gsup.hlr.123456789012345.imsi" -> "gsup.hlr" "123456789012345" imsi
+"gsup.hlr.1.imsi" -> rc = -5
+"sip.voice.1.msisdn" -> "sip.voice" "1" msisdn
+"a.b.c.imsi" -> rc = -5
+"" -> rc = -2
+"." -> rc = -3
+"..." -> rc = -4
+"....." -> rc = -4
+".....1.msisdn" -> "...." "1" msisdn
+"fofdndsf. d.ads ofdsf. ads.kj.1243455132.msisdn" -> "fofdndsf. d.ads ofdsf. ads.kj" "1243455132" msisdn
+"foo.12345678901234567890.imsi" -> rc = -11
+"gsup.hlr.123456789012345.what" -> rc = -7
+NULL -> rc = -1
+"blarg" -> rc = -2
+"blarg." -> rc = -3
+"blarg.1." -> rc = -4
+"blarg.1.msisdn" -> "blarg" "1" msisdn
+"blarg.1.msisdn." -> rc = -4
+".1.msisdn" -> rc = -3
+"1.msisdn" -> rc = -3
+"qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmm.1.msisdn" -> rc = -11
+"qwerty.1.qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmm" -> rc = -7
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 58c197d..39df7aa 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -39,3 +39,15 @@
 cat $abs_srcdir/db_upgrade/db_upgrade_test.err > experr
 AT_CHECK([$abs_srcdir/db_upgrade/db_upgrade_test.sh $abs_srcdir/db_upgrade $abs_builddir/db_upgrade], [], [expout], [experr])
 AT_CLEANUP
+
+AT_SETUP([mslookup])
+AT_KEYWORDS([mslookup])
+cat $abs_srcdir/mslookup/mslookup_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/mslookup/mslookup_test], [0], [ignore], [experr])
+AT_CLEANUP
+
+AT_SETUP([mslookup_client])
+AT_KEYWORDS([mslookup_client])
+cat $abs_srcdir/mslookup/mslookup_client_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/mslookup/mslookup_client_test], [0], [ignore], [experr])
+AT_CLEANUP