Change in osmo-hlr[master]: db_hlr.c: add db_subscr_exists_by_imsi()

Harald Welte gerrit-no-reply at lists.osmocom.org
Sun May 19 07:19:50 UTC 2019


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

Change subject: db_hlr.c: add db_subscr_exists_by_imsi()
......................................................................

db_hlr.c: add db_subscr_exists_by_imsi()

Check if a subscriber exists without generating an error log entry if
it does not. This is cheaper than db_subscr_get_by_imsi(), as it does
not fetch the subscriber entry. subscriber-create-on-demand will use
this function.

Related: OS#2542
Change-Id: I63818c0dd4fd22b41dadeeba2a07a651b5454c54
---
M src/db.c
M src/db.h
M src/db_hlr.c
M tests/db/db_test.c
M tests/db/db_test.err
5 files changed, 40 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved
  Vadim Yanitskiy: Looks good to me, but someone else must approve



diff --git a/src/db.c b/src/db.c
index 770c3a4..9cad263 100644
--- a/src/db.c
+++ b/src/db.c
@@ -79,6 +79,7 @@
 		" VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
 	[DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id",
 	[DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id",
+	[DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi",
 };
 
 static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
diff --git a/src/db.h b/src/db.h
index 3cb42e4..8543085 100644
--- a/src/db.h
+++ b/src/db.h
@@ -28,6 +28,7 @@
 	DB_STMT_AUC_3G_INSERT,
 	DB_STMT_AUC_3G_DELETE,
 	DB_STMT_SET_LAST_LU_SEEN,
+	DB_STMT_EXISTS_BY_IMSI,
 	_NUM_DB_STMT
 };
 
@@ -130,6 +131,8 @@
 			       const struct sub_auth_data_str *aud);
 int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei);
 
+int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi);
+
 int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
 			  struct hlr_subscriber *subscr);
 int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
diff --git a/src/db_hlr.c b/src/db_hlr.c
index 40209c5..c59daf7 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -511,6 +511,31 @@
 	return ret;
 }
 
+/*! Check if a subscriber exists in the HLR database.
+ * \param[in, out] dbc  database context.
+ * \param[in] imsi  ASCII string of IMSI digits.
+ * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error.
+ */
+int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi) {
+	sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_IMSI];
+	const char *err;
+	int rc;
+
+	if (!db_bind_text(stmt, NULL, imsi))
+		return -EIO;
+
+	rc = sqlite3_step(stmt);
+	db_remove_reset(stmt);
+	if (rc == SQLITE_ROW)
+		return 0; /* exists */
+	if (rc == SQLITE_DONE)
+		return -ENOENT; /* does not exist */
+
+	err = sqlite3_errmsg(dbc->db);
+	LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists by IMSI='%s': %s\n", imsi, err);
+	return rc;
+}
+
 /*! Retrieve subscriber data from the HLR database.
  * \param[in,out] dbc  database context.
  * \param[in] imsi  ASCII string of IMSI digits.
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index 72feed4..217a8c5 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -256,6 +256,10 @@
 	ASSERT_SEL(imsi, short_imsi, 0);
 	id_short = g_subscr.id;
 
+	comment("Check if subscriber exists (by IMSI)");
+
+	ASSERT_RC(db_subscr_exists_by_imsi(dbc, imsi0), 0);
+	ASSERT_RC(db_subscr_exists_by_imsi(dbc, unknown_imsi), -ENOENT);
 
 	comment("Set valid / invalid MSISDN");
 
diff --git a/tests/db/db_test.err b/tests/db/db_test.err
index 979562e..0701089 100644
--- a/tests/db/db_test.err
+++ b/tests/db/db_test.err
@@ -93,6 +93,13 @@
 }
 
 
+--- Check if subscriber exists (by IMSI)
+
+db_subscr_exists_by_imsi(dbc, imsi0) --> 0
+
+db_subscr_exists_by_imsi(dbc, unknown_imsi) --> -ENOENT
+
+
 --- Set valid / invalid MSISDN
 
 db_subscr_get_by_imsi(dbc, imsi0, &g_subscr) --> 0

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

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I63818c0dd4fd22b41dadeeba2a07a651b5454c54
Gerrit-Change-Number: 13712
Gerrit-PatchSet: 5
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190519/75650e76/attachment.html>


More information about the gerrit-log mailing list