This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
neels gerrit-no-reply at lists.osmocom.orgHello osmith,
I'd like you to do a code review. Please visit
https://gerrit.osmocom.org/c/osmo-hlr/+/16202
to review the following change.
Change subject: add libosmo-mslookup and mDNS implementation
......................................................................
add libosmo-mslookup and mDNS implementation
Create a library with generic functions for performing a distributed
subscriber lookup, which allow querying IPs and ports of services by
MSISDN/IMSI.
The first implementation uses multicast DNS packets. I decided to write
custom DNS functions instead of using libc-ares (which we use in
OsmoSGSN already), because it is only a DNS client implementation and we
need both client and server. The DNS code is abstracted from the
mslookup specific code, so the DNS part could in theory be replaced with
a third party library in the future.
We decided to place the mslookup library in the OsmoHLR source tree
(instead of libosmocore), because the way we have implemented the D-GSM
network, OsmoHLR will be the only user of the library for now. In the
future, OsmoMSC may need it, but it depends on OsmoHLR already.
Related: OS#4237
Patch-by: osmith, nhofmeyr
Change-Id: I83487ab8aad1611eb02e997dafbcb8344da13df1
---
M configure.ac
M debian/control
A debian/libosmo-mslookup-dev.install
A debian/libosmo-mslookup0.install
M include/Makefile.am
M include/osmocom/hlr/logging.h
A include/osmocom/mslookup/mdns.h
A include/osmocom/mslookup/mdns_sock.h
A include/osmocom/mslookup/mslookup.h
A include/osmocom/mslookup/mslookup_client.h
A include/osmocom/mslookup/mslookup_client_fake.h
A include/osmocom/mslookup/mslookup_client_mdns.h
A libosmo-mslookup.pc.in
M src/Makefile.am
A src/mslookup/Makefile.am
A src/mslookup/mdns.c
A src/mslookup/mdns_msg.c
A src/mslookup/mdns_msg.h
A src/mslookup/mdns_record.c
A src/mslookup/mdns_record.h
A src/mslookup/mdns_rfc.c
A src/mslookup/mdns_rfc.h
A src/mslookup/mdns_sock.c
A src/mslookup/mslookup.c
A src/mslookup/mslookup_client.c
A src/mslookup/mslookup_client_fake.c
A src/mslookup/mslookup_client_mdns.c
M tests/Makefile.am
A tests/mslookup/Makefile.am
A tests/mslookup/mdns_test.c
A tests/mslookup/mdns_test.err
A tests/mslookup/mslookup_client_mdns_test.c
A tests/mslookup/mslookup_client_mdns_test.err
A tests/mslookup/mslookup_client_test.c
A tests/mslookup/mslookup_client_test.err
A tests/mslookup/mslookup_test.c
A tests/mslookup/mslookup_test.err
M tests/testsuite.at
38 files changed, 4,375 insertions(+), 2 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/02/16202/1
diff --git a/configure.ac b/configure.ac
index d39edb2..1afb5c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -174,10 +174,12 @@
doc/examples/Makefile
src/Makefile
src/gsupclient/Makefile
+ src/mslookup/Makefile
include/Makefile
include/osmocom/Makefile
include/osmocom/hlr/Makefile
libosmo-gsup-client.pc
+ libosmo-mslookup.pc
sql/Makefile
doc/manuals/Makefile
contrib/Makefile
@@ -188,4 +190,5 @@
tests/gsup_server/Makefile
tests/db/Makefile
tests/db_upgrade/Makefile
+ tests/mslookup/Makefile
)
diff --git a/debian/control b/debian/control
index debf669..dda411e 100644
--- a/debian/control
+++ b/debian/control
@@ -59,6 +59,28 @@
.
This package contains the development headers.
+Package: libosmo-mslookup0
+Section: libs
+Architecture: any
+Multi-Arch: same
+Depends: ${shlibs:Depends},
+ ${misc:Depends}
+Pre-Depends: ${misc:Pre-Depends}
+Description: Osmocom MS lookup library
+ This shared library contains routines for looking up mobile subscribers.
+
+Package: libosmo-mslookup-dev
+Architecture: any
+Multi-Arch: same
+Depends: ${shlibs:Depends},
+ libosmo-mslookup0 (= ${binary:Version}),
+ libosmocore-dev
+Pre-Depends: ${misc:Pre-Depends}
+Description: Development headers of Osmocom MS lookup library
+ This shared library contains routines for looking up mobile subscribers.
+ .
+ This package contains the development headers.
+
Package: osmo-hlr-doc
Architecture: all
Section: doc
diff --git a/debian/libosmo-mslookup-dev.install b/debian/libosmo-mslookup-dev.install
new file mode 100644
index 0000000..539bba8
--- /dev/null
+++ b/debian/libosmo-mslookup-dev.install
@@ -0,0 +1,5 @@
+usr/include/osmocom/mslookup
+usr/lib/*/libosmo-mslookup*.a
+usr/lib/*/libosmo-mslookup*.so
+usr/lib/*/libosmo-mslookup*.la
+usr/lib/*/pkgconfig/libosmo-mslookup.pc
diff --git a/debian/libosmo-mslookup0.install b/debian/libosmo-mslookup0.install
new file mode 100644
index 0000000..9cad0e8
--- /dev/null
+++ b/debian/libosmo-mslookup0.install
@@ -0,0 +1 @@
+usr/lib/*/libosmo-mslookup*.so.*
diff --git a/include/Makefile.am b/include/Makefile.am
index d8eb1ec..9827950 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,3 +1,11 @@
SUBDIRS = osmocom
-nobase_include_HEADERS = osmocom/gsupclient/gsup_client.h
+nobase_include_HEADERS = \
+ osmocom/gsupclient/gsup_client.h \
+ osmocom/mslookup/mdns.h \
+ osmocom/mslookup/mdns_sock.h \
+ osmocom/mslookup/mslookup_client_fake.h \
+ osmocom/mslookup/mslookup_client.h \
+ osmocom/mslookup/mslookup_client_mdns.h \
+ osmocom/mslookup/mslookup.h \
+ $(NULL)
diff --git a/include/osmocom/hlr/logging.h b/include/osmocom/hlr/logging.h
index ed24075..83f1acd 100644
--- a/include/osmocom/hlr/logging.h
+++ b/include/osmocom/hlr/logging.h
@@ -8,6 +8,7 @@
DGSUP,
DAUC,
DSS,
+ DMSLOOKUP,
};
extern const struct log_info hlr_log_info;
diff --git a/include/osmocom/mslookup/mdns.h b/include/osmocom/mslookup/mdns.h
new file mode 100644
index 0000000..137a1f0
--- /dev/null
+++ b/include/osmocom/mslookup/mdns.h
@@ -0,0 +1,36 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+/*! \file mdns.h */
+
+#pragma once
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/mslookup/mslookup.h>
+
+struct msgb *osmo_mdns_query_encode(void *ctx, uint16_t packet_id, const struct osmo_mslookup_query *query);
+
+struct osmo_mslookup_query *osmo_mdns_query_decode(void *ctx, const uint8_t *data, size_t data_len,
+ uint16_t *packet_id);
+
+struct msgb *osmo_mdns_result_encode(void *ctx, uint16_t packet_id, const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result);
+
+int osmo_mdns_result_decode(void *ctx, const uint8_t *data, size_t data_len, uint16_t *packet_id,
+ struct osmo_mslookup_query *query, struct osmo_mslookup_result *result);
diff --git a/include/osmocom/mslookup/mdns_sock.h b/include/osmocom/mslookup/mdns_sock.h
new file mode 100644
index 0000000..fde87a8
--- /dev/null
+++ b/include/osmocom/mslookup/mdns_sock.h
@@ -0,0 +1,33 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+
+struct osmo_mdns_sock {
+ struct osmo_fd osmo_fd;
+ struct addrinfo *ai;
+};
+
+struct osmo_mdns_sock *osmo_mdns_sock_init(void *ctx, const char *ip, unsigned int port,
+ int (*cb)(struct osmo_fd *fd, unsigned int what),
+ void *data, unsigned int priv_nr);
+int osmo_mdns_sock_send(const struct osmo_mdns_sock *mdns_sock, struct msgb *msg);
+void osmo_mdns_sock_cleanup(struct osmo_mdns_sock *mdns_sock);
diff --git a/include/osmocom/mslookup/mslookup.h b/include/osmocom/mslookup/mslookup.h
new file mode 100644
index 0000000..e5a42a1
--- /dev/null
+++ b/include/osmocom/mslookup/mslookup.h
@@ -0,0 +1,127 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/gsm/protocol/gsm_23_003.h>
+
+#define OSMO_MSLOOKUP_SERVICE_MAXLEN 64
+
+/*! Request HLR for the home HLR's GSUP connection. */
+#define OSMO_MSLOOKUP_SERVICE_HLR_GSUP "gsup.hlr"
+
+/*! Request SIP for a voice call (osmo-sip-connector or PBX). */
+#define OSMO_MSLOOKUP_SERVICE_SIP "sip.voice"
+
+/*! Request SMPP to deliver an SMS (osmo-msc or SMPP handler). */
+#define OSMO_MSLOOKUP_SERVICE_SMPP "smpp.sms"
+
+/*! Request GSUP to deliver an SMS (osmo-hlr or ESME). */
+#define OSMO_MSLOOKUP_SERVICE_SMS_GSUP "gsup.sms"
+
+bool osmo_mslookup_service_valid(const char *service);
+
+enum osmo_mslookup_id_type {
+ OSMO_MSLOOKUP_ID_NONE = 0,
+ OSMO_MSLOOKUP_ID_IMSI,
+ OSMO_MSLOOKUP_ID_MSISDN,
+};
+
+extern const struct value_string osmo_mslookup_id_type_names[];
+static inline const char *osmo_mslookup_id_type_name(enum osmo_mslookup_id_type val)
+{ return get_value_string(osmo_mslookup_id_type_names, val); }
+
+struct osmo_mslookup_id {
+ enum osmo_mslookup_id_type type;
+ union {
+ char imsi[GSM23003_IMSI_MAX_DIGITS+1];
+ char msisdn[GSM23003_MSISDN_MAX_DIGITS+1];
+ };
+};
+
+int osmo_mslookup_id_cmp(const struct osmo_mslookup_id *a, const struct osmo_mslookup_id *b);
+bool osmo_mslookup_id_valid(const struct osmo_mslookup_id *id);
+
+enum osmo_mslookup_result_code {
+ OSMO_MSLOOKUP_RC_NONE = 0,
+ /*! An intermediate valid result. The request is still open for more results. */
+ OSMO_MSLOOKUP_RC_RESULT,
+ /*! Returned when the final request timeout has elapsed without results. */
+ OSMO_MSLOOKUP_RC_NOT_FOUND,
+};
+
+extern const struct value_string osmo_mslookup_result_code_names[];
+static inline const char *osmo_mslookup_result_code_name(enum osmo_mslookup_result_code val)
+{ return get_value_string(osmo_mslookup_result_code_names, val); }
+
+/*! Information to request from a lookup. */
+struct osmo_mslookup_query {
+ /*! Which service to request: HLR, SMS or voice. Typically an OSMO_MSLOOKUP_SERVICE_* constant, but could be an
+ * arbitrary string that service providers understand. */
+ char service[OSMO_MSLOOKUP_SERVICE_MAXLEN + 1];
+ /*! IMSI or MSISDN to look up. */
+ struct osmo_mslookup_id id;
+
+ /*! Caller provided private data, if desired. */
+ void *priv;
+};
+
+/*! Result data as passed back to a lookup client that invoked an osmo_mslookup_client_request. */
+struct osmo_mslookup_result {
+ /*! Outcome of the request. */
+ enum osmo_mslookup_result_code rc;
+
+ /*! IP address and port to reach the given service via IPv4, if any. */
+ struct osmo_sockaddr_str host_v4;
+
+ /*! IP address and port to reach the given service via IPv6, if any. */
+ struct osmo_sockaddr_str host_v6;
+
+ /*! How long ago the service last verified presence of the subscriber, in seconds, or zero if the presence is
+ * invariable (like the home HLR record for an IMSI).
+ * If a subscriber has recently moved to a different location, we get multiple replies and want to choose the
+ * most recent one. If this were a timestamp, firstly the time zones would need to be taken care of.
+ * Even if we choose UTC, a service provider with an inaccurate date/time would end up affecting the result.
+ * The least susceptible to configuration errors or difference in local and remote clock is a value that
+ * indicates the actual age of the record in seconds. The time that the lookup query took to be answered should
+ * be neglectable here, since we would typically wait one second (or very few seconds) for lookup replies,
+ * while typical Location Updating periods are in the range of 15 minutes. */
+ uint32_t age;
+
+ /*! Whether this is the last result returned for this request. */
+ bool last;
+};
+
+int osmo_mslookup_query_from_domain_str(struct osmo_mslookup_query *q, const char *domain);
+
+size_t osmo_mslookup_id_name_buf(char *buf, size_t buflen, const struct osmo_mslookup_id *id);
+char *osmo_mslookup_id_name_c(void *ctx, const struct osmo_mslookup_id *id);
+char *osmo_mslookup_id_name_b(char *buf, size_t buflen, const struct osmo_mslookup_id *id);
+
+size_t osmo_mslookup_result_name_buf(char *buf, size_t buflen,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result);
+char *osmo_mslookup_result_name_c(void *ctx,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result);
+char *osmo_mslookup_result_name_b(char *buf, size_t buflen,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result);
diff --git a/include/osmocom/mslookup/mslookup_client.h b/include/osmocom/mslookup/mslookup_client.h
new file mode 100644
index 0000000..b749686
--- /dev/null
+++ b/include/osmocom/mslookup/mslookup_client.h
@@ -0,0 +1,127 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/mslookup/mslookup.h>
+
+struct osmo_mslookup_client;
+struct osmo_mslookup_result;
+
+typedef void (*osmo_mslookup_cb_t)(struct osmo_mslookup_client *client,
+ uint32_t request_handle,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result);
+
+/*! This part of a lookup request is not seen by the individual query method implementations. */
+struct osmo_mslookup_query_handling {
+ /*! Wait at least this long before returning any results.
+ *
+ * If nonzero, result_cb will be called as soon as this delay has elapsed, either with the so far youngest age
+ * result, or with a "not found yet" result. After this delay has elapsed, receiving results will continue
+ * until result_timeout_milliseconds has elapsed.
+ *
+ * If zero, responses are fed to the result_cb right from the start, every time a younger aged result than
+ * before comes in.
+ *
+ * If a result with age == 0 is received, min_delay_milliseconds is ignored, the result is returned immediately
+ * and listening for responses ends.
+ *
+ * Rationale: If a subscriber has recently moved between sites, multiple results will arrive, and the youngest
+ * age wins. It can make sense to wait a minimum time for responses before determining the winning result.
+ *
+ * However, if no result or no valid result has arrived within a short period, the subscriber may be at a site
+ * that is far away or that is currently experiencing high latency. It is thus a good safety net to still
+ * receive results for an extended period of time.
+ *
+ * For some services, it is possible to establish links to every received result, and whichever link succeeds
+ * will be used (for example for SIP calls: first to pick up the call gets connected, the others are dropped
+ * silently).
+ */
+ uint32_t min_delay_milliseconds;
+
+ /*! Total time in milliseconds to listen for lookup responses.
+ *
+ * When this timeout elapses, osmo_mslookup_client_request_cancel() is called implicitly; Manually invoking
+ * osmo_mslookup_client_request_cancel() after result_timeout_milliseconds has elapsed is not necessary, but is
+ * still safe to do anyway.
+ *
+ * If zero, min_delay_milliseconds is also used as result_timeout_milliseconds; if that is also zero, a default
+ * timeout value is used.
+ *
+ * If result_timeout_milliseconds <= min_delay_milliseconds, then min_delay_milliseconds is used as
+ * result_timeout_milliseconds, i.e. the timeout triggers as soon as min_delay_milliseconds hits.
+ *
+ * osmo_mslookup_client_request_cancel() can be called any time to end the request.
+ */
+ uint32_t result_timeout_milliseconds;
+
+ /*! Invoked every time a result with a younger age than the previous result has arrived.
+ * To stop receiving results before result_timeout_milliseconds has elapsed, call
+ * osmo_mslookup_client_request_cancel().
+ */
+ osmo_mslookup_cb_t result_cb;
+};
+
+uint32_t osmo_mslookup_client_request(struct osmo_mslookup_client *client,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_query_handling *handling);
+
+void osmo_mslookup_client_request_cancel(struct osmo_mslookup_client *client, uint32_t request_handle);
+
+struct osmo_mslookup_client *osmo_mslookup_client_new(void *ctx);
+bool osmo_mslookup_client_active(struct osmo_mslookup_client *client);
+void osmo_mslookup_client_free(struct osmo_mslookup_client *client);
+
+struct osmo_mslookup_client_method {
+ struct llist_head entry;
+
+ /*! Human readable name of this lookup method. */
+ const char *name;
+
+ /*! Private data for the lookup method implementation. */
+ void *priv;
+
+ /*! Backpointer to the client this method is added to. */
+ struct osmo_mslookup_client *client;
+
+ /*! Launch a lookup query. Called from osmo_mslookup_client_request().
+ * The implementation returns results by calling osmo_mslookup_client_rx_result(). */
+ void (*request)(struct osmo_mslookup_client_method *method,
+ const struct osmo_mslookup_query *query,
+ uint32_t request_handle);
+ /*! End a lookup query. Called from osmo_mslookup_client_request_cancel(). It is guaranteed to be called
+ * exactly once per above request() invocation. (The API user is required to invoke
+ * osmo_mslookup_client_request_cancel() exactly once per osmo_mslookup_client_request().) */
+ void (*request_cleanup)(struct osmo_mslookup_client_method *method,
+ uint32_t request_handle);
+
+ /*! The mslookup_client is removing this method, clean up all open requests, lists and allocations. */
+ void (*destruct)(struct osmo_mslookup_client_method *method);
+};
+
+void osmo_mslookup_client_method_add(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_client_method *method);
+bool osmo_mslookup_client_method_del(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_client_method *method);
+void osmo_mslookup_client_rx_result(struct osmo_mslookup_client *client, uint32_t request_handle,
+ const struct osmo_mslookup_result *result);
diff --git a/include/osmocom/mslookup/mslookup_client_fake.h b/include/osmocom/mslookup/mslookup_client_fake.h
new file mode 100644
index 0000000..65df42f
--- /dev/null
+++ b/include/osmocom/mslookup/mslookup_client_fake.h
@@ -0,0 +1,33 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+/*! MS lookup fake API for testing purposes. */
+#include <osmocom/mslookup/mslookup_client.h>
+
+struct osmo_mslookup_fake_response {
+ struct timeval time_to_reply;
+ struct osmo_mslookup_id for_id;
+ const char *for_service;
+ struct osmo_mslookup_result result;
+ bool sent;
+};
+
+struct osmo_mslookup_client_method *osmo_mslookup_client_add_fake(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_fake_response *responses,
+ size_t responses_len);
diff --git a/include/osmocom/mslookup/mslookup_client_mdns.h b/include/osmocom/mslookup/mslookup_client_mdns.h
new file mode 100644
index 0000000..bdefc13
--- /dev/null
+++ b/include/osmocom/mslookup/mslookup_client_mdns.h
@@ -0,0 +1,36 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+struct osmo_mslookup_client;
+struct osmo_mslookup_client_method;
+
+/*! MS Lookup mDNS server bind default IP. Taken from the Administratevly Scoped block, particularly the Organizational
+ * Scoped range, https://tools.ietf.org/html/rfc2365 . */
+#define OSMO_MSLOOKUP_MDNS_IP4 "239.192.23.42"
+#define OSMO_MSLOOKUP_MDNS_IP6 "ff08::23:42" // <-- TODO: sane?
+#define OSMO_MSLOOKUP_MDNS_PORT 4266
+
+struct osmo_mslookup_client_method *osmo_mslookup_client_add_mdns(struct osmo_mslookup_client *client, const char *ip,
+ uint16_t port, int initial_packet_id);
+
+const struct osmo_sockaddr_str *osmo_mslookup_client_method_mdns_get_bind_addr(struct osmo_mslookup_client_method *dns_method);
diff --git a/libosmo-mslookup.pc.in b/libosmo-mslookup.pc.in
new file mode 100644
index 0000000..25a873c
--- /dev/null
+++ b/libosmo-mslookup.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: Osmocom MS Lookup Library
+Description: C Utility Library
+Version: @VERSION@
+Libs: -L${libdir} @TALLOC_LIBS@ -losmogsm -losmo-mslookup -losmocore
+Cflags: -I${includedir}/
+
diff --git a/src/Makefile.am b/src/Makefile.am
index a5b71cf..f858ff0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,7 @@
-SUBDIRS = gsupclient
+SUBDIRS = \
+ gsupclient \
+ mslookup \
+ $(NULL)
AM_CFLAGS = \
-Wall \
diff --git a/src/mslookup/Makefile.am b/src/mslookup/Makefile.am
new file mode 100644
index 0000000..eb57efa
--- /dev/null
+++ b/src/mslookup/Makefile.am
@@ -0,0 +1,35 @@
+# 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_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include
+AM_CFLAGS = -fPIC -Wall $(PCSC_CFLAGS) $(TALLOC_CFLAGS)
+AM_LDFLAGS = $(COVERAGE_LDFLAGS)
+
+noinst_HEADERS = \
+ mdns_msg.h \
+ mdns_record.h \
+ mdns_rfc.h \
+ $(NULL)
+
+lib_LTLIBRARIES = libosmo-mslookup.la
+
+libosmo_mslookup_la_SOURCES = \
+ mdns.c \
+ mdns_msg.c \
+ mdns_record.c \
+ mdns_rfc.c \
+ mdns_sock.c \
+ mslookup.c \
+ mslookup_client.c \
+ mslookup_client_fake.c \
+ mslookup_client_mdns.c \
+ $(NULL)
+
+libosmo_mslookup_la_LDFLAGS = -version-info $(LIBVERSION)
+libosmo_mslookup_la_LIBADD = \
+ $(LIBOSMOCORE_LIBS) \
+ $(LIBOSMOGSM_LIBS) \
+ $(TALLOC_LIBS) \
+ $(NULL)
diff --git a/src/mslookup/mdns.c b/src/mslookup/mdns.c
new file mode 100644
index 0000000..ede4bcf
--- /dev/null
+++ b/src/mslookup/mdns.c
@@ -0,0 +1,394 @@
+/* mslookup specific functions for encoding and decoding mslookup queries/results into mDNS packets, using the high
+ * level functions from mdns_msg.c and mdns_record.c to build the request/answer messages. */
+
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <osmocom/hlr/logging.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/mslookup/mslookup.h>
+#include <errno.h>
+#include <inttypes.h>
+#include "mdns_msg.h"
+#include "mdns_record.h"
+
+static struct msgb *osmo_mdns_msgb_alloc(const char *label)
+{
+ return msgb_alloc(1024, __func__);
+}
+
+/*! Combine the mslookup query service, ID and ID type into a domain string.
+ * \returns allocated buffer with the resulting domain (i.e. "sip.voice.123.msisdn") on success,
+ * NULL on failure.
+ */
+static char *domain_from_query(void *ctx, const struct osmo_mslookup_query *query)
+{
+ const char *id;
+
+ /* Get id from query */
+ switch (query->id.type) {
+ case OSMO_MSLOOKUP_ID_IMSI:
+ id = query->id.imsi;
+ break;
+ case OSMO_MSLOOKUP_ID_MSISDN:
+ id = query->id.msisdn;
+ break;
+ default:
+ LOGP(DMSLOOKUP, LOGL_ERROR, "can't encode mslookup query id type %i", query->id.type);
+ return NULL;
+ }
+
+ return talloc_asprintf(ctx, "%s.%s.%s", query->service, id, osmo_mslookup_id_type_name(query->id.type));
+}
+
+/*! Encode a mslookup query into a mDNS packet.
+ * \returns msgb, or NULL on error.
+ */
+struct msgb *osmo_mdns_query_encode(void *ctx, uint16_t packet_id, const struct osmo_mslookup_query *query)
+{
+ struct osmo_mdns_msg_request req = {0};
+ struct msgb *msg = osmo_mdns_msgb_alloc(__func__);
+
+ req.id = packet_id;
+ req.type = OSMO_MDNS_RFC_RECORD_TYPE_ALL;
+ req.domain = domain_from_query(ctx, query);
+ if (!req.domain) {
+ msgb_free(msg);
+ return NULL;
+ }
+ if (osmo_mdns_msg_request_encode(ctx, msg, &req)) {
+ msgb_free(msg);
+ return NULL;
+ }
+ talloc_free(req.domain);
+ return msg;
+}
+
+/*! Decode a mDNS request packet into a mslookup query.
+ * \param[out] packet_id the result must be sent with the same packet_id.
+ * \returns allocated mslookup query on success,
+ * NULL on error.
+ */
+struct osmo_mslookup_query *osmo_mdns_query_decode(void *ctx, const uint8_t *data, size_t data_len,
+ uint16_t *packet_id)
+{
+ struct osmo_mdns_msg_request *req = NULL;
+ struct osmo_mslookup_query *query = NULL;
+
+ req = osmo_mdns_msg_request_decode(ctx, data, data_len);
+ if (!req)
+ return NULL;
+
+ query = talloc_zero(ctx, struct osmo_mslookup_query);
+ if (osmo_mslookup_query_from_domain_str(query, req->domain) < 0)
+ goto error;
+
+ *packet_id = req->id;
+ talloc_free(req);
+ return query;
+error:
+ if (req)
+ talloc_free(req);
+ if (query)
+ talloc_free(query);
+ return NULL;
+}
+
+/*! Parse sockaddr_str from mDNS record, so the mslookup result can be filled with it.
+ * \param[out] sockaddr_str resulting IPv4 or IPv6 sockaddr_str.
+ * \param[in] rec single record of the abstracted list of mDNS records
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+static int sockaddr_str_from_mdns_record(struct osmo_sockaddr_str *sockaddr_str, struct osmo_mdns_record *rec)
+{
+ switch (rec->type) {
+ case OSMO_MDNS_RFC_RECORD_TYPE_A:
+ if (rec->length != 4) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "unexpected length of A record\n");
+ return -EINVAL;
+ }
+ osmo_sockaddr_str_from_32(sockaddr_str, *(uint32_t *)rec->data, 0);
+ break;
+ case OSMO_MDNS_RFC_RECORD_TYPE_AAAA:
+ if (rec->length != 16) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "unexpected length of AAAA record\n");
+ return -EINVAL;
+ }
+ osmo_sockaddr_str_from_in6_addr(sockaddr_str, (struct in6_addr*)rec->data, 0);
+ break;
+ default:
+ LOGP(DMSLOOKUP, LOGL_ERROR, "unexpected record type\n");
+ return -EINVAL;
+ }
+ return 0;
+}
+
+/*! Encode a successful mslookup result, along with the original query and packet_id into one mDNS answer packet.
+ *
+ * The records in the packet are ordered as follows:
+ * 1) "age", ip_v4/v6, "port" (only IPv4 or IPv6 present) or
+ * 2) "age", ip_v4, "port", ip_v6, "port" (both IPv4 and v6 present).
+ * "age" and "port" are TXT records, ip_v4 is an A record, ip_v6 is an AAAA record.
+ *
+ * \param[in] packet_id as received in osmo_mdns_query_decode().
+ * \param[in] query the original query, so we can send the domain back in the answer (i.e. "sip.voice.1234.msisdn").
+ * \param[in] result holds the age, IPs and ports of the queried service.
+ * \returns msg on success, NULL on error.
+ */
+struct msgb *osmo_mdns_result_encode(void *ctx, uint16_t packet_id, const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result)
+{
+ struct osmo_mdns_msg_answer ans = {};
+ struct osmo_mdns_record *rec_age = NULL;
+ struct osmo_mdns_record rec_ip_v4 = {0};
+ struct osmo_mdns_record rec_ip_v6 = {0};
+ struct osmo_mdns_record *rec_ip_v4_port = NULL;
+ struct osmo_mdns_record *rec_ip_v6_port = NULL;
+ struct in_addr rec_ip_v4_in;
+ struct in6_addr rec_ip_v6_in;
+ struct msgb *msg = osmo_mdns_msgb_alloc(__func__);
+ char buf[256];
+
+ ctx = talloc_named(ctx, 0, "osmo_mdns_result_encode");
+
+ /* Prepare answer (ans) */
+ ans.domain = domain_from_query(ctx, query);
+ if (!ans.domain)
+ goto error;
+ ans.id = packet_id;
+ INIT_LLIST_HEAD(&ans.records);
+
+ /* Record for age */
+ rec_age = osmo_mdns_record_txt_keyval_encode(ctx, "age", "%"PRIu32, result->age);
+ OSMO_ASSERT(rec_age);
+ llist_add_tail(&rec_age->list, &ans.records);
+
+ /* Records for IPv4 */
+ if (osmo_sockaddr_str_is_set(&result->host_v4)) {
+ if (osmo_sockaddr_str_to_in_addr(&result->host_v4, &rec_ip_v4_in) < 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "failed to encode ipv4: %s\n",
+ osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
+ goto error;
+ }
+ rec_ip_v4.type = OSMO_MDNS_RFC_RECORD_TYPE_A;
+ rec_ip_v4.data = (uint8_t *)&rec_ip_v4_in;
+ rec_ip_v4.length = sizeof(rec_ip_v4_in);
+ llist_add_tail(&rec_ip_v4.list, &ans.records);
+
+ rec_ip_v4_port = osmo_mdns_record_txt_keyval_encode(ctx, "port", "%"PRIu16, result->host_v4.port);
+ OSMO_ASSERT(rec_ip_v4_port);
+ llist_add_tail(&rec_ip_v4_port->list, &ans.records);
+ }
+
+ /* Records for IPv6 */
+ if (osmo_sockaddr_str_is_set(&result->host_v6)) {
+ if (osmo_sockaddr_str_to_in6_addr(&result->host_v6, &rec_ip_v6_in) < 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "failed to encode ipv6: %s\n",
+ osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
+ goto error;
+ }
+ rec_ip_v6.type = OSMO_MDNS_RFC_RECORD_TYPE_AAAA;
+ rec_ip_v6.data = (uint8_t *)&rec_ip_v6_in;
+ rec_ip_v6.length = sizeof(rec_ip_v6_in);
+ llist_add_tail(&rec_ip_v6.list, &ans.records);
+
+ rec_ip_v6_port = osmo_mdns_record_txt_keyval_encode(ctx, "port", "%"PRIu16, result->host_v6.port);
+ OSMO_ASSERT(rec_ip_v6_port);
+ llist_add_tail(&rec_ip_v6_port->list, &ans.records);
+ }
+
+ if (osmo_mdns_msg_answer_encode(ctx, msg, &ans)) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "failed to encode mDNS answer: %s\n",
+ osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
+ goto error;
+ }
+ talloc_free(ctx);
+ return msg;
+error:
+ msgb_free(msg);
+ talloc_free(ctx);
+ return NULL;
+}
+
+static int decode_uint32_t(const char *str, uint32_t *val)
+{
+ long long int lld;
+ char *endptr = NULL;
+ *val = 0;
+ errno = 0;
+ lld = strtoll(str, &endptr, 10);
+ if (errno || !endptr || *endptr)
+ return -EINVAL;
+ if (lld < 0 || lld > UINT32_MAX)
+ return -EINVAL;
+ *val = lld;
+ return 0;
+}
+
+static int decode_port(const char *str, uint16_t *port)
+{
+ uint32_t val;
+ if (decode_uint32_t(str, &val))
+ return -EINVAL;
+ if (val > 65535)
+ return -EINVAL;
+ *port = val;
+ return 0;
+}
+
+/*! Read expected mDNS records into mslookup result.
+ *
+ * The records in the packet must be ordered as follows:
+ * 1) "age", ip_v4/v6, "port" (only IPv4 or IPv6 present) or
+ * 2) "age", ip_v4, "port", ip_v6, "port" (both IPv4 and v6 present).
+ * "age" and "port" are TXT records, ip_v4 is an A record, ip_v6 is an AAAA record.
+ *
+ * \param[out] result holds the age, IPs and ports of the queried service.
+ * \param[in] ans abstracted mDNS answer with a list of resource records.
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+int osmo_mdns_result_from_answer(struct osmo_mslookup_result *result, const struct osmo_mdns_msg_answer *ans)
+{
+ struct osmo_mdns_record *rec;
+ char txt_key[64];
+ char txt_value[64];
+ bool found_age = false;
+ bool found_ip_v4 = false;
+ bool found_ip_v6 = false;
+ struct osmo_sockaddr_str *expect_port_for = NULL;
+
+ result->rc = OSMO_MSLOOKUP_RC_NONE;
+
+ llist_for_each_entry(rec, &ans->records, list) {
+ switch (rec->type) {
+ case OSMO_MDNS_RFC_RECORD_TYPE_A:
+ if (expect_port_for) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "'A' record found, but still expecting a 'port' value first\n");
+ return -EINVAL;
+ }
+ if (found_ip_v4) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "'A' record found twice in mDNS answer\n");
+ return -EINVAL;
+ }
+ found_ip_v4 = true;
+ expect_port_for = &result->host_v4;
+ if (sockaddr_str_from_mdns_record(expect_port_for, rec)) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "'A' record with invalid address data\n");
+ return -EINVAL;
+ }
+ break;
+ case OSMO_MDNS_RFC_RECORD_TYPE_AAAA:
+ if (expect_port_for) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "'AAAA' record found, but still expecting a 'port' value first\n");
+ return -EINVAL;
+ }
+ if (found_ip_v6) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "'AAAA' record found twice in mDNS answer\n");
+ return -EINVAL;
+ }
+ found_ip_v6 = true;
+ expect_port_for = &result->host_v6;
+ if (sockaddr_str_from_mdns_record(expect_port_for, rec) != 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "'AAAA' record with invalid address data\n");
+ return -EINVAL;
+ }
+ break;
+ case OSMO_MDNS_RFC_RECORD_TYPE_TXT:
+ if (osmo_mdns_record_txt_keyval_decode(rec, txt_key, sizeof(txt_key),
+ txt_value, sizeof(txt_value)) != 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "failed to decode txt record\n");
+ return -EINVAL;
+ }
+ if (strcmp(txt_key, "age") == 0) {
+ if (found_age) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "duplicate 'TXT' record for 'age'\n");
+ return -EINVAL;
+ }
+ found_age = true;
+ if (decode_uint32_t(txt_value, &result->age)) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "'TXT' record: invalid 'age' value ('age=%s')\n", txt_value);
+ return -EINVAL;
+ }
+ } else if (strcmp(txt_key, "port") == 0) {
+ if (!expect_port_for) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "'TXT' record for 'port' without previous 'A' or 'AAAA' record\n");
+ return -EINVAL;
+ }
+ if (decode_port(txt_value, &expect_port_for->port)) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "'TXT' record: invalid 'port' value ('port=%s')\n", txt_value);
+ return -EINVAL;
+ }
+ expect_port_for = NULL;
+ } else {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "unexpected key '%s' in TXT record\n", txt_key);
+ return -EINVAL;
+ }
+ break;
+ default:
+ LOGP(DMSLOOKUP, LOGL_ERROR, "unexpected record type\n");
+ return -EINVAL;
+ }
+ }
+
+ /* Check if everything was found */
+ if (!found_age || !(found_ip_v4 || found_ip_v6) || expect_port_for) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "missing resource records in mDNS answer\n");
+ return -EINVAL;
+ }
+
+ result->rc = OSMO_MSLOOKUP_RC_RESULT;
+ return 0;
+}
+
+/*! Decode a mDNS answer packet into a mslookup result, query and packet_id.
+ * \param[out] packet_id same ID as sent in the request packet.
+ * \param[out] query the original query (service, ID, ID type).
+ * \param[out] result holds the age, IPs and ports of the queried service.
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+int osmo_mdns_result_decode(void *ctx, const uint8_t *data, size_t data_len, uint16_t *packet_id,
+ struct osmo_mslookup_query *query, struct osmo_mslookup_result *result)
+{
+ int rc = -EINVAL;
+ struct osmo_mdns_msg_answer *ans;
+ ans = osmo_mdns_msg_answer_decode(ctx, data, data_len);
+ if (!ans)
+ goto exit_free;
+
+ if (osmo_mslookup_query_from_domain_str(query, ans->domain) < 0)
+ goto exit_free;
+
+ if (osmo_mdns_result_from_answer(result, ans) < 0)
+ goto exit_free;
+
+ *packet_id = ans->id;
+ rc = 0;
+exit_free:
+ if (ans)
+ talloc_free(ans);
+ return rc;
+}
diff --git a/src/mslookup/mdns_msg.c b/src/mslookup/mdns_msg.c
new file mode 100644
index 0000000..ef94c04
--- /dev/null
+++ b/src/mslookup/mdns_msg.c
@@ -0,0 +1,186 @@
+/* High level mDNS encoding and decoding functions for whole messages:
+ * Request message (header, question)
+ * Answer message (header, resource record 1, ... resource record N)*/
+
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 "mdns_msg.h"
+#include "mdns_record.h"
+#include <errno.h>
+#include <string.h>
+#include <osmocom/hlr/logging.h>
+
+/*! Encode request message into one mDNS packet, consisting of the header section and one question section.
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+int osmo_mdns_msg_request_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_msg_request *req)
+{
+ struct osmo_mdns_rfc_header hdr = {0};
+ struct osmo_mdns_rfc_question qst = {0};
+
+ hdr.id = req->id;
+ hdr.qdcount = 1;
+ osmo_mdns_rfc_header_encode(msg, &hdr);
+
+ qst.domain = req->domain;
+ qst.qtype = req->type;
+ qst.qclass = OSMO_MDNS_RFC_CLASS_IN;
+ if (osmo_mdns_rfc_question_encode(ctx, msg, &qst) != 0)
+ return -EINVAL;
+
+ return 0;
+}
+
+/*! Decode request message from a mDNS packet, consisting of the header section and one question section.
+ * \returns allocated request message on success,
+ * NULL on error.
+ */
+struct osmo_mdns_msg_request *osmo_mdns_msg_request_decode(void *ctx, const uint8_t *data, size_t data_len)
+{
+ struct osmo_mdns_rfc_header hdr = {0};
+ size_t hdr_len = sizeof(struct osmo_mdns_rfc_header);
+ struct osmo_mdns_rfc_question* qst = NULL;
+ struct osmo_mdns_msg_request *ret = NULL;
+
+ if (data_len < hdr_len || osmo_mdns_rfc_header_decode(data, hdr_len, &hdr) != 0 || hdr.qr != 0)
+ return NULL;
+
+ qst = osmo_mdns_rfc_question_decode(ctx, data + hdr_len, data_len - hdr_len);
+ if (!qst)
+ return NULL;
+
+ ret = talloc(ctx, struct osmo_mdns_msg_request);
+ ret->id = hdr.id;
+ ret->domain = talloc_strdup(ret, qst->domain);
+ ret->type = qst->qtype;
+
+ talloc_free(qst);
+ return ret;
+}
+
+/*! Initialize the linked list for resource records in a answer message. */
+void osmo_mdns_msg_answer_init(struct osmo_mdns_msg_answer *ans)
+{
+ *ans = (struct osmo_mdns_msg_answer){};
+ INIT_LLIST_HEAD(&ans->records);
+}
+
+/*! Encode answer message into one mDNS packet, consisting of the header section and N resource records.
+ *
+ * To keep things simple, this sends the domain with each resource record. Other DNS implementations make use of
+ * "message compression", which would send a question section with the domain before the resource records, and then
+ * point inside each resource record with an offset back to the domain in the question section (RFC 1035 4.1.4).
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+int osmo_mdns_msg_answer_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_msg_answer *ans)
+{
+ struct osmo_mdns_rfc_header hdr = {0};
+ struct osmo_mdns_record *ans_record;
+
+ hdr.id = ans->id;
+ hdr.qr = 1;
+ hdr.ancount = llist_count(&ans->records);
+ osmo_mdns_rfc_header_encode(msg, &hdr);
+
+ llist_for_each_entry(ans_record, &ans->records, list) {
+ struct osmo_mdns_rfc_record rec = {0};
+
+ rec.domain = ans->domain;
+ rec.type = ans_record->type;
+ rec.class = OSMO_MDNS_RFC_CLASS_IN;
+ rec.ttl = 0;
+ rec.rdlength = ans_record->length;
+ rec.rdata = ans_record->data;
+
+ if (osmo_mdns_rfc_record_encode(ctx, msg, &rec) != 0)
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/*! Decode answer message from a mDNS packet.
+ *
+ * Answer messages must consist of one header and one or more resource records. An additional question section or
+ * message compression (RFC 1035 4.1.4) are not supported.
+* \returns allocated answer message on success,
+ * NULL on error.
+ */
+struct osmo_mdns_msg_answer *osmo_mdns_msg_answer_decode(void *ctx, const uint8_t *data, size_t data_len)
+{
+ struct osmo_mdns_rfc_header hdr = {};
+ size_t hdr_len = sizeof(struct osmo_mdns_rfc_header);
+ struct osmo_mdns_msg_answer *ret = talloc_zero(ctx, struct osmo_mdns_msg_answer);
+
+ /* Parse header section */
+ if (data_len < hdr_len || osmo_mdns_rfc_header_decode(data, hdr_len, &hdr) != 0 || hdr.qr != 1)
+ goto error;
+ ret->id = hdr.id;
+ data_len -= hdr_len;
+ data += hdr_len;
+
+ /* Parse resource records */
+ INIT_LLIST_HEAD(&ret->records);
+ while (data_len) {
+ size_t record_len;
+ struct osmo_mdns_rfc_record *rec;
+ struct osmo_mdns_record* ret_record;
+
+ rec = osmo_mdns_rfc_record_decode(ret, data, data_len, &record_len);
+ if (!rec)
+ goto error;
+
+ /* Copy domain to ret */
+ if (ret->domain) {
+ if (strcmp(ret->domain, rec->domain) != 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "domain mismatch in resource records ('%s' vs '%s')\n",
+ ret->domain, rec->domain);
+ goto error;
+ }
+ }
+ else
+ ret->domain = talloc_strdup(ret, rec->domain);
+
+ /* Add simplified record to ret */
+ ret_record = talloc(ret, struct osmo_mdns_record);
+ ret_record->type = rec->type;
+ ret_record->length = rec->rdlength;
+ ret_record->data = talloc_memdup(ret_record, rec->rdata, rec->rdlength);
+ llist_add_tail(&ret_record->list, &ret->records);
+
+ data += record_len;
+ data_len -= record_len;
+ talloc_free(rec);
+ }
+
+ /* Verify record count */
+ if (llist_count(&ret->records) != hdr.ancount) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "amount of parsed records (%i) doesn't match count in header (%i)\n",
+ llist_count(&ret->records), hdr.ancount);
+ goto error;
+ }
+
+ return ret;
+error:
+ talloc_free(ret);
+ return NULL;
+}
diff --git a/src/mslookup/mdns_msg.h b/src/mslookup/mdns_msg.h
new file mode 100644
index 0000000..e4ab13f
--- /dev/null
+++ b/src/mslookup/mdns_msg.h
@@ -0,0 +1,43 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include "mdns_rfc.h"
+
+struct osmo_mdns_msg_request {
+ uint16_t id;
+ char *domain;
+ enum osmo_mdns_rfc_record_type type;
+};
+
+struct osmo_mdns_msg_answer {
+ uint16_t id;
+ char *domain;
+ struct llist_head records;
+};
+
+int osmo_mdns_msg_request_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_msg_request *req);
+struct osmo_mdns_msg_request *osmo_mdns_msg_request_decode(void *ctx, const uint8_t *data, size_t data_len);
+
+void osmo_mdns_msg_answer_init(struct osmo_mdns_msg_answer *answer);
+int osmo_mdns_msg_answer_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_msg_answer *ans);
+struct osmo_mdns_msg_answer *osmo_mdns_msg_answer_decode(void *ctx, const uint8_t *data, size_t data_len);
+int osmo_mdns_result_from_answer(struct osmo_mslookup_result *result, const struct osmo_mdns_msg_answer *ans);
diff --git a/src/mslookup/mdns_record.c b/src/mslookup/mdns_record.c
new file mode 100644
index 0000000..c9b6e4c
--- /dev/null
+++ b/src/mslookup/mdns_record.c
@@ -0,0 +1,104 @@
+/* High level mDNS functions for specific resource records, in particular for handling "key=value" TXT records. */
+
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <errno.h>
+#include <talloc.h>
+#include "mdns_record.h"
+
+/*! Get a TXT resource record, which stores a key=value string.
+ * \returns allocated resource record on success,
+ * NULL on error.
+ */
+static struct osmo_mdns_record *_osmo_mdns_record_txt_encode(void *ctx, const char *key, const char *value)
+{
+ struct osmo_mdns_record *ret = talloc_zero(ctx, struct osmo_mdns_record);
+ size_t len = strlen(key) + 1 + strlen(value);
+
+ ret->data = (uint8_t *)talloc_asprintf(ctx, "%c%s=%s", (char)len, key, value);
+ if (!ret->data)
+ return NULL;
+ ret->type = OSMO_MDNS_RFC_RECORD_TYPE_TXT;
+ ret->length = len + 1;
+ return ret;
+}
+
+/*! Get a TXT resource record, which stores a key=value string, but build value from a format string.
+ * \returns allocated resource record on success,
+ * NULL on error.
+ */
+struct osmo_mdns_record *osmo_mdns_record_txt_keyval_encode(void *ctx, const char *key, const char *value_fmt, ...)
+{
+ va_list ap;
+ char *value = NULL;
+ struct osmo_mdns_record *r;
+
+ if (!value_fmt)
+ return _osmo_mdns_record_txt_encode(ctx, key, "");
+
+ va_start(ap, value_fmt);
+ value = talloc_vasprintf(ctx, value_fmt, ap);
+ if (!value)
+ return NULL;
+ va_end(ap);
+ r = _osmo_mdns_record_txt_encode(ctx, key, value);
+ talloc_free(value);
+ return r;
+}
+
+/*! Decode a TXT resource record, which stores a key=value string.
+ * \returns 0 on success,
+ * -EINVAL on error.
+ */
+int osmo_mdns_record_txt_keyval_decode(const struct osmo_mdns_record *rec,
+ char *key_buf, size_t key_size, char *value_buf, size_t value_size)
+{
+ const char *key_value;
+ const char *key_value_end;
+ const char *sep;
+ const char *value;
+
+ if (rec->type != OSMO_MDNS_RFC_RECORD_TYPE_TXT)
+ return -EINVAL;
+
+ key_value = (const char *)rec->data;
+ key_value_end = key_value + rec->length;
+
+ /* Verify and then skip the redundant string length byte */
+ if (*key_value != rec->length - 1)
+ return -EINVAL;
+ key_value++;
+
+ if (key_value >= key_value_end)
+ return -EINVAL;
+
+ /* Find equals sign */
+ sep = osmo_strnchr(key_value, key_value_end - key_value, '=');
+ if (!sep)
+ return -EINVAL;
+
+ /* Parse key */
+ osmo_token_copy(key_buf, key_size, key_value, sep - key_value);
+
+ /* Parse value */
+ value = sep + 1;
+ osmo_token_copy(value_buf, value_size, value, key_value_end - value);
+ return 0;
+}
diff --git a/src/mslookup/mdns_record.h b/src/mslookup/mdns_record.h
new file mode 100644
index 0000000..d5247c5
--- /dev/null
+++ b/src/mslookup/mdns_record.h
@@ -0,0 +1,35 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include "mdns_rfc.h"
+#include <osmocom/core/linuxlist.h>
+#include <stdint.h>
+
+struct osmo_mdns_record {
+ struct llist_head list;
+ enum osmo_mdns_rfc_record_type type;
+ uint16_t length;
+ uint8_t *data;
+};
+
+struct osmo_mdns_record *osmo_mdns_record_txt_keyval_encode(void *ctx, const char *key, const char *value_fmt, ...);
+int osmo_mdns_record_txt_keyval_decode(const struct osmo_mdns_record *rec,
+ char *key_buf, size_t key_size, char *value_buf, size_t value_size);
diff --git a/src/mslookup/mdns_rfc.c b/src/mslookup/mdns_rfc.c
new file mode 100644
index 0000000..199d5f1
--- /dev/null
+++ b/src/mslookup/mdns_rfc.c
@@ -0,0 +1,261 @@
+/* Low level mDNS encoding and decoding functions of the qname IE, header/question sections and resource records,
+ * as described in these RFCs:
+ * - RFC 1035 (Domain names - implementation and specification)
+ * - RFC 3596 (DNS Extensions to Support IP Version 6) */
+
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <ctype.h>
+#include <errno.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/bitvec.h>
+#include <osmocom/core/logging.h>
+#include "mdns_rfc.h"
+
+/*
+ * Encode/decode IEs
+ */
+
+/*! Encode a domain string as qname (RFC 1035 4.1.2).
+ * \param[in] domain multiple labels separated by dots, e.g. "sip.voice.1234.msisdn".
+ * \returns allocated buffer with length-value pairs for each label (e.g. 0x03 "sip" 0x05 "voice" ...),
+ * NULL on error.
+ */
+char *osmo_mdns_rfc_qname_encode(void *ctx, const char *domain)
+{
+ char *domain_dup;
+ char *domain_iter;
+ char buf[OSMO_MDNS_RFC_MAX_NAME_LEN + 2] = ""; /* len(qname) is len(domain) +1 */
+ struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
+ char *label;
+
+ if (strlen(domain) > OSMO_MDNS_RFC_MAX_NAME_LEN)
+ return NULL;
+
+ domain_iter = domain_dup = talloc_strdup(ctx, domain);
+ while ((label = strsep(&domain_iter, "."))) {
+ size_t len = strlen(label);
+
+ /* Empty domain, dot at start, two dots in a row, or ending with a dot */
+ if (!len)
+ goto error;
+
+ OSMO_STRBUF_PRINTF(sb, "%c%s", (char)len, label);
+ }
+
+ talloc_free(domain_dup);
+ return talloc_strdup(ctx, buf);
+
+error:
+ talloc_free(domain_dup);
+ return NULL;
+}
+
+/*! Decode a domain string from a qname (RFC 1035 4.1.2).
+ * \param[in] qname buffer with length-value pairs for each label (e.g. 0x03 "sip" 0x05 "voice" ...)
+ * \param[in] qname_max_len amount of bytes that can be read at most from the memory location that qname points to.
+ * \returns allocated buffer with domain string, multiple labels separated by dots (e.g. "sip.voice.1234.msisdn"),
+ * NULL on error.
+ */
+char *osmo_mdns_rfc_qname_decode(void *ctx, const char *qname, size_t qname_max_len)
+{
+ const char *next_label, *qname_end = qname + qname_max_len;
+ char buf[OSMO_MDNS_RFC_MAX_NAME_LEN + 1];
+ int i = 0;
+
+ if (qname_max_len < 1)
+ return NULL;
+
+ while (*qname) {
+ size_t len = *qname;
+ next_label = qname + len + 1;
+
+ if (next_label >= qname_end || i + len > OSMO_MDNS_RFC_MAX_NAME_LEN)
+ return NULL;
+
+ if (i) {
+ /* Two dots in a row is not allowed */
+ if (buf[i - 1] == '.')
+ return NULL;
+
+ buf[i] = '.';
+ i++;
+ }
+
+ memcpy(buf + i, qname + 1, len);
+ i += len;
+ qname = next_label;
+ }
+ buf[i] = '\0';
+
+ return talloc_strdup(ctx, buf);
+}
+
+/*
+ * Encode/decode message sections
+ */
+
+/*! Encode header section (RFC 1035 4.1.1). */
+void osmo_mdns_rfc_header_encode(struct msgb *msg, const struct osmo_mdns_rfc_header *hdr)
+{
+ struct osmo_mdns_rfc_header *buf = (struct osmo_mdns_rfc_header *) msgb_put(msg, sizeof(*hdr));
+ memcpy(buf, hdr, sizeof(*hdr));
+
+ osmo_store16be(buf->id, &buf->id);
+ osmo_store16be(buf->qdcount, &buf->qdcount);
+ osmo_store16be(buf->ancount, &buf->ancount);
+ osmo_store16be(buf->nscount, &buf->nscount);
+ osmo_store16be(buf->arcount, &buf->arcount);
+}
+
+/*! Decode header section (RFC 1035 4.1.1). */
+int osmo_mdns_rfc_header_decode(const uint8_t *data, size_t data_len, struct osmo_mdns_rfc_header *hdr)
+{
+ if (data_len != sizeof(*hdr))
+ return -EINVAL;
+
+ memcpy(hdr, data, data_len);
+
+ hdr->id = osmo_load16be(&hdr->id);
+ hdr->qdcount = osmo_load16be(&hdr->qdcount);
+ hdr->ancount = osmo_load16be(&hdr->ancount);
+ hdr->nscount = osmo_load16be(&hdr->nscount);
+ hdr->arcount = osmo_load16be(&hdr->arcount);
+
+ return 0;
+}
+
+/*! Encode question section (RFC 1035 4.1.2). */
+int osmo_mdns_rfc_question_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_question *qst)
+{
+ char *qname;
+ size_t qname_len;
+ uint8_t *qname_buf;
+
+ /* qname */
+ qname = osmo_mdns_rfc_qname_encode(ctx, qst->domain);
+ if (!qname)
+ return -EINVAL;
+ qname_len = strlen(qname) + 1;
+ qname_buf = msgb_put(msg, qname_len);
+ memcpy(qname_buf, qname, qname_len);
+ talloc_free(qname);
+
+ /* qtype and qclass */
+ msgb_put_u16(msg, qst->qtype);
+ msgb_put_u16(msg, qst->qclass);
+
+ return 0;
+}
+
+/*! Decode question section (RFC 1035 4.1.2). */
+struct osmo_mdns_rfc_question *osmo_mdns_rfc_question_decode(void *ctx, const uint8_t *data, size_t data_len)
+{
+ struct osmo_mdns_rfc_question *ret;
+ size_t qname_len = data_len - 4;
+
+ if (data_len < 6)
+ return NULL;
+
+ /* qname */
+ ret = talloc_zero(ctx, struct osmo_mdns_rfc_question);
+ if (!ret)
+ return NULL;
+ ret->domain = osmo_mdns_rfc_qname_decode(ret, (const char *)data, qname_len);
+ if (!ret->domain) {
+ talloc_free(ret);
+ return NULL;
+ }
+
+ /* qtype and qclass */
+ ret->qtype = osmo_load16be(data + qname_len);
+ ret->qclass = osmo_load16be(data + qname_len + 2);
+
+ return ret;
+}
+
+/*
+ * Encode/decode resource records
+ */
+
+/*! Encode one resource record (RFC 1035 4.1.3). */
+int osmo_mdns_rfc_record_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_record *rec)
+{
+ char *name;
+ size_t name_len;
+ uint8_t *buf;
+
+ /* name */
+ name = osmo_mdns_rfc_qname_encode(ctx, rec->domain);
+ if (!name)
+ return -EINVAL;
+ name_len = strlen(name) + 1;
+ buf = msgb_put(msg, name_len);
+ memcpy(buf, name, name_len);
+ talloc_free(name);
+
+ /* type, class, ttl, rdlength */
+ msgb_put_u16(msg, rec->type);
+ msgb_put_u16(msg, rec->class);
+ msgb_put_u32(msg, rec->ttl);
+ msgb_put_u16(msg, rec->rdlength);
+
+ /* rdata */
+ buf = msgb_put(msg, rec->rdlength);
+ memcpy(buf, rec->rdata, rec->rdlength);
+ return 0;
+}
+
+/*! Decode one resource record (RFC 1035 4.1.3). */
+struct osmo_mdns_rfc_record *osmo_mdns_rfc_record_decode(void *ctx, const uint8_t *data, size_t data_len,
+ size_t *record_len)
+{
+ struct osmo_mdns_rfc_record *ret = talloc_zero(ctx, struct osmo_mdns_rfc_record);
+ size_t name_len;
+
+ /* name */
+ ret->domain = osmo_mdns_rfc_qname_decode(ret, (const char *)data, data_len - 10);
+ if (!ret->domain)
+ goto error;
+ name_len = strlen(ret->domain) + 2;
+ if (name_len + 10 > data_len)
+ goto error;
+
+ /* type, class, ttl, rdlength */
+ ret->type = osmo_load16be(data + name_len);
+ ret->class = osmo_load16be(data + name_len + 2);
+ ret->ttl = osmo_load32be(data + name_len + 4);
+ ret->rdlength = osmo_load16be(data + name_len + 8);
+ if (name_len + 10 + ret->rdlength > data_len)
+ goto error;
+
+ /* rdata */
+ ret->rdata = talloc_memdup(ret, data + name_len + 10, ret->rdlength);
+ if (!ret->rdata)
+ return NULL;
+
+ *record_len = name_len + 10 + ret->rdlength;
+ return ret;
+error:
+ talloc_free(ret);
+ return NULL;
+}
+
diff --git a/src/mslookup/mdns_rfc.h b/src/mslookup/mdns_rfc.h
new file mode 100644
index 0000000..257bcca
--- /dev/null
+++ b/src/mslookup/mdns_rfc.h
@@ -0,0 +1,111 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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/>.
+ *
+ */
+
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/endian.h>
+#include <osmocom/mslookup/mdns.h>
+
+/* RFC 1035 2.3.4 */
+#define OSMO_MDNS_RFC_MAX_NAME_LEN 255
+
+enum osmo_mdns_rfc_record_type {
+ OSMO_MDNS_RFC_RECORD_TYPE_UNKNOWN = 0,
+
+ /* RFC 1035 3.2.2 */
+ OSMO_MDNS_RFC_RECORD_TYPE_A = 1, /* IPv4 address */
+ OSMO_MDNS_RFC_RECORD_TYPE_TXT = 16, /* Text strings */
+
+ /* RFC 3596 2.1 */
+ OSMO_MDNS_RFC_RECORD_TYPE_AAAA = 28, /* IPv6 address */
+
+ /* RFC 1035 3.2.3 */
+ OSMO_MDNS_RFC_RECORD_TYPE_ALL = 255, /* Request only: ask for all */
+};
+
+enum osmo_mdns_rfc_class {
+ OSMO_MDNS_RFC_CLASS_UNKNOWN = 0,
+
+ /* RFC 1035 3.2.4 */
+ OSMO_MDNS_RFC_CLASS_IN = 1, /* Internet and IP networks */
+
+ /* RFC 1035 3.2.5 */
+ OSMO_MDNS_RFC_CLASS_ALL = 255, /* Request only: ask for all */
+};
+
+/* RFC 1035 4.1.1 */
+struct osmo_mdns_rfc_header {
+#if OSMO_IS_LITTLE_ENDIAN
+ uint16_t id;
+ uint8_t rd:1,
+ tc:1,
+ aa:1,
+ opcode:4,
+ qr:1; /* QR (0: query, 1: response) */
+ uint8_t rcode:4,
+ z:3,
+ ra:1;
+ uint16_t qdcount; /* Number of questions */
+ uint16_t ancount; /* Number of answers */
+ uint16_t nscount; /* Number of authority records */
+ uint16_t arcount; /* Number of additional records */
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+ uint16_t id;
+ uint8_t qr:1, opcode:4, aa:1, tc:1, rd:1;
+ uint8_t ra:1, z:3, rcode:4;
+ uint16_t qdcount;
+ uint16_t ancount;
+ uint16_t nscount;
+ uint16_t arcount;
+#endif
+} __attribute__ ((packed));
+
+/* RFC 1035 4.1.2 */
+struct osmo_mdns_rfc_question {
+ char *domain; /* Domain to be encoded as qname (e.g. "hlr.1234567.imsi") */
+ enum osmo_mdns_rfc_record_type qtype;
+ enum osmo_mdns_rfc_class qclass;
+};
+
+/* RFC 1035 4.1.3 */
+struct osmo_mdns_rfc_record {
+ char *domain; /* Domain to be encoded as name (e.g. "hlr.1234567.imsi") */
+ enum osmo_mdns_rfc_record_type type;
+ enum osmo_mdns_rfc_class class;
+ uint32_t ttl;
+ uint16_t rdlength;
+ uint8_t *rdata;
+};
+
+char *osmo_mdns_rfc_qname_encode(void *ctx, const char *domain);
+char *osmo_mdns_rfc_qname_decode(void *ctx, const char *qname, size_t qname_len);
+
+void osmo_mdns_rfc_header_encode(struct msgb *msg, const struct osmo_mdns_rfc_header *hdr);
+int osmo_mdns_rfc_header_decode(const uint8_t *data, size_t data_len, struct osmo_mdns_rfc_header *hdr);
+
+int osmo_mdns_rfc_question_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_question *qst);
+struct osmo_mdns_rfc_question *osmo_mdns_rfc_question_decode(void *ctx, const uint8_t *data, size_t data_len);
+
+int osmo_mdns_rfc_record_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_record *rec);
+struct osmo_mdns_rfc_record *osmo_mdns_rfc_record_decode(void *ctx, const uint8_t *data, size_t data_len,
+ size_t *record_len);
diff --git a/src/mslookup/mdns_sock.c b/src/mslookup/mdns_sock.c
new file mode 100644
index 0000000..e45dfe2
--- /dev/null
+++ b/src/mslookup/mdns_sock.c
@@ -0,0 +1,146 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdbool.h>
+#include <talloc.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/select.h>
+#include <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mdns_sock.h>
+
+/*! Open socket to send and receive multicast data.
+ *
+ * The socket is opened with SO_REUSEADDR, so we can bind to the same IP and port multiple times. Note that the callback
+ * will not only be called when someone else is sending data, but also for data that was sent from this osmo_mdns_sock.
+ *
+ * \param[in] ip multicast IPv4 or IPv6 address.
+ * \param[in] port port number.
+ * \param[in] cb callback for incoming data (should read from osmo_fd->fd).
+ * \param[in] data userdata passed to osmo_fd (available in cb as osmo_fd->data).
+ * \param[in] priv_nr additional userdata integer passed to osmo_fd (available in cb as osmo_fd->priv_nr).
+ * \returns allocated osmo_mdns_sock,
+ * NULL on error.
+ */
+struct osmo_mdns_sock *osmo_mdns_sock_init(void *ctx, const char *ip, unsigned int port,
+ int (*cb)(struct osmo_fd *fd, unsigned int what),
+ void *data, unsigned int priv_nr)
+{
+ struct osmo_mdns_sock *ret;
+ int sock, rc;
+ struct addrinfo hints = {0};
+ struct ip_mreq multicast_req = {0};
+ in_addr_t iface = INADDR_ANY;
+ char portbuf[10];
+ int y = 1;
+
+ snprintf(portbuf, sizeof(portbuf) -1, "%u", port);
+ ret = talloc_zero(ctx, struct osmo_mdns_sock);
+ OSMO_ASSERT(ret);
+
+ /* Fill addrinfo */
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_flags = (AI_PASSIVE | AI_NUMERICHOST);
+ rc = getaddrinfo(ip, portbuf, &hints, &ret->ai);
+ if (rc != 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: getaddrinfo: %s\n", gai_strerror(rc));
+ ret->ai = NULL;
+ goto error;
+ }
+
+ /* Open socket */
+ sock = socket(ret->ai->ai_family, ret->ai->ai_socktype, 0);
+ if (sock == -1) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: socket: %s\n", strerror(errno));
+ goto error;
+ }
+
+ /* Set multicast options */
+ rc = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char*)&iface, sizeof(iface));
+ if (rc == -1) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno));
+ goto error;
+ }
+ memcpy(&multicast_req.imr_multiaddr, &((struct sockaddr_in*)(ret->ai->ai_addr))->sin_addr,
+ sizeof(multicast_req.imr_multiaddr));
+ multicast_req.imr_interface.s_addr = htonl(INADDR_ANY);
+ rc = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multicast_req, sizeof(multicast_req));
+ if (rc == -1) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno));
+ goto error;
+ }
+
+ /* Always allow binding the same IP and port twice. This is needed in OsmoHLR (where the code becomes cleaner by
+ * just using a different socket for server and client code) and in the mslookup_client_mdns_test. Also for
+ * osmo-mslookup-client if it is running multiple times in parallel (i.e. two incoming calls almost at the same
+ * time need to be resolved with the simple dialplan example that just starts new processes). */
+ rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&y, sizeof(y));
+ if (rc == -1) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno));
+ goto error;
+ }
+
+ /* Bind and register osmo_fd callback */
+ rc = bind(sock, ret->ai->ai_addr, ret->ai->ai_addrlen);
+ if (rc == -1) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: bind: %s\n", strerror(errno));
+ goto error;
+ }
+ osmo_fd_setup(&ret->osmo_fd, sock, OSMO_FD_READ, cb, data, priv_nr);
+ if (osmo_fd_register(&ret->osmo_fd) != 0)
+ goto error;
+
+ return ret;
+error:
+ if (ret->ai)
+ freeaddrinfo(ret->ai);
+ talloc_free(ret);
+ return NULL;
+}
+
+/*! Send msgb over mdns_sock and consume msgb.
+ * \returns 0 on success,
+ * -1 on error.
+ */
+int osmo_mdns_sock_send(const struct osmo_mdns_sock *mdns_sock, struct msgb *msg)
+{
+ size_t len = msgb_length(msg);
+ int rc = sendto(mdns_sock->osmo_fd.fd, msgb_data(msg), len, 0, mdns_sock->ai->ai_addr,
+ mdns_sock->ai->ai_addrlen);
+ msgb_free(msg);
+ return (rc == len) ? 0 : -1;
+}
+
+/*! Tear down osmo_mdns_sock. */
+void osmo_mdns_sock_cleanup(struct osmo_mdns_sock *mdns_sock)
+{
+ osmo_fd_close(&mdns_sock->osmo_fd);
+ freeaddrinfo(mdns_sock->ai);
+ talloc_free(mdns_sock);
+}
diff --git a/src/mslookup/mslookup.c b/src/mslookup/mslookup.c
new file mode 100644
index 0000000..c7edb82
--- /dev/null
+++ b/src/mslookup/mslookup.c
@@ -0,0 +1,256 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <errno.h>
+#include <osmocom/gsm/gsm23003.h>
+#include <osmocom/mslookup/mslookup.h>
+
+#define CMP(a,b) (a < b? -1 : (a > b? 1 : 0))
+
+const struct value_string osmo_mslookup_id_type_names[] = {
+ { OSMO_MSLOOKUP_ID_NONE, "none" },
+ { OSMO_MSLOOKUP_ID_IMSI, "imsi" },
+ { OSMO_MSLOOKUP_ID_MSISDN, "msisdn" },
+ {}
+};
+
+const struct value_string osmo_mslookup_result_code_names[] = {
+ { OSMO_MSLOOKUP_RC_NONE, "none" },
+ { OSMO_MSLOOKUP_RC_RESULT, "result" },
+ { OSMO_MSLOOKUP_RC_NOT_FOUND, "not-found" },
+ {}
+};
+
+/*! Compare two struct osmo_mslookup_id.
+ * \returns 0 if a and b are equal,
+ * < 0 if a (or the ID type / start of ID) is < b,
+ * > 0 if a (or the ID type / start of ID) is > b.
+ */
+int osmo_mslookup_id_cmp(const struct osmo_mslookup_id *a, const struct osmo_mslookup_id *b)
+{
+ int cmp;
+ if (a == b)
+ return 0;
+ if (!a)
+ return -1;
+ if (!b)
+ return 1;
+
+ cmp = CMP(a->type, b->type);
+ if (cmp)
+ return cmp;
+
+ switch (a->type) {
+ case OSMO_MSLOOKUP_ID_IMSI:
+ return strncmp(a->imsi, b->imsi, sizeof(a->imsi));
+ case OSMO_MSLOOKUP_ID_MSISDN:
+ return strncmp(a->msisdn, b->msisdn, sizeof(a->msisdn));
+ default:
+ return 0;
+ }
+}
+
+bool osmo_mslookup_id_valid(const struct osmo_mslookup_id *id)
+{
+ switch (id->type) {
+ case OSMO_MSLOOKUP_ID_IMSI:
+ return osmo_imsi_str_valid(id->imsi);
+ case OSMO_MSLOOKUP_ID_MSISDN:
+ return osmo_msisdn_str_valid(id->msisdn);
+ default:
+ return false;
+ }
+}
+
+bool osmo_mslookup_service_valid(const char *service)
+{
+ return strlen(service) > 0;
+}
+
+/*! Write ID and ID type to a buffer.
+ * \param[out] buf nul-terminated {id}.{id_type} string (e.g. "1234.msisdn") or
+* "?.none" if the ID type is invalid.
+ * \returns amount of bytes written to buf.
+ */
+size_t osmo_mslookup_id_name_buf(char *buf, size_t buflen, const struct osmo_mslookup_id *id)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ switch (id->type) {
+ case OSMO_MSLOOKUP_ID_IMSI:
+ OSMO_STRBUF_PRINTF(sb, "%s", id->imsi);
+ break;
+ case OSMO_MSLOOKUP_ID_MSISDN:
+ OSMO_STRBUF_PRINTF(sb, "%s", id->msisdn);
+ break;
+ default:
+ OSMO_STRBUF_PRINTF(sb, "?");
+ break;
+ }
+ OSMO_STRBUF_PRINTF(sb, ".%s", osmo_mslookup_id_type_name(id->type));
+ return sb.chars_needed;
+}
+
+/*! Same as osmo_mslookup_id_name_buf(), but return a talloc allocated string of sufficient size. */
+char *osmo_mslookup_id_name_c(void *ctx, const struct osmo_mslookup_id *id)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_mslookup_id_name_buf, id)
+}
+
+/*! Same as osmo_mslookup_id_name_buf(), but directly return the char* (for printf-like string formats). */
+char *osmo_mslookup_id_name_b(char *buf, size_t buflen, const struct osmo_mslookup_id *id)
+{
+ int rc = osmo_mslookup_id_name_buf(buf, buflen, id);
+ if (rc < 0 && buflen)
+ buf[0] = '\0';
+ return buf;
+}
+
+/*! Write mslookup result string to buffer.
+ * \param[in] query with the service, ID and ID type to be written to buf like a domain string, or NULL to omit.
+ * \param[in] result with the result code, IPv4/v6 and age to be written to buf or NULL to omit.
+ * \param[out] buf result as flat string, which looks like the following for a valid query and result with IPv4 and v6
+ * answer: "sip.voice.1234.msisdn -> ipv4: 42.42.42.42:1337 -> ipv6: [1234:5678:9ABC::]:1338 (age=1)",
+ * the result part can also be " -> timeout" or " -> rc=5" depending on the result code.
+ * \returns amount of bytes written to buf.
+ */
+size_t osmo_mslookup_result_name_buf(char *buf, size_t buflen,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result)
+{
+ struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+ if (query) {
+ OSMO_STRBUF_PRINTF(sb, "%s.", query->service);
+ OSMO_STRBUF_APPEND(sb, osmo_mslookup_id_name_buf, &query->id);
+ }
+ if (result && result->rc == OSMO_MSLOOKUP_RC_NONE)
+ result = NULL;
+ if (result) {
+ if (result->rc != OSMO_MSLOOKUP_RC_RESULT)
+ OSMO_STRBUF_PRINTF(sb, " %s", osmo_mslookup_result_code_name(result->rc));
+ if (result->rc == OSMO_MSLOOKUP_RC_RESULT) {
+ if (result->host_v4.ip[0]) {
+ OSMO_STRBUF_PRINTF(sb, " -> ipv4: " OSMO_SOCKADDR_STR_FMT,
+ OSMO_SOCKADDR_STR_FMT_ARGS(&result->host_v4));
+ }
+ if (result->host_v6.ip[0]) {
+ OSMO_STRBUF_PRINTF(sb, " -> ipv6: " OSMO_SOCKADDR_STR_FMT,
+ OSMO_SOCKADDR_STR_FMT_ARGS(&result->host_v6));
+ }
+ OSMO_STRBUF_PRINTF(sb, " (age=%u)", result->age);
+ }
+ OSMO_STRBUF_PRINTF(sb, " %s", result->last ? "(last)" : "(not-last)");
+ }
+ return sb.chars_needed;
+}
+
+/*! Same as osmo_mslookup_result_name_buf(), but return a talloc allocated string of sufficient size. */
+char *osmo_mslookup_result_name_c(void *ctx,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result)
+{
+ OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_mslookup_result_name_buf, query, result)
+}
+
+/*! Same as osmo_mslookup_result_name_buf(), but directly return the char* (for printf-like string formats). */
+char *osmo_mslookup_result_name_b(char *buf, size_t buflen,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_result *result)
+{
+ int rc = osmo_mslookup_result_name_buf(buf, buflen, query, result);
+ if (rc < 0 && buflen)
+ buf[0] = '\0';
+ return buf;
+}
+
+/*! Copy part of a string to a buffer and nul-terminate it.
+ * \returns 0 on success,
+ * negative on error.
+ */
+static int token(char *dest, size_t dest_size, const char *start, const char *end)
+{
+ int len;
+ if (start >= end)
+ return -10;
+ len = end - start;
+ if (len >= dest_size)
+ return -11;
+ strncpy(dest, start, len);
+ dest[len] = '\0';
+ return 0;
+}
+
+/*! Parse a string like "foo.moo.goo.123456789012345.msisdn" into service="foo.moo.goo", id="123456789012345" and
+ * id_type="msisdn", placed in a struct osmo_mslookup_query.
+ * \returns 0 on success,
+ * negative on error.
+ */
+int osmo_mslookup_query_from_domain_str(struct osmo_mslookup_query *q, const char *domain)
+{
+ const char *last_dot;
+ const char *second_last_dot;
+ const char *id_type;
+ const char *id;
+ int rc;
+
+ *q = (struct osmo_mslookup_query){};
+
+ if (!domain)
+ return -1;
+
+ last_dot = strrchr(domain, '.');
+
+ if (!last_dot)
+ return -2;
+
+ if (last_dot <= domain)
+ return -3;
+
+ for (second_last_dot = last_dot - 1; second_last_dot > domain && *second_last_dot != '.'; second_last_dot--);
+ if (second_last_dot == domain || *second_last_dot != '.')
+ return -3;
+
+ id_type = last_dot + 1;
+ if (!*id_type)
+ return -4;
+
+ q->id.type = get_string_value(osmo_mslookup_id_type_names, id_type);
+
+ id = second_last_dot + 1;
+ switch (q->id.type) {
+ case OSMO_MSLOOKUP_ID_IMSI:
+ rc = token(q->id.imsi, sizeof(q->id.imsi), id, last_dot);
+ if (rc)
+ return rc;
+ if (!osmo_imsi_str_valid(q->id.imsi))
+ return -5;
+ break;
+ case OSMO_MSLOOKUP_ID_MSISDN:
+ rc = token(q->id.msisdn, sizeof(q->id.msisdn), id, last_dot);
+ if (rc)
+ return rc;
+ if (!osmo_msisdn_str_valid(q->id.msisdn))
+ return -6;
+ break;
+ default:
+ return -7;
+ }
+
+ return token(q->service, sizeof(q->service), domain, second_last_dot);
+}
diff --git a/src/mslookup/mslookup_client.c b/src/mslookup/mslookup_client.c
new file mode 100644
index 0000000..f53f374
--- /dev/null
+++ b/src/mslookup/mslookup_client.c
@@ -0,0 +1,311 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mslookup_client.h>
+
+/*! Lookup client's internal data for a query. */
+struct osmo_mslookup_client {
+ struct llist_head lookup_methods;
+ struct llist_head requests;
+ uint32_t next_request_handle;
+};
+
+/*! Lookup client's internal data for a query.
+ * The request methods only get to see the query part, and result handling is done commonly for all request methods. */
+struct osmo_mslookup_client_request {
+ struct llist_head entry;
+ struct osmo_mslookup_client *client;
+ uint32_t request_handle;
+
+ struct osmo_mslookup_query query;
+ struct osmo_mslookup_query_handling handling;
+ struct osmo_timer_list timeout;
+ bool waiting_min_delay;
+
+ struct osmo_mslookup_result result;
+};
+
+static struct osmo_mslookup_client_request *get_request(struct osmo_mslookup_client *client, uint32_t request_handle)
+{
+ struct osmo_mslookup_client_request *r;
+ if (!request_handle)
+ return NULL;
+ llist_for_each_entry(r, &client->requests, entry) {
+ if (r->request_handle == request_handle)
+ return r;
+ }
+ return NULL;
+}
+
+struct osmo_mslookup_client *osmo_mslookup_client_new(void *ctx)
+{
+ struct osmo_mslookup_client *client = talloc_zero(ctx, struct osmo_mslookup_client);
+ OSMO_ASSERT(client);
+ INIT_LLIST_HEAD(&client->lookup_methods);
+ INIT_LLIST_HEAD(&client->requests);
+ return client;
+}
+
+/*! Return whether any lookup methods are available.
+ * \param[in] client Client to query.
+ * \return true when a client is present that has at least one osmo_mslookup_client_method registered.
+ */
+bool osmo_mslookup_client_active(struct osmo_mslookup_client *client)
+{
+ if (!client)
+ return false;
+ if (llist_empty(&client->lookup_methods))
+ return false;
+ return true;
+}
+
+static void _osmo_mslookup_client_method_del(struct osmo_mslookup_client_method *method)
+{
+ if (method->destruct)
+ method->destruct(method);
+ llist_del(&method->entry);
+ talloc_free(method);
+}
+
+/*! Stop and free mslookup client and all registered lookup methods.
+ */
+void osmo_mslookup_client_free(struct osmo_mslookup_client *client)
+{
+ struct osmo_mslookup_client_method *m, *n;
+ if (!client)
+ return;
+ llist_for_each_entry_safe(m, n, &client->lookup_methods, entry) {
+ _osmo_mslookup_client_method_del(m);
+ }
+ talloc_free(client);
+}
+
+/*! Add an osmo_mslookup_client_method to service MS Lookup requests.
+ * Note, osmo_mslookup_client_method_del() will talloc_free() the method pointer, so it needs to be dynamically
+ * allocated.
+ * \param client The osmo_mslookup_client instance to add to.
+ * \param method A fully initialized method struct, allocated by talloc.
+ */
+void osmo_mslookup_client_method_add(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_client_method *method)
+{
+ method->client = client;
+ llist_add_tail(&method->entry, &client->lookup_methods);
+}
+
+/*! \return false if the method was not listed, true if the method was listed, removed and talloc_free()d.
+ */
+bool osmo_mslookup_client_method_del(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_client_method *method)
+{
+ struct osmo_mslookup_client_method *m;
+ llist_for_each_entry(m, &client->lookup_methods, entry) {
+ if (m == method) {
+ _osmo_mslookup_client_method_del(method);
+ return true;
+ }
+ }
+ return false;
+}
+
+static void osmo_mslookup_request_send_result(struct osmo_mslookup_client_request *r, bool finish)
+{
+ struct osmo_mslookup_client *client = r->client;
+ uint32_t request_handle = r->request_handle;
+
+ r->result.last = finish;
+ r->handling.result_cb(r->client, r->request_handle, &r->query, &r->result);
+
+ /* Make sure the request struct is discarded.
+ * The result_cb() may already have triggered a cleanup, so query by request_handle. */
+ if (finish)
+ osmo_mslookup_client_request_cancel(client, request_handle);
+}
+
+void osmo_mslookup_client_rx_result(struct osmo_mslookup_client *client, uint32_t request_handle,
+ const struct osmo_mslookup_result *result)
+{
+ struct osmo_mslookup_client_request *req = get_request(client, request_handle);
+
+ if (!req) {
+ LOGP(DMSLOOKUP, LOGL_ERROR,
+ "Internal error: Got mslookup result for a request that does not exist (handle %u)\n",
+ req->request_handle);
+ return;
+ }
+
+ /* Ignore incoming results that are not successful */
+ if (result->rc != OSMO_MSLOOKUP_RC_RESULT)
+ return;
+
+ /* If we already stored an earlier successful result, keep that if its age is younger. */
+ if (req->result.rc == OSMO_MSLOOKUP_RC_RESULT
+ && result->age >= req->result.age)
+ return;
+
+ req->result = *result;
+
+ /* If age == 0, it doesn't get any better, so return the result immediately. */
+ if (req->result.age == 0) {
+ osmo_mslookup_request_send_result(req, true);
+ return;
+ }
+
+ if (req->waiting_min_delay)
+ return;
+
+ osmo_mslookup_request_send_result(req, false);
+}
+
+static void _osmo_mslookup_client_request_cleanup(struct osmo_mslookup_client_request *r)
+{
+ struct osmo_mslookup_client_method *m;
+ osmo_timer_del(&r->timeout);
+ llist_for_each_entry(m, &r->client->lookup_methods, entry) {
+ if (!m->request_cleanup)
+ continue;
+ m->request_cleanup(m, r->request_handle);
+ }
+ llist_del(&r->entry);
+ talloc_free(r);
+}
+
+static void timeout_cb(void *data);
+
+static void set_timer(struct osmo_mslookup_client_request *r, unsigned long milliseconds)
+{
+ osmo_timer_setup(&r->timeout, timeout_cb, r);
+ osmo_timer_schedule(&r->timeout, milliseconds / 1000, (milliseconds % 1000) * 1000);
+}
+
+static void timeout_cb(void *data)
+{
+ struct osmo_mslookup_client_request *r = data;
+ if (r->waiting_min_delay) {
+ /* The initial delay has passed. See if it stops here, or whether the overall timeout continues. */
+ r->waiting_min_delay = false;
+
+ if (r->handling.result_timeout_milliseconds <= r->handling.min_delay_milliseconds) {
+ /* It ends here. Return a final result. */
+ if (r->result.rc != OSMO_MSLOOKUP_RC_RESULT)
+ r->result.rc = OSMO_MSLOOKUP_RC_NOT_FOUND;
+ osmo_mslookup_request_send_result(r, true);
+ return;
+ }
+
+ /* We continue to listen for results. If one is already on record, send it now. */
+ if (r->result.rc == OSMO_MSLOOKUP_RC_RESULT)
+ osmo_mslookup_request_send_result(r, false);
+
+ set_timer(r, r->handling.result_timeout_milliseconds - r->handling.min_delay_milliseconds);
+ return;
+ }
+ /* The final timeout has passed, finish and clean up the request. */
+ switch (r->result.rc) {
+ case OSMO_MSLOOKUP_RC_RESULT:
+ /* If the rc == OSMO_MSLOOKUP_RC_RESULT, this result has already been sent.
+ * Don't send it again, instead send an RC_NONE, last=true result. */
+ r->result.rc = OSMO_MSLOOKUP_RC_NONE;
+ break;
+ default:
+ r->result.rc = OSMO_MSLOOKUP_RC_NOT_FOUND;
+ break;
+ }
+ osmo_mslookup_request_send_result(r, true);
+}
+
+/*! Launch a subscriber lookup with the provided query.
+ * A request is cleared implicitly when the handling->result_cb is invoked; if the quer->priv pointer becomes invalid
+ * before that, a request should be canceled by calling osmo_mslookup_client_request_cancel() with the returned
+ * request_handle. A request handle of zero indicates error.
+ * \return a nonzero request_handle that allows ending the request, or 0 on invalid query data. */
+uint32_t osmo_mslookup_client_request(struct osmo_mslookup_client *client,
+ const struct osmo_mslookup_query *query,
+ const struct osmo_mslookup_query_handling *handling)
+{
+ struct osmo_mslookup_client_request *r;
+ struct osmo_mslookup_client_request *other;
+ struct osmo_mslookup_client_method *m;
+
+ if (!osmo_mslookup_service_valid(query->service)
+ || !osmo_mslookup_id_valid(&query->id)) {
+ char buf[256];
+ LOGP(DMSLOOKUP, LOGL_ERROR, "Invalid query: %s\n",
+ osmo_mslookup_result_name_b(buf, sizeof(buf), query, NULL));
+ return 0;
+ }
+
+ r = talloc_zero(client, struct osmo_mslookup_client_request);
+ OSMO_ASSERT(r);
+
+ /* A request_handle of zero means error, so make sure we don't use a zero handle. */
+ if (!client->next_request_handle)
+ client->next_request_handle++;
+ *r = (struct osmo_mslookup_client_request){
+ .client = client,
+ .query = *query,
+ .handling = *handling,
+ .request_handle = client->next_request_handle++,
+ };
+
+ if (!r->handling.result_timeout_milliseconds)
+ r->handling.result_timeout_milliseconds = r->handling.min_delay_milliseconds;
+ if (!r->handling.result_timeout_milliseconds)
+ r->handling.result_timeout_milliseconds = 1000;
+
+ /* Paranoia: make sure a request_handle exists only once, by expiring an already existing one. This is unlikely
+ * to happen in practice: before we get near wrapping a uint32_t range, previous requests should long have
+ * timed out or ended. */
+ llist_for_each_entry(other, &client->requests, entry) {
+ if (other->request_handle != r->request_handle)
+ continue;
+ osmo_mslookup_request_send_result(other, true);
+ /* we're sure it exists only once. */
+ break;
+ }
+
+ /* Now sure that the new request_handle does not exist a second time. */
+ llist_add_tail(&r->entry, &client->requests);
+
+ if (r->handling.min_delay_milliseconds) {
+ r->waiting_min_delay = true;
+ set_timer(r, r->handling.min_delay_milliseconds);
+ } else {
+ set_timer(r, r->handling.result_timeout_milliseconds);
+ }
+
+ /* Let the lookup implementations know */
+ llist_for_each_entry(m, &client->lookup_methods, entry) {
+ m->request(m, query, r->request_handle);
+ }
+ return r->request_handle;
+}
+
+/*! End or cancel a subscriber lookup. This *must* be invoked exactly once per osmo_mslookup_client_request() invocation,
+ * either after a lookup has concluded or to abort an ongoing lookup.
+ * \param[in] request_handle The request_handle returned by an osmo_mslookup_client_request() invocation.
+ */
+void osmo_mslookup_client_request_cancel(struct osmo_mslookup_client *client, uint32_t request_handle)
+{
+ struct osmo_mslookup_client_request *r = get_request(client, request_handle);
+ if (!r)
+ return;
+ _osmo_mslookup_client_request_cleanup(r);
+}
diff --git a/src/mslookup/mslookup_client_fake.c b/src/mslookup/mslookup_client_fake.c
new file mode 100644
index 0000000..b5f1e21
--- /dev/null
+++ b/src/mslookup/mslookup_client_fake.c
@@ -0,0 +1,157 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mslookup_client.h>
+#include <osmocom/mslookup/mslookup_client_fake.h>
+
+#include <string.h>
+
+/* Fake mslookup method */
+
+struct fake_lookup_state {
+ struct osmo_mslookup_client *client;
+ struct llist_head requests;
+ struct osmo_timer_list async_response_timer;
+ struct osmo_mslookup_fake_response *responses;
+ size_t responses_len;
+};
+
+struct fake_lookup_request {
+ struct llist_head entry;
+ uint32_t request_handle;
+ struct osmo_mslookup_query query;
+ struct timeval received_at;
+};
+
+/*! Args for osmo_timer_schedule: seconds and microseconds. */
+#define ASYNC_RESPONSE_PERIOD 0, (1e6 / 10)
+static void fake_lookup_async_response(void *state);
+
+static void fake_lookup_request(struct osmo_mslookup_client_method *method,
+ const struct osmo_mslookup_query *query,
+ uint32_t request_handle)
+{
+ struct fake_lookup_state *state = method->priv;
+ char buf[256];
+ LOGP(DMSLOOKUP, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_mslookup_result_name_b(buf, sizeof(buf), query, NULL));
+
+ /* A real implementation would send packets to some remote server.
+ * Here this is simulated: add to the list of requests, which fake_lookup_async_response() will reply upon
+ * according to the test data listing the replies that the test wants to generate. */
+
+ struct fake_lookup_request *r = talloc_zero(method->client, struct fake_lookup_request);
+ *r = (struct fake_lookup_request){
+ .request_handle = request_handle,
+ .query = *query,
+ };
+ osmo_gettimeofday(&r->received_at, NULL);
+ llist_add_tail(&r->entry, &state->requests);
+}
+
+static void fake_lookup_request_cleanup(struct osmo_mslookup_client_method *method,
+ uint32_t request_handle)
+{
+ struct fake_lookup_state *state = method->priv;
+
+ /* Tear down any state associated with this handle. */
+ struct fake_lookup_request *r;
+ llist_for_each_entry(r, &state->requests, entry) {
+ if (r->request_handle != request_handle)
+ continue;
+ llist_del(&r->entry);
+ talloc_free(r);
+ LOGP(DMSLOOKUP, LOGL_DEBUG, "%s() ok\n", __func__);
+ return;
+ }
+ LOGP(DMSLOOKUP, LOGL_DEBUG, "%s() FAILED\n", __func__);
+}
+
+static void fake_lookup_async_response(void *data)
+{
+ struct fake_lookup_state *state = data;
+ struct fake_lookup_request *req, *n;
+ struct timeval now;
+ char str[256];
+
+ osmo_gettimeofday(&now, NULL);
+
+ llist_for_each_entry_safe(req, n, &state->requests, entry) {
+ struct osmo_mslookup_fake_response *resp;
+
+ for (resp = state->responses;
+ (resp - state->responses) < state->responses_len;
+ resp++) {
+ struct timeval diff;
+
+ if (resp->sent)
+ continue;
+ if (osmo_mslookup_id_cmp(&req->query.id, &resp->for_id) != 0)
+ continue;
+ if (strcmp(req->query.service, resp->for_service) != 0)
+ continue;
+
+ timersub(&now, &req->received_at, &diff);
+ if (timercmp(&diff, &resp->time_to_reply, <))
+ continue;
+
+ /* It's time to reply to this request. */
+ LOGP(DMSLOOKUP, LOGL_DEBUG, "osmo_mslookup_client_rx_result(): %s\n",
+ osmo_mslookup_result_name_b(str, sizeof(str), &req->query, &resp->result));
+ osmo_mslookup_client_rx_result(state->client, req->request_handle, &resp->result);
+ resp->sent = true;
+
+ /* The req will have been cleaned up now, so we must not iterate over state->responses anymore
+ * with this req. */
+ break;
+ }
+ }
+
+ osmo_timer_schedule(&state->async_response_timer, ASYNC_RESPONSE_PERIOD);
+}
+
+struct osmo_mslookup_client_method *osmo_mslookup_client_add_fake(struct osmo_mslookup_client *client,
+ struct osmo_mslookup_fake_response *responses,
+ size_t responses_len)
+{
+ struct osmo_mslookup_client_method *method = talloc_zero(client, struct osmo_mslookup_client_method);
+ OSMO_ASSERT(method);
+
+ struct fake_lookup_state *state = talloc_zero(method, struct fake_lookup_state);
+ OSMO_ASSERT(state);
+ *state = (struct fake_lookup_state){
+ .client = client,
+ .responses = responses,
+ .responses_len = responses_len,
+ };
+ INIT_LLIST_HEAD(&state->requests);
+
+ *method = (struct osmo_mslookup_client_method){
+ .name = "fake",
+ .priv = state,
+ .request = fake_lookup_request,
+ .request_cleanup = fake_lookup_request_cleanup,
+ };
+
+ osmo_timer_setup(&state->async_response_timer, fake_lookup_async_response, state);
+ osmo_mslookup_client_method_add(client, method);
+
+ osmo_timer_schedule(&state->async_response_timer, ASYNC_RESPONSE_PERIOD);
+ return method;
+}
diff --git a/src/mslookup/mslookup_client_mdns.c b/src/mslookup/mslookup_client_mdns.c
new file mode 100644
index 0000000..12501ee
--- /dev/null
+++ b/src/mslookup/mslookup_client_mdns.c
@@ -0,0 +1,223 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <errno.h>
+#include <osmocom/core/select.h>
+#include <osmocom/gsm/gsm_utils.h>
+#include <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mdns.h>
+#include <osmocom/mslookup/mdns_sock.h>
+#include <osmocom/mslookup/mslookup_client.h>
+#include <osmocom/mslookup/mslookup_client_mdns.h>
+
+struct osmo_mdns_method_state {
+ /* Parameters passed by _add_method_dns() */
+ struct osmo_sockaddr_str bind_addr;
+
+ struct osmo_mdns_sock *mc;
+
+ struct osmo_mslookup_client *client;
+ struct llist_head requests;
+ uint16_t next_packet_id;
+};
+
+struct osmo_mdns_method_request {
+ struct llist_head entry;
+ uint32_t request_handle;
+ struct osmo_mslookup_query query;
+ uint16_t packet_id;
+};
+
+static int request_handle_by_query(uint32_t *request_handle, struct osmo_mdns_method_state *state,
+ struct osmo_mslookup_query *query, uint16_t packet_id)
+{
+ struct osmo_mdns_method_request *request;
+
+ llist_for_each_entry(request, &state->requests, entry) {
+ if (strcmp(request->query.service, query->service) != 0)
+ continue;
+ if (osmo_mslookup_id_cmp(&request->query.id, &query->id) != 0)
+ continue;
+
+ /* Match! */
+ *request_handle = request->request_handle;
+ return 0;
+ }
+ return -1;
+}
+
+static int mdns_method_recv(struct osmo_fd *osmo_fd, unsigned int what)
+{
+ struct osmo_mdns_method_state *state = osmo_fd->data;
+ struct osmo_mslookup_result result;
+ struct osmo_mslookup_query query;
+ uint16_t packet_id;
+ int n;
+ uint8_t buffer[1024];
+ uint32_t request_handle = 0;
+ void *ctx = state;
+
+ n = read(osmo_fd->fd, buffer, sizeof(buffer));
+ if (n < 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "failed to read from socket\n");
+ return n;
+ }
+
+ if (osmo_mdns_result_decode(ctx, buffer, n, &packet_id, &query, &result) < 0)
+ return -EINVAL;
+
+ if (request_handle_by_query(&request_handle, state, &query, packet_id) != 0)
+ return -EINVAL;
+
+ osmo_mslookup_client_rx_result(state->client, request_handle, &result);
+ return n;
+}
+
+static void mdns_method_request(struct osmo_mslookup_client_method *method, const struct osmo_mslookup_query *query,
+ uint32_t request_handle)
+{
+ char buf[256];
+ struct osmo_mdns_method_state *state = method->priv;
+ struct msgb *msg;
+ struct osmo_mdns_method_request *r = talloc_zero(method->client, struct osmo_mdns_method_request);
+
+ *r = (struct osmo_mdns_method_request){
+ .request_handle = request_handle,
+ .query = *query,
+ .packet_id = state->next_packet_id,
+ };
+ llist_add(&r->entry, &state->requests);
+ state->next_packet_id++;
+
+ msg = osmo_mdns_query_encode(method->client, r->packet_id, query);
+ if (!msg) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "Cannot encode request: %s\n",
+ osmo_mslookup_result_name_b(buf, sizeof(buf), query, NULL));
+ }
+
+ /* Send over the wire */
+ LOGP(DMSLOOKUP, LOGL_DEBUG, "sending mDNS query: %s.%s\n", query->service,
+ osmo_mslookup_id_name_b(buf, sizeof(buf), &query->id));
+ if (osmo_mdns_sock_send(state->mc, msg) == -1)
+ LOGP(DMSLOOKUP, LOGL_ERROR, "sending mDNS query failed\n");
+}
+
+static void mdns_method_request_cleanup(struct osmo_mslookup_client_method *method, uint32_t request_handle)
+{
+ struct osmo_mdns_method_state *state = method->priv;
+
+ /* Tear down any state associated with this handle. */
+ struct osmo_mdns_method_request *r;
+ llist_for_each_entry(r, &state->requests, entry) {
+ if (r->request_handle != request_handle)
+ continue;
+ llist_del(&r->entry);
+ talloc_free(r);
+ return;
+ }
+}
+
+static void mdns_method_destruct(struct osmo_mslookup_client_method *method)
+{
+ struct osmo_mdns_method_state *state = method->priv;
+ struct osmo_mdns_method_request *e, *n;
+ if (!state)
+ return;
+
+ /* Drop all DNS lookup request state. Triggering a timeout event and cleanup for mslookup client users will
+ * happen in the mslookup_client.c, we will simply stop responding from this lookup method. */
+ llist_for_each_entry_safe(e, n, &state->requests, entry) {
+ llist_del(&e->entry);
+ }
+
+ osmo_mdns_sock_cleanup(state->mc);
+}
+
+/*! Initialize the mDNS lookup method.
+ * \param[in] client the client to attach the method to.
+ * \param[in] ip IPv4 or IPv6 address string.
+ * \param[in] port The port to bind to.
+ * \param[in] initial_packet_id Used in the first mslookup query, then increased by one in each following query. All
+ * servers answer to each query with the same packet ID. Set to -1 to use a random
+ * initial ID (recommended unless you need deterministic output). This ID is for visually
+ * distinguishing the packets in packet sniffers, the mslookup client uses not just the
+ * ID, but all query parameters (service type, ID, ID type), to determine if a reply is
+ * relevant. */
+struct osmo_mslookup_client_method *osmo_mslookup_client_add_mdns(struct osmo_mslookup_client *client, const char *ip,
+ uint16_t port, int initial_packet_id)
+{
+ struct osmo_mdns_method_state *state;
+ struct osmo_mslookup_client_method *m;
+
+ m = talloc_zero(client, struct osmo_mslookup_client_method);
+ OSMO_ASSERT(m);
+
+ state = talloc_zero(m, struct osmo_mdns_method_state);
+ OSMO_ASSERT(state);
+ INIT_LLIST_HEAD(&state->requests);
+ if (osmo_sockaddr_str_from_str(&state->bind_addr, ip, port)) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "mslookup mDNS: invalid address/port: %s %u\n",
+ ip, port);
+ goto error_cleanup;
+ }
+
+ if (initial_packet_id == -1) {
+ if (osmo_get_rand_id((uint8_t *)&state->next_packet_id, 2) < 0) {
+ LOGP(DMSLOOKUP, LOGL_ERROR, "mslookup mDNS: failed to generate random initial packet ID\n");
+ goto error_cleanup;
+ }
+ } else
+ state->next_packet_id = initial_packet_id;
+
+ state->client = client;
+
+ state->mc = osmo_mdns_sock_init(state, ip, port, mdns_method_recv, state, 0);
+ if (!state->mc)
+ goto error_cleanup;
+
+ *m = (struct osmo_mslookup_client_method){
+ .name = "mDNS",
+ .priv = state,
+ .request = mdns_method_request,
+ .request_cleanup = mdns_method_request_cleanup,
+ .destruct = mdns_method_destruct,
+ };
+
+ osmo_mslookup_client_method_add(client, m);
+ return m;
+
+error_cleanup:
+ talloc_free(m);
+ return NULL;
+}
+
+const struct osmo_sockaddr_str *osmo_mslookup_client_method_mdns_get_bind_addr(struct osmo_mslookup_client_method
+ *dns_method)
+{
+ struct osmo_mdns_method_state *state;
+ if (!dns_method || !dns_method->priv)
+ return NULL;
+ state = dns_method->priv;
+ return &state->bind_addr;
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bbe21b5..bc5fc87 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..ebf2add
--- /dev/null
+++ b/tests/mslookup/Makefile.am
@@ -0,0 +1,69 @@
+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 = \
+ mdns_test.err \
+ mslookup_client_mdns_test.err \
+ mslookup_client_test.err \
+ mslookup_test.err \
+ $(NULL)
+
+check_PROGRAMS = \
+ mdns_test \
+ mslookup_client_mdns_test \
+ 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)
+
+mslookup_client_mdns_test_SOURCES = \
+ mslookup_client_mdns_test.c \
+ $(NULL)
+mslookup_client_mdns_test_LDADD = \
+ $(top_builddir)/src/mslookup/libosmo-mslookup.la \
+ $(LIBOSMOGSM_LIBS) \
+ $(NULL)
+
+mdns_test_SOURCES = \
+ mdns_test.c \
+ $(NULL)
+mdns_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/mdns_test.c b/tests/mslookup/mdns_test.c
new file mode 100644
index 0000000..ac87bbe
--- /dev/null
+++ b/tests/mslookup/mdns_test.c
@@ -0,0 +1,603 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 "../../src/mslookup/mdns_rfc.h"
+#include "../../src/mslookup/mdns_record.h"
+#include "../../src/mslookup/mdns_msg.h"
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
+
+struct qname_enc_dec_test {
+ const char *domain;
+ const char *qname;
+ size_t qname_max_len; /* default: strlen(qname) + 1 */
+};
+
+static const struct qname_enc_dec_test qname_enc_dec_test_data[] = {
+ {
+ /* OK: typical mslookup domain */
+ .domain = "hlr.1234567.imsi",
+ .qname = "\x03" "hlr" "\x07" "1234567" "\x04" "imsi",
+ },
+ {
+ /* Wrong format: double dot */
+ .domain = "hlr..imsi",
+ .qname = NULL,
+ },
+ {
+ /* Wrong format: double dot */
+ .domain = "hlr",
+ .qname = "\x03hlr\0\x03imsi",
+ },
+ {
+ /* Wrong format: dot at end */
+ .domain = "hlr.",
+ .qname = NULL,
+ },
+ {
+ /* Wrong format: dot at start */
+ .domain = ".hlr",
+ .qname = NULL,
+ },
+ {
+ /* Wrong format: empty */
+ .domain = "",
+ .qname = NULL,
+ },
+ {
+ /* OK: maximum length */
+ .domain =
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "12345"
+ ,
+ .qname =
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\x05" "12345"
+ },
+ {
+ /* Error: too long domain */
+ .domain =
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "123456789." "123456789." "123456789." "123456789." "123456789."
+ "12345toolong"
+ ,
+ .qname = NULL,
+ },
+ {
+ /* Error: too long qname */
+ .domain = NULL,
+ .qname =
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ "\t123456789\t123456789\t123456789\t123456789\t123456789"
+ },
+ {
+ /* Error: wrong token length in qname */
+ .domain = NULL,
+ .qname = "\x03" "hlr" "\x07" "1234567" "\x05" "imsi",
+ },
+ {
+ /* Error: wrong token length in qname */
+ .domain = NULL,
+ .qname = "\x02" "hlr" "\x07" "1234567" "\x04" "imsi",
+ },
+ {
+ /* Wrong format: token length at end of qname */
+ .domain = NULL,
+ .qname = "\x03hlr\x03",
+ },
+ {
+ /* Error: overflow in label length */
+ .domain = NULL,
+ .qname = "\x03" "hlr" "\x07" "1234567" "\x04" "imsi",
+ .qname_max_len = 17,
+ },
+};
+
+void test_enc_dec_rfc_qname(void *ctx)
+{
+ char quote_buf[300];
+ int i;
+
+ fprintf(stderr, "-- %s --\n", __func__);
+
+ for (i = 0; i < ARRAY_SIZE(qname_enc_dec_test_data); i++) {
+ const struct qname_enc_dec_test *t = &qname_enc_dec_test_data[i];
+ char *res;
+
+ if (t->domain) {
+ fprintf(stderr, "domain: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->domain, -1));
+ fprintf(stderr, "exp: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->qname, -1));
+ res = osmo_mdns_rfc_qname_encode(ctx, t->domain);
+ fprintf(stderr, "res: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), res, -1));
+ if (t->qname == res || (t->qname && res && strcmp(t->qname, res) == 0))
+ fprintf(stderr, "=> OK\n");
+ else
+ fprintf(stderr, "=> ERROR\n");
+ if (res)
+ talloc_free(res);
+ fprintf(stderr, "\n");
+ }
+
+ if (t->qname) {
+ size_t qname_max_len = t->qname_max_len;
+ if (qname_max_len)
+ fprintf(stderr, "qname_max_len: %lu\n", qname_max_len);
+ else
+ qname_max_len = strlen(t->qname) + 1;
+
+ fprintf(stderr, "qname: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->qname, -1));
+ fprintf(stderr, "exp: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->domain, -1));
+ res = osmo_mdns_rfc_qname_decode(ctx, t->qname, qname_max_len);
+ fprintf(stderr, "res: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), res, -1));
+ if (t->domain == res || (t->domain && res && strcmp(t->domain, res) == 0))
+ fprintf(stderr, "=> OK\n");
+ else
+ fprintf(stderr, "=> ERROR\n");
+ if (res)
+ talloc_free(res);
+ fprintf(stderr, "\n");
+ }
+ }
+}
+
+#define PRINT_HDR(hdr, name) \
+ fprintf(stderr, "header %s:\n" \
+ ".id = %i\n" \
+ ".qr = %i\n" \
+ ".opcode = %x\n" \
+ ".aa = %i\n" \
+ ".tc = %i\n" \
+ ".rd = %i\n" \
+ ".ra = %i\n" \
+ ".z = %x\n" \
+ ".rcode = %x\n" \
+ ".qdcount = %u\n" \
+ ".ancount = %u\n" \
+ ".nscount = %u\n" \
+ ".arcount = %u\n", \
+ name, hdr.id, hdr.qr, hdr.opcode, hdr.aa, hdr.tc, hdr.rd, hdr.ra, hdr.z, hdr.rcode, hdr.qdcount, \
+ hdr.ancount, hdr.nscount, hdr.arcount)
+
+static const struct osmo_mdns_rfc_header header_enc_dec_test_data[] = {
+ {
+ /* Typical use case for mslookup */
+ .id = 1337,
+ .qdcount = 1,
+ },
+ {
+ /* Fill out everything */
+ .id = 42,
+ .qr = 1,
+ .opcode = 0x02,
+ .aa = 1,
+ .tc = 1,
+ .rd = 1,
+ .ra = 1,
+ .z = 0x02,
+ .rcode = 0x03,
+ .qdcount = 1234,
+ .ancount = 1111,
+ .nscount = 2222,
+ .arcount = 3333,
+ },
+};
+
+void test_enc_dec_rfc_header()
+{
+ int i;
+
+ fprintf(stderr, "-- %s --\n", __func__);
+ for (i = 0; i< ARRAY_SIZE(header_enc_dec_test_data); i++) {
+ const struct osmo_mdns_rfc_header in = header_enc_dec_test_data[i];
+ struct osmo_mdns_rfc_header out = {0};
+ struct msgb *msg = msgb_alloc(4096, "dns_test");
+
+ PRINT_HDR(in, "in");
+ osmo_mdns_rfc_header_encode(msg, &in);
+ fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
+ assert(osmo_mdns_rfc_header_decode(msgb_data(msg), msgb_length(msg), &out) == 0);
+ PRINT_HDR(out, "out");
+
+ fprintf(stderr, "in (hexdump): %s\n", osmo_hexdump((unsigned char *)&in, sizeof(in)));
+ fprintf(stderr, "out (hexdump): %s\n", osmo_hexdump((unsigned char *)&out, sizeof(out)));
+ assert(memcmp(&in, &out, sizeof(in)) == 0);
+
+ fprintf(stderr, "=> OK\n\n");
+ msgb_free(msg);
+ }
+}
+
+void test_enc_dec_rfc_header_einval()
+{
+ struct osmo_mdns_rfc_header out = {0};
+ struct msgb *msg = msgb_alloc(4096, "dns_test");
+ fprintf(stderr, "-- %s --\n", __func__);
+
+ assert(osmo_mdns_rfc_header_decode(msgb_data(msg), 11, &out) == -EINVAL);
+ fprintf(stderr, "=> OK\n\n");
+
+ msgb_free(msg);
+}
+
+#define PRINT_QST(qst, name) \
+ fprintf(stderr, "question %s:\n" \
+ ".domain = %s\n" \
+ ".qtype = %i\n" \
+ ".qclass = %i\n", \
+ name, (qst)->domain, (qst)->qtype, (qst)->qclass)
+
+static const struct osmo_mdns_rfc_question question_enc_dec_test_data[] = {
+ {
+ .domain = "hlr.1234567.imsi",
+ .qtype = OSMO_MDNS_RFC_RECORD_TYPE_ALL,
+ .qclass = OSMO_MDNS_RFC_CLASS_IN,
+ },
+ {
+ .domain = "hlr.1234567.imsi",
+ .qtype = OSMO_MDNS_RFC_RECORD_TYPE_A,
+ .qclass = OSMO_MDNS_RFC_CLASS_ALL,
+ },
+ {
+ .domain = "hlr.1234567.imsi",
+ .qtype = OSMO_MDNS_RFC_RECORD_TYPE_AAAA,
+ .qclass = OSMO_MDNS_RFC_CLASS_ALL,
+ },
+};
+
+void test_enc_dec_rfc_question(void *ctx)
+{
+ int i;
+
+ fprintf(stderr, "-- %s --\n", __func__);
+ for (i = 0; i< ARRAY_SIZE(question_enc_dec_test_data); i++) {
+ const struct osmo_mdns_rfc_question in = question_enc_dec_test_data[i];
+ struct osmo_mdns_rfc_question *out;
+ struct msgb *msg = msgb_alloc(4096, "dns_test");
+
+ PRINT_QST(&in, "in");
+ assert(osmo_mdns_rfc_question_encode(ctx, msg, &in) == 0);
+ fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
+ out = osmo_mdns_rfc_question_decode(ctx, msgb_data(msg), msgb_length(msg));
+ assert(out);
+ PRINT_QST(out, "out");
+
+ if (strcmp(in.domain, out->domain) != 0)
+ fprintf(stderr, "=> ERROR: domain does not match\n");
+ else if (in.qtype != out->qtype)
+ fprintf(stderr, "=> ERROR: qtype does not match\n");
+ else if (in.qclass != out->qclass)
+ fprintf(stderr, "=> ERROR: qclass does not match\n");
+ else
+ fprintf(stderr, "=> OK\n");
+
+ fprintf(stderr, "\n");
+ msgb_free(msg);
+ talloc_free(out);
+ }
+}
+
+void test_enc_dec_rfc_question_null(void *ctx)
+{
+ uint8_t data[5] = {0};
+
+ fprintf(stderr, "-- %s --\n", __func__);
+ assert(osmo_mdns_rfc_question_decode(ctx, data, sizeof(data)) == NULL);
+ fprintf(stderr, "=> OK\n\n");
+}
+
+#define PRINT_REC(rec, name) \
+ fprintf(stderr, "question %s:\n" \
+ ".domain = %s\n" \
+ ".type = %i\n" \
+ ".class = %i\n" \
+ ".ttl = %i\n" \
+ ".rdlength = %i\n" \
+ ".rdata = %s\n", \
+ name, (rec)->domain, (rec)->type, (rec)->class, (rec)->ttl, (rec)->rdlength, \
+ osmo_quote_str((char *)(rec)->rdata, (rec)->rdlength))
+
+static const struct osmo_mdns_rfc_record record_enc_dec_test_data[] = {
+ {
+ .domain = "hlr.1234567.imsi",
+ .type = OSMO_MDNS_RFC_RECORD_TYPE_A,
+ .class = OSMO_MDNS_RFC_CLASS_IN,
+ .ttl = 1234,
+ .rdlength = 9,
+ .rdata = (uint8_t *)"10.42.2.1",
+ },
+};
+
+void test_enc_dec_rfc_record(void *ctx)
+{
+ int i;
+
+ fprintf(stderr, "-- %s --\n", __func__);
+ for (i=0; i< ARRAY_SIZE(record_enc_dec_test_data); i++) {
+ const struct osmo_mdns_rfc_record in = record_enc_dec_test_data[i];
+ struct osmo_mdns_rfc_record *out;
+ struct msgb *msg = msgb_alloc(4096, "dns_test");
+ size_t record_len;
+
+ PRINT_REC(&in, "in");
+ assert(osmo_mdns_rfc_record_encode(ctx, msg, &in) == 0);
+ fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
+ out = osmo_mdns_rfc_record_decode(ctx, msgb_data(msg), msgb_length(msg), &record_len);
+ fprintf(stderr, "record_len: %lu\n", record_len);
+ assert(out);
+ PRINT_REC(out, "out");
+
+ if (strcmp(in.domain, out->domain) != 0)
+ fprintf(stderr, "=> ERROR: domain does not match\n");
+ else if (in.type != out->type)
+ fprintf(stderr, "=> ERROR: type does not match\n");
+ else if (in.class != out->class)
+ fprintf(stderr, "=> ERROR: class does not match\n");
+ else if (in.ttl != out->ttl)
+ fprintf(stderr, "=> ERROR: ttl does not match\n");
+ else if (in.rdlength != out->rdlength)
+ fprintf(stderr, "=> ERROR: rdlength does not match\n");
+ else if (memcmp(in.rdata, out->rdata, in.rdlength) != 0)
+ fprintf(stderr, "=> ERROR: rdata does not match\n");
+ else
+ fprintf(stderr, "=> OK\n");
+
+ fprintf(stderr, "\n");
+ msgb_free(msg);
+ talloc_free(out);
+ }
+}
+
+static uint8_t ip_v4_n[] = {23, 42, 47, 11};
+static uint8_t ip_v6_n[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
+ 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00};
+
+
+enum test_records {
+ RECORD_NONE,
+ RECORD_A,
+ RECORD_AAAA,
+ RECORD_TXT_AGE,
+ RECORD_TXT_PORT_444,
+ RECORD_TXT_PORT_666,
+ RECORD_TXT_INVALID_KEY,
+ RECORD_TXT_INVALID_NO_KEY_VALUE,
+ RECORD_INVALID,
+};
+struct result_from_answer_test {
+ const char *desc;
+ const enum test_records records[5];
+ bool error;
+ const struct osmo_mslookup_result res;
+};
+
+static void test_result_from_answer(void *ctx)
+{
+ void *print_ctx = talloc_named_const(ctx, 0, __func__);
+ struct osmo_sockaddr_str test_host_v4 = {.af = AF_INET, .port=444, .ip = "23.42.47.11"};
+ struct osmo_sockaddr_str test_host_v6 = {.af = AF_INET6, .port=666,
+ .ip = "1122:3344:5566:7788:99aa:bbcc:ddee:ff00"};
+ struct osmo_mslookup_result test_result_v4 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
+ .host_v4 = test_host_v4};
+ struct osmo_mslookup_result test_result_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
+ .host_v6 = test_host_v6};
+ struct osmo_mslookup_result test_result_v4_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
+ .host_v4 = test_host_v4, .host_v6 = test_host_v6};
+ struct result_from_answer_test result_from_answer_data[] = {
+ {
+ .desc = "IPv4",
+ .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444},
+ .res = test_result_v4
+ },
+ {
+ .desc = "IPv6",
+ .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666},
+ .res = test_result_v6
+ },
+ {
+ .desc = "IPv4 + IPv6",
+ .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_AAAA, RECORD_TXT_PORT_666},
+ .res = test_result_v4_v6
+ },
+ {
+ .desc = "A twice",
+ .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_A},
+ .error = true
+ },
+ {
+ .desc = "AAAA twice",
+ .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_444, RECORD_AAAA},
+ .error = true
+ },
+ {
+ .desc = "invalid TXT: no key/value pair",
+ .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_NO_KEY_VALUE},
+ .error = true
+ },
+ {
+ .desc = "age twice",
+ .records = {RECORD_TXT_AGE, RECORD_TXT_AGE},
+ .error = true
+ },
+ {
+ .desc = "port as first record",
+ .records = {RECORD_TXT_PORT_444},
+ .error = true
+ },
+ {
+ .desc = "port without previous ip record",
+ .records = {RECORD_TXT_AGE, RECORD_TXT_PORT_444},
+ .error = true
+ },
+ {
+ .desc = "invalid TXT: invalid key",
+ .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_KEY},
+ .error = true
+ },
+ {
+ .desc = "unexpected record type",
+ .records = {RECORD_TXT_AGE, RECORD_INVALID},
+ .error = true
+ },
+ {
+ .desc = "missing record: age",
+ .records = {RECORD_A, RECORD_TXT_PORT_444},
+ .error = true
+ },
+ {
+ .desc = "missing record: port for ipv4",
+ .records = {RECORD_TXT_AGE, RECORD_A},
+ .error = true
+ },
+ {
+ .desc = "missing record: port for ipv4 #2",
+ .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666, RECORD_A},
+ .error = true
+ },
+ };
+ int i = 0;
+ int j = 0;
+
+ fprintf(stderr, "-- %s --\n", __func__);
+ for (i = 0; i < ARRAY_SIZE(result_from_answer_data); i++) {
+ struct result_from_answer_test *t = &result_from_answer_data[i];
+ struct osmo_mdns_msg_answer ans = {0};
+ struct osmo_mslookup_result res = {0};
+ void *ctx_test = talloc_named_const(ctx, 0, t->desc);
+ bool is_error;
+
+ fprintf(stderr, "---\n");
+ fprintf(stderr, "test: %s\n", t->desc);
+ fprintf(stderr, "error: %s\n", t->error ? "true" : "false");
+ fprintf(stderr, "records:\n");
+ /* Build records list */
+ INIT_LLIST_HEAD(&ans.records);
+ for (j = 0; j < ARRAY_SIZE(t->records); j++) {
+ struct osmo_mdns_record *rec = NULL;
+
+ switch (t->records[j]) {
+ case RECORD_NONE:
+ break;
+ case RECORD_A:
+ fprintf(stderr, "- A 42.42.42.42\n");
+ rec = talloc_zero(ctx_test, struct osmo_mdns_record);
+ rec->type = OSMO_MDNS_RFC_RECORD_TYPE_A;
+ rec->data = ip_v4_n;
+ rec->length = sizeof(ip_v4_n);
+ break;
+ case RECORD_AAAA:
+ fprintf(stderr, "- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00\n");
+ rec = talloc_zero(ctx_test, struct osmo_mdns_record);
+ rec->type = OSMO_MDNS_RFC_RECORD_TYPE_AAAA;
+ rec->data = ip_v6_n;
+ rec->length = sizeof(ip_v6_n);
+ break;
+ case RECORD_TXT_AGE:
+ fprintf(stderr, "- TXT age=3\n");
+ rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "age", "3");
+ break;
+ case RECORD_TXT_PORT_444:
+ fprintf(stderr, "- TXT port=444\n");
+ rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "444");
+ break;
+ case RECORD_TXT_PORT_666:
+ fprintf(stderr, "- TXT port=666\n");
+ rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "666");
+ break;
+ case RECORD_TXT_INVALID_KEY:
+ fprintf(stderr, "- TXT hello=world\n");
+ rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "hello", "world");
+ break;
+ case RECORD_TXT_INVALID_NO_KEY_VALUE:
+ fprintf(stderr, "- TXT 12345\n");
+ rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "12", "45");
+ rec->data[3] = '3';
+ break;
+ case RECORD_INVALID:
+ fprintf(stderr, "- (invalid)\n");
+ rec = talloc_zero(ctx, struct osmo_mdns_record);
+ rec->type = OSMO_MDNS_RFC_RECORD_TYPE_UNKNOWN;
+ break;
+ }
+
+ if (rec)
+ llist_add_tail(&rec->list, &ans.records);
+ }
+
+ /* Verify output */
+ is_error = (osmo_mdns_result_from_answer(&res, &ans) != 0);
+ if (t->error != is_error) {
+ fprintf(stderr, "got %s\n", is_error ? "error" : "no error");
+ OSMO_ASSERT(false);
+ }
+ if (!t->error) {
+ fprintf(stderr, "exp: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &t->res));
+ fprintf(stderr, "res: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &res));
+ OSMO_ASSERT(t->res.rc == res.rc);
+ OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v4, &res.host_v4));
+ OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v6, &res.host_v6));
+ OSMO_ASSERT(t->res.age == res.age);
+ OSMO_ASSERT(t->res.last == res.last);
+ }
+
+ talloc_free(ctx_test);
+ fprintf(stderr, "=> OK\n");
+ }
+}
+
+int main()
+{
+ void *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, 1);
+ log_set_print_category(osmo_stderr_target, 1);
+ log_set_print_category_hex(osmo_stderr_target, 0);
+ log_set_use_color(osmo_stderr_target, 0);
+
+ test_enc_dec_rfc_qname(ctx);
+ test_enc_dec_rfc_header();
+ test_enc_dec_rfc_header_einval();
+ test_enc_dec_rfc_question(ctx);
+ test_enc_dec_rfc_question_null(ctx);
+ test_enc_dec_rfc_record(ctx);
+
+ test_result_from_answer(ctx);
+
+ return 0;
+}
diff --git a/tests/mslookup/mdns_test.err b/tests/mslookup/mdns_test.err
new file mode 100644
index 0000000..51e5afe
--- /dev/null
+++ b/tests/mslookup/mdns_test.err
@@ -0,0 +1,336 @@
+-- test_enc_dec_rfc_qname --
+domain: "hlr.1234567.imsi"
+exp: "\3hlr\a1234567\4imsi"
+res: "\3hlr\a1234567\4imsi"
+=> OK
+
+qname: "\3hlr\a1234567\4imsi"
+exp: "hlr.1234567.imsi"
+res: "hlr.1234567.imsi"
+=> OK
+
+domain: "hlr..imsi"
+exp: NULL
+res: NULL
+=> OK
+
+domain: "hlr"
+exp: "\3hlr"
+res: "\3hlr"
+=> OK
+
+qname: "\3hlr"
+exp: "hlr"
+res: "hlr"
+=> OK
+
+domain: "hlr."
+exp: NULL
+res: NULL
+=> OK
+
+domain: ".hlr"
+exp: NULL
+res: NULL
+=> OK
+
+domain: ""
+exp: NULL
+res: NULL
+=> OK
+
+domain: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345"
+exp: "\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\512345"
+res: "\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\512345"
+=> OK
+
+qname: "\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\512345"
+exp: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345"
+res: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345"
+=> OK
+
+domain: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345toolong"
+exp: NULL
+res: NULL
+=> OK
+
+qname: "\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\t123456789\
+exp: NULL
+res: NULL
+=> OK
+
+qname: "\3hlr\a1234567\5imsi"
+exp: NULL
+res: NULL
+=> OK
+
+qname: "\2hlr\a1234567\4imsi"
+exp: NULL
+res: NULL
+=> OK
+
+qname: "\3hlr\3"
+exp: NULL
+res: NULL
+=> OK
+
+qname_max_len: 17
+qname: "\3hlr\a1234567\4imsi"
+exp: NULL
+res: NULL
+=> OK
+
+-- test_enc_dec_rfc_header --
+header in:
+.id = 1337
+.qr = 0
+.opcode = 0
+.aa = 0
+.tc = 0
+.rd = 0
+.ra = 0
+.z = 0
+.rcode = 0
+.qdcount = 1
+.ancount = 0
+.nscount = 0
+.arcount = 0
+encoded: 05 39 00 00 00 01 00 00 00 00 00 00
+header out:
+.id = 1337
+.qr = 0
+.opcode = 0
+.aa = 0
+.tc = 0
+.rd = 0
+.ra = 0
+.z = 0
+.rcode = 0
+.qdcount = 1
+.ancount = 0
+.nscount = 0
+.arcount = 0
+in (hexdump): 39 05 00 00 01 00 00 00 00 00 00 00
+out (hexdump): 39 05 00 00 01 00 00 00 00 00 00 00
+=> OK
+
+header in:
+.id = 42
+.qr = 1
+.opcode = 2
+.aa = 1
+.tc = 1
+.rd = 1
+.ra = 1
+.z = 2
+.rcode = 3
+.qdcount = 1234
+.ancount = 1111
+.nscount = 2222
+.arcount = 3333
+encoded: 00 2a 97 a3 04 d2 04 57 08 ae 0d 05
+header out:
+.id = 42
+.qr = 1
+.opcode = 2
+.aa = 1
+.tc = 1
+.rd = 1
+.ra = 1
+.z = 2
+.rcode = 3
+.qdcount = 1234
+.ancount = 1111
+.nscount = 2222
+.arcount = 3333
+in (hexdump): 2a 00 97 a3 d2 04 57 04 ae 08 05 0d
+out (hexdump): 2a 00 97 a3 d2 04 57 04 ae 08 05 0d
+=> OK
+
+-- test_enc_dec_rfc_header_einval --
+=> OK
+
+-- test_enc_dec_rfc_question --
+question in:
+.domain = hlr.1234567.imsi
+.qtype = 255
+.qclass = 1
+encoded: 03 68 6c 72 07 31 32 33 34 35 36 37 04 69 6d 73 69 00 00 ff 00 01
+question out:
+.domain = hlr.1234567.imsi
+.qtype = 255
+.qclass = 1
+=> OK
+
+question in:
+.domain = hlr.1234567.imsi
+.qtype = 1
+.qclass = 255
+encoded: 03 68 6c 72 07 31 32 33 34 35 36 37 04 69 6d 73 69 00 00 01 00 ff
+question out:
+.domain = hlr.1234567.imsi
+.qtype = 1
+.qclass = 255
+=> OK
+
+question in:
+.domain = hlr.1234567.imsi
+.qtype = 28
+.qclass = 255
+encoded: 03 68 6c 72 07 31 32 33 34 35 36 37 04 69 6d 73 69 00 00 1c 00 ff
+question out:
+.domain = hlr.1234567.imsi
+.qtype = 28
+.qclass = 255
+=> OK
+
+-- test_enc_dec_rfc_question_null --
+=> OK
+
+-- test_enc_dec_rfc_record --
+question in:
+.domain = hlr.1234567.imsi
+.type = 1
+.class = 1
+.ttl = 1234
+.rdlength = 9
+.rdata = "10.42.2.1"
+encoded: 03 68 6c 72 07 31 32 33 34 35 36 37 04 69 6d 73 69 00 00 01 00 01 00 00 04 d2 00 09 31 30 2e 34 32 2e 32 2e 31
+record_len: 37
+question out:
+.domain = hlr.1234567.imsi
+.type = 1
+.class = 1
+.ttl = 1234
+.rdlength = 9
+.rdata = "10.42.2.1"
+=> OK
+
+-- test_result_from_answer --
+---
+test: IPv4
+error: false
+records:
+- TXT age=3
+- A 42.42.42.42
+- TXT port=444
+exp: -> ipv4: 23.42.47.11:444 (age=3) (not-last)
+res: -> ipv4: 23.42.47.11:444 (age=3) (not-last)
+=> OK
+---
+test: IPv6
+error: false
+records:
+- TXT age=3
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT port=666
+exp: -> ipv6: [1122:3344:5566:7788:99aa:bbcc:ddee:ff00]:666 (age=3) (not-last)
+res: -> ipv6: [1122:3344:5566:7788:99aa:bbcc:ddee:ff00]:666 (age=3) (not-last)
+=> OK
+---
+test: IPv4 + IPv6
+error: false
+records:
+- TXT age=3
+- A 42.42.42.42
+- TXT port=444
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT port=666
+exp: -> ipv4: 23.42.47.11:444 -> ipv6: [1122:3344:5566:7788:99aa:bbcc:ddee:ff00]:666 (age=3) (not-last)
+res: -> ipv4: 23.42.47.11:444 -> ipv6: [1122:3344:5566:7788:99aa:bbcc:ddee:ff00]:666 (age=3) (not-last)
+=> OK
+---
+test: A twice
+error: true
+records:
+- TXT age=3
+- A 42.42.42.42
+- TXT port=444
+- A 42.42.42.42
+DLGLOBAL ERROR 'A' record found twice in mDNS answer
+=> OK
+---
+test: AAAA twice
+error: true
+records:
+- TXT age=3
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT port=444
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+DLGLOBAL ERROR 'AAAA' record found twice in mDNS answer
+=> OK
+---
+test: invalid TXT: no key/value pair
+error: true
+records:
+- TXT age=3
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT 12345
+DLGLOBAL ERROR failed to decode txt record
+=> OK
+---
+test: age twice
+error: true
+records:
+- TXT age=3
+- TXT age=3
+DLGLOBAL ERROR duplicate 'TXT' record for 'age'
+=> OK
+---
+test: port as first record
+error: true
+records:
+- TXT port=444
+DLGLOBAL ERROR 'TXT' record for 'port' without previous 'A' or 'AAAA' record
+=> OK
+---
+test: port without previous ip record
+error: true
+records:
+- TXT age=3
+- TXT port=444
+DLGLOBAL ERROR 'TXT' record for 'port' without previous 'A' or 'AAAA' record
+=> OK
+---
+test: invalid TXT: invalid key
+error: true
+records:
+- TXT age=3
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT hello=world
+DLGLOBAL ERROR unexpected key 'hello' in TXT record
+=> OK
+---
+test: unexpected record type
+error: true
+records:
+- TXT age=3
+- (invalid)
+DLGLOBAL ERROR unexpected record type
+=> OK
+---
+test: missing record: age
+error: true
+records:
+- A 42.42.42.42
+- TXT port=444
+DLGLOBAL ERROR missing resource records in mDNS answer
+=> OK
+---
+test: missing record: port for ipv4
+error: true
+records:
+- TXT age=3
+- A 42.42.42.42
+DLGLOBAL ERROR missing resource records in mDNS answer
+=> OK
+---
+test: missing record: port for ipv4 #2
+error: true
+records:
+- TXT age=3
+- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00
+- TXT port=666
+- A 42.42.42.42
+DLGLOBAL ERROR missing resource records in mDNS answer
+=> OK
diff --git a/tests/mslookup/mslookup_client_mdns_test.c b/tests/mslookup/mslookup_client_mdns_test.c
new file mode 100644
index 0000000..c54250b
--- /dev/null
+++ b/tests/mslookup/mslookup_client_mdns_test.c
@@ -0,0 +1,219 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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 <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/hlr/logging.h>
+#include <osmocom/mslookup/mslookup.h>
+#include <osmocom/mslookup/mslookup_client.h>
+#include <osmocom/mslookup/mslookup_client_mdns.h>
+#include <osmocom/mslookup/mdns.h>
+#include <osmocom/mslookup/mdns_sock.h>
+
+void *ctx = NULL;
+
+#define TEST_IP OSMO_MSLOOKUP_MDNS_IP4
+#define TEST_PORT OSMO_MSLOOKUP_MDNS_PORT
+
+/*
+ * Test server (emulates the mDNS server in OsmoHLR) and client
+ */
+struct osmo_mdns_sock *server_mc;
+
+
+static void server_reply(struct osmo_mslookup_query *query, uint16_t packet_id)
+{
+ struct osmo_mslookup_result result = {0};
+ struct msgb *msg;
+
+ result.rc = OSMO_MSLOOKUP_RC_RESULT;
+ result.age = 3;
+ osmo_sockaddr_str_from_str(&result.host_v4, "42.42.42.42", 444);
+ osmo_sockaddr_str_from_str(&result.host_v6, "1122:3344:5566:7788:99aa:bbcc:ddee:ff00", 666);
+
+ msg = osmo_mdns_result_encode(ctx, packet_id, query, &result);
+ OSMO_ASSERT(msg);
+ OSMO_ASSERT(osmo_mdns_sock_send(server_mc, msg) == 0);
+}
+
+static int server_recv(struct osmo_fd *osmo_fd, unsigned int what)
+{
+ int n;
+ uint8_t buffer[1024];
+ uint16_t packet_id;
+ struct osmo_mslookup_query *query;
+
+ fprintf(stderr, "%s\n", __func__);
+
+ /* Parse the message and print it */
+ n = read(osmo_fd->fd, buffer, sizeof(buffer));
+ OSMO_ASSERT(n >= 0);
+
+ query = osmo_mdns_query_decode(ctx, buffer, n, &packet_id);
+ if (!query)
+ return -1; /* server receiving own answer is expected */
+
+ fprintf(stderr, "received request\n");
+ server_reply(query, packet_id);
+ talloc_free(query);
+ return n;
+}
+
+static void server_init()
+{
+ fprintf(stderr, "%s\n", __func__);
+ server_mc = osmo_mdns_sock_init(ctx, TEST_IP, TEST_PORT, server_recv, NULL, 0);
+ OSMO_ASSERT(server_mc);
+}
+
+static void server_stop()
+{
+ fprintf(stderr, "%s\n", __func__);
+ OSMO_ASSERT(server_mc);
+ osmo_mdns_sock_cleanup(server_mc);
+ server_mc = NULL;
+}
+
+struct osmo_mslookup_client* client;
+struct osmo_mslookup_client_method* client_method;
+
+static void client_init()
+{
+ fprintf(stderr, "%s\n", __func__);
+ client = osmo_mslookup_client_new(ctx);
+ OSMO_ASSERT(client);
+ client_method = osmo_mslookup_client_add_mdns(client, TEST_IP, TEST_PORT, 1337);
+ OSMO_ASSERT(client_method);
+}
+
+static void client_recv(struct osmo_mslookup_client *client, uint32_t request_handle,
+ const struct osmo_mslookup_query *query, const struct osmo_mslookup_result *result)
+{
+ char buf[256];
+ fprintf(stderr, "%s\n", __func__);
+ fprintf(stderr, "client_recv(): %s\n", osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
+
+ osmo_mslookup_client_request_cancel(client, request_handle);
+}
+
+static void client_query()
+{
+ struct osmo_mslookup_id id = {.type = OSMO_MSLOOKUP_ID_IMSI,
+ .imsi = "123456789012345"};
+ const struct osmo_mslookup_query query = {
+ .service = OSMO_MSLOOKUP_SERVICE_HLR_GSUP,
+ .id = id,
+ };
+ struct osmo_mslookup_query_handling handling = {
+ .result_timeout_milliseconds = 2000,
+ .result_cb = client_recv,
+ };
+
+ fprintf(stderr, "%s\n", __func__);
+ osmo_mslookup_client_request(client, &query, &handling);
+}
+
+static void client_stop()
+{
+ fprintf(stderr, "%s\n", __func__);
+ osmo_mslookup_client_free(client);
+ client = NULL;
+}
+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 test_server_client()
+{
+ fprintf(stderr, "-- %s --\n", __func__);
+ server_init();
+ client_init();
+ client_query();
+
+ /* Let the server receive the query and indirectly call server_recv(). As side effect of using the same IP and
+ * port, the client will also receive its own question. The client will dismiss its own question, as it is just
+ * looking for answers. */
+ OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
+
+ /* Let the mslookup client receive the answer (also same side effect as above). It does not call the callback
+ * (client_recv()) just yet, because it is waiting for the best result within two seconds. */
+ OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
+
+ /* Time flies by, client_recv() gets called. */
+ fake_time_passes(5, 0);
+
+ server_stop();
+ client_stop();
+}
+
+/*
+ * Run all tests
+ */
+int main()
+{
+ talloc_enable_null_tracking();
+ 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();
+
+ test_server_client();
+
+ log_fini();
+
+ OSMO_ASSERT(talloc_total_blocks(ctx) == 1);
+ talloc_free(ctx);
+ OSMO_ASSERT(talloc_total_blocks(NULL) == 1);
+ talloc_disable_null_tracking();
+
+ return 0;
+}
diff --git a/tests/mslookup/mslookup_client_mdns_test.err b/tests/mslookup/mslookup_client_mdns_test.err
new file mode 100644
index 0000000..b4ea269
--- /dev/null
+++ b/tests/mslookup/mslookup_client_mdns_test.err
@@ -0,0 +1,14 @@
+Total time passed: 0.000000 s
+-- test_server_client --
+server_init
+client_init
+client_query
+sending mDNS query: gsup.hlr.123456789012345.imsi
+server_recv
+received request
+server_recv
+client_recv
+client_recv(): gsup.hlr.123456789012345.imsi -> ipv4: 42.42.42.42:444 -> ipv6: [1122:3344:5566:7788:99aa:bbcc:ddee:ff00]:666 (age=3) (not-last)
+Total time passed: 5.000000 s
+server_stop
+client_stop
diff --git a/tests/mslookup/mslookup_client_test.c b/tests/mslookup/mslookup_client_test.c
new file mode 100644
index 0000000..96b5846
--- /dev/null
+++ b/tests/mslookup/mslookup_client_test.c
@@ -0,0 +1,242 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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>
+
+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 = OSMO_MSLOOKUP_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 = OSMO_MSLOOKUP_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 = OSMO_MSLOOKUP_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 = OSMO_MSLOOKUP_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 = OSMO_MSLOOKUP_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_delay_milliseconds */
+ .min_delay_milliseconds = 2000,
+ .result_cb = result_cb_once,
+ };
+
+ struct osmo_mslookup_query q1 = {
+ .service = OSMO_MSLOOKUP_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 = OSMO_MSLOOKUP_SERVICE_SIP,
+ .id = {
+ .type = OSMO_MSLOOKUP_ID_MSISDN,
+ .msisdn = "112",
+ },
+ };
+ handling.min_delay_milliseconds = 3000;
+ OSMO_ASSERT(osmo_mslookup_client_request(client, &q2, &handling));
+
+ struct osmo_mslookup_query q3 = {
+ .service = OSMO_MSLOOKUP_SERVICE_SMPP,
+ .id = {
+ .type = OSMO_MSLOOKUP_ID_MSISDN,
+ .msisdn = "00000",
+ },
+ };
+ handling.min_delay_milliseconds = 5000;
+ OSMO_ASSERT(osmo_mslookup_client_request(client, &q3, &handling));
+
+ struct osmo_mslookup_query q4 = {
+ .service = OSMO_MSLOOKUP_SERVICE_HLR_GSUP,
+ .id = {
+ .type = OSMO_MSLOOKUP_ID_MSISDN,
+ .msisdn = "666",
+ },
+ };
+ handling.min_delay_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..302e6b7
--- /dev/null
+++ b/tests/mslookup/mslookup_test.c
@@ -0,0 +1,88 @@
+/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info at 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_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_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_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..827e9f8 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -39,3 +39,27 @@
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([mdns])
+AT_KEYWORDS([mdns])
+cat $abs_srcdir/mslookup/mdns_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/mslookup/mdns_test], [0], [ignore], [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
+
+AT_SETUP([mslookup_client_mdns])
+AT_KEYWORDS([mslookup_client_mdns])
+cat $abs_srcdir/mslookup/mslookup_client_mdns_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/mslookup/mslookup_client_mdns_test], [0], [ignore], [experr])
+AT_CLEANUP
--
To view, visit https://gerrit.osmocom.org/c/osmo-hlr/+/16202
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Change-Id: I83487ab8aad1611eb02e997dafbcb8344da13df1
Gerrit-Change-Number: 16202
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20191125/91b8743e/attachment.htm>