1
0
mirror of https://github.com/danog/dns.git synced 2024-11-26 20:14:51 +01:00

Use ext-filter instead of inet_pton to avoid error suppression (#73)

This commit is contained in:
Austin Heap 2018-03-31 08:56:32 -07:00 committed by Niklas Keller
parent e9ee48e269
commit 41d02a30bb
2 changed files with 20 additions and 14 deletions

View File

@ -42,7 +42,8 @@
"amphp/parser": "^1",
"amphp/uri": "^0.1",
"amphp/windows-registry": "^0.3",
"daverandom/libdns": "^2.0.1"
"daverandom/libdns": "^2.0.1",
"ext-filter": "*"
},
"require-dev": {
"amphp/phpunit-util": "^1",

View File

@ -89,23 +89,28 @@ final class BasicResolver implements Resolver {
yield $this->reloadConfig();
}
$inAddr = @\inet_pton($name);
if ($inAddr !== false) {
// It's already a valid IP, don't query, immediately return
if ($typeRestriction) {
if ($typeRestriction === Record::A && isset($inAddr[4])) {
switch ($typeRestriction) {
case Record::A:
if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return [new Record($name, Record::A, null)];
} elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
throw new ResolutionException("Got an IPv6 address, but type is restricted to IPv4");
}
if ($typeRestriction === Record::AAAA && !isset($inAddr[4])) {
break;
case Record::AAAA:
if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return [new Record($name, Record::AAAA, null)];
} elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
throw new ResolutionException("Got an IPv4 address, but type is restricted to IPv6");
}
break;
default:
if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return [new Record($name, Record::A, null)];
} elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return [new Record($name, Record::AAAA, null)];
}
return [
new Record($name, isset($inAddr[4]) ? Record::AAAA : Record::A, null),
];
break;
}
$name = normalizeDnsName($name);