aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/db.c
blob: b12889f7a9a31f29014463b92ee8803ab4d65465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
/* Simple HLR/VLR database backend using sqlite3 */
/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2009,2022 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 <time.h>
#include <sqlite3.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>

enum stmt_idx {
	DB_STMT_SMS_STORE,
	DB_STMT_SMS_GET,
	DB_STMT_SMS_GET_NEXT_UNSENT,
	DB_STMT_SMS_GET_UNSENT_FOR_SUBSCR,
	DB_STMT_SMS_GET_NEXT_UNSENT_RR_MSISDN,
	DB_STMT_SMS_MARK_DELIVERED,
	DB_STMT_SMS_INC_DELIVER_ATTEMPTS,
	DB_STMT_SMS_DEL_BY_MSISDN,
	DB_STMT_SMS_DEL_BY_ID,
	DB_STMT_SMS_DEL_EXPIRED,
	DB_STMT_SMS_GET_VALID_UNTIL_BY_ID,
	DB_STMT_SMS_GET_OLDEST_EXPIRED,
	_NUM_DB_STMT
};

struct db_context {
	char *fname;
	sqlite3 *db;
	sqlite3_stmt *stmt[_NUM_DB_STMT];
};

static struct db_context *g_dbc;


/***********************************************************************
 * DATABASE SCHEMA AND MIGRATION
 ***********************************************************************/

#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 "
		")",
};

#if 0
static void parse_tp_ud_from_result(struct gsm_sms *sms, sqlite3_stmtp *stmt);

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 *daddr;
	char buf[32];
	char *quoted;
	dbi_result result2;
	const char *extension;

	if (!sms)
		return NULL;

	sms->id = dbi_result_get_ulonglong(result, "id");

	/* 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);

	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_ARRAY(sms->src.addr, extension);
	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_ARRAY(sms->dst.addr, daddr);

	/* Parse TP-UD, TP-UDL and decoded text */
	parse_tp_ud_from_result(sms, result);

	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;
	}
	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 char *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_ARRAY(sms->src.addr, 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_ARRAY(sms->dst.addr, addr);
	sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
	sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");

	/* Parse TP-UD, TP-UDL and decoded text */
	parse_tp_ud_from_result(sms, result);

	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;
	}
	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;
}
#endif


/***********************************************************************
 * PREPARED STATEMENTS
 ***********************************************************************/

/* don't change this order as the code assumes this ordering when dereferencing
 * database query results! */
#define SEL_COLUMNS \
	"id," \
	"strftime('%s',created)," \
	"sent," \
	"deliver_attempts," \
	"strftime('%s',valid_until)," \
	"reply_path_req," \
	"status_rep_req," \
	"is_report," \
	"msg_ref," \
	"protocol_id," \
	"data_coding_scheme," \
	"ud_hdr_ind," \
	"src_addr," \
	"src_ton," \
	"src_npi," \
	"dest_addr," \
	"dest_ton," \
	"dest_npi," \
	"user_data," \
	"header," \
	"text"

enum db_sms_column_idx {
	COL_ID,
	COL_CREATED,
	COL_SENT,
	COL_DELIVER_ATTEMPTS,
	COL_VALID_UNTIL,
	COL_REPLY_PATH_REQ,
	COL_STATUS_REP_REQ,
	COL_IS_REPORT,
	COL_MSG_REF,
	COL_PROTOCOL_ID,
	COL_DATA_CODING_SCHEME,
	COL_UD_HDR_IND,
	COL_SRC_ADDR,
	COL_SRC_TON,
	COL_SRC_NPI,
	COL_DEST_ADDR,
	COL_DEST_TON,
	COL_DEST_NPI,
	COL_USER_DATA,
	COL_HEADER,
	COL_TEXT,
};

static const char *stmt_sql[] = {
	[DB_STMT_SMS_STORE] =
		"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($created, 'unixepoch'), datetime($valid_until, 'unixepoch'), "
		 "$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)",
	[DB_STMT_SMS_GET] = "SELECT " SEL_COLUMNS " FROM SMS WHERE SMS.id = $id",
	[DB_STMT_SMS_GET_NEXT_UNSENT] =
		"SELECT " SEL_COLUMNS " FROM SMS"
		" WHERE sent IS NULL"
		" AND id >= $id"
		" AND deliver_attempts <= $attempts"
		" ORDER BY id LIMIT 1",
	[DB_STMT_SMS_GET_UNSENT_FOR_SUBSCR] =
		"SELECT " SEL_COLUMNS " FROM SMS"
		" WHERE sent IS NULL"
		" AND dest_addr = $dest_addr"
		" AND deliver_attempts <= $attempts"
		" ORDER BY id LIMIT 1",
	[DB_STMT_SMS_GET_NEXT_UNSENT_RR_MSISDN] =
		"SELECT " SEL_COLUMNS " FROM SMS"
		" WHERE sent IS NULL"
		" AND dest_addr > $dest_addr"
		" AND deliver_attempts <= $attempts"
		" ORDER BY dest_addr, id LIMIT 1",
	[DB_STMT_SMS_MARK_DELIVERED] =
		"UPDATE SMS "
		" SET sent = datetime('now') "
		" WHERE id = $id",
	[DB_STMT_SMS_INC_DELIVER_ATTEMPTS] =
		"UPDATE SMS "
		" SET deliver_attempts = deliver_attempts + 1 "
		" WHERE id = $id",
	[DB_STMT_SMS_DEL_BY_MSISDN] =
		"DELETE FROM SMS WHERE src_addr=$src_addr OR dest_addr=$dest_addr",
	[DB_STMT_SMS_DEL_BY_ID] =
		"DELETE FROM SMS WHERE id = $id AND sent is NOT NULL",
	[DB_STMT_SMS_DEL_EXPIRED] =
		"DELETE FROM SMS WHERE id = $id",
	[DB_STMT_SMS_GET_VALID_UNTIL_BY_ID] =
		"SELECT strftime('%s',valid_until) FROM SMS WHERE id = $id",
	[DB_STMT_SMS_GET_OLDEST_EXPIRED] =
		"SELECT id, strftime('%s',valid_until) FROM SMS ORDER BY valid_until LIMIT 1",
};

/***********************************************************************
 * libsqlite3 helpers
 ***********************************************************************/

/* libsqlite3 call-back for error logging */
static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
{
	LOGP(DDB, LOGL_ERROR, "SQLITE3: (%d) %s\n", err_code, msg);
	osmo_log_backtrace(DDB, LOGL_ERROR);
}

/* libsqlite3 call-back for normal logging */
static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
{
	switch (type) {
	case 0:
		LOGP(DDB, LOGL_DEBUG, "Opened database\n");
		break;
	case 1:
		LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
		break;
	case 2:
		LOGP(DDB, LOGL_DEBUG, "Closed database\n");
		break;
	default:
		LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
		break;
	}
}

/* remove statement bindings and reset statement to be re-executed */
static void db_remove_reset(sqlite3_stmt *stmt)
{
	sqlite3_clear_bindings(stmt);
	/* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
	/* coverity[CHECKED_RETURN] */
	sqlite3_reset(stmt);
}

/** bind blob arg and do proper cleanup in case of failure. If param_name is
 * NULL, bind to the first parameter (useful for SQL statements that have only
 * one parameter). */
static bool db_bind_blob(sqlite3_stmt *stmt, const char *param_name,
			 const uint8_t *blob, size_t blob_len)
{
	int rc;
	int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
	if (idx < 1) {
		LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
		     param_name);
		return false;
	}
	rc = sqlite3_bind_blob(stmt, idx, blob, blob_len, SQLITE_STATIC);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Error binding blob to SQL parameter %s: %d\n",
		     param_name ? param_name : "#1", rc);
		db_remove_reset(stmt);
		return false;
	}
	return true;
}

/** bind text arg and do proper cleanup in case of failure. If param_name is
 * NULL, bind to the first parameter (useful for SQL statements that have only
 * one parameter). */
static bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
{
	int rc;
	int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
	if (idx < 1) {
		LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
		     param_name);
		return false;
	}
	rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
		     param_name ? param_name : "#1", rc);
		db_remove_reset(stmt);
		return false;
	}
	return true;
}

/** bind int arg and do proper cleanup in case of failure. If param_name is
 * NULL, bind to the first parameter (useful for SQL statements that have only
 * one parameter). */
static bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
{
	int rc;
	int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
	if (idx < 1) {
		LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
		     param_name);
		return false;
	}
	rc = sqlite3_bind_int(stmt, idx, nr);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
		     param_name ? param_name : "#1", rc);
		db_remove_reset(stmt);
		return false;
	}
	return true;
}

/** bind int64 arg and do proper cleanup in case of failure. If param_name is
 * NULL, bind to the first parameter (useful for SQL statements that have only
 * one parameter). */
static bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
{
	int rc;
	int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
	if (idx < 1) {
		LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
		     param_name);
		return false;
	}
	rc = sqlite3_bind_int64(stmt, idx, nr);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
		     param_name ? param_name : "#1", rc);
		db_remove_reset(stmt);
		return false;
	}
	return true;
}

/* callback for sqlite3_exec() below */
static int db_rev_exec_cb(void *priv, int num_cols, char **vals, char **names)
{
	char **rev_s = priv;
	OSMO_ASSERT(!strcmp(names[0], "value"));
	*rev_s = talloc_strdup(NULL, vals[0]);
	return 0;
}

static int check_db_revision(struct db_context *dbc)
{
	char *errstr = NULL;
	char *rev_s;
	//int db_rev = 0;
	int rc;

	/* Make a query */
	rc = sqlite3_exec(dbc->db, "SELECT value FROM Meta WHERE key = 'revision'",
			  db_rev_exec_cb, &rev_s, &errstr);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Cannot execute SELECT value from META: %s\n", errstr);
		sqlite3_free(errstr);
		return -EINVAL;
	}

	if (!strcmp(rev_s, SCHEMA_REVISION)) {
		/* Everything is fine */
		talloc_free(rev_s);
		return 0;
	}

	LOGP(DDB, LOGL_NOTICE, "Detected DB Revision %s, expected %s\n", rev_s, SCHEMA_REVISION);

	OSMO_ASSERT(!strcmp(rev_s, SCHEMA_REVISION));
#if 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;
	/* fall through */
	case 3:
		if (update_db_revision_3())
			goto error;
	/* fall through */
	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);
#endif
	talloc_free(rev_s);

	return -EINVAL;
}

/***********************************************************************
 * USER API
 ***********************************************************************/

int db_init(void *ctx, const char *fname, bool enable_sqlite_logging)
{
	unsigned int i;
	int rc;
	bool has_sqlite_config_sqllog = false;

	g_dbc = talloc_zero(ctx, struct db_context);
	OSMO_ASSERT(g_dbc);

	/* we are a single-threaded program; we want to avoid all the mutex/etc. overhead */
	sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);

	LOGP(DDB, LOGL_NOTICE, "Init database connection to '%s' using SQLite3 lib version %s\n",
	     fname, sqlite3_libversion());

	g_dbc->fname = talloc_strdup(g_dbc, fname);

	for (i = 0; i < 0xfffff; i++) {
		const char *o = sqlite3_compileoption_get(i);
		if (!o)
			break;
		LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
		if (!strcmp(o, "ENABLE_SQLLOG"))
			has_sqlite_config_sqllog = true;
	}

	if (enable_sqlite_logging) {
		rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
		if (rc != SQLITE_OK)
			LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
	}

	if (has_sqlite_config_sqllog) {
		rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
		if (rc != SQLITE_OK)
			LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
	} else {
		LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
		     " SQLite3 compiled without support for it\n");
	}

	rc = sqlite3_open(g_dbc->fname, &g_dbc->db);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc =%d\n", rc);
		talloc_free(g_dbc);
		return -1;
	}

	/* enable extended result codes */
	rc = sqlite3_extended_result_codes(g_dbc->db, 1);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
		/* non-fatal */
	}

	char *err_msg;
	rc = sqlite3_exec(g_dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
	if (rc != SQLITE_OK) {
		LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", err_msg);
		sqlite3_free(err_msg);
		/* non-fatal */
	}

	return 0;
}

int db_fini(void)
{
	unsigned int i;
	int rc;

	if (!g_dbc)
		return 0;

	for (i = 0; i < ARRAY_SIZE(g_dbc->stmt); i++) {
		/* it is ok to call finalize on NULL */
		sqlite3_finalize(g_dbc->stmt[i]);
	}

	/* Ask sqlite3 to close DB */
	rc = sqlite3_close(g_dbc->db);
	if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
		LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
			rc, sqlite3_errmsg(g_dbc->db));
	}

	talloc_free(g_dbc);
	g_dbc = NULL;

	return 0;
}

/* run (execute) a series of SQL statements */
static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count)
{
	int rc = 0;
	int i;
	for (i = 0; i < statements_count; i++) {
		const char *stmt_str = statements[i];
		sqlite3_stmt *stmt;

		rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL);
		if (rc != SQLITE_OK) {
			LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str);
			return rc;
		}
		rc = sqlite3_step(stmt);
		db_remove_reset(stmt);
		sqlite3_finalize(stmt);
		if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
			LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'",
			     rc, sqlite3_errmsg(dbc->db), stmt_str);
			return rc;
		}
	}
	return rc;
}

static int db_configure(struct db_context *dbc)
{
	const char *sync_stmts[] = { "PRAGMA synchronous = FULL" };

	return db_run_statements(dbc, sync_stmts, ARRAY_SIZE(sync_stmts));
}

int db_prepare(void)
{
	unsigned int i;
	int rc;

	OSMO_ASSERT(g_dbc);
	rc = db_run_statements(g_dbc, create_stmts, ARRAY_SIZE(create_stmts));
	if (rc != SQLITE_DONE) {
		LOGP(DDB, LOGL_ERROR, "Failed to create some table.\n");
		return 1;
	}

	if (check_db_revision(g_dbc) < 0) {
		LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
			"please update your database schema\n");
                return -1;
	}

	db_configure(g_dbc);

	/* prepare all SQL statements */
	for (i = 0; i < ARRAY_SIZE(g_dbc->stmt); i++) {
		rc = sqlite3_prepare_v2(g_dbc->db, stmt_sql[i], -1,
					&g_dbc->stmt[i], NULL);
		if (rc != SQLITE_OK) {
			LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
			return -1;
		}
	}

	return 0;
}

/* store an [unsent] SMS to the database */
int db_sms_store(struct gsm_sms *sms)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_STORE];
	time_t now, validity_timestamp;
	int rc;

	now = time(NULL);
	validity_timestamp = now + sms->validity_minutes * 60;

	db_bind_int64(stmt, "$created", (int64_t) now);
	db_bind_int64(stmt, "$valid_until", (int64_t) validity_timestamp);
	db_bind_int(stmt, "$reply_path_req", sms->reply_path_req);
	db_bind_int(stmt, "$status_rep_req", sms->status_rep_req);
	db_bind_int(stmt, "$is_report", sms->is_report);
	db_bind_int(stmt, "$msg_ref", sms->msg_ref);
	db_bind_int(stmt, "$protocol_id", sms->protocol_id);
	db_bind_int(stmt, "$data_coding_scheme", sms->data_coding_scheme);
	db_bind_int(stmt, "$ud_hdr_ind", sms->ud_hdr_ind);
	/* FIXME: do we need to use legacy DBI compatible quoting of sms->user_data? */
	db_bind_blob(stmt, "$user_data", sms->user_data, sms->user_data_len);
	db_bind_text(stmt, "$text", (char *)sms->text);
	db_bind_text(stmt, "$dest_addr", (char *)sms->dst.addr);
	db_bind_int(stmt, "$dest_ton", sms->dst.ton);
	db_bind_int(stmt, "$dest_npi", sms->dst.npi);
	db_bind_text(stmt, "$src_addr", (char *)sms->src.addr);
	db_bind_int(stmt, "$src_ton", sms->src.ton);
	db_bind_int(stmt, "$src_npi", sms->src.npi);

	/* execute statement */
	rc = sqlite3_step(stmt);
	db_remove_reset(stmt);
	if (rc != SQLITE_DONE) {
		LOGP(DDB, LOGL_ERROR, "Cannot create SMS: SQL error: (%d) %s\n", rc, sqlite3_errmsg(g_dbc->db));
		return -EIO;
	}

	sms->id = sqlite3_last_insert_rowid(g_dbc->db);

	LOGP(DLSMS, LOGL_INFO, "Stored SMS id=%llu in DB\n", sms->id);

	return 0;
}

static void parse_tp_ud_from_result(struct gsm_sms *sms, sqlite3_stmt *stmt)
{
	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 = sqlite3_column_bytes(stmt, COL_USER_DATA);
	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 */
	if (user_data_len > 0) {
		user_data = sqlite3_column_blob(stmt, COL_USER_DATA);
		memcpy(sms->user_data, user_data, user_data_len);
	}

	/* Retrieve the text length (excluding '\0') */
	text_len = sqlite3_column_bytes(stmt, COL_TEXT);
	if (text_len >= sizeof(sms->text)) {
		LOGP(DDB, LOGL_ERROR,
		     "SMS text length %u is too big, truncating to %zu\n",
		     text_len, sizeof(sms->text) - 1);
		/* OSMO_STRLCPY_ARRAY() does truncation for us */
	}

	/* Retrieve the text parsed from TP-UD (User-Data) */
	text = (const char *)sqlite3_column_text(stmt, COL_TEXT);
	if (text)
		OSMO_STRLCPY_ARRAY(sms->text, text);
}

static struct gsm_sms *sms_from_result(struct gsm_network *net, sqlite3_stmt *stmt)
{
	struct gsm_sms *sms = sms_alloc();
	const char *daddr, *saddr;
	time_t validity_timestamp;

	if (!sms)
		return NULL;

	sms->id = sqlite3_column_int64(stmt, COL_ID);

	sms->created = sqlite3_column_int64(stmt, COL_CREATED);
	validity_timestamp = sqlite3_column_int64(stmt, COL_VALID_UNTIL);

	sms->validity_minutes = (validity_timestamp - sms->created) / 60;
	sms->reply_path_req = sqlite3_column_int(stmt, COL_REPLY_PATH_REQ);
	sms->status_rep_req = sqlite3_column_int(stmt, COL_STATUS_REP_REQ);
	sms->is_report = sqlite3_column_int(stmt, COL_IS_REPORT);
	sms->msg_ref = sqlite3_column_int(stmt, COL_MSG_REF);
	sms->ud_hdr_ind = sqlite3_column_int(stmt, COL_UD_HDR_IND);
	sms->protocol_id = sqlite3_column_int(stmt, COL_PROTOCOL_ID);
	sms->data_coding_scheme = sqlite3_column_int(stmt, COL_DATA_CODING_SCHEME);

	sms->dst.npi = sqlite3_column_int(stmt, COL_DEST_NPI);
	sms->dst.ton = sqlite3_column_int(stmt, COL_DEST_TON);
	daddr = (const char *)sqlite3_column_text(stmt, COL_DEST_ADDR);
	if (daddr)
		OSMO_STRLCPY_ARRAY(sms->dst.addr, daddr);

	if (net != NULL) /* db_sms_test passes NULL, so we need to be tolerant */
		sms->receiver = vlr_subscr_find_by_msisdn(net->vlr, sms->dst.addr,
							  VSUB_USE_SMS_RECEIVER);

	sms->src.npi = sqlite3_column_int(stmt, COL_SRC_NPI);
	sms->src.ton = sqlite3_column_int(stmt, COL_SRC_TON);
	saddr = (const char *)sqlite3_column_text(stmt, COL_SRC_ADDR);
	if (saddr)
		OSMO_STRLCPY_ARRAY(sms->src.addr, saddr);

	/* Parse TP-UD, TP-UDL and decoded text */
	parse_tp_ud_from_result(sms, stmt);

	return sms;
}

struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET];
	struct gsm_sms *sms;
	int rc;

	db_bind_int64(stmt, "$id", id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		db_remove_reset(stmt);
		return NULL;
	}

	sms = sms_from_result(net, stmt);

	db_remove_reset(stmt);
	return sms;
}

struct gsm_sms *db_sms_get_next_unsent(struct gsm_network *net,
				       unsigned long long min_sms_id,
				       unsigned int max_failed)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET_NEXT_UNSENT];
	struct gsm_sms *sms;
	int rc;

	db_bind_int64(stmt, "$id", min_sms_id);
	db_bind_int(stmt, "$attempts", max_failed);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		db_remove_reset(stmt);
		return NULL;
	}

	sms = sms_from_result(net, stmt);

	db_remove_reset(stmt);
	return sms;
}

/* retrieve the next unsent SMS for a given subscriber */
struct gsm_sms *db_sms_get_unsent_for_subscr(struct vlr_subscr *vsub,
					     unsigned int max_failed)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET_UNSENT_FOR_SUBSCR];
	struct gsm_network *net = vsub->vlr->user_ctx;
	struct gsm_sms *sms;
	int rc;

	if (!vsub->lu_complete)
		return NULL;

	/* A subscriber having no phone number cannot possibly receive SMS. */
	if (*vsub->msisdn == '\0')
		return NULL;

	db_bind_text(stmt, "$dest_addr", vsub->msisdn);
	db_bind_int(stmt, "$attempts", max_failed);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		db_remove_reset(stmt);
		return NULL;
	}

	sms = sms_from_result(net, stmt);

	db_remove_reset(stmt);
	return sms;
}

struct gsm_sms *db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
						 const char *last_msisdn,
						 unsigned int max_failed)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET_NEXT_UNSENT_RR_MSISDN];
	struct gsm_sms *sms;
	int rc;

	db_bind_text(stmt, "$dest_addr", last_msisdn);
	db_bind_int(stmt, "$attempts", max_failed);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		db_remove_reset(stmt);
		return NULL;
	}

	sms = sms_from_result(net, stmt);

	db_remove_reset(stmt);

	return sms;
}

/* mark a given SMS as delivered */
int db_sms_mark_delivered(struct gsm_sms *sms)
{
	sqlite3_stmt *stmt;
	int rc;

	/* this only happens in unit tests that don't db_init() */
	if (!g_dbc)
		return 0;

	stmt = g_dbc->stmt[DB_STMT_SMS_MARK_DELIVERED];
	db_bind_int64(stmt, "$id", sms->id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_DONE) {
		db_remove_reset(stmt);
		LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
		return 1;
	}

	db_remove_reset(stmt);
	return 0;
}

/* increase the number of attempted deliveries */
int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
{
	sqlite3_stmt *stmt;
	int rc;

	/* this only happens in unit tests that don't db_init() */
	if (!g_dbc)
		return 0;

	stmt = g_dbc->stmt[DB_STMT_SMS_INC_DELIVER_ATTEMPTS];
	db_bind_int64(stmt, "$id", sms->id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_DONE) {
		db_remove_reset(stmt);
		LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for SMS %llu.\n", sms->id);
		return 1;
	}

	db_remove_reset(stmt);
	return 0;
}

/* Drop all pending SMS to or from the given extension */
int db_sms_delete_by_msisdn(const char *msisdn)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_DEL_BY_MSISDN];
	int rc;

	if (!msisdn || !*msisdn)
		return 0;

	db_bind_text(stmt, "$src_addr", msisdn);
	db_bind_text(stmt, "$dest_addr", msisdn);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_DONE) {
		db_remove_reset(stmt);
		LOGP(DDB, LOGL_ERROR, "Failed to delete SMS for %s\n", msisdn);
		return -1;
	}

	db_remove_reset(stmt);
	return 0;
}

int db_sms_delete_sent_message_by_id(unsigned long long sms_id)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_DEL_BY_ID];
	int rc;

	db_bind_int64(stmt, "$id", sms_id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_DONE) {
		db_remove_reset(stmt);
		LOGP(DDB, LOGL_ERROR, "Failed to delete SMS %llu.\n", sms_id);
		return 1;
	}

	db_remove_reset(stmt);
	return 0;
}

static int delete_expired_sms(unsigned long long sms_id, time_t validity_timestamp)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_DEL_EXPIRED];
	time_t now;
	int rc;

	now = time(NULL);

	/* Net yet expired */
	if (validity_timestamp > now)
		return -1;

	db_bind_int64(stmt, "$id", sms_id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_DONE) {
		db_remove_reset(stmt);
		LOGP(DDB, LOGL_ERROR, "Failed to delete SMS %llu.\n", sms_id);
		return -1;
	}

	db_remove_reset(stmt);
	return 0;
}

int db_sms_delete_expired_message_by_id(unsigned long long sms_id)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET_VALID_UNTIL_BY_ID];
	time_t validity_timestamp;
	int rc;

	db_bind_int64(stmt, "$id", sms_id);

	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		db_remove_reset(stmt);
		return -1;
	}

	validity_timestamp = sqlite3_column_int64(stmt, 0);

	db_remove_reset(stmt);
	return delete_expired_sms(sms_id, validity_timestamp);
}

void db_sms_delete_oldest_expired_message(void)
{
	OSMO_ASSERT(g_dbc);
	sqlite3_stmt *stmt = g_dbc->stmt[DB_STMT_SMS_GET_OLDEST_EXPIRED];
	int rc;

	rc = sqlite3_step(stmt);
	if (rc == SQLITE_ROW) {
		unsigned long long sms_id;
		time_t validity_timestamp;

		sms_id = sqlite3_column_int64(stmt, 0);
		validity_timestamp = sqlite3_column_int64(stmt, 1);
		delete_expired_sms(sms_id, validity_timestamp);
	}

	db_remove_reset(stmt);
}