blob: cf9df11ad4248f8a8aad8803ee3b15fa25fa9509 [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>
Harald Welte (local)ee4410a2009-08-17 09:39:55 +020027#include <openbsc/debug.h>
Harald Welteffa55a42009-12-22 19:07:32 +010028#include <openbsc/statistics.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029
Holger Freytherbde36102008-12-28 22:51:39 +000030#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000031#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000032#include <stdlib.h>
33#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000034#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000035#include <dbi/dbi.h>
36
Holger Freytherbde36102008-12-28 22:51:39 +000037static char *db_basename = NULL;
38static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000039static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000040
Harald Welte7e310b12009-03-30 20:56:32 +000041static char *create_stmts[] = {
42 "CREATE TABLE IF NOT EXISTS Meta ("
43 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
44 "key TEXT UNIQUE NOT NULL, "
45 "value TEXT NOT NULL"
46 ")",
47 "INSERT OR IGNORE INTO Meta "
48 "(key, value) "
49 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020050 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000051 "CREATE TABLE IF NOT EXISTS Subscriber ("
52 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
53 "created TIMESTAMP NOT NULL, "
54 "updated TIMESTAMP NOT NULL, "
55 "imsi NUMERIC UNIQUE NOT NULL, "
56 "name TEXT, "
57 "extension TEXT UNIQUE, "
58 "authorized INTEGER NOT NULL DEFAULT 0, "
59 "tmsi TEXT UNIQUE, "
60 "lac INTEGER NOT NULL DEFAULT 0"
61 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020062 "CREATE TABLE IF NOT EXISTS AuthToken ("
63 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
64 "subscriber_id INTEGER UNIQUE NOT NULL, "
65 "created TIMESTAMP NOT NULL, "
66 "token TEXT UNIQUE NOT NULL"
67 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000068 "CREATE TABLE IF NOT EXISTS Equipment ("
69 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
70 "created TIMESTAMP NOT NULL, "
71 "updated TIMESTAMP NOT NULL, "
72 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020073 "classmark1 NUMERIC, "
74 "classmark2 BLOB, "
75 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000076 "imei NUMERIC UNIQUE NOT NULL"
77 ")",
78 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
79 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
80 "created TIMESTAMP NOT NULL, "
81 "updated TIMESTAMP NOT NULL, "
82 "subscriber_id NUMERIC NOT NULL, "
83 "equipment_id NUMERIC NOT NULL, "
84 "UNIQUE (subscriber_id, equipment_id) "
85 ")",
86 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020087 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000088 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
89 "created TIMESTAMP NOT NULL, "
90 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020091 "sender_id INTEGER NOT NULL, "
92 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020093 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020094 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020095 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020096 "reply_path_req INTEGER NOT NULL, "
97 "status_rep_req INTEGER NOT NULL, "
98 "protocol_id INTEGER NOT NULL, "
99 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200100 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200101 "dest_addr TEXT, "
102 "user_data BLOB, " /* TP-UD */
103 /* additional data, interpreted from SMS */
104 "header BLOB, " /* UD Header */
105 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000106 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000107 "CREATE TABLE IF NOT EXISTS VLR ("
108 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
109 "created TIMESTAMP NOT NULL, "
110 "updated TIMESTAMP NOT NULL, "
111 "subscriber_id NUMERIC UNIQUE NOT NULL, "
112 "last_bts NUMERIC NOT NULL "
113 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200114 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
115 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
116 "created TIMESTAMP NOT NULL, "
117 "apdu_id_flags INTEGER NOT NULL, "
118 "subscriber_id INTEGER NOT NULL, "
119 "apdu BLOB "
120 ")",
Harald Welteffa55a42009-12-22 19:07:32 +0100121 "CREATE TABLE IF NOT EXISTS Counters ("
122 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
123 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100124 "value INTEGER NOT NULL, "
125 "name TEXT NOT NULL "
Harald Welte3606cc52009-12-05 15:13:22 +0530126 "CREATE TABLE IF NOT EXISTS AuthKeys ("
127 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
128 "subscriber_id NUMERIC UNIQUE NOT NULL, "
129 "algorithm_id NUMERIC NOT NULL, "
130 "a3a8_ki BLOB "
131 ")",
132 "CREATE TABLE IF NOT EXISTS AuthTuples ("
133 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
134 "subscriber_id NUMERIC UNIQUE NOT NULL, "
135 "rand BLOB"
136 "sres BLOB"
137 "kc BLOB"
Harald Welteffa55a42009-12-22 19:07:32 +0100138 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000139};
140
Holger Freyther12aa50d2009-01-01 18:02:05 +0000141void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000142 const char* msg;
143 dbi_conn_error(conn, &msg);
144 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000145}
146
Harald Welted0b7b772009-08-09 19:03:42 +0200147static int check_db_revision(void)
148{
149 dbi_result result;
150 const char *rev;
151
152 result = dbi_conn_query(conn,
153 "SELECT value FROM Meta WHERE key='revision'");
154 if (!result)
155 return -EINVAL;
156
157 if (!dbi_result_next_row(result)) {
158 dbi_result_free(result);
159 return -EINVAL;
160 }
161 rev = dbi_result_get_string(result, "value");
162 if (!rev || atoi(rev) != 2) {
163 dbi_result_free(result);
164 return -EINVAL;
165 }
166
167 dbi_result_free(result);
168 return 0;
169}
170
Holger Freytherc7b86f92009-06-06 13:54:20 +0000171int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000172 dbi_initialize(NULL);
173 conn = dbi_conn_new("sqlite3");
174 if (conn==NULL) {
175 printf("DB: Failed to create connection.\n");
176 return 1;
177 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000178
Holger Freyther12aa50d2009-01-01 18:02:05 +0000179 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000180
Jan Luebbe5c15c852008-12-27 15:59:25 +0000181 /* MySQL
182 dbi_conn_set_option(conn, "host", "localhost");
183 dbi_conn_set_option(conn, "username", "your_name");
184 dbi_conn_set_option(conn, "password", "your_password");
185 dbi_conn_set_option(conn, "dbname", "your_dbname");
186 dbi_conn_set_option(conn, "encoding", "UTF-8");
187 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000188
Jan Luebbe5c15c852008-12-27 15:59:25 +0000189 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000190 db_basename = strdup(name);
191 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000192 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
193 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000194
Harald Welted0b7b772009-08-09 19:03:42 +0200195 if (dbi_conn_connect(conn) < 0)
196 goto out_err;
197
Jan Luebbe5c15c852008-12-27 15:59:25 +0000198 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200199
200out_err:
201 free(db_dirname);
202 free(db_basename);
203 db_dirname = db_basename = NULL;
204 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000205}
206
Harald Welted0b7b772009-08-09 19:03:42 +0200207
Jan Luebbe7398eb92008-12-27 00:45:41 +0000208int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000209 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000210 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000211
Harald Welte7e310b12009-03-30 20:56:32 +0000212 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
213 result = dbi_conn_query(conn, create_stmts[i]);
214 if (result==NULL) {
215 printf("DB: Failed to create some table.\n");
216 return 1;
217 }
218 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000219 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000220
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200221 if (check_db_revision() < 0) {
222 fprintf(stderr, "Database schema revision invalid, "
223 "please update your database schema\n");
224 return -1;
225 }
226
Jan Luebbe5c15c852008-12-27 15:59:25 +0000227 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000228}
229
230int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 dbi_conn_close(conn);
232 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000233
234 if (db_dirname)
235 free(db_dirname);
236 if (db_basename)
237 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000238 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000239}
240
Harald Welte9176bd42009-07-23 18:46:00 +0200241struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
242{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000243 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000244 struct gsm_subscriber* subscr;
245
246 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200247 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000248 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000249 result = dbi_conn_queryf(conn,
250 "UPDATE Subscriber set updated = datetime('now') "
251 "WHERE imsi = %s " , imsi);
252 if (result==NULL) {
253 printf("DB: failed to update timestamp\n");
254 } else {
255 dbi_result_free(result);
256 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000257 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000258 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259
260 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200261 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 if (!subscr)
263 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000264 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000265 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000266 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000267 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000268 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000269 imsi
270 );
271 if (result==NULL) {
272 printf("DB: Failed to create Subscriber by IMSI.\n");
273 }
Harald Welte9176bd42009-07-23 18:46:00 +0200274 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275 subscr->id = dbi_conn_sequence_last(conn, NULL);
276 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000277 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000278 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200279 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000280 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000281}
282
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200283static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
284{
285 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200286 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100287 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200288 const unsigned char *cm2, *cm3;
289 struct gsm_equipment *equip = &subscr->equipment;
290
291 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200292 "SELECT equipment.* FROM Equipment,EquipmentWatch "
293 "WHERE EquipmentWatch.equipment_id=Equipment.id "
294 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200295 "ORDER BY updated DESC", subscr->id);
296 if (!result)
297 return -EIO;
298
299 if (!dbi_result_next_row(result)) {
300 dbi_result_free(result);
301 return -ENOENT;
302 }
303
304 equip->id = dbi_result_get_ulonglong(result, "id");
305
306 string = dbi_result_get_string(result, "imei");
307 if (string)
308 strncpy(equip->imei, string, sizeof(equip->imei));
309
310 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
311 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
312
313 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
314 cm2 = dbi_result_get_binary(result, "classmark2");
315 if (equip->classmark2_len > sizeof(equip->classmark2))
316 equip->classmark2_len = sizeof(equip->classmark2);
317 memcpy(equip->classmark2, cm2, equip->classmark2_len);
318
319 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
320 cm3 = dbi_result_get_binary(result, "classmark3");
321 if (equip->classmark3_len > sizeof(equip->classmark3))
322 equip->classmark3_len = sizeof(equip->classmark3);
323 memcpy(equip->classmark3, cm3, equip->classmark3_len);
324
325 dbi_result_free(result);
326
327 return 0;
328}
Harald Welte3606cc52009-12-05 15:13:22 +0530329
330int get_authinfo_by_subscr(struct gsm_auth_info *ainfo,
331 struct gsm_subscriber *subscr)
332{
333 dbi_result result;
334 const unsigned char *a3a8_ki;
335
336 result = dbi_conn_queryf(conn,
337 "SELECT * FROM AuthKeys WHERE subscriber_id=%u",
338 subscr->id);
339 if (!result)
340 return -EIO;
341
342 if (!dbi_result_next_row(result)) {
343 dbi_result_free(result);
344 return -ENOENT;
345 }
346
347 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
348 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
349 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
350 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki_len))
351 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki_len);
352 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
353
354 dbi_result_free(result);
355
356 return 0;
357}
358
359int get_authtuple_by_subscr(struct gsm_auth_tuple *atuple,
360 struct gsm_subscriber *subscr)
361{
362 dbi_result result;
363 int len;
364 const unsigned char *blob;
365
366 result = dbi_conn_queryf(conn,
367 "SELECT * FROM AuthTuples WHERE subscriber_id=%u",
368 subscr->id);
369 if (!result)
370 return -EIO;
371
372 if (!dbi_result_next_row(result)) {
373 dbi_result_free(result);
374 return -ENOENT;
375 }
376
377 memset(atuple, 0, sizeof(atuple));
378
379 len = dbi_result_get_field_length(result, "rand");
380 if (len != sizeof(atuple->rand))
381 goto err_size;
382
383 blob = dbi_result_get_binary(result, "rand");
384 memcpy(atuple->rand, blob, len);
385
386 len = dbi_result_get_field_length(result, "sres");
387 if (len != sizeof(atuple->sres))
388 goto err_size;
389
390 blob = dbi_result_get_binary(result, "sres");
391 memcpy(atuple->sres, blob, len);
392
393 len = dbi_result_get_field_length(result, "kc");
394 if (len != sizeof(atuple->kc))
395 goto err_size;
396
397 blob = dbi_result_get_binary(result, "kc");
398 memcpy(atuple->kc, blob, len);
399
400 dbi_result_free(result);
401
402 return 0;
403
404err_size:
405 dbi_result_free(result);
406 return -EIO;
407}
408
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200409#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200410struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
411 enum gsm_subscriber_field field,
412 const char *id)
413{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000414 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000415 const char *string;
416 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000417 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000418
Jan Luebbe5c15c852008-12-27 15:59:25 +0000419 switch (field) {
420 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000421 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000422 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200423 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000424 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000425 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000426 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000427 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 break;
429 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000430 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000431 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200432 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000433 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000434 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000435 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000436 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000437 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000438 case GSM_SUBSCRIBER_EXTENSION:
439 dbi_conn_quote_string_copy(conn, id, &quoted);
440 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200441 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000442 "WHERE extension = %s ",
443 quoted
444 );
445 free(quoted);
446 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200447 case GSM_SUBSCRIBER_ID:
448 dbi_conn_quote_string_copy(conn, id, &quoted);
449 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200450 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200451 "WHERE id = %s ", quoted);
452 free(quoted);
453 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000454 default:
455 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000456 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000457 }
458 if (result==NULL) {
459 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000460 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000461 }
462 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000463 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
464 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000465 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000466 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000467 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000468
469 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200470 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000471 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000472 string = dbi_result_get_string(result, "imsi");
473 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000474 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000475
476 string = dbi_result_get_string(result, "tmsi");
477 if (string)
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200478 subscr->tmsi = tmsi_from_string(string);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000479
480 string = dbi_result_get_string(result, "name");
481 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000482 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000483
484 string = dbi_result_get_string(result, "extension");
485 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000486 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000487
Holger Freyther12aa50d2009-01-01 18:02:05 +0000488 subscr->lac = dbi_result_get_uint(result, "lac");
489 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200490 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
Holger Freyther91754472009-06-09 08:52:41 +0000491 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000492 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000493 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200494
495 get_equipment_by_subscr(subscr);
496
Holger Freyther12aa50d2009-01-01 18:02:05 +0000497 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000498}
499
Holger Freyther12aa50d2009-01-01 18:02:05 +0000500int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000501 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200502 char tmsi[14];
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200503 char *q_tmsi;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200504
505 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
506 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200507 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200508 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200509 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200510 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200511 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000512 result = dbi_conn_queryf(conn,
513 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000514 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200515 "name = '%s', "
516 "extension = '%s', "
517 "authorized = %i, "
518 "tmsi = %s, "
519 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000520 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200521 subscriber->name,
522 subscriber->extension,
523 subscriber->authorized,
524 q_tmsi,
525 subscriber->lac,
526 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000527 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200528 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000529 if (result==NULL) {
530 printf("DB: Failed to update Subscriber (by IMSI).\n");
531 return 1;
532 }
533 dbi_result_free(result);
534 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000535}
536
Harald Weltec2e302d2009-07-05 14:08:13 +0200537int db_sync_equipment(struct gsm_equipment *equip)
538{
539 dbi_result result;
540 unsigned char *cm2, *cm3;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200541 u_int8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +0200542
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200543 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200544 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200545 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200546 if (equip->classmark2_len)
547 printf(", classmark2=%s",
548 hexdump(equip->classmark2, equip->classmark2_len));
549 if (equip->classmark3_len)
550 printf(", classmark3=%s",
551 hexdump(equip->classmark3, equip->classmark3_len));
552 printf("\n");
553
Harald Weltec2e302d2009-07-05 14:08:13 +0200554 dbi_conn_quote_binary_copy(conn, equip->classmark2,
555 equip->classmark2_len, &cm2);
556 dbi_conn_quote_binary_copy(conn, equip->classmark3,
557 equip->classmark3_len, &cm3);
558
559 result = dbi_conn_queryf(conn,
560 "UPDATE Equipment SET "
561 "updated = datetime('now'), "
562 "classmark1 = %u, "
563 "classmark2 = %s, "
564 "classmark3 = %s "
565 "WHERE imei = '%s' ",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200566 classmark1, cm2, cm3, equip->imei);
Harald Weltec2e302d2009-07-05 14:08:13 +0200567
568 free(cm2);
569 free(cm3);
570
571 if (!result) {
572 printf("DB: Failed to update Equipment\n");
573 return -EIO;
574 }
575
576 dbi_result_free(result);
577 return 0;
578}
579
Jan Luebbe5c15c852008-12-27 15:59:25 +0000580int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000581 dbi_result result=NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200582 char tmsi[14];
Jan Luebbe5c15c852008-12-27 15:59:25 +0000583 char* tmsi_quoted;
584 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200585 subscriber->tmsi = rand();
586 if (subscriber->tmsi == GSM_RESERVED_TMSI)
587 continue;
588
589 sprintf(tmsi, "%u", subscriber->tmsi);
590 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000591 result = dbi_conn_queryf(conn,
592 "SELECT * FROM Subscriber "
593 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000594 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000595 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000596 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000597 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000598 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000599 return 1;
600 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000601 if (dbi_result_get_numrows(result)){
602 dbi_result_free(result);
603 continue;
604 }
605 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000606 dbi_result_free(result);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200607 printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000608 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000609 }
610 dbi_result_free(result);
611 }
612 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000613}
614
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200615int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
616 dbi_result result=NULL;
617 u_int32_t try;
618 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200619 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200620 result = dbi_conn_queryf(conn,
621 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200622 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200623 try
624 );
625 if (result==NULL) {
626 printf("DB: Failed to query Subscriber while allocating new extension.\n");
627 return 1;
628 }
629 if (dbi_result_get_numrows(result)){
630 dbi_result_free(result);
631 continue;
632 }
633 if (!dbi_result_next_row(result)) {
634 dbi_result_free(result);
635 break;
636 }
637 dbi_result_free(result);
638 }
639 sprintf(subscriber->extension, "%i", try);
640 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
641 return db_sync_subscriber(subscriber);
642}
Jan Luebbe31bef492009-08-12 14:31:14 +0200643/*
644 * try to allocate a new unique token for this subscriber and return it
645 * via a parameter. if the subscriber already has a token, return
646 * an error.
647 */
648
Harald Welte (local)3feef252009-08-13 13:26:11 +0200649int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
650{
651 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200652 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200653
Jan Luebbe31bef492009-08-12 14:31:14 +0200654 for (;;) {
655 try = rand();
656 if (!try) /* 0 is an invalid token */
657 continue;
658 result = dbi_conn_queryf(conn,
659 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200660 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
661 subscriber->id, try);
662 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200663 printf("DB: Failed to query AuthToken while allocating new token.\n");
664 return 1;
665 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200666 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200667 dbi_result_free(result);
668 continue;
669 }
670 if (!dbi_result_next_row(result)) {
671 dbi_result_free(result);
672 break;
673 }
674 dbi_result_free(result);
675 }
676 result = dbi_conn_queryf(conn,
677 "INSERT INTO AuthToken "
678 "(subscriber_id, created, token) "
679 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200680 "(%llu, datetime('now'), \"%08X\") ",
681 subscriber->id, try);
682 if (!result) {
683 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200684 return 1;
685 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200686 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200687 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200688 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
689
Jan Luebbe31bef492009-08-12 14:31:14 +0200690 return 0;
691}
692
Jan Luebbefac25fc2008-12-27 18:04:34 +0000693int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
Harald Welted409be72009-11-07 00:06:19 +0900694 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +0000695 dbi_result result;
696
Harald Weltec2e302d2009-07-05 14:08:13 +0200697 strncpy(subscriber->equipment.imei, imei,
698 sizeof(subscriber->equipment.imei)-1),
699
Jan Luebbefac25fc2008-12-27 18:04:34 +0000700 result = dbi_conn_queryf(conn,
701 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000702 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000703 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000704 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000705 imei
706 );
707 if (result==NULL) {
708 printf("DB: Failed to create Equipment by IMEI.\n");
709 return 1;
710 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000711 equipment_id = 0;
712 if (dbi_result_get_numrows_affected(result)) {
713 equipment_id = dbi_conn_sequence_last(conn, NULL);
714 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000715 dbi_result_free(result);
716 if (equipment_id) {
717 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
718 }
719 else {
720 result = dbi_conn_queryf(conn,
721 "SELECT id FROM Equipment "
722 "WHERE imei = %s ",
723 imei
724 );
725 if (result==NULL) {
726 printf("DB: Failed to query Equipment by IMEI.\n");
727 return 1;
728 }
729 if (!dbi_result_next_row(result)) {
730 printf("DB: Failed to find the Equipment.\n");
731 dbi_result_free(result);
732 return 1;
733 }
734 equipment_id = dbi_result_get_ulonglong(result, "id");
735 dbi_result_free(result);
736 }
737
738 result = dbi_conn_queryf(conn,
739 "INSERT OR IGNORE INTO EquipmentWatch "
740 "(subscriber_id, equipment_id, created, updated) "
741 "VALUES "
742 "(%llu, %llu, datetime('now'), datetime('now')) ",
743 subscriber->id, equipment_id
744 );
745 if (result==NULL) {
746 printf("DB: Failed to create EquipmentWatch.\n");
747 return 1;
748 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000749 watch_id = 0;
750 if (dbi_result_get_numrows_affected(result)) {
751 watch_id = dbi_conn_sequence_last(conn, NULL);
752 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000753 dbi_result_free(result);
754 if (watch_id) {
755 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
756 }
757 else {
758 result = dbi_conn_queryf(conn,
759 "UPDATE EquipmentWatch "
760 "SET updated = datetime('now') "
761 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
762 subscriber->id, equipment_id
763 );
764 if (result==NULL) {
765 printf("DB: Failed to update EquipmentWatch.\n");
766 return 1;
767 }
768 dbi_result_free(result);
769 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
770 }
771
772 return 0;
773}
774
Harald Welte7e310b12009-03-30 20:56:32 +0000775/* store an [unsent] SMS to the database */
776int db_sms_store(struct gsm_sms *sms)
777{
778 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200779 char *q_text, *q_daddr;
780 unsigned char *q_udata;
781 char *validity_timestamp = "2222-2-2";
782
783 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000784
785 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200786 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
787 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
788 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200789 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000790 result = dbi_conn_queryf(conn,
791 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200792 "(created, sender_id, receiver_id, valid_until, "
793 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200794 "data_coding_scheme, ud_hdr_ind, dest_addr, "
795 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200796 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200797 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000798 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200799 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
800 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200801 sms->data_coding_scheme, sms->ud_hdr_ind,
802 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000803 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200804 free(q_daddr);
805 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000806
807 if (!result)
808 return -EIO;
809
810 dbi_result_free(result);
811 return 0;
812}
813
Harald Welte2ebabca2009-08-09 19:05:21 +0200814static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000815{
Harald Welte76042182009-08-08 16:03:15 +0200816 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200817 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200818 const char *text, *daddr;
819 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000820
Harald Welte76042182009-08-08 16:03:15 +0200821 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000822 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000823
Harald Weltebe3e3782009-07-05 14:06:41 +0200824 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000825
Harald Weltebe3e3782009-07-05 14:06:41 +0200826 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200827 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200828
829 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200830 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200831
Harald Weltef3efc592009-07-27 20:11:35 +0200832 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200833 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
834 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
835 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
836 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
837 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
838 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200839 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200840 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200841
Harald Welte76042182009-08-08 16:03:15 +0200842 daddr = dbi_result_get_string(result, "dest_addr");
843 if (daddr) {
844 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
845 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
846 }
847
848 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
849 user_data = dbi_result_get_binary(result, "user_data");
850 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200851 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200852 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200853
854 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200855 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200856 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200857 sms->text[sizeof(sms->text)-1] = '\0';
858 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200859 return sms;
860}
861
862/* retrieve the next unsent SMS with ID >= min_id */
863struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
864{
865 dbi_result result;
866 struct gsm_sms *sms;
867
868 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200869 "SELECT * FROM SMS,Subscriber "
870 "WHERE sms.id >= %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100871 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200872 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100873 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200874 min_id);
875 if (!result)
876 return NULL;
877
878 if (!dbi_result_next_row(result)) {
879 dbi_result_free(result);
880 return NULL;
881 }
882
883 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000884
885 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200886
887 return sms;
888}
889
Sylvain Munautff1f19e2009-12-22 13:22:29 +0100890struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, int min_subscr_id)
891{
892 dbi_result result;
893 struct gsm_sms *sms;
894
895 result = dbi_conn_queryf(conn,
896 "SELECT * FROM SMS,Subscriber "
897 "WHERE sms.receiver_id >= %llu AND sms.sent is NULL "
898 "AND sms.receiver_id = subscriber.id "
899 "AND subscriber.lac > 0 "
900 "ORDER BY sms.receiver_id, id LIMIT 1",
901 min_subscr_id);
902 if (!result)
903 return NULL;
904
905 if (!dbi_result_next_row(result)) {
906 dbi_result_free(result);
907 return NULL;
908 }
909
910 sms = sms_from_result(net, result);
911
912 dbi_result_free(result);
913
914 return sms;
915}
916
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100917/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +0200918struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
919{
920 dbi_result result;
921 struct gsm_sms *sms;
922
923 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200924 "SELECT * FROM SMS,Subscriber "
925 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100926 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200927 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100928 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200929 subscr->id);
930 if (!result)
931 return NULL;
932
933 if (!dbi_result_next_row(result)) {
934 dbi_result_free(result);
935 return NULL;
936 }
937
938 sms = sms_from_result(subscr->net, result);
939
940 dbi_result_free(result);
941
Harald Welte7e310b12009-03-30 20:56:32 +0000942 return sms;
943}
944
945/* mark a given SMS as read */
946int db_sms_mark_sent(struct gsm_sms *sms)
947{
948 dbi_result result;
949
950 result = dbi_conn_queryf(conn,
951 "UPDATE SMS "
952 "SET sent = datetime('now') "
953 "WHERE id = %llu", sms->id);
954 if (!result) {
955 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
956 return 1;
957 }
958
959 dbi_result_free(result);
960 return 0;
961}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200962
963/* increase the number of attempted deliveries */
964int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
965{
966 dbi_result result;
967
968 result = dbi_conn_queryf(conn,
969 "UPDATE SMS "
970 "SET deliver_attempts = deliver_attempts + 1 "
971 "WHERE id = %llu", sms->id);
972 if (!result) {
973 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
974 return 1;
975 }
976
977 dbi_result_free(result);
978 return 0;
979}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200980
981int db_apdu_blob_store(struct gsm_subscriber *subscr,
982 u_int8_t apdu_id_flags, u_int8_t len,
983 u_int8_t *apdu)
984{
985 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200986 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +0200987
988 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
989
990 result = dbi_conn_queryf(conn,
991 "INSERT INTO ApduBlobs "
992 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
993 "(datetime('now'),%llu,%u,%s)",
994 subscr->id, apdu_id_flags, q_apdu);
995
996 free(q_apdu);
997
998 if (!result)
999 return -EIO;
1000
1001 dbi_result_free(result);
1002 return 0;
1003}
Harald Welteffa55a42009-12-22 19:07:32 +01001004
1005int db_store_counter(struct counter *ctr)
1006{
1007 dbi_result result;
1008 char *q_name;
1009
1010 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1011
1012 result = dbi_conn_queryf(conn,
1013 "INSERT INTO Counters "
1014 "(timestamp,name,value) VALUES "
1015 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1016
1017 free(q_name);
1018
1019 if (!result)
1020 return -EIO;
1021
1022 dbi_result_free(result);
1023 return 0;
1024}