KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > Inet6AddressImpl


1 /*
2  * @(#)Inet6AddressImpl.java 1.9 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.net;
8 import java.io.IOException JavaDoc;
9
10 /*
11  * Package private implementation of InetAddressImpl for dual
12  * IPv4/IPv6 stack.
13  * <p>
14  * If InetAddress.preferIPv6Address is true then anyLocalAddress(),
15  * loopbackAddress(), and localHost() will return IPv6 addresses,
16  * otherwise IPv4 addresses.
17  *
18  * @since 1.4
19  */

20
21 class Inet6AddressImpl implements InetAddressImpl JavaDoc {
22     public native String JavaDoc getLocalHostName() throws UnknownHostException JavaDoc;
23     public native InetAddress JavaDoc[]
24         lookupAllHostAddr(String JavaDoc hostname) throws UnknownHostException JavaDoc;
25     public native String JavaDoc getHostByAddr(byte[] addr) throws UnknownHostException JavaDoc;
26     private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException JavaDoc;
27
28     public boolean isReachable(InetAddress JavaDoc addr, int timeout, NetworkInterface JavaDoc netif, int ttl) throws IOException JavaDoc {
29     byte[] ifaddr = null;
30     int scope = -1;
31     int netif_scope = -1;
32     if (netif != null) {
33         /*
34          * Let's make sure we bind to an address of the proper family.
35          * Which means same family as addr because at this point it could
36          * be either an IPv6 address or an IPv4 address (case of a dual
37          * stack system).
38          */

39         java.util.Enumeration JavaDoc it = netif.getInetAddresses();
40         InetAddress JavaDoc inetaddr = null;
41         while (it.hasMoreElements()) {
42         inetaddr = (InetAddress JavaDoc) it.nextElement();
43         if (inetaddr.getClass().isInstance(addr)) {
44             ifaddr = inetaddr.getAddress();
45             if (inetaddr instanceof Inet6Address JavaDoc) {
46             netif_scope = ((Inet6Address JavaDoc) inetaddr).getScopeId();
47             }
48             break;
49         }
50         }
51         if (ifaddr == null) {
52         // Interface doesn't support the address family of
53
// the destination
54
return false;
55         }
56     }
57     if (addr instanceof Inet6Address JavaDoc)
58         scope = ((Inet6Address JavaDoc) addr).getScopeId();
59     return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);
60     }
61
62     public synchronized InetAddress JavaDoc anyLocalAddress() {
63         if (anyLocalAddress == null) {
64             if (InetAddress.preferIPv6Address) {
65                 anyLocalAddress = new Inet6Address JavaDoc();
66                 anyLocalAddress.hostName = "::";
67             } else {
68                 anyLocalAddress = (new Inet4AddressImpl JavaDoc()).anyLocalAddress();
69             }
70         }
71         return anyLocalAddress;
72     }
73
74     public synchronized InetAddress JavaDoc loopbackAddress() {
75         if (loopbackAddress == null) {
76              if (InetAddress.preferIPv6Address) {
77                  byte[] loopback =
78             {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
80                  loopbackAddress = new Inet6Address JavaDoc("localhost", loopback);
81              } else {
82                 loopbackAddress = (new Inet4AddressImpl JavaDoc()).loopbackAddress();
83              }
84         }
85         return loopbackAddress;
86     }
87
88     private InetAddress JavaDoc anyLocalAddress;
89     private InetAddress JavaDoc loopbackAddress;
90 }
91
92
Popular Tags