aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <axilirator@gmail.com>2019-03-30 17:03:42 +0700
committerOliver Smith <osmith@sysmocom.de>2019-05-13 08:55:24 +0200
commitc13599dc696fcab64219eba99afd0a2da9d0eec5 (patch)
tree9c8ac1ff6ecc794cc7dc0e82a71e2ca90738099e
parent6b73fd9678b5b4570edc3722fda467a82b3c7ccb (diff)
db_hlr.c: add db_subscr_exists_by_msisdn()
Check if a subscriber exists without generating an error log entry if it does not. This is cheaper than db_subscr_get_by_msisdn(), as it does not fetch the subscriber entry. subscriber-create-on-demand will use this function to generate a random unique MSISDN for new subscribers. Related: OS#2542 Change-Id: Ibfbc408c966197682ba2b12d166ade4bfeb7abc2
-rw-r--r--src/db.c1
-rw-r--r--src/db.h2
-rw-r--r--src/db_hlr.c27
-rw-r--r--tests/db/db_test.c5
-rw-r--r--tests/db/db_test.err7
5 files changed, 42 insertions, 0 deletions
diff --git a/src/db.c b/src/db.c
index 9cad263..7de61a2 100644
--- a/src/db.c
+++ b/src/db.c
@@ -80,6 +80,7 @@ static const char *stmt_sql[] = {
[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",
+ [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn",
};
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 8543085..12e74f8 100644
--- a/src/db.h
+++ b/src/db.h
@@ -29,6 +29,7 @@ enum stmt_idx {
DB_STMT_AUC_3G_DELETE,
DB_STMT_SET_LAST_LU_SEEN,
DB_STMT_EXISTS_BY_IMSI,
+ DB_STMT_EXISTS_BY_MSISDN,
_NUM_DB_STMT
};
@@ -132,6 +133,7 @@ int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
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_exists_by_msisdn(struct db_context *dbc, const char *msisdn);
int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
struct hlr_subscriber *subscr);
diff --git a/src/db_hlr.c b/src/db_hlr.c
index c59daf7..362dcf2 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -560,6 +560,33 @@ int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
return rc;
}
+/*! Check if a subscriber exists in the HLR database.
+ * \param[in, out] dbc database context.
+ * \param[in] msisdn ASCII string of MSISDN digits.
+ * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error.
+ */
+int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn)
+{
+ sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_MSISDN];
+ const char *err;
+ int rc;
+
+ if (!db_bind_text(stmt, NULL, msisdn))
+ 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 MSISDN='%s': %s\n", msisdn, err);
+ return rc;
+}
+
/*! Retrieve subscriber data from the HLR database.
* \param[in,out] dbc database context.
* \param[in] msisdn ASCII string of MSISDN digits.
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index 217a8c5..fdd62c5 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -294,6 +294,11 @@ static void test_subscr_create_update_sel_delete()
ASSERT_SEL(imsi, imsi0, 0);
ASSERT_SEL(msisdn, "5432101234567891", -ENOENT);
+ comment("Check if subscriber exists (by MSISDN)");
+
+ ASSERT_RC(db_subscr_exists_by_msisdn(dbc, "543210123456789"), 0);
+ ASSERT_RC(db_subscr_exists_by_msisdn(dbc, "5432101234567891"), -ENOENT);
+
comment("Set MSISDN on non-existent / invalid IMSI");
ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, unknown_imsi, "99"), -ENOENT);
diff --git a/tests/db/db_test.err b/tests/db/db_test.err
index 0701089..4dc77e8 100644
--- a/tests/db/db_test.err
+++ b/tests/db/db_test.err
@@ -219,6 +219,13 @@ db_subscr_get_by_msisdn(dbc, "5432101234567891", &g_subscr) --> -ENOENT
DAUC Cannot read subscriber from db: MSISDN='5432101234567891': No such subscriber
+--- Check if subscriber exists (by MSISDN)
+
+db_subscr_exists_by_msisdn(dbc, "543210123456789") --> 0
+
+db_subscr_exists_by_msisdn(dbc, "5432101234567891") --> -ENOENT
+
+
--- Set MSISDN on non-existent / invalid IMSI
db_subscr_update_msisdn_by_imsi(dbc, unknown_imsi, "99") --> -ENOENT