public static Supplier<List<InetAddress>> createAnyAddressSupplier() { String localHostAddresses = System.getProperty(NetworkUtils.LOCAL_HOST_ADDRESS_PROPERTY); if (!isNullOrEmpty(localHostAddresses)) { List<InetAddress> address = new ArrayList<>(); for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split(localHostAddresses)) { address.add(NetworkUtils.withCanonicalAddress(InetAddresses.forString(s))); } return () -> address; } return NetworkUtils.anyAddressSupplier(); } }
public static synchronized String getCanonicalHostName() { if (canonicalHostName == null) { canonicalHostName = getPreferredHostName(); } return canonicalHostName; }
private static Predicate<InetAddress> hasProtocolFamily(final ProtocolFamily protocolFamily) { return address -> getProtocolFamily(address) == protocolFamily; }
throws SocketException InetAddress localAddress = getLocalAddress(expectedSource, getProtocolFamily(expectedSource)); if (localAddress == null) { if (!FAKED_ADDRESSES.isEmpty()) { return Ordering.natural().onResultOf(InetAddressScope::of).min( filter(LOCAL_ADDRESS_SUPPLIER.get(), and(greaterThanOrEquals(minScope), isNotMulticast()))); } catch (NoSuchElementException e) { throw new SocketException("Unable to find address that faces " + expectedSource);
public HostListAddressSupplier(@Nullable String hosts) throws UnknownHostException { boolean wildcard = false; ImmutableList.Builder<InetAddress> builder = ImmutableList.builder(); for (String host : Splitter.on(' ').omitEmptyStrings().split(hosts)) { if (isInetAddress(host)) { InetAddress address = InetAddresses.forString(host); checkArgument(!address.isMulticastAddress(), "Invalid address %s: cannot publish a multicast address", host); if (address.isAnyLocalAddress()) { wildcard = true; } else { builder.add(withCanonicalAddress(address)); } } else { builder.add(InetAddress.getByName(host)); // REVISIT InetAddress#getAllByName ? } } dynamicAddresses = wildcard ? new AnyAddressSupplier() : () -> Collections.emptyList(); fixedAddresses = builder.build(); }
if (getProtocolFamily(address) == protocolFamily) { return address; return Ordering.natural().onResultOf(InetAddressScope::of).min( filter(LOCAL_ADDRESS_SUPPLIER.get(), and(greaterThanOrEquals(minScope), hasProtocolFamily(protocolFamily), isNotMulticast()))); } catch (NoSuchElementException e) { return null; if (getProtocolFamily(localAddress) != protocolFamily) { InetAddressScope intendedScope = InetAddressScope.of(expectedSource); NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(localAddress); return Ordering.natural().onResultOf(InetAddressScope::of).min( Iterators.filter(forEnumeration(byInetAddress.getInetAddresses()), and(greaterThanOrEquals(intendedScope), hasProtocolFamily(protocolFamily), isNotMulticast()))); } catch (NoSuchElementException e) { return null;
String address = NetworkUtils.getLocalAddress(InetAddress.getByName(remoteControlChannel.getHost())).getHostAddress(); int localPort = serverSocket.getLocalPort();
private static String getPreferredHostName() { List<InetAddress> addresses = Ordering.natural().onResultOf(InetAddressScope::of).reverse().sortedCopy(getLocalAddresses()); if (addresses.isEmpty()) { return "localhost"; } /* For legibility, we prefer to see a traditional * DNS host name; but if there is no mapping, * use the first address. */ for (InetAddress a: addresses) { String hostName = stripScope(a.getCanonicalHostName()); if (!InetAddresses.isInetAddress(hostName)) { return hostName; } } return addresses.get(0).getCanonicalHostName(); }
Enumeration<InetAddress> e = i.getInetAddresses(); while (e.hasMoreElements()) { addresses.add(NetworkUtils.withCanonicalAddress(e.nextElement()));
@Override public Connection getConnection(String username, String password) throws SQLException { try { return delegate().getConnection(username, password); } catch (SQLException sql) { LOGGER.error(AlarmMarkerFactory.getMarker(PredefinedAlarm.DB_CONNECTION_FAILURE, url, connectorName, NetworkUtils.getCanonicalHostName()), "Could not get connection to database", sql); throw sql; } }
/** * Sets both the port and address from the given socket address. */ public synchronized void setSocketAddress(InetSocketAddress socketAddress) { InetAddress address = socketAddress.getAddress(); checkArgument(!address.isMulticastAddress()); _port = socketAddress.getPort(); if (address.isAnyLocalAddress()) { setAddressSupplier(createAnyAddressSupplier()); } else if (NetworkUtils.isInetAddress(socketAddress.getHostString())) { InetAddress canonicalAddress = NetworkUtils.withCanonicalAddress(address); setAddressSupplier(() -> Collections.singletonList(canonicalAddress)); } else { setAddressSupplier(() -> Collections.singletonList(address)); } }
public HostPortList setStripedPassive(PortRange range, int queue) throws IOException { // remove existing sockets, if any socketPool.flush(); if (serverSocket == null) { ServerSocketChannel channel = ServerSocketChannel.open(); range.bind(channel.socket(), queue); serverSocket = channel.socket(); } gSession.serverMode = GridFTPSession.SERVER_EPAS; gSession.serverAddressList = new HostPortList(); String address = NetworkUtils.getLocalAddress(InetAddress.getByName(remoteControlChannel.getHost())).getHostAddress(); int localPort = serverSocket.getLocalPort(); HostPort hp = null; if (remoteControlChannel.isIPv6()) { String version = HostPort6.getIPAddressVersion(address); hp = new HostPort6(version, address, localPort); } else { hp = new HostPort(address, localPort); } gSession.serverAddressList.add(hp); logger.debug("started single striped passive server at port {}", gSession.serverAddressList.get(0).getPort()); return gSession.serverAddressList; }
@Override public Connection getConnection() throws SQLException { try { return delegate().getConnection(); } catch (SQLException sql) { LOGGER.error(AlarmMarkerFactory.getMarker(PredefinedAlarm.DB_CONNECTION_FAILURE, url, connectorName, NetworkUtils.getCanonicalHostName()), "Could not get connection to database", sql); throw sql; } }