blob: 7b864fb43109397d432e8e41aae1478e8297b769 [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 Welteffa55a42009-12-22 19:07:32 +0100126 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000127};
128
Holger Freyther12aa50d2009-01-01 18:02:05 +0000129void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000130 const char* msg;
131 dbi_conn_error(conn, &msg);
132 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000133}
134
Harald Welted0b7b772009-08-09 19:03:42 +0200135static int check_db_revision(void)
136{
137 dbi_result result;
138 const char *rev;
139
140 result = dbi_conn_query(conn,
141 "SELECT value FROM Meta WHERE key='revision'");
142 if (!result)
143 return -EINVAL;
144
145 if (!dbi_result_next_row(result)) {
146 dbi_result_free(result);
147 return -EINVAL;
148 }
149 rev = dbi_result_get_string(result, "value");
150 if (!rev || atoi(rev) != 2) {
151 dbi_result_free(result);
152 return -EINVAL;
153 }
154
155 dbi_result_free(result);
156 return 0;
157}
158
Holger Freytherc7b86f92009-06-06 13:54:20 +0000159int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000160 dbi_initialize(NULL);
161 conn = dbi_conn_new("sqlite3");
162 if (conn==NULL) {
163 printf("DB: Failed to create connection.\n");
164 return 1;
165 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000166
Holger Freyther12aa50d2009-01-01 18:02:05 +0000167 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000168
Jan Luebbe5c15c852008-12-27 15:59:25 +0000169 /* MySQL
170 dbi_conn_set_option(conn, "host", "localhost");
171 dbi_conn_set_option(conn, "username", "your_name");
172 dbi_conn_set_option(conn, "password", "your_password");
173 dbi_conn_set_option(conn, "dbname", "your_dbname");
174 dbi_conn_set_option(conn, "encoding", "UTF-8");
175 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000176
Jan Luebbe5c15c852008-12-27 15:59:25 +0000177 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000178 db_basename = strdup(name);
179 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000180 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
181 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000182
Harald Welted0b7b772009-08-09 19:03:42 +0200183 if (dbi_conn_connect(conn) < 0)
184 goto out_err;
185
Jan Luebbe5c15c852008-12-27 15:59:25 +0000186 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200187
188out_err:
189 free(db_dirname);
190 free(db_basename);
191 db_dirname = db_basename = NULL;
192 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000193}
194
Harald Welted0b7b772009-08-09 19:03:42 +0200195
Jan Luebbe7398eb92008-12-27 00:45:41 +0000196int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000197 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000198 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000199
Harald Welte7e310b12009-03-30 20:56:32 +0000200 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
201 result = dbi_conn_query(conn, create_stmts[i]);
202 if (result==NULL) {
203 printf("DB: Failed to create some table.\n");
204 return 1;
205 }
206 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000207 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000208
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200209 if (check_db_revision() < 0) {
210 fprintf(stderr, "Database schema revision invalid, "
211 "please update your database schema\n");
212 return -1;
213 }
214
Jan Luebbe5c15c852008-12-27 15:59:25 +0000215 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000216}
217
218int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 dbi_conn_close(conn);
220 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000221
222 if (db_dirname)
223 free(db_dirname);
224 if (db_basename)
225 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000226 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000227}
228
Harald Welte9176bd42009-07-23 18:46:00 +0200229struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
230{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000232 struct gsm_subscriber* subscr;
233
234 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200235 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000236 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000237 result = dbi_conn_queryf(conn,
238 "UPDATE Subscriber set updated = datetime('now') "
239 "WHERE imsi = %s " , imsi);
240 if (result==NULL) {
241 printf("DB: failed to update timestamp\n");
242 } else {
243 dbi_result_free(result);
244 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000246 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000247
248 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200249 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000250 if (!subscr)
251 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000252 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000253 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000254 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000255 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000256 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000257 imsi
258 );
259 if (result==NULL) {
260 printf("DB: Failed to create Subscriber by IMSI.\n");
261 }
Harald Welte9176bd42009-07-23 18:46:00 +0200262 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000263 subscr->id = dbi_conn_sequence_last(conn, NULL);
264 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000265 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200267 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000268 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000269}
270
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200271static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
272{
273 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200274 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100275 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200276 const unsigned char *cm2, *cm3;
277 struct gsm_equipment *equip = &subscr->equipment;
278
279 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200280 "SELECT equipment.* FROM Equipment,EquipmentWatch "
281 "WHERE EquipmentWatch.equipment_id=Equipment.id "
282 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200283 "ORDER BY updated DESC", subscr->id);
284 if (!result)
285 return -EIO;
286
287 if (!dbi_result_next_row(result)) {
288 dbi_result_free(result);
289 return -ENOENT;
290 }
291
292 equip->id = dbi_result_get_ulonglong(result, "id");
293
294 string = dbi_result_get_string(result, "imei");
295 if (string)
296 strncpy(equip->imei, string, sizeof(equip->imei));
297
298 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
299 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
300
301 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
302 cm2 = dbi_result_get_binary(result, "classmark2");
303 if (equip->classmark2_len > sizeof(equip->classmark2))
304 equip->classmark2_len = sizeof(equip->classmark2);
305 memcpy(equip->classmark2, cm2, equip->classmark2_len);
306
307 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
308 cm3 = dbi_result_get_binary(result, "classmark3");
309 if (equip->classmark3_len > sizeof(equip->classmark3))
310 equip->classmark3_len = sizeof(equip->classmark3);
311 memcpy(equip->classmark3, cm3, equip->classmark3_len);
312
313 dbi_result_free(result);
314
315 return 0;
316}
317#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200318struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
319 enum gsm_subscriber_field field,
320 const char *id)
321{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000322 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000323 const char *string;
324 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000325 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000326
Jan Luebbe5c15c852008-12-27 15:59:25 +0000327 switch (field) {
328 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000329 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000330 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200331 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000333 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000334 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000335 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 break;
337 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000338 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000339 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200340 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000341 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000342 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000343 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000344 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000345 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000346 case GSM_SUBSCRIBER_EXTENSION:
347 dbi_conn_quote_string_copy(conn, id, &quoted);
348 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200349 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000350 "WHERE extension = %s ",
351 quoted
352 );
353 free(quoted);
354 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200355 case GSM_SUBSCRIBER_ID:
356 dbi_conn_quote_string_copy(conn, id, &quoted);
357 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200358 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200359 "WHERE id = %s ", quoted);
360 free(quoted);
361 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000362 default:
363 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000364 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000365 }
366 if (result==NULL) {
367 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000368 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000369 }
370 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000371 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
372 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000373 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000374 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000375 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000376
377 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200378 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000379 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000380 string = dbi_result_get_string(result, "imsi");
381 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000382 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000383
384 string = dbi_result_get_string(result, "tmsi");
385 if (string)
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200386 subscr->tmsi = tmsi_from_string(string);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000387
388 string = dbi_result_get_string(result, "name");
389 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000390 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000391
392 string = dbi_result_get_string(result, "extension");
393 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000394 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000395
Holger Freyther12aa50d2009-01-01 18:02:05 +0000396 subscr->lac = dbi_result_get_uint(result, "lac");
397 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200398 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 +0000399 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000400 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000401 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200402
403 get_equipment_by_subscr(subscr);
404
Holger Freyther12aa50d2009-01-01 18:02:05 +0000405 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000406}
407
Holger Freyther12aa50d2009-01-01 18:02:05 +0000408int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000409 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200410 char tmsi[14];
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200411 char *q_tmsi;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200412
413 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
414 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200415 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200416 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200417 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200418 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200419 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000420 result = dbi_conn_queryf(conn,
421 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000422 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200423 "name = '%s', "
424 "extension = '%s', "
425 "authorized = %i, "
426 "tmsi = %s, "
427 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200429 subscriber->name,
430 subscriber->extension,
431 subscriber->authorized,
432 q_tmsi,
433 subscriber->lac,
434 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000435 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200436 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000437 if (result==NULL) {
438 printf("DB: Failed to update Subscriber (by IMSI).\n");
439 return 1;
440 }
441 dbi_result_free(result);
442 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000443}
444
Harald Weltec2e302d2009-07-05 14:08:13 +0200445int db_sync_equipment(struct gsm_equipment *equip)
446{
447 dbi_result result;
448 unsigned char *cm2, *cm3;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200449 u_int8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +0200450
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200451 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200452 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200453 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200454 if (equip->classmark2_len)
455 printf(", classmark2=%s",
456 hexdump(equip->classmark2, equip->classmark2_len));
457 if (equip->classmark3_len)
458 printf(", classmark3=%s",
459 hexdump(equip->classmark3, equip->classmark3_len));
460 printf("\n");
461
Harald Weltec2e302d2009-07-05 14:08:13 +0200462 dbi_conn_quote_binary_copy(conn, equip->classmark2,
463 equip->classmark2_len, &cm2);
464 dbi_conn_quote_binary_copy(conn, equip->classmark3,
465 equip->classmark3_len, &cm3);
466
467 result = dbi_conn_queryf(conn,
468 "UPDATE Equipment SET "
469 "updated = datetime('now'), "
470 "classmark1 = %u, "
471 "classmark2 = %s, "
472 "classmark3 = %s "
473 "WHERE imei = '%s' ",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200474 classmark1, cm2, cm3, equip->imei);
Harald Weltec2e302d2009-07-05 14:08:13 +0200475
476 free(cm2);
477 free(cm3);
478
479 if (!result) {
480 printf("DB: Failed to update Equipment\n");
481 return -EIO;
482 }
483
484 dbi_result_free(result);
485 return 0;
486}
487
Jan Luebbe5c15c852008-12-27 15:59:25 +0000488int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000489 dbi_result result=NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200490 char tmsi[14];
Jan Luebbe5c15c852008-12-27 15:59:25 +0000491 char* tmsi_quoted;
492 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200493 subscriber->tmsi = rand();
494 if (subscriber->tmsi == GSM_RESERVED_TMSI)
495 continue;
496
497 sprintf(tmsi, "%u", subscriber->tmsi);
498 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000499 result = dbi_conn_queryf(conn,
500 "SELECT * FROM Subscriber "
501 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000502 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000503 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000504 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000505 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000506 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000507 return 1;
508 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000509 if (dbi_result_get_numrows(result)){
510 dbi_result_free(result);
511 continue;
512 }
513 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000514 dbi_result_free(result);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200515 printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000516 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000517 }
518 dbi_result_free(result);
519 }
520 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000521}
522
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200523int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
524 dbi_result result=NULL;
525 u_int32_t try;
526 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200527 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200528 result = dbi_conn_queryf(conn,
529 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200530 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200531 try
532 );
533 if (result==NULL) {
534 printf("DB: Failed to query Subscriber while allocating new extension.\n");
535 return 1;
536 }
537 if (dbi_result_get_numrows(result)){
538 dbi_result_free(result);
539 continue;
540 }
541 if (!dbi_result_next_row(result)) {
542 dbi_result_free(result);
543 break;
544 }
545 dbi_result_free(result);
546 }
547 sprintf(subscriber->extension, "%i", try);
548 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
549 return db_sync_subscriber(subscriber);
550}
Jan Luebbe31bef492009-08-12 14:31:14 +0200551/*
552 * try to allocate a new unique token for this subscriber and return it
553 * via a parameter. if the subscriber already has a token, return
554 * an error.
555 */
556
Harald Welte (local)3feef252009-08-13 13:26:11 +0200557int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
558{
559 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200560 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200561
Jan Luebbe31bef492009-08-12 14:31:14 +0200562 for (;;) {
563 try = rand();
564 if (!try) /* 0 is an invalid token */
565 continue;
566 result = dbi_conn_queryf(conn,
567 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200568 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
569 subscriber->id, try);
570 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200571 printf("DB: Failed to query AuthToken while allocating new token.\n");
572 return 1;
573 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200574 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200575 dbi_result_free(result);
576 continue;
577 }
578 if (!dbi_result_next_row(result)) {
579 dbi_result_free(result);
580 break;
581 }
582 dbi_result_free(result);
583 }
584 result = dbi_conn_queryf(conn,
585 "INSERT INTO AuthToken "
586 "(subscriber_id, created, token) "
587 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200588 "(%llu, datetime('now'), \"%08X\") ",
589 subscriber->id, try);
590 if (!result) {
591 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200592 return 1;
593 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200594 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200595 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200596 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
597
Jan Luebbe31bef492009-08-12 14:31:14 +0200598 return 0;
599}
600
Jan Luebbefac25fc2008-12-27 18:04:34 +0000601int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
Harald Welted409be72009-11-07 00:06:19 +0900602 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +0000603 dbi_result result;
604
Harald Weltec2e302d2009-07-05 14:08:13 +0200605 strncpy(subscriber->equipment.imei, imei,
606 sizeof(subscriber->equipment.imei)-1),
607
Jan Luebbefac25fc2008-12-27 18:04:34 +0000608 result = dbi_conn_queryf(conn,
609 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000610 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000611 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000612 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000613 imei
614 );
615 if (result==NULL) {
616 printf("DB: Failed to create Equipment by IMEI.\n");
617 return 1;
618 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000619 equipment_id = 0;
620 if (dbi_result_get_numrows_affected(result)) {
621 equipment_id = dbi_conn_sequence_last(conn, NULL);
622 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000623 dbi_result_free(result);
624 if (equipment_id) {
625 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
626 }
627 else {
628 result = dbi_conn_queryf(conn,
629 "SELECT id FROM Equipment "
630 "WHERE imei = %s ",
631 imei
632 );
633 if (result==NULL) {
634 printf("DB: Failed to query Equipment by IMEI.\n");
635 return 1;
636 }
637 if (!dbi_result_next_row(result)) {
638 printf("DB: Failed to find the Equipment.\n");
639 dbi_result_free(result);
640 return 1;
641 }
642 equipment_id = dbi_result_get_ulonglong(result, "id");
643 dbi_result_free(result);
644 }
645
646 result = dbi_conn_queryf(conn,
647 "INSERT OR IGNORE INTO EquipmentWatch "
648 "(subscriber_id, equipment_id, created, updated) "
649 "VALUES "
650 "(%llu, %llu, datetime('now'), datetime('now')) ",
651 subscriber->id, equipment_id
652 );
653 if (result==NULL) {
654 printf("DB: Failed to create EquipmentWatch.\n");
655 return 1;
656 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000657 watch_id = 0;
658 if (dbi_result_get_numrows_affected(result)) {
659 watch_id = dbi_conn_sequence_last(conn, NULL);
660 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000661 dbi_result_free(result);
662 if (watch_id) {
663 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
664 }
665 else {
666 result = dbi_conn_queryf(conn,
667 "UPDATE EquipmentWatch "
668 "SET updated = datetime('now') "
669 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
670 subscriber->id, equipment_id
671 );
672 if (result==NULL) {
673 printf("DB: Failed to update EquipmentWatch.\n");
674 return 1;
675 }
676 dbi_result_free(result);
677 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
678 }
679
680 return 0;
681}
682
Harald Welte7e310b12009-03-30 20:56:32 +0000683/* store an [unsent] SMS to the database */
684int db_sms_store(struct gsm_sms *sms)
685{
686 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200687 char *q_text, *q_daddr;
688 unsigned char *q_udata;
689 char *validity_timestamp = "2222-2-2";
690
691 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000692
693 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200694 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
695 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
696 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200697 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000698 result = dbi_conn_queryf(conn,
699 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200700 "(created, sender_id, receiver_id, valid_until, "
701 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200702 "data_coding_scheme, ud_hdr_ind, dest_addr, "
703 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200704 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200705 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000706 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200707 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
708 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200709 sms->data_coding_scheme, sms->ud_hdr_ind,
710 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000711 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200712 free(q_daddr);
713 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000714
715 if (!result)
716 return -EIO;
717
718 dbi_result_free(result);
719 return 0;
720}
721
Harald Welte2ebabca2009-08-09 19:05:21 +0200722static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000723{
Harald Welte76042182009-08-08 16:03:15 +0200724 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200725 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200726 const char *text, *daddr;
727 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000728
Harald Welte76042182009-08-08 16:03:15 +0200729 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000730 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000731
Harald Weltebe3e3782009-07-05 14:06:41 +0200732 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000733
Harald Weltebe3e3782009-07-05 14:06:41 +0200734 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200735 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200736
737 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200738 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200739
Harald Weltef3efc592009-07-27 20:11:35 +0200740 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200741 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
742 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
743 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
744 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
745 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
746 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200747 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200748 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200749
Harald Welte76042182009-08-08 16:03:15 +0200750 daddr = dbi_result_get_string(result, "dest_addr");
751 if (daddr) {
752 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
753 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
754 }
755
756 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
757 user_data = dbi_result_get_binary(result, "user_data");
758 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200759 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200760 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200761
762 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200763 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200764 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200765 sms->text[sizeof(sms->text)-1] = '\0';
766 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200767 return sms;
768}
769
770/* retrieve the next unsent SMS with ID >= min_id */
771struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
772{
773 dbi_result result;
774 struct gsm_sms *sms;
775
776 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200777 "SELECT * FROM SMS,Subscriber "
778 "WHERE sms.id >= %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100779 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200780 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100781 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200782 min_id);
783 if (!result)
784 return NULL;
785
786 if (!dbi_result_next_row(result)) {
787 dbi_result_free(result);
788 return NULL;
789 }
790
791 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000792
793 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200794
795 return sms;
796}
797
Sylvain Munautff1f19e2009-12-22 13:22:29 +0100798struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, int min_subscr_id)
799{
800 dbi_result result;
801 struct gsm_sms *sms;
802
803 result = dbi_conn_queryf(conn,
804 "SELECT * FROM SMS,Subscriber "
805 "WHERE sms.receiver_id >= %llu AND sms.sent is NULL "
806 "AND sms.receiver_id = subscriber.id "
807 "AND subscriber.lac > 0 "
808 "ORDER BY sms.receiver_id, id LIMIT 1",
809 min_subscr_id);
810 if (!result)
811 return NULL;
812
813 if (!dbi_result_next_row(result)) {
814 dbi_result_free(result);
815 return NULL;
816 }
817
818 sms = sms_from_result(net, result);
819
820 dbi_result_free(result);
821
822 return sms;
823}
824
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100825/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +0200826struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
827{
828 dbi_result result;
829 struct gsm_sms *sms;
830
831 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200832 "SELECT * FROM SMS,Subscriber "
833 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100834 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200835 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100836 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200837 subscr->id);
838 if (!result)
839 return NULL;
840
841 if (!dbi_result_next_row(result)) {
842 dbi_result_free(result);
843 return NULL;
844 }
845
846 sms = sms_from_result(subscr->net, result);
847
848 dbi_result_free(result);
849
Harald Welte7e310b12009-03-30 20:56:32 +0000850 return sms;
851}
852
853/* mark a given SMS as read */
854int db_sms_mark_sent(struct gsm_sms *sms)
855{
856 dbi_result result;
857
858 result = dbi_conn_queryf(conn,
859 "UPDATE SMS "
860 "SET sent = datetime('now') "
861 "WHERE id = %llu", sms->id);
862 if (!result) {
863 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
864 return 1;
865 }
866
867 dbi_result_free(result);
868 return 0;
869}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200870
871/* increase the number of attempted deliveries */
872int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
873{
874 dbi_result result;
875
876 result = dbi_conn_queryf(conn,
877 "UPDATE SMS "
878 "SET deliver_attempts = deliver_attempts + 1 "
879 "WHERE id = %llu", sms->id);
880 if (!result) {
881 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
882 return 1;
883 }
884
885 dbi_result_free(result);
886 return 0;
887}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200888
889int db_apdu_blob_store(struct gsm_subscriber *subscr,
890 u_int8_t apdu_id_flags, u_int8_t len,
891 u_int8_t *apdu)
892{
893 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200894 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +0200895
896 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
897
898 result = dbi_conn_queryf(conn,
899 "INSERT INTO ApduBlobs "
900 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
901 "(datetime('now'),%llu,%u,%s)",
902 subscr->id, apdu_id_flags, q_apdu);
903
904 free(q_apdu);
905
906 if (!result)
907 return -EIO;
908
909 dbi_result_free(result);
910 return 0;
911}
Harald Welteffa55a42009-12-22 19:07:32 +0100912
913int db_store_counter(struct counter *ctr)
914{
915 dbi_result result;
916 char *q_name;
917
918 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
919
920 result = dbi_conn_queryf(conn,
921 "INSERT INTO Counters "
922 "(timestamp,name,value) VALUES "
923 "(datetime('now'),%s,%lu)", q_name, ctr->value);
924
925 free(q_name);
926
927 if (!result)
928 return -EIO;
929
930 dbi_result_free(result);
931 return 0;
932}