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 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(); }
Enumeration<InetAddress> e = i.getInetAddresses(); while (e.hasMoreElements()) { addresses.add(NetworkUtils.withCanonicalAddress(e.nextElement()));
/** * 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)); } }