LibcDnsDriver.php: use something less bad than gethostbynamel

This commit is contained in:
Zankaria 2025-04-24 00:05:11 +02:00
parent 4135c2924e
commit 3f68cd799d

View File

@ -11,18 +11,28 @@ class LibcDnsDriver implements DnsDriver {
\putenv("RES_OPTIONS=retrans:1 retry:1 timeout:{$timeout} attempts:1"); \putenv("RES_OPTIONS=retrans:1 retry:1 timeout:{$timeout} attempts:1");
} }
/**
* For the love of god never use this.
* https://www.php.net/manual/en/function.gethostbynamel.php#119535
*/
public function nameToIPs(string $name): ?array { public function nameToIPs(string $name): ?array {
// Add a trailing dot to not return the loopback address on failure $ret = \dns_get_record($name, DNS_A | DNS_AAAA);
// https://www.php.net/manual/en/function.gethostbynamel.php#119535
$ret = \gethostbynamel("{$name}.");
if ($ret === false) { if ($ret === false) {
return null; return null;
} }
return $ret;
$ips = [];
foreach ($ret as $dns_record) {
if ($dns_record['type'] == 'A') {
$ips[] = $dns_record['ip'];
} elseif ($dns_record['type'] == 'AAAA') {
$ips[] = $dns_record['ipv6'];
}
}
if (empty($ips)) {
return [];
} else {
// Stable return order.
\sort($ips, \SORT_STRING);
return $ips;
}
} }
/** /**