Repository: netty/netty
Issue: Error parsing IPv6 interface Unix resolv.conf

### Expected behavior

I have the following `/etc/resolv.conf`:

``` conf
# Generated by NetworkManager
search localdomain
nameserver 10.211.55.1
nameserver fe80::21c:42ff:fe00:18%enp0s5
```

if I comment the last line, it works!

my network interfaces are:

``` bash
[fabio@localhost infinispan]$ ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:1c:42:32:14:87 brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.25/24 brd 10.211.55.255 scope global dynamic noprefixroute enp0s5
       valid_lft 1480sec preferred_lft 1480sec
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fe32:1487/64 scope global dynamic noprefixroute 
       valid_lft 2591703sec preferred_lft 604503sec
    inet6 fe80::21c:42ff:fe32:1487/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:ec:2a:d3:64 brd ff:ff:ff:ff:ff:ff
```

### Actual behavior

``` bash
java.lang.ExceptionInInitializerError: Exception java.lang.ExceptionInInitializerError [in thread "testng-TracingPropagationTest"]
	at io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.parseSilently(UnixResolverDnsServerAddressStreamProvider.java:80)
	at io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder$1.provider(DnsServerAddressStreamProviders.java:151)
	at io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder$1.<init>(DnsServerAddressStreamProviders.java:130)
	at io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder.<clinit>(DnsServerAddressStreamProviders.java:128)
	at io.netty.resolver.dns.DnsServerAddressStreamProviders.unixDefault(DnsServerAddressStreamProviders.java:117)
	at io.netty.resolver.dns.DnsServerAddressStreamProviders.platformDefault(DnsServerAddressStreamProviders.java:113)
	at io.netty.resolver.dns.DnsNameResolverBuilder.<init>(DnsNameResolverBuilder.java:66)
	at org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory.start(ChannelFactory.java:127)
```

### Steps to reproduce

Try to use a similar `/etc/resolv.conf`

### Minimal yet complete reproducer code (or URL to code)

DnsNameResolverBuilder builder = new DnsNameResolverBuilder()
               .channelType(io.netty.channel.epoll.EpollDatagramChannel.class)
               .ttl(0, 2147483647)
               .negativeTtl(0);

### Netty version

4.1.94.Final

### JVM version (e.g. `java -version`)

openjdk 17.0.7 2023-04-18 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.7.0.7-1.el7openjdkportable) (build 17.0.7+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.7.0.7-1.el7openjdkportable) (build 17.0.7+7-LTS, mixed mode, sharing)

### OS version (e.g. `uname -a`)

Linux localhost.localdomain 5.14.0-284.18.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 31 10:39:18 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux


Issue comments:
@fax4ever the `java.lang.ExceptionInInitializerError`should have a cause set... can you add this one as well ?
> @fax4ever the `java.lang.ExceptionInInitializerError`should have a cause set... can you add this one as well ?

Debugging the following seems to be the cause:

``` bash
java.lang.IllegalArgumentException: cannot use an unresolved DNS server address: fe80::21c:42ff:fe00:18%enp0s5/<unresolved>:5

io.netty.resolver.dns.DnsServerAddresses.sanitize(DnsServerAddresses.java:175)
io.netty.resolver.dns.DnsServerAddresses.sequential(DnsServerAddresses.java:67)
io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.putIfAbsent(UnixResolverDnsServerAddressStreamProvider.java:261)
io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.parse(UnixResolverDnsServerAddressStreamProvider.java:241)
io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.<init>(UnixResolverDnsServerAddressStreamProvider.java:98)
io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.<init>(UnixResolverDnsServerAddressStreamProvider.java:133)
io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.parseSilently(UnixResolverDnsServerAddressStreamProvider.java:72)
io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder$1.provider(DnsServerAddressStreamProviders.java:151)
io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder$1.<init>(DnsServerAddressStreamProviders.java:130)
io.netty.resolver.dns.DnsServerAddressStreamProviders$DefaultProviderHolder.<clinit>(DnsServerAddressStreamProviders.java:128)
io.netty.resolver.dns.DnsServerAddressStreamProviders.unixDefault(DnsServerAddressStreamProviders.java:117)
io.netty.resolver.dns.DnsServerAddressStreamProviders.platformDefault(DnsServerAddressStreamProviders.java:113)
io.netty.resolver.dns.DnsNameResolverBuilder.<init>(DnsNameResolverBuilder.java:66)
```

Thanks for the support!
Maybe the address `fe80::21c:42ff:fe00:18` cannot be resolved?
@fax4ever should be fixed by https://github.com/netty/netty/pull/13614
> @fax4ever should be fixed by #13614

thank you @normanmaurer for the very quick fix!

Pull request review comments:
We can reuse the `addr` variable here, instead of recreating the `InetSocketAddress` object.

Patch that resolved the issue:
diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProvider.java b/resolver-dns/src/main/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProvider.java
index 33f461b47f2..f8eff1740ed 100644
--- a/resolver-dns/src/main/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProvider.java
+++ b/resolver-dns/src/main/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProvider.java
@@ -211,7 +211,14 @@ private static Map<String, DnsServerAddresses> parse(File... etcResolverFiles) t
                                 port = Integer.parseInt(maybeIP.substring(i + 1));
                                 maybeIP = maybeIP.substring(0, i);
                             }
-                            addresses.add(SocketUtils.socketAddress(maybeIP, port));
+                            InetSocketAddress addr = SocketUtils.socketAddress(maybeIP, port);
+                            // Check if the address is resolved and only if this is the case use it. Otherwise just
+                            // ignore it. This is needed to filter out invalid entries, as if for example an ipv6
+                            // address is used with a scope that represent a network interface that does not exists
+                            // on the host.
+                            if (!addr.isUnresolved()) {
+                                addresses.add(addr);
+                            }
                         } else if (line.startsWith(DOMAIN_ROW_LABEL)) {
                             int i = indexOfNonWhiteSpace(line, DOMAIN_ROW_LABEL.length());
                             if (i < 0) {


Test patch:
diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java
index 41411baf751..f00035912ee 100644
--- a/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java
+++ b/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java
@@ -288,6 +288,18 @@ public void ignoreComments(@TempDir Path tempDir) throws Exception {
         assertHostNameEquals("127.0.0.2", stream.next());
     }
 
+    @Test
+    public void ipv6Nameserver(@TempDir Path tempDir) throws Exception {
+        File f = buildFile(tempDir, "search localdomain\n" +
+                "nameserver 10.211.55.1\n" +
+                "nameserver fe80::21c:42ff:fe00:18%nonexisting\n");
+        UnixResolverDnsServerAddressStreamProvider p =
+                new UnixResolverDnsServerAddressStreamProvider(f, null);
+
+        DnsServerAddressStream stream = p.nameServerAddressStream("somehost");
+        assertHostNameEquals("10.211.55.1", stream.next());
+    }
+
     private static void assertHostNameEquals(String expectedHostname, InetSocketAddress next) {
         assertEquals(expectedHostname, next.getHostString(), "unexpected hostname: " + next);
     }