Skip to content

Commit

Permalink
Update lastSeen for the network_addresses fields. Also add some more …
Browse files Browse the repository at this point in the history
…debugging output to ARP processing.

Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Mar 14, 2020
1 parent 560685a commit f69930b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ int db_query_int(const char* querystr)
return DB_FAILED;
}

if(config.debug & DEBUG_DATABASE)
{
logg("dbquery: \"%s\"", querystr);
}

sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(FTL_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
Expand Down
52 changes: 35 additions & 17 deletions src/database/network-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,10 @@ void parse_neighbor_cache(void)

// Add unique pair of ID (corresponds to one particular hardware
// address) and IP address if it does not exist (INSERT). In case
// this pair already exists, the UNIQUE(network_id,ip) trigger
// becomes active and the line is instead REPLACEd, causing the
// lastQuery timestamp to be updated
dbquery("INSERT OR IGNORE INTO network_addresses "\
"(network_id,ip) VALUES(%i,\'%s\');", dbID, ip);
// this pair already exists, replace it
dbquery("INSERT OR REPLACE INTO network_addresses "\
"(network_id,ip,lastSeen) VALUES(%i,\'%s\',(cast(strftime('%%s', 'now') as int)));",
dbID, ip);

// Count number of processed ARP cache entries
entries++;
Expand All @@ -287,25 +286,43 @@ void parse_neighbor_cache(void)
// all to the database
for(int clientID = 0; clientID < counters->clients; clientID++)
{
// Skip if already handled above
if(ARP_client[clientID])
continue;

// Get client pointer
clientsData* client = getClient(clientID, true);
if(client == NULL || client->count < 1)
continue;

// Do not create records or update clients without active queries
// This reduces database I/O when nothing would change anyways
if(client->numQueriesARP == 0)
if(client == NULL)
{
if(config.debug & DEBUG_ARP)
logg("Network table: Client %d returned NULL pointer", clientID);
continue;
}

// Get hostname and IP address of this client
const char *hostname, *ipaddr;
ipaddr = getstr(client->ippos);
hostname = getstr(client->namepos);

// Skip if this client was inactive (last query may be older than 24 hours)
// This also reduces database I/O when nothing would change anyways
if(client->count < 1 || client->numQueriesARP < 1)
{
if(config.debug & DEBUG_ARP)
logg("Network table: Client %s has zero queries (%d, %d)",
ipaddr, client->count, client->numQueriesARP);
continue;
}
// Skip if already handled above
else if(ARP_client[clientID])
{
if(config.debug & DEBUG_ARP)
logg("Network table: Client %s known through ARP/neigh cache",
ipaddr);
continue;
}
else if(config.debug & DEBUG_ARP)
{
logg("Network table: %s NOT known through ARP/neigh cache", ipaddr);
}

// Get device with mock-hardware address (ip-<IP>)
char* querystr = NULL;
ret = asprintf(&querystr, "SELECT id FROM network WHERE hwaddr = \'ip-%s\';", ipaddr);
Expand Down Expand Up @@ -368,9 +385,10 @@ void parse_neighbor_cache(void)
}
}

// Add IP/mock-MAC pair to address database
dbquery("INSERT OR IGNORE INTO network_addresses "\
"(network_id,ip) VALUES(%i,\'%s\');", dbID, ipaddr);
// Add/replace IP/mock-MAC pair to address database
dbquery("INSERT OR REPLACE INTO network_addresses "\
"(network_id,ip,lastSeen) VALUES(%i,\'%s\',(cast(strftime('%%s', 'now') as int)));",
dbID, ipaddr);

// Add to number of processed ARP cache entries
additional_entries++;
Expand Down

0 comments on commit f69930b

Please sign in to comment.