Skip to content

Commit

Permalink
Have hosts file support for DnsNameResolver, close netty#4074
Browse files Browse the repository at this point in the history
Motivation:

On contrary to `DefaultNameResolver`, `DnsNameResolver` doesn't currently honor hosts file.

Modifications:

* Introduce `HostsFileParser` that parses `/etc/hosts` or `C:\Windows\system32\drivers\etc\hosts` depending on the platform
* Introduce `HostsFileEntriesResolver` that uses the former to resolve host names
* Make `DnsNameResolver` check his `HostsFileEntriesResolver` prior to trying to resolve names against the DNS server
* Introduce `DnsNameResolverBuilder` so we now have a builder for `DnsNameResolver`s
* Additionally introduce a `CompositeNameResolver` that takes several `NameResolver`s and tries to resolve names by delegating sequentially
* Change `DnsNameResolver.asAddressResolver` to return a composite and honor hosts file

Result:

Hosts file support when using `DnsNameResolver`.
Consistent behavior with JDK implementation.
  • Loading branch information
slandelle authored and normanmaurer committed Dec 17, 2015
1 parent 4e467f5 commit 8d4db05
Show file tree
Hide file tree
Showing 14 changed files with 973 additions and 421 deletions.
44 changes: 44 additions & 0 deletions common/src/main/java/io/netty/util/internal/ObjectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,48 @@ public static <T> T checkNotNull(T arg, String text) {
}
return arg;
}

/**
* Checks that the given argument is strictly positive. If it is, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static int checkPositive(int i, String name) {
if (i <= 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
}
return i;
}

/**
* Checks that the given argument is strictly positive. If it is, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static long checkPositive(long i, String name) {
if (i <= 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
}
return i;
}

/**
* Checks that the given argument is positive or zero. If it is, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static int checkPositiveOrZero(int i, String name) {
if (i < 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
}
return i;
}

/**
* Checks that the given argument is neither null nor empty.
* If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static <T> T[] checkNonEmpty(T[] array, String name) {
checkNotNull(array, name);
checkPositive(array.length, name + ".length");
return array;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ protected AddressResolver<InetSocketAddress> newResolver(
EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
InetSocketAddress localAddress, DnsServerAddresses nameServerAddresses) throws Exception {

return new DnsNameResolver(eventLoop, channelFactory, localAddress, nameServerAddresses)
return new DnsNameResolverBuilder(eventLoop)
.channelFactory(channelFactory)
.localAddress(localAddress)
.nameServerAddresses(nameServerAddresses)
.build()
.asAddressResolver();
}
}
Loading

0 comments on commit 8d4db05

Please sign in to comment.