Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always ensure FTL messages are unique #1480

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 51 additions & 54 deletions src/database/message-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool flush_message_table(void)
return true;
}

static bool add_message(const enum message_type type, const bool unique,
static bool add_message(const enum message_type type,
const char *message, const int count,...)
{
bool okay = false;
Expand All @@ -166,55 +166,52 @@ static bool add_message(const enum message_type type, const bool unique,
return false;
}

// Ensure there are no duplicates when adding host name or rate-limiting messages
if(unique)
{
sqlite3_stmt* stmt = NULL;
const char *querystr = "DELETE FROM message WHERE type = ?1 AND message = ?2";
int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
logg("add_message(type=%u, message=%s) - SQL error prepare DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}

// Bind type to prepared statement
if((rc = sqlite3_bind_text(stmt, 1, message_types[type], -1, SQLITE_STATIC)) != SQLITE_OK)
{
logg("add_message(type=%u, message=%s) - Failed to bind type DELETE: %s",
type, message, sqlite3_errstr(rc));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
goto end_of_add_message;
}
// Ensure there are no duplicates when adding messages
sqlite3_stmt* stmt = NULL;
const char *querystr = "DELETE FROM message WHERE type = ?1 AND message = ?2";
int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
logg("add_message(type=%u, message=%s) - SQL error prepare DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}

// Bind message to prepared statement
if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK)
{
logg("add_message(type=%u, message=%s) - Failed to bind message DELETE: %s",
type, message, sqlite3_errstr(rc));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
goto end_of_add_message;
}
// Bind type to prepared statement
if((rc = sqlite3_bind_text(stmt, 1, message_types[type], -1, SQLITE_STATIC)) != SQLITE_OK)
{
logg("add_message(type=%u, message=%s) - Failed to bind type DELETE: %s",
type, message, sqlite3_errstr(rc));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
goto end_of_add_message;
}

// Execute and finalize
if((rc = sqlite3_step(stmt)) != SQLITE_OK && rc != SQLITE_DONE)
{
logg("add_message(type=%u, message=%s) - SQL error step DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
sqlite3_clear_bindings(stmt);
// Bind message to prepared statement
if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK)
{
logg("add_message(type=%u, message=%s) - Failed to bind message DELETE: %s",
type, message, sqlite3_errstr(rc));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
goto end_of_add_message;
}

// Execute and finalize
if((rc = sqlite3_step(stmt)) != SQLITE_OK && rc != SQLITE_DONE)
{
logg("add_message(type=%u, message=%s) - SQL error step DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
stmt = NULL;

// Prepare SQLite statement
sqlite3_stmt* stmt = NULL;
const char *querystr = "INSERT INTO message (timestamp,type,message,blob1,blob2,blob3,blob4,blob5) "
"VALUES ((cast(strftime('%s', 'now') as int)),?,?,?,?,?,?,?);";
int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
querystr = "INSERT INTO message (timestamp,type,message,blob1,blob2,blob3,blob4,blob5) "
"VALUES ((cast(strftime('%s', 'now') as int)),?,?,?,?,?,?,?);";
rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
logg("add_message(type=%u, message=%s) - SQL error prepare: %s",
Expand Down Expand Up @@ -317,7 +314,7 @@ void logg_regex_warning(const char *type, const char *warning, const int dbindex

// Log to database only if not in CLI mode
if(!cli_mode)
add_message(REGEX_MESSAGE, false, warning, 3, type, regex, dbindex);
add_message(REGEX_MESSAGE, warning, 3, type, regex, dbindex);
}

void logg_subnet_warning(const char *ip, const int matching_count, const char *matching_ids,
Expand All @@ -332,7 +329,7 @@ void logg_subnet_warning(const char *ip, const int matching_count, const char *m

// Log to database
char *names = get_client_names_from_ids(matching_ids);
add_message(SUBNET_MESSAGE, false, ip, 5, matching_count, names, matching_ids, chosen_match_text, chosen_match_id);
add_message(SUBNET_MESSAGE, ip, 5, matching_count, names, matching_ids, chosen_match_text, chosen_match_id);
free(names);
}

Expand All @@ -343,7 +340,7 @@ void logg_hostname_warning(const char *ip, const char *name, const unsigned int
ip, name, pos);

// Log to database
add_message(HOSTNAME_MESSAGE, true, ip, 2, name, (const int)pos);
add_message(HOSTNAME_MESSAGE, ip, 2, name, (const int)pos);
}

void logg_fatal_dnsmasq_message(const char *message)
Expand All @@ -352,7 +349,7 @@ void logg_fatal_dnsmasq_message(const char *message)
logg("FATAL ERROR in dnsmasq core: %s", message);

// Log to database
add_message(DNSMASQ_CONFIG_MESSAGE, false, message, 0);
add_message(DNSMASQ_CONFIG_MESSAGE, message, 0);

// FTL will dies after this point, so we should make sure to clean up
// behind ourselves
Expand All @@ -368,7 +365,7 @@ void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit
clientIP, turnaround, turnaround == 1 ? "" : "s");

// Log to database
add_message(RATE_LIMIT_MESSAGE, true, clientIP, 2, config.rate_limit.count, config.rate_limit.interval);
add_message(RATE_LIMIT_MESSAGE, clientIP, 2, config.rate_limit.count, config.rate_limit.interval);
}

void logg_warn_dnsmasq_message(char *message)
Expand All @@ -377,33 +374,33 @@ void logg_warn_dnsmasq_message(char *message)
logg("WARNING in dnsmasq core: %s", message);

// Log to database
add_message(DNSMASQ_WARN_MESSAGE, false, message, 0);
add_message(DNSMASQ_WARN_MESSAGE, message, 0);
}

void log_resource_shortage(const double load, const int nprocs, const int shmem, const int disk, const char *path, const char *msg)
{
if(load > 0.0)
{
logg("WARNING: Long-term load (15min avg) larger than number of processors: %.1f > %d", load, nprocs);
add_message(LOAD_MESSAGE, true, "excessive load", 2, load, nprocs);
add_message(LOAD_MESSAGE, "excessive load", 2, load, nprocs);
}
else if(shmem > -1)
{
logg("WARNING: RAM shortage (%s) ahead: %d%% is used (%s)", path, shmem, msg);
add_message(SHMEM_MESSAGE, true, path, 2, shmem, msg);
add_message(SHMEM_MESSAGE, path, 2, shmem, msg);
}
else if(disk > -1)
{
logg("WARNING: Disk shortage (%s) ahead: %d%% is used (%s)", path, disk, msg);
add_message(DISK_MESSAGE, true, path, 2, disk, msg);
add_message(DISK_MESSAGE, path, 2, disk, msg);
}
}

void logg_inaccessible_adlist(const int dbindex, const char *address)
{
// Log to FTL.log
logg("Adlist warning: Adlist with ID %d (%s) was inaccessible during last gravity run", dbindex, address);
logg("ADLIST WARNING: Adlist with ID %d (%s) was inaccessible during last gravity run", dbindex, address);

// Log to database
add_message(INACCESSIBLE_ADLIST_MESSAGE, false, address, 1, dbindex);
add_message(INACCESSIBLE_ADLIST_MESSAGE, address, 1, dbindex);
}