Java Code Examples for com.google.common.net.InetAddresses#isInetAddress()
The following examples show how to use
com.google.common.net.InetAddresses#isInetAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnsJavaResolver.java From Dream-Catcher with MIT License | 6 votes |
@Override public Collection<InetAddress> resolveRemapped(String remappedHost) { // special case for IP literals: return the InetAddress without doing a dnsjava lookup. dnsjava seems to handle ipv4 literals // reasonably well, but does not handle ipv6 literals (with or without [] brackets) correctly. // note this does not work properly for ipv6 literals with a scope identifier, which is a known issue for InetAddresses.isInetAddress(). // (dnsjava also handles the situation incorrectly) if (InetAddresses.isInetAddress(remappedHost)) { return Collections.singletonList(InetAddresses.forString(remappedHost)); } // retrieve IPv4 addresses, then retrieve IPv6 addresses only if no IPv4 addresses are found. the current implementation always uses the // first returned address, so there is no need to look for IPv6 addresses if an IPv4 address is found. Collection<InetAddress> ipv4addresses = resolveHostByType(remappedHost, Type.A); if (!ipv4addresses.isEmpty()) { return ipv4addresses; } else { return resolveHostByType(remappedHost, Type.AAAA); } }
Example 2
Source File: DFSClient.java From big-c with Apache License 2.0 | 6 votes |
/** * Return the socket addresses to use with each configured * local interface. Local interfaces may be specified by IP * address, IP address range using CIDR notation, interface * name (e.g. eth0) or sub-interface name (e.g. eth0:0). * The socket addresses consist of the IPs for the interfaces * and the ephemeral port (port 0). If an IP, IP range, or * interface name matches an interface with sub-interfaces * only the IP of the interface is used. Sub-interfaces can * be used by specifying them explicitly (by IP or name). * * @return SocketAddresses for the configured local interfaces, * or an empty array if none are configured * @throws UnknownHostException if a given interface name is invalid */ private static SocketAddress[] getLocalInterfaceAddrs( String interfaceNames[]) throws UnknownHostException { List<SocketAddress> localAddrs = new ArrayList<SocketAddress>(); for (String interfaceName : interfaceNames) { if (InetAddresses.isInetAddress(interfaceName)) { localAddrs.add(new InetSocketAddress(interfaceName, 0)); } else if (NetUtils.isValidSubnet(interfaceName)) { for (InetAddress addr : NetUtils.getIPs(interfaceName, false)) { localAddrs.add(new InetSocketAddress(addr, 0)); } } else { for (String ip : DNS.getIPs(interfaceName, false)) { localAddrs.add(new InetSocketAddress(ip, 0)); } } } return localAddrs.toArray(new SocketAddress[localAddrs.size()]); }
Example 3
Source File: WorfInteractive.java From warp10-platform with Apache License 2.0 | 6 votes |
private static String readInteger(ConsoleReader reader, PrintWriter out, String prompt) { try { String inputString = readInputString(reader, out, prompt); if (NumberUtils.isNumber(inputString)) { return inputString; } if (InetAddresses.isInetAddress(inputString)) { return inputString; } out.println("Error, " + inputString + " is not a number"); } catch (Exception exp) { if (WorfCLI.verbose) { exp.printStackTrace(); } out.println("Error, unable to read the host. error=" + exp.getMessage()); } return null; }
Example 4
Source File: HttpRequestUtil.java From consulo with Apache License 2.0 | 5 votes |
public static boolean isLocalHost(String host, boolean onlyAnyOrLoopback, boolean hostsOnly) { if (NetUtil.isLocalhost(host)) { return true; } // if IP address, it is safe to use getByName (not affected by DNS rebinding) if (onlyAnyOrLoopback && !InetAddresses.isInetAddress(host)) { return false; } ThrowableNotNullFunction<InetAddress, Boolean, SocketException> isLocal = inetAddress -> inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() || NetworkInterface.getByInetAddress(inetAddress) != null; try { InetAddress address = InetAddress.getByName(host); if (!isLocal.fun(address)) { return false; } // be aware - on windows hosts file doesn't contain localhost // hosts can contain remote addresses, so, we check it if (hostsOnly && !InetAddresses.isInetAddress(host)) { InetAddress hostInetAddress = HostsFileEntriesResolver.DEFAULT.address(host, ResolvedAddressTypes.IPV4_PREFERRED); return hostInetAddress != null && isLocal.fun(hostInetAddress); } else { return true; } } catch (IOException ignored) { return false; } }
Example 5
Source File: LocalAppEngineServerLaunchConfigurationDelegate.java From google-cloud-eclipse with Apache License 2.0 | 5 votes |
/** * Resolve a host or IP address to an IP address. * * @return an {@link InetAddress}, or {@code null} if unable to be resolved (equivalent to * {@code INADDR_ANY}) */ static InetAddress resolveAddress(String ipOrHost) { if (!Strings.isNullOrEmpty(ipOrHost)) { if (InetAddresses.isInetAddress(ipOrHost)) { return InetAddresses.forString(ipOrHost); } try { InetAddress[] addresses = InetAddress.getAllByName(ipOrHost); return addresses[0]; } catch (UnknownHostException ex) { logger.info("Unable to resolve '" + ipOrHost + "' to an address"); //$NON-NLS-1$ //$NON-NLS-2$ } } return null; }
Example 6
Source File: StringUtils.java From big-c with Apache License 2.0 | 5 votes |
/** * Given a full hostname, return the word upto the first dot. * @param fullHostname the full hostname * @return the hostname to the first dot */ public static String simpleHostname(String fullHostname) { if (InetAddresses.isInetAddress(fullHostname)) { return fullHostname; } int offset = fullHostname.indexOf('.'); if (offset != -1) { return fullHostname.substring(0, offset); } return fullHostname; }
Example 7
Source File: AddressUtils.java From haven-platform with Apache License 2.0 | 5 votes |
public static boolean isLocal(String host) { if("localhost".equalsIgnoreCase(host) || "localhost.localdomain".equalsIgnoreCase(host)) { return true; } boolean ip = InetAddresses.isInetAddress(host); if(!ip) { return true; } return InetAddresses.forString(host).isLoopbackAddress(); }
Example 8
Source File: DatanodeManager.java From big-c with Apache License 2.0 | 5 votes |
/** * Parse a DatanodeID from a hosts file entry * @param hostLine of form [hostname|ip][:port]? * @return DatanodeID constructed from the given string */ private DatanodeID parseDNFromHostsEntry(String hostLine) { DatanodeID dnId; String hostStr; int port; int idx = hostLine.indexOf(':'); if (-1 == idx) { hostStr = hostLine; port = DFSConfigKeys.DFS_DATANODE_DEFAULT_PORT; } else { hostStr = hostLine.substring(0, idx); port = Integer.parseInt(hostLine.substring(idx+1)); } if (InetAddresses.isInetAddress(hostStr)) { // The IP:port is sufficient for listing in a report dnId = new DatanodeID(hostStr, "", "", port, DFSConfigKeys.DFS_DATANODE_HTTP_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_HTTPS_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_IPC_DEFAULT_PORT); } else { String ipAddr = ""; try { ipAddr = InetAddress.getByName(hostStr).getHostAddress(); } catch (UnknownHostException e) { LOG.warn("Invalid hostname " + hostStr + " in hosts file"); } dnId = new DatanodeID(ipAddr, hostStr, "", port, DFSConfigKeys.DFS_DATANODE_HTTP_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_HTTPS_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_IPC_DEFAULT_PORT); } return dnId; }
Example 9
Source File: NeutronNetworkingApi.java From attic-stratos with Apache License 2.0 | 5 votes |
/** * Get all predefined all floating IPs defined in cartridge definition. * * @param array of {@link NetworkInterface} * @return list of predefined floating IPs */ public List<String> getAllPredefinedFloatingIPs(NetworkInterface[] networkInterfaces) { String nwInterfacesNullMsg = "Input NetworkInterface array cannot be null"; assertNotNull(networkInterfaces, nwInterfacesNullMsg); List<String> allPredefinedFloatingIPs = new ArrayList<String>(); for (NetworkInterface networkInterface : networkInterfaces) { // if no floating networks defined, skip it if (null == networkInterface.getFloatingNetworks()) { continue; } FloatingNetwork[] floatingNetworks = networkInterface.getFloatingNetworks().getFloatingNetworks(); if (floatingNetworks == null || floatingNetworks.length == 0) { continue; } for (FloatingNetwork floatingNetwork : floatingNetworks) { String floatingIP = floatingNetwork.getFloatingIP(); // we are giving more priority to network uuid over fixed floating IPs // so if both network uuid and floating IPs defined, we are not going to assign those floating IPs // so these can be assigned to some other interfaces // hence excluding from predefined floating IPs list String networkUuid = floatingNetwork.getNetworkUuid(); if (networkUuid == null || networkUuid.isEmpty()) { if (floatingIP != null && InetAddresses.isInetAddress(floatingIP)) { allPredefinedFloatingIPs.add(floatingIP); } } } } return allPredefinedFloatingIPs; }
Example 10
Source File: StringUtils.java From hadoop with Apache License 2.0 | 5 votes |
/** * Given a full hostname, return the word upto the first dot. * @param fullHostname the full hostname * @return the hostname to the first dot */ public static String simpleHostname(String fullHostname) { if (InetAddresses.isInetAddress(fullHostname)) { return fullHostname; } int offset = fullHostname.indexOf('.'); if (offset != -1) { return fullHostname.substring(0, offset); } return fullHostname; }
Example 11
Source File: DatanodeManager.java From hadoop with Apache License 2.0 | 5 votes |
/** * Parse a DatanodeID from a hosts file entry * @param hostLine of form [hostname|ip][:port]? * @return DatanodeID constructed from the given string */ private DatanodeID parseDNFromHostsEntry(String hostLine) { DatanodeID dnId; String hostStr; int port; int idx = hostLine.indexOf(':'); if (-1 == idx) { hostStr = hostLine; port = DFSConfigKeys.DFS_DATANODE_DEFAULT_PORT; } else { hostStr = hostLine.substring(0, idx); port = Integer.parseInt(hostLine.substring(idx+1)); } if (InetAddresses.isInetAddress(hostStr)) { // The IP:port is sufficient for listing in a report dnId = new DatanodeID(hostStr, "", "", port, DFSConfigKeys.DFS_DATANODE_HTTP_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_HTTPS_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_IPC_DEFAULT_PORT); } else { String ipAddr = ""; try { ipAddr = InetAddress.getByName(hostStr).getHostAddress(); } catch (UnknownHostException e) { LOG.warn("Invalid hostname " + hostStr + " in hosts file"); } dnId = new DatanodeID(ipAddr, hostStr, "", port, DFSConfigKeys.DFS_DATANODE_HTTP_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_HTTPS_DEFAULT_PORT, DFSConfigKeys.DFS_DATANODE_IPC_DEFAULT_PORT); } return dnId; }
Example 12
Source File: BouncyCastleSecurityProviderTool.java From AndroidHttpCapture with MIT License | 5 votes |
/** * Converts a list of domain name Subject Alternative Names into ASN1Encodable GeneralNames objects, for use with * the Bouncy Castle certificate builder. * * @param subjectAlternativeNames domain name SANs to convert * @return a GeneralNames instance that includes the specifie dsubjectAlternativeNames as DNS name fields */ private static GeneralNames getDomainNameSANsAsASN1Encodable(List<String> subjectAlternativeNames) { List<GeneralName> encodedSANs = new ArrayList<>(subjectAlternativeNames.size()); for (String subjectAlternativeName : subjectAlternativeNames) { // IP addresses use the IP Address tag instead of the DNS Name tag in the SAN list boolean isIpAddress = InetAddresses.isInetAddress(subjectAlternativeName); GeneralName generalName = new GeneralName(isIpAddress ? GeneralName.iPAddress : GeneralName.dNSName, subjectAlternativeName); encodedSANs.add(generalName); } return new GeneralNames(encodedSANs.toArray(new GeneralName[encodedSANs.size()])); }
Example 13
Source File: Utils.java From wt1 with Apache License 2.0 | 5 votes |
/** * Checks whether a String is a valid public IP address. */ public static boolean isPublicAddress(String a) { if (! InetAddresses.isInetAddress(a)) { return false; } InetAddress addr = InetAddresses.forString(a); return ! addr.isSiteLocalAddress() && ! addr.isLinkLocalAddress() && ! addr.isLoopbackAddress() && ! addr.isAnyLocalAddress() && ! addr.isMulticastAddress(); }
Example 14
Source File: StringUtils.java From nd4j with Apache License 2.0 | 5 votes |
/** * Given a full hostname, return the word upto the first dot. * @param fullHostname the full hostname * @return the hostname to the first dot */ public static String simpleHostname(String fullHostname) { if (InetAddresses.isInetAddress(fullHostname)) { return fullHostname; } int offset = fullHostname.indexOf('.'); if (offset != -1) { return fullHostname.substring(0, offset); } return fullHostname; }
Example 15
Source File: AddExtraHostContainerDecorator.java From helios with Apache License 2.0 | 5 votes |
/** * Validates that the argument has form "host:ip-address". Does not validate the form of the * hostname argument, but checks that the second half is valid via {@link * InetAddresses#isInetAddress}. */ public static boolean isValidArg(String arg) { final String[] args = arg.split(":"); if (args.length != 2) { return false; } final String ip = args[1]; return InetAddresses.isInetAddress(ip); }
Example 16
Source File: DnsNameResolver.java From vespa with Apache License 2.0 | 5 votes |
@Override public Optional<String> getHostname(String ipAddress) { try { String hostname = InetAddress.getByName(ipAddress).getHostName(); return InetAddresses.isInetAddress(hostname) ? Optional.empty() : Optional.of(hostname); } catch (UnknownHostException e) { // This is not an exceptional state hence the debug level logger.log(Level.FINE, "Unable to resolve ipaddress", e); } return Optional.empty(); }
Example 17
Source File: ControllerResolverFactory.java From pravega with Apache License 2.0 | 5 votes |
@Override @Synchronized public void start(Listener listener) { Preconditions.checkState(this.resolverUpdater == null, "ControllerNameResolver has already been started"); Preconditions.checkState(!shutdown, "ControllerNameResolver is shutdown, restart is not supported"); this.resolverUpdater = listener; boolean scheduleDiscovery; // If the servers comprise only of IP addresses then we need to update the controller list only once. List<EquivalentAddressGroup> servers = new ArrayList<>(); if (!this.enableDiscovery) { scheduleDiscovery = false; // Use the bootstrapped server list as the final set of controllers. for (InetSocketAddress address : bootstrapServers) { if (InetAddresses.isInetAddress(address.getHostString())) { servers.add(new EquivalentAddressGroup( new InetSocketAddress(address.getHostString(), address.getPort()))); } else { scheduleDiscovery = true; } } } else { scheduleDiscovery = true; } if (scheduleDiscovery) { // Schedule the first discovery immediately. this.scheduledFuture = this.scheduledExecutor.schedule(this::getControllers, 0L, TimeUnit.SECONDS); } else { log.info("Updating client with controllers: {}", servers); this.resolverUpdater.onAddresses(servers, Attributes.EMPTY); } }
Example 18
Source File: MachineList.java From hadoop with Apache License 2.0 | 4 votes |
/** * Accepts a collection of ip/cidr/host addresses * * @param hostEntries * @param addressFactory addressFactory to convert host to InetAddress */ public MachineList(Collection<String> hostEntries, InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.size() == 1) && (hostEntries.contains(WILDCARD_VALUE))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; Set<String> ips = new HashSet<String>(); List<SubnetUtils.SubnetInfo> cidrs = new LinkedList<SubnetUtils.SubnetInfo>(); Set<String> hosts = new HashSet<String>(); for (String hostEntry : hostEntries) { //ip address range if (hostEntry.indexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.setInclusiveHostCount(true); cidrs.add(subnet.getInfo()); } catch (IllegalArgumentException e) { LOG.warn("Invalid CIDR syntax : " + hostEntry); throw e; } } else if (InetAddresses.isInetAddress(hostEntry)) { //ip address ips.add(hostEntry); } else { //hostname hosts.add(hostEntry); } } ipAddresses = (ips.size() > 0) ? ips : null; cidrAddresses = (cidrs.size() > 0) ? cidrs : null; hostNames = (hosts.size() > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }
Example 19
Source File: InternetUtils.java From vividus with Apache License 2.0 | 4 votes |
private static String getTopDomain(String host) { return host.contains(DOMAIN_PARTS_SEPARATOR) && !InetAddresses.isInetAddress(host) ? InternetDomainName.from(host).topPrivateDomain().toString() : host; }
Example 20
Source File: NeutronNetworkingApi.java From attic-stratos with Apache License 2.0 | 2 votes |
/** * Check whether the given IP is valid. * * @param ip IP to be validated * @return true if valid, false otherwise */ private boolean isValidIP(String ip) { return (ip != null && InetAddresses.isInetAddress(ip)); }