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 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 255 | // FIXME handle extension |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 256 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 257 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Jan Luebbe | 3444ddb | 2008-12-28 12:37:15 +0000 | [diff] [blame] | 258 | 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] | 259 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, |
| 260 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 261 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 262 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 265 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 266 | dbi_result result; |
| 267 | result = dbi_conn_queryf(conn, |
| 268 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 269 | "SET updated = datetime('now'), " |
| 270 | "tmsi = %s, " |
| 271 | "lac = %i, " |
| 272 | "authorized = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 273 | "WHERE imsi = %s ", |
Jan Luebbe | 6e2e545 | 2008-12-27 16:47:55 +0000 | [diff] [blame] | 274 | subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 275 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 276 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 277 | if (result==NULL) { |
| 278 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 279 | return 1; |
| 280 | } |
| 281 | dbi_result_free(result); |
| 282 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 285 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 286 | dbi_result result=NULL; |
| 287 | char* tmsi_quoted; |
| 288 | for (;;) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 289 | sprintf(subscriber->tmsi, "%i", rand()); |
| 290 | dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 291 | result = dbi_conn_queryf(conn, |
| 292 | "SELECT * FROM Subscriber " |
| 293 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 294 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 295 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 296 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 297 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 298 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 299 | return 1; |
| 300 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 301 | if (dbi_result_get_numrows(result)){ |
| 302 | dbi_result_free(result); |
| 303 | continue; |
| 304 | } |
| 305 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 306 | dbi_result_free(result); |
| 307 | 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] | 308 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 309 | } |
| 310 | dbi_result_free(result); |
| 311 | } |
| 312 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 315 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
| 316 | u_int64_t equipment_id, watch_id; |
| 317 | dbi_result result; |
| 318 | |
| 319 | result = dbi_conn_queryf(conn, |
| 320 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 321 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 322 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 323 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 324 | imei |
| 325 | ); |
| 326 | if (result==NULL) { |
| 327 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 328 | return 1; |
| 329 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 330 | equipment_id = 0; |
| 331 | if (dbi_result_get_numrows_affected(result)) { |
| 332 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 333 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 334 | dbi_result_free(result); |
| 335 | if (equipment_id) { |
| 336 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 337 | } |
| 338 | else { |
| 339 | result = dbi_conn_queryf(conn, |
| 340 | "SELECT id FROM Equipment " |
| 341 | "WHERE imei = %s ", |
| 342 | imei |
| 343 | ); |
| 344 | if (result==NULL) { |
| 345 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 346 | return 1; |
| 347 | } |
| 348 | if (!dbi_result_next_row(result)) { |
| 349 | printf("DB: Failed to find the Equipment.\n"); |
| 350 | dbi_result_free(result); |
| 351 | return 1; |
| 352 | } |
| 353 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 354 | dbi_result_free(result); |
| 355 | } |
| 356 | |
| 357 | result = dbi_conn_queryf(conn, |
| 358 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 359 | "(subscriber_id, equipment_id, created, updated) " |
| 360 | "VALUES " |
| 361 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 362 | subscriber->id, equipment_id |
| 363 | ); |
| 364 | if (result==NULL) { |
| 365 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 366 | return 1; |
| 367 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 368 | watch_id = 0; |
| 369 | if (dbi_result_get_numrows_affected(result)) { |
| 370 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 371 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 372 | dbi_result_free(result); |
| 373 | if (watch_id) { |
| 374 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 375 | } |
| 376 | else { |
| 377 | result = dbi_conn_queryf(conn, |
| 378 | "UPDATE EquipmentWatch " |
| 379 | "SET updated = datetime('now') " |
| 380 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 381 | subscriber->id, equipment_id |
| 382 | ); |
| 383 | if (result==NULL) { |
| 384 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 385 | return 1; |
| 386 | } |
| 387 | dbi_result_free(result); |
| 388 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 394 | /* store an [unsent] SMS to the database */ |
| 395 | int db_sms_store(struct gsm_sms *sms) |
| 396 | { |
| 397 | dbi_result result; |
| 398 | char *q_text; |
| 399 | |
| 400 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
| 401 | result = dbi_conn_queryf(conn, |
| 402 | "INSERT INTO SMS " |
| 403 | "(created,sender_id,receiver_id,header,text) VALUES " |
| 404 | "(datetime('now'),%llu,%llu,%s,%s)", |
| 405 | sms->sender->id, |
| 406 | sms->receiver ? sms->receiver->id : 0, |
| 407 | NULL, q_text); |
| 408 | free(q_text); |
| 409 | |
| 410 | if (!result) |
| 411 | return -EIO; |
| 412 | |
| 413 | dbi_result_free(result); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 418 | struct gsm_sms *db_sms_get_unsent(int min_id) |
| 419 | { |
| 420 | dbi_result result; |
| 421 | struct gsm_sms *sms = malloc(sizeof(*sms)); |
| 422 | |
| 423 | if (!sms) { |
| 424 | free(sms); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | result = dbi_conn_queryf(conn, |
| 429 | "SELECT * FROM SMS " |
| 430 | "WHERE id >= %llu ORDER BY id", min_id); |
| 431 | if (!result) { |
| 432 | free(sms); |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | /* FIXME: fill gsm_sms from database */ |
| 437 | |
| 438 | dbi_result_free(result); |
| 439 | return sms; |
| 440 | } |
| 441 | |
| 442 | /* mark a given SMS as read */ |
| 443 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 444 | { |
| 445 | dbi_result result; |
| 446 | |
| 447 | result = dbi_conn_queryf(conn, |
| 448 | "UPDATE SMS " |
| 449 | "SET sent = datetime('now') " |
| 450 | "WHERE id = %llu", sms->id); |
| 451 | if (!result) { |
| 452 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | dbi_result_free(result); |
| 457 | return 0; |
| 458 | } |