/** * @param hostName * @return ip address or hostName if UnknownHostException */ public static String getIpByHost(String hostName) { try { return InetAddress.getByName(hostName).getHostAddress(); } catch (UnknownHostException e) { return hostName; } }
private static String generateId() { String host; try { host = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ex) { host = "unknown"; } return host + '-' + UUID.randomUUID(); }
public static String toAddressString(InetSocketAddress address) { return address.getAddress().getHostAddress() + ":" + address.getPort(); }
public HostInfo() { String hostName; String hostAddress; try { InetAddress localhost = InetAddress.getLocalHost(); hostName = localhost.getHostName(); hostAddress = localhost.getHostAddress(); } catch (UnknownHostException e) { hostName = "localhost"; hostAddress = "127.0.0.1"; } HOST_NAME = hostName; HOST_ADDRESS = hostAddress; }
@Override public Channel getChannel(InetSocketAddress remoteAddress) { String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName(); int port = remoteAddress.getPort(); Channel channel = super.getChannel(remoteAddress); if (channel == null) { for (Map.Entry<URL, Client> entry : clients.entrySet()) { URL url = entry.getKey(); if (url.getIp().equals(host) && url.getPort() == port) { return entry.getValue(); } } } return channel; }
private boolean isAllowedToImpersonateFromHost(Set<String> authorizedHosts, InetAddress remoteAddress) { return authorizedHosts.contains(WILD_CARD) || authorizedHosts.contains(remoteAddress.getCanonicalHostName()) || authorizedHosts.contains(remoteAddress.getHostName()) || authorizedHosts.contains(remoteAddress.getHostAddress()); }
/** * Obtain a "host" from an {@link InetSocketAddress}. This returns a string containing either an * actual host name or a numeric IP address. */ // Visible for testing static String getHostString(InetSocketAddress socketAddress) { InetAddress address = socketAddress.getAddress(); if (address == null) { // The InetSocketAddress was specified with a string (either a numeric IP or a host name). If // it is a name, all IPs for that name should be tried. If it is an IP address, only that IP // address should be tried. return socketAddress.getHostName(); } // The InetSocketAddress has a specific address: we should only try that address. Therefore we // return the address and ignore any host name that may be available. return address.getHostAddress(); }
@Test public void shouldDetermineIfAddressIsLocal() throws UnknownHostException { InetAddress local; try { local = InetAddress.getLocalHost(); } catch (UnknownHostException e) { local = InetAddress.getByName("localhost"); } assertThat("Localhost (" + local.getHostName() + ") should be a local address.", SystemUtil.isLocalhost(local.getHostAddress()), is(true)); }
InetAddress inetAddress = bindAddress.getAddress(); final String address; address = config.getString(JobManagerOptions.ADDRESS, InetAddress.getLocalHost().getHostName()); } else { address = inetAddress.getHostAddress(); int port = bindAddress.getPort();
/** * Get the ip address of local host. */ public static String getHostAddress() throws SocketException, UnknownHostException { DatagramSocket ds = new DatagramSocket(); ds.connect(InetAddress.getByName(DUMMY_OUT_IP), 80); InetAddress localAddress = ds.getLocalAddress(); if (localAddress.getHostAddress().equals("0.0.0.0")) { localAddress = InetAddress.getLocalHost(); } return localAddress.getHostAddress(); }
/** * Gets the InetAddress of the SOCKS proxy server. */ private InetAddress socksGetServerAddress() throws UnknownHostException { String proxyName; // get socks server address from proxy. It is unnecessary to check // "socksProxyHost" property, since all proxy setting should be // determined by ProxySelector. InetSocketAddress addr = (InetSocketAddress) proxy.address(); proxyName = addr.getHostName(); if (proxyName == null) { proxyName = addr.getAddress().getHostAddress(); } return InetAddress.getByName(proxyName); }
public HostInfo() { String hostName; String hostAddress; try { InetAddress localhost = InetAddress.getLocalHost(); hostName = localhost.getHostName(); hostAddress = localhost.getHostAddress(); } catch (UnknownHostException e) { hostName = "localhost"; hostAddress = "127.0.0.1"; } HOST_NAME = hostName; HOST_ADDRESS = hostAddress; }
/** * @param hostName * @return ip address or hostName if UnknownHostException */ public static String getIpByHost(String hostName) { try { return InetAddress.getByName(hostName).getHostAddress(); } catch (UnknownHostException e) { return hostName; } }
public static String toAddressString(InetSocketAddress address) { return address.getAddress().getHostAddress() + ":" + address.getPort(); }
@Override public ExchangeChannel getExchangeChannel(InetSocketAddress remoteAddress) { String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName(); int port = remoteAddress.getPort(); ExchangeChannel channel = super.getExchangeChannel(remoteAddress); if (channel == null) { for (Map.Entry<URL, ExchangeClient> entry : clients.entrySet()) { URL url = entry.getKey(); if (url.getIp().equals(host) && url.getPort() == port) { return entry.getValue(); } } } return channel; }
public HTTPSession(NanoHTTPD httpd, ITempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { this.httpd = httpd; this.tempFileManager = tempFileManager; this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE); this.outputStream = outputStream; this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString(); this.headers = new HashMap<String, String>(); }
public static String ip() { String hostname = null; try { hostname = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { LOG.error("local_hostname", e); } return hostname; }
/** * get remote host. * * @return remote host */ public String getRemoteHost() { return remoteAddress == null ? null : remoteAddress.getAddress() == null ? remoteAddress.getHostName() : NetUtils.filterLocalHost(remoteAddress.getAddress().getHostAddress()); }
String hostname1 = InetAddress.getLocalHost().getHostName(); String host1 = InetAddress.getLocalHost().getHostAddress(); String hostname2 = "neo4j.org"; assertDoesNotThrow( () -> InetAddress.getByName( hostname2 ) ); assumeFalse( hostname1.equals( hostname2 ) );
@Test void testMatchesIPWithHostUnknown() throws Exception { // Given String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); String host1 = InetAddress.getLocalHost().getHostAddress(); HostnamePort hostnamePortSinglePort = new HostnamePort( host1 + ":1234" ); HostnamePort hostnamePortWithRange = new HostnamePort( host1 + ":1234-1236" ); // When & Then // should return false if matched with any unknown host assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1234") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1235") ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://" + unknownHost + ":1236") ) ); }