| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.constants; |
| 12 |
|
| 13 |
import java.net.Inet4Address; |
| 14 |
import java.net.Inet6Address; |
| 15 |
import java.net.InetAddress; |
| 16 |
import java.net.UnknownHostException; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.Map; |
| 19 |
import java.util.Random; |
| 20 |
|
| 21 |
public class DnsRootServer { |
| 22 |
|
| 23 |
private static final Map<Character, Inet4Address> IPV4_ROOT_SERVER_MAP = new HashMap<>(); |
| 24 |
|
| 25 |
private static final Map<Character, Inet6Address> IPV6_ROOT_SERVER_MAP = new HashMap<>(); |
| 26 |
|
| 27 |
protected static final Inet4Address[] IPV4_ROOT_SERVERS = new Inet4Address[] { |
| 28 |
rootServerInet4Address('a', 198, 41, 0, 4), |
| 29 |
rootServerInet4Address('b', 192, 228, 79, 201), |
| 30 |
rootServerInet4Address('c', 192, 33, 4, 12), |
| 31 |
rootServerInet4Address('d', 199, 7, 91 , 13), |
| 32 |
rootServerInet4Address('e', 192, 203, 230, 10), |
| 33 |
rootServerInet4Address('f', 192, 5, 5, 241), |
| 34 |
rootServerInet4Address('g', 192, 112, 36, 4), |
| 35 |
rootServerInet4Address('h', 198, 97, 190, 53), |
| 36 |
rootServerInet4Address('i', 192, 36, 148, 17), |
| 37 |
rootServerInet4Address('j', 192, 58, 128, 30), |
| 38 |
rootServerInet4Address('k', 193, 0, 14, 129), |
| 39 |
rootServerInet4Address('l', 199, 7, 83, 42), |
| 40 |
rootServerInet4Address('m', 202, 12, 27, 33), |
| 41 |
}; |
| 42 |
|
| 43 |
protected static final Inet6Address[] IPV6_ROOT_SERVERS = new Inet6Address[] { |
| 44 |
rootServerInet6Address('a', 0x2001, 0x0503, 0xba3e, 0x0000, 0x0000, 0x000, 0x0002, 0x0030), |
| 45 |
rootServerInet6Address('b', 0x2001, 0x0500, 0x0084, 0x0000, 0x0000, 0x000, 0x0000, 0x000b), |
| 46 |
rootServerInet6Address('c', 0x2001, 0x0500, 0x0002, 0x0000, 0x0000, 0x000, 0x0000, 0x000c), |
| 47 |
rootServerInet6Address('d', 0x2001, 0x0500, 0x002d, 0x0000, 0x0000, 0x000, 0x0000, 0x000d), |
| 48 |
rootServerInet6Address('f', 0x2001, 0x0500, 0x002f, 0x0000, 0x0000, 0x000, 0x0000, 0x000f), |
| 49 |
rootServerInet6Address('h', 0x2001, 0x0500, 0x0001, 0x0000, 0x0000, 0x000, 0x0000, 0x0053), |
| 50 |
rootServerInet6Address('i', 0x2001, 0x07fe, 0x0000, 0x0000, 0x0000, 0x000, 0x0000, 0x0053), |
| 51 |
rootServerInet6Address('j', 0x2001, 0x0503, 0x0c27, 0x0000, 0x0000, 0x000, 0x0002, 0x0030), |
| 52 |
rootServerInet6Address('l', 0x2001, 0x0500, 0x0003, 0x0000, 0x0000, 0x000, 0x0000, 0x0042), |
| 53 |
rootServerInet6Address('m', 0x2001, 0x0dc3, 0x0000, 0x0000, 0x0000, 0x000, 0x0000, 0x0035), |
| 54 |
}; |
| 55 |
|
| 56 |
private static Inet4Address rootServerInet4Address(char rootServerId, int addr0, int addr1, int addr2, int addr3) { |
| 57 |
Inet4Address inetAddress; |
| 58 |
String name = rootServerId + ".root-servers.net"; |
| 59 |
try { |
| 60 |
inetAddress = (Inet4Address) InetAddress.getByAddress(name, new byte[] { (byte) addr0, (byte) addr1, (byte) addr2, |
| 61 |
(byte) addr3 }); |
| 62 |
IPV4_ROOT_SERVER_MAP.put(rootServerId, inetAddress); |
| 63 |
} catch (UnknownHostException e) { |
| 64 |
// This should never happen, if it does it's our fault! |
| 65 |
throw new RuntimeException(e); |
| 66 |
} |
| 67 |
|
| 68 |
return inetAddress; |
| 69 |
} |
| 70 |
|
| 71 |
private static Inet6Address rootServerInet6Address(char rootServerId, int addr0, int addr1, int addr2, int addr3, int addr4, int addr5, int addr6, int addr7) { |
| 72 |
Inet6Address inetAddress; |
| 73 |
String name = rootServerId + ".root-servers.net"; |
| 74 |
try { |
| 75 |
inetAddress = (Inet6Address) InetAddress.getByAddress(name, new byte[] { |
| 76 |
// @formatter:off |
| 77 |
(byte) (addr0 >> 8), (byte) addr0, (byte) (addr1 >> 8), (byte) addr1, |
| 78 |
(byte) (addr2 >> 8), (byte) addr2, (byte) (addr3 >> 8), (byte) addr3, |
| 79 |
(byte) (addr4 >> 8), (byte) addr4, (byte) (addr5 >> 8), (byte) addr5, |
| 80 |
(byte) (addr6 >> 8), (byte) addr6, (byte) (addr7 >> 8), (byte) addr7 |
| 81 |
// @formatter:on |
| 82 |
}); |
| 83 |
IPV6_ROOT_SERVER_MAP.put(rootServerId, inetAddress); |
| 84 |
} catch (UnknownHostException e) { |
| 85 |
// This should never happen, if it does it's our fault! |
| 86 |
throw new RuntimeException(e); |
| 87 |
} |
| 88 |
return inetAddress; |
| 89 |
} |
| 90 |
|
| 91 |
public static Inet4Address getRandomIpv4RootServer(Random random) { |
| 92 |
return IPV4_ROOT_SERVERS[random.nextInt(IPV4_ROOT_SERVERS.length)]; |
| 93 |
} |
| 94 |
|
| 95 |
public static Inet6Address getRandomIpv6RootServer(Random random) { |
| 96 |
return IPV6_ROOT_SERVERS[random.nextInt(IPV6_ROOT_SERVERS.length)]; |
| 97 |
} |
| 98 |
|
| 99 |
public static Inet4Address getIpv4RootServerById(char id) { |
| 100 |
return IPV4_ROOT_SERVER_MAP.get(id); |
| 101 |
} |
| 102 |
|
| 103 |
public static Inet6Address getIpv6RootServerById(char id) { |
| 104 |
return IPV6_ROOT_SERVER_MAP.get(id); |
| 105 |
} |
| 106 |
|
| 107 |
} |