1. /*
  2. * @(#)Inet4AddressImpl.java 1.4 03/12/19
  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 IPv4.
  11. *
  12. * @since 1.4
  13. */
  14. class Inet4AddressImpl implements InetAddressImpl {
  15. public native String getLocalHostName() throws UnknownHostException;
  16. public native byte[][]
  17. lookupAllHostAddr(String hostname) throws UnknownHostException;
  18. public native String getHostByAddr(byte[] addr) throws UnknownHostException;
  19. private native boolean isReachable0(byte[] addr, int timeout, byte[] ifaddr, int ttl) throws IOException;
  20. public synchronized InetAddress anyLocalAddress() {
  21. if (anyLocalAddress == null) {
  22. anyLocalAddress = new Inet4Address(); // {0x00,0x00,0x00,0x00}
  23. anyLocalAddress.hostName = "0.0.0.0";
  24. }
  25. return anyLocalAddress;
  26. }
  27. public synchronized InetAddress loopbackAddress() {
  28. if (loopbackAddress == null) {
  29. byte[] loopback = {0x7f,0x00,0x00,0x01};
  30. loopbackAddress = new Inet4Address("localhost", loopback);
  31. }
  32. return loopbackAddress;
  33. }
  34. public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException {
  35. byte[] ifaddr = null;
  36. if (netif != null) {
  37. /*
  38. * Let's make sure we use an address of the proper family
  39. */
  40. java.util.Enumeration it = netif.getInetAddresses();
  41. InetAddress inetaddr = null;
  42. while (!(inetaddr instanceof Inet4Address) &&
  43. it.hasMoreElements())
  44. inetaddr = (InetAddress) it.nextElement();
  45. if (inetaddr instanceof Inet4Address)
  46. ifaddr = inetaddr.getAddress();
  47. }
  48. return isReachable0(addr.getAddress(), timeout, ifaddr, ttl);
  49. }
  50. private InetAddress anyLocalAddress;
  51. private InetAddress loopbackAddress;
  52. }