1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
package javax.jmdns.impl; |
6 |
|
|
7 |
|
import java.io.IOException; |
8 |
|
import java.net.DatagramPacket; |
9 |
|
import java.net.Inet4Address; |
10 |
|
import java.net.Inet6Address; |
11 |
|
import java.net.InetAddress; |
12 |
|
import java.net.NetworkInterface; |
13 |
|
import java.net.UnknownHostException; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Collection; |
16 |
|
import java.util.List; |
17 |
|
import java.util.logging.Level; |
18 |
|
import java.util.logging.Logger; |
19 |
|
|
20 |
|
import javax.jmdns.NetworkTopologyDiscovery; |
21 |
|
import javax.jmdns.impl.constants.DNSConstants; |
22 |
|
import javax.jmdns.impl.constants.DNSRecordClass; |
23 |
|
import javax.jmdns.impl.constants.DNSRecordType; |
24 |
|
import javax.jmdns.impl.constants.DNSState; |
25 |
|
import javax.jmdns.impl.tasks.DNSTask; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
@author |
31 |
|
|
|
|
| 55.7% |
Uncovered Elements: 101 (228) |
Complexity: 82 |
Complexity Density: 0.61 |
|
32 |
|
public class HostInfo implements DNSStatefulObject { |
33 |
|
private static Logger logger = Logger.getLogger(HostInfo.class.getName()); |
34 |
|
|
35 |
|
protected String _name; |
36 |
|
|
37 |
|
protected InetAddress _address; |
38 |
|
|
39 |
|
protected NetworkInterface _interfaze; |
40 |
|
|
41 |
|
private final HostInfoState _state; |
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.5 |
|
43 |
|
private final static class HostInfoState extends DNSStatefulObject.DefaultImplementation { |
44 |
|
|
45 |
|
private static final long serialVersionUID = -8191476803620402088L; |
46 |
|
|
47 |
|
|
48 |
|
@param |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
50 |
29
|
public HostInfoState(JmDNSImpl dns) {... |
51 |
29
|
super(); |
52 |
29
|
this.setDns(dns); |
53 |
|
} |
54 |
|
|
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
|
@param |
59 |
|
|
60 |
|
@param |
61 |
|
|
62 |
|
@param |
63 |
|
|
64 |
|
@return |
65 |
|
|
|
|
| 54.8% |
Uncovered Elements: 19 (42) |
Complexity: 13 |
Complexity Density: 0.5 |
|
66 |
29
|
public static HostInfo newHostInfo(InetAddress address, JmDNSImpl dns, String jmdnsName) {... |
67 |
29
|
HostInfo localhost = null; |
68 |
29
|
String aName = ""; |
69 |
29
|
InetAddress addr = address; |
70 |
29
|
try { |
71 |
29
|
if (addr == null) { |
72 |
27
|
String ip = System.getProperty("net.mdns.interface"); |
73 |
27
|
if (ip != null) { |
74 |
0
|
addr = InetAddress.getByName(ip); |
75 |
|
} else { |
76 |
27
|
addr = InetAddress.getLocalHost(); |
77 |
27
|
if (addr.isLoopbackAddress()) { |
78 |
|
|
79 |
0
|
InetAddress[] addresses = NetworkTopologyDiscovery.Factory.getInstance().getInetAddresses(); |
80 |
0
|
if (addresses.length > 0) { |
81 |
0
|
addr = addresses[0]; |
82 |
|
} |
83 |
|
} |
84 |
|
} |
85 |
27
|
aName = addr.getHostName(); |
86 |
27
|
if (addr.isLoopbackAddress()) { |
87 |
0
|
logger.warning("Could not find any address beside the loopback."); |
88 |
|
} |
89 |
|
} else { |
90 |
2
|
aName = addr.getHostName(); |
91 |
|
} |
92 |
29
|
if (aName.contains("in-addr.arpa") || (aName.equals(addr.getHostAddress()))) { |
93 |
0
|
aName = ((jmdnsName != null) && (jmdnsName.length() > 0) ? jmdnsName : addr.getHostAddress()); |
94 |
|
} |
95 |
|
} catch (final IOException e) { |
96 |
0
|
logger.log(Level.WARNING, "Could not intialize the host network interface on " + address + "because of an error: " + e.getMessage(), e); |
97 |
|
|
98 |
0
|
addr = loopbackAddress(); |
99 |
0
|
aName = ((jmdnsName != null) && (jmdnsName.length() > 0) ? jmdnsName : "computer"); |
100 |
|
} |
101 |
|
|
102 |
29
|
aName = aName.replace('.', '-'); |
103 |
29
|
aName += ".local."; |
104 |
29
|
localhost = new HostInfo(addr, aName, dns); |
105 |
29
|
return localhost; |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
108 |
0
|
private static InetAddress loopbackAddress() {... |
109 |
0
|
try { |
110 |
0
|
return InetAddress.getByName(null); |
111 |
|
} catch (UnknownHostException exception) { |
112 |
0
|
return null; |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
private int hostNameCount; |
120 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
121 |
29
|
private HostInfo(final InetAddress address, final String name, final JmDNSImpl dns) {... |
122 |
29
|
super(); |
123 |
29
|
this._state = new HostInfoState(dns); |
124 |
29
|
this._address = address; |
125 |
29
|
this._name = name; |
126 |
29
|
if (address != null) { |
127 |
29
|
try { |
128 |
29
|
_interfaze = NetworkInterface.getByInetAddress(address); |
129 |
|
} catch (Exception exception) { |
130 |
0
|
logger.log(Level.SEVERE, "LocalHostInfo() exception ", exception); |
131 |
|
} |
132 |
|
} |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
1257
|
public String getName() {... |
136 |
1267
|
return _name; |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
2038
|
public InetAddress getInetAddress() {... |
140 |
2065
|
return _address; |
141 |
|
} |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
143 |
20
|
Inet4Address getInet4Address() {... |
144 |
20
|
if (this.getInetAddress() instanceof Inet4Address) { |
145 |
19
|
return (Inet4Address) _address; |
146 |
|
} |
147 |
1
|
return null; |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
150 |
20
|
Inet6Address getInet6Address() {... |
151 |
20
|
if (this.getInetAddress() instanceof Inet6Address) { |
152 |
1
|
return (Inet6Address) _address; |
153 |
|
} |
154 |
19
|
return null; |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
157 |
58
|
public NetworkInterface getInterface() {... |
158 |
58
|
return _interfaze; |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
161 |
436
|
public boolean conflictWithRecord(DNSRecord.Address record) {... |
162 |
451
|
DNSRecord.Address hostAddress = this.getDNSAddressRecord(record.getRecordType(), record.isUnique(), DNSConstants.DNS_TTL); |
163 |
453
|
if (hostAddress != null) { |
164 |
360
|
return hostAddress.sameType(record) && hostAddress.sameName(record) && (!hostAddress.sameValue(record)); |
165 |
|
} |
166 |
94
|
return false; |
167 |
|
} |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
169 |
0
|
synchronized String incrementHostName() {... |
170 |
0
|
hostNameCount++; |
171 |
0
|
int plocal = _name.indexOf(".local."); |
172 |
0
|
int punder = _name.lastIndexOf('-'); |
173 |
0
|
_name = _name.substring(0, (punder == -1 ? plocal : punder)) + "-" + hostNameCount + ".local."; |
174 |
0
|
return _name; |
175 |
|
} |
176 |
|
|
|
|
| 64.7% |
Uncovered Elements: 6 (17) |
Complexity: 7 |
Complexity Density: 0.78 |
|
177 |
621
|
boolean shouldIgnorePacket(DatagramPacket packet) {... |
178 |
647
|
boolean result = false; |
179 |
643
|
if (this.getInetAddress() != null) { |
180 |
643
|
InetAddress from = packet.getAddress(); |
181 |
654
|
if (from != null) { |
182 |
644
|
if (from.isLinkLocalAddress() && (!this.getInetAddress().isLinkLocalAddress())) { |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
0
|
result = true; |
188 |
|
} |
189 |
641
|
if (from.isLoopbackAddress() && (!this.getInetAddress().isLoopbackAddress())) { |
190 |
|
|
191 |
0
|
result = true; |
192 |
|
} |
193 |
|
} |
194 |
|
} |
195 |
645
|
return result; |
196 |
|
} |
197 |
|
|
|
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 4 |
Complexity Density: 0.5 |
|
198 |
446
|
DNSRecord.Address getDNSAddressRecord(DNSRecordType type, boolean unique, int ttl) {... |
199 |
432
|
switch (type) { |
200 |
336
|
case TYPE_A: |
201 |
343
|
return this.getDNS4AddressRecord(unique, ttl); |
202 |
0
|
case TYPE_A6: |
203 |
102
|
case TYPE_AAAA: |
204 |
101
|
return this.getDNS6AddressRecord(unique, ttl); |
205 |
0
|
default: |
206 |
|
} |
207 |
0
|
return null; |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 4 |
Complexity Density: 1.33 |
|
210 |
511
|
private DNSRecord.Address getDNS4AddressRecord(boolean unique, int ttl) {... |
211 |
519
|
if ((this.getInetAddress() instanceof Inet4Address) || ((this.getInetAddress() instanceof Inet6Address) && (((Inet6Address) this.getInetAddress()).isIPv4CompatibleAddress()))) { |
212 |
506
|
return new DNSRecord.IPv4Address(this.getName(), DNSRecordClass.CLASS_IN, unique, ttl, this.getInetAddress()); |
213 |
|
} |
214 |
8
|
return null; |
215 |
|
} |
216 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
217 |
269
|
private DNSRecord.Address getDNS6AddressRecord(boolean unique, int ttl) {... |
218 |
271
|
if (this.getInetAddress() instanceof Inet6Address) { |
219 |
16
|
return new DNSRecord.IPv6Address(this.getName(), DNSRecordClass.CLASS_IN, unique, ttl, this.getInetAddress()); |
220 |
|
} |
221 |
253
|
return null; |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 4 |
Complexity Density: 0.5 |
|
224 |
0
|
DNSRecord.Pointer getDNSReverseAddressRecord(DNSRecordType type, boolean unique, int ttl) {... |
225 |
0
|
switch (type) { |
226 |
0
|
case TYPE_A: |
227 |
0
|
return this.getDNS4ReverseAddressRecord(unique, ttl); |
228 |
0
|
case TYPE_A6: |
229 |
0
|
case TYPE_AAAA: |
230 |
0
|
return this.getDNS6ReverseAddressRecord(unique, ttl); |
231 |
0
|
default: |
232 |
|
} |
233 |
0
|
return null; |
234 |
|
} |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
236 |
0
|
private DNSRecord.Pointer getDNS4ReverseAddressRecord(boolean unique, int ttl) {... |
237 |
0
|
if (this.getInetAddress() instanceof Inet4Address) { |
238 |
0
|
return new DNSRecord.Pointer(this.getInetAddress().getHostAddress() + ".in-addr.arpa.", DNSRecordClass.CLASS_IN, unique, ttl, this.getName()); |
239 |
|
} |
240 |
0
|
if ((this.getInetAddress() instanceof Inet6Address) && (((Inet6Address) this.getInetAddress()).isIPv4CompatibleAddress())) { |
241 |
0
|
byte[] rawAddress = this.getInetAddress().getAddress(); |
242 |
0
|
String address = (rawAddress[12] & 0xff) + "." + (rawAddress[13] & 0xff) + "." + (rawAddress[14] & 0xff) + "." + (rawAddress[15] & 0xff); |
243 |
0
|
return new DNSRecord.Pointer(address + ".in-addr.arpa.", DNSRecordClass.CLASS_IN, unique, ttl, this.getName()); |
244 |
|
} |
245 |
0
|
return null; |
246 |
|
} |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
248 |
0
|
private DNSRecord.Pointer getDNS6ReverseAddressRecord(boolean unique, int ttl) {... |
249 |
0
|
if (this.getInetAddress() instanceof Inet6Address) { |
250 |
0
|
return new DNSRecord.Pointer(this.getInetAddress().getHostAddress() + ".ip6.arpa.", DNSRecordClass.CLASS_IN, unique, ttl, this.getName()); |
251 |
|
} |
252 |
0
|
return null; |
253 |
|
} |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
255 |
0
|
@Override... |
256 |
|
public String toString() { |
257 |
0
|
StringBuilder buf = new StringBuilder(1024); |
258 |
0
|
buf.append("local host info["); |
259 |
0
|
buf.append(getName() != null ? getName() : "no name"); |
260 |
0
|
buf.append(", "); |
261 |
0
|
buf.append(getInterface() != null ? getInterface().getDisplayName() : "???"); |
262 |
0
|
buf.append(":"); |
263 |
0
|
buf.append(getInetAddress() != null ? getInetAddress().getHostAddress() : "no address"); |
264 |
0
|
buf.append(", "); |
265 |
0
|
buf.append(_state); |
266 |
0
|
buf.append("]"); |
267 |
0
|
return buf.toString(); |
268 |
|
} |
269 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
270 |
167
|
public Collection<DNSRecord> answers(boolean unique, int ttl) {... |
271 |
168
|
List<DNSRecord> list = new ArrayList<DNSRecord>(); |
272 |
168
|
DNSRecord answer = this.getDNS4AddressRecord(unique, ttl); |
273 |
168
|
if (answer != null) { |
274 |
160
|
list.add(answer); |
275 |
|
} |
276 |
168
|
answer = this.getDNS6AddressRecord(unique, ttl); |
277 |
168
|
if (answer != null) { |
278 |
8
|
list.add(answer); |
279 |
|
} |
280 |
168
|
return list; |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
@inheritDoc |
285 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
286 |
0
|
@Override... |
287 |
|
public JmDNSImpl getDns() { |
288 |
0
|
return this._state.getDns(); |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
@inheritDoc |
293 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
164
|
@Override... |
295 |
|
public boolean advanceState(DNSTask task) { |
296 |
164
|
return this._state.advanceState(task); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
@inheritDoc |
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
302 |
133
|
@Override... |
303 |
|
public void removeAssociationWithTask(DNSTask task) { |
304 |
133
|
this._state.removeAssociationWithTask(task); |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
@inheritDoc |
309 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
310 |
0
|
@Override... |
311 |
|
public boolean revertState() { |
312 |
0
|
return this._state.revertState(); |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
@inheritDoc |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
318 |
184
|
@Override... |
319 |
|
public void associateWithTask(DNSTask task, DNSState state) { |
320 |
184
|
this._state.associateWithTask(task, state); |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
@inheritDoc |
325 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
326 |
318
|
@Override... |
327 |
|
public boolean isAssociatedWithTask(DNSTask task, DNSState state) { |
328 |
318
|
return this._state.isAssociatedWithTask(task, state); |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
@inheritDoc |
333 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
334 |
0
|
@Override... |
335 |
|
public boolean cancelState() { |
336 |
0
|
return this._state.cancelState(); |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
|
@inheritDoc |
341 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
342 |
29
|
@Override... |
343 |
|
public boolean closeState() { |
344 |
29
|
return this._state.closeState(); |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
|
@inheritDoc |
349 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
350 |
0
|
@Override... |
351 |
|
public boolean recoverState() { |
352 |
0
|
return this._state.recoverState(); |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
@inheritDoc |
357 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
358 |
0
|
@Override... |
359 |
|
public boolean isProbing() { |
360 |
0
|
return this._state.isProbing(); |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
@inheritDoc |
365 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
366 |
0
|
@Override... |
367 |
|
public boolean isAnnouncing() { |
368 |
0
|
return this._state.isAnnouncing(); |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
|
@inheritDoc |
373 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
374 |
532
|
@Override... |
375 |
|
public boolean isAnnounced() { |
376 |
533
|
return this._state.isAnnounced(); |
377 |
|
} |
378 |
|
|
379 |
|
|
380 |
|
@inheritDoc |
381 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
382 |
2387
|
@Override... |
383 |
|
public boolean isCanceling() { |
384 |
2412
|
return this._state.isCanceling(); |
385 |
|
} |
386 |
|
|
387 |
|
|
388 |
|
@inheritDoc |
389 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
390 |
2407
|
@Override... |
391 |
|
public boolean isCanceled() { |
392 |
2419
|
return this._state.isCanceled(); |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
@inheritDoc |
397 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
398 |
722
|
@Override... |
399 |
|
public boolean isClosing() { |
400 |
736
|
return this._state.isClosing(); |
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
|
@inheritDoc |
405 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
406 |
651
|
@Override... |
407 |
|
public boolean isClosed() { |
408 |
663
|
return this._state.isClosed(); |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
|
@inheritDoc |
413 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
414 |
20
|
@Override... |
415 |
|
public boolean waitForAnnounced(long timeout) { |
416 |
20
|
return _state.waitForAnnounced(timeout); |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
|
@inheritDoc |
421 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
422 |
29
|
@Override... |
423 |
|
public boolean waitForCanceled(long timeout) { |
424 |
29
|
if (_address == null) { |
425 |
|
|
426 |
0
|
return true; |
427 |
|
} |
428 |
29
|
return _state.waitForCanceled(timeout); |
429 |
|
} |
430 |
|
|
431 |
|
} |