Change in osmo-msc[master]: WIP: store SM-TP-UDL in the database

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

Vadim Yanitskiy gerrit-no-reply at lists.osmocom.org
Sun Apr 14 09:41:49 UTC 2019


Vadim Yanitskiy has uploaded this change for review. ( https://gerrit.osmocom.org/13632


Change subject: WIP: store SM-TP-UDL in the database
......................................................................

WIP: store SM-TP-UDL in the database

Change-Id: I747a795ca486f3fcfa7583c5ab8aa2aee07a8ec2
---
M src/libmsc/db.c
1 file changed, 133 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-msc refs/changes/32/13632/1

diff --git a/src/libmsc/db.c b/src/libmsc/db.c
index d9717d9..a9d6f0b 100644
--- a/src/libmsc/db.c
+++ b/src/libmsc/db.c
@@ -48,7 +48,7 @@
 static dbi_conn conn;
 static dbi_inst inst;
 
-#define SCHEMA_REVISION "5"
+#define SCHEMA_REVISION "6"
 
 enum {
 	SCHEMA_META,
@@ -133,6 +133,7 @@
 		"dest_addr TEXT NOT NULL, "
 		"dest_ton INTEGER NOT NULL, "
 		"dest_npi INTEGER NOT NULL, "
+		"user_data_len INTEGER NOT NULL, " /* TP-UDL */
 		"user_data BLOB, "	/* TP-UD */
 		/* additional data, interpreted from SMS */
 		"header BLOB, "		/* UD Header */
@@ -227,20 +228,32 @@
 
 static void parse_tp_ud_from_result(struct gsm_sms *sms, dbi_result result)
 {
+	unsigned int user_data_len, tp_udl;
 	const unsigned char *user_data;
-	unsigned int user_data_len;
 	unsigned int text_len;
 	const char *text;
 
 	/* Retrieve TP-UDL (User-Data-Length) in octets (regardless of DCS) */
 	user_data_len = dbi_result_get_field_length(result, "user_data");
+
+	/* Retrieve the original TP-UDL (User-Data-Length) value.
+	 * Depending on DCS, may indicate the amount of octets or septets.
+	 * Backwards compatibility: this field is available since v6.
+	 * For older versions this call will return 0 (zero). */
+	tp_udl = dbi_result_get_ulonglong(result, "user_data_len");
+	if (tp_udl == 0) {
+		if (user_data_len > 0)
+	} else {
+		sms->user_data_len = tp_udl;
+	}
+
+	/* Prevent TP-UD buffer overflow */
 	if (user_data_len > sizeof(sms->user_data)) {
 		LOGP(DDB, LOGL_ERROR,
 		     "SMS TP-UD length %u is too big, truncating to %zu\n",
 		     user_data_len, sizeof(sms->user_data));
 		user_data_len = (uint8_t) sizeof(sms->user_data);
 	}
-	sms->user_data_len = user_data_len;
 
 	/* Retrieve the TP-UD (User-Data) itself */
 	user_data = dbi_result_get_binary(result, "user_data");
@@ -555,6 +568,119 @@
 	return -EINVAL;
 }
 
+/* Just like v4, but there is a new TP-UDL (User-Data-Length) field */
+static struct gsm_sms *sms_from_result_v5(dbi_result result)
+{
+	struct gsm_sms *sms;
+
+
+	sms = sms_from_result_v4(result);
+}
+
+static int update_db_revision_5(void)
+{
+	struct gsm_sms *sms;
+	dbi_result result;
+
+	LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 5\n");
+
+	result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR,
+		     "Failed to begin transaction (upgrade from rev 5)\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_5");
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR,
+		     "Failed to rename the old SMS table (upgrade from rev 5)\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 5)\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_5");
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR,
+		     "Failed fetch messages from the old SMS table "
+		     "(upgrade from rev 5)\n");
+		goto rollback;
+	}
+	while (next_row(result)) {
+		sms = sms_from_result_v5(result);
+		if (db_sms_store(sms) != 0) {
+			LOGP(DDB, LOGL_ERROR,
+			     "Failed to store message to the new SMS table "
+			     "(upgrade from rev 5)\n");
+			dbi_result_free(result);
+			sms_free(sms);
+			goto rollback;
+		}
+		sms_free(sms);
+	}
+	dbi_result_free(result);
+
+	/* Remove the temporary table */
+	result = dbi_conn_query(conn, "DROP TABLE SMS_5");
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR,
+		     "Failed to drop the old SMS table (upgrade from rev 5)\n");
+		goto rollback;
+	}
+	dbi_result_free(result);
+
+	/* We're done, bump DB Meta revision */
+	result = dbi_conn_query(conn,
+				"UPDATE Meta "
+				"SET value = '6' "
+				"WHERE key = 'revision'");
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR,
+		     "Failed to update DB schema revision (upgrade from rev 5)\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 5)\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 5)\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 5)\n");
+	else
+		dbi_result_free(result);
+	return -EINVAL;
+}
+
 static int check_db_revision(void)
 {
 	dbi_result result;
@@ -603,6 +729,10 @@
 	case 4:
 		if (update_db_revision_4())
 			goto error;
+	/* fall through */
+	case 5:
+		if (update_db_revision_5())
+			goto error;
 
 	/* The end of waterfall */
 	break;

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

Gerrit-Project: osmo-msc
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I747a795ca486f3fcfa7583c5ab8aa2aee07a8ec2
Gerrit-Change-Number: 13632
Gerrit-PatchSet: 1
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190414/6ec734a4/attachment.htm>


More information about the gerrit-log mailing list