Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 1 | /* Simple HLR/VLR database backend using dbi */ |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 2 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [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 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 23 | #include <openbsc/gsm_data.h> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 24 | #include <openbsc/db.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 25 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 26 | #include <libgen.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 31 | #include <dbi/dbi.h> |
| 32 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 33 | static char *db_basename = NULL; |
| 34 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 35 | static dbi_conn conn; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 36 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 37 | static char *create_stmts[] = { |
| 38 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 39 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 40 | "key TEXT UNIQUE NOT NULL, " |
| 41 | "value TEXT NOT NULL" |
| 42 | ")", |
| 43 | "INSERT OR IGNORE INTO Meta " |
| 44 | "(key, value) " |
| 45 | "VALUES " |
| 46 | "('revision', '1')", |
| 47 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 48 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 49 | "created TIMESTAMP NOT NULL, " |
| 50 | "updated TIMESTAMP NOT NULL, " |
| 51 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 52 | "name TEXT, " |
| 53 | "extension TEXT UNIQUE, " |
| 54 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 55 | "tmsi TEXT UNIQUE, " |
| 56 | "lac INTEGER NOT NULL DEFAULT 0" |
| 57 | ")", |
| 58 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 59 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 60 | "created TIMESTAMP NOT NULL, " |
| 61 | "updated TIMESTAMP NOT NULL, " |
| 62 | "name TEXT, " |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 63 | "classmark1 NUMERIC, " |
| 64 | "classmark2 BLOB, " |
| 65 | "classmark3 BLOB, " |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 66 | "imei NUMERIC UNIQUE NOT NULL" |
| 67 | ")", |
| 68 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 69 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 70 | "created TIMESTAMP NOT NULL, " |
| 71 | "updated TIMESTAMP NOT NULL, " |
| 72 | "subscriber_id NUMERIC NOT NULL, " |
| 73 | "equipment_id NUMERIC NOT NULL, " |
| 74 | "UNIQUE (subscriber_id, equipment_id) " |
| 75 | ")", |
| 76 | "CREATE TABLE IF NOT EXISTS SMS (" |
| 77 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 78 | "created TIMESTAMP NOT NULL, " |
| 79 | "sent TIMESTAMP, " |
| 80 | "sender_id NUMERIC NOT NULL, " |
| 81 | "receiver_id NUMERIC NOT NULL, " |
| 82 | "header NUMERIC, " |
| 83 | "text TEXT NOT NULL " |
| 84 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 85 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 86 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 87 | "created TIMESTAMP NOT NULL, " |
| 88 | "updated TIMESTAMP NOT NULL, " |
| 89 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 90 | "last_bts NUMERIC NOT NULL " |
| 91 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 94 | void db_error_func(dbi_conn conn, void* data) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 95 | const char* msg; |
| 96 | dbi_conn_error(conn, &msg); |
| 97 | printf("DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Holger Freyther | c7b86f9 | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 100 | int db_init(const char *name) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 101 | dbi_initialize(NULL); |
| 102 | conn = dbi_conn_new("sqlite3"); |
| 103 | if (conn==NULL) { |
| 104 | printf("DB: Failed to create connection.\n"); |
| 105 | return 1; |
| 106 | } |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 107 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 108 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 109 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 110 | /* MySQL |
| 111 | dbi_conn_set_option(conn, "host", "localhost"); |
| 112 | dbi_conn_set_option(conn, "username", "your_name"); |
| 113 | dbi_conn_set_option(conn, "password", "your_password"); |
| 114 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 115 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 116 | */ |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 117 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 118 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 119 | db_basename = strdup(name); |
| 120 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 121 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 122 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 123 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 124 | if (dbi_conn_connect(conn) < 0) { |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 125 | free(db_dirname); |
| 126 | free(db_basename); |
| 127 | db_dirname = db_basename = NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int db_prepare() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 135 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 136 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 137 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 138 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 139 | result = dbi_conn_query(conn, create_stmts[i]); |
| 140 | if (result==NULL) { |
| 141 | printf("DB: Failed to create some table.\n"); |
| 142 | return 1; |
| 143 | } |
| 144 | dbi_result_free(result); |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 145 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 146 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 147 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int db_fini() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 151 | dbi_conn_close(conn); |
| 152 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 153 | |
| 154 | if (db_dirname) |
| 155 | free(db_dirname); |
| 156 | if (db_basename) |
| 157 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 158 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 161 | struct gsm_subscriber* db_create_subscriber(char *imsi) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 162 | dbi_result result; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 163 | struct gsm_subscriber* subscr; |
| 164 | |
| 165 | /* Is this subscriber known in the db? */ |
| 166 | subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
| 167 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 168 | result = dbi_conn_queryf(conn, |
| 169 | "UPDATE Subscriber set updated = datetime('now') " |
| 170 | "WHERE imsi = %s " , imsi); |
| 171 | if (result==NULL) { |
| 172 | printf("DB: failed to update timestamp\n"); |
| 173 | } else { |
| 174 | dbi_result_free(result); |
| 175 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 176 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 177 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 178 | |
| 179 | subscr = subscr_alloc(); |
| 180 | if (!subscr) |
| 181 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 182 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 183 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 184 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 185 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 186 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 187 | imsi |
| 188 | ); |
| 189 | if (result==NULL) { |
| 190 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 191 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 192 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 193 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 194 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 195 | printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
| 196 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 199 | struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 200 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 201 | const char *string; |
| 202 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 203 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 204 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 205 | switch (field) { |
| 206 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 207 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 208 | result = dbi_conn_queryf(conn, |
| 209 | "SELECT * FROM Subscriber " |
| 210 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 211 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 212 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 213 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 214 | break; |
| 215 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 216 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 217 | result = dbi_conn_queryf(conn, |
| 218 | "SELECT * FROM Subscriber " |
| 219 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 220 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 221 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 222 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 223 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 224 | case GSM_SUBSCRIBER_EXTENSION: |
| 225 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 226 | result = dbi_conn_queryf(conn, |
| 227 | "SELECT * FROM Subscriber " |
| 228 | "WHERE extension = %s ", |
| 229 | quoted |
| 230 | ); |
| 231 | free(quoted); |
| 232 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 233 | default: |
| 234 | printf("DB: Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 235 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 236 | } |
| 237 | if (result==NULL) { |
| 238 | printf("DB: Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 239 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 240 | } |
| 241 | if (!dbi_result_next_row(result)) { |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 242 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 243 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 244 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 245 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 246 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 247 | |
| 248 | subscr = subscr_alloc(); |
| 249 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 250 | string = dbi_result_get_string(result, "imsi"); |
| 251 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 252 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 253 | |
| 254 | string = dbi_result_get_string(result, "tmsi"); |
| 255 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 256 | strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 257 | |
| 258 | string = dbi_result_get_string(result, "name"); |
| 259 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 260 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 261 | |
| 262 | string = dbi_result_get_string(result, "extension"); |
| 263 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 264 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 265 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 266 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 267 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Holger Freyther | 9175447 | 2009-06-09 08:52:41 +0000 | [diff] [blame] | 268 | printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n", |
| 269 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 270 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 271 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 272 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 275 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 276 | dbi_result result; |
| 277 | result = dbi_conn_queryf(conn, |
| 278 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 279 | "SET updated = datetime('now'), " |
Holger Freyther | 9175447 | 2009-06-09 08:52:41 +0000 | [diff] [blame] | 280 | "tmsi = '%s', " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 281 | "lac = %i, " |
| 282 | "authorized = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 283 | "WHERE imsi = %s ", |
Jan Luebbe | 6e2e545 | 2008-12-27 16:47:55 +0000 | [diff] [blame] | 284 | subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 285 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 286 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 287 | if (result==NULL) { |
| 288 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 289 | return 1; |
| 290 | } |
| 291 | dbi_result_free(result); |
| 292 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 295 | int db_sync_equipment(struct gsm_equipment *equip) |
| 296 | { |
| 297 | dbi_result result; |
| 298 | unsigned char *cm2, *cm3; |
| 299 | |
| 300 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 301 | equip->classmark2_len, &cm2); |
| 302 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 303 | equip->classmark3_len, &cm3); |
| 304 | |
| 305 | result = dbi_conn_queryf(conn, |
| 306 | "UPDATE Equipment SET " |
| 307 | "updated = datetime('now'), " |
| 308 | "classmark1 = %u, " |
| 309 | "classmark2 = %s, " |
| 310 | "classmark3 = %s " |
| 311 | "WHERE imei = '%s' ", |
| 312 | equip->classmark1, cm2, cm3, equip->imei); |
| 313 | |
| 314 | free(cm2); |
| 315 | free(cm3); |
| 316 | |
| 317 | if (!result) { |
| 318 | printf("DB: Failed to update Equipment\n"); |
| 319 | return -EIO; |
| 320 | } |
| 321 | |
| 322 | dbi_result_free(result); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 326 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 327 | dbi_result result=NULL; |
| 328 | char* tmsi_quoted; |
| 329 | for (;;) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 330 | sprintf(subscriber->tmsi, "%i", rand()); |
| 331 | dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 332 | result = dbi_conn_queryf(conn, |
| 333 | "SELECT * FROM Subscriber " |
| 334 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 335 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 336 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 337 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 338 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 339 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 340 | return 1; |
| 341 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 342 | if (dbi_result_get_numrows(result)){ |
| 343 | dbi_result_free(result); |
| 344 | continue; |
| 345 | } |
| 346 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 347 | dbi_result_free(result); |
| 348 | printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 349 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 350 | } |
| 351 | dbi_result_free(result); |
| 352 | } |
| 353 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 356 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
| 357 | u_int64_t equipment_id, watch_id; |
| 358 | dbi_result result; |
| 359 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 360 | strncpy(subscriber->equipment.imei, imei, |
| 361 | sizeof(subscriber->equipment.imei)-1), |
| 362 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 363 | result = dbi_conn_queryf(conn, |
| 364 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 365 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 366 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 367 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 368 | imei |
| 369 | ); |
| 370 | if (result==NULL) { |
| 371 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 372 | return 1; |
| 373 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 374 | equipment_id = 0; |
| 375 | if (dbi_result_get_numrows_affected(result)) { |
| 376 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 377 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 378 | dbi_result_free(result); |
| 379 | if (equipment_id) { |
| 380 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 381 | } |
| 382 | else { |
| 383 | result = dbi_conn_queryf(conn, |
| 384 | "SELECT id FROM Equipment " |
| 385 | "WHERE imei = %s ", |
| 386 | imei |
| 387 | ); |
| 388 | if (result==NULL) { |
| 389 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 390 | return 1; |
| 391 | } |
| 392 | if (!dbi_result_next_row(result)) { |
| 393 | printf("DB: Failed to find the Equipment.\n"); |
| 394 | dbi_result_free(result); |
| 395 | return 1; |
| 396 | } |
| 397 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 398 | dbi_result_free(result); |
| 399 | } |
| 400 | |
| 401 | result = dbi_conn_queryf(conn, |
| 402 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 403 | "(subscriber_id, equipment_id, created, updated) " |
| 404 | "VALUES " |
| 405 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 406 | subscriber->id, equipment_id |
| 407 | ); |
| 408 | if (result==NULL) { |
| 409 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 410 | return 1; |
| 411 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 412 | watch_id = 0; |
| 413 | if (dbi_result_get_numrows_affected(result)) { |
| 414 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 415 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 416 | dbi_result_free(result); |
| 417 | if (watch_id) { |
| 418 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 419 | } |
| 420 | else { |
| 421 | result = dbi_conn_queryf(conn, |
| 422 | "UPDATE EquipmentWatch " |
| 423 | "SET updated = datetime('now') " |
| 424 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 425 | subscriber->id, equipment_id |
| 426 | ); |
| 427 | if (result==NULL) { |
| 428 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 429 | return 1; |
| 430 | } |
| 431 | dbi_result_free(result); |
| 432 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 438 | /* store an [unsent] SMS to the database */ |
| 439 | int db_sms_store(struct gsm_sms *sms) |
| 440 | { |
| 441 | dbi_result result; |
| 442 | char *q_text; |
| 443 | |
| 444 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
| 445 | result = dbi_conn_queryf(conn, |
| 446 | "INSERT INTO SMS " |
| 447 | "(created,sender_id,receiver_id,header,text) VALUES " |
| 448 | "(datetime('now'),%llu,%llu,%s,%s)", |
| 449 | sms->sender->id, |
| 450 | sms->receiver ? sms->receiver->id : 0, |
| 451 | NULL, q_text); |
| 452 | free(q_text); |
| 453 | |
| 454 | if (!result) |
| 455 | return -EIO; |
| 456 | |
| 457 | dbi_result_free(result); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 462 | struct gsm_sms *db_sms_get_unsent(int min_id) |
| 463 | { |
| 464 | dbi_result result; |
| 465 | struct gsm_sms *sms = malloc(sizeof(*sms)); |
| 466 | |
| 467 | if (!sms) { |
| 468 | free(sms); |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | result = dbi_conn_queryf(conn, |
| 473 | "SELECT * FROM SMS " |
| 474 | "WHERE id >= %llu ORDER BY id", min_id); |
| 475 | if (!result) { |
| 476 | free(sms); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | /* FIXME: fill gsm_sms from database */ |
| 481 | |
| 482 | dbi_result_free(result); |
| 483 | return sms; |
| 484 | } |
| 485 | |
| 486 | /* mark a given SMS as read */ |
| 487 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 488 | { |
| 489 | dbi_result result; |
| 490 | |
| 491 | result = dbi_conn_queryf(conn, |
| 492 | "UPDATE SMS " |
| 493 | "SET sent = datetime('now') " |
| 494 | "WHERE id = %llu", sms->id); |
| 495 | if (!result) { |
| 496 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | dbi_result_free(result); |
| 501 | return 0; |
| 502 | } |