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

Add IPv6 support for network table #591

Merged
merged 41 commits into from Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fa18fe1
Obtain network information from ip command instead of through procfs
DL6ER Jun 10, 2019
264e3db
Create network-addresses table. This updates the databsae version to 5
DL6ER Jun 10, 2019
836c359
Store addresses in new table network_addresses
DL6ER Jun 10, 2019
a85b973
Rename network_addresses(id) to network_addresses(network_id) and ref…
DL6ER Jun 12, 2019
717cca0
Rename function to parse_neighbor_cache()
DL6ER Jun 12, 2019
2f1fbda
Use PRIMARY KEY for pair (network_id,ip). This implies UNIQUE.
DL6ER Jun 14, 2019
2a1361d
Always add IP addresses to network_addresses, even if the client isn'…
DL6ER Jun 14, 2019
7a4c4f3
Also analyze loops with router related information
DL6ER Jun 14, 2019
9076223
Merge branch 'new/networktable_ip_neigh' of github.com:pi-hole/FTL in…
DL6ER Jun 14, 2019
4a2b38b
Change lastQuery column to lastSeen as this is a better description (…
DL6ER Jun 14, 2019
a78807e
Remove obsolete debugging output
DL6ER Jun 14, 2019
9457341
Add test for successful creation of network_addresses table.
DL6ER Jun 14, 2019
5f075e1
Remove extra parentheses
DL6ER Jun 15, 2019
01b2205
Enable the enforcement of foreign key constraints. PRAGMA FOREIGN_KEY…
DL6ER Jun 15, 2019
8fd02cd
Use UNIQUE for the pair (network_id,ip) as PRIMARY KEY does not enfor…
DL6ER Jun 15, 2019
634f97a
Update tests after having changed table schema
DL6ER Jun 15, 2019
e9834d4
Update dbID when INSERTing a new row into the network table to have t…
DL6ER Jun 15, 2019
32185d9
Merge branch 'development' into new/networktable_ip_neigh
DL6ER Jun 16, 2019
e8e64c0
Add an initial entry in network_addresses for each device in the netw…
DL6ER Jun 16, 2019
46ed609
Tests: ALTER TABLE ... RENAME TO ... created a table using CREATE TAB…
DL6ER Jun 16, 2019
5f44b5b
Tests: Added default value for lastSeen column in network_addresses t…
DL6ER Jun 16, 2019
7f4bb01
Wrap upgrading database into SQL transaction.
DL6ER Jun 16, 2019
d1f8e1c
Merge branch 'development' into new/networktable_ip_neigh
DL6ER Jun 16, 2019
ecffbd6
Put UNIQUE constraint directly on definition of the hwaddr column ins…
DL6ER Jun 16, 2019
7eb1d70
Merge branch 'new/networktable_ip_neigh' of github.com:pi-hole/FTL in…
DL6ER Jun 16, 2019
46b97d5
DROP TABLE also DROPs the index so we do not need to do this separately.
DL6ER Jun 16, 2019
c922835
Tests: Change schema of network table
DL6ER Jun 16, 2019
4809366
Use ip-neighbor's Neighbour Unreachability Detection (NUD) option to …
DL6ER Jun 16, 2019
95b4ac5
Update comment
DL6ER Jun 16, 2019
26c52dc
Use macro to reduce code duplication in create_network_addresses_tabl…
DL6ER Jun 16, 2019
2697829
Use new macros where possible to reduce code duplication.
DL6ER Jun 16, 2019
07acaf9
Rename MACRO SQL to SQL_bool to highlight that it will return a boole…
DL6ER Jun 16, 2019
faee471
Move database macros into routines.h
DL6ER Jun 19, 2019
63b0724
Use SQL_* macros where possible in database.c
DL6ER Jun 19, 2019
9d93763
Enable the enforcement of foreign key constraints for all database co…
DL6ER Jun 24, 2019
adbdf3f
Running dbclose() within check_database() further reduces code duplic…
DL6ER Jun 24, 2019
a44d594
Merge branch 'development' into new/networktable_ip_neigh
DL6ER Jun 25, 2019
b98535c
Close the database connection in case the ip neigh command fails.
DL6ER Jun 26, 2019
f7cbe10
Temporarily disable foreign key enforcement during update. This becom…
DL6ER Jul 1, 2019
26de086
Re-enable foreign key enforcement at the end of the upgrade transaction.
DL6ER Jul 3, 2019
1afb43c
Merge branch 'development' into new/networktable_ip_neigh
DL6ER Jul 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 40 additions & 22 deletions networktable.c
Expand Up @@ -44,8 +44,8 @@ bool create_network_addresses_table(void)
// Create network_addresses table in the database
ret = dbquery("CREATE TABLE network_addresses ( network_id INTEGER NOT NULL, "\
"ip TEXT NOT NULL, "\
"lastQuery INTEGER NOT NULL, "\
"UNIQUE(network_id,ip), "\
"lastSeen INTEGER NOT NULL, "\
"PRIMARY KEY(network_id,ip), "\
"FOREIGN KEY(network_id) REFERENCES network(id));");
if(!ret){ dbclose(); return false; }

Expand Down Expand Up @@ -82,7 +82,7 @@ void parse_neighbor_cache(void)
// Prepare buffers
char * linebuffer = NULL;
size_t linebuffersize = 0;
char ip[100], status[100], hwaddr[100], iface[100];
char ip[100], status1[100], status2[100], hwaddr[100], iface[100];
unsigned int entries = 0;
time_t now = time(NULL);

Expand All @@ -93,17 +93,35 @@ void parse_neighbor_cache(void)
// Read ARP cache line by line
while(getline(&linebuffer, &linebuffersize, arpfp) != -1)
{
int num = sscanf(linebuffer, "%99s dev %99s lladdr %99s %99s",
ip, iface, hwaddr, status);
int num = sscanf(linebuffer, "%99s dev %99s lladdr %99s %99s %99s",
ip, iface, hwaddr, status1, status2);

// Skip addresses without hardware address information
if (num != 4)
continue;

// Only process active entries
if(strcasecmp(status, "reachable") != 0 &&
strcasecmp(status, "stale") != 0)
continue;
// Check if we want to process the line we just read
switch(num)
{
// Example: 2003:17:7:dc:e1b1:1e01:1a5a:ff dev eth0 lladdr a0:20:29:24:98:99 REACHABLE
case 4:
if(strcasecmp(status1, "reachable") != 0 &&
strcasecmp(status1, "stale") != 0)
{
continue;
}
break;

// Example: dead:beef:dead:beef:dead:beef dev wlp3s0 lladdr de:ad:be:ef:de:ad router REACHABLE
case 5:
if(strcasecmp(status1, "router") != 0 &&
(strcasecmp(status2, "reachable") != 0 &&
DL6ER marked this conversation as resolved.
Show resolved Hide resolved
strcasecmp(status2, "stale") != 0 ))
{
continue;
}
break;

// Skip lines without hardware information
default:
continue;
}
DL6ER marked this conversation as resolved.
Show resolved Hide resolved

// Get ID of this device in our network database. If it cannot be
// found, then this is a new device. We only use the hardware address
Expand Down Expand Up @@ -194,15 +212,6 @@ void parse_neighbor_cache(void)
"WHERE id = %i;",\
ip, dbID);

// 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 REPLACE INTO network_addresses "\
"(network_id,ip,lastQuery) VALUES(%i,\'%s\',%i);",\
dbID, ip, client->lastQuery);

// Store hostname if available
if(strlen(hostname) > 0)
{
Expand All @@ -216,6 +225,15 @@ void parse_neighbor_cache(void)
// else:
// Device in database but not known to Pi-hole: No action required

// 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 REPLACE INTO network_addresses "\
"(network_id,ip,lastSeen) VALUES(%i,\'%s\',%d);",\
dbID, ip, time(NULL));

// Count number of processed ARP cache entries
entries++;
}
Expand Down
1 change: 1 addition & 0 deletions test/test_suite.bats
Expand Up @@ -244,6 +244,7 @@
[[ "${lines[@]}" == *"CREATE TABLE ftl ( id INTEGER PRIMARY KEY NOT NULL, value BLOB NOT NULL );"* ]]
[[ "${lines[@]}" == *"CREATE TABLE counters ( id INTEGER PRIMARY KEY NOT NULL, value INTEGER NOT NULL );"* ]]
[[ "${lines[@]}" == *"CREATE TABLE network ( id INTEGER PRIMARY KEY NOT NULL, ip TEXT NOT NULL, hwaddr TEXT NOT NULL, interface TEXT NOT NULL, name TEXT, firstSeen INTEGER NOT NULL, lastQuery INTEGER NOT NULL, numQueries INTEGER NOT NULL,macVendor TEXT);"* ]]
[[ "${lines[@]}" == *"CREATE TABLE network_addresses ( network_id INTEGER NOT NULL, ip TEXT NOT NULL, lastSeen INTEGER NOT NULL, PRIMARY KEY(network_id,ip), FOREIGN KEY(network_id) REFERENCES network(id));"* ]]
[[ "${lines[@]}" == *"CREATE INDEX idx_queries_timestamps ON queries (timestamp);"* ]]
[[ "${lines[@]}" == *"CREATE UNIQUE INDEX network_hwaddr_idx ON network(hwaddr);"* ]]
}
Expand Down