|
14 | 14 | * See the License for the specific language governing permissions and
|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 |
| -package java.net; |
| 17 | +package org.apache.ignite.internal.ducktest.tests.dns_failure_test; |
| 18 | + |
| 19 | +import org.apache.ignite.IgniteCheckedException; |
| 20 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 21 | +import org.apache.ignite.startup.cmdline.CommandLineStartup; |
| 22 | +import sun.net.spi.nameservice.NameService; |
18 | 23 |
|
19 | 24 | import java.io.File;
|
20 | 25 | import java.io.FileNotFoundException;
|
| 26 | +import java.net.InetAddress; |
| 27 | +import java.net.UnknownHostException; |
21 | 28 | import java.text.SimpleDateFormat;
|
22 | 29 | import java.util.Arrays;
|
23 | 30 | import java.util.Date;
|
| 31 | +import java.util.List; |
24 | 32 | import java.util.Scanner;
|
25 | 33 |
|
26 | 34 | /** */
|
27 |
| -public class DnsBlocker { |
| 35 | +public class DnsBlocker implements NameService { |
28 | 36 | /** */
|
29 | 37 | private static final String BLOCK_DNS_FILE = "/tmp/block_dns";
|
30 | 38 |
|
31 | 39 | /** */
|
32 |
| - public static final DnsBlocker INSTANCE = new DnsBlocker(); |
| 40 | + private final InetAddress loopback; |
33 | 41 |
|
34 |
| - /** */ |
35 |
| - private DnsBlocker() { |
36 |
| - // No-op. |
37 |
| - } |
| 42 | + /** Original NameService to use after unblock. */ |
| 43 | + private final NameService orig; |
38 | 44 |
|
39 | 45 | /**
|
40 |
| - * Check and block hostname resolve request if needed. |
41 |
| - * @param impl Implementation. |
42 |
| - * @param hostname Hostname. |
| 46 | + * @param orig Original NameService to use after unblock. |
43 | 47 | */
|
44 |
| - public void onHostResolve(InetAddressImpl impl, String hostname) throws UnknownHostException { |
45 |
| - if (!impl.loopbackAddress().getHostAddress().equals(hostname)) |
| 48 | + private DnsBlocker(NameService orig) { |
| 49 | + loopback = InetAddress.getLoopbackAddress(); |
| 50 | + this.orig = orig; |
| 51 | + } |
| 52 | + |
| 53 | + /** Installs DnsBlocker as main NameService to JVM. */ |
| 54 | + private static void install() throws IgniteCheckedException { |
| 55 | + List<NameService> nameSrvc = U.staticField(InetAddress.class, "nameServices"); |
| 56 | + |
| 57 | + NameService ns = new DnsBlocker(nameSrvc.get(0)); |
| 58 | + |
| 59 | + // Put the blocking name service ahead. |
| 60 | + nameSrvc.add(0, ns); |
| 61 | + |
| 62 | + System.out.println("Installed DnsBlocker as main NameService to JVM [ns=" + nameSrvc.size() + ']'); |
| 63 | + } |
| 64 | + |
| 65 | + /** */ |
| 66 | + public static void main(String[] args) throws Exception { |
| 67 | + install(); |
| 68 | + |
| 69 | + CommandLineStartup.main(args); |
| 70 | + } |
| 71 | + |
| 72 | + /** */ |
| 73 | + @Override public InetAddress[] lookupAllHostAddr(String hostname) throws UnknownHostException { |
| 74 | + if (!loopback.getHostAddress().equals(hostname)) |
46 | 75 | check(hostname);
|
| 76 | + |
| 77 | + return orig.lookupAllHostAddr(hostname); |
47 | 78 | }
|
48 | 79 |
|
49 |
| - /** |
50 |
| - * Check and block address resolve request if needed. |
51 |
| - * @param impl Implementation. |
52 |
| - * @param addr Address. |
53 |
| - */ |
54 |
| - public void onAddrResolve(InetAddressImpl impl, byte[] addr) throws UnknownHostException { |
55 |
| - if (!Arrays.equals(impl.loopbackAddress().getAddress(), addr)) |
| 80 | + /** */ |
| 81 | + @Override public String getHostByAddr(byte[] addr) throws UnknownHostException { |
| 82 | + if (!Arrays.equals(loopback.getAddress(), addr)) |
56 | 83 | check(InetAddress.getByAddress(addr).toString());
|
| 84 | + |
| 85 | + return orig.getHostByAddr(addr); |
57 | 86 | }
|
58 | 87 |
|
59 | 88 | /** */
|
|
0 commit comments