blob: 1c412c2f2de5277fd445848732d8396835fe86f4 [file] [log] [blame]
Jan Luebbefaaa49c2008-12-27 01:07:07 +00001/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
2 * All Rights Reserved
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20#include <openbsc/db.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000021
22#include <stdio.h>
23#include <dbi/dbi.h>
24
25dbi_conn conn;
26
27void db__error_func(dbi_conn conn, void* data) {
28 const char* msg;
29 dbi_conn_error(conn, &msg);
30 printf("DBI: %s\n", msg);
31}
32
33int db_init() {
34 dbi_initialize(NULL);
35 conn = dbi_conn_new("sqlite3");
36
37 dbi_conn_error_handler( conn, db__error_func, NULL );
38
39 /* MySQL
40 dbi_conn_set_option(conn, "host", "localhost");
41 dbi_conn_set_option(conn, "username", "your_name");
42 dbi_conn_set_option(conn, "password", "your_password");
43 dbi_conn_set_option(conn, "dbname", "your_dbname");
44 dbi_conn_set_option(conn, "encoding", "UTF-8");
45 */
46
47 /* SqLite 3 */
48 dbi_conn_set_option(conn, "sqlite3_dbdir", "/tmp");
49 dbi_conn_set_option(conn, "dbname", "hlr.sqlite3");
50
51 if (dbi_conn_connect(conn) < 0) {
52 return 1;
53 }
54
55 return 0;
56}
57
58int db_prepare() {
59 dbi_result result;
60 result = dbi_conn_query(conn,
61 "CREATE TABLE IF NOT EXISTS Subscriber ("
62 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
63 "imsi INTEGER UNIQUE NOT NULL, "
64 "tmsi INTEGER UNIQUE, "
65 "extension TEXT UNIQUE, "
66 "lac INTEGER"
67 ")"
68 );
69 if (result==NULL) {
70 printf("DB: Failed to create Subscriber table.\n");
71 return 1;
72 }
73 dbi_result_free(result);
74 result = dbi_conn_query(conn,
75 "CREATE TABLE IF NOT EXISTS Equipment ("
76 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
77 "imei INTEGER UNIQUE NOT NULL"
78 ")"
79 );
80 if (result==NULL) {
81 printf("DB: Failed to create Equipment table.\n");
82 return 1;
83 }
84 dbi_result_free(result);
85 return 0;
86}
87
88int db_fini() {
89 dbi_conn_close(conn);
90 dbi_shutdown();
91}
92
93int db_insert_imei(uint64_t imei) {
94 dbi_result result;
95 result = dbi_conn_queryf(conn,
96 "INSERT OR IGNORE INTO Equipment "
97 "(imei) "
98 "VALUES "
99 "(%llu) ",
100 imei
101 );
102 if (result==NULL) {
103 printf("DB: Failed to create Equipment by IMEI.\n");
104 return 1;
105 }
106 dbi_result_free(result);
107 return 0;
108}
109
110int db_insert_imsi(uint64_t imsi) {
111 dbi_result result;
112 result = dbi_conn_queryf(conn,
113 "INSERT OR IGNORE INTO Subscriber "
114 "(imsi) "
115 "VALUES "
116 "(%llu) ",
117 imsi
118 );
119 if (result==NULL) {
120 printf("DB: Failed to create Subscriber by IMSI.\n");
121 return 1;
122 }
123 dbi_result_free(result);
124 return 0;
125}
126
127int db_imsi_set_tmsi(uint64_t imsi, uint64_t tmsi) {
128 if (db_insert_imsi(imsi)) {
129 return 1;
130 }
131 dbi_result result;
132 result = dbi_conn_queryf(conn,
133 "UPDATE Subscriber "
134 "SET tmsi = %llu "
135 "WHERE imsi = %llu ",
136 tmsi, imsi
137 );
138 if (result==NULL) {
139 printf("DB: Failed to update Subscriber with TMSI by IMSI.\n");
140 return 1;
141 }
142 dbi_result_free(result);
143 return 0;
144}
145
146int db_imsi_set_lac(uint64_t imsi, uint16_t lac) {
147 if (db_insert_imsi(imsi)) {
148 return 1;
149 }
150 dbi_result result;
151 result = dbi_conn_queryf(conn,
152 "UPDATE Subscriber "
153 "SET lac = %u "
154 "WHERE imsi = %llu ",
155 lac, imsi
156 );
157 if (result==NULL) {
158 printf("DB: Failed to update Subscriber with LAC by IMSI.\n");
159 return 1;
160 }
161 dbi_result_free(result);
162 return 0;
163}
164
165int db__parse_subscriber(dbi_result result, db_subscriber* subscriber) {
166 if (!dbi_result_first_row(result)) {
167 printf("DB: Failed to find Subscriber.\n");
168 return 1;
169 }
170 subscriber->imsi = dbi_result_get_ulonglong(result, "imsi");
171 subscriber->tmsi = dbi_result_get_ulonglong(result, "tmsi");
172 // FIXME handle extension
173 subscriber->lac = dbi_result_get_uint(result, "lac");
174 printf("DB: Subscriber: IMSI %llu, TMSI %llu, LAC %hu\n", subscriber->imsi, subscriber->tmsi, subscriber->lac);
175 return 0;
176}
177
178int db_imsi_get_subscriber(uint64_t imsi, db_subscriber* subscriber) {
179 dbi_result result;
180 result = dbi_conn_queryf(conn,
181 "SELECT * FROM Subscriber "
182 "WHERE imsi = %llu ",
183 imsi
184 );
185 if (result==NULL) {
186 printf("DB: Failed to find Subscriber by IMSI.\n");
187 return 1;
188 }
189 if (db__parse_subscriber(result, subscriber)) {
190 printf("DB: Failed to parse Subscriber.\n");
191 dbi_result_free(result);
192 return 1;
193 }
194 dbi_result_free(result);
195 return 0;
196}
197
198int db_tmsi_get_subscriber(uint64_t tmsi, db_subscriber* subscriber) {
199 dbi_result result;
200 result = dbi_conn_queryf(conn,
201 "SELECT * FROM Subscriber "
202 "WHERE tmsi = %llu ",
203 tmsi
204 );
205 if (result==NULL) {
206 printf("DB: Failed to find Subscriber by TMSI.\n");
207 return 1;
208 }
209 if (db__parse_subscriber(result, subscriber)) {
210 printf("DB: Failed to parse Subscriber.\n");
211 dbi_result_free(result);
212 return 1;
213 }
214 dbi_result_free(result);
215 return 0;
216}
217