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 cache = new Hashtable(); 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); }