|
22 | 22 | import org.apache.lucene.util.BytesRef;
|
23 | 23 | import org.apache.lucene.util.Constants;
|
24 | 24 |
|
| 25 | +import java.io.IOException; |
25 | 26 | import java.net.Inet4Address;
|
26 | 27 | import java.net.Inet6Address;
|
27 | 28 | import java.net.InetAddress;
|
|
33 | 34 | import java.util.Comparator;
|
34 | 35 | import java.util.List;
|
35 | 36 | import java.util.Optional;
|
| 37 | +import java.util.function.Predicate; |
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * Utilities for network interfaces / addresses binding and publishing.
|
@@ -150,77 +152,53 @@ public static boolean defaultReuseAddress() {
|
150 | 152 | return Constants.WINDOWS ? false : true;
|
151 | 153 | }
|
152 | 154 |
|
153 |
| - /** Returns all interface-local scope (loopback) addresses for interfaces that are up. */ |
154 |
| - static InetAddress[] getLoopbackAddresses() throws SocketException { |
155 |
| - List<InetAddress> list = new ArrayList<>(); |
156 |
| - for (NetworkInterface intf : getInterfaces()) { |
157 |
| - if (intf.isUp()) { |
158 |
| - for (InetAddress address : Collections.list(intf.getInetAddresses())) { |
159 |
| - if (address.isLoopbackAddress()) { |
160 |
| - list.add(address); |
161 |
| - } |
| 155 | + private static InetAddress[] filterAllAddresses(final Predicate<InetAddress> predicate, final String message) throws IOException { |
| 156 | + final List<NetworkInterface> interfaces = getInterfaces(); |
| 157 | + final List<InetAddress> list = new ArrayList<>(); |
| 158 | + for (final NetworkInterface intf : interfaces) { |
| 159 | + for (final InetAddress address : Collections.list(intf.getInetAddresses())) { |
| 160 | + if (predicate.test(address) && isUp(intf)) { |
| 161 | + list.add(address); |
162 | 162 | }
|
163 | 163 | }
|
164 | 164 | }
|
165 | 165 | if (list.isEmpty()) {
|
166 |
| - throw new IllegalArgumentException("No up-and-running loopback addresses found, got " + getInterfaces()); |
| 166 | + throw new IllegalArgumentException(message + ", got " + interfaces); |
| 167 | + } |
| 168 | + return list.toArray(new InetAddress[0]); |
| 169 | + } |
| 170 | + |
| 171 | + private static boolean isUp(final NetworkInterface intf) throws IOException { |
| 172 | + try { |
| 173 | + return intf.isUp(); |
| 174 | + } catch (final SocketException e) { |
| 175 | + throw new IOException("failed to check if interface [" + intf.getName() + "] is up", e); |
167 | 176 | }
|
168 |
| - return list.toArray(new InetAddress[list.size()]); |
| 177 | + } |
| 178 | + |
| 179 | + /** Returns all interface-local scope (loopback) addresses for interfaces that are up. */ |
| 180 | + static InetAddress[] getLoopbackAddresses() throws IOException { |
| 181 | + return filterAllAddresses(InetAddress::isLoopbackAddress, "no up-and-running loopback addresses found"); |
169 | 182 | }
|
170 | 183 |
|
171 | 184 | /** Returns all site-local scope (private) addresses for interfaces that are up. */
|
172 |
| - static InetAddress[] getSiteLocalAddresses() throws SocketException { |
173 |
| - List<InetAddress> list = new ArrayList<>(); |
174 |
| - for (NetworkInterface intf : getInterfaces()) { |
175 |
| - if (intf.isUp()) { |
176 |
| - for (InetAddress address : Collections.list(intf.getInetAddresses())) { |
177 |
| - if (address.isSiteLocalAddress()) { |
178 |
| - list.add(address); |
179 |
| - } |
180 |
| - } |
181 |
| - } |
182 |
| - } |
183 |
| - if (list.isEmpty()) { |
184 |
| - throw new IllegalArgumentException("No up-and-running site-local (private) addresses found, got " + getInterfaces()); |
185 |
| - } |
186 |
| - return list.toArray(new InetAddress[list.size()]); |
| 185 | + static InetAddress[] getSiteLocalAddresses() throws IOException { |
| 186 | + return filterAllAddresses(InetAddress::isSiteLocalAddress, "No up-and-running site-local (private) addresses found"); |
187 | 187 | }
|
188 | 188 |
|
189 | 189 | /** Returns all global scope addresses for interfaces that are up. */
|
190 |
| - static InetAddress[] getGlobalAddresses() throws SocketException { |
191 |
| - List<InetAddress> list = new ArrayList<>(); |
192 |
| - for (NetworkInterface intf : getInterfaces()) { |
193 |
| - if (intf.isUp()) { |
194 |
| - for (InetAddress address : Collections.list(intf.getInetAddresses())) { |
195 |
| - if (address.isLoopbackAddress() == false && |
196 |
| - address.isSiteLocalAddress() == false && |
197 |
| - address.isLinkLocalAddress() == false) { |
198 |
| - list.add(address); |
199 |
| - } |
200 |
| - } |
201 |
| - } |
202 |
| - } |
203 |
| - if (list.isEmpty()) { |
204 |
| - throw new IllegalArgumentException("No up-and-running global-scope (public) addresses found, got " + getInterfaces()); |
205 |
| - } |
206 |
| - return list.toArray(new InetAddress[list.size()]); |
| 190 | + static InetAddress[] getGlobalAddresses() throws IOException { |
| 191 | + return filterAllAddresses( |
| 192 | + address -> address.isLoopbackAddress() == false |
| 193 | + && address.isSiteLocalAddress() == false |
| 194 | + && address.isLinkLocalAddress() == false, |
| 195 | + "no up-and-running global-scope (public) addresses found"); |
207 | 196 | }
|
208 | 197 |
|
209 | 198 | /** Returns all addresses (any scope) for interfaces that are up.
|
210 | 199 | * This is only used to pick a publish address, when the user set network.host to a wildcard */
|
211 |
| - static InetAddress[] getAllAddresses() throws SocketException { |
212 |
| - List<InetAddress> list = new ArrayList<>(); |
213 |
| - for (NetworkInterface intf : getInterfaces()) { |
214 |
| - if (intf.isUp()) { |
215 |
| - for (InetAddress address : Collections.list(intf.getInetAddresses())) { |
216 |
| - list.add(address); |
217 |
| - } |
218 |
| - } |
219 |
| - } |
220 |
| - if (list.isEmpty()) { |
221 |
| - throw new IllegalArgumentException("No up-and-running addresses found, got " + getInterfaces()); |
222 |
| - } |
223 |
| - return list.toArray(new InetAddress[list.size()]); |
| 200 | + static InetAddress[] getAllAddresses() throws IOException { |
| 201 | + return filterAllAddresses(address -> true, "no up-and-running addresses found"); |
224 | 202 | }
|
225 | 203 |
|
226 | 204 | /** Returns addresses for the given interface (it must be marked up) */
|
|
0 commit comments