diff --git a/common/src/main/java/io/netty/util/internal/StringUtil.java b/common/src/main/java/io/netty/util/internal/StringUtil.java index 2b107d71b21c..0890f2b3d8d3 100644 --- a/common/src/main/java/io/netty/util/internal/StringUtil.java +++ b/common/src/main/java/io/netty/util/internal/StringUtil.java @@ -542,7 +542,7 @@ public static boolean isNullOrEmpty(String s) { * * @param seq The string to search. * @param offset The offset to start searching at. - * @return the index of the first non-white space character or <{@code 0} if none was found. + * @return the index of the first non-white space character or <{@code -1} if none was found. */ public static int indexOfNonWhiteSpace(CharSequence seq, int offset) { for (; offset < seq.length(); ++offset) { @@ -553,6 +553,22 @@ public static int indexOfNonWhiteSpace(CharSequence seq, int offset) { return -1; } + /** + * Find the index of the first white space character in {@code s} starting at {@code offset}. + * + * @param seq The string to search. + * @param offset The offset to start searching at. + * @return the index of the first white space character or <{@code -1} if none was found. + */ + public static int indexOfWhiteSpace(CharSequence seq, int offset) { + for (; offset < seq.length(); ++offset) { + if (Character.isWhitespace(seq.charAt(offset))) { + return offset; + } + } + return -1; + } + /** * Determine if {@code c} lies within the range of values defined for * Surrogate Code Point. 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 b11f1f4c2045..4e49daebd4d5 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 @@ -37,6 +37,7 @@ import static io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.DNS_PORT; import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.StringUtil.indexOfNonWhiteSpace; +import static io.netty.util.internal.StringUtil.indexOfWhiteSpace; /** * Able to parse files such as /etc/resolv.conf and @@ -178,7 +179,20 @@ private static Map parse(File... etcResolverFiles) t throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL + " in file " + etcResolverFile + ". value: " + line); } - String maybeIP = line.substring(i); + String maybeIP; + int x = indexOfWhiteSpace(line, i); + if (x == -1) { + maybeIP = line.substring(i); + } else { + // ignore comments + int idx = indexOfNonWhiteSpace(line, x); + if (idx == -1 || line.charAt(idx) != '#') { + throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL + + " in file " + etcResolverFile + ". value: " + line); + } + maybeIP = line.substring(i, x); + } + // There may be a port appended onto the IP address so we attempt to extract it. if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) { i = maybeIP.lastIndexOf('.'); 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 ce5f0c07ae9e..6bb133a11aaa 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 @@ -188,6 +188,17 @@ private File buildFile(String contents) throws IOException { return f; } + @Test + public void ignoreComments() throws Exception { + File f = buildFile("domain linecorp.local\n" + + "nameserver 127.0.0.2 #somecomment\n"); + UnixResolverDnsServerAddressStreamProvider p = + new UnixResolverDnsServerAddressStreamProvider(f, null); + + DnsServerAddressStream stream = p.nameServerAddressStream("somehost"); + assertHostNameEquals("127.0.0.2", stream.next()); + } + private static void assertHostNameEquals(String expectedHostname, InetSocketAddress next) { assertEquals("unexpected hostname: " + next, expectedHostname, next.getHostString()); }