Skip to content
2 changes: 1 addition & 1 deletion src/Query/HostsFileExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function getIpFromHost($host)
return inet_ntop(strrev($ip));
} elseif (substr($host, -9) === '.ip6.arpa') {
// IPv6: replace dots, reverse nibbles and interpret as hexadecimal string
$ip = @inet_ntop(hex2bin(strrev(str_replace('.', '', substr($host, 0, -9)))));
$ip = @inet_ntop(pack('H*', strrev(str_replace('.', '', substr($host, 0, -9)))));
Comment thread
clue marked this conversation as resolved.
if ($ip === false) {
return null;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/Query/HostsFileExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ public function testReverseFallsBackForInvalidIpv4Address()
$this->executor->query('8.8.8.8', new Query('::1.in-addr.arpa', Message::TYPE_PTR, Message::CLASS_IN, 0));
}

public function testReverseFallsBackForInvalidIpv6Address()
public function testReverseFallsBackForInvalidLengthIpv6Address()
{
$this->hosts->expects($this->never())->method('getHostsForIp');
$this->fallback->expects($this->once())->method('query');

$this->executor->query('8.8.8.8', new Query('abcd.ip6.arpa', Message::TYPE_PTR, Message::CLASS_IN, 0));
}

public function testReverseFallsBackForInvalidHexIpv6Address()
{
$this->hosts->expects($this->never())->method('getHostsForIp');
$this->fallback->expects($this->once())->method('query');

$this->executor->query('8.8.8.8', new Query('zZz.ip6.arpa', Message::TYPE_PTR, Message::CLASS_IN, 0));
}
}