diff options
author | Oliver Smith <osmith@sysmocom.de> | 2019-03-06 13:49:05 +0100 |
---|---|---|
committer | Oliver Smith <osmith@sysmocom.de> | 2019-05-13 08:55:24 +0200 |
commit | 6b73fd9678b5b4570edc3722fda467a82b3c7ccb (patch) | |
tree | 12f680906e5d7e6b14596c8eb1d8f0f95f2ae2d4 | |
parent | cd2af5ead7a6fbba3e7b3df620536619a10ef356 (diff) |
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
-rw-r--r-- | src/db.c | 1 | ||||
-rw-r--r-- | src/db.h | 3 | ||||
-rw-r--r-- | src/db_hlr.c | 25 | ||||
-rw-r--r-- | tests/db/db_test.c | 4 | ||||
-rw-r--r-- | tests/db/db_test.err | 7 |
5 files changed, 40 insertions, 0 deletions
@@ -79,6 +79,7 @@ static const char *stmt_sql[] = { " 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) @@ -28,6 +28,7 @@ enum stmt_idx { 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 @@ int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, 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 @@ out: 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 @@ static void test_subscr_create_update_sel_delete() 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 @@ struct hlr_subscriber { } +--- 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 |