blob: 707d877bbecc3554b69bb02422703394a9d6ab5b [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>
Jan Luebbe7398eb92008-12-27 00:45:41 +000028
Holger Freytherbde36102008-12-28 22:51:39 +000029#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000030#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000031#include <stdlib.h>
32#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000033#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000034#include <dbi/dbi.h>
35
Holger Freytherbde36102008-12-28 22:51:39 +000036static char *db_basename = NULL;
37static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000038static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000039
Harald Welte7e310b12009-03-30 20:56:32 +000040static char *create_stmts[] = {
41 "CREATE TABLE IF NOT EXISTS Meta ("
42 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
43 "key TEXT UNIQUE NOT NULL, "
44 "value TEXT NOT NULL"
45 ")",
46 "INSERT OR IGNORE INTO Meta "
47 "(key, value) "
48 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020049 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000050 "CREATE TABLE IF NOT EXISTS Subscriber ("
51 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
52 "created TIMESTAMP NOT NULL, "
53 "updated TIMESTAMP NOT NULL, "
54 "imsi NUMERIC UNIQUE NOT NULL, "
55 "name TEXT, "
56 "extension TEXT UNIQUE, "
57 "authorized INTEGER NOT NULL DEFAULT 0, "
58 "tmsi TEXT UNIQUE, "
59 "lac INTEGER NOT NULL DEFAULT 0"
60 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020061 "CREATE TABLE IF NOT EXISTS AuthToken ("
62 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
63 "subscriber_id INTEGER UNIQUE NOT NULL, "
64 "created TIMESTAMP NOT NULL, "
65 "token TEXT UNIQUE NOT NULL"
66 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000067 "CREATE TABLE IF NOT EXISTS Equipment ("
68 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
69 "created TIMESTAMP NOT NULL, "
70 "updated TIMESTAMP NOT NULL, "
71 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020072 "classmark1 NUMERIC, "
73 "classmark2 BLOB, "
74 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000075 "imei NUMERIC UNIQUE NOT NULL"
76 ")",
77 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
78 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
79 "created TIMESTAMP NOT NULL, "
80 "updated TIMESTAMP NOT NULL, "
81 "subscriber_id NUMERIC NOT NULL, "
82 "equipment_id NUMERIC NOT NULL, "
83 "UNIQUE (subscriber_id, equipment_id) "
84 ")",
85 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020086 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000087 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
88 "created TIMESTAMP NOT NULL, "
89 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020090 "sender_id INTEGER NOT NULL, "
91 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020092 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020093 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020094 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020095 "reply_path_req INTEGER NOT NULL, "
96 "status_rep_req INTEGER NOT NULL, "
97 "protocol_id INTEGER NOT NULL, "
98 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020099 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200100 "dest_addr TEXT, "
101 "user_data BLOB, " /* TP-UD */
102 /* additional data, interpreted from SMS */
103 "header BLOB, " /* UD Header */
104 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000105 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000106 "CREATE TABLE IF NOT EXISTS VLR ("
107 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
108 "created TIMESTAMP NOT NULL, "
109 "updated TIMESTAMP NOT NULL, "
110 "subscriber_id NUMERIC UNIQUE NOT NULL, "
111 "last_bts NUMERIC NOT NULL "
112 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200113 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
114 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
115 "created TIMESTAMP NOT NULL, "
116 "apdu_id_flags INTEGER NOT NULL, "
117 "subscriber_id INTEGER NOT NULL, "
118 "apdu BLOB "
119 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000120};
121
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000123 const char* msg;
124 dbi_conn_error(conn, &msg);
125 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000126}
127
Harald Welted0b7b772009-08-09 19:03:42 +0200128static int check_db_revision(void)
129{
130 dbi_result result;
131 const char *rev;
132
133 result = dbi_conn_query(conn,
134 "SELECT value FROM Meta WHERE key='revision'");
135 if (!result)
136 return -EINVAL;
137
138 if (!dbi_result_next_row(result)) {
139 dbi_result_free(result);
140 return -EINVAL;
141 }
142 rev = dbi_result_get_string(result, "value");
143 if (!rev || atoi(rev) != 2) {
144 dbi_result_free(result);
145 return -EINVAL;
146 }
147
148 dbi_result_free(result);
149 return 0;
150}
151
Holger Freytherc7b86f92009-06-06 13:54:20 +0000152int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000153 dbi_initialize(NULL);
154 conn = dbi_conn_new("sqlite3");
155 if (conn==NULL) {
156 printf("DB: Failed to create connection.\n");
157 return 1;
158 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000159
Holger Freyther12aa50d2009-01-01 18:02:05 +0000160 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000161
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 /* MySQL
163 dbi_conn_set_option(conn, "host", "localhost");
164 dbi_conn_set_option(conn, "username", "your_name");
165 dbi_conn_set_option(conn, "password", "your_password");
166 dbi_conn_set_option(conn, "dbname", "your_dbname");
167 dbi_conn_set_option(conn, "encoding", "UTF-8");
168 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000169
Jan Luebbe5c15c852008-12-27 15:59:25 +0000170 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000171 db_basename = strdup(name);
172 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000173 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
174 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000175
Harald Welted0b7b772009-08-09 19:03:42 +0200176 if (dbi_conn_connect(conn) < 0)
177 goto out_err;
178
Jan Luebbe5c15c852008-12-27 15:59:25 +0000179 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200180
181out_err:
182 free(db_dirname);
183 free(db_basename);
184 db_dirname = db_basename = NULL;
185 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000186}
187
Harald Welted0b7b772009-08-09 19:03:42 +0200188
Jan Luebbe7398eb92008-12-27 00:45:41 +0000189int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000190 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000191 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000192
Harald Welte7e310b12009-03-30 20:56:32 +0000193 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
194 result = dbi_conn_query(conn, create_stmts[i]);
195 if (result==NULL) {
196 printf("DB: Failed to create some table.\n");
197 return 1;
198 }
199 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000200 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000201
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200202 if (check_db_revision() < 0) {
203 fprintf(stderr, "Database schema revision invalid, "
204 "please update your database schema\n");
205 return -1;
206 }
207
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209}
210
211int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 dbi_conn_close(conn);
213 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000214
215 if (db_dirname)
216 free(db_dirname);
217 if (db_basename)
218 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000220}
221
Harald Welte9176bd42009-07-23 18:46:00 +0200222struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
223{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225 struct gsm_subscriber* subscr;
226
227 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200228 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000229 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000230 result = dbi_conn_queryf(conn,
231 "UPDATE Subscriber set updated = datetime('now') "
232 "WHERE imsi = %s " , imsi);
233 if (result==NULL) {
234 printf("DB: failed to update timestamp\n");
235 } else {
236 dbi_result_free(result);
237 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000238 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000239 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000240
241 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200242 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 if (!subscr)
244 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000246 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000247 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000248 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000249 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000250 imsi
251 );
252 if (result==NULL) {
253 printf("DB: Failed to create Subscriber by IMSI.\n");
254 }
Harald Welte9176bd42009-07-23 18:46:00 +0200255 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 subscr->id = dbi_conn_sequence_last(conn, NULL);
257 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000258 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200260 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000261 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000262}
263
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200264static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
265{
266 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200267 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100268 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200269 const unsigned char *cm2, *cm3;
270 struct gsm_equipment *equip = &subscr->equipment;
271
272 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200273 "SELECT equipment.* FROM Equipment,EquipmentWatch "
274 "WHERE EquipmentWatch.equipment_id=Equipment.id "
275 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200276 "ORDER BY updated DESC", subscr->id);
277 if (!result)
278 return -EIO;
279
280 if (!dbi_result_next_row(result)) {
281 dbi_result_free(result);
282 return -ENOENT;
283 }
284
285 equip->id = dbi_result_get_ulonglong(result, "id");
286
287 string = dbi_result_get_string(result, "imei");
288 if (string)
289 strncpy(equip->imei, string, sizeof(equip->imei));
290
291 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
292 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
293
294 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
295 cm2 = dbi_result_get_binary(result, "classmark2");
296 if (equip->classmark2_len > sizeof(equip->classmark2))
297 equip->classmark2_len = sizeof(equip->classmark2);
298 memcpy(equip->classmark2, cm2, equip->classmark2_len);
299
300 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
301 cm3 = dbi_result_get_binary(result, "classmark3");
302 if (equip->classmark3_len > sizeof(equip->classmark3))
303 equip->classmark3_len = sizeof(equip->classmark3);
304 memcpy(equip->classmark3, cm3, equip->classmark3_len);
305
306 dbi_result_free(result);
307
308 return 0;
309}
310#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200311struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
312 enum gsm_subscriber_field field,
313 const char *id)
314{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000315 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000316 const char *string;
317 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000318 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000319
Jan Luebbe5c15c852008-12-27 15:59:25 +0000320 switch (field) {
321 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000322 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000323 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200324 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000325 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000326 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000327 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000328 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000329 break;
330 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000331 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200333 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000334 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000335 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000337 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000339 case GSM_SUBSCRIBER_EXTENSION:
340 dbi_conn_quote_string_copy(conn, id, &quoted);
341 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200342 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000343 "WHERE extension = %s ",
344 quoted
345 );
346 free(quoted);
347 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200348 case GSM_SUBSCRIBER_ID:
349 dbi_conn_quote_string_copy(conn, id, &quoted);
350 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200351 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200352 "WHERE id = %s ", quoted);
353 free(quoted);
354 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000355 default:
356 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000357 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000358 }
359 if (result==NULL) {
360 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000361 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000362 }
363 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000364 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
365 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000366 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000367 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000368 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000369
370 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200371 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000372 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000373 string = dbi_result_get_string(result, "imsi");
374 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000375 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000376
377 string = dbi_result_get_string(result, "tmsi");
378 if (string)
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200379 subscr->tmsi = tmsi_from_string(string);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000380
381 string = dbi_result_get_string(result, "name");
382 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000383 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000384
385 string = dbi_result_get_string(result, "extension");
386 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000387 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000388
Holger Freyther12aa50d2009-01-01 18:02:05 +0000389 subscr->lac = dbi_result_get_uint(result, "lac");
390 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200391 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 +0000392 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000393 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000394 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200395
396 get_equipment_by_subscr(subscr);
397
Holger Freyther12aa50d2009-01-01 18:02:05 +0000398 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000399}
400
Holger Freyther12aa50d2009-01-01 18:02:05 +0000401int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000402 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200403 char tmsi[14];
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200404 char *q_tmsi;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200405
406 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
407 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200408 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200409 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200410 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200411 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200412 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000413 result = dbi_conn_queryf(conn,
414 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000415 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200416 "name = '%s', "
417 "extension = '%s', "
418 "authorized = %i, "
419 "tmsi = %s, "
420 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000421 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200422 subscriber->name,
423 subscriber->extension,
424 subscriber->authorized,
425 q_tmsi,
426 subscriber->lac,
427 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200429 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000430 if (result==NULL) {
431 printf("DB: Failed to update Subscriber (by IMSI).\n");
432 return 1;
433 }
434 dbi_result_free(result);
435 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000436}
437
Harald Weltec2e302d2009-07-05 14:08:13 +0200438int db_sync_equipment(struct gsm_equipment *equip)
439{
440 dbi_result result;
441 unsigned char *cm2, *cm3;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200442 u_int8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +0200443
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200444 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200445 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200446 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200447 if (equip->classmark2_len)
448 printf(", classmark2=%s",
449 hexdump(equip->classmark2, equip->classmark2_len));
450 if (equip->classmark3_len)
451 printf(", classmark3=%s",
452 hexdump(equip->classmark3, equip->classmark3_len));
453 printf("\n");
454
Harald Weltec2e302d2009-07-05 14:08:13 +0200455 dbi_conn_quote_binary_copy(conn, equip->classmark2,
456 equip->classmark2_len, &cm2);
457 dbi_conn_quote_binary_copy(conn, equip->classmark3,
458 equip->classmark3_len, &cm3);
459
460 result = dbi_conn_queryf(conn,
461 "UPDATE Equipment SET "
462 "updated = datetime('now'), "
463 "classmark1 = %u, "
464 "classmark2 = %s, "
465 "classmark3 = %s "
466 "WHERE imei = '%s' ",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200467 classmark1, cm2, cm3, equip->imei);
Harald Weltec2e302d2009-07-05 14:08:13 +0200468
469 free(cm2);
470 free(cm3);
471
472 if (!result) {
473 printf("DB: Failed to update Equipment\n");
474 return -EIO;
475 }
476
477 dbi_result_free(result);
478 return 0;
479}
480
Jan Luebbe5c15c852008-12-27 15:59:25 +0000481int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000482 dbi_result result=NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200483 char tmsi[14];
Jan Luebbe5c15c852008-12-27 15:59:25 +0000484 char* tmsi_quoted;
485 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200486 subscriber->tmsi = rand();
487 if (subscriber->tmsi == GSM_RESERVED_TMSI)
488 continue;
489
490 sprintf(tmsi, "%u", subscriber->tmsi);
491 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000492 result = dbi_conn_queryf(conn,
493 "SELECT * FROM Subscriber "
494 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000495 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000496 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000497 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000498 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000499 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000500 return 1;
501 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000502 if (dbi_result_get_numrows(result)){
503 dbi_result_free(result);
504 continue;
505 }
506 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000507 dbi_result_free(result);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200508 printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000509 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000510 }
511 dbi_result_free(result);
512 }
513 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000514}
515
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200516int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
517 dbi_result result=NULL;
518 u_int32_t try;
519 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200520 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200521 result = dbi_conn_queryf(conn,
522 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200523 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200524 try
525 );
526 if (result==NULL) {
527 printf("DB: Failed to query Subscriber while allocating new extension.\n");
528 return 1;
529 }
530 if (dbi_result_get_numrows(result)){
531 dbi_result_free(result);
532 continue;
533 }
534 if (!dbi_result_next_row(result)) {
535 dbi_result_free(result);
536 break;
537 }
538 dbi_result_free(result);
539 }
540 sprintf(subscriber->extension, "%i", try);
541 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
542 return db_sync_subscriber(subscriber);
543}
Jan Luebbe31bef492009-08-12 14:31:14 +0200544/*
545 * try to allocate a new unique token for this subscriber and return it
546 * via a parameter. if the subscriber already has a token, return
547 * an error.
548 */
549
Harald Welte (local)3feef252009-08-13 13:26:11 +0200550int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
551{
552 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200553 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200554
Jan Luebbe31bef492009-08-12 14:31:14 +0200555 for (;;) {
556 try = rand();
557 if (!try) /* 0 is an invalid token */
558 continue;
559 result = dbi_conn_queryf(conn,
560 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200561 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
562 subscriber->id, try);
563 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200564 printf("DB: Failed to query AuthToken while allocating new token.\n");
565 return 1;
566 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200567 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200568 dbi_result_free(result);
569 continue;
570 }
571 if (!dbi_result_next_row(result)) {
572 dbi_result_free(result);
573 break;
574 }
575 dbi_result_free(result);
576 }
577 result = dbi_conn_queryf(conn,
578 "INSERT INTO AuthToken "
579 "(subscriber_id, created, token) "
580 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200581 "(%llu, datetime('now'), \"%08X\") ",
582 subscriber->id, try);
583 if (!result) {
584 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200585 return 1;
586 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200587 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200588 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200589 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
590
Jan Luebbe31bef492009-08-12 14:31:14 +0200591 return 0;
592}
593
Jan Luebbefac25fc2008-12-27 18:04:34 +0000594int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
Harald Welted409be72009-11-07 00:06:19 +0900595 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +0000596 dbi_result result;
597
Harald Weltec2e302d2009-07-05 14:08:13 +0200598 strncpy(subscriber->equipment.imei, imei,
599 sizeof(subscriber->equipment.imei)-1),
600
Jan Luebbefac25fc2008-12-27 18:04:34 +0000601 result = dbi_conn_queryf(conn,
602 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000603 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000604 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000605 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000606 imei
607 );
608 if (result==NULL) {
609 printf("DB: Failed to create Equipment by IMEI.\n");
610 return 1;
611 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000612 equipment_id = 0;
613 if (dbi_result_get_numrows_affected(result)) {
614 equipment_id = dbi_conn_sequence_last(conn, NULL);
615 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000616 dbi_result_free(result);
617 if (equipment_id) {
618 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
619 }
620 else {
621 result = dbi_conn_queryf(conn,
622 "SELECT id FROM Equipment "
623 "WHERE imei = %s ",
624 imei
625 );
626 if (result==NULL) {
627 printf("DB: Failed to query Equipment by IMEI.\n");
628 return 1;
629 }
630 if (!dbi_result_next_row(result)) {
631 printf("DB: Failed to find the Equipment.\n");
632 dbi_result_free(result);
633 return 1;
634 }
635 equipment_id = dbi_result_get_ulonglong(result, "id");
636 dbi_result_free(result);
637 }
638
639 result = dbi_conn_queryf(conn,
640 "INSERT OR IGNORE INTO EquipmentWatch "
641 "(subscriber_id, equipment_id, created, updated) "
642 "VALUES "
643 "(%llu, %llu, datetime('now'), datetime('now')) ",
644 subscriber->id, equipment_id
645 );
646 if (result==NULL) {
647 printf("DB: Failed to create EquipmentWatch.\n");
648 return 1;
649 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000650 watch_id = 0;
651 if (dbi_result_get_numrows_affected(result)) {
652 watch_id = dbi_conn_sequence_last(conn, NULL);
653 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000654 dbi_result_free(result);
655 if (watch_id) {
656 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
657 }
658 else {
659 result = dbi_conn_queryf(conn,
660 "UPDATE EquipmentWatch "
661 "SET updated = datetime('now') "
662 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
663 subscriber->id, equipment_id
664 );
665 if (result==NULL) {
666 printf("DB: Failed to update EquipmentWatch.\n");
667 return 1;
668 }
669 dbi_result_free(result);
670 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
671 }
672
673 return 0;
674}
675
Harald Welte7e310b12009-03-30 20:56:32 +0000676/* store an [unsent] SMS to the database */
677int db_sms_store(struct gsm_sms *sms)
678{
679 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200680 char *q_text, *q_daddr;
681 unsigned char *q_udata;
682 char *validity_timestamp = "2222-2-2";
683
684 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000685
686 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200687 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
688 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
689 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200690 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000691 result = dbi_conn_queryf(conn,
692 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200693 "(created, sender_id, receiver_id, valid_until, "
694 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200695 "data_coding_scheme, ud_hdr_ind, dest_addr, "
696 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200697 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200698 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000699 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200700 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
701 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200702 sms->data_coding_scheme, sms->ud_hdr_ind,
703 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000704 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200705 free(q_daddr);
706 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000707
708 if (!result)
709 return -EIO;
710
711 dbi_result_free(result);
712 return 0;
713}
714
Harald Welte2ebabca2009-08-09 19:05:21 +0200715static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000716{
Harald Welte76042182009-08-08 16:03:15 +0200717 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200718 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200719 const char *text, *daddr;
720 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000721
Harald Welte76042182009-08-08 16:03:15 +0200722 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000723 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000724
Harald Weltebe3e3782009-07-05 14:06:41 +0200725 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000726
Harald Weltebe3e3782009-07-05 14:06:41 +0200727 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200728 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200729
730 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200731 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200732
Harald Weltef3efc592009-07-27 20:11:35 +0200733 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200734 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
735 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
736 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
737 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
738 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
739 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200740 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200741 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200742
Harald Welte76042182009-08-08 16:03:15 +0200743 daddr = dbi_result_get_string(result, "dest_addr");
744 if (daddr) {
745 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
746 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
747 }
748
749 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
750 user_data = dbi_result_get_binary(result, "user_data");
751 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200752 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200753 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200754
755 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200756 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200757 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200758 sms->text[sizeof(sms->text)-1] = '\0';
759 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200760 return sms;
761}
762
763/* retrieve the next unsent SMS with ID >= min_id */
764struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
765{
766 dbi_result result;
767 struct gsm_sms *sms;
768
769 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200770 "SELECT * FROM SMS,Subscriber "
771 "WHERE sms.id >= %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100772 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200773 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100774 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200775 min_id);
776 if (!result)
777 return NULL;
778
779 if (!dbi_result_next_row(result)) {
780 dbi_result_free(result);
781 return NULL;
782 }
783
784 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000785
786 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200787
788 return sms;
789}
790
Sylvain Munautff1f19e2009-12-22 13:22:29 +0100791struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, int min_subscr_id)
792{
793 dbi_result result;
794 struct gsm_sms *sms;
795
796 result = dbi_conn_queryf(conn,
797 "SELECT * FROM SMS,Subscriber "
798 "WHERE sms.receiver_id >= %llu AND sms.sent is NULL "
799 "AND sms.receiver_id = subscriber.id "
800 "AND subscriber.lac > 0 "
801 "ORDER BY sms.receiver_id, id LIMIT 1",
802 min_subscr_id);
803 if (!result)
804 return NULL;
805
806 if (!dbi_result_next_row(result)) {
807 dbi_result_free(result);
808 return NULL;
809 }
810
811 sms = sms_from_result(net, result);
812
813 dbi_result_free(result);
814
815 return sms;
816}
817
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100818/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +0200819struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
820{
821 dbi_result result;
822 struct gsm_sms *sms;
823
824 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200825 "SELECT * FROM SMS,Subscriber "
826 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100827 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200828 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100829 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200830 subscr->id);
831 if (!result)
832 return NULL;
833
834 if (!dbi_result_next_row(result)) {
835 dbi_result_free(result);
836 return NULL;
837 }
838
839 sms = sms_from_result(subscr->net, result);
840
841 dbi_result_free(result);
842
Harald Welte7e310b12009-03-30 20:56:32 +0000843 return sms;
844}
845
846/* mark a given SMS as read */
847int db_sms_mark_sent(struct gsm_sms *sms)
848{
849 dbi_result result;
850
851 result = dbi_conn_queryf(conn,
852 "UPDATE SMS "
853 "SET sent = datetime('now') "
854 "WHERE id = %llu", sms->id);
855 if (!result) {
856 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
857 return 1;
858 }
859
860 dbi_result_free(result);
861 return 0;
862}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200863
864/* increase the number of attempted deliveries */
865int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
866{
867 dbi_result result;
868
869 result = dbi_conn_queryf(conn,
870 "UPDATE SMS "
871 "SET deliver_attempts = deliver_attempts + 1 "
872 "WHERE id = %llu", sms->id);
873 if (!result) {
874 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
875 return 1;
876 }
877
878 dbi_result_free(result);
879 return 0;
880}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200881
882int db_apdu_blob_store(struct gsm_subscriber *subscr,
883 u_int8_t apdu_id_flags, u_int8_t len,
884 u_int8_t *apdu)
885{
886 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200887 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +0200888
889 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
890
891 result = dbi_conn_queryf(conn,
892 "INSERT INTO ApduBlobs "
893 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
894 "(datetime('now'),%llu,%u,%s)",
895 subscr->id, apdu_id_flags, q_apdu);
896
897 free(q_apdu);
898
899 if (!result)
900 return -EIO;
901
902 dbi_result_free(result);
903 return 0;
904}