Skip to content

Commit

Permalink
Ignore inline comments when parsing nameservers (netty#9894)
Browse files Browse the repository at this point in the history
Motivation:

The resolv.conf file may contain inline comments which should be ignored

Modifications:

- Detect if we have a comment after the ipaddress and if so skip it
- Add unit test

Result:

Fixes netty#9889
  • Loading branch information
normanmaurer authored Dec 18, 2019
1 parent 79d4e74 commit 68cfab4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
18 changes: 17 additions & 1 deletion common/src/main/java/io/netty/util/internal/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 &lt;{@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
* <a href="http://unicode.org/glossary/#surrogate_code_point">Surrogate Code Point</a>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://linux.die.net/man/5/resolver">/etc/resolv.conf</a> and
Expand Down Expand Up @@ -178,7 +179,20 @@ private static Map<String, DnsServerAddresses> 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('.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 68cfab4

Please sign in to comment.