Skip to content

Commit

Permalink
Merge pull request #1793 from pi-hole/tweak/group_clients_MAC_hostnames
Browse files Browse the repository at this point in the history
Try to obtain hostname for MAC clients
  • Loading branch information
DL6ER authored Apr 30, 2021
2 parents dff02dd + ca71bc8 commit eab215e
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions scripts/pi-hole/php/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,45 @@ function JSON_error($message = null)
throw new Exception('While executing network table statement: ' . $db->lastErrorMsg());
}

// There will always be a result. Unknown host names are NULL
// Check if got a hostname from the database. This may not be the case if the client is
// specified by MAC address, a hostname or via a more general selector like an interface.
$name_result = $result->fetchArray(SQLITE3_ASSOC);
$res['name'] = $name_result['name'];
if(!is_bool($name_result))
{
$res['name'] = $name_result['name'];
error_log("IP: ".$name_result['name']);
}
else
{
// Check if we can get a host name from the database when looking up the MAC
// address of this client instead.
$stmt = $FTLdb->prepare('SELECT name FROM network n JOIN network_addresses na ON na.network_id = n.id WHERE hwaddr=:hwaddr COLLATE NOCASE AND name IS NOT NULL;');
if (!$stmt) {
throw new Exception('Error while preparing network table statement: ' . $db->lastErrorMsg());
}

if (!$stmt->bindValue(':hwaddr', $res['ip'], SQLITE3_TEXT)) {
throw new Exception('While binding to network table statement: ' . $db->lastErrorMsg());
}

$result = $stmt->execute();
if (!$result) {
throw new Exception('While executing network table statement: ' . $db->lastErrorMsg());
}

// Check if we found a result. There may be multiple entries for
// this client in the network_addresses table. We use the first
// hostname we find for the sake of simplicity.
$name_result = $result->fetchArray(SQLITE3_ASSOC);
if(!is_bool($name_result))
{
$res['name'] = $name_result['name'];
}
else
{
$res['name'] = null;
}
}

$groups = array();
while ($gres = $group_query->fetchArray(SQLITE3_ASSOC)) {
Expand Down

0 comments on commit eab215e

Please sign in to comment.