Change in osmo-hlr[master]: 2/2: wrap ipa_name in osmo_cni_peer_id with type enum and union

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/.

laforge gerrit-no-reply at lists.osmocom.org
Fri May 1 14:37:33 UTC 2020


laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-hlr/+/16459 )

Change subject: 2/2: wrap ipa_name in osmo_cni_peer_id with type enum and union
......................................................................

2/2: wrap ipa_name in osmo_cni_peer_id with type enum and union

To be prepared for the future in public API, wrap the new osmo_ipa_name struct
in an enum-type and union called osmo_cni_peer.

During code review it was requested to insert an ability to handle different
kinds of peer id, in order to be able to add a Global Title in the future.

Use the generic osmo_cni_peer only in the publicly visible API. For osmo-hlr
internal code, I intend to postpone implementing this into the future, when a
different peer identification actually gets introduced.

This way we don't need to implement it now in all osmo-hlr code paths (save
time now), but still make all external API users aware that this type may be
extended in the future.

Change-Id: Ide9dcdca283ab989240cfc6e53e9211862a199c5
---
M include/Makefile.am
A include/osmocom/gsupclient/cni_peer_id.h
M include/osmocom/gsupclient/gsup_req.h
D include/osmocom/gsupclient/ipa_name.h
M include/osmocom/hlr/db.h
M include/osmocom/hlr/gsup_server.h
M src/Makefile.am
M src/db_hlr.c
M src/gsup_server.c
M src/gsupclient/Makefile.am
A src/gsupclient/cni_peer_id.c
M src/gsupclient/gsup_req.c
D src/gsupclient/ipa_name.c
M src/hlr.c
M src/hlr_ussd.c
M src/lu_fsm.c
M tests/db/Makefile.am
M tests/db/db_test.c
M tests/gsup_server/Makefile.am
19 files changed, 346 insertions(+), 181 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/include/Makefile.am b/include/Makefile.am
index aeeb03f..e93fe29 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,7 +1,7 @@
 SUBDIRS = osmocom
 
 nobase_include_HEADERS = \
-	osmocom/gsupclient/ipa_name.h \
+	osmocom/gsupclient/cni_peer_id.h \
 	osmocom/gsupclient/gsup_client.h \
 	osmocom/gsupclient/gsup_req.h \
 	osmocom/mslookup/mdns.h \
diff --git a/include/osmocom/gsupclient/cni_peer_id.h b/include/osmocom/gsupclient/cni_peer_id.h
new file mode 100644
index 0000000..6e58d06
--- /dev/null
+++ b/include/osmocom/gsupclient/cni_peer_id.h
@@ -0,0 +1,66 @@
+/* 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 General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#include <unistd.h>
+#include <stdint.h>
+#include <osmocom/core/utils.h>
+
+/*! IPA Name: Arbitrary length blob, not necessarily zero-terminated.
+ * In osmo-hlr, struct hlr_subscriber is mostly used as static reference and cannot serve as talloc context, which is
+ * why this is also implemented as a fixed-maximum-size buffer instead of a talloc'd arbitrary sized buffer.
+ * NOTE: The length of val may be extended in the future if it becomes necessary.
+ * At the time of writing, this holds IPA unit name strings of very limited length.
+ */
+struct osmo_ipa_name {
+	size_t len;
+	uint8_t val[128];
+};
+
+bool osmo_ipa_name_is_empty(const struct osmo_ipa_name *ipa_name);
+int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len);
+int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...);
+int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b);
+const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name);
+const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name);
+
+enum osmo_cni_peer_id_type {
+	OSMO_CNI_PEER_ID_EMPTY=0,
+	OSMO_CNI_PEER_ID_IPA_NAME,
+	/* OSMO_CNI_PEER_ID_GLOBAL_TITLE, <-- currently not implemented, but likely future possibility */
+};
+
+extern const struct value_string osmo_cni_peer_id_type_names[];
+static inline const char *osmo_cni_peer_id_type_name(enum osmo_cni_peer_id_type val)
+{ return get_value_string(osmo_cni_peer_id_type_names, val); }
+
+struct osmo_cni_peer_id {
+	enum osmo_cni_peer_id_type type;
+	union {
+		struct osmo_ipa_name ipa_name;
+	};
+};
+
+bool osmo_cni_peer_id_is_empty(const struct osmo_cni_peer_id *cni_peer_id);
+int osmo_cni_peer_id_set(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
+			  const uint8_t *val, size_t len);
+int osmo_cni_peer_id_set_str(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
+			      const char *str_fmt, ...);
+int osmo_cni_peer_id_cmp(const struct osmo_cni_peer_id *a, const struct osmo_cni_peer_id *b);
+const char *osmo_cni_peer_id_to_str(const struct osmo_cni_peer_id *cni_peer_id);
+const char *osmo_cni_peer_id_to_str_c(void *ctx, const struct osmo_cni_peer_id *cni_peer_id);
diff --git a/include/osmocom/gsupclient/gsup_req.h b/include/osmocom/gsupclient/gsup_req.h
index c61483b..2d3cf38 100644
--- a/include/osmocom/gsupclient/gsup_req.h
+++ b/include/osmocom/gsupclient/gsup_req.h
@@ -19,14 +19,14 @@
 #pragma once
 
 #include <osmocom/gsm/gsup.h>
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 
 struct osmo_gsup_req;
 
 #define LOG_GSUP_REQ_CAT_SRC(req, subsys, level, file, line, fmt, args...) \
 	LOGPSRC(subsys, level, file, line, "GSUP %u: %s: IMSI-%s %s: " fmt, \
 		(req) ? (req)->nr : 0, \
-		(req) ? osmo_ipa_name_to_str(&(req)->source_name) : "NULL", \
+		(req) ? osmo_cni_peer_id_to_str(&(req)->source_name) : "NULL", \
 		(req) ? (req)->gsup.imsi : "NULL", \
 		(req) ? osmo_gsup_message_type_name((req)->gsup.message_type) : "NULL", \
 		##args)
@@ -56,11 +56,11 @@
 	/* The ultimate source of this message: the source_name form the GSUP message, or, if not present, then the
 	 * immediate GSUP peer. GSUP messages going via a proxy reflect the initial source in the source_name.
 	 * This source_name is implicitly added to the routes for the conn the message was received on. */
-	struct osmo_ipa_name source_name;
+	struct osmo_cni_peer_id source_name;
 
 	/* If the source_name is not an immediate GSUP peer, this is set to the closest intermediate peer between here
 	 * and source_name. */
-	struct osmo_ipa_name via_proxy;
+	struct osmo_cni_peer_id via_proxy;
 
 	/* Identify this request by number, for logging. */
 	unsigned int nr;
@@ -82,7 +82,7 @@
 	struct msgb *msg;
 };
 
-struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg,
+struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_cni_peer_id *from_peer, struct msgb *msg,
 					osmo_gsup_req_send_response_t send_response_cb, void *cb_data,
 					struct llist_head *add_to_list);
 void osmo_gsup_req_free(struct osmo_gsup_req *req);
diff --git a/include/osmocom/gsupclient/ipa_name.h b/include/osmocom/gsupclient/ipa_name.h
deleted file mode 100644
index 73cd1fb..0000000
--- a/include/osmocom/gsupclient/ipa_name.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/* 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 General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-#include <unistd.h>
-#include <stdint.h>
-
-/*! IPA Name: Arbitrary length blob, not necessarily zero-terminated.
- * In osmo-hlr, struct hlr_subscriber is mostly used as static reference and cannot serve as talloc context, which is
- * why this is also implemented as a fixed-maximum-size buffer instead of a talloc'd arbitrary sized buffer.
- * NOTE: The length of val may be extended in the future if it becomes necessary.
- * At the time of writing, this holds IPA unit name strings of very limited length.
- */
-struct osmo_ipa_name {
-	size_t len;
-	uint8_t val[128];
-};
-
-int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len);
-int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...);
-int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b);
-const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name);
-const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name);
diff --git a/include/osmocom/hlr/db.h b/include/osmocom/hlr/db.h
index 5c627be..9be88d8 100644
--- a/include/osmocom/hlr/db.h
+++ b/include/osmocom/hlr/db.h
@@ -3,7 +3,7 @@
 #include <stdbool.h>
 #include <sqlite3.h>
 
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 
 struct hlr;
 
diff --git a/include/osmocom/hlr/gsup_server.h b/include/osmocom/hlr/gsup_server.h
index 149971a..774f750 100644
--- a/include/osmocom/hlr/gsup_server.h
+++ b/include/osmocom/hlr/gsup_server.h
@@ -5,7 +5,7 @@
 #include <osmocom/abis/ipa.h>
 #include <osmocom/abis/ipaccess.h>
 #include <osmocom/gsm/gsup.h>
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 #include <osmocom/gsupclient/gsup_req.h>
 
 #ifndef OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN
diff --git a/src/Makefile.am b/src/Makefile.am
index bfbe775..fec5275 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -72,7 +72,7 @@
 	logging.c \
 	rand_urandom.c \
 	dbd_decode_binary.c \
-	$(srcdir)/gsupclient/ipa_name.c \
+	$(srcdir)/gsupclient/cni_peer_id.c \
 	$(NULL)
 
 osmo_hlr_db_tool_LDADD = \
diff --git a/src/db_hlr.c b/src/db_hlr.c
index fdac75f..ec8f896 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -37,7 +37,7 @@
 #include <osmocom/hlr/logging.h>
 #include <osmocom/hlr/hlr.h>
 #include <osmocom/hlr/db.h>
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 
 #define LOGHLR(imsi, level, fmt, args ...)	LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args)
 
diff --git a/src/gsup_server.c b/src/gsup_server.c
index ba2d456..e9418d4 100644
--- a/src/gsup_server.c
+++ b/src/gsup_server.c
@@ -65,13 +65,21 @@
 static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response)
 {
 	struct osmo_gsup_server *server = req->cb_data;
-	struct osmo_gsup_conn *conn;
+	struct osmo_gsup_conn *conn = NULL;
 	struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
 	int rc;
 
-	conn = gsup_route_find_by_ipa_name(server, &req->source_name);
+	switch (req->source_name.type) {
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		conn = gsup_route_find_by_ipa_name(server, &req->source_name.ipa_name);
+		break;
+	default:
+		LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
+			     osmo_cni_peer_id_type_name(req->source_name.type));
+		break;
+	}
 	if (!conn) {
-		LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request was disconnected, cannot respond\n");
+		LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n");
 		msgb_free(msg);
 		return;
 	}
@@ -91,19 +99,34 @@
 
 struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg)
 {
-	struct osmo_gsup_req *req = osmo_gsup_req_new(conn->server, &conn->peer_name, msg, gsup_server_send_req_response,
-						      conn->server, NULL);
+	struct osmo_gsup_req *req;
+	struct osmo_cni_peer_id cpi = {
+		.type = OSMO_CNI_PEER_ID_IPA_NAME,
+		.ipa_name = conn->peer_name,
+	};
+
+	req = osmo_gsup_req_new(conn->server, &cpi, msg, gsup_server_send_req_response, conn->server, NULL);
 	if (!req)
 		return NULL;
 
-	if (req->via_proxy.len) {
+	if (!osmo_cni_peer_id_is_empty(&req->via_proxy)) {
+		switch (req->via_proxy.type) {
+		case OSMO_CNI_PEER_ID_IPA_NAME:
+			break;
+		default:
+			LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
+				     osmo_cni_peer_id_type_name(req->source_name.type));
+			osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
+			return NULL;
+		}
+
 		/* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that
 		 * source. Add it to the routes for this conn (so we can route responses back). */
-		if (gsup_route_add_ipa_name(conn, &req->source_name)) {
+		if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) {
 			LOG_GSUP_REQ(req, LOGL_ERROR,
 				     "GSUP message received from %s via peer %s, but there already exists a"
 				     " different route to this source, message is not routable\n",
-				     osmo_ipa_name_to_str(&req->source_name),
+				     osmo_cni_peer_id_to_str(&req->source_name),
 				     osmo_ipa_name_to_str(&conn->peer_name));
 			osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
 			return NULL;
diff --git a/src/gsupclient/Makefile.am b/src/gsupclient/Makefile.am
index 38b1582..3412c40 100644
--- a/src/gsupclient/Makefile.am
+++ b/src/gsupclient/Makefile.am
@@ -9,7 +9,7 @@
 lib_LTLIBRARIES = libosmo-gsup-client.la
 
 libosmo_gsup_client_la_SOURCES = \
-	ipa_name.c \
+	cni_peer_id.c \
 	gsup_client.c \
 	gsup_req.c \
 	$(NULL)
diff --git a/src/gsupclient/cni_peer_id.c b/src/gsupclient/cni_peer_id.c
new file mode 100644
index 0000000..de7b4bc
--- /dev/null
+++ b/src/gsupclient/cni_peer_id.c
@@ -0,0 +1,187 @@
+/* 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 General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
+
+bool osmo_ipa_name_is_empty(const struct osmo_ipa_name *ipa_name)
+{
+	return (!ipa_name) || (!ipa_name->len);
+}
+
+int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len)
+{
+	if (!val || !len) {
+		*ipa_name = (struct osmo_ipa_name){};
+		return 0;
+	}
+	if (len > sizeof(ipa_name->val))
+		return -ENOSPC;
+	ipa_name->len = len;
+	memcpy(ipa_name->val, val, len);
+	return 0;
+}
+
+static int osmo_ipa_name_set_str_va(struct osmo_ipa_name *ipa_name, const char *str_fmt, va_list ap)
+{
+	if (!str_fmt)
+		return osmo_ipa_name_set(ipa_name, NULL, 0);
+	vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap);
+	ipa_name->len = strlen((char*)(ipa_name->val))+1;
+	return 0;
+}
+
+int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...)
+{
+	va_list ap;
+	int rc;
+	va_start(ap, str_fmt);
+	rc = osmo_ipa_name_set_str_va(ipa_name, str_fmt, ap);
+	va_end(ap);
+	return rc;
+}
+
+int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b)
+{
+	int cmp;
+	if (a == b)
+		return 0;
+	if (!a)
+		return -1;
+	if (!b)
+		return 1;
+	if (!a->len && !b->len)
+		return 0;
+	if (!a->len && b->len)
+		return -1;
+	if (!b->len && a->len)
+		return 1;
+
+	if (a->len == b->len)
+		return memcmp(a->val, b->val, a->len);
+	else if (a->len < b->len) {
+		cmp = memcmp(a->val, b->val, a->len);
+		if (!cmp)
+			cmp = -1;
+		return cmp;
+	} else {
+		/* a->len > b->len */
+		cmp = memcmp(a->val, b->val, b->len);
+		if (!cmp)
+			cmp = 1;
+		return cmp;
+	}
+}
+
+/* Call osmo_ipa_name_to_str_c with OTC_SELECT. */
+const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name)
+{
+	return osmo_ipa_name_to_str_c(OTC_SELECT, ipa_name);
+}
+
+/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */
+const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name)
+{
+	size_t len = ipa_name->len;
+	if (!len)
+		return talloc_strdup(ctx, "");
+	if (ipa_name->val[len-1] == '\0')
+		len--;
+	return osmo_escape_str_c(ctx, (char*)ipa_name->val, len);
+}
+
+bool osmo_cni_peer_id_is_empty(const struct osmo_cni_peer_id *cni_peer_id)
+{
+	if (!cni_peer_id)
+		return true;
+	switch (cni_peer_id->type) {
+	case OSMO_CNI_PEER_ID_EMPTY:
+		return true;
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		return osmo_ipa_name_is_empty(&cni_peer_id->ipa_name);
+	default:
+		return false;
+	}
+}
+int osmo_cni_peer_id_set(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
+			  const uint8_t *val, size_t len)
+{
+	cni_peer_id->type = type;
+	switch (type) {
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		return osmo_ipa_name_set(&cni_peer_id->ipa_name, val, len);
+	default:
+		return -EINVAL;
+	}
+}
+
+int osmo_cni_peer_id_set_str(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
+			      const char *str_fmt, ...)
+{
+	va_list ap;
+	int rc;
+
+	*cni_peer_id = (struct osmo_cni_peer_id){};
+
+	switch (type) {
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		cni_peer_id->type = OSMO_CNI_PEER_ID_IPA_NAME;
+		va_start(ap, str_fmt);
+		rc = osmo_ipa_name_set_str_va(&cni_peer_id->ipa_name, str_fmt, ap);
+		va_end(ap);
+		return rc;
+	default:
+		return -EINVAL;
+	}
+}
+
+int osmo_cni_peer_id_cmp(const struct osmo_cni_peer_id *a, const struct osmo_cni_peer_id *b)
+{
+	if (a->type != b->type)
+		return OSMO_CMP(a->type, b->type);
+	switch (a->type) {
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		return osmo_ipa_name_cmp(&a->ipa_name, &b->ipa_name);
+	default:
+		return -EINVAL;
+	}
+}
+
+const struct value_string osmo_cni_peer_id_type_names[] = {
+	{ OSMO_CNI_PEER_ID_IPA_NAME, "IPA-name" },
+	{}
+};
+
+/* Call osmo_cni_peer_id_to_str_c with OTC_SELECT */
+const char *osmo_cni_peer_id_to_str(const struct osmo_cni_peer_id *cpi)
+{
+	return osmo_cni_peer_id_to_str_c(OTC_SELECT, cpi);
+}
+
+/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */
+const char *osmo_cni_peer_id_to_str_c(void *ctx, const struct osmo_cni_peer_id *cpi)
+{
+	switch (cpi->type) {
+	case OSMO_CNI_PEER_ID_IPA_NAME:
+		return osmo_ipa_name_to_str_c(ctx, &cpi->ipa_name);
+	default:
+		return talloc_strdup(ctx, osmo_cni_peer_id_type_name(cpi->type));
+	}
+}
diff --git a/src/gsupclient/gsup_req.c b/src/gsupclient/gsup_req.c
index 4a2ff23..a8a66d5 100644
--- a/src/gsupclient/gsup_req.c
+++ b/src/gsupclient/gsup_req.c
@@ -99,7 +99,7 @@
  * \return a newly allocated osmo_gsup_req, or NULL on error. If NULL is returned, an error response has already been
  *         dispatched to the send_response_cb.
  */
-struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg,
+struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_cni_peer_id *from_peer, struct msgb *msg,
 					osmo_gsup_req_send_response_t send_response_cb, void *cb_data,
 					struct llist_head *add_to_list)
 {
@@ -109,7 +109,7 @@
 
 	if (!msgb_l2(msg) || !msgb_l2len(msg)) {
 		LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: missing or empty L2 data\n",
-		     osmo_ipa_name_to_str(from_peer));
+		     osmo_cni_peer_id_to_str(from_peer));
 		msgb_free(msg);
 		return NULL;
 	}
@@ -125,7 +125,7 @@
 		req->source_name = *from_peer;
 	rc = osmo_gsup_decode(msgb_l2(req->msg), msgb_l2len(req->msg), (struct osmo_gsup_message*)&req->gsup);
 	if (rc < 0) {
-		LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_ipa_name_to_str(from_peer), rc);
+		LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_cni_peer_id_to_str(from_peer), rc);
 		osmo_gsup_req_free(req);
 		return NULL;
 	}
@@ -133,17 +133,18 @@
 	LOG_GSUP_REQ(req, LOGL_DEBUG, "new request: {%s}\n", osmo_gsup_message_to_str_c(OTC_SELECT, &req->gsup));
 
 	if (req->gsup.source_name_len) {
-		if (osmo_ipa_name_set(&req->source_name, req->gsup.source_name, req->gsup.source_name_len)) {
+		if (osmo_cni_peer_id_set(&req->source_name, OSMO_CNI_PEER_ID_IPA_NAME,
+					  req->gsup.source_name, req->gsup.source_name_len)) {
 			LOGP(DLGSUP, LOGL_ERROR,
 			     "Rx GSUP from %s: failed to decode source_name, message is not routable\n",
-			     osmo_ipa_name_to_str(from_peer));
+			     osmo_cni_peer_id_to_str(from_peer));
 			osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
 			return NULL;
 		}
 
 		/* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source.
 		 */
-		if (osmo_ipa_name_cmp(&req->source_name, from_peer))
+		if (osmo_cni_peer_id_cmp(&req->source_name, from_peer))
 			req->via_proxy = *from_peer;
 	}
 
diff --git a/src/gsupclient/ipa_name.c b/src/gsupclient/ipa_name.c
deleted file mode 100644
index 2db069f..0000000
--- a/src/gsupclient/ipa_name.c
+++ /dev/null
@@ -1,97 +0,0 @@
-/* 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 General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <errno.h>
-#include <string.h>
-#include <osmocom/core/utils.h>
-#include <osmocom/gsupclient/ipa_name.h>
-
-int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len)
-{
-	if (!val || !len) {
-		*ipa_name = (struct osmo_ipa_name){};
-		return 0;
-	}
-	if (len > sizeof(ipa_name->val))
-		return -ENOSPC;
-	ipa_name->len = len;
-	memcpy(ipa_name->val, val, len);
-	return 0;
-}
-
-int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...)
-{
-	va_list ap;
-	if (!str_fmt)
-		return osmo_ipa_name_set(ipa_name, NULL, 0);
-
-	va_start(ap, str_fmt);
-	vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap);
-	va_end(ap);
-	ipa_name->len = strlen((char*)(ipa_name->val))+1;
-	return 0;
-}
-
-int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b)
-{
-	int cmp;
-	if (a == b)
-		return 0;
-	if (!a)
-		return -1;
-	if (!b)
-		return 1;
-	if (!a->len && !b->len)
-		return 0;
-	if (!a->len && b->len)
-		return -1;
-	if (!b->len && a->len)
-		return 1;
-
-	if (a->len == b->len)
-		return memcmp(a->val, b->val, a->len);
-	else if (a->len < b->len) {
-		cmp = memcmp(a->val, b->val, a->len);
-		if (!cmp)
-			cmp = -1;
-		return cmp;
-	} else {
-		/* a->len > b->len */
-		cmp = memcmp(a->val, b->val, b->len);
-		if (!cmp)
-			cmp = 1;
-		return cmp;
-	}
-}
-
-/* Call osmo_ipa_name_to_str_c with OTC_SELECT. */
-const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name)
-{
-	return osmo_ipa_name_to_str_c(OTC_SELECT, ipa_name);
-}
-
-/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */
-const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name)
-{
-	size_t len = ipa_name->len;
-	if (!len)
-		return talloc_strdup(ctx, "");
-	if (ipa_name->val[len-1] == '\0')
-		len--;
-	return osmo_escape_str_c(ctx, (char*)ipa_name->val, len);
-}
diff --git a/src/hlr.c b/src/hlr.c
index 47b3211..a33a68c 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -37,7 +37,7 @@
 #include <osmocom/gsm/gsm_utils.h>
 #include <osmocom/gsm/gsm23003.h>
 
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 #include <osmocom/hlr/db.h>
 #include <osmocom/hlr/hlr.h>
 #include <osmocom/hlr/ctrl.h>
diff --git a/src/hlr_ussd.c b/src/hlr_ussd.c
index aa7614e..25e9354 100644
--- a/src/hlr_ussd.c
+++ b/src/hlr_ussd.c
@@ -416,22 +416,24 @@
 }
 
 /* is this GSUP connection an EUSE (true) or not (false)? */
-static bool peer_name_is_euse(const struct osmo_ipa_name *peer_name)
+static bool peer_name_is_euse(const struct osmo_cni_peer_id *peer_name)
 {
-	if (peer_name->len <= 5)
+	if (peer_name->type != OSMO_CNI_PEER_ID_IPA_NAME)
 		return false;
-	if (!strncmp((char *)(peer_name->val), "EUSE-", 5))
-		return true;
-	else
+	if (peer_name->ipa_name.len <= 5)
 		return false;
+	return strncmp((char *)(peer_name->ipa_name.val), "EUSE-", 5) == 0;
 }
 
-static struct hlr_euse *euse_by_name(const struct osmo_ipa_name *peer_name)
+static struct hlr_euse *euse_by_name(const struct osmo_cni_peer_id *peer_name)
 {
 	if (!peer_name_is_euse(peer_name))
 		return NULL;
 
-	return euse_find(g_hlr, (const char*)(peer_name->val)+5);
+	/* above peer_name_is_euse() ensures this: */
+	OSMO_ASSERT(peer_name->type == OSMO_CNI_PEER_ID_IPA_NAME);
+
+	return euse_find(g_hlr, (const char*)(peer_name->ipa_name.val)+5);
 }
 
 static int handle_ss(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup,
@@ -519,6 +521,14 @@
 	LOGP(DSS, LOGL_DEBUG, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id,
 		osmo_gsup_session_state_name(gsup->session_state));
 
+	if (gsup_req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
+		LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to process SS request: Unsupported GSUP peer id type%s\n",
+		     gsup->imsi, gsup->session_id,
+		     osmo_cni_peer_id_type_name(gsup_req->source_name.type));
+		osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_PROTO_ERR_UNSPEC, "error processing SS request");
+		return;
+	}
+
 	/* decode and find out what kind of SS message it is */
 	if (gsup->ss_info && gsup->ss_info_len) {
 		if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
@@ -556,7 +566,8 @@
 		if (!is_euse_originated) {
 			ss->initial_req_from_ms = gsup_req;
 			free_gsup_req = NULL;
-			ss->vlr_name = gsup_req->source_name;
+			OSMO_ASSERT(gsup_req->source_name.type == OSMO_CNI_PEER_ID_IPA_NAME); /* checked above */
+			ss->vlr_name = gsup_req->source_name.ipa_name;
 		} else {
 			ss->initial_req_from_euse = gsup_req;
 			free_gsup_req = NULL;
diff --git a/src/lu_fsm.c b/src/lu_fsm.c
index bded4ef..b5493db 100644
--- a/src/lu_fsm.c
+++ b/src/lu_fsm.c
@@ -26,7 +26,7 @@
 #include <osmocom/gsm/apn.h>
 #include <osmocom/gsm/gsm48_ie.h>
 
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 #include <osmocom/gsupclient/gsup_req.h>
 #include <osmocom/hlr/logging.h>
 #include <osmocom/hlr/hlr.h>
@@ -52,11 +52,11 @@
 	bool is_ps;
 
 	/* VLR requesting the LU. */
-	struct osmo_ipa_name vlr_name;
+	struct osmo_cni_peer_id vlr_name;
 
 	/* If the LU request was received via a proxy and not immediately from a local VLR, this indicates the closest
 	 * peer that forwarded the GSUP message. */
-	struct osmo_ipa_name via_proxy;
+	struct osmo_cni_peer_id via_proxy;
 };
 LLIST_HEAD(g_all_lu);
 
@@ -130,7 +130,7 @@
 
 	osmo_fsm_inst_update_id_f_sanitize(fi, '_', "%s:IMSI-%s", lu->is_ps ? "PS" : "CS", update_location_req->gsup.imsi);
 
-	if (!lu->vlr_name.len) {
+	if (osmo_cni_peer_id_is_empty(&lu->vlr_name)) {
 		lu_failure(lu, GMM_CAUSE_NET_FAIL, "LU without a VLR");
 		return;
 	}
@@ -163,18 +163,30 @@
 #endif
 
 	/* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */
-	if (lu->via_proxy.len) {
+	if (!osmo_cni_peer_id_is_empty(&lu->via_proxy)) {
 		LOG_GSUP_REQ(update_location_req, LOGL_DEBUG, "storing %s = %s, via proxy %s\n",
 			     lu->is_ps ? "SGSN number" : "VLR number",
-			     osmo_ipa_name_to_str(&lu->vlr_name),
-			     osmo_ipa_name_to_str(&lu->via_proxy));
+			     osmo_cni_peer_id_to_str(&lu->vlr_name),
+			     osmo_cni_peer_id_to_str(&lu->via_proxy));
 	} else {
 		LOG_GSUP_REQ(update_location_req, LOGL_DEBUG, "storing %s = %s\n",
 		     lu->is_ps ? "SGSN number" : "VLR number",
-		     osmo_ipa_name_to_str(&lu->vlr_name));
+		     osmo_cni_peer_id_to_str(&lu->vlr_name));
 	}
 
-	if (db_subscr_lu(g_hlr->dbc, lu->subscr.id, &lu->vlr_name, lu->is_ps, &lu->via_proxy)) {
+	if (osmo_cni_peer_id_is_empty(&lu->vlr_name)
+	    || (lu->vlr_name.type != OSMO_CNI_PEER_ID_IPA_NAME)) {
+		lu_failure(lu, GMM_CAUSE_PROTO_ERR_UNSPEC, "Unsupported GSUP peer id type for vlr_name: %s",
+			   osmo_cni_peer_id_type_name(lu->vlr_name.type));
+		return;
+	}
+	if (!osmo_cni_peer_id_is_empty(&lu->via_proxy) && (lu->via_proxy.type != OSMO_CNI_PEER_ID_IPA_NAME)) {
+		lu_failure(lu, GMM_CAUSE_PROTO_ERR_UNSPEC, "Unsupported GSUP peer id type for via_proxy: %s",
+			   osmo_cni_peer_id_type_name(lu->via_proxy.type));
+		return;
+	}
+	if (db_subscr_lu(g_hlr->dbc, lu->subscr.id, &lu->vlr_name.ipa_name, lu->is_ps,
+			 osmo_cni_peer_id_is_empty(&lu->via_proxy)? NULL : &lu->via_proxy.ipa_name)) {
 		lu_failure(lu, GMM_CAUSE_NET_FAIL, "Cannot update %s in the database",
 			   lu->is_ps ? "SGSN number" : "VLR number");
 		return;
diff --git a/tests/db/Makefile.am b/tests/db/Makefile.am
index 0194957..ece34d3 100644
--- a/tests/db/Makefile.am
+++ b/tests/db/Makefile.am
@@ -30,7 +30,7 @@
 	$(top_builddir)/src/db_auc.o \
 	$(top_builddir)/src/db_hlr.o \
 	$(top_builddir)/src/db.o \
-	$(top_builddir)/src/ipa_name.o \
+	$(top_builddir)/src/cni_peer_id.o \
 	$(LIBOSMOCORE_LIBS) \
 	$(LIBOSMOGSM_LIBS) \
 	$(LIBOSMOABIS_LIBS) \
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index c53baed..c1df9bf 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -27,7 +27,7 @@
 #include <osmocom/core/utils.h>
 #include <osmocom/core/logging.h>
 
-#include <osmocom/gsupclient/ipa_name.h>
+#include <osmocom/gsupclient/cni_peer_id.h>
 #include <osmocom/hlr/db.h>
 #include <osmocom/hlr/logging.h>
 
diff --git a/tests/gsup_server/Makefile.am b/tests/gsup_server/Makefile.am
index 48fda88..a32ddf9 100644
--- a/tests/gsup_server/Makefile.am
+++ b/tests/gsup_server/Makefile.am
@@ -31,7 +31,7 @@
 gsup_server_test_LDADD = \
 	$(top_srcdir)/src/gsup_server.c \
 	$(top_srcdir)/src/gsup_router.c \
-	$(top_srcdir)/src/gsupclient/ipa_name.c \
+	$(top_srcdir)/src/gsupclient/cni_peer_id.c \
 	$(top_srcdir)/src/gsupclient/gsup_req.c \
 	$(LIBOSMOCORE_LIBS) \
 	$(LIBOSMOGSM_LIBS) \

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-hlr/+/16459
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Change-Id: Ide9dcdca283ab989240cfc6e53e9211862a199c5
Gerrit-Change-Number: 16459
Gerrit-PatchSet: 11
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <axilirator at gmail.com>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: neels <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200501/04651d73/attachment.htm>


More information about the gerrit-log mailing list