1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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);
}
|