Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
../../../img/srcFileCovDistChart6.png 31% of files have more coverage
137   431   83   3.61
56   294   0.61   19
38     2.18  
2    
 
  HostInfo       Line # 32 135 0% 82 101 55.7% 0.55701756
  HostInfo.HostInfoState       Line # 43 2 0% 1 0 100% 1.0
 
  (21)
 
1    // Copyright 2003-2005 Arthur van Hoff, Rick Blair
2    // Licensed under Apache License version 2.0
3    // Original license LGPL
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    * HostInfo information on the local host to be able to cope with change of addresses.
29    *
30    * @author Pierre Frisch, Werner Randelshofer
31    */
 
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   
 
43    private final static class HostInfoState extends DNSStatefulObject.DefaultImplementation {
44   
45    private static final long serialVersionUID = -8191476803620402088L;
46   
47    /**
48    * @param dns
49    */
 
50  29 toggle public HostInfoState(JmDNSImpl dns) {
51  29 super();
52  29 this.setDns(dns);
53    }
54   
55    }
56   
57    /**
58    * @param address
59    * IP address to bind
60    * @param dns
61    * JmDNS instance
62    * @param jmdnsName
63    * JmDNS name
64    * @return new HostInfo
65    */
 
66  29 toggle 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    // Find local address that isn't a loopback address
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    // This is only used for running unit test on Debian / Ubuntu
98  0 addr = loopbackAddress();
99  0 aName = ((jmdnsName != null) && (jmdnsName.length() > 0) ? jmdnsName : "computer");
100    }
101    // A host name with "." is illegal. so strip off everything and append .local.
102  29 aName = aName.replace('.', '-');
103  29 aName += ".local.";
104  29 localhost = new HostInfo(addr, aName, dns);
105  29 return localhost;
106    }
107   
 
108  0 toggle 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    * This is used to create a unique name for the host name.
118    */
119    private int hostNameCount;
120   
 
121  29 toggle 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   
 
135  1257 toggle public String getName() {
136  1267 return _name;
137    }
138   
 
139  2038 toggle public InetAddress getInetAddress() {
140  2065 return _address;
141    }
142   
 
143  20 toggle Inet4Address getInet4Address() {
144  20 if (this.getInetAddress() instanceof Inet4Address) {
145  19 return (Inet4Address) _address;
146    }
147  1 return null;
148    }
149   
 
150  20 toggle Inet6Address getInet6Address() {
151  20 if (this.getInetAddress() instanceof Inet6Address) {
152  1 return (Inet6Address) _address;
153    }
154  19 return null;
155    }
156   
 
157  58 toggle public NetworkInterface getInterface() {
158  58 return _interfaze;
159    }
160   
 
161  436 toggle 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   
 
169  0 toggle 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   
 
177  621 toggle 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    // Ignore linklocal packets on regular interfaces, unless this is
184    // also a linklocal interface. This is to avoid duplicates. This is
185    // a terrible hack caused by the lack of an API to get the address
186    // of the interface on which the packet was received.
187  0 result = true;
188    }
189  641 if (from.isLoopbackAddress() && (!this.getInetAddress().isLoopbackAddress())) {
190    // Ignore loopback packets on a regular interface unless this is also a loopback interface.
191  0 result = true;
192    }
193    }
194    }
195  645 return result;
196    }
197   
 
198  446 toggle 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   
 
210  511 toggle 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   
 
217  269 toggle 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   
 
224  0 toggle 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   
 
236  0 toggle 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   
 
248  0 toggle 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   
 
255  0 toggle @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   
 
270  167 toggle 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    */
 
286  0 toggle @Override
287    public JmDNSImpl getDns() {
288  0 return this._state.getDns();
289    }
290   
291    /**
292    * {@inheritDoc}
293    */
 
294  164 toggle @Override
295    public boolean advanceState(DNSTask task) {
296  164 return this._state.advanceState(task);
297    }
298   
299    /**
300    * {@inheritDoc}
301    */
 
302  133 toggle @Override
303    public void removeAssociationWithTask(DNSTask task) {
304  133 this._state.removeAssociationWithTask(task);
305    }
306   
307    /**
308    * {@inheritDoc}
309    */
 
310  0 toggle @Override
311    public boolean revertState() {
312  0 return this._state.revertState();
313    }
314   
315    /**
316    * {@inheritDoc}
317    */
 
318  184 toggle @Override
319    public void associateWithTask(DNSTask task, DNSState state) {
320  184 this._state.associateWithTask(task, state);
321    }
322   
323    /**
324    * {@inheritDoc}
325    */
 
326  318 toggle @Override
327    public boolean isAssociatedWithTask(DNSTask task, DNSState state) {
328  318 return this._state.isAssociatedWithTask(task, state);
329    }
330   
331    /**
332    * {@inheritDoc}
333    */
 
334  0 toggle @Override
335    public boolean cancelState() {
336  0 return this._state.cancelState();
337    }
338   
339    /**
340    * {@inheritDoc}
341    */
 
342  29 toggle @Override
343    public boolean closeState() {
344  29 return this._state.closeState();
345    }
346   
347    /**
348    * {@inheritDoc}
349    */
 
350  0 toggle @Override
351    public boolean recoverState() {
352  0 return this._state.recoverState();
353    }
354   
355    /**
356    * {@inheritDoc}
357    */
 
358  0 toggle @Override
359    public boolean isProbing() {
360  0 return this._state.isProbing();
361    }
362   
363    /**
364    * {@inheritDoc}
365    */
 
366  0 toggle @Override
367    public boolean isAnnouncing() {
368  0 return this._state.isAnnouncing();
369    }
370   
371    /**
372    * {@inheritDoc}
373    */
 
374  532 toggle @Override
375    public boolean isAnnounced() {
376  533 return this._state.isAnnounced();
377    }
378   
379    /**
380    * {@inheritDoc}
381    */
 
382  2387 toggle @Override
383    public boolean isCanceling() {
384  2412 return this._state.isCanceling();
385    }
386   
387    /**
388    * {@inheritDoc}
389    */
 
390  2407 toggle @Override
391    public boolean isCanceled() {
392  2419 return this._state.isCanceled();
393    }
394   
395    /**
396    * {@inheritDoc}
397    */
 
398  722 toggle @Override
399    public boolean isClosing() {
400  736 return this._state.isClosing();
401    }
402   
403    /**
404    * {@inheritDoc}
405    */
 
406  651 toggle @Override
407    public boolean isClosed() {
408  663 return this._state.isClosed();
409    }
410   
411    /**
412    * {@inheritDoc}
413    */
 
414  20 toggle @Override
415    public boolean waitForAnnounced(long timeout) {
416  20 return _state.waitForAnnounced(timeout);
417    }
418   
419    /**
420    * {@inheritDoc}
421    */
 
422  29 toggle @Override
423    public boolean waitForCanceled(long timeout) {
424  29 if (_address == null) {
425    // No need to wait this was never announced.
426  0 return true;
427    }
428  29 return _state.waitForCanceled(timeout);
429    }
430   
431    }