summaryrefslogtreecommitdiff
path: root/NET/worlds/network/DNSLookup.java
blob: 026b64171e5679d5d4a9f7853ce30acb5d36f62a (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*     */ package NET.worlds.network;
/*     */ 
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.net.UnknownHostException;
/*     */ import java.util.Hashtable;
/*     */ import java.util.StringTokenizer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DNSLookup
/*     */   implements Runnable
/*     */ {
/*     */   private static final int defaultTimeout = 30;
/*  69 */   private static Hashtable<String, String[]> cache = new Hashtable();
/*     */   
/*     */   private String hostName;
/*     */   
/*     */   private String[] dottedNames;
/*     */   
/*     */   private int timeout;
/*     */   
/*     */ 
/*     */   public static URL lookup(URL url)
/*     */     throws MalformedURLException, UnknownHostException
/*     */   {
/*  81 */     return lookup(url, 30);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static URL lookupAll(URL url)
/*     */     throws MalformedURLException, UnknownHostException
/*     */   {
/*  90 */     return lookupAll(url, 30);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static URL lookup(URL url, int timeout)
/*     */     throws MalformedURLException, UnknownHostException
/*     */   {
/*  99 */     String hostName = url.getHost();
/* 100 */     if (hostName == null) {
/* 101 */       return url;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 113 */     return new URL(url.getProtocol(), hostName, url.getPort(), url
/* 114 */       .getFile());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static URL lookupAll(URL url, int timeout)
/*     */     throws MalformedURLException, UnknownHostException
/*     */   {
/* 124 */     String hostName = url.getHost();
/* 125 */     if (hostName == null) {
/* 126 */       return url;
/*     */     }
/* 128 */     String[] addresses = lookupAll(hostName, timeout);
/* 129 */     String hosts = "";
/* 130 */     for (int i = 0; i < addresses.length; i++) {
/* 131 */       if (i != 0)
/* 132 */         hosts = hosts + ";";
/* 133 */       hosts = hosts + addresses[i];
/*     */     }
/* 135 */     return new URL(url.getProtocol(), hosts, url.getPort(), url.getFile());
/*     */   }
/*     */   
/*     */   public static String lookup(String hostName) throws UnknownHostException
/*     */   {
/* 140 */     return lookup(hostName, 30);
/*     */   }
/*     */   
/*     */   public static String lookup(String hostName, int timeout)
/*     */     throws UnknownHostException
/*     */   {
/* 146 */     if (isDotted(hostName))
/* 147 */       return hostName;
/* 148 */     return lookupAllCommon(hostName, timeout)[0];
/*     */   }
/*     */   
/*     */   public static String[] lookupAll(String hostName)
/*     */     throws UnknownHostException
/*     */   {
/* 154 */     return lookupAll(hostName, 30);
/*     */   }
/*     */   
/*     */   public static String[] lookupAll(String hostName, int timeout)
/*     */     throws UnknownHostException
/*     */   {
/* 160 */     if (isDotted(hostName)) {
/* 161 */       String[] names = new String[1];
/* 162 */       names[0] = hostName;
/* 163 */       return names;
/*     */     }
/* 165 */     return lookupAllCommon(hostName, timeout);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String[] lookupAllCommon(String hostName, int timeout)
/*     */     throws UnknownHostException
/*     */   {
/* 174 */     String[] dotted = (String[])cache.get(hostName);
/* 175 */     if (dotted == null) {
/* 176 */       dotted = new DNSLookup(hostName, timeout).getDottedNames();
/* 177 */       if (dotted == null)
/* 178 */         throw new UnknownHostException(hostName);
/* 179 */       cache.put(hostName, dotted);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 185 */     return dotted;
/*     */   }
/*     */   
/*     */   private DNSLookup(String hostName, int timeout) {
/* 189 */     this.hostName = hostName;
/* 190 */     this.timeout = timeout;
/* 191 */     if (timeout != 0) {
/* 192 */       Thread t = new Thread(this);
/* 193 */       t.setDaemon(true);
/* 194 */       t.start();
/*     */     } else {
/* 196 */       run();
/*     */     }
/*     */   }
/*     */   
/*     */   public void run() {
/* 201 */     this.dottedNames = gethostbyname(this.hostName);
/* 202 */     if (this.timeout != 0) {
/* 203 */       synchronized (this) {
/* 204 */         notify();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private synchronized String[] getDottedNames() {
/* 210 */     if ((this.dottedNames == null) && (this.timeout != 0)) {
/*     */       try {
/* 212 */         wait(this.timeout * 1000);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {}
/*     */     }
/* 216 */     return this.dottedNames;
/*     */   }
/*     */   
/*     */ 
/*     */   private static boolean isDotted(String name)
/*     */   {
/* 222 */     StringTokenizer tok = new StringTokenizer(name, ".");
/* 223 */     if (tok.countTokens() != 4)
/* 224 */       return false;
/* 225 */     for (int i = 0; i < 4; i++) {
/*     */       try {
/* 227 */         int val = Integer.parseInt(tok.nextToken());
/* 228 */         if ((val < 0) || (val > 255))
/* 229 */           return false;
/*     */       } catch (NumberFormatException nfe) {
/* 231 */         return false;
/*     */       }
/*     */     }
/* 234 */     return true;
/*     */   }
/*     */   
/*     */   private static native String[] gethostbyname(String paramString);
/*     */ }


/* Location:              C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\network\DNSLookup.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */