Skip to content

Commit b1b4b8c

Browse files
committed
Return IPv6 addresses for type AAAA queries
1 parent d8554e5 commit b1b4b8c

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/Query/HostsFileExecutor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ public function __construct(HostsFile $hosts, ExecutorInterface $fallback)
2727

2828
public function query($nameserver, Query $query)
2929
{
30-
if ($query->class === Message::CLASS_IN && $query->type === Message::TYPE_A) {
30+
if ($query->class === Message::CLASS_IN && ($query->type === Message::TYPE_A || $query->type === Message::TYPE_AAAA)) {
3131
$records = array();
32+
$expectsColon = $query->type === Message::TYPE_AAAA;
3233
foreach ($this->hosts->getIpsForHost($query->name) as $ip) {
33-
// ensure this is an IPv4 address
34-
if (strpos($ip, ':') === false) {
34+
// ensure this is an IPv4/IPV6 address according to query type
35+
if ((strpos($ip, ':') !== false) === $expectsColon) {
3536
$records[] = new Record($query->name, $query->type, $query->class, 0, $ip);
3637
}
3738
}

src/Resolver/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private function decorateHostsFileExecutor(ExecutorInterface $executor)
6060
// To compensate for this, we explicitly use hard-coded defaults for localhost
6161
if (DIRECTORY_SEPARATOR === '\\') {
6262
$executor = new HostsFileExecutor(
63-
new HostsFile("127.0.0.1 localhost"),
63+
new HostsFile("127.0.0.1 localhost\n::1 localhost"),
6464
$executor
6565
);
6666
}

tests/Query/HostsFileExecutorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ public function testFallsBackIfNoIpv4Matches()
5151

5252
$ret = $this->executor->query('8.8.8.8', new Query('google.com', Message::TYPE_A, Message::CLASS_IN, 0));
5353
}
54+
55+
public function testReturnsResponseMessageIfIpv6AddressesWereFound()
56+
{
57+
$this->hosts->expects($this->once())->method('getIpsForHost')->willReturn(array('::1'));
58+
$this->fallback->expects($this->never())->method('query');
59+
60+
$ret = $this->executor->query('8.8.8.8', new Query('google.com', Message::TYPE_AAAA, Message::CLASS_IN, 0));
61+
}
62+
63+
public function testFallsBackIfNoIpv6Matches()
64+
{
65+
$this->hosts->expects($this->once())->method('getIpsForHost')->willReturn(array('127.0.0.1'));
66+
$this->fallback->expects($this->once())->method('query');
67+
68+
$ret = $this->executor->query('8.8.8.8', new Query('google.com', Message::TYPE_AAAA, Message::CLASS_IN, 0));
69+
}
5470
}

0 commit comments

Comments
 (0)