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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #include <libgen.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <dbi/dbi.h> |
| 30 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 31 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | ef3b59d | 2010-12-22 18:21:14 +0100 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 33 | #include <openbsc/gsm_04_11.h> |
| 34 | #include <openbsc/db.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | b74772b | 2010-12-22 18:16:01 +0100 | [diff] [blame] | 36 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/statistics.h> |
| 39 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 40 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | static char *db_basename = NULL; |
| 42 | static char *db_dirname = NULL; |
| 43 | static dbi_conn conn; |
| 44 | |
| 45 | static char *create_stmts[] = { |
| 46 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 47 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 48 | "key TEXT UNIQUE NOT NULL, " |
| 49 | "value TEXT NOT NULL" |
| 50 | ")", |
| 51 | "INSERT OR IGNORE INTO Meta " |
| 52 | "(key, value) " |
| 53 | "VALUES " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 54 | "('revision', '2')", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 55 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 56 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 57 | "created TIMESTAMP NOT NULL, " |
| 58 | "updated TIMESTAMP NOT NULL, " |
| 59 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 60 | "name TEXT, " |
| 61 | "extension TEXT UNIQUE, " |
| 62 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 63 | "tmsi TEXT UNIQUE, " |
| 64 | "lac INTEGER NOT NULL DEFAULT 0" |
| 65 | ")", |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 66 | "CREATE TABLE IF NOT EXISTS AuthToken (" |
| 67 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 68 | "subscriber_id INTEGER UNIQUE NOT NULL, " |
| 69 | "created TIMESTAMP NOT NULL, " |
| 70 | "token TEXT UNIQUE NOT NULL" |
| 71 | ")", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 73 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 74 | "created TIMESTAMP NOT NULL, " |
| 75 | "updated TIMESTAMP NOT NULL, " |
| 76 | "name TEXT, " |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 77 | "classmark1 NUMERIC, " |
| 78 | "classmark2 BLOB, " |
| 79 | "classmark3 BLOB, " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 80 | "imei NUMERIC UNIQUE NOT NULL" |
| 81 | ")", |
| 82 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 83 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 84 | "created TIMESTAMP NOT NULL, " |
| 85 | "updated TIMESTAMP NOT NULL, " |
| 86 | "subscriber_id NUMERIC NOT NULL, " |
| 87 | "equipment_id NUMERIC NOT NULL, " |
| 88 | "UNIQUE (subscriber_id, equipment_id) " |
| 89 | ")", |
| 90 | "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 91 | /* metadata, not part of sms */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 92 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 93 | "created TIMESTAMP NOT NULL, " |
| 94 | "sent TIMESTAMP, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 95 | "sender_id INTEGER NOT NULL, " |
| 96 | "receiver_id INTEGER NOT NULL, " |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 97 | "deliver_attempts INTEGER NOT NULL DEFAULT 0, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 98 | /* data directly copied/derived from SMS */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 99 | "valid_until TIMESTAMP, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 100 | "reply_path_req INTEGER NOT NULL, " |
| 101 | "status_rep_req INTEGER NOT NULL, " |
| 102 | "protocol_id INTEGER NOT NULL, " |
| 103 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 104 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 105 | "dest_addr TEXT, " |
| 106 | "user_data BLOB, " /* TP-UD */ |
| 107 | /* additional data, interpreted from SMS */ |
| 108 | "header BLOB, " /* UD Header */ |
| 109 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 110 | ")", |
| 111 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 112 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 113 | "created TIMESTAMP NOT NULL, " |
| 114 | "updated TIMESTAMP NOT NULL, " |
| 115 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 116 | "last_bts NUMERIC NOT NULL " |
| 117 | ")", |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 118 | "CREATE TABLE IF NOT EXISTS ApduBlobs (" |
| 119 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 120 | "created TIMESTAMP NOT NULL, " |
| 121 | "apdu_id_flags INTEGER NOT NULL, " |
| 122 | "subscriber_id INTEGER NOT NULL, " |
| 123 | "apdu BLOB " |
| 124 | ")", |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 125 | "CREATE TABLE IF NOT EXISTS Counters (" |
| 126 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 127 | "timestamp TIMESTAMP NOT NULL, " |
Harald Welte | 4051831 | 2009-12-22 21:40:42 +0100 | [diff] [blame] | 128 | "value INTEGER NOT NULL, " |
| 129 | "name TEXT NOT NULL " |
Harald Welte | d574fac | 2009-12-24 09:42:07 +0100 | [diff] [blame] | 130 | ")", |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 131 | "CREATE TABLE IF NOT EXISTS RateCounters (" |
| 132 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 133 | "timestamp TIMESTAMP NOT NULL, " |
| 134 | "value INTEGER NOT NULL, " |
| 135 | "name TEXT NOT NULL, " |
Harald Welte | 805eb48 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 136 | "idx INTEGER NOT NULL " |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 137 | ")", |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 138 | "CREATE TABLE IF NOT EXISTS AuthKeys (" |
Sylvain Munaut | 6025f80 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 139 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | 47a79bc | 2009-12-27 19:26:12 +0100 | [diff] [blame] | 140 | "algorithm_id INTEGER NOT NULL, " |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 141 | "a3a8_ki BLOB " |
| 142 | ")", |
Sylvain Munaut | f190bdf | 2010-06-09 13:03:39 +0200 | [diff] [blame] | 143 | "CREATE TABLE IF NOT EXISTS AuthLastTuples (" |
Sylvain Munaut | 6025f80 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 144 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | da87636 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 145 | "issued TIMESTAMP NOT NULL, " |
| 146 | "use_count INTEGER NOT NULL DEFAULT 0, " |
| 147 | "key_seq INTEGER NOT NULL, " |
| 148 | "rand BLOB NOT NULL, " |
| 149 | "sres BLOB NOT NULL, " |
| 150 | "kc BLOB NOT NULL " |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 151 | ")", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 152 | }; |
| 153 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 154 | void db_error_func(dbi_conn conn, void *data) |
| 155 | { |
| 156 | const char *msg; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 157 | dbi_conn_error(conn, &msg); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 158 | LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 159 | } |
| 160 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 161 | static int check_db_revision(void) |
| 162 | { |
| 163 | dbi_result result; |
| 164 | const char *rev; |
| 165 | |
| 166 | result = dbi_conn_query(conn, |
| 167 | "SELECT value FROM Meta WHERE key='revision'"); |
| 168 | if (!result) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | if (!dbi_result_next_row(result)) { |
| 172 | dbi_result_free(result); |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | rev = dbi_result_get_string(result, "value"); |
| 176 | if (!rev || atoi(rev) != 2) { |
| 177 | dbi_result_free(result); |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | dbi_result_free(result); |
| 182 | return 0; |
| 183 | } |
| 184 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 185 | int db_init(const char *name) |
| 186 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 187 | dbi_initialize(NULL); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 188 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 189 | conn = dbi_conn_new("sqlite3"); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 190 | if (conn == NULL) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 191 | LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
| 196 | |
| 197 | /* MySQL |
| 198 | dbi_conn_set_option(conn, "host", "localhost"); |
| 199 | dbi_conn_set_option(conn, "username", "your_name"); |
| 200 | dbi_conn_set_option(conn, "password", "your_password"); |
| 201 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 202 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 203 | */ |
| 204 | |
| 205 | /* SqLite 3 */ |
| 206 | db_basename = strdup(name); |
| 207 | db_dirname = strdup(name); |
| 208 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 209 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
| 210 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 211 | if (dbi_conn_connect(conn) < 0) |
| 212 | goto out_err; |
| 213 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 214 | return 0; |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 215 | |
| 216 | out_err: |
| 217 | free(db_dirname); |
| 218 | free(db_basename); |
| 219 | db_dirname = db_basename = NULL; |
| 220 | return -1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 223 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 224 | int db_prepare() |
| 225 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 226 | dbi_result result; |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 230 | result = dbi_conn_query(conn, create_stmts[i]); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 231 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 232 | LOGP(DDB, LOGL_ERROR, |
| 233 | "Failed to create some table.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | dbi_result_free(result); |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | d8d0035 | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 239 | if (check_db_revision() < 0) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 240 | LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, " |
Holger Hans Peter Freyther | d8d0035 | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 241 | "please update your database schema\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 248 | int db_fini() |
| 249 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 250 | dbi_conn_close(conn); |
| 251 | dbi_shutdown(); |
| 252 | |
| 253 | if (db_dirname) |
| 254 | free(db_dirname); |
| 255 | if (db_basename) |
| 256 | free(db_basename); |
| 257 | return 0; |
| 258 | } |
| 259 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 260 | struct gsm_subscriber *db_create_subscriber(struct gsm_network *net, char *imsi) |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 261 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 262 | dbi_result result; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 263 | struct gsm_subscriber *subscr; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 264 | |
| 265 | /* Is this subscriber known in the db? */ |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 266 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 267 | if (subscr) { |
| 268 | result = dbi_conn_queryf(conn, |
| 269 | "UPDATE Subscriber set updated = datetime('now') " |
| 270 | "WHERE imsi = %s " , imsi); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 271 | if (!result) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 272 | LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n"); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 273 | else |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 274 | dbi_result_free(result); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 275 | return subscr; |
| 276 | } |
| 277 | |
| 278 | subscr = subscr_alloc(); |
Jan Luebbe | e297403 | 2009-08-12 10:12:52 +0200 | [diff] [blame] | 279 | subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 280 | if (!subscr) |
| 281 | return NULL; |
| 282 | result = dbi_conn_queryf(conn, |
| 283 | "INSERT INTO Subscriber " |
| 284 | "(imsi, created, updated) " |
| 285 | "VALUES " |
| 286 | "(%s, datetime('now'), datetime('now')) ", |
| 287 | imsi |
| 288 | ); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 289 | if (!result) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 290 | LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n"); |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 291 | subscr->net = net; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 292 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 293 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
| 294 | dbi_result_free(result); |
Harald Welte (local) | c9ba256 | 2009-12-26 18:57:32 +0100 | [diff] [blame] | 295 | LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 296 | db_subscriber_alloc_exten(subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 297 | return subscr; |
| 298 | } |
| 299 | |
Holger Hans Peter Freyther | 5a5ee60 | 2010-03-30 15:28:36 +0200 | [diff] [blame] | 300 | static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size); |
| 301 | |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 302 | static int get_equipment_by_subscr(struct gsm_subscriber *subscr) |
| 303 | { |
| 304 | dbi_result result; |
Harald Welte | 044008d | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 305 | const char *string; |
Harald Welte | f330bf4 | 2009-12-09 19:19:45 +0100 | [diff] [blame] | 306 | unsigned char cm1; |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 307 | const unsigned char *cm2, *cm3; |
| 308 | struct gsm_equipment *equip = &subscr->equipment; |
| 309 | |
| 310 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 311 | "SELECT Equipment.* " |
| 312 | "FROM Equipment JOIN EquipmentWatch ON " |
| 313 | "EquipmentWatch.equipment_id=Equipment.id " |
| 314 | "WHERE EquipmentWatch.subscriber_id = %llu " |
| 315 | "ORDER BY EquipmentWatch.updated DESC", subscr->id); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 316 | if (!result) |
| 317 | return -EIO; |
| 318 | |
| 319 | if (!dbi_result_next_row(result)) { |
| 320 | dbi_result_free(result); |
| 321 | return -ENOENT; |
| 322 | } |
| 323 | |
| 324 | equip->id = dbi_result_get_ulonglong(result, "id"); |
| 325 | |
| 326 | string = dbi_result_get_string(result, "imei"); |
| 327 | if (string) |
| 328 | strncpy(equip->imei, string, sizeof(equip->imei)); |
| 329 | |
Harald Welte (local) | 990ca03 | 2009-12-26 18:56:00 +0100 | [diff] [blame] | 330 | string = dbi_result_get_string(result, "classmark1"); |
Holger Hans Peter Freyther | 5a5ee60 | 2010-03-30 15:28:36 +0200 | [diff] [blame] | 331 | if (string) { |
| 332 | cm1 = atoi(string) & 0xff; |
| 333 | memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1)); |
| 334 | } |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 335 | |
| 336 | equip->classmark2_len = dbi_result_get_field_length(result, "classmark2"); |
| 337 | cm2 = dbi_result_get_binary(result, "classmark2"); |
| 338 | if (equip->classmark2_len > sizeof(equip->classmark2)) |
| 339 | equip->classmark2_len = sizeof(equip->classmark2); |
| 340 | memcpy(equip->classmark2, cm2, equip->classmark2_len); |
| 341 | |
| 342 | equip->classmark3_len = dbi_result_get_field_length(result, "classmark3"); |
| 343 | cm3 = dbi_result_get_binary(result, "classmark3"); |
| 344 | if (equip->classmark3_len > sizeof(equip->classmark3)) |
| 345 | equip->classmark3_len = sizeof(equip->classmark3); |
| 346 | memcpy(equip->classmark3, cm3, equip->classmark3_len); |
| 347 | |
| 348 | dbi_result_free(result); |
| 349 | |
| 350 | return 0; |
| 351 | } |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 352 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 353 | int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 354 | struct gsm_subscriber *subscr) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 355 | { |
| 356 | dbi_result result; |
| 357 | const unsigned char *a3a8_ki; |
| 358 | |
| 359 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 360 | "SELECT * FROM AuthKeys WHERE subscriber_id=%llu", |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 361 | subscr->id); |
| 362 | if (!result) |
| 363 | return -EIO; |
| 364 | |
| 365 | if (!dbi_result_next_row(result)) { |
| 366 | dbi_result_free(result); |
| 367 | return -ENOENT; |
| 368 | } |
| 369 | |
| 370 | ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id"); |
| 371 | ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki"); |
| 372 | a3a8_ki = dbi_result_get_binary(result, "a3a8_ki"); |
Sylvain Munaut | 00b043a | 2009-12-27 19:24:05 +0100 | [diff] [blame] | 373 | if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki)) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 374 | ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki_len); |
| 375 | memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len); |
| 376 | |
| 377 | dbi_result_free(result); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 382 | int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 383 | struct gsm_subscriber *subscr) |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 384 | { |
| 385 | dbi_result result; |
| 386 | struct gsm_auth_info ainfo_old; |
| 387 | int rc, upd; |
| 388 | unsigned char *ki_str; |
| 389 | |
| 390 | /* Deletion ? */ |
| 391 | if (ainfo == NULL) { |
| 392 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 393 | "DELETE FROM AuthKeys WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 394 | subscr->id); |
| 395 | |
| 396 | if (!result) |
| 397 | return -EIO; |
| 398 | |
| 399 | dbi_result_free(result); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | /* Check if already existing */ |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 405 | rc = db_get_authinfo_for_subscr(&ainfo_old, subscr); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 406 | if (rc && rc != -ENOENT) |
| 407 | return rc; |
| 408 | upd = rc ? 0 : 1; |
| 409 | |
| 410 | /* Update / Insert */ |
| 411 | dbi_conn_quote_binary_copy(conn, |
| 412 | ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str); |
| 413 | |
| 414 | if (!upd) { |
| 415 | result = dbi_conn_queryf(conn, |
| 416 | "INSERT INTO AuthKeys " |
| 417 | "(subscriber_id, algorithm_id, a3a8_ki) " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 418 | "VALUES (%llu, %u, %s)", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 419 | subscr->id, ainfo->auth_algo, ki_str); |
| 420 | } else { |
| 421 | result = dbi_conn_queryf(conn, |
| 422 | "UPDATE AuthKeys " |
| 423 | "SET algorithm_id=%u, a3a8_ki=%s " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 424 | "WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 425 | ainfo->auth_algo, ki_str, subscr->id); |
| 426 | } |
| 427 | |
| 428 | free(ki_str); |
| 429 | |
| 430 | if (!result) |
| 431 | return -EIO; |
| 432 | |
| 433 | dbi_result_free(result); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 438 | int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 439 | struct gsm_subscriber *subscr) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 440 | { |
| 441 | dbi_result result; |
| 442 | int len; |
| 443 | const unsigned char *blob; |
| 444 | |
| 445 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 446 | "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu", |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 447 | subscr->id); |
| 448 | if (!result) |
| 449 | return -EIO; |
| 450 | |
| 451 | if (!dbi_result_next_row(result)) { |
| 452 | dbi_result_free(result); |
| 453 | return -ENOENT; |
| 454 | } |
| 455 | |
| 456 | memset(atuple, 0, sizeof(atuple)); |
| 457 | |
Sylvain Munaut | da87636 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 458 | atuple->use_count = dbi_result_get_ulonglong(result, "use_count"); |
| 459 | atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq"); |
| 460 | |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 461 | len = dbi_result_get_field_length(result, "rand"); |
| 462 | if (len != sizeof(atuple->rand)) |
| 463 | goto err_size; |
| 464 | |
| 465 | blob = dbi_result_get_binary(result, "rand"); |
| 466 | memcpy(atuple->rand, blob, len); |
| 467 | |
| 468 | len = dbi_result_get_field_length(result, "sres"); |
| 469 | if (len != sizeof(atuple->sres)) |
| 470 | goto err_size; |
| 471 | |
| 472 | blob = dbi_result_get_binary(result, "sres"); |
| 473 | memcpy(atuple->sres, blob, len); |
| 474 | |
| 475 | len = dbi_result_get_field_length(result, "kc"); |
| 476 | if (len != sizeof(atuple->kc)) |
| 477 | goto err_size; |
| 478 | |
| 479 | blob = dbi_result_get_binary(result, "kc"); |
| 480 | memcpy(atuple->kc, blob, len); |
| 481 | |
| 482 | dbi_result_free(result); |
| 483 | |
| 484 | return 0; |
| 485 | |
| 486 | err_size: |
| 487 | dbi_result_free(result); |
| 488 | return -EIO; |
| 489 | } |
| 490 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 491 | int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 492 | struct gsm_subscriber *subscr) |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 493 | { |
| 494 | dbi_result result; |
| 495 | int rc, upd; |
| 496 | struct gsm_auth_tuple atuple_old; |
| 497 | unsigned char *rand_str, *sres_str, *kc_str; |
| 498 | |
| 499 | /* Deletion ? */ |
| 500 | if (atuple == NULL) { |
| 501 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 502 | "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 503 | subscr->id); |
| 504 | |
| 505 | if (!result) |
| 506 | return -EIO; |
| 507 | |
| 508 | dbi_result_free(result); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* Check if already existing */ |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 514 | rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 515 | if (rc && rc != -ENOENT) |
| 516 | return rc; |
| 517 | upd = rc ? 0 : 1; |
| 518 | |
| 519 | /* Update / Insert */ |
| 520 | dbi_conn_quote_binary_copy(conn, |
| 521 | atuple->rand, sizeof(atuple->rand), &rand_str); |
| 522 | dbi_conn_quote_binary_copy(conn, |
| 523 | atuple->sres, sizeof(atuple->sres), &sres_str); |
| 524 | dbi_conn_quote_binary_copy(conn, |
| 525 | atuple->kc, sizeof(atuple->kc), &kc_str); |
| 526 | |
| 527 | if (!upd) { |
| 528 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | f190bdf | 2010-06-09 13:03:39 +0200 | [diff] [blame] | 529 | "INSERT INTO AuthLastTuples " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 530 | "(subscriber_id, issued, use_count, " |
| 531 | "key_seq, rand, sres, kc) " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 532 | "VALUES (%llu, datetime('now'), %u, " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 533 | "%u, %s, %s, %s ) ", |
| 534 | subscr->id, atuple->use_count, atuple->key_seq, |
| 535 | rand_str, sres_str, kc_str); |
| 536 | } else { |
| 537 | char *issued = atuple->key_seq == atuple_old.key_seq ? |
| 538 | "issued" : "datetime('now')"; |
| 539 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | ef710ae | 2010-06-10 22:26:21 +0200 | [diff] [blame] | 540 | "UPDATE AuthLastTuples " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 541 | "SET issued=%s, use_count=%u, " |
| 542 | "key_seq=%u, rand=%s, sres=%s, kc=%s " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 543 | "WHERE subscriber_id = %llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 544 | issued, atuple->use_count, atuple->key_seq, |
| 545 | rand_str, sres_str, kc_str, subscr->id); |
| 546 | } |
| 547 | |
| 548 | free(rand_str); |
| 549 | free(sres_str); |
| 550 | free(kc_str); |
| 551 | |
| 552 | if (!result) |
| 553 | return -EIO; |
| 554 | |
| 555 | dbi_result_free(result); |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 560 | static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result) |
| 561 | { |
| 562 | const char *string; |
| 563 | string = dbi_result_get_string(result, "imsi"); |
| 564 | if (string) |
| 565 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
| 566 | |
| 567 | string = dbi_result_get_string(result, "tmsi"); |
| 568 | if (string) |
| 569 | subscr->tmsi = tmsi_from_string(string); |
| 570 | |
| 571 | string = dbi_result_get_string(result, "name"); |
| 572 | if (string) |
| 573 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
| 574 | |
| 575 | string = dbi_result_get_string(result, "extension"); |
| 576 | if (string) |
| 577 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
| 578 | |
| 579 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 580 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
| 581 | } |
| 582 | |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 583 | #define BASE_QUERY "SELECT * FROM Subscriber " |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 584 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 585 | enum gsm_subscriber_field field, |
| 586 | const char *id) |
| 587 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 588 | dbi_result result; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 589 | char *quoted; |
| 590 | struct gsm_subscriber *subscr; |
| 591 | |
| 592 | switch (field) { |
| 593 | case GSM_SUBSCRIBER_IMSI: |
| 594 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 595 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 596 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 597 | "WHERE imsi = %s ", |
| 598 | quoted |
| 599 | ); |
| 600 | free(quoted); |
| 601 | break; |
| 602 | case GSM_SUBSCRIBER_TMSI: |
| 603 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 604 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 605 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 606 | "WHERE tmsi = %s ", |
| 607 | quoted |
| 608 | ); |
| 609 | free(quoted); |
| 610 | break; |
| 611 | case GSM_SUBSCRIBER_EXTENSION: |
| 612 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 613 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 614 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 615 | "WHERE extension = %s ", |
| 616 | quoted |
| 617 | ); |
| 618 | free(quoted); |
| 619 | break; |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 620 | case GSM_SUBSCRIBER_ID: |
| 621 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 622 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 623 | BASE_QUERY |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 624 | "WHERE id = %s ", quoted); |
| 625 | free(quoted); |
| 626 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 627 | default: |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 628 | LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 629 | return NULL; |
| 630 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 631 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 632 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
| 635 | if (!dbi_result_next_row(result)) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 636 | DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 637 | field, id); |
| 638 | dbi_result_free(result); |
| 639 | return NULL; |
| 640 | } |
| 641 | |
| 642 | subscr = subscr_alloc(); |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 643 | subscr->net = net; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 644 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 645 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 646 | db_set_from_query(subscr, result); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 647 | DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 648 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
| 649 | subscr->lac, subscr->authorized); |
| 650 | dbi_result_free(result); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 651 | |
| 652 | get_equipment_by_subscr(subscr); |
| 653 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 654 | return subscr; |
| 655 | } |
| 656 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 657 | int db_subscriber_update(struct gsm_subscriber *subscr) |
| 658 | { |
| 659 | char buf[32]; |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 660 | dbi_result result; |
| 661 | |
| 662 | /* Copy the id to a string as queryf with %llu is failing */ |
| 663 | sprintf(buf, "%llu", subscr->id); |
| 664 | result = dbi_conn_queryf(conn, |
| 665 | BASE_QUERY |
| 666 | "WHERE id = %s", buf); |
| 667 | |
| 668 | if (!result) { |
| 669 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id); |
| 670 | return -EIO; |
| 671 | } |
| 672 | if (!dbi_result_next_row(result)) { |
| 673 | DEBUGP(DDB, "Failed to find the Subscriber. %llu\n", |
| 674 | subscr->id); |
| 675 | dbi_result_free(result); |
| 676 | return -EIO; |
| 677 | } |
| 678 | |
| 679 | db_set_from_query(subscr, result); |
| 680 | dbi_result_free(result); |
| 681 | get_equipment_by_subscr(subscr); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 686 | int db_sync_subscriber(struct gsm_subscriber *subscriber) |
| 687 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 688 | dbi_result result; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 689 | char tmsi[14]; |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 690 | char *q_tmsi, *q_name, *q_extension; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 691 | |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 692 | dbi_conn_quote_string_copy(conn, |
| 693 | subscriber->name, &q_name); |
| 694 | dbi_conn_quote_string_copy(conn, |
| 695 | subscriber->extension, &q_extension); |
| 696 | |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 697 | if (subscriber->tmsi != GSM_RESERVED_TMSI) { |
| 698 | sprintf(tmsi, "%u", subscriber->tmsi); |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 699 | dbi_conn_quote_string_copy(conn, |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 700 | tmsi, |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 701 | &q_tmsi); |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 702 | } else |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 703 | q_tmsi = strdup("NULL"); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 704 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 705 | result = dbi_conn_queryf(conn, |
| 706 | "UPDATE Subscriber " |
| 707 | "SET updated = datetime('now'), " |
Harald Welte | c06687d | 2010-12-26 21:23:51 +0100 | [diff] [blame] | 708 | "name = %s, " |
| 709 | "extension = %s, " |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 710 | "authorized = %i, " |
| 711 | "tmsi = %s, " |
| 712 | "lac = %i " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 713 | "WHERE imsi = %s ", |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 714 | q_name, |
| 715 | q_extension, |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 716 | subscriber->authorized, |
| 717 | q_tmsi, |
| 718 | subscriber->lac, |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 719 | subscriber->imsi); |
| 720 | |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 721 | free(q_tmsi); |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 722 | free(q_name); |
| 723 | free(q_extension); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 724 | |
| 725 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 726 | LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 727 | return 1; |
| 728 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 729 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 730 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 731 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 735 | int db_sync_equipment(struct gsm_equipment *equip) |
| 736 | { |
| 737 | dbi_result result; |
| 738 | unsigned char *cm2, *cm3; |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 739 | char *q_imei; |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 740 | u_int8_t classmark1; |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 741 | |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 742 | memcpy(&classmark1, &equip->classmark1, sizeof(classmark1)); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 743 | DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x", |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 744 | equip->imei, classmark1); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 745 | if (equip->classmark2_len) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 746 | DEBUGPC(DDB, ", classmark2=%s", |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 747 | hexdump(equip->classmark2, equip->classmark2_len)); |
| 748 | if (equip->classmark3_len) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 749 | DEBUGPC(DDB, ", classmark3=%s", |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 750 | hexdump(equip->classmark3, equip->classmark3_len)); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 751 | DEBUGPC(DDB, "\n"); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 752 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 753 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 754 | equip->classmark2_len, &cm2); |
| 755 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 756 | equip->classmark3_len, &cm3); |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 757 | dbi_conn_quote_string_copy(conn, equip->imei, &q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 758 | |
| 759 | result = dbi_conn_queryf(conn, |
| 760 | "UPDATE Equipment SET " |
| 761 | "updated = datetime('now'), " |
| 762 | "classmark1 = %u, " |
| 763 | "classmark2 = %s, " |
| 764 | "classmark3 = %s " |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 765 | "WHERE imei = %s ", |
| 766 | classmark1, cm2, cm3, q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 767 | |
| 768 | free(cm2); |
| 769 | free(cm3); |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 770 | free(q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 771 | |
| 772 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 773 | LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n"); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 774 | return -EIO; |
| 775 | } |
| 776 | |
| 777 | dbi_result_free(result); |
| 778 | return 0; |
| 779 | } |
| 780 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 781 | int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber) |
| 782 | { |
| 783 | dbi_result result = NULL; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 784 | char tmsi[14]; |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 785 | char *tmsi_quoted; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 786 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 787 | for (;;) { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 788 | subscriber->tmsi = rand(); |
| 789 | if (subscriber->tmsi == GSM_RESERVED_TMSI) |
| 790 | continue; |
| 791 | |
| 792 | sprintf(tmsi, "%u", subscriber->tmsi); |
| 793 | dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 794 | result = dbi_conn_queryf(conn, |
| 795 | "SELECT * FROM Subscriber " |
| 796 | "WHERE tmsi = %s ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 797 | tmsi_quoted); |
| 798 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 799 | free(tmsi_quoted); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 800 | |
| 801 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 802 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 803 | "while allocating new TMSI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 804 | return 1; |
| 805 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 806 | if (dbi_result_get_numrows(result)) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 807 | dbi_result_free(result); |
| 808 | continue; |
| 809 | } |
| 810 | if (!dbi_result_next_row(result)) { |
| 811 | dbi_result_free(result); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 812 | DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n", |
| 813 | subscriber->tmsi, subscriber->imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 814 | return db_sync_subscriber(subscriber); |
| 815 | } |
| 816 | dbi_result_free(result); |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 821 | int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber) |
| 822 | { |
| 823 | dbi_result result = NULL; |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 824 | u_int32_t try; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 825 | |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 826 | for (;;) { |
Jan Luebbe | ae73121 | 2009-08-12 21:27:43 +0200 | [diff] [blame] | 827 | try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 828 | result = dbi_conn_queryf(conn, |
| 829 | "SELECT * FROM Subscriber " |
Jan Luebbe | 70a9a0f | 2009-08-12 19:59:27 +0200 | [diff] [blame] | 830 | "WHERE extension = %i", |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 831 | try |
| 832 | ); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 833 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 834 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 835 | "while allocating new extension.\n"); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 836 | return 1; |
| 837 | } |
| 838 | if (dbi_result_get_numrows(result)){ |
| 839 | dbi_result_free(result); |
| 840 | continue; |
| 841 | } |
| 842 | if (!dbi_result_next_row(result)) { |
| 843 | dbi_result_free(result); |
| 844 | break; |
| 845 | } |
| 846 | dbi_result_free(result); |
| 847 | } |
| 848 | sprintf(subscriber->extension, "%i", try); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 849 | DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 850 | return db_sync_subscriber(subscriber); |
| 851 | } |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 852 | /* |
| 853 | * try to allocate a new unique token for this subscriber and return it |
| 854 | * via a parameter. if the subscriber already has a token, return |
| 855 | * an error. |
| 856 | */ |
| 857 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 858 | int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, u_int32_t *token) |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 859 | { |
| 860 | dbi_result result; |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 861 | u_int32_t try; |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 862 | |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 863 | for (;;) { |
| 864 | try = rand(); |
| 865 | if (!try) /* 0 is an invalid token */ |
| 866 | continue; |
| 867 | result = dbi_conn_queryf(conn, |
| 868 | "SELECT * FROM AuthToken " |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 869 | "WHERE subscriber_id = %llu OR token = \"%08X\" ", |
| 870 | subscriber->id, try); |
| 871 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 872 | LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken " |
| 873 | "while allocating new token.\n"); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 874 | return 1; |
| 875 | } |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 876 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 877 | dbi_result_free(result); |
| 878 | continue; |
| 879 | } |
| 880 | if (!dbi_result_next_row(result)) { |
| 881 | dbi_result_free(result); |
| 882 | break; |
| 883 | } |
| 884 | dbi_result_free(result); |
| 885 | } |
| 886 | result = dbi_conn_queryf(conn, |
| 887 | "INSERT INTO AuthToken " |
| 888 | "(subscriber_id, created, token) " |
| 889 | "VALUES " |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 890 | "(%llu, datetime('now'), \"%08X\") ", |
| 891 | subscriber->id, try); |
| 892 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 893 | LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for " |
| 894 | "IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 895 | return 1; |
| 896 | } |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 897 | dbi_result_free(result); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 898 | *token = try; |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 899 | DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi); |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 900 | |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 904 | int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH]) |
| 905 | { |
Harald Welte | 26abffa | 2009-11-07 00:06:19 +0900 | [diff] [blame] | 906 | unsigned long long equipment_id, watch_id; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 907 | dbi_result result; |
| 908 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 909 | strncpy(subscriber->equipment.imei, imei, |
| 910 | sizeof(subscriber->equipment.imei)-1), |
| 911 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 912 | result = dbi_conn_queryf(conn, |
| 913 | "INSERT OR IGNORE INTO Equipment " |
| 914 | "(imei, created, updated) " |
| 915 | "VALUES " |
| 916 | "(%s, datetime('now'), datetime('now')) ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 917 | imei); |
| 918 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 919 | LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 920 | return 1; |
| 921 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 922 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 923 | equipment_id = 0; |
| 924 | if (dbi_result_get_numrows_affected(result)) { |
| 925 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 926 | } |
| 927 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 928 | |
| 929 | if (equipment_id) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 930 | DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 931 | else { |
| 932 | result = dbi_conn_queryf(conn, |
| 933 | "SELECT id FROM Equipment " |
| 934 | "WHERE imei = %s ", |
| 935 | imei |
| 936 | ); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 937 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 938 | LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 939 | return 1; |
| 940 | } |
| 941 | if (!dbi_result_next_row(result)) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 942 | LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 943 | dbi_result_free(result); |
| 944 | return 1; |
| 945 | } |
| 946 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 947 | dbi_result_free(result); |
| 948 | } |
| 949 | |
| 950 | result = dbi_conn_queryf(conn, |
| 951 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 952 | "(subscriber_id, equipment_id, created, updated) " |
| 953 | "VALUES " |
| 954 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 955 | subscriber->id, equipment_id); |
| 956 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 957 | LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 958 | return 1; |
| 959 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 960 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 961 | watch_id = 0; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 962 | if (dbi_result_get_numrows_affected(result)) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 963 | watch_id = dbi_conn_sequence_last(conn, NULL); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 964 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 965 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 966 | if (watch_id) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 967 | DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 968 | equipment_id, subscriber->imsi, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 969 | else { |
| 970 | result = dbi_conn_queryf(conn, |
| 971 | "UPDATE EquipmentWatch " |
| 972 | "SET updated = datetime('now') " |
| 973 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 974 | subscriber->id, equipment_id); |
| 975 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 976 | LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 977 | return 1; |
| 978 | } |
| 979 | dbi_result_free(result); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 980 | DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 981 | equipment_id, subscriber->imsi, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | /* store an [unsent] SMS to the database */ |
| 988 | int db_sms_store(struct gsm_sms *sms) |
| 989 | { |
| 990 | dbi_result result; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 991 | char *q_text, *q_daddr; |
| 992 | unsigned char *q_udata; |
| 993 | char *validity_timestamp = "2222-2-2"; |
| 994 | |
| 995 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 996 | |
| 997 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 998 | dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr); |
| 999 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 1000 | &q_udata); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1001 | /* FIXME: correct validity period */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1002 | result = dbi_conn_queryf(conn, |
| 1003 | "INSERT INTO SMS " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1004 | "(created, sender_id, receiver_id, valid_until, " |
| 1005 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1006 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 1007 | "user_data, text) VALUES " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1008 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1009 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1010 | sms->sender->id, |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1011 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 1012 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1013 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 1014 | q_daddr, q_udata, q_text); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1015 | free(q_text); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1016 | free(q_daddr); |
| 1017 | free(q_udata); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1018 | |
| 1019 | if (!result) |
| 1020 | return -EIO; |
| 1021 | |
| 1022 | dbi_result_free(result); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1026 | 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] | 1027 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1028 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1029 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1030 | const char *text, *daddr; |
| 1031 | const unsigned char *user_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1032 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1033 | if (!sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1034 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1035 | |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1036 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1037 | |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1038 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1039 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1040 | |
| 1041 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1042 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1043 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1044 | /* FIXME: validity */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1045 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
| 1046 | sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); |
| 1047 | sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); |
| 1048 | sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind"); |
| 1049 | sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); |
| 1050 | sms->data_coding_scheme = dbi_result_get_uint(result, |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1051 | "data_coding_scheme"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1052 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1053 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1054 | daddr = dbi_result_get_string(result, "dest_addr"); |
| 1055 | if (daddr) { |
| 1056 | strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr)); |
| 1057 | sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0'; |
| 1058 | } |
| 1059 | |
| 1060 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 1061 | user_data = dbi_result_get_binary(result, "user_data"); |
| 1062 | if (sms->user_data_len > sizeof(sms->user_data)) |
Holger Hans Peter Freyther | 689e132 | 2009-08-10 07:56:16 +0200 | [diff] [blame] | 1063 | sms->user_data_len = (u_int8_t) sizeof(sms->user_data); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1064 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1065 | |
| 1066 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1067 | if (text) { |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1068 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1069 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 1070 | } |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1071 | return sms; |
| 1072 | } |
| 1073 | |
Holger Hans Peter Freyther | d0095e4 | 2010-12-24 23:18:31 +0100 | [diff] [blame] | 1074 | struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id) |
| 1075 | { |
| 1076 | dbi_result result; |
| 1077 | struct gsm_sms *sms; |
| 1078 | |
| 1079 | result = dbi_conn_queryf(conn, |
| 1080 | "SELECT * FROM SMS WHERE SMS.id = %llu", id); |
| 1081 | if (!result) |
| 1082 | return NULL; |
| 1083 | |
| 1084 | if (!dbi_result_next_row(result)) { |
| 1085 | dbi_result_free(result); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | sms = sms_from_result(net, result); |
| 1090 | |
| 1091 | dbi_result_free(result); |
| 1092 | |
| 1093 | return sms; |
| 1094 | } |
| 1095 | |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1096 | /* retrieve the next unsent SMS with ID >= min_id */ |
Holger Hans Peter Freyther | 082ec46 | 2010-03-25 09:59:30 +0100 | [diff] [blame] | 1097 | struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id) |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1098 | { |
| 1099 | dbi_result result; |
| 1100 | struct gsm_sms *sms; |
| 1101 | |
| 1102 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1103 | "SELECT SMS.* " |
| 1104 | "FROM SMS JOIN Subscriber ON " |
| 1105 | "SMS.receiver_id = Subscriber.id " |
| 1106 | "WHERE SMS.id >= %llu AND SMS.sent IS NULL " |
| 1107 | "AND Subscriber.lac > 0 " |
| 1108 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1109 | min_id); |
| 1110 | if (!result) |
| 1111 | return NULL; |
| 1112 | |
| 1113 | if (!dbi_result_next_row(result)) { |
| 1114 | dbi_result_free(result); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
| 1118 | sms = sms_from_result(net, result); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1119 | |
| 1120 | dbi_result_free(result); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1121 | |
| 1122 | return sms; |
| 1123 | } |
| 1124 | |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1125 | struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, |
| 1126 | unsigned long long min_subscr_id, |
| 1127 | unsigned int failed) |
Sylvain Munaut | c2e0a44 | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1128 | { |
| 1129 | dbi_result result; |
| 1130 | struct gsm_sms *sms; |
| 1131 | |
| 1132 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1133 | "SELECT SMS.* " |
| 1134 | "FROM SMS JOIN Subscriber ON " |
| 1135 | "SMS.receiver_id = Subscriber.id " |
| 1136 | "WHERE SMS.receiver_id >= %llu AND SMS.sent IS NULL " |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1137 | "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u " |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1138 | "ORDER BY SMS.receiver_id, SMS.id LIMIT 1", |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1139 | min_subscr_id, failed); |
Sylvain Munaut | c2e0a44 | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1140 | if (!result) |
| 1141 | return NULL; |
| 1142 | |
| 1143 | if (!dbi_result_next_row(result)) { |
| 1144 | dbi_result_free(result); |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | |
| 1148 | sms = sms_from_result(net, result); |
| 1149 | |
| 1150 | dbi_result_free(result); |
| 1151 | |
| 1152 | return sms; |
| 1153 | } |
| 1154 | |
Sylvain Munaut | 889b01c | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 1155 | /* retrieve the next unsent SMS for a given subscriber */ |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1156 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 1157 | { |
| 1158 | dbi_result result; |
| 1159 | struct gsm_sms *sms; |
| 1160 | |
| 1161 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1162 | "SELECT SMS.* " |
| 1163 | "FROM SMS JOIN Subscriber ON " |
| 1164 | "SMS.receiver_id = Subscriber.id " |
| 1165 | "WHERE SMS.receiver_id = %llu AND SMS.sent IS NULL " |
| 1166 | "AND Subscriber.lac > 0 " |
| 1167 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1168 | subscr->id); |
| 1169 | if (!result) |
| 1170 | return NULL; |
| 1171 | |
| 1172 | if (!dbi_result_next_row(result)) { |
| 1173 | dbi_result_free(result); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | sms = sms_from_result(subscr->net, result); |
| 1178 | |
| 1179 | dbi_result_free(result); |
| 1180 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1181 | return sms; |
| 1182 | } |
| 1183 | |
| 1184 | /* mark a given SMS as read */ |
| 1185 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 1186 | { |
| 1187 | dbi_result result; |
| 1188 | |
| 1189 | result = dbi_conn_queryf(conn, |
| 1190 | "UPDATE SMS " |
| 1191 | "SET sent = datetime('now') " |
| 1192 | "WHERE id = %llu", sms->id); |
| 1193 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1194 | LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1195 | return 1; |
| 1196 | } |
| 1197 | |
| 1198 | dbi_result_free(result); |
| 1199 | return 0; |
| 1200 | } |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1201 | |
| 1202 | /* increase the number of attempted deliveries */ |
| 1203 | int db_sms_inc_deliver_attempts(struct gsm_sms *sms) |
| 1204 | { |
| 1205 | dbi_result result; |
| 1206 | |
| 1207 | result = dbi_conn_queryf(conn, |
| 1208 | "UPDATE SMS " |
| 1209 | "SET deliver_attempts = deliver_attempts + 1 " |
| 1210 | "WHERE id = %llu", sms->id); |
| 1211 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1212 | LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for " |
| 1213 | "SMS %llu.\n", sms->id); |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1214 | return 1; |
| 1215 | } |
| 1216 | |
| 1217 | dbi_result_free(result); |
| 1218 | return 0; |
| 1219 | } |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1220 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 1221 | int db_apdu_blob_store(struct gsm_subscriber *subscr, |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1222 | u_int8_t apdu_id_flags, u_int8_t len, |
| 1223 | u_int8_t *apdu) |
| 1224 | { |
| 1225 | dbi_result result; |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 1226 | unsigned char *q_apdu; |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1227 | |
| 1228 | dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu); |
| 1229 | |
| 1230 | result = dbi_conn_queryf(conn, |
| 1231 | "INSERT INTO ApduBlobs " |
| 1232 | "(created,subscriber_id,apdu_id_flags,apdu) VALUES " |
| 1233 | "(datetime('now'),%llu,%u,%s)", |
| 1234 | subscr->id, apdu_id_flags, q_apdu); |
| 1235 | |
| 1236 | free(q_apdu); |
| 1237 | |
| 1238 | if (!result) |
| 1239 | return -EIO; |
| 1240 | |
| 1241 | dbi_result_free(result); |
| 1242 | return 0; |
| 1243 | } |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1244 | |
| 1245 | int db_store_counter(struct counter *ctr) |
| 1246 | { |
| 1247 | dbi_result result; |
| 1248 | char *q_name; |
| 1249 | |
| 1250 | dbi_conn_quote_string_copy(conn, ctr->name, &q_name); |
| 1251 | |
| 1252 | result = dbi_conn_queryf(conn, |
| 1253 | "INSERT INTO Counters " |
| 1254 | "(timestamp,name,value) VALUES " |
| 1255 | "(datetime('now'),%s,%lu)", q_name, ctr->value); |
| 1256 | |
| 1257 | free(q_name); |
| 1258 | |
| 1259 | if (!result) |
| 1260 | return -EIO; |
| 1261 | |
| 1262 | dbi_result_free(result); |
| 1263 | return 0; |
| 1264 | } |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1265 | |
| 1266 | static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num, |
| 1267 | char *q_prefix) |
| 1268 | { |
| 1269 | dbi_result result; |
| 1270 | char *q_name; |
| 1271 | |
| 1272 | dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name, |
| 1273 | &q_name); |
| 1274 | |
| 1275 | result = dbi_conn_queryf(conn, |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1276 | "Insert INTO RateCounters " |
Harald Welte | 805eb48 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 1277 | "(timestamp,name,idx,value) VALUES " |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1278 | "(datetime('now'),%s.%s,%u,%"PRIu64")", |
| 1279 | q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current); |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1280 | |
| 1281 | free(q_name); |
| 1282 | |
| 1283 | if (!result) |
| 1284 | return -EIO; |
| 1285 | |
| 1286 | dbi_result_free(result); |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | int db_store_rate_ctr_group(struct rate_ctr_group *ctrg) |
| 1291 | { |
| 1292 | unsigned int i; |
| 1293 | char *q_prefix; |
| 1294 | |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1295 | dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix); |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1296 | |
| 1297 | for (i = 0; i < ctrg->desc->num_ctr; i++) |
| 1298 | db_store_rate_ctr(ctrg, i, q_prefix); |
| 1299 | |
| 1300 | free(q_prefix); |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |