Osmocom Mobile Switching Centre
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
osmo-msc/src/libmsc/db.c

1031 lines
26 KiB

/* Simple HLR/VLR database backend using dbi */
/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
* (C) 2009 by Harald Welte <laforge@gnumonks.org>
* 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 <stdint.h>
#include <inttypes.h>
#include <libgen.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dbi/dbi.h>
#include <osmocom/msc/gsm_data.h>
#include <osmocom/msc/gsm_subscriber.h>
#include <osmocom/msc/gsm_04_11.h>
#include <osmocom/msc/db.h>
#include <osmocom/msc/debug.h>
#include <osmocom/msc/vlr.h>
#include <osmocom/gsm/protocol/gsm_23_003.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/statistics.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/utils.h>
static char *db_basename = NULL;
static char *db_dirname = NULL;
static dbi_conn conn;
static dbi_inst inst;
#define SCHEMA_REVISION "5"
enum {
SCHEMA_META,
INSERT_META,
SCHEMA_SUBSCRIBER,
SCHEMA_AUTH,
SCHEMA_EQUIPMENT,
SCHEMA_EQUIPMENT_WATCH,
SCHEMA_SMS,
SCHEMA_VLR,
SCHEMA_APDU,
SCHEMA_COUNTERS,
SCHEMA_RATE,
SCHEMA_AUTHKEY,
SCHEMA_AUTHLAST,
};
static const char *create_stmts[] = {
[SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"key TEXT UNIQUE NOT NULL, "
"value TEXT NOT NULL"
")",
[INSERT_META] = "INSERT OR IGNORE INTO Meta "
"(key, value) "
"VALUES "
"('revision', " SCHEMA_REVISION ")",
[SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"updated TIMESTAMP NOT NULL, "
"imsi NUMERIC UNIQUE NOT NULL, "
"name TEXT, "
"extension TEXT UNIQUE, "
"authorized INTEGER NOT NULL DEFAULT 0, "
"tmsi TEXT UNIQUE, "
"lac INTEGER NOT NULL DEFAULT 0, "
"expire_lu TIMESTAMP DEFAULT NULL"
")",
[SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"subscriber_id INTEGER UNIQUE NOT NULL, "
"created TIMESTAMP NOT NULL, "
"token TEXT UNIQUE NOT NULL"
")",
[SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"updated TIMESTAMP NOT NULL, "
"name TEXT, "
"classmark1 NUMERIC, "
"classmark2 BLOB, "
"classmark3 BLOB, "
"imei NUMERIC UNIQUE NOT NULL"
")",
[SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"updated TIMESTAMP NOT NULL, "
"subscriber_id NUMERIC NOT NULL, "
"equipment_id NUMERIC NOT NULL, "
"UNIQUE (subscriber_id, equipment_id) "
")",
[SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
/* metadata, not part of sms */
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"sent TIMESTAMP, "
"deliver_attempts INTEGER NOT NULL DEFAULT 0, "
/* data directly copied/derived from SMS */
"valid_until TIMESTAMP, "
"reply_path_req INTEGER NOT NULL, "
"status_rep_req INTEGER NOT NULL, "
"is_report INTEGER NOT NULL, "
"msg_ref INTEGER NOT NULL, "
"protocol_id INTEGER NOT NULL, "
"data_coding_scheme INTEGER NOT NULL, "
"ud_hdr_ind INTEGER NOT NULL, "
"src_addr TEXT NOT NULL, "
"src_ton INTEGER NOT NULL, "
"src_npi INTEGER NOT NULL, "
"dest_addr TEXT NOT NULL, "
"dest_ton INTEGER NOT NULL, "
"dest_npi INTEGER NOT NULL, "
"user_data BLOB, " /* TP-UD */
/* additional data, interpreted from SMS */
"header BLOB, " /* UD Header */
"text TEXT " /* decoded UD after UDH */
")",
[SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"updated TIMESTAMP NOT NULL, "
"subscriber_id NUMERIC UNIQUE NOT NULL, "
"last_bts NUMERIC NOT NULL "
")",
[SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
"apdu_id_flags INTEGER NOT NULL, "
"subscriber_id INTEGER NOT NULL, "
"apdu BLOB "
")",
[SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"timestamp TIMESTAMP NOT NULL, "
"value INTEGER NOT NULL, "
"name TEXT NOT NULL "
")",
[SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"timestamp TIMESTAMP NOT NULL, "
"value INTEGER NOT NULL, "
"name TEXT NOT NULL, "
"idx INTEGER NOT NULL "
")",
[SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
"subscriber_id INTEGER PRIMARY KEY, "
"algorithm_id INTEGER NOT NULL, "
"a3a8_ki BLOB "
")",
[SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
"subscriber_id INTEGER PRIMARY KEY, "
"issued TIMESTAMP NOT NULL, "
"use_count INTEGER NOT NULL DEFAULT 0, "
"key_seq INTEGER NOT NULL, "
"rand BLOB NOT NULL, "
"sres BLOB NOT NULL, "
"kc BLOB NOT NULL "
")",
};
sms.db: silence libdbi warnings on out-of-range index Apparently, since libdbi 0.9.0 aka 0.9.0-5 on debian-testing, osmo-msc barfs numerous libdbi warnings whenever a query rightfully returns no rows. Trivially query whether there are any rows first by adding an inline wrap function next_row(). Silenced: DDB <000d> ../../../../src/osmo-msc/src/libmsc/db.c:188 DBI: -6: An invalid or out-of-range index was passed to libdbi DDB <000d> ../../../src/libosmocore/src/backtrace.c:47 backtrace() returned 11 addresses DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xfb81) [0x555555563b81] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(_error_handler+0x99) [0x7ffff63f5c39] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(dbi_result_next_row+0x3d) [0x7ffff63f785d] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x11172) [0x555555565172] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e6bc) [0x5555555726bc] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e7f6) [0x5555555727f6] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1f1d2) [0x5555555731d2] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbb86) [0x55555555fb86] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7ffff5cfe561] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbfba) [0x55555555ffba] Related: OS#2667 Change-Id: Ib8993c8db171d1e845a6297deef137d18506cda3
5 years ago
static inline int next_row(dbi_result result)
{
if (!dbi_result_has_next_row(result))
return 0;
return dbi_result_next_row(result);
}
void db_error_func(dbi_conn conn, void *data)
{
const char *msg;
dbi_conn_error(conn, &msg);
LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
osmo_log_backtrace(DDB, LOGL_ERROR);
}
static int update_db_revision_2(void)
{
dbi_result result;
result = dbi_conn_query(conn,
"ALTER TABLE Subscriber "
"ADD COLUMN expire_lu "
"TIMESTAMP DEFAULT NULL");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to alter table Subscriber (upgrade from rev 2).\n");
return -EINVAL;
}
dbi_result_free(result);
result = dbi_conn_query(conn,
"UPDATE Meta "
"SET value = '3' "
"WHERE key = 'revision'");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to update DB schema revision (upgrade from rev 2).\n");
return -EINVAL;
}
dbi_result_free(result);
return 0;
}
/**
* Copied from the normal sms_from_result_v3 to avoid having
* to make sure that the real routine will remain backward
* compatible.
*/
static struct gsm_sms *sms_from_result_v3(dbi_result result)
{
struct gsm_sms *sms = sms_alloc();
long long unsigned int sender_id;
const char *text, *daddr;
const unsigned char *user_data;
char buf[32];
Use libvlr in libmsc (large refactoring) Original libvlr code is by Harald Welte <laforge@gnumonks.org>, polished and tweaked by Neels Hofmeyr <nhofmeyr@sysmocom.de>. This is a long series of trial-and-error development collapsed in one patch. This may be split in smaller commits if reviewers prefer that. If we can keep it as one, we have saved ourselves the additional separation work. SMS: The SQL based lookup of SMS for attached subscribers no longer works since the SQL database no longer has the subscriber data. Replace with a round-robin on the SMS recipient MSISDNs paired with a VLR subscriber RAM lookup whether the subscriber is currently attached. If there are many SMS for not-attached subscribers in the SMS database, this will become inefficient: a DB hit returns a pending SMS, the RAM lookup will reveal that the subscriber is not attached, after which the DB is hit for the next SMS. It would become more efficient e.g. by having an MSISDN based hash list for the VLR subscribers and by marking non-attached SMS recipients in the SMS database so that they can be excluded with the SQL query already. There is a sanity limit to do at most 100 db hits per attempt to find a pending SMS. So if there are more than 100 stored SMS waiting for their recipients to actually attach to the MSC, it may take more than one SMS queue trigger to deliver SMS for subscribers that are actually attached. This is not very beautiful, but is merely intended to carry us over to a time when we have a proper separate SMSC entity. Introduce gsm_subscriber_connection ref-counting in libmsc. Remove/Disable VTY and CTRL commands to create subscribers, which is now a task of the OsmoHLR. Adjust the python tests accordingly. Remove VTY cmd subscriber-keep-in-ram. Use OSMO_GSUP_PORT = 4222 instead of 2222. See I4222e21686c823985be8ff1f16b1182be8ad6175. So far use the LAC from conn->bts, will be replaced by conn->lac in Id3705236350d5f69e447046b0a764bbabc3d493c. Related: OS#1592 OS#1974 Change-Id: I639544a6cdda77a3aafc4e3446a55393f60e4050
7 years ago
char *quoted;
dbi_result result2;
const char *extension;
if (!sms)
return NULL;
sms->id = dbi_result_get_ulonglong(result, "id");
Use libvlr in libmsc (large refactoring) Original libvlr code is by Harald Welte <laforge@gnumonks.org>, polished and tweaked by Neels Hofmeyr <nhofmeyr@sysmocom.de>. This is a long series of trial-and-error development collapsed in one patch. This may be split in smaller commits if reviewers prefer that. If we can keep it as one, we have saved ourselves the additional separation work. SMS: The SQL based lookup of SMS for attached subscribers no longer works since the SQL database no longer has the subscriber data. Replace with a round-robin on the SMS recipient MSISDNs paired with a VLR subscriber RAM lookup whether the subscriber is currently attached. If there are many SMS for not-attached subscribers in the SMS database, this will become inefficient: a DB hit returns a pending SMS, the RAM lookup will reveal that the subscriber is not attached, after which the DB is hit for the next SMS. It would become more efficient e.g. by having an MSISDN based hash list for the VLR subscribers and by marking non-attached SMS recipients in the SMS database so that they can be excluded with the SQL query already. There is a sanity limit to do at most 100 db hits per attempt to find a pending SMS. So if there are more than 100 stored SMS waiting for their recipients to actually attach to the MSC, it may take more than one SMS queue trigger to deliver SMS for subscribers that are actually attached. This is not very beautiful, but is merely intended to carry us over to a time when we have a proper separate SMSC entity. Introduce gsm_subscriber_connection ref-counting in libmsc. Remove/Disable VTY and CTRL commands to create subscribers, which is now a task of the OsmoHLR. Adjust the python tests accordingly. Remove VTY cmd subscriber-keep-in-ram. Use OSMO_GSUP_PORT = 4222 instead of 2222. See I4222e21686c823985be8ff1f16b1182be8ad6175. So far use the LAC from conn->bts, will be replaced by conn->lac in Id3705236350d5f69e447046b0a764bbabc3d493c. Related: OS#1592 OS#1974 Change-Id: I639544a6cdda77a3aafc4e3446a55393f60e4050
7 years ago
/* find extension by id, assuming that the subscriber still exists in
* the db */
sender_id = dbi_result_get_ulonglong(result, "sender_id");
snprintf(buf, sizeof(buf), "%llu", sender_id);
Use libvlr in libmsc (large refactoring) Original libvlr code is by Harald Welte <laforge@gnumonks.org>, polished and tweaked by Neels Hofmeyr <nhofmeyr@sysmocom.de>. This is a long series of trial-and-error development collapsed in one patch. This may be split in smaller commits if reviewers prefer that. If we can keep it as one, we have saved ourselves the additional separation work. SMS: The SQL based lookup of SMS for attached subscribers no longer works since the SQL database no longer has the subscriber data. Replace with a round-robin on the SMS recipient MSISDNs paired with a VLR subscriber RAM lookup whether the subscriber is currently attached. If there are many SMS for not-attached subscribers in the SMS database, this will become inefficient: a DB hit returns a pending SMS, the RAM lookup will reveal that the subscriber is not attached, after which the DB is hit for the next SMS. It would become more efficient e.g. by having an MSISDN based hash list for the VLR subscribers and by marking non-attached SMS recipients in the SMS database so that they can be excluded with the SQL query already. There is a sanity limit to do at most 100 db hits per attempt to find a pending SMS. So if there are more than 100 stored SMS waiting for their recipients to actually attach to the MSC, it may take more than one SMS queue trigger to deliver SMS for subscribers that are actually attached. This is not very beautiful, but is merely intended to carry us over to a time when we have a proper separate SMSC entity. Introduce gsm_subscriber_connection ref-counting in libmsc. Remove/Disable VTY and CTRL commands to create subscribers, which is now a task of the OsmoHLR. Adjust the python tests accordingly. Remove VTY cmd subscriber-keep-in-ram. Use OSMO_GSUP_PORT = 4222 instead of 2222. See I4222e21686c823985be8ff1f16b1182be8ad6175. So far use the LAC from conn->bts, will be replaced by conn->lac in Id3705236350d5f69e447046b0a764bbabc3d493c. Related: OS#1592 OS#1974 Change-Id: I639544a6cdda77a3aafc4e3446a55393f60e4050
7 years ago
dbi_conn_quote_string_copy(conn, buf, &quoted);
result2 = dbi_conn_queryf(conn,
"SELECT extension FROM Subscriber "
"WHERE id = %s ", quoted);
free(quoted);
extension = dbi_result_get_string(result2, "extension");
if (extension)
osmo_strlcpy(sms->src.addr, extension, sizeof(sms->src.addr));
dbi_result_free(result2);
/* got the extension */
sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
sms->data_coding_scheme = dbi_result_get_ulonglong(result,
"data_coding_scheme");
daddr = dbi_result_get_string(result, "dest_addr");
if (daddr)
osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
sms->user_data_len = dbi_result_get_field_length(result, "user_data");
user_data = dbi_result_get_binary(result, "user_data");
if (sms->user_data_len > sizeof(sms->user_data))
sms->user_data_len = (uint8_t) sizeof(sms->user_data);
memcpy(sms->user_data, user_data, sms->user_data_len);
text = dbi_result_get_string(result, "text");
if (text)
osmo_strlcpy(sms->text, text, sizeof(sms->text));
return sms;
}
static int update_db_revision_3(void)
{
dbi_result result;
struct gsm_sms *sms;
LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to begin transaction (upgrade from rev 3)\n");
return -EINVAL;
}
dbi_result_free(result);
/* Rename old SMS table to be able create a new one */
result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to rename the old SMS table (upgrade from rev 3).\n");
goto rollback;
}
dbi_result_free(result);
/* Create new SMS table with all the bells and whistles! */
result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create a new SMS table (upgrade from rev 3).\n");
goto rollback;
}
dbi_result_free(result);
/* Cycle through old messages and convert them to the new format */
result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
goto rollback;
}
sms.db: silence libdbi warnings on out-of-range index Apparently, since libdbi 0.9.0 aka 0.9.0-5 on debian-testing, osmo-msc barfs numerous libdbi warnings whenever a query rightfully returns no rows. Trivially query whether there are any rows first by adding an inline wrap function next_row(). Silenced: DDB <000d> ../../../../src/osmo-msc/src/libmsc/db.c:188 DBI: -6: An invalid or out-of-range index was passed to libdbi DDB <000d> ../../../src/libosmocore/src/backtrace.c:47 backtrace() returned 11 addresses DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xfb81) [0x555555563b81] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(_error_handler+0x99) [0x7ffff63f5c39] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(dbi_result_next_row+0x3d) [0x7ffff63f785d] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x11172) [0x555555565172] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e6bc) [0x5555555726bc] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e7f6) [0x5555555727f6] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1f1d2) [0x5555555731d2] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbb86) [0x55555555fb86] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7ffff5cfe561] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbfba) [0x55555555ffba] Related: OS#2667 Change-Id: Ib8993c8db171d1e845a6297deef137d18506cda3
5 years ago
while (next_row(result)) {
sms = sms_from_result_v3(result);
if (db_sms_store(sms) != 0) {
LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
sms_free(sms);
dbi_result_free(result);
goto rollback;
}
sms_free(sms);
}
dbi_result_free(result);
/* Remove the temporary table */
result = dbi_conn_query(conn, "DROP TABLE SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to drop the old SMS table (upgrade from rev 3).\n");
goto rollback;
}
dbi_result_free(result);
/* We're done. Bump DB Meta revision to 4 */
result = dbi_conn_query(conn,
"UPDATE Meta "
"SET value = '4' "
"WHERE key = 'revision'");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to update DB schema revision (upgrade from rev 3).\n");
goto rollback;
}
dbi_result_free(result);
result = dbi_conn_query(conn, "COMMIT TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to commit the transaction (upgrade from rev 3)\n");
return -EINVAL;
} else {
dbi_result_free(result);
}
/* Shrink DB file size by actually wiping out SMS_3 table data */
result = dbi_conn_query(conn, "VACUUM");
if (!result)
LOGP(DDB, LOGL_ERROR,
"VACUUM failed. Ignoring it (upgrade from rev 3).\n");
else
dbi_result_free(result);
return 0;
rollback:
result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
if (!result)
LOGP(DDB, LOGL_ERROR,
"Rollback failed (upgrade from rev 3).\n");
else
dbi_result_free(result);
return -EINVAL;
}
/* Just like v3, but there is a new message reference field for status reports,
* that is set to zero for existing entries since there is no way we can infer
* this.
*/
static struct gsm_sms *sms_from_result_v4(dbi_result result)
{
struct gsm_sms *sms = sms_alloc();
const unsigned char *user_data;
const char *text, *addr;
if (!sms)
return NULL;
sms->id = dbi_result_get_ulonglong(result, "id");
sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
sms->data_coding_scheme = dbi_result_get_ulonglong(result,
"data_coding_scheme");
addr = dbi_result_get_string(result, "src_addr");
osmo_strlcpy(sms->src.addr, addr, sizeof(sms->src.addr));
sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
addr = dbi_result_get_string(result, "dest_addr");
osmo_strlcpy(sms->dst.addr, addr, sizeof(sms->dst.addr));
sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
sms->user_data_len = dbi_result_get_field_length(result, "user_data");
user_data = dbi_result_get_binary(result, "user_data");
if (sms->user_data_len > sizeof(sms->user_data))
sms->user_data_len = (uint8_t) sizeof(sms->user_data);
memcpy(sms->user_data, user_data, sms->user_data_len);
text = dbi_result_get_string(result, "text");
if (text)
osmo_strlcpy(sms->text, text, sizeof(sms->text));
return sms;
}
static int update_db_revision_4(void)
{
dbi_result result;
struct gsm_sms *sms;
LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 4\n");
result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to begin transaction (upgrade from rev 4)\n");
return -EINVAL;
}
dbi_result_free(result);
/* Rename old SMS table to be able create a new one */
result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to rename the old SMS table (upgrade from rev 4).\n");
goto rollback;
}
dbi_result_free(result);
/* Create new SMS table with all the bells and whistles! */
result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create a new SMS table (upgrade from rev 4).\n");
goto rollback;
}
dbi_result_free(result);
/* Cycle through old messages and convert them to the new format */
result = dbi_conn_query(conn, "SELECT * FROM SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed fetch messages from the old SMS table (upgrade from rev 4).\n");
goto rollback;
}
sms.db: silence libdbi warnings on out-of-range index Apparently, since libdbi 0.9.0 aka 0.9.0-5 on debian-testing, osmo-msc barfs numerous libdbi warnings whenever a query rightfully returns no rows. Trivially query whether there are any rows first by adding an inline wrap function next_row(). Silenced: DDB <000d> ../../../../src/osmo-msc/src/libmsc/db.c:188 DBI: -6: An invalid or out-of-range index was passed to libdbi DDB <000d> ../../../src/libosmocore/src/backtrace.c:47 backtrace() returned 11 addresses DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xfb81) [0x555555563b81] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(_error_handler+0x99) [0x7ffff63f5c39] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(dbi_result_next_row+0x3d) [0x7ffff63f785d] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x11172) [0x555555565172] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e6bc) [0x5555555726bc] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e7f6) [0x5555555727f6] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1f1d2) [0x5555555731d2] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbb86) [0x55555555fb86] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7ffff5cfe561] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbfba) [0x55555555ffba] Related: OS#2667 Change-Id: Ib8993c8db171d1e845a6297deef137d18506cda3
5 years ago
while (next_row(result)) {
sms = sms_from_result_v4(result);
if (db_sms_store(sms) != 0) {
LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 4).\n");
sms_free(sms);
dbi_result_free(result);
goto rollback;
}
sms_free(sms);
}
dbi_result_free(result);
/* Remove the temporary table */
result = dbi_conn_query(conn, "DROP TABLE SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to drop the old SMS table (upgrade from rev 4).\n");
goto rollback;
}
dbi_result_free(result);
/* We're done. Bump DB Meta revision to 4 */
result = dbi_conn_query(conn,
"UPDATE Meta "
"SET value = '5' "
"WHERE key = 'revision'");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to update DB schema revision (upgrade from rev 4).\n");
goto rollback;
}
dbi_result_free(result);
result = dbi_conn_query(conn, "COMMIT TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to commit the transaction (upgrade from rev 4)\n");
return -EINVAL;
} else {
dbi_result_free(result);
}
/* Shrink DB file size by actually wiping out SMS_4 table data */
result = dbi_conn_query(conn, "VACUUM");
if (!result)
LOGP(DDB, LOGL_ERROR,
"VACUUM failed. Ignoring it (upgrade from rev 4).\n");
else
dbi_result_free(result);
return 0;
rollback:
result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
if (!result)
LOGP(DDB, LOGL_ERROR,
"Rollback failed (upgrade from rev 4).\n");
else
dbi_result_free(result);
return -EINVAL;
}
static int check_db_revision(void)
{
dbi_result result;
const char *rev_s;
int db_rev = 0;
/* Make a query */
result = dbi_conn_query(conn,
"SELECT value FROM Meta "
"WHERE key = 'revision'");
if (!result)
return -EINVAL;
sms.db: silence libdbi warnings on out-of-range index Apparently, since libdbi 0.9.0 aka 0.9.0-5 on debian-testing, osmo-msc barfs numerous libdbi warnings whenever a query rightfully returns no rows. Trivially query whether there are any rows first by adding an inline wrap function next_row(). Silenced: DDB <000d> ../../../../src/osmo-msc/src/libmsc/db.c:188 DBI: -6: An invalid or out-of-range index was passed to libdbi DDB <000d> ../../../src/libosmocore/src/backtrace.c:47 backtrace() returned 11 addresses DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xfb81) [0x555555563b81] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(_error_handler+0x99) [0x7ffff63f5c39] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/lib/x86_64-linux-gnu/libdbi.so.1(dbi_result_next_row+0x3d) [0x7ffff63f785d] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x11172) [0x555555565172] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e6bc) [0x5555555726bc] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1e7f6) [0x5555555727f6] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0x1f1d2) [0x5555555731d2] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbb86) [0x55555555fb86] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7ffff5cfe561] DDB <000d> ../../../src/libosmocore/src/backtrace.c:57 /usr/local/bin/osmo-msc(+0xbfba) [0x55555555ffba] Related: OS#2667 Change-Id: Ib8993c8db171d1e845a6297deef137d18506cda3
5 years ago
if (!next_row(result)) {
dbi_result_free(result);
return -EINVAL;
}
/* Fetch the DB schema revision */
rev_s = dbi_result_get_string(result, "value");
if (!rev_s) {
dbi_result_free(result);
return -EINVAL;
}
if (!strcmp(rev_s, SCHEMA_REVISION)) {
/* Everything is fine */
dbi_result_free(result);
return 0;
}
db_rev = atoi(rev_s);
dbi_result_free(result);
/* Incremental migration waterfall */
switch (db_rev) {
case 2:
if (update_db_revision_2())
goto error;
case 3:
if (update_db_revision_3())
goto error;
case 4:
if (update_db_revision_4())
goto error;
/* The end of waterfall */
break;
default:
LOGP(DDB, LOGL_FATAL,
"Invalid database schema revision '%d'.\n", db_rev);
return -EINVAL;
}
return 0;
error:
LOGP(DDB, LOGL_FATAL, "Failed to update database "
"from schema revision '%d'.\n", db_rev);
return -EINVAL;
}
static int db_configure(void)
{
dbi_result result;
result = dbi_conn_query(conn,
"PRAGMA synchronous = FULL");
if (!result)
return -EINVAL;
dbi_result_free(result);
return 0;
}
int db_init(const char *name)
{
dbi_initialize_r(NULL, &inst);
conn = dbi_conn_new_r("sqlite3", inst);
if (conn == NULL) {
LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
return 1;
}
dbi_conn_error_handler( conn, db_error_func, NULL );
/* MySQL
dbi_conn_set_option(conn, "host", "localhost");
dbi_conn_set_option(conn, "username", "your_name");
dbi_conn_set_option(conn, "password", "your_password");
dbi_conn_set_option(conn, "dbname", "your_dbname");
dbi_conn_set_option(conn, "encoding", "UTF-8");
*/
/* SqLite 3 */
db_basename = strdup(name);
db_dirname = strdup(name);
dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
dbi_conn_set_option(conn, "dbname", basename(db_basename));
if (dbi_conn_connect(conn) < 0)
goto out_err;
return 0;
out_err:
free(db_dirname);
free(db_basename);
db_dirname = db_basename = NULL;
return -1;
}
int db_prepare(void)
{
dbi_result result;
int i;
for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
result = dbi_conn_query(conn, create_stmts[i]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create some table.\n");
return 1;
}
dbi_result_free(result);
}
if (check_db_revision() < 0) {
LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
"please update your database schema\n");
return -1;
}
db_configure();
return 0;
}
int db_fini(void)
{
dbi_conn_close(conn);
dbi_shutdown_r(inst);
free(db_dirname);
free(db_basename);
return 0;
}
/* store an [unsent] SMS to the database */
int db_sms_store(struct gsm_sms *sms)
{
dbi_result result;
char *q_text, *q_daddr, *q_saddr;
unsigned char *q_udata;
char *validity_timestamp = "2222-2-2";
/* FIXME: generate validity timestamp based on validity_minutes */
dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
&q_udata);
/* FIXME: correct validity period */
result = dbi_conn_queryf(conn,
"INSERT INTO SMS "
"(created, valid_until, "
"reply_path_req, status_rep_req, is_report, "
"msg_ref, protocol_id, data_coding_scheme, "
"ud_hdr_ind, "
"user_data, text, "
"dest_addr, dest_ton, dest_npi, "
"src_addr, src_ton, src_npi) VALUES "
"(datetime('now'), %u, "
"%u, %u, %u, "
"%u, %u, %u, "
"%u, "
"%s, %s, "
"%s, %u, %u, "
"%s, %u, %u)",
validity_timestamp,
sms->reply_path_req, sms->status_rep_req, sms->is_report,
sms->msg_ref, sms->protocol_id, sms->data_coding_scheme,
sms->ud_hdr_ind,
q_udata, q_text,
q_daddr, sms->dst.ton, sms->dst.npi,
q_saddr, sms->src.ton, sms->src.npi);
free(q_text);
free(q_udata);
free(q_daddr);
free(q_saddr);
if (!result)
return -EIO;
dbi_result_free(result);
return 0;
}
static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
{
struct gsm_sms *sms = sms_alloc();
const char *text, *daddr, *saddr;
const unsigned char *user_data;
if (!sms)
return NULL;
sms->id = dbi_result_get_ulonglong(result, "id");
/* FIXME: validity */
/* FIXME: those should all be get_uchar, but sqlite3 is braindead */
sms->created = dbi_result_get_datetime(result, "created");
sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
sms->is_report = dbi_result_get_ulonglong(result, "is_report");
sms->msg_ref = dbi_result_get_ulonglong(result, "msg_ref");
sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
sms->data_coding_scheme = dbi_result_get_ulonglong(result,
"data_coding_scheme");
sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
daddr = dbi_result_get_string(result, "dest_addr");
if (daddr)
osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
Use libvlr in libmsc (large refactoring) Original libvlr code is by Harald Welte <laforge@gnumonks.org>, polished and tweaked by Neels Hofmeyr <nhofmeyr@sysmocom.de>. This is a long series of trial-and-error development collapsed in one patch. This may be split in smaller commits if reviewers prefer that. If we can keep it as one, we have saved ourselves the additional separation work. SMS: The SQL based lookup of SMS for attached subscribers no longer works since the SQL database no longer has the subscriber data. Replace with a round-robin on the SMS recipient MSISDNs paired with a VLR subscriber RAM lookup whether the subscriber is currently attached. If there are many SMS for not-attached subscribers in the SMS database, this will become inefficient: a DB hit returns a pending SMS, the RAM lookup will reveal that the subscriber is not attached, after which the DB is hit for the next SMS. It would become more efficient e.g. by having an MSISDN based hash list for the VLR subscribers and by marking non-attached SMS recipients in the SMS database so that they can be excluded with the SQL query already. There is a sanity limit to do at most 100 db hits per attempt to find a pending SMS. So if there are more than 100 stored SMS waiting for their recipients to actually attach to the MSC, it may take more than one SMS queue trigger to deliver SMS for subscribers that are actually attached. This is not very beautiful, but is merely intended to carry us over to a time when we have a proper separate SMSC entity. Introduce gsm_subscriber_connection ref-counting in libmsc. Remove/Disable VTY and CTRL commands to create subscribers, which is now a task of the OsmoHLR. Adjust the python tests accordingly. Remove VTY cmd subscriber-keep-in-ram. Use OSMO_GSUP_PORT = 4222 instead of 2222. See I4222e21686c823985be8ff1f16b1182be8ad6175. So far use the LAC from conn->bts, will be replaced by conn->lac in Id3705236350d5f69e447046b0a764bbabc3d493c. Related: OS#1592 OS#1974 Change-Id: I639544a6cdda77a3aafc4e3446a55393f60e4050
7 years ago
sms->receiver = vlr_subscr_find_by_msisdn(net->vlr, sms->dst.addr);
sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
saddr = dbi_result_get_string(result, "src_addr");
if (saddr)
osmo_strlcpy(sms->src.addr, saddr, sizeof(sms->src.addr));
sms->user_data_len = dbi_result_get_field_length(result, "user_data");
user_data = dbi_result_get_binary(result, "user_data");
if (sms->user_data_len > sizeof(sms->user_data))
sms->user_data_len = (uint8_t) sizeof(sms->user_data);
memcpy(sms->user_data, user_data, sms->user_data_len);
text = dbi_result_get_string(result, "text");
if (text)
osmo_strlcpy(sms->text, text, sizeof(sms->text));
return sms;