Skip to content

Commit 8d4db05

Browse files
slandellenormanmaurer
authored andcommitted
Have hosts file support for DnsNameResolver, close netty#4074
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.
1 parent 4e467f5 commit 8d4db05

File tree

14 files changed

+973
-421
lines changed

14 files changed

+973
-421
lines changed

common/src/main/java/io/netty/util/internal/ObjectUtil.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,48 @@ public static <T> T checkNotNull(T arg, String text) {
3232
}
3333
return arg;
3434
}
35+
36+
/**
37+
* Checks that the given argument is strictly positive. If it is, throws {@link IllegalArgumentException}.
38+
* Otherwise, returns the argument.
39+
*/
40+
public static int checkPositive(int i, String name) {
41+
if (i <= 0) {
42+
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
43+
}
44+
return i;
45+
}
46+
47+
/**
48+
* Checks that the given argument is strictly positive. If it is, throws {@link IllegalArgumentException}.
49+
* Otherwise, returns the argument.
50+
*/
51+
public static long checkPositive(long i, String name) {
52+
if (i <= 0) {
53+
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
54+
}
55+
return i;
56+
}
57+
58+
/**
59+
* Checks that the given argument is positive or zero. If it is, throws {@link IllegalArgumentException}.
60+
* Otherwise, returns the argument.
61+
*/
62+
public static int checkPositiveOrZero(int i, String name) {
63+
if (i < 0) {
64+
throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
65+
}
66+
return i;
67+
}
68+
69+
/**
70+
* Checks that the given argument is neither null nor empty.
71+
* If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
72+
* Otherwise, returns the argument.
73+
*/
74+
public static <T> T[] checkNonEmpty(T[] array, String name) {
75+
checkNotNull(array, name);
76+
checkPositive(array.length, name + ".length");
77+
return array;
78+
}
3579
}

resolver-dns/src/main/java/io/netty/resolver/dns/DnsAddressResolverGroup.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ protected AddressResolver<InetSocketAddress> newResolver(
8181
EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
8282
InetSocketAddress localAddress, DnsServerAddresses nameServerAddresses) throws Exception {
8383

84-
return new DnsNameResolver(eventLoop, channelFactory, localAddress, nameServerAddresses)
84+
return new DnsNameResolverBuilder(eventLoop)
85+
.channelFactory(channelFactory)
86+
.localAddress(localAddress)
87+
.nameServerAddresses(nameServerAddresses)
88+
.build()
8589
.asAddressResolver();
8690
}
8791
}

0 commit comments

Comments
 (0)