Ejemplo n.º 1
0
 static InetAddressImpl create() {
   Object o;
   if (isIPv6Supported()) {
     o = InetAddress.loadImpl("Inet6AddressImpl");
   } else {
     o = InetAddress.loadImpl("Inet4AddressImpl");
   }
   return (InetAddressImpl) o;
 }
Ejemplo n.º 2
0
  /**
   * Returns the hostname for this address.
   *
   * <p>If there is a security manager, this method first calls its <code>checkConnect</code> method
   * with the hostname and <code>-1</code> as its arguments to see if the calling code is allowed to
   * know the hostname for this IP address, i.e., to connect to the host. If the operation is not
   * allowed, it will return the textual representation of the IP address.
   *
   * @return the host name for this IP address, or if the operation is not allowed by the security
   *     check, the textual representation of the IP address.
   * @param check make security check if true
   * @see SecurityManager#checkConnect
   */
  private static String getHostFromNameService(InetAddress addr, boolean check) {
    String host = null;
    for (NameService nameService : nameServices) {
      try {
        // first lookup the hostname
        host = nameService.getHostByAddr(addr.getAddress());

        /* check to see if calling code is allowed to know
         * the hostname for this IP address, ie, connect to the host
         */
        if (check) {
          SecurityManager sec = System.getSecurityManager();
          if (sec != null) {
            sec.checkConnect(host, -1);
          }
        }

        /* now get all the IP addresses for this hostname,
         * and make sure one of them matches the original IP
         * address. We do this to try and prevent spoofing.
         */

        InetAddress[] arr = InetAddress.getAllByName0(host, check);
        boolean ok = false;

        if (arr != null) {
          for (int i = 0; !ok && i < arr.length; i++) {
            ok = addr.equals(arr[i]);
          }
        }

        // XXX: if it looks a spoof just return the address?
        if (!ok) {
          host = addr.getHostAddress();
          return host;
        }

        break;

      } catch (SecurityException e) {
        host = addr.getHostAddress();
        break;
      } catch (UnknownHostException e) {
        host = addr.getHostAddress();
        // let next provider resolve the hostname
      }
    }

    return host;
  }
Ejemplo n.º 3
0
  /**
   * Returns the address of the local host. This is achieved by retrieving the name of the host from
   * the system, then resolving that name into an <code>InetAddress</code>.
   *
   * <p>Note: The resolved address may be cached for a short period of time.
   *
   * <p>If there is a security manager, its <code>checkConnect</code> method is called with the
   * local host name and <code>-1</code> as its arguments to see if the operation is allowed. If the
   * operation is not allowed, an InetAddress representing the loopback address is returned.
   *
   * @return the address of the local host.
   * @exception UnknownHostException if the local host name could not be resolved into an address.
   * @see SecurityManager#checkConnect
   * @see java.net.InetAddress#getByName(java.lang.String)
   */
  public static InetAddress getLocalHost() throws UnknownHostException {

    SecurityManager security = System.getSecurityManager();
    try {
      String local = impl.getLocalHostName();

      if (security != null) {
        security.checkConnect(local, -1);
      }

      if (local.equals("localhost")) {
        return impl.loopbackAddress();
      }

      InetAddress ret = null;
      synchronized (cacheLock) {
        long now = System.currentTimeMillis();
        if (cachedLocalHost != null) {
          if ((now - cacheTime) < maxCacheTime) // Less than 5s old?
          ret = cachedLocalHost;
          else cachedLocalHost = null;
        }

        // we are calling getAddressFromNameService directly
        // to avoid getting localHost from cache
        if (ret == null) {
          InetAddress[] localAddrs;
          try {
            localAddrs = (InetAddress[]) InetAddress.getAddressFromNameService(local, null);
          } catch (UnknownHostException uhe) {
            throw new UnknownHostException(local + ": " + uhe.getMessage());
          }
          cachedLocalHost = localAddrs[0];
          cacheTime = now;
          ret = localAddrs[0];
        }
      }
      return ret;
    } catch (java.lang.SecurityException e) {
      return impl.loopbackAddress();
    }
  }
Ejemplo n.º 4
0
 // called from deployment cache manager
 private static InetAddress getByName(String host, InetAddress reqAddr)
     throws UnknownHostException {
   return InetAddress.getAllByName(host, reqAddr)[0];
 }
Ejemplo n.º 5
0
 /**
  * Determines the IP address of a host, given the host's name.
  *
  * <p>The host name can either be a machine name, such as "<code>java.sun.com</code>", or a
  * textual representation of its IP address. If a literal IP address is supplied, only the
  * validity of the address format is checked.
  *
  * <p>For <code>host</code> specified in literal IPv6 address, either the form defined in RFC 2732
  * or the literal IPv6 address format defined in RFC 2373 is accepted. IPv6 scoped addresses are
  * also supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
  * scoped addresses.
  *
  * <p>If the host is <tt>null</tt> then an <tt>InetAddress</tt> representing an address of the
  * loopback interface is returned. See <a
  * href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a> section&nbsp;2 and <a
  * href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a> section&nbsp;2.5.3.
  *
  * @param host the specified host, or <code>null</code>.
  * @return an IP address for the given host name.
  * @exception UnknownHostException if no IP address for the <code>host</code> could be found, or
  *     if a scope_id was specified for a global IPv6 address.
  * @exception SecurityException if a security manager exists and its checkConnect method doesn't
  *     allow the operation
  */
 public static InetAddress getByName(String host) throws UnknownHostException {
   return InetAddress.getAllByName(host)[0];
 }
Ejemplo n.º 6
0
 /**
  * Gets the fully qualified domain name for this IP address. Best effort method, meaning we may
  * not be able to return the FQDN depending on the underlying system configuration.
  *
  * <p>If there is a security manager, this method first calls its <code>checkConnect</code> method
  * with the hostname and <code>-1</code> as its arguments to see if the calling code is allowed to
  * know the hostname for this IP address, i.e., to connect to the host. If the operation is not
  * allowed, it will return the textual representation of the IP address.
  *
  * @return the fully qualified domain name for this IP address, or if the operation is not allowed
  *     by the security check, the textual representation of the IP address.
  * @see SecurityManager#checkConnect
  * @since 1.4
  */
 public String getCanonicalHostName() {
   if (canonicalHostName == null) {
     canonicalHostName = InetAddress.getHostFromNameService(this, true);
   }
   return canonicalHostName;
 }
Ejemplo n.º 7
0
 /**
  * Returns the hostname for this address. If the host is equal to null, then this address refers
  * to any of the local machine's available network addresses. this is package private so
  * SocketPermission can make calls into here without a security check.
  *
  * <p>If there is a security manager, this method first calls its <code>checkConnect</code> method
  * with the hostname and <code>-1</code> as its arguments to see if the calling code is allowed to
  * know the hostname for this IP address, i.e., to connect to the host. If the operation is not
  * allowed, it will return the textual representation of the IP address.
  *
  * @return the host name for this IP address, or if the operation is not allowed by the security
  *     check, the textual representation of the IP address.
  * @param check make security check if true
  * @see SecurityManager#checkConnect
  */
 String getHostName(boolean check) {
   if (hostName == null) {
     hostName = InetAddress.getHostFromNameService(this, check);
   }
   return hostName;
 }