diff --git a/src/Config/Config.php b/src/Config/Config.php index 37ae91d9..9677ee57 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -82,10 +82,20 @@ public static function loadResolvConfBlocking($path = null) throw new RuntimeException('Unable to load resolv.conf file "' . $path . '"'); } + $matches = array(); preg_match_all('/^nameserver\s+(\S+)\s*$/m', $contents, $matches); $config = new self(); - $config->nameservers = $matches[1]; + foreach ($matches[1] as $ip) { + // remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`) + if (strpos($ip, ':') !== false && ($pos = strpos($ip, '%')) !== false) { + $ip = substr($ip, 0, $pos); + } + + if (@inet_pton($ip) !== false) { + $config->nameservers[] = $ip; + } + } return $config; } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 6416495b..224a87f7 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -47,6 +47,15 @@ public function testParsesSingleEntryFile() $this->assertEquals($expected, $config->nameservers); } + public function testParsesNameserverWithoutIpv6ScopeId() + { + $contents = 'nameserver ::1%lo'; + $expected = array('::1'); + + $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents)); + $this->assertEquals($expected, $config->nameservers); + } + public function testParsesNameserverEntriesFromAverageFileCorrectly() { $contents = '# @@ -70,7 +79,6 @@ public function testParsesNameserverEntriesFromAverageFileCorrectly() public function testParsesEmptyFileWithoutNameserverEntries() { - $contents = ''; $expected = array(); $config = Config::loadResolvConfBlocking('data://text/plain;base64,'); @@ -87,6 +95,7 @@ public function testParsesFileAndIgnoresCommentsAndInvalidNameserverEntries() nameserver 4.5.6.7 5.6.7.8 nameserver 6.7.8.9 NameServer 7.8.9.10 +nameserver localhost '; $expected = array();