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