blob: e24b619c644afcf5885b2beff1842f49984fc381 [file] [log] [blame]
Holger Freyther12aa50d2009-01-01 18:02:05 +00001/* Simple HLR/VLR database backend using dbi */
Jan Luebbefaaa49c2008-12-27 01:07:07 +00002/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00003 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltec2e302d2009-07-05 14:08:13 +02004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte7e310b12009-03-30 20:56:32 +000023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020024#include <openbsc/gsm_04_11.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000025#include <openbsc/db.h>
Harald Welte76042182009-08-08 16:03:15 +020026#include <openbsc/talloc.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000027
Holger Freytherbde36102008-12-28 22:51:39 +000028#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000030#include <stdlib.h>
31#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000032#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000033#include <dbi/dbi.h>
34
Holger Freytherbde36102008-12-28 22:51:39 +000035static char *db_basename = NULL;
36static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000037static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000038
Harald Welte7e310b12009-03-30 20:56:32 +000039static char *create_stmts[] = {
40 "CREATE TABLE IF NOT EXISTS Meta ("
41 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
42 "key TEXT UNIQUE NOT NULL, "
43 "value TEXT NOT NULL"
44 ")",
45 "INSERT OR IGNORE INTO Meta "
46 "(key, value) "
47 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020048 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000049 "CREATE TABLE IF NOT EXISTS Subscriber ("
50 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
51 "created TIMESTAMP NOT NULL, "
52 "updated TIMESTAMP NOT NULL, "
53 "imsi NUMERIC UNIQUE NOT NULL, "
54 "name TEXT, "
55 "extension TEXT UNIQUE, "
56 "authorized INTEGER NOT NULL DEFAULT 0, "
57 "tmsi TEXT UNIQUE, "
58 "lac INTEGER NOT NULL DEFAULT 0"
59 ")",
60 "CREATE TABLE IF NOT EXISTS Equipment ("
61 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
62 "created TIMESTAMP NOT NULL, "
63 "updated TIMESTAMP NOT NULL, "
64 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020065 "classmark1 NUMERIC, "
66 "classmark2 BLOB, "
67 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000068 "imei NUMERIC UNIQUE NOT NULL"
69 ")",
70 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
71 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
72 "created TIMESTAMP NOT NULL, "
73 "updated TIMESTAMP NOT NULL, "
74 "subscriber_id NUMERIC NOT NULL, "
75 "equipment_id NUMERIC NOT NULL, "
76 "UNIQUE (subscriber_id, equipment_id) "
77 ")",
78 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020079 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000080 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
81 "created TIMESTAMP NOT NULL, "
82 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020083 "sender_id INTEGER NOT NULL, "
84 "receiver_id INTEGER NOT NULL, "
85 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020086 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020087 "reply_path_req INTEGER NOT NULL, "
88 "status_rep_req INTEGER NOT NULL, "
89 "protocol_id INTEGER NOT NULL, "
90 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020091 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +020092 "dest_addr TEXT, "
93 "user_data BLOB, " /* TP-UD */
94 /* additional data, interpreted from SMS */
95 "header BLOB, " /* UD Header */
96 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +000097 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +000098 "CREATE TABLE IF NOT EXISTS VLR ("
99 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
100 "created TIMESTAMP NOT NULL, "
101 "updated TIMESTAMP NOT NULL, "
102 "subscriber_id NUMERIC UNIQUE NOT NULL, "
103 "last_bts NUMERIC NOT NULL "
104 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000105};
106
Holger Freyther12aa50d2009-01-01 18:02:05 +0000107void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000108 const char* msg;
109 dbi_conn_error(conn, &msg);
110 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000111}
112
Harald Welted0b7b772009-08-09 19:03:42 +0200113static int check_db_revision(void)
114{
115 dbi_result result;
116 const char *rev;
117
118 result = dbi_conn_query(conn,
119 "SELECT value FROM Meta WHERE key='revision'");
120 if (!result)
121 return -EINVAL;
122
123 if (!dbi_result_next_row(result)) {
124 dbi_result_free(result);
125 return -EINVAL;
126 }
127 rev = dbi_result_get_string(result, "value");
128 if (!rev || atoi(rev) != 2) {
129 dbi_result_free(result);
130 return -EINVAL;
131 }
132
133 dbi_result_free(result);
134 return 0;
135}
136
Holger Freytherc7b86f92009-06-06 13:54:20 +0000137int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000138 dbi_initialize(NULL);
139 conn = dbi_conn_new("sqlite3");
140 if (conn==NULL) {
141 printf("DB: Failed to create connection.\n");
142 return 1;
143 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000144
Holger Freyther12aa50d2009-01-01 18:02:05 +0000145 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000146
Jan Luebbe5c15c852008-12-27 15:59:25 +0000147 /* MySQL
148 dbi_conn_set_option(conn, "host", "localhost");
149 dbi_conn_set_option(conn, "username", "your_name");
150 dbi_conn_set_option(conn, "password", "your_password");
151 dbi_conn_set_option(conn, "dbname", "your_dbname");
152 dbi_conn_set_option(conn, "encoding", "UTF-8");
153 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000154
Jan Luebbe5c15c852008-12-27 15:59:25 +0000155 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000156 db_basename = strdup(name);
157 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000158 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
159 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000160
Harald Welted0b7b772009-08-09 19:03:42 +0200161 if (dbi_conn_connect(conn) < 0)
162 goto out_err;
163
Jan Luebbe5c15c852008-12-27 15:59:25 +0000164 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200165
166out_err:
167 free(db_dirname);
168 free(db_basename);
169 db_dirname = db_basename = NULL;
170 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000171}
172
Harald Welted0b7b772009-08-09 19:03:42 +0200173
Jan Luebbe7398eb92008-12-27 00:45:41 +0000174int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000175 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000176 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000177
Harald Welte7e310b12009-03-30 20:56:32 +0000178 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
179 result = dbi_conn_query(conn, create_stmts[i]);
180 if (result==NULL) {
181 printf("DB: Failed to create some table.\n");
182 return 1;
183 }
184 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000185 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000186
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200187 if (check_db_revision() < 0) {
188 fprintf(stderr, "Database schema revision invalid, "
189 "please update your database schema\n");
190 return -1;
191 }
192
Jan Luebbe5c15c852008-12-27 15:59:25 +0000193 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000194}
195
196int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000197 dbi_conn_close(conn);
198 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000199
200 if (db_dirname)
201 free(db_dirname);
202 if (db_basename)
203 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000204 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000205}
206
Harald Welte9176bd42009-07-23 18:46:00 +0200207struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
208{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000209 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000210 struct gsm_subscriber* subscr;
211
212 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200213 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000214 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000215 result = dbi_conn_queryf(conn,
216 "UPDATE Subscriber set updated = datetime('now') "
217 "WHERE imsi = %s " , imsi);
218 if (result==NULL) {
219 printf("DB: failed to update timestamp\n");
220 } else {
221 dbi_result_free(result);
222 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000223 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225
226 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200227 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000228 if (!subscr)
229 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000230 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000231 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000232 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000233 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000234 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000235 imsi
236 );
237 if (result==NULL) {
238 printf("DB: Failed to create Subscriber by IMSI.\n");
239 }
Harald Welte9176bd42009-07-23 18:46:00 +0200240 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000241 subscr->id = dbi_conn_sequence_last(conn, NULL);
242 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000243 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000244 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
245 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000246}
247
Harald Welte9176bd42009-07-23 18:46:00 +0200248struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
249 enum gsm_subscriber_field field,
250 const char *id)
251{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000252 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000253 const char *string;
254 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000256
Jan Luebbe5c15c852008-12-27 15:59:25 +0000257 switch (field) {
258 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000260 result = dbi_conn_queryf(conn,
261 "SELECT * FROM Subscriber "
262 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000263 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000264 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000265 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000266 break;
267 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000268 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000269 result = dbi_conn_queryf(conn,
270 "SELECT * FROM Subscriber "
271 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000272 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000273 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000274 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000275 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000276 case GSM_SUBSCRIBER_EXTENSION:
277 dbi_conn_quote_string_copy(conn, id, &quoted);
278 result = dbi_conn_queryf(conn,
279 "SELECT * FROM Subscriber "
280 "WHERE extension = %s ",
281 quoted
282 );
283 free(quoted);
284 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200285 case GSM_SUBSCRIBER_ID:
286 dbi_conn_quote_string_copy(conn, id, &quoted);
287 result = dbi_conn_queryf(conn,
288 "SELECT * FROM Subscriber "
289 "WHERE id = %s ", quoted);
290 free(quoted);
291 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000292 default:
293 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000294 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000295 }
296 if (result==NULL) {
297 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000298 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000299 }
300 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000301 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
302 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000303 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000304 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000305 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000306
307 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200308 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000309 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000310 string = dbi_result_get_string(result, "imsi");
311 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000312 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000313
314 string = dbi_result_get_string(result, "tmsi");
315 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000316 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000317
318 string = dbi_result_get_string(result, "name");
319 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000320 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000321
322 string = dbi_result_get_string(result, "extension");
323 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000324 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000325
Holger Freyther12aa50d2009-01-01 18:02:05 +0000326 subscr->lac = dbi_result_get_uint(result, "lac");
327 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000328 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
329 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000330 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000331 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000332 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000333}
334
Holger Freyther12aa50d2009-01-01 18:02:05 +0000335int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 dbi_result result;
337 result = dbi_conn_queryf(conn,
338 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000339 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000340 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000341 "lac = %i, "
342 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000343 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000344 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000345 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000346
Jan Luebbe5c15c852008-12-27 15:59:25 +0000347 if (result==NULL) {
348 printf("DB: Failed to update Subscriber (by IMSI).\n");
349 return 1;
350 }
351 dbi_result_free(result);
352 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000353}
354
Harald Weltec2e302d2009-07-05 14:08:13 +0200355int db_sync_equipment(struct gsm_equipment *equip)
356{
357 dbi_result result;
358 unsigned char *cm2, *cm3;
359
360 dbi_conn_quote_binary_copy(conn, equip->classmark2,
361 equip->classmark2_len, &cm2);
362 dbi_conn_quote_binary_copy(conn, equip->classmark3,
363 equip->classmark3_len, &cm3);
364
365 result = dbi_conn_queryf(conn,
366 "UPDATE Equipment SET "
367 "updated = datetime('now'), "
368 "classmark1 = %u, "
369 "classmark2 = %s, "
370 "classmark3 = %s "
371 "WHERE imei = '%s' ",
372 equip->classmark1, cm2, cm3, equip->imei);
373
374 free(cm2);
375 free(cm3);
376
377 if (!result) {
378 printf("DB: Failed to update Equipment\n");
379 return -EIO;
380 }
381
382 dbi_result_free(result);
383 return 0;
384}
385
Jan Luebbe5c15c852008-12-27 15:59:25 +0000386int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000387 dbi_result result=NULL;
388 char* tmsi_quoted;
389 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000390 sprintf(subscriber->tmsi, "%i", rand());
391 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000392 result = dbi_conn_queryf(conn,
393 "SELECT * FROM Subscriber "
394 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000395 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000396 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000397 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000398 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000399 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000400 return 1;
401 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000402 if (dbi_result_get_numrows(result)){
403 dbi_result_free(result);
404 continue;
405 }
406 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000407 dbi_result_free(result);
408 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000409 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000410 }
411 dbi_result_free(result);
412 }
413 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000414}
415
Jan Luebbefac25fc2008-12-27 18:04:34 +0000416int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
417 u_int64_t equipment_id, watch_id;
418 dbi_result result;
419
Harald Weltec2e302d2009-07-05 14:08:13 +0200420 strncpy(subscriber->equipment.imei, imei,
421 sizeof(subscriber->equipment.imei)-1),
422
Jan Luebbefac25fc2008-12-27 18:04:34 +0000423 result = dbi_conn_queryf(conn,
424 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000425 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000426 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000427 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000428 imei
429 );
430 if (result==NULL) {
431 printf("DB: Failed to create Equipment by IMEI.\n");
432 return 1;
433 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000434 equipment_id = 0;
435 if (dbi_result_get_numrows_affected(result)) {
436 equipment_id = dbi_conn_sequence_last(conn, NULL);
437 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000438 dbi_result_free(result);
439 if (equipment_id) {
440 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
441 }
442 else {
443 result = dbi_conn_queryf(conn,
444 "SELECT id FROM Equipment "
445 "WHERE imei = %s ",
446 imei
447 );
448 if (result==NULL) {
449 printf("DB: Failed to query Equipment by IMEI.\n");
450 return 1;
451 }
452 if (!dbi_result_next_row(result)) {
453 printf("DB: Failed to find the Equipment.\n");
454 dbi_result_free(result);
455 return 1;
456 }
457 equipment_id = dbi_result_get_ulonglong(result, "id");
458 dbi_result_free(result);
459 }
460
461 result = dbi_conn_queryf(conn,
462 "INSERT OR IGNORE INTO EquipmentWatch "
463 "(subscriber_id, equipment_id, created, updated) "
464 "VALUES "
465 "(%llu, %llu, datetime('now'), datetime('now')) ",
466 subscriber->id, equipment_id
467 );
468 if (result==NULL) {
469 printf("DB: Failed to create EquipmentWatch.\n");
470 return 1;
471 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000472 watch_id = 0;
473 if (dbi_result_get_numrows_affected(result)) {
474 watch_id = dbi_conn_sequence_last(conn, NULL);
475 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000476 dbi_result_free(result);
477 if (watch_id) {
478 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
479 }
480 else {
481 result = dbi_conn_queryf(conn,
482 "UPDATE EquipmentWatch "
483 "SET updated = datetime('now') "
484 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
485 subscriber->id, equipment_id
486 );
487 if (result==NULL) {
488 printf("DB: Failed to update EquipmentWatch.\n");
489 return 1;
490 }
491 dbi_result_free(result);
492 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
493 }
494
495 return 0;
496}
497
Harald Welte7e310b12009-03-30 20:56:32 +0000498/* store an [unsent] SMS to the database */
499int db_sms_store(struct gsm_sms *sms)
500{
501 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200502 char *q_text, *q_daddr;
503 unsigned char *q_udata;
504 char *validity_timestamp = "2222-2-2";
505
506 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000507
508 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200509 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
510 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
511 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200512 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000513 result = dbi_conn_queryf(conn,
514 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200515 "(created, sender_id, receiver_id, valid_until, "
516 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200517 "data_coding_scheme, ud_hdr_ind, dest_addr, "
518 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200519 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200520 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000521 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200522 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
523 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200524 sms->data_coding_scheme, sms->ud_hdr_ind,
525 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000526 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200527 free(q_daddr);
528 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000529
530 if (!result)
531 return -EIO;
532
533 dbi_result_free(result);
534 return 0;
535}
536
Harald Welte2ebabca2009-08-09 19:05:21 +0200537static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000538{
Harald Welte76042182009-08-08 16:03:15 +0200539 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200540 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200541 const char *text, *daddr;
542 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000543
Harald Welte76042182009-08-08 16:03:15 +0200544 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000545 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000546
Harald Weltebe3e3782009-07-05 14:06:41 +0200547 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000548
Harald Weltebe3e3782009-07-05 14:06:41 +0200549 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200550 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200551
552 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200553 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200554
Harald Weltef3efc592009-07-27 20:11:35 +0200555 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200556 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
557 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
558 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
559 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
560 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
561 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200562 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200563 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200564
Harald Welte76042182009-08-08 16:03:15 +0200565 daddr = dbi_result_get_string(result, "dest_addr");
566 if (daddr) {
567 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
568 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
569 }
570
571 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
572 user_data = dbi_result_get_binary(result, "user_data");
573 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200574 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200575 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200576
577 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200578 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200579 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200580 sms->text[sizeof(sms->text)-1] = '\0';
581 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200582 return sms;
583}
584
585/* retrieve the next unsent SMS with ID >= min_id */
586struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
587{
588 dbi_result result;
589 struct gsm_sms *sms;
590
591 result = dbi_conn_queryf(conn,
592 "SELECT * FROM SMS "
593 "WHERE id >= %llu AND sent is NULL ORDER BY id",
594 min_id);
595 if (!result)
596 return NULL;
597
598 if (!dbi_result_next_row(result)) {
599 dbi_result_free(result);
600 return NULL;
601 }
602
603 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000604
605 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200606
607 return sms;
608}
609
610/* retrieve the next unsent SMS with ID >= min_id */
611struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
612{
613 dbi_result result;
614 struct gsm_sms *sms;
615
616 result = dbi_conn_queryf(conn,
617 "SELECT * FROM SMS "
618 "WHERE receiver_id = %llu AND sent is NULL ORDER BY id",
619 subscr->id);
620 if (!result)
621 return NULL;
622
623 if (!dbi_result_next_row(result)) {
624 dbi_result_free(result);
625 return NULL;
626 }
627
628 sms = sms_from_result(subscr->net, result);
629
630 dbi_result_free(result);
631
Harald Welte7e310b12009-03-30 20:56:32 +0000632 return sms;
633}
634
635/* mark a given SMS as read */
636int db_sms_mark_sent(struct gsm_sms *sms)
637{
638 dbi_result result;
639
640 result = dbi_conn_queryf(conn,
641 "UPDATE SMS "
642 "SET sent = datetime('now') "
643 "WHERE id = %llu", sms->id);
644 if (!result) {
645 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
646 return 1;
647 }
648
649 dbi_result_free(result);
650 return 0;
651}