From 54253bd7d59cd7afc07eee51c2a72bcf10eab1c0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 26 Dec 2018 11:25:41 +0100 Subject: [PATCH] Added more comments Signed-off-by: DL6ER --- networktable.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/networktable.c b/networktable.c index e3c0d89a..ce8c1df4 100644 --- a/networktable.c +++ b/networktable.c @@ -87,13 +87,13 @@ void parse_arp_cache(void) // such as this SELECT command will be executed immediately on the database. char* querystr = NULL; int ret = asprintf(&querystr, "SELECT id FROM network WHERE ip = \"%s\" AND hwaddr = \"%s\";", ip, hwaddr); - if(querystr == NULL || ret < 0) { logg("Memory allocation failed in parse_arp_cache (%i)", ret); break; } + // Perform SQL query int dbID = db_query_int(querystr); free(querystr); @@ -110,8 +110,12 @@ void parse_arp_cache(void) lock_shm(); int clientID = findClientID(ip, false); unlock_shm(); + + // This client is known (by its IP address) to pihole-FTL if + // findClientID() returned a non-negative index bool clientKnown = clientID >= 0; + // Get hostname of this client if the client is known char *hostname = NULL; if(clientKnown) { @@ -119,17 +123,17 @@ void parse_arp_cache(void) hostname = getstr(clients[clientID].namepos); } + // Device not in database, add new entry if(dbID == -1) { - // Device not in database, add new entry dbquery("INSERT INTO network "\ "(ip,hwaddr,interface,firstSeen,lastQuery,name) "\ - "VALUES "\ - "(\"%s\",\"%s\",\"%s\",%lu, %lu, \"%s\");",\ + "VALUES (\"%s\",\"%s\",\"%s\",%lu, %ld, \"%s\");",\ ip, hwaddr, iface, now, clientKnown ? clients[clientID].lastQuery : 0L, hostname == NULL ? "" : hostname); } + // Device in database AND client known to Pi-hole else if(clientKnown) { // Update lastQuery, only use new value if larger @@ -151,12 +155,17 @@ void parse_arp_cache(void) hostname, dbID); } } + // else: + // Device in database but not known to Pi-hole: No action required + + // Count number of processed ARP cache entries entries++; } // Actually update the database dbquery("COMMIT"); + // Debug logging if(debug) logg("ARP table processing (%i entries) took %.1f ms", entries, timer_elapsed_msec(ARP_TIMER)); // Close file handle