mirror of
https://github.com/danog/dns.git
synced 2024-12-02 09:27:55 +01:00
Concise IP check
This commit is contained in:
parent
7344cceb95
commit
ce71845dd1
@ -108,38 +108,24 @@ final class Rfc1035StubDnsResolver implements DnsResolver
|
|||||||
return $this->blockingFallbackResolver->resolve($name, $typeRestriction, $cancellation);
|
return $this->blockingFallbackResolver->resolve($name, $typeRestriction, $cancellation);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($typeRestriction) {
|
// Check if provided $name is an IP address.
|
||||||
case DnsRecord::A:
|
$isIp = \array_filter([
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
DnsRecord::A => \filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4),
|
||||||
return [new DnsRecord($name, DnsRecord::A, null)];
|
DnsRecord::AAAA => \filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// If the name is an IP address, return that IP (or throw if it does match the type restriction).
|
||||||
|
if ($isIp) {
|
||||||
|
$type = \array_key_first($isIp);
|
||||||
|
if (!\in_array($type, $recordTypes, true)) {
|
||||||
|
throw new DnsException("Got an IP address that is not valid for the restricted record type");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
return [new DnsRecord($name, $type, null)];
|
||||||
throw new DnsException("Got an IPv6 address, but type is restricted to IPv4");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DnsRecord::AAAA:
|
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
||||||
return [new DnsRecord($name, DnsRecord::AAAA, null)];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
||||||
throw new DnsException("Got an IPv4 address, but type is restricted to IPv6");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
||||||
return [new DnsRecord($name, DnsRecord::A, null)];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (\filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
||||||
return [new DnsRecord($name, DnsRecord::AAAA, null)];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$dots = \substr_count($name, ".");
|
$dots = \substr_count($name, ".");
|
||||||
$trailingDot = $name[-1] === ".";
|
$trailingDot = $name !== '' && $name[-1] === ".";
|
||||||
$name = normalizeName($name);
|
$name = normalizeName($name);
|
||||||
|
|
||||||
if ($records = $this->queryHosts($name, $typeRestriction)) {
|
if ($records = $this->queryHosts($name, $typeRestriction)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user