diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/network/DNSLookup.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/network/DNSLookup.java')
| -rw-r--r-- | NET/worlds/network/DNSLookup.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/NET/worlds/network/DNSLookup.java b/NET/worlds/network/DNSLookup.java new file mode 100644 index 0000000..878c63d --- /dev/null +++ b/NET/worlds/network/DNSLookup.java @@ -0,0 +1,132 @@ +package NET.worlds.network; + +import java.net.MalformedURLException; +import java.net.UnknownHostException; +import java.util.Hashtable; +import java.util.StringTokenizer; + +public class DNSLookup implements Runnable { + private static final int defaultTimeout = 30; + private static Hashtable<String, String[]> cache = new Hashtable<String, String[]>(); + private String hostName; + private String[] dottedNames; + private int timeout; + + public static java.net.URL lookup(java.net.URL url) throws MalformedURLException, UnknownHostException { + return lookup(url, 30); + } + + public static java.net.URL lookupAll(java.net.URL url) throws MalformedURLException, UnknownHostException { + return lookupAll(url, 30); + } + + public static java.net.URL lookup(java.net.URL url, int timeout) throws MalformedURLException, UnknownHostException { + String hostName = url.getHost(); + return hostName == null ? url : new java.net.URL(url.getProtocol(), hostName, url.getPort(), url.getFile()); + } + + public static java.net.URL lookupAll(java.net.URL url, int timeout) throws MalformedURLException, UnknownHostException { + String hostName = url.getHost(); + if (hostName == null) { + return url; + } else { + String[] addresses = lookupAll(hostName, timeout); + String hosts = ""; + + for (int i = 0; i < addresses.length; i++) { + if (i != 0) { + hosts = hosts + ";"; + } + + hosts = hosts + addresses[i]; + } + + return new java.net.URL(url.getProtocol(), hosts, url.getPort(), url.getFile()); + } + } + + public static String lookup(String hostName) throws UnknownHostException { + return lookup(hostName, 30); + } + + public static String lookup(String hostName, int timeout) throws UnknownHostException { + return isDotted(hostName) ? hostName : lookupAllCommon(hostName, timeout)[0]; + } + + public static String[] lookupAll(String hostName) throws UnknownHostException { + return lookupAll(hostName, 30); + } + + public static String[] lookupAll(String hostName, int timeout) throws UnknownHostException { + return isDotted(hostName) ? new String[]{hostName} : lookupAllCommon(hostName, timeout); + } + + private static String[] lookupAllCommon(String hostName, int timeout) throws UnknownHostException { + String[] dotted = cache.get(hostName); + if (dotted == null) { + dotted = new DNSLookup(hostName, timeout).getDottedNames(); + if (dotted == null) { + throw new UnknownHostException(hostName); + } + + cache.put(hostName, dotted); + } + + return dotted; + } + + private DNSLookup(String hostName, int timeout) { + this.hostName = hostName; + this.timeout = timeout; + if (timeout != 0) { + Thread t = new Thread(this); + t.setDaemon(true); + t.start(); + } else { + this.run(); + } + } + + @Override + public void run() { + this.dottedNames = gethostbyname(this.hostName); + if (this.timeout != 0) { + synchronized (this) { + this.notify(); + } + } + } + + private synchronized String[] getDottedNames() { + if (this.dottedNames == null && this.timeout != 0) { + try { + this.wait(this.timeout * 1000); + } catch (InterruptedException var2) { + } + } + + return this.dottedNames; + } + + private static boolean isDotted(String name) { + StringTokenizer tok = new StringTokenizer(name, "."); + if (tok.countTokens() != 4) { + return false; + } else { + for (int i = 0; i < 4; i++) { + try { + int val = Integer.parseInt(tok.nextToken()); + if (val < 0 || val > 255) { + return false; + } + } catch (NumberFormatException var4) { + return false; + } + } + + return true; + } + } + + private static native String[] gethostbyname(String var0); +} |