Skip to content

Commit

Permalink
Add support for multiple hostnames per client (hardware address).
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed May 14, 2020
1 parent 68b9965 commit 3e1a1f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
16 changes: 11 additions & 5 deletions api_db.php
Expand Up @@ -62,15 +62,21 @@ function resolveHostname($clientip, $printIP)

while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC))
{
$id = $res["id"];
// Empty array for holding the IP addresses
$res["ip"] = array();
$id = intval($res["id"]);

// Get IP addresses for this device
$res["ip"] = array();
$network_addresses = $db->query("SELECT ip FROM network_addresses WHERE network_id = $id ORDER BY lastSeen DESC");
while($network_addresses !== false && $ip = $network_addresses->fetchArray(SQLITE3_ASSOC))
array_push($res["ip"],$ip["ip"]);
// UTF-8 encode host name and vendor
$res["name"] = utf8_encode($res["name"]);

// Get list of host names for this device
$res["name"] = array();
$network_names = $db->query("SELECT name FROM network_names WHERE network_id = $id ORDER BY lastSeen DESC");
while($network_names !== false && $ip = $network_names->fetchArray(SQLITE3_ASSOC))
array_push($res["name"],utf8_encode($ip["name"]));

// UTF-8 encode vendor
$res["macVendor"] = utf8_encode($res["macVendor"]);
array_push($network, $res);
}
Expand Down
18 changes: 18 additions & 0 deletions scripts/pi-hole/js/network.js
Expand Up @@ -108,8 +108,26 @@ $(document).ready(function () {
// Set hostname to "unknown" if not available
if (!data.name || data.name.length === 0) {
$("td:eq(3)", row).html("<em>unknown</em>");
} else {
var names = [];
var maxiter = Math.min(data.name.length, MAXIPDISPLAY);
for (var index = 0; index < maxiter; index++) {
var name = data.name[index];
names.push('<a href="queries.php?client=' + name + '">' + name + "</a>");
}

if (data.name.length > MAXIPDISPLAY) {
// We hit the maximum above, add "..." to symbolize we would
// have more to show here
names.push("...");
}
$("td:eq(3)", row).html(names.join("<br>"));
$("td:eq(3)", row).hover(function () {
this.title = data.name.join("\n");
});
}


// Set number of queries to localized string (add thousand separators)
$("td:eq(6)", row).html(data.numQueries.toLocaleString());

Expand Down
12 changes: 9 additions & 3 deletions scripts/pi-hole/php/groups.php
Expand Up @@ -172,7 +172,7 @@ function JSON_error($message = null)
throw new Exception('Error while querying gravity\'s client_by_group table: ' . $db->lastErrorMsg());
}

$stmt = $FTLdb->prepare('SELECT name FROM network WHERE id = (SELECT network_id FROM network_addresses WHERE ip = :ip);');
$stmt = $FTLdb->prepare('SELECT name FROM network_names WHERE network_id = (SELECT network_id FROM network_addresses WHERE ip = :ip);');
if (!$stmt) {
throw new Exception('Error while preparing network table statement: ' . $db->lastErrorMsg());
}
Expand Down Expand Up @@ -208,15 +208,21 @@ function JSON_error($message = null)
$QUERYDB = getQueriesDBFilename();
$FTLdb = SQLite3_connect($QUERYDB);

$query = $FTLdb->query('SELECT DISTINCT hwaddr,name FROM network;');
$query = $FTLdb->query('SELECT DISTINCT id,hwaddr FROM network;');
if (!$query) {
throw new Exception('Error while querying FTL\'s database: ' . $db->lastErrorMsg());
}

// Loop over results
$ips = array();
while ($res = $query->fetchArray(SQLITE3_ASSOC)) {
$ips[strtoupper($res['hwaddr'])] = $res['name'] !== null ? $res['name'] : '';
$id = intval($res["id"]);
$query_names = $FTLdb->query("SELECT name FROM network_names WHERE network_id = $id ORDER BY lastSeen DESC;");
$names = [];
while ($res_names = $query_names->fetchArray(SQLITE3_ASSOC)) {
array_push($names, utf8_encode($res_names["name"]));
}
$ips[strtoupper($res['hwaddr'])] = implode(",", $names);
}
$FTLdb->close();

Expand Down

1 comment on commit 3e1a1f5

@pralor-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/incorrect-hostnames/33111/9

Please sign in to comment.