forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.c
780 lines (653 loc) · 19.5 KB
/
database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Database routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
sqlite3 *db;
bool database = false;
bool DBdeleteoldqueries = false;
long int lastdbindex = 0;
long int lastDBimportedtimestamp = 0;
pthread_mutex_t dblock;
// TABLE ftl
enum { DB_VERSION, DB_LASTTIMESTAMP, DB_FIRSTCOUNTERTIMESTAMP };
// TABLE counters
enum { DB_TOTALQUERIES, DB_BLOCKEDQUERIES };
bool db_set_counter(unsigned int ID, int value);
bool db_set_FTL_property(unsigned int ID, int value);
int db_get_FTL_property(unsigned int ID);
void check_database(int rc)
{
// We will retry if the database is busy at the moment
// However, we won't retry if any other error happened
// and - instead - disable the database functionality
// altogether in FTL (setting database to false)
if(rc != SQLITE_OK &&
rc != SQLITE_DONE &&
rc != SQLITE_ROW &&
rc != SQLITE_BUSY)
{
database = false;
}
}
void dbclose(void)
{
int rc = sqlite3_close(db);
// Report any error
if( rc )
logg("dbclose() - SQL error (%i): %s", rc, sqlite3_errmsg(db));
// Unlock mutex on the database
pthread_mutex_unlock(&dblock);
}
double get_db_filesize(void)
{
struct stat st;
if(stat(FTLfiles.db, &st) != 0)
{
// stat() failed (maybe the DB file does not exist?)
return 0;
}
return 1e-6*st.st_size;
}
bool dbopen(void)
{
pthread_mutex_lock(&dblock);
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE, NULL);
if( rc ){
logg("dbopen() - SQL error (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return false;
}
return true;
}
bool dbquery(const char *format, ...)
{
char *zErrMsg = NULL;
va_list args;
va_start(args, format);
char *query = sqlite3_vmprintf(format, args);
va_end(args);
if(query == NULL)
{
logg("Memory allocation failed in dbquery()");
return false;
}
int rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
logg("dbquery(%s) - SQL error (%i): %s", query, rc, zErrMsg);
sqlite3_free(zErrMsg);
check_database(rc);
return false;
}
sqlite3_free(query);
return true;
}
bool create_counter_table(void)
{
bool ret;
// Create FTL table in the database (holds properties like database version, etc.)
ret = dbquery("CREATE TABLE counters ( id INTEGER PRIMARY KEY NOT NULL, value INTEGER NOT NULL );");
if(!ret){ dbclose(); return false; }
// ID 0 = total queries
ret = db_set_counter(DB_TOTALQUERIES, 0);
if(!ret){ dbclose(); return false; }
// ID 1 = total blocked queries
ret = db_set_counter(DB_BLOCKEDQUERIES, 0);
if(!ret){ dbclose(); return false; }
// Time stamp of creation of the counters database
ret = db_set_FTL_property(DB_FIRSTCOUNTERTIMESTAMP, time(NULL));
if(!ret){ dbclose(); return false; }
// Update database version to 2
ret = db_set_FTL_property(DB_VERSION, 2);
if(!ret){ dbclose(); return false; }
return true;
}
bool db_create(void)
{
bool ret;
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if( rc ){
logg("db_create() - SQL error (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return false;
}
// Create Queries table in the database
ret = dbquery("CREATE TABLE queries ( id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, type INTEGER NOT NULL, status INTEGER NOT NULL, domain TEXT NOT NULL, client TEXT NOT NULL, forward TEXT );");
if(!ret){ dbclose(); return false; }
// Add an index on the timestamps (not a unique index!)
ret = dbquery("CREATE INDEX idx_queries_timestamps ON queries (timestamp);");
if(!ret){ dbclose(); return false; }
// Create FTL table in the database (holds properties like database version, etc.)
ret = dbquery("CREATE TABLE ftl ( id INTEGER PRIMARY KEY NOT NULL, value BLOB NOT NULL );");
if(!ret){ dbclose(); return false; }
// DB version 2
ret = dbquery("INSERT INTO ftl (ID,VALUE) VALUES(%i,2);", DB_VERSION);
if(!ret){ dbclose(); return false; }
// Most recent timestamp initialized to 00:00 1 Jan 1970
ret = dbquery("INSERT INTO ftl (ID,VALUE) VALUES(%i,0);", DB_LASTTIMESTAMP);
if(!ret){ dbclose(); return false; }
// Create counter table
if(!create_counter_table())
return false;
return true;
}
void db_init(void)
{
// First check if the user doesn't want to use the database and set an
// empty string as file name in FTL's config file
if(FTLfiles.db == NULL || strlen(FTLfiles.db) == 0)
{
database = false;
return;
}
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE, NULL);
if( rc ){
logg("db_init() - Cannot open database (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
logg("Creating new (empty) database");
if (!db_create())
{
logg("Database not available");
database = false;
return;
}
}
// Test DB version and see if we need to upgrade the database file
int dbversion = db_get_FTL_property(DB_VERSION);
if(dbversion < 1)
{
logg("Database version incorrect, database not available");
database = false;
return;
}
else if(dbversion < 2)
{
// Database is still in version 1
// Update to version 2 and create counters table
if (!create_counter_table())
{
logg("Counter table not initialized, database not available");
database = false;
return;
}
}
// Close database to prevent having it opened all time
// we already closed the database when we returned earlier
sqlite3_close(db);
if (pthread_mutex_init(&dblock, NULL) != 0)
{
logg("FATAL: DB mutex init failed\n");
// Return failure
exit(EXIT_FAILURE);
}
logg("Database successfully initialized");
database = true;
}
int db_get_FTL_property(unsigned int ID)
{
int rc, ret = 0;
sqlite3_stmt* dbstmt;
char *querystring = NULL;
// Prepare SQL statement
ret = asprintf(&querystring, "SELECT VALUE FROM ftl WHERE id = %u;",ID);
if(querystring == NULL || ret < 0)
{
logg("Memory allocation failed in db_get_FTL_property, not saving query with ID = %u (%i)", ID, ret);
return false;
}
rc = sqlite3_prepare(db, querystring, -1, &dbstmt, NULL);
if( rc ){
logg("db_get_FTL_property() - SQL error prepare (%i): %s", rc, sqlite3_errmsg(db));
logg("Query: \"%s\"", querystring);
dbclose();
check_database(rc);
return -1;
}
free(querystring);
// Evaluate SQL statement
rc = sqlite3_step(dbstmt);
if( rc != SQLITE_ROW ){
logg("db_get_FTL_property() - SQL error step (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return -1;
}
int result = sqlite3_column_int(dbstmt, 0);
sqlite3_finalize(dbstmt);
return result;
}
bool db_set_FTL_property(unsigned int ID, int value)
{
return dbquery("INSERT OR REPLACE INTO ftl (id, value) VALUES ( %u, %i );", ID, value);
}
bool db_set_counter(unsigned int ID, int value)
{
return dbquery("INSERT OR REPLACE INTO counters (id, value) VALUES ( %u, %i );", ID, value);
}
bool db_update_counters(int total, int blocked)
{
if(!dbquery("UPDATE counters SET value = value + %i WHERE id = %i;", total, DB_TOTALQUERIES))
return false;
if(!dbquery("UPDATE counters SET value = value + %i WHERE id = %i;", blocked, DB_BLOCKEDQUERIES))
return false;
return true;
}
int number_of_queries_in_DB(void)
{
sqlite3_stmt* stmt;
// Count number of rows using the index timestamp is faster than select(*)
int rc = sqlite3_prepare_v2(db, "SELECT COUNT(timestamp) FROM queries", -1, &stmt, NULL);
if( rc ){
logg("number_of_queries_in_DB() - SQL error prepare (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return -1;
}
rc = sqlite3_step(stmt);
if( rc != SQLITE_ROW ){
logg("number_of_queries_in_DB() - SQL error step (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return -1;
}
int result = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
return result;
}
int get_number_of_queries_in_DB(void)
{
int result = -1;
if(!dbopen())
{
logg("Failed to open DB in get_number_of_queries_in_DB()");
return -2;
}
result = number_of_queries_in_DB();
// Close database
dbclose();
return result;
}
void save_to_DB(void)
{
// Don't save anything to the database if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Start database timer
if(debug) timer_start(DATABASE_WRITE_TIMER);
// Open database
if(!dbopen())
{
logg("save_to_DB() - failed to open DB");
return;
}
unsigned int saved = 0, saved_error = 0;
long int i;
sqlite3_stmt* stmt;
bool ret = dbquery("BEGIN TRANSACTION");
if(!ret)
{
logg("save_to_DB() - unable to begin transaction (%i): %s", ret, sqlite3_errmsg(db));
dbclose();
return;
}
int rc = sqlite3_prepare_v2(db, "INSERT INTO queries VALUES (NULL,?,?,?,?,?,?)", -1, &stmt, NULL);
if( rc )
{
logg("save_to_DB() - error in preparing SQL statement (%i): %s", ret, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return;
}
int total = 0, blocked = 0;
time_t currenttimestamp = time(NULL);
time_t newlasttimestamp = 0;
for(i = 0; i < counters.queries; i++)
{
validate_access("queries", i, true, __LINE__, __FUNCTION__, __FILE__);
if(queries[i].db)
{
// Skip, already saved in database
continue;
}
if(!queries[i].complete && queries[i].timestamp > currenttimestamp-2)
{
// Break if a brand new query (age < 2 seconds) is not yet completed
// giving it a chance to be stored next time
break;
}
// Memory checks
validate_access("queries", i, true, __LINE__, __FUNCTION__, __FILE__);
if(queries[i].privacylevel >= PRIVACY_MAXIMUM)
{
// Skip, we never store nor count queries recorded
// while have been in maximum privacy mode in the database
continue;
}
// TIMESTAMP
sqlite3_bind_int(stmt, 1, queries[i].timestamp);
// TYPE
sqlite3_bind_int(stmt, 2, queries[i].type);
// STATUS
sqlite3_bind_int(stmt, 3, queries[i].status);
// DOMAIN
char *domain = getDomainString(i);
sqlite3_bind_text(stmt, 4, domain, -1, SQLITE_TRANSIENT);
// CLIENT
char *client = getClientIPString(i);
sqlite3_bind_text(stmt, 5, client, -1, SQLITE_TRANSIENT);
// FORWARD
if(queries[i].status == QUERY_FORWARDED && queries[i].forwardID > -1)
{
validate_access("forwarded", queries[i].forwardID, true, __LINE__, __FUNCTION__, __FILE__);
sqlite3_bind_text(stmt, 6, forwarded[queries[i].forwardID].ip, -1, SQLITE_TRANSIENT);
}
else
{
sqlite3_bind_null(stmt, 6);
}
// Step and check if successful
rc = sqlite3_step(stmt);
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
if( rc != SQLITE_DONE ){
logg("save_to_DB() - SQL error (%i): %s", rc, sqlite3_errmsg(db));
saved_error++;
if(saved_error < 3)
{
continue;
}
else
{
logg("save_to_DB() - exiting due to too many errors");
break;
}
// Check this error message
check_database(rc);
}
saved++;
// Mark this query as saved in the database only if successful
queries[i].db = true;
// Total counter information (delta computation)
total++;
if(queries[i].status == QUERY_GRAVITY ||
queries[i].status == QUERY_BLACKLIST ||
queries[i].status == QUERY_WILDCARD ||
queries[i].status == QUERY_EXTERNAL_BLOCKED)
blocked++;
// Update lasttimestamp variable with timestamp of the latest stored query
if(queries[i].timestamp > newlasttimestamp)
newlasttimestamp = queries[i].timestamp;
}
// Finish prepared statement
ret = dbquery("END TRANSACTION");
int ret2 = sqlite3_finalize(stmt);
if(!ret || ret2 != SQLITE_OK){ dbclose(); return; }
// Store index for next loop interation round and update last time stamp
// in the database only if all queries have been saved successfully
if(saved_error == 0)
{
lastdbindex = i;
db_set_FTL_property(DB_LASTTIMESTAMP, newlasttimestamp);
}
// Update total counters in DB
if(!db_update_counters(total, blocked))
{
dbclose();
return;
}
// Close database
dbclose();
if(debug)
{
logg("Notice: Queries stored in DB: %u (took %.1f ms)", saved, timer_elapsed_msec(DATABASE_WRITE_TIMER));
if(saved_error > 0)
logg(" There are queries that have not been saved");
}
}
void delete_old_queries_in_DB(void)
{
// Open database
if(!dbopen())
{
logg("Failed to open DB in delete_old_queries_in_DB()");
return;
}
int timestamp = time(NULL) - config.maxDBdays * 86400;
if(!dbquery("DELETE FROM queries WHERE timestamp <= %i", timestamp))
{
dbclose();
logg("delete_old_queries_in_DB(): Deleting queries due to age of entries failed!");
database = true;
return;
}
// Get how many rows have been affected (deleted)
int affected = sqlite3_changes(db);
// Print final message only if there is a difference
if(debug || affected)
logg("Notice: Database size is %.2f MB, deleted %i rows", get_db_filesize(), affected);
// Close database
dbclose();
// Re-enable database actions
database = true;
}
int lastDBsave = 0;
void *DB_thread(void *val)
{
// Set thread name
prctl(PR_SET_NAME,"database",0,0,0);
// Save timestamp as we do not want to store immediately
// to the database
lastDBsave = time(NULL) - time(NULL)%config.DBinterval;
while(!killed && database)
{
if(time(NULL) - lastDBsave >= config.DBinterval)
{
// Update lastDBsave timer
lastDBsave = time(NULL) - time(NULL)%config.DBinterval;
// Lock FTL's data structure, since it is
// likely that it will be changed here
enable_thread_lock();
// Save data to database
save_to_DB();
// Release thread lock
disable_thread_lock();
// Check if GC should be done on the database
if(DBdeleteoldqueries)
{
// No thread locks needed
delete_old_queries_in_DB();
DBdeleteoldqueries = false;
}
}
sleepms(100);
}
return NULL;
}
// Get most recent 24 hours data from long-term database
void read_data_from_DB(void)
{
// Don't try to load anything to the database if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Open database file
if(!dbopen())
{
logg("read_data_from_DB() - Failed to open DB");
return;
}
// Prepare request
char *rstr = NULL;
// Get time stamp 24 hours in the past
time_t now = time(NULL);
time_t mintime = now - config.maxlogage;
int rc = asprintf(&rstr, "SELECT * FROM queries WHERE timestamp >= %li", mintime);
if(rc < 1)
{
logg("read_data_from_DB() - Allocation error (%i): %s", rc, sqlite3_errmsg(db));
return;
}
// Log DB query string in debug mode
if(debug) logg(rstr);
// Prepare SQLite3 statement
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, rstr, -1, &stmt, NULL);
if( rc ){
logg("read_data_from_DB() - SQL error prepare (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return;
}
// Loop through returned database rows
while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
// Ensure we have enough space in the queries struct
memory_check(QUERIES);
// Set ID for this query
int queryID = counters.queries;
int queryTimeStamp = sqlite3_column_int(stmt, 1);
// 1483228800 = 01/01/2017 @ 12:00am (UTC)
if(queryTimeStamp < 1483228800)
{
logg("DB warn: TIMESTAMP should be larger than 01/01/2017 but is %i", queryTimeStamp);
continue;
}
if(queryTimeStamp > now)
{
if(debug) logg("DB warn: Skipping query logged in the future (%i)", queryTimeStamp);
continue;
}
int type = sqlite3_column_int(stmt, 2);
if(type < TYPE_A || type >= TYPE_MAX)
{
logg("DB warn: TYPE should not be %i", type);
continue;
}
// Don't import AAAA queries from database if the user set
// AAAA_QUERY_ANALYSIS=no in pihole-FTL.conf
if(type == TYPE_AAAA && !config.analyze_AAAA)
{
continue;
}
int status = sqlite3_column_int(stmt, 3);
if(status < QUERY_UNKNOWN || status > QUERY_EXTERNAL_BLOCKED)
{
logg("DB warn: STATUS should be within [%i,%i] but is %i", QUERY_UNKNOWN, QUERY_EXTERNAL_BLOCKED, status);
continue;
}
const char * domain = (const char *)sqlite3_column_text(stmt, 4);
if(domain == NULL)
{
logg("DB warn: DOMAIN should never be NULL, %i", queryTimeStamp);
continue;
}
const char * client = (const char *)sqlite3_column_text(stmt, 5);
if(client == NULL)
{
logg("DB warn: CLIENT should never be NULL, %i", queryTimeStamp);
continue;
}
// Check if user wants to skip queries coming from localhost
if(config.ignore_localhost &&
(strcmp(client, "127.0.0.1") == 0 || strcmp(client, "::1") == 0))
{
continue;
}
// Obtain IDs only after filtering which queries we want to keep
int domainID = findDomainID(domain);
int clientID = findClientID(client);
const char *forwarddest = (const char *)sqlite3_column_text(stmt, 6);
int forwardID = 0;
// Determine forwardID only when status == 2 (forwarded) as the
// field need not to be filled for other query status types
if(status == QUERY_FORWARDED)
{
if(forwarddest == NULL)
{
logg("DB warn: FORWARD should not be NULL with status QUERY_FORWARDED, %i", queryTimeStamp);
continue;
}
forwardID = findForwardID(forwarddest, true);
}
int overTimeTimeStamp = queryTimeStamp - (queryTimeStamp % 600) + 300;
int timeidx = findOverTimeID(overTimeTimeStamp);
validate_access("overTime", timeidx, true, __LINE__, __FUNCTION__, __FILE__);
// Store this query in memory
validate_access("queries", queryID, false, __LINE__, __FUNCTION__, __FILE__);
queries[queryID].magic = MAGICBYTE;
queries[queryID].timestamp = queryTimeStamp;
queries[queryID].type = type;
queries[queryID].status = status;
queries[queryID].domainID = domainID;
queries[queryID].clientID = clientID;
queries[queryID].forwardID = forwardID;
queries[queryID].timeidx = timeidx;
queries[queryID].db = true; // Mark this as already present in the database
queries[queryID].id = 0; // This is dnsmasq's internal ID. We don't store it in the database
queries[queryID].complete = true; // Mark as all information is avaiable
queries[queryID].response = 0;
queries[queryID].AD = false;
lastDBimportedtimestamp = queryTimeStamp;
// Handle type counters
if(type >= TYPE_A && type < TYPE_MAX)
{
counters.querytype[type-1]++;
overTime[timeidx].querytypedata[type-1]++;
}
// Update overTime data
overTime[timeidx].total++;
// Update overTime data structure with the new client
validate_access_oTcl(timeidx, clientID, __LINE__, __FUNCTION__, __FILE__);
overTime[timeidx].clientdata[clientID]++;
// Increase DNS queries counter
counters.queries++;
// Increment status counters
switch(status)
{
case QUERY_UNKNOWN: // Unknown
counters.unknown++;
break;
case QUERY_GRAVITY: // Blocked by gravity.list
case QUERY_WILDCARD: // Blocked by regex filter
case QUERY_BLACKLIST: // Blocked by black.list
case QUERY_EXTERNAL_BLOCKED: // Blocked by external provider
counters.blocked++;
overTime[timeidx].blocked++;
domains[domainID].blockedcount++;
clients[clientID].blockedcount++;
break;
case QUERY_FORWARDED: // Forwarded
counters.forwardedqueries++;
// Update overTime data structure
break;
case QUERY_CACHE: // Cached or local config
counters.cached++;
// Update overTime data structure
overTime[timeidx].cached++;
break;
default:
logg("Error: Found unknown status %i in long term database!", status);
logg(" Timestamp: %i", queryTimeStamp);
logg(" Continuing anyway...");
break;
}
}
logg("Imported %i queries from the long-term database", counters.queries);
if( rc != SQLITE_DONE ){
logg("read_data_from_DB() - SQL error step (%i): %s", rc, sqlite3_errmsg(db));
dbclose();
check_database(rc);
return;
}
// Finalize SQLite3 statement
sqlite3_finalize(stmt);
dbclose();
free(rstr);
}