1. /*
  2. * @(#)Inet6AddressImpl.java 1.7 04/01/13
  3. *
  4. * Copyright 2004 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;
  9. /*
  10. * Package private implementation of InetAddressImpl for dual
  11. * IPv4/IPv6 stack.
  12. * <p>
  13. * If InetAddress.preferIPv6Address is true then anyLocalAddress(),
  14. * loopbackAddress(), and localHost() will return IPv6 addresses,
  15. * otherwise IPv4 addresses.
  16. *
  17. * @since 1.4
  18. */
  19. class Inet6AddressImpl implements InetAddressImpl {
  20. public native String getLocalHostName() throws UnknownHostException;
  21. public native byte[][]
  22. lookupAllHostAddr(String hostname) throws UnknownHostException;
  23. public native String getHostByAddr(byte[] addr) throws UnknownHostException;
  24. private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException;
  25. public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException {
  26. byte[] ifaddr = null;
  27. int scope = -1;
  28. int netif_scope = -1;
  29. if (netif != null) {
  30. /*
  31. * Let's make sure we bind to an address of the proper family.
  32. * Which means same family as addr because at this point it could
  33. * be either an IPv6 address or an IPv4 address (case of a dual
  34. * stack system).
  35. */
  36. java.util.Enumeration it = netif.getInetAddresses();
  37. InetAddress inetaddr = null;
  38. while (it.hasMoreElements()) {
  39. inetaddr = (InetAddress) it.nextElement();
  40. if (inetaddr.getClass().isInstance(addr)) {
  41. ifaddr = inetaddr.getAddress();
  42. if (inetaddr instanceof Inet6Address) {
  43. netif_scope = ((Inet6Address) inetaddr).getScopeId();
  44. }
  45. break;
  46. }
  47. }
  48. if (ifaddr == null) {
  49. // Interface doesn't support the address family of
  50. // the destination
  51. return false;
  52. }
  53. }
  54. if (addr instanceof Inet6Address)
  55. scope = ((Inet6Address) addr).getScopeId();
  56. return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);
  57. }
  58. public synchronized InetAddress anyLocalAddress() {
  59. if (anyLocalAddress == null) {
  60. if (InetAddress.preferIPv6Address) {
  61. anyLocalAddress = new Inet6Address();
  62. anyLocalAddress.hostName = "::";
  63. } else {
  64. anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();
  65. }
  66. }
  67. return anyLocalAddress;
  68. }
  69. public synchronized InetAddress loopbackAddress() {
  70. if (loopbackAddress == null) {
  71. if (InetAddress.preferIPv6Address) {
  72. byte[] loopback =
  73. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  75. loopbackAddress = new Inet6Address("localhost", loopback);
  76. } else {
  77. loopbackAddress = (new Inet4AddressImpl()).loopbackAddress();
  78. }
  79. }
  80. return loopbackAddress;
  81. }
  82. private InetAddress anyLocalAddress;
  83. private InetAddress loopbackAddress;
  84. }