2014-09-19 15:36:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
2019-02-04 10:32:25 +01:00
|
|
|
import static java.util.Objects.requireNonNull;
|
|
|
|
|
2016-04-12 14:22:41 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Provides an infinite sequence of DNS server addresses to {@link DnsNameResolver}.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2014-09-19 15:36:32 +02:00
|
|
|
@SuppressWarnings("IteratorNextCanNotThrowNoSuchElementException")
|
2015-08-19 04:51:15 +02:00
|
|
|
public abstract class DnsServerAddresses {
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
2017-03-31 22:52:46 +02:00
|
|
|
* @deprecated Use {@link DefaultDnsServerAddressStreamProvider#defaultAddressList()}.
|
|
|
|
* <p>
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the list of the system DNS server addresses. If it failed to retrieve the list of the system DNS server
|
2014-09-19 15:36:32 +02:00
|
|
|
* addresses from the environment, it will return {@code "8.8.8.8"} and {@code "8.8.4.4"}, the addresses of the
|
2015-08-19 04:51:15 +02:00
|
|
|
* Google public DNS servers.
|
|
|
|
*/
|
2017-03-31 22:52:46 +02:00
|
|
|
@Deprecated
|
2015-08-19 04:51:15 +02:00
|
|
|
public static List<InetSocketAddress> defaultAddressList() {
|
2017-03-31 22:52:46 +02:00
|
|
|
return DefaultDnsServerAddressStreamProvider.defaultAddressList();
|
2015-08-19 04:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-31 22:52:46 +02:00
|
|
|
* @deprecated Use {@link DefaultDnsServerAddressStreamProvider#defaultAddresses()}.
|
|
|
|
* <p>
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the system DNS server addresses sequentially. If it failed to
|
|
|
|
* retrieve the list of the system DNS server addresses from the environment, it will use {@code "8.8.8.8"} and
|
|
|
|
* {@code "8.8.4.4"}, the addresses of the Google public DNS servers.
|
|
|
|
* <p>
|
|
|
|
* This method has the same effect with the following code:
|
2014-09-19 15:36:32 +02:00
|
|
|
* <pre>
|
2015-08-19 04:51:15 +02:00
|
|
|
* DnsServerAddresses.sequential(DnsServerAddresses.defaultAddressList());
|
2014-09-19 15:36:32 +02:00
|
|
|
* </pre>
|
2015-08-19 04:51:15 +02:00
|
|
|
* </p>
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2017-03-31 22:52:46 +02:00
|
|
|
@Deprecated
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses defaultAddresses() {
|
2017-03-31 22:52:46 +02:00
|
|
|
return DefaultDnsServerAddressStreamProvider.defaultAddresses();
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code addresses} sequentially. Once the
|
|
|
|
* last address is yielded, it will start again from the first address.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses sequential(Iterable<? extends InetSocketAddress> addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return sequential0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code addresses} sequentially. Once the
|
|
|
|
* last address is yielded, it will start again from the first address.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses sequential(InetSocketAddress... addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return sequential0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
private static DnsServerAddresses sequential0(final List<InetSocketAddress> addresses) {
|
|
|
|
if (addresses.size() == 1) {
|
|
|
|
return singleton(addresses.get(0));
|
2015-08-19 04:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new DefaultDnsServerAddresses("sequential", addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
2015-08-19 04:51:15 +02:00
|
|
|
public DnsServerAddressStream stream() {
|
|
|
|
return new SequentialDnsServerAddressStream(addresses, 0);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code address} in a shuffled order. Once all
|
|
|
|
* addresses are yielded, the addresses are shuffled again.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses shuffled(Iterable<? extends InetSocketAddress> addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return shuffled0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code addresses} in a shuffled order. Once all
|
|
|
|
* addresses are yielded, the addresses are shuffled again.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses shuffled(InetSocketAddress... addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return shuffled0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
private static DnsServerAddresses shuffled0(List<InetSocketAddress> addresses) {
|
|
|
|
if (addresses.size() == 1) {
|
|
|
|
return singleton(addresses.get(0));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
return new DefaultDnsServerAddresses("shuffled", addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
2015-08-19 04:51:15 +02:00
|
|
|
public DnsServerAddressStream stream() {
|
|
|
|
return new ShuffledDnsServerAddressStream(addresses);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code addresses} in a rotational sequential
|
|
|
|
* order. It is similar to {@link #sequential(Iterable)}, but each {@link DnsServerAddressStream} starts from
|
|
|
|
* a different starting point. For example, the first {@link #stream()} will start from the first address, the
|
|
|
|
* second one will start from the second address, and so on.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses rotational(Iterable<? extends InetSocketAddress> addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return rotational0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields the specified {@code addresses} in a rotational sequential
|
|
|
|
* order. It is similar to {@link #sequential(Iterable)}, but each {@link DnsServerAddressStream} starts from
|
|
|
|
* a different starting point. For example, the first {@link #stream()} will start from the first address, the
|
|
|
|
* second one will start from the second address, and so on.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses rotational(InetSocketAddress... addresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return rotational0(sanitize(addresses));
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
private static DnsServerAddresses rotational0(List<InetSocketAddress> addresses) {
|
|
|
|
if (addresses.size() == 1) {
|
|
|
|
return singleton(addresses.get(0));
|
2015-08-19 04:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new RotationalDnsServerAddresses(addresses);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-19 04:51:15 +02:00
|
|
|
* Returns the {@link DnsServerAddresses} that yields only a single {@code address}.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-08-19 04:51:15 +02:00
|
|
|
public static DnsServerAddresses singleton(final InetSocketAddress address) {
|
2019-02-04 10:32:25 +01:00
|
|
|
requireNonNull(address, "address");
|
2014-09-19 15:36:32 +02:00
|
|
|
if (address.isUnresolved()) {
|
|
|
|
throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + address);
|
|
|
|
}
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
return new SingletonDnsServerAddresses(address);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
private static List<InetSocketAddress> sanitize(Iterable<? extends InetSocketAddress> addresses) {
|
2019-02-04 10:32:25 +01:00
|
|
|
requireNonNull(addresses, "addresses");
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
final List<InetSocketAddress> list;
|
|
|
|
if (addresses instanceof Collection) {
|
2019-01-22 16:07:26 +01:00
|
|
|
list = new ArrayList<>(((Collection<?>) addresses).size());
|
2014-09-19 15:36:32 +02:00
|
|
|
} else {
|
2019-01-22 16:07:26 +01:00
|
|
|
list = new ArrayList<>(4);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (InetSocketAddress a : addresses) {
|
|
|
|
if (a == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (a.isUnresolved()) {
|
|
|
|
throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + a);
|
|
|
|
}
|
|
|
|
list.add(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list.isEmpty()) {
|
2015-08-19 04:51:15 +02:00
|
|
|
throw new IllegalArgumentException("empty addresses");
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
return list;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
private static List<InetSocketAddress> sanitize(InetSocketAddress[] addresses) {
|
2019-02-04 10:32:25 +01:00
|
|
|
requireNonNull(addresses, "addresses");
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2019-01-22 16:07:26 +01:00
|
|
|
List<InetSocketAddress> list = new ArrayList<>(addresses.length);
|
2014-09-19 15:36:32 +02:00
|
|
|
for (InetSocketAddress a: addresses) {
|
|
|
|
if (a == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (a.isUnresolved()) {
|
|
|
|
throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + a);
|
|
|
|
}
|
|
|
|
list.add(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list.isEmpty()) {
|
2018-08-22 17:49:22 +02:00
|
|
|
return DefaultDnsServerAddressStreamProvider.defaultAddressList();
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:49:22 +02:00
|
|
|
return list;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
/**
|
|
|
|
* Starts a new infinite stream of DNS server addresses. This method is invoked by {@link DnsNameResolver} on every
|
2016-03-22 16:27:19 +01:00
|
|
|
* uncached {@link DnsNameResolver#resolve(String)}or {@link DnsNameResolver#resolveAll(String)}.
|
2015-08-19 04:51:15 +02:00
|
|
|
*/
|
|
|
|
public abstract DnsServerAddressStream stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|