2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This file implements restrictions entity handler methods.
20 * @file table-restrictions.c
24 #include "db-internal.h"
25 #include "table-restrictions.h"
27 /* DELETE statements */
28 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
29 "WHERE binpath=? AND iftype=? AND imsi = ?"
31 /* SELECT statements */
32 #define SELECT_RESTRICTIONS "SELECT binpath, rcv_limit, " \
33 "send_limit, iftype, rst_state, roaming, ifname, imsi, " \
34 "rcv_warn_limit, send_warn_limit, restriction_id FROM restrictions"
36 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, rcv_limit, " \
37 "send_limit, iftype, rst_state, roaming, ifname, imsi, " \
38 "rcv_warn_limit, send_warn_limit, restriction_id " \
39 "FROM restrictions INDEXED BY restrictions_index " \
42 #define SELECT_RESTRICTION_STATE "SELECT rst_state " \
43 "FROM restrictions INDEXED BY restrictions_index " \
44 "WHERE binpath = ? AND iftype = ?"
46 #define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \
47 "FROM restrictions INDEXED BY restrictions_index " \
48 "WHERE binpath = ? AND iftype = ? AND imsi = ?"
50 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
51 "WHERE binpath = ? AND iftype = ? AND imsi = ? AND send_limit=? " \
52 "AND rcv_limit=? AND rcv_warn_limit=? AND send_warn_limit=? " \
53 "AND rst_state=? AND roaming=? AND ifname=?"
55 /* UPDATE statement */
56 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
57 "SET binpath=?, rcv_limit=?, send_limit=?, iftype=?, rst_state=?, " \
58 "roaming=?, ifname=?, imsi=?, rcv_warn_limit=?, send_warn_limit=? " \
59 "WHERE restriction_id=?"
61 /* INSERT statement */
62 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
63 "(binpath, rcv_limit, send_limit, iftype, rst_state, " \
64 "roaming, ifname, imsi, rcv_warn_limit, send_warn_limit) " \
65 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
67 static void __finalize_delete(void);
69 #define PREPARE_DELETE(stm, query) do { \
70 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
71 if (rc != SQLITE_OK) { \
73 __finalize_delete(); \
74 STC_LOGE("Failed to prepare \"%s\"query" \
80 static void __finalize_select(void);
82 #define PREPARE_SELECT(stm, query) do { \
83 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
84 if (rc != SQLITE_OK) { \
86 __finalize_select(); \
87 STC_LOGE("Failed to prepare \"%s\"query" \
93 static void __finalize_update(void);
95 #define PREPARE_UPDATE(stm, query) do { \
96 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
97 if (rc != SQLITE_OK) { \
99 __finalize_update(); \
100 STC_LOGE("Failed to prepare \"%s\"query" \
106 static void __finalize_insert(void);
108 #define PREPARE_INSERT(stm, query) do { \
109 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
110 if (rc != SQLITE_OK) { \
112 __finalize_insert(); \
113 STC_LOGE("Failed to prepare \"%s\"query" \
119 #define FINALIZE(stm) do { \
121 sqlite3_finalize(stm); \
126 /* DELETE statements */
127 static sqlite3_stmt *delete_restrictions;
129 /* SELECT statements */
130 static sqlite3_stmt *select_restriction;
131 static sqlite3_stmt *select_restriction_per_app;
132 static sqlite3_stmt *select_restriction_state;
133 static sqlite3_stmt *select_restriction_state_imsi;
134 static sqlite3_stmt *select_restriction_id;
136 /* REPLACE statements */
137 static sqlite3_stmt *update_net_restrictions;
139 /* INSERT statements */
140 static sqlite3_stmt *insert_net_restrictions;
142 static int __prepare_delete(sqlite3 *db)
144 __STC_LOG_FUNC_ENTER__;
146 static int initialized;
149 __STC_LOG_FUNC_EXIT__;
153 PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
156 __STC_LOG_FUNC_EXIT__;
160 static void __finalize_delete(void)
162 __STC_LOG_FUNC_ENTER__;
164 FINALIZE(delete_restrictions);
166 __STC_LOG_FUNC_EXIT__;
169 static int __prepare_select(sqlite3 *db)
171 __STC_LOG_FUNC_ENTER__;
173 static int initialized;
176 __STC_LOG_FUNC_EXIT__;
180 PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
181 PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
182 PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE);
183 PREPARE_SELECT(select_restriction_state_imsi, SELECT_RESTRICTION_STATE_IMSI);
184 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
187 __STC_LOG_FUNC_EXIT__;
191 static void __finalize_select(void)
193 __STC_LOG_FUNC_ENTER__;
195 FINALIZE(select_restriction);
196 FINALIZE(select_restriction_per_app);
197 FINALIZE(select_restriction_state);
198 FINALIZE(select_restriction_state_imsi);
199 FINALIZE(select_restriction_id);
201 __STC_LOG_FUNC_EXIT__;
204 static int __prepare_replace(sqlite3 *db)
206 __STC_LOG_FUNC_ENTER__;
208 static int initialized;
211 __STC_LOG_FUNC_EXIT__;
215 PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
218 __STC_LOG_FUNC_EXIT__;
222 static void __finalize_update(void)
224 __STC_LOG_FUNC_ENTER__;
226 FINALIZE(update_net_restrictions);
228 __STC_LOG_FUNC_EXIT__;
231 static int __prepare_insert(sqlite3 *db)
233 __STC_LOG_FUNC_ENTER__;
235 static int initialized;
238 __STC_LOG_FUNC_EXIT__;
242 PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
245 __STC_LOG_FUNC_EXIT__;
249 static void __finalize_insert(void)
251 __STC_LOG_FUNC_ENTER__;
253 FINALIZE(insert_net_restrictions);
255 __STC_LOG_FUNC_EXIT__;
258 stc_error_e table_restrictions_per_app(const gchar* app_id,
259 const table_restrictions_info_cb restriction_cb,
262 __STC_LOG_FUNC_ENTER__;
263 table_restrictions_info data;
265 stc_error_e error_code = STC_ERROR_NONE;
266 sqlite3_stmt *stmt = select_restriction_per_app;
269 __STC_LOG_FUNC_EXIT__;
270 return STC_ERROR_DB_FAILED;
273 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
275 data.app_id = (char *)app_id;
278 rc = sqlite3_step(stmt);
280 memset(&data, 0, sizeof(data));
286 data.app_id = (char *)sqlite3_column_text(stmt, 0);
287 data.rcv_limit = sqlite3_column_int64(stmt, 1);
288 data.send_limit = sqlite3_column_int64(stmt, 2);
289 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3);
291 (stc_restriction_state_e)sqlite3_column_int(stmt, 4);
292 data.roaming = sqlite3_column_int(stmt, 5);
293 data.ifname = (char *)sqlite3_column_text(stmt, 6);
294 data.imsi = (char *)sqlite3_column_text(stmt, 7);
295 data.rcv_warn_limit = sqlite3_column_int64(stmt, 8);
296 data.send_warn_limit = sqlite3_column_int64(stmt, 9);
297 data.restriction_id = sqlite3_column_int64(stmt, 10);
299 if (restriction_cb(&data, user_data) == STC_CANCEL)
304 STC_LOGE("Failed to enumerate restrictions: %s\n",
305 sqlite3_errmsg(stc_db_get_database()));
307 __STC_LOG_FUNC_EXIT__;
308 error_code = STC_ERROR_DB_FAILED;
310 } while (rc == SQLITE_ROW);
314 __STC_LOG_FUNC_EXIT__;
318 stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
321 __STC_LOG_FUNC_ENTER__;
322 table_restrictions_info data;
324 stc_error_e error_code = STC_ERROR_NONE;
325 sqlite3_stmt *stmt = select_restriction;
328 rc = sqlite3_step(stmt);
330 memset(&data, 0, sizeof(data));
336 data.app_id = (char *)sqlite3_column_text(stmt, 0);
337 data.rcv_limit = sqlite3_column_int64(stmt, 1);
338 data.send_limit = sqlite3_column_int64(stmt, 2);
339 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3);
341 (stc_restriction_state_e)sqlite3_column_int(stmt, 4);
342 data.roaming = sqlite3_column_int(stmt, 5);
343 data.ifname = (char *)sqlite3_column_text(stmt, 6);
344 data.imsi = (char *)sqlite3_column_text(stmt, 7);
345 data.rcv_warn_limit = sqlite3_column_int64(stmt, 8);
346 data.send_warn_limit = sqlite3_column_int64(stmt, 9);
347 data.restriction_id = sqlite3_column_int64(stmt, 10);
349 if (restriction_cb(&data, user_data) == STC_CANCEL)
354 STC_LOGE("Failed to enumerate restrictions: %s\n",
355 sqlite3_errmsg(stc_db_get_database()));
357 __STC_LOG_FUNC_EXIT__;
358 error_code = STC_ERROR_DB_FAILED;
360 } while (rc == SQLITE_ROW);
363 __STC_LOG_FUNC_EXIT__;
367 stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id,
368 stc_iface_type_e iftype,
369 const char *imsi_hash,
370 stc_restriction_state_e *state)
372 __STC_LOG_FUNC_ENTER__;
373 int error_code = STC_ERROR_NONE;
378 STC_LOGE("Please provide valid argument!");
379 __STC_LOG_FUNC_EXIT__;
380 return STC_ERROR_INVALID_PARAMETER;
383 *state = STC_RESTRICTION_UNKNOWN;
384 sqlite3_reset(select_restriction_state_imsi);
385 sqlite3_reset(select_restriction_state);
387 if (imsi_hash == NULL) {
389 DB_ACTION(sqlite3_bind_text(select_restriction_state, 1,
390 app_id ? app_id : "", -1,
392 DB_ACTION(sqlite3_bind_int(select_restriction_state, 2,
394 ret = sqlite3_step(select_restriction_state);
397 DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 1,
398 app_id ? app_id : "", -1,
400 DB_ACTION(sqlite3_bind_int(select_restriction_state_imsi, 2,
402 DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 3,
403 imsi_hash, -1, SQLITE_STATIC));
404 ret = sqlite3_step(select_restriction_state_imsi);
412 *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_imsi, 0);
414 *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0);
418 STC_LOGE("Can't perform sql query: %s \n%s",
419 SELECT_RESTRICTION_STATE,
420 sqlite3_errmsg(stc_db_get_database()));
421 error_code = STC_ERROR_DB_FAILED;
425 sqlite3_reset(select_restriction_state);
426 sqlite3_reset(select_restriction_state_imsi);
430 stc_error_e table_restrictions_get_restriction_state(const char *app_id,
431 stc_iface_type_e iftype,
432 stc_restriction_state_e *state)
434 __STC_LOG_FUNC_ENTER__;
435 __STC_LOG_FUNC_EXIT__;
436 return table_restrictions_get_restriction_state_imsi(app_id, iftype,
440 stc_error_e table_restrictions_delete(const char *app_id,
441 const stc_iface_type_e iftype,
444 stc_error_e error_code = STC_ERROR_NONE;
445 sqlite3_stmt *stmt = delete_restrictions;
447 STC_LOGD("app_id [%s], iftype [%d], imsi [%s]",
448 app_id, iftype, imsi);
450 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
451 -1, SQLITE_TRANSIENT));
452 DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
453 DB_ACTION(sqlite3_bind_text(stmt, 3, imsi ? imsi : "", -1,
456 if (sqlite3_step(stmt) != SQLITE_DONE) {
457 STC_LOGE("Failed to remove restrictions by network interface %s\n",
458 sqlite3_errmsg(stc_db_get_database()));
459 error_code = STC_ERROR_DB_FAILED;
463 STC_LOGD("Restriction deleted for app_id [%s]", app_id);
471 stc_error_e __get_restriction_id(table_restrictions_info *info)
473 __STC_LOG_FUNC_ENTER__;
475 stc_error_e error_code = STC_ERROR_NONE;
476 sqlite3_stmt *stmt = select_restriction_id;
478 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
479 -1, SQLITE_TRANSIENT));
480 DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
481 DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "",
482 -1, SQLITE_TRANSIENT));
483 DB_ACTION(sqlite3_bind_int64(stmt, 4, info->send_limit));
484 DB_ACTION(sqlite3_bind_int64(stmt, 5, info->rcv_limit));
485 DB_ACTION(sqlite3_bind_int64(stmt, 6, info->rcv_warn_limit));
486 DB_ACTION(sqlite3_bind_int64(stmt, 7, info->send_warn_limit));
488 DB_ACTION(sqlite3_bind_int(stmt, 8, info->rst_state));
489 DB_ACTION(sqlite3_bind_int(stmt, 9, info->roaming));
490 DB_ACTION(sqlite3_bind_text(stmt, 10, info->ifname ? info->ifname : "",
491 -1, SQLITE_TRANSIENT));
493 rc = sqlite3_step(stmt);
499 info->restriction_id = sqlite3_column_int64(stmt, 0);
500 STC_LOGD("restriction id [%llu]", info->restriction_id);
504 STC_LOGE("Failed to get restriction id: %s\n",
505 sqlite3_errmsg(stc_db_get_database()));
510 __STC_LOG_FUNC_EXIT__;
514 stc_error_e table_restrictions_update(table_restrictions_info *info)
516 stc_error_e error_code = STC_ERROR_NONE;
517 sqlite3_stmt *stmt = insert_net_restrictions;
520 error_code = STC_ERROR_INVALID_PARAMETER;
524 if (info->restriction_id)
525 stmt = update_net_restrictions;
527 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
528 -1, SQLITE_TRANSIENT));
529 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->rcv_limit));
530 DB_ACTION(sqlite3_bind_int64(stmt, 3, info->send_limit));
531 DB_ACTION(sqlite3_bind_int(stmt, 4, info->iftype));
532 DB_ACTION(sqlite3_bind_int(stmt, 5, info->rst_state));
533 DB_ACTION(sqlite3_bind_int(stmt, 6, info->roaming));
534 DB_ACTION(sqlite3_bind_text(stmt, 7, info->ifname ? info->ifname : "",
535 -1, SQLITE_TRANSIENT));
536 DB_ACTION(sqlite3_bind_text(stmt, 8, info->imsi ? info->imsi : "",
537 -1, SQLITE_TRANSIENT));
538 DB_ACTION(sqlite3_bind_int64(stmt, 9, info->rcv_warn_limit));
539 DB_ACTION(sqlite3_bind_int64(stmt, 10, info->send_warn_limit));
541 if (info->restriction_id)
542 DB_ACTION(sqlite3_bind_int64(stmt, 11, info->restriction_id));
544 if (sqlite3_step(stmt) != SQLITE_DONE) {
545 STC_LOGE("Failed to set network restriction: %s\n",
546 sqlite3_errmsg(stc_db_get_database()));
547 error_code = STC_ERROR_DB_FAILED;
551 if (info->restriction_id) {
552 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
554 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
555 __get_restriction_id(info);
563 stc_error_e table_restrictions_prepare(sqlite3 *db)
565 __STC_LOG_FUNC_ENTER__;
567 stc_error_e error_code = STC_ERROR_NONE;
570 __STC_LOG_FUNC_EXIT__;
571 return STC_ERROR_FAIL;
574 DB_ACTION(__prepare_delete(db));
575 DB_ACTION(__prepare_select(db));
576 DB_ACTION(__prepare_replace(db));
577 DB_ACTION(__prepare_insert(db));
581 __STC_LOG_FUNC_EXIT__;
585 void table_restrictions_finalize(void)
587 __STC_LOG_FUNC_ENTER__;
592 __STC_LOG_FUNC_EXIT__;