@Override public InetSocketAddress getLocalAddress() { if (client == null) { return InetSocketAddress.createUnresolved(NetUtils.getLocalHost(), 0); } else { return client.getLocalAddress(); } }
/** * get local host. * * @return local host */ public String getLocalHost() { String host = localAddress == null ? null : localAddress.getAddress() == null ? localAddress.getHostName() : NetUtils.filterLocalHost(localAddress.getAddress().getHostAddress()); if (host == null || host.length() == 0) { return NetUtils.getLocalHost(); } return host; }
public static String filterLocalHost(String host) { if (host == null || host.length() == 0) { return host; } if (host.contains("://")) { URL u = URL.valueOf(host); if (NetUtils.isInvalidLocalHost(u.getHost())) { return u.setHost(NetUtils.getLocalHost()).toFullString(); } } else if (host.contains(":")) { int i = host.lastIndexOf(':'); if (NetUtils.isInvalidLocalHost(host.substring(0, i))) { return NetUtils.getLocalHost() + host.substring(i); } } else { if (NetUtils.isInvalidLocalHost(host)) { return NetUtils.getLocalHost(); } } return host; }
@Override public Channel getChannel(InetSocketAddress remoteAddress) { return channels.get(NetUtils.toAddressString(remoteAddress)); }
/** * get remote host. * * @return remote host */ public String getRemoteHost() { return remoteAddress == null ? null : remoteAddress.getAddress() == null ? remoteAddress.getHostName() : NetUtils.filterLocalHost(remoteAddress.getAddress().getHostAddress()); }
public static boolean isValidLocalHost(String host) { return !isInvalidLocalHost(host); }
/** * Fetch IP address for this URL. * <p> * Pls. note that IP should be used instead of Host when to compare with socket's address or to search in a map * which use address as its key. * * @return ip in string format */ public String getIp() { if (ip == null) { ip = NetUtils.getIpByHost(host); } return ip; }
public static int getAvailablePort(int port) { if (port <= 0) { return getAvailablePort(); } for (int i = port; i < MAX_PORT; i++) { try (ServerSocket ss = new ServerSocket(i)) { return i; } catch (IOException e) { // continue } } return port; }
@Override public Channel getChannel(InetSocketAddress remoteAddress) { return channels.get(NetUtils.toAddressString(remoteAddress)); }
/** * get remote host. * * @return remote host */ public String getRemoteHost() { return remoteAddress == null ? null : remoteAddress.getAddress() == null ? remoteAddress.getHostName() : NetUtils.filterLocalHost(remoteAddress.getAddress().getHostAddress()); }
public static boolean isValidLocalHost(String host) { return !isInvalidLocalHost(host); }
/** * Fetch IP address for this URL. * <p> * Pls. note that IP should be used instead of Host when to compare with socket's address or to search in a map * which use address as its key. * * @return ip in string format */ public String getIp() { if (ip == null) { ip = NetUtils.getIpByHost(host); } return ip; }
public static int getAvailablePort(int port) { if (port <= 0) { return getAvailablePort(); } for (int i = port; i < MAX_PORT; i++) { try (ServerSocket ss = new ServerSocket(i)) { return i; } catch (IOException e) { // continue } } return port; }
@Override public InetSocketAddress getLocalAddress() { if (client == null) { return InetSocketAddress.createUnresolved(NetUtils.getLocalHost(), 0); } else { return client.getLocalAddress(); } }
public static String filterLocalHost(String host) { if (host == null || host.length() == 0) { return host; } if (host.contains("://")) { URL u = URL.valueOf(host); if (NetUtils.isInvalidLocalHost(u.getHost())) { return u.setHost(NetUtils.getLocalHost()).toFullString(); } } else if (host.contains(":")) { int i = host.lastIndexOf(':'); if (NetUtils.isInvalidLocalHost(host.substring(0, i))) { return NetUtils.getLocalHost() + host.substring(i); } } else { if (NetUtils.isInvalidLocalHost(host)) { return NetUtils.getLocalHost(); } } return host; }
/** * get local host. * * @return local host */ public String getLocalHost() { String host = localAddress == null ? null : localAddress.getAddress() == null ? localAddress.getHostName() : NetUtils.filterLocalHost(localAddress.getAddress().getHostAddress()); if (host == null || host.length() == 0) { return NetUtils.getLocalHost(); } return host; }
@Override public Channel getChannel(InetSocketAddress remoteAddress) { return channels.get(NetUtils.toAddressString(remoteAddress)); }
private static boolean isClientSide(Channel channel) { InetSocketAddress address = channel.getRemoteAddress(); URL url = channel.getUrl(); return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); }
public static InetSocketAddress getLocalSocketAddress(String host, int port) { return isInvalidLocalHost(host) ? new InetSocketAddress(port) : new InetSocketAddress(host, port); }
protected String getAddr(URL url) { String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost()); if (url.getParameter(Constants.ANYHOST_KEY, false)) { bindIp = Constants.ANYHOST_VALUE; } return NetUtils.getIpByHost(bindIp) + ":" + url.getParameter(Constants.BIND_PORT_KEY, url.getPort()); }