Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Simple HLR/VLR database backend using dbi */ |
| 2 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
| 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 5 | * 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 | |
| 23 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 735cd9e | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 24 | #include <openbsc/gsm_04_11.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 25 | #include <openbsc/db.h> |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 26 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 27 | |
| 28 | #include <libgen.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | #include <dbi/dbi.h> |
| 34 | |
| 35 | static char *db_basename = NULL; |
| 36 | static char *db_dirname = NULL; |
| 37 | static dbi_conn conn; |
| 38 | |
| 39 | static char *create_stmts[] = { |
| 40 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 41 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 42 | "key TEXT UNIQUE NOT NULL, " |
| 43 | "value TEXT NOT NULL" |
| 44 | ")", |
| 45 | "INSERT OR IGNORE INTO Meta " |
| 46 | "(key, value) " |
| 47 | "VALUES " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 48 | "('revision', '2')", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 49 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 50 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 51 | "created TIMESTAMP NOT NULL, " |
| 52 | "updated TIMESTAMP NOT NULL, " |
| 53 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 54 | "name TEXT, " |
| 55 | "extension TEXT UNIQUE, " |
| 56 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 57 | "tmsi TEXT UNIQUE, " |
| 58 | "lac INTEGER NOT NULL DEFAULT 0" |
| 59 | ")", |
| 60 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 61 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 62 | "created TIMESTAMP NOT NULL, " |
| 63 | "updated TIMESTAMP NOT NULL, " |
| 64 | "name TEXT, " |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 65 | "classmark1 NUMERIC, " |
| 66 | "classmark2 BLOB, " |
| 67 | "classmark3 BLOB, " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 68 | "imei NUMERIC UNIQUE NOT NULL" |
| 69 | ")", |
| 70 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 71 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 72 | "created TIMESTAMP NOT NULL, " |
| 73 | "updated TIMESTAMP NOT NULL, " |
| 74 | "subscriber_id NUMERIC NOT NULL, " |
| 75 | "equipment_id NUMERIC NOT NULL, " |
| 76 | "UNIQUE (subscriber_id, equipment_id) " |
| 77 | ")", |
| 78 | "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 79 | /* metadata, not part of sms */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 80 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 81 | "created TIMESTAMP NOT NULL, " |
| 82 | "sent TIMESTAMP, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 83 | "sender_id INTEGER NOT NULL, " |
| 84 | "receiver_id INTEGER NOT NULL, " |
| 85 | /* data directly copied/derived from SMS */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 86 | "valid_until TIMESTAMP, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 87 | "reply_path_req INTEGER NOT NULL, " |
| 88 | "status_rep_req INTEGER NOT NULL, " |
| 89 | "protocol_id INTEGER NOT NULL, " |
| 90 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 91 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 92 | "dest_addr TEXT, " |
| 93 | "user_data BLOB, " /* TP-UD */ |
| 94 | /* additional data, interpreted from SMS */ |
| 95 | "header BLOB, " /* UD Header */ |
| 96 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 97 | ")", |
| 98 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 99 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 100 | "created TIMESTAMP NOT NULL, " |
| 101 | "updated TIMESTAMP NOT NULL, " |
| 102 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 103 | "last_bts NUMERIC NOT NULL " |
| 104 | ")", |
| 105 | }; |
| 106 | |
| 107 | void db_error_func(dbi_conn conn, void* data) { |
| 108 | const char* msg; |
| 109 | dbi_conn_error(conn, &msg); |
| 110 | printf("DBI: %s\n", msg); |
| 111 | } |
| 112 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 113 | static int check_db_revision(void) |
| 114 | { |
| 115 | dbi_result result; |
| 116 | const char *rev; |
| 117 | |
| 118 | result = dbi_conn_query(conn, |
| 119 | "SELECT value FROM Meta WHERE key='revision'"); |
| 120 | if (!result) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | if (!dbi_result_next_row(result)) { |
| 124 | dbi_result_free(result); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | rev = dbi_result_get_string(result, "value"); |
| 128 | if (!rev || atoi(rev) != 2) { |
| 129 | dbi_result_free(result); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | dbi_result_free(result); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 137 | int db_init(const char *name) { |
| 138 | dbi_initialize(NULL); |
| 139 | conn = dbi_conn_new("sqlite3"); |
| 140 | if (conn==NULL) { |
| 141 | printf("DB: Failed to create connection.\n"); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
| 146 | |
| 147 | /* MySQL |
| 148 | dbi_conn_set_option(conn, "host", "localhost"); |
| 149 | dbi_conn_set_option(conn, "username", "your_name"); |
| 150 | dbi_conn_set_option(conn, "password", "your_password"); |
| 151 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 152 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 153 | */ |
| 154 | |
| 155 | /* SqLite 3 */ |
| 156 | db_basename = strdup(name); |
| 157 | db_dirname = strdup(name); |
| 158 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 159 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
| 160 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 161 | if (dbi_conn_connect(conn) < 0) |
| 162 | goto out_err; |
| 163 | |
| 164 | if (check_db_revision() < 0) { |
| 165 | fprintf(stderr, "Database schema revision invalid, " |
| 166 | "please update your database schema\n"); |
| 167 | goto out_err; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return 0; |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 171 | |
| 172 | out_err: |
| 173 | free(db_dirname); |
| 174 | free(db_basename); |
| 175 | db_dirname = db_basename = NULL; |
| 176 | return -1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 179 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 180 | int db_prepare() { |
| 181 | dbi_result result; |
| 182 | int i; |
| 183 | |
| 184 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 185 | result = dbi_conn_query(conn, create_stmts[i]); |
| 186 | if (result==NULL) { |
| 187 | printf("DB: Failed to create some table.\n"); |
| 188 | return 1; |
| 189 | } |
| 190 | dbi_result_free(result); |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int db_fini() { |
| 197 | dbi_conn_close(conn); |
| 198 | dbi_shutdown(); |
| 199 | |
| 200 | if (db_dirname) |
| 201 | free(db_dirname); |
| 202 | if (db_basename) |
| 203 | free(db_basename); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 207 | struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi) |
| 208 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 209 | dbi_result result; |
| 210 | struct gsm_subscriber* subscr; |
| 211 | |
| 212 | /* Is this subscriber known in the db? */ |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 213 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 214 | if (subscr) { |
| 215 | result = dbi_conn_queryf(conn, |
| 216 | "UPDATE Subscriber set updated = datetime('now') " |
| 217 | "WHERE imsi = %s " , imsi); |
| 218 | if (result==NULL) { |
| 219 | printf("DB: failed to update timestamp\n"); |
| 220 | } else { |
| 221 | dbi_result_free(result); |
| 222 | } |
| 223 | return subscr; |
| 224 | } |
| 225 | |
| 226 | subscr = subscr_alloc(); |
| 227 | if (!subscr) |
| 228 | return NULL; |
| 229 | result = dbi_conn_queryf(conn, |
| 230 | "INSERT INTO Subscriber " |
| 231 | "(imsi, created, updated) " |
| 232 | "VALUES " |
| 233 | "(%s, datetime('now'), datetime('now')) ", |
| 234 | imsi |
| 235 | ); |
| 236 | if (result==NULL) { |
| 237 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 238 | } |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 239 | subscr->net = net; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 240 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 241 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
| 242 | dbi_result_free(result); |
| 243 | printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
| 244 | return subscr; |
| 245 | } |
| 246 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 247 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 248 | enum gsm_subscriber_field field, |
| 249 | const char *id) |
| 250 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 251 | dbi_result result; |
| 252 | const char *string; |
| 253 | char *quoted; |
| 254 | struct gsm_subscriber *subscr; |
| 255 | |
| 256 | switch (field) { |
| 257 | case GSM_SUBSCRIBER_IMSI: |
| 258 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 259 | result = dbi_conn_queryf(conn, |
| 260 | "SELECT * FROM Subscriber " |
| 261 | "WHERE imsi = %s ", |
| 262 | quoted |
| 263 | ); |
| 264 | free(quoted); |
| 265 | break; |
| 266 | case GSM_SUBSCRIBER_TMSI: |
| 267 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 268 | result = dbi_conn_queryf(conn, |
| 269 | "SELECT * FROM Subscriber " |
| 270 | "WHERE tmsi = %s ", |
| 271 | quoted |
| 272 | ); |
| 273 | free(quoted); |
| 274 | break; |
| 275 | case GSM_SUBSCRIBER_EXTENSION: |
| 276 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 277 | result = dbi_conn_queryf(conn, |
| 278 | "SELECT * FROM Subscriber " |
| 279 | "WHERE extension = %s ", |
| 280 | quoted |
| 281 | ); |
| 282 | free(quoted); |
| 283 | break; |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 284 | case GSM_SUBSCRIBER_ID: |
| 285 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 286 | result = dbi_conn_queryf(conn, |
| 287 | "SELECT * FROM Subscriber " |
| 288 | "WHERE id = %s ", quoted); |
| 289 | free(quoted); |
| 290 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 291 | default: |
| 292 | printf("DB: Unknown query selector for Subscriber.\n"); |
| 293 | return NULL; |
| 294 | } |
| 295 | if (result==NULL) { |
| 296 | printf("DB: Failed to query Subscriber.\n"); |
| 297 | return NULL; |
| 298 | } |
| 299 | if (!dbi_result_next_row(result)) { |
| 300 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 301 | field, id); |
| 302 | dbi_result_free(result); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | subscr = subscr_alloc(); |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 307 | subscr->net = net; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 308 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
| 309 | string = dbi_result_get_string(result, "imsi"); |
| 310 | if (string) |
| 311 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
| 312 | |
| 313 | string = dbi_result_get_string(result, "tmsi"); |
| 314 | if (string) |
| 315 | strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH); |
| 316 | |
| 317 | string = dbi_result_get_string(result, "name"); |
| 318 | if (string) |
| 319 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
| 320 | |
| 321 | string = dbi_result_get_string(result, "extension"); |
| 322 | if (string) |
| 323 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
| 324 | |
| 325 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 326 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
| 327 | printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n", |
| 328 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
| 329 | subscr->lac, subscr->authorized); |
| 330 | dbi_result_free(result); |
| 331 | return subscr; |
| 332 | } |
| 333 | |
| 334 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
| 335 | dbi_result result; |
| 336 | result = dbi_conn_queryf(conn, |
| 337 | "UPDATE Subscriber " |
| 338 | "SET updated = datetime('now'), " |
| 339 | "tmsi = '%s', " |
| 340 | "lac = %i, " |
| 341 | "authorized = %i " |
| 342 | "WHERE imsi = %s ", |
| 343 | subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi |
| 344 | ); |
| 345 | |
| 346 | if (result==NULL) { |
| 347 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 348 | return 1; |
| 349 | } |
| 350 | dbi_result_free(result); |
| 351 | return 0; |
| 352 | } |
| 353 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 354 | int db_sync_equipment(struct gsm_equipment *equip) |
| 355 | { |
| 356 | dbi_result result; |
| 357 | unsigned char *cm2, *cm3; |
| 358 | |
| 359 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 360 | equip->classmark2_len, &cm2); |
| 361 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 362 | equip->classmark3_len, &cm3); |
| 363 | |
| 364 | result = dbi_conn_queryf(conn, |
| 365 | "UPDATE Equipment SET " |
| 366 | "updated = datetime('now'), " |
| 367 | "classmark1 = %u, " |
| 368 | "classmark2 = %s, " |
| 369 | "classmark3 = %s " |
| 370 | "WHERE imei = '%s' ", |
| 371 | equip->classmark1, cm2, cm3, equip->imei); |
| 372 | |
| 373 | free(cm2); |
| 374 | free(cm3); |
| 375 | |
| 376 | if (!result) { |
| 377 | printf("DB: Failed to update Equipment\n"); |
| 378 | return -EIO; |
| 379 | } |
| 380 | |
| 381 | dbi_result_free(result); |
| 382 | return 0; |
| 383 | } |
| 384 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 385 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
| 386 | dbi_result result=NULL; |
| 387 | char* tmsi_quoted; |
| 388 | for (;;) { |
| 389 | sprintf(subscriber->tmsi, "%i", rand()); |
| 390 | dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted); |
| 391 | result = dbi_conn_queryf(conn, |
| 392 | "SELECT * FROM Subscriber " |
| 393 | "WHERE tmsi = %s ", |
| 394 | tmsi_quoted |
| 395 | ); |
| 396 | free(tmsi_quoted); |
| 397 | if (result==NULL) { |
| 398 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
| 399 | return 1; |
| 400 | } |
| 401 | if (dbi_result_get_numrows(result)){ |
| 402 | dbi_result_free(result); |
| 403 | continue; |
| 404 | } |
| 405 | if (!dbi_result_next_row(result)) { |
| 406 | dbi_result_free(result); |
| 407 | printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi); |
| 408 | return db_sync_subscriber(subscriber); |
| 409 | } |
| 410 | dbi_result_free(result); |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
| 416 | u_int64_t equipment_id, watch_id; |
| 417 | dbi_result result; |
| 418 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 419 | strncpy(subscriber->equipment.imei, imei, |
| 420 | sizeof(subscriber->equipment.imei)-1), |
| 421 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 422 | result = dbi_conn_queryf(conn, |
| 423 | "INSERT OR IGNORE INTO Equipment " |
| 424 | "(imei, created, updated) " |
| 425 | "VALUES " |
| 426 | "(%s, datetime('now'), datetime('now')) ", |
| 427 | imei |
| 428 | ); |
| 429 | if (result==NULL) { |
| 430 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 431 | return 1; |
| 432 | } |
| 433 | equipment_id = 0; |
| 434 | if (dbi_result_get_numrows_affected(result)) { |
| 435 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 436 | } |
| 437 | dbi_result_free(result); |
| 438 | if (equipment_id) { |
| 439 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 440 | } |
| 441 | else { |
| 442 | result = dbi_conn_queryf(conn, |
| 443 | "SELECT id FROM Equipment " |
| 444 | "WHERE imei = %s ", |
| 445 | imei |
| 446 | ); |
| 447 | if (result==NULL) { |
| 448 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 449 | return 1; |
| 450 | } |
| 451 | if (!dbi_result_next_row(result)) { |
| 452 | printf("DB: Failed to find the Equipment.\n"); |
| 453 | dbi_result_free(result); |
| 454 | return 1; |
| 455 | } |
| 456 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 457 | dbi_result_free(result); |
| 458 | } |
| 459 | |
| 460 | result = dbi_conn_queryf(conn, |
| 461 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 462 | "(subscriber_id, equipment_id, created, updated) " |
| 463 | "VALUES " |
| 464 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 465 | subscriber->id, equipment_id |
| 466 | ); |
| 467 | if (result==NULL) { |
| 468 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 469 | return 1; |
| 470 | } |
| 471 | watch_id = 0; |
| 472 | if (dbi_result_get_numrows_affected(result)) { |
| 473 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 474 | } |
| 475 | dbi_result_free(result); |
| 476 | if (watch_id) { |
| 477 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 478 | } |
| 479 | else { |
| 480 | result = dbi_conn_queryf(conn, |
| 481 | "UPDATE EquipmentWatch " |
| 482 | "SET updated = datetime('now') " |
| 483 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 484 | subscriber->id, equipment_id |
| 485 | ); |
| 486 | if (result==NULL) { |
| 487 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 488 | return 1; |
| 489 | } |
| 490 | dbi_result_free(result); |
| 491 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* store an [unsent] SMS to the database */ |
| 498 | int db_sms_store(struct gsm_sms *sms) |
| 499 | { |
| 500 | dbi_result result; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 501 | char *q_text, *q_daddr; |
| 502 | unsigned char *q_udata; |
| 503 | char *validity_timestamp = "2222-2-2"; |
| 504 | |
| 505 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 506 | |
| 507 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 508 | dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr); |
| 509 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 510 | &q_udata); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 511 | /* FIXME: correct validity period */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 512 | result = dbi_conn_queryf(conn, |
| 513 | "INSERT INTO SMS " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 514 | "(created, sender_id, receiver_id, valid_until, " |
| 515 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 516 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 517 | "user_data, text) VALUES " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 518 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 519 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 520 | sms->sender->id, |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 521 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 522 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 523 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 524 | q_daddr, q_udata, q_text); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 525 | free(q_text); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 526 | free(q_daddr); |
| 527 | free(q_udata); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 528 | |
| 529 | if (!result) |
| 530 | return -EIO; |
| 531 | |
| 532 | dbi_result_free(result); |
| 533 | return 0; |
| 534 | } |
| 535 | |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 536 | static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 537 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 538 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 539 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 540 | const char *text, *daddr; |
| 541 | const unsigned char *user_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 542 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 543 | if (!sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 544 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 545 | |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 546 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 547 | |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 548 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 549 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 550 | |
| 551 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 552 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 553 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 554 | /* FIXME: validity */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 555 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
| 556 | sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); |
| 557 | sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); |
| 558 | sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind"); |
| 559 | sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); |
| 560 | sms->data_coding_scheme = dbi_result_get_uint(result, |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 561 | "data_coding_scheme"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 562 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 563 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 564 | daddr = dbi_result_get_string(result, "dest_addr"); |
| 565 | if (daddr) { |
| 566 | strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr)); |
| 567 | sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0'; |
| 568 | } |
| 569 | |
| 570 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 571 | user_data = dbi_result_get_binary(result, "user_data"); |
| 572 | if (sms->user_data_len > sizeof(sms->user_data)) |
Holger Hans Peter Freyther | 689e132 | 2009-08-10 07:56:16 +0200 | [diff] [blame] | 573 | sms->user_data_len = (u_int8_t) sizeof(sms->user_data); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 574 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 575 | |
| 576 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 577 | if (text) { |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 578 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 579 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 580 | } |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 581 | return sms; |
| 582 | } |
| 583 | |
| 584 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 585 | struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id) |
| 586 | { |
| 587 | dbi_result result; |
| 588 | struct gsm_sms *sms; |
| 589 | |
| 590 | result = dbi_conn_queryf(conn, |
| 591 | "SELECT * FROM SMS " |
| 592 | "WHERE id >= %llu AND sent is NULL ORDER BY id", |
| 593 | min_id); |
| 594 | if (!result) |
| 595 | return NULL; |
| 596 | |
| 597 | if (!dbi_result_next_row(result)) { |
| 598 | dbi_result_free(result); |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | sms = sms_from_result(net, result); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 603 | |
| 604 | dbi_result_free(result); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 605 | |
| 606 | return sms; |
| 607 | } |
| 608 | |
| 609 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 610 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 611 | { |
| 612 | dbi_result result; |
| 613 | struct gsm_sms *sms; |
| 614 | |
| 615 | result = dbi_conn_queryf(conn, |
| 616 | "SELECT * FROM SMS " |
| 617 | "WHERE receiver_id = %llu AND sent is NULL ORDER BY id", |
| 618 | subscr->id); |
| 619 | if (!result) |
| 620 | return NULL; |
| 621 | |
| 622 | if (!dbi_result_next_row(result)) { |
| 623 | dbi_result_free(result); |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | sms = sms_from_result(subscr->net, result); |
| 628 | |
| 629 | dbi_result_free(result); |
| 630 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 631 | return sms; |
| 632 | } |
| 633 | |
| 634 | /* mark a given SMS as read */ |
| 635 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 636 | { |
| 637 | dbi_result result; |
| 638 | |
| 639 | result = dbi_conn_queryf(conn, |
| 640 | "UPDATE SMS " |
| 641 | "SET sent = datetime('now') " |
| 642 | "WHERE id = %llu", sms->id); |
| 643 | if (!result) { |
| 644 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 645 | return 1; |
| 646 | } |
| 647 | |
| 648 | dbi_result_free(result); |
| 649 | return 0; |
| 650 | } |