Exemplo n.º 1
0
 /** Returns true if host matches the LOOPBACK (127.0.0.1). */
 public static boolean isLoopback(Object host) {
   if (host instanceof InetAddress) {
     return LOOPBACK.equals(host);
   } else {
     return isLoopback(InetAddressUtil.toInetAddress(host.toString()));
   }
 }
Exemplo n.º 2
0
 /** Returns true if host matches the LOCALHOST. */
 public static boolean isLocalHost(Object host) {
   if (host instanceof InetAddress) {
     if (LOCALHOST.equals(host)) {
       return true;
     } else {
       //        InetAddress hostAddr = (InetAddress)host;
       try {
         Enumeration en = NetworkInterface.getNetworkInterfaces();
         while (en.hasMoreElements()) {
           NetworkInterface i = (NetworkInterface) en.nextElement();
           for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements(); ) {
             InetAddress addr = (InetAddress) en2.nextElement();
             if (host.equals(addr)) {
               return true;
             }
           }
         }
         return false;
       } catch (SocketException e) {
         throw new GemFireIOException(
             LocalizedStrings.InetAddressUtil_UNABLE_TO_QUERY_NETWORK_INTERFACE
                 .toLocalizedString(),
             e);
       }
     }
   } else {
     return isLocalHost(InetAddressUtil.toInetAddress(host.toString()));
   }
 }
  /**
   * Creates a new <code>DistributedSystemConfigImpl</code> based on the configuration stored in a
   * <code>DistributedSystem</code>'s <code>DistributionConfig</code>.
   */
  public DistributedSystemConfigImpl(DistributionConfig distConfig, String remoteCommand) {
    if (distConfig == null) {
      throw new IllegalArgumentException(
          LocalizedStrings.DistributedSystemConfigImpl_DISTRIBUTIONCONFIG_MUST_NOT_BE_NULL
              .toLocalizedString());
    }

    this.mcastAddress = InetAddressUtil.toString(distConfig.getMcastAddress());
    this.mcastPort = distConfig.getMcastPort();
    this.locators = distConfig.getLocators();
    this.membershipPortRange = getMembershipPortRangeString(distConfig.getMembershipPortRange());

    this.systemName = distConfig.getName();

    this.sslEnabled = distConfig.getSSLEnabled();
    this.sslCiphers = distConfig.getSSLCiphers();
    this.sslProtocols = distConfig.getSSLProtocols();
    this.sslAuthenticationRequired = distConfig.getSSLRequireAuthentication();

    this.logFile = distConfig.getLogFile().getPath();
    this.logLevel = LogWriterImpl.levelToString(distConfig.getLogLevel());
    this.logDiskSpaceLimit = distConfig.getLogDiskSpaceLimit();
    this.logFileSizeLimit = distConfig.getLogFileSizeLimit();

    basicSetBindAddress(distConfig.getBindAddress());
    this.tcpPort = distConfig.getTcpPort();

    this.disableTcp = distConfig.getDisableTcp();

    this.remoteCommand = remoteCommand;
    this.serverBindAddress = distConfig.getServerBindAddress();
    this.enableNetworkPartitionDetection = distConfig.getEnableNetworkPartitionDetection();
    this.memberTimeout = distConfig.getMemberTimeout();
    this.refreshInterval = DistributedSystemConfig.DEFAULT_REFRESH_INTERVAL;
    this.gfSecurityProperties = (Properties) distConfig.getSSLProperties().clone();
  }
Exemplo n.º 4
0
/**
 * Provides static utilities for manipulating, validating, and converting InetAddresses and host
 * strings.
 *
 * @author Kirk Lund
 * @since 3.5
 */
public class InetAddressUtil {

  /** InetAddress instance representing the local host */
  public static final InetAddress LOCALHOST = createLocalHost();

  public static final String LOOPBACK_ADDRESS =
      Boolean.getBoolean("java.net.preferIPv6Addresses") ? "::1" : "127.0.0.1";

  public static final InetAddress LOOPBACK = InetAddressUtil.toInetAddress(LOOPBACK_ADDRESS);

  /** Disallows InetAddressUtil instantiation. */
  private InetAddressUtil() {}

  /**
   * Returns a string version of InetAddress which can be converted back to an InetAddress later.
   * Essentially any leading slash is trimmed.
   *
   * @param val the InetAddress or String to return a formatted string of
   * @return string version the InetAddress minus any leading slash
   */
  public static String toString(Object val) {
    if (val instanceof String) {
      return trimLeadingSlash((String) val);

    } else if (val instanceof InetAddress) {
      return ((InetAddress) val).getHostAddress();

    } else {
      return trimLeadingSlash(val.toString());
    }
  }

  /**
   * Converts the string host to an instance of InetAddress. Returns null if the string is empty.
   * Fails Assertion if the conversion would result in <code>java.lang.UnknownHostException</code>.
   *
   * <p>Any leading slashes on host will be ignored.
   *
   * @param host string version the InetAddress
   * @return the host converted to InetAddress instance
   */
  public static InetAddress toInetAddress(String host) {
    if (host == null || host.length() == 0) {
      return null;
    }
    try {
      if (host.indexOf("/") > -1) {
        return InetAddress.getByName(host.substring(host.indexOf("/") + 1));
      } else {
        return InetAddress.getByName(host);
      }
    } catch (java.net.UnknownHostException e) {
      logStackTrace(e);
      Assert.assertTrue(false, "Failed to get InetAddress: " + host);
      return null; // will never happen since the Assert will fail
    }
  }

  /**
   * Creates an InetAddress representing the local host. The checked exception <code>
   * java.lang.UnknownHostException</code> is captured and results in an Assertion failure instead.
   *
   * @return InetAddress instance representing the local host
   */
  public static InetAddress createLocalHost() {
    try {
      return SocketCreator.getLocalHost();
    } catch (java.net.UnknownHostException e) {
      logStackTrace(e);
      Assert.assertTrue(false, "Failed to get local host");
      return null; // will never happen
    }
  }

  /**
   * Validates the host by making sure it can successfully be used to get an instance of
   * InetAddress. If the host string is null, empty or would result in <code>
   * java.lang.UnknownHostException</code> then null is returned.
   *
   * <p>Any leading slashes on host will be ignored.
   *
   * @param host string version the InetAddress
   * @return the host converted to InetAddress instance
   */
  public static String validateHost(String host) {
    if (host == null || host.length() == 0) {
      return null;
    }
    try {
      InetAddress.getByName(trimLeadingSlash(host));
      return host;
    } catch (java.net.UnknownHostException e) {
      logStackTrace(e);
      return null;
    }
  }

  /** Returns true if host matches the LOCALHOST. */
  public static boolean isLocalHost(Object host) {
    if (host instanceof InetAddress) {
      if (LOCALHOST.equals(host)) {
        return true;
      } else {
        //        InetAddress hostAddr = (InetAddress)host;
        try {
          Enumeration en = NetworkInterface.getNetworkInterfaces();
          while (en.hasMoreElements()) {
            NetworkInterface i = (NetworkInterface) en.nextElement();
            for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements(); ) {
              InetAddress addr = (InetAddress) en2.nextElement();
              if (host.equals(addr)) {
                return true;
              }
            }
          }
          return false;
        } catch (SocketException e) {
          throw new GemFireIOException(
              LocalizedStrings.InetAddressUtil_UNABLE_TO_QUERY_NETWORK_INTERFACE
                  .toLocalizedString(),
              e);
        }
      }
    } else {
      return isLocalHost(InetAddressUtil.toInetAddress(host.toString()));
    }
  }

  /** Returns true if host matches the LOOPBACK (127.0.0.1). */
  public static boolean isLoopback(Object host) {
    if (host instanceof InetAddress) {
      return LOOPBACK.equals(host);
    } else {
      return isLoopback(InetAddressUtil.toInetAddress(host.toString()));
    }
  }

  /** Returns a version of the value after removing any leading slashes */
  private static String trimLeadingSlash(String value) {
    if (value == null) return "";
    while (value.indexOf("/") > -1) {
      value = value.substring(value.indexOf("/") + 1);
    }
    return value;
  }

  /**
   * Logs the stack trace for the given Throwable if logger is initialized else prints the stack
   * trace using System.out. If logged the logs are logged at WARNING level.
   *
   * @param throwable Throwable to log stack trace for
   */
  private static void logStackTrace(Throwable throwable) {
    AdminDistributedSystemImpl adminDS = AdminDistributedSystemImpl.getConnectedInstance();

    if (adminDS != null) {
      adminDS.getLogWriter().warning(throwable);
    } else {
      throwable.printStackTrace();
    }
  }
}
 /**
  * Validates the bind address. The address may be a host name or IP address, but it must not be
  * empty and must be usable for creating an InetAddress. Cannot have a leading '/' (which
  * InetAddress.toString() produces).
  *
  * @param bindAddress host name or IP address to validate
  */
 public static boolean validateBindAddress(String bindAddress) {
   if (bindAddress == null || bindAddress.length() == 0) return true;
   if (InetAddressUtil.validateHost(bindAddress) == null) return false;
   return true;
 }