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