2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.network.internal.utils;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.net.ConnectException;
19 import java.net.DatagramPacket;
20 import java.net.DatagramSocket;
21 import java.net.Inet4Address;
22 import java.net.InetAddress;
23 import java.net.InetSocketAddress;
24 import java.net.NetworkInterface;
25 import java.net.NoRouteToHostException;
26 import java.net.PortUnreachableException;
27 import java.net.Socket;
28 import java.net.SocketAddress;
29 import java.net.SocketException;
30 import java.net.SocketTimeoutException;
31 import java.time.Duration;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.LinkedHashSet;
35 import java.util.Optional;
37 import java.util.stream.Collectors;
39 import org.apache.commons.lang3.SystemUtils;
40 import org.apache.commons.net.util.SubnetUtils;
41 import org.eclipse.jdt.annotation.NonNullByDefault;
42 import org.eclipse.jdt.annotation.Nullable;
43 import org.openhab.core.io.net.exec.ExecUtil;
44 import org.openhab.core.net.CidrAddress;
45 import org.openhab.core.net.NetUtil;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Network utility functions for pinging and for determining all interfaces and assigned IP addresses.
52 * @author David Graeff - Initial contribution
55 public class NetworkUtils {
56 private final Logger logger = LoggerFactory.getLogger(NetworkUtils.class);
58 private LatencyParser latencyParser = new LatencyParser();
61 * Gets every IPv4 Address on each Interface except the loopback
62 * The Address format is ip/subnet
64 * @return The collected IPv4 Addresses
66 public Set<CidrAddress> getInterfaceIPs() {
67 return NetUtil.getAllInterfaceAddresses().stream().filter(a -> a.getAddress() instanceof Inet4Address)
68 .collect(Collectors.toSet());
72 * Get a set of all interface names.
74 * @return Set of interface names
76 public Set<String> getInterfaceNames() {
77 Set<String> result = new HashSet<>();
80 // For each interface ...
81 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
82 NetworkInterface networkInterface = en.nextElement();
83 if (!networkInterface.isLoopback()) {
84 result.add(networkInterface.getName());
87 } catch (SocketException ignored) {
88 // If we are not allowed to enumerate, we return an empty result set.
95 * Determines every IP which can be assigned on all available interfaces
97 * @param maximumPerInterface The maximum of IP addresses per interface or 0 to get all.
98 * @return Every single IP which can be assigned on the Networks the computer is connected to
100 public Set<String> getNetworkIPs(int maximumPerInterface) {
101 return getNetworkIPs(getInterfaceIPs(), maximumPerInterface);
105 * Takes the interfaceIPs and fetches every IP which can be assigned on their network
107 * @param interfaceIPs The IPs which are assigned to the Network Interfaces
108 * @param maximumPerInterface The maximum of IP addresses per interface or 0 to get all.
109 * @return Every single IP which can be assigned on the Networks the computer is connected to
111 public Set<String> getNetworkIPs(Set<CidrAddress> interfaceIPs, int maximumPerInterface) {
112 LinkedHashSet<String> networkIPs = new LinkedHashSet<>();
114 short minCidrPrefixLength = 8; // historic Class A network, addresses = 16777214
115 if (maximumPerInterface != 0) {
116 // calculate minimum CIDR prefix length from maximumPerInterface
117 // (equals leading unset bits (Integer has 32 bits)
118 minCidrPrefixLength = (short) Integer.numberOfLeadingZeros(maximumPerInterface);
119 if (Integer.bitCount(maximumPerInterface) == 1) {
120 // if only the highest is set, decrease prefix by 1 to cover all addresses
121 minCidrPrefixLength--;
124 logger.trace("set minCidrPrefixLength to {}, maximumPerInterface is {}", minCidrPrefixLength,
125 maximumPerInterface);
127 for (CidrAddress cidrNotation : interfaceIPs) {
128 if (cidrNotation.getPrefix() < minCidrPrefixLength) {
130 "CIDR prefix is smaller than /{} on interface with address {}, truncating to /{}, some addresses might be lost",
131 minCidrPrefixLength, cidrNotation, minCidrPrefixLength);
132 cidrNotation = new CidrAddress(cidrNotation.getAddress(), minCidrPrefixLength);
135 SubnetUtils utils = new SubnetUtils(cidrNotation.toString());
136 String[] addresses = utils.getInfo().getAllAddresses();
137 int len = addresses.length;
138 if (maximumPerInterface != 0 && maximumPerInterface < len) {
139 len = maximumPerInterface;
141 for (int i = 0; i < len; i++) {
142 networkIPs.add(addresses[i]);
150 * Try to establish a tcp connection to the given port. Returns false if a timeout occurred
151 * or the connection was denied.
153 * @param host The IP or hostname
154 * @param port The tcp port. Must be not 0.
155 * @param timeout Timeout in ms
156 * @return Ping result information. Optional is empty if ping command was not executed.
157 * @throws IOException
159 public Optional<PingResult> servicePing(String host, int port, int timeout) throws IOException {
160 double execStartTimeInMS = System.currentTimeMillis();
162 SocketAddress socketAddress = new InetSocketAddress(host, port);
163 try (Socket socket = new Socket()) {
164 socket.connect(socketAddress, timeout);
165 return Optional.of(new PingResult(true, System.currentTimeMillis() - execStartTimeInMS));
166 } catch (ConnectException | SocketTimeoutException | NoRouteToHostException ignored) {
167 return Optional.of(new PingResult(false, System.currentTimeMillis() - execStartTimeInMS));
172 * Return the working method for the native system ping. If no native ping
173 * works JavaPing is returned.
175 public IpPingMethodEnum determinePingMethod() {
176 IpPingMethodEnum method;
177 if (SystemUtils.IS_OS_WINDOWS) {
178 method = IpPingMethodEnum.WINDOWS_PING;
179 } else if (SystemUtils.IS_OS_MAC) {
180 method = IpPingMethodEnum.MAC_OS_PING;
181 } else if (SystemUtils.IS_OS_UNIX) {
182 method = IpPingMethodEnum.IPUTILS_LINUX_PING;
184 // We cannot estimate the command line for any other operating system and just return false
185 return IpPingMethodEnum.JAVA_PING;
189 Optional<PingResult> pingResult = nativePing(method, "127.0.0.1", 1000);
190 if (pingResult.isPresent() && pingResult.get().isSuccess()) {
193 } catch (IOException ignored) {
194 } catch (InterruptedException e) {
195 Thread.currentThread().interrupt(); // Reset interrupt flag
197 return IpPingMethodEnum.JAVA_PING;
201 * Return true if the external arp ping utility (arping) is available and executable on the given path.
203 public ArpPingUtilEnum determineNativeARPpingMethod(String arpToolPath) {
204 String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofMillis(100), arpToolPath, "--help");
205 if (result == null || result.isBlank()) {
206 return ArpPingUtilEnum.DISABLED_UNKNOWN_TOOL;
207 } else if (result.contains("Thomas Habets")) {
208 if (result.matches("(?s)(.*)w sec Specify a timeout(.*)")) {
209 return ArpPingUtilEnum.THOMAS_HABERT_ARPING;
211 return ArpPingUtilEnum.THOMAS_HABERT_ARPING_WITHOUT_TIMEOUT;
213 } else if (result.contains("-w timeout") || result.contains("-w <timeout>")) {
214 return ArpPingUtilEnum.IPUTILS_ARPING;
215 } else if (result.contains("Usage: arp-ping.exe")) {
216 return ArpPingUtilEnum.ELI_FULKERSON_ARP_PING_FOR_WINDOWS;
218 return ArpPingUtilEnum.DISABLED_UNKNOWN_TOOL;
221 public enum IpPingMethodEnum {
229 * Use the native ping utility of the operating system to detect device presence.
231 * @param hostname The DNS name, IPv4 or IPv6 address. Must not be null.
232 * @param timeoutInMS Timeout in milliseconds. Be aware that DNS resolution is not part of this timeout.
233 * @return Ping result information. Optional is empty if ping command was not executed.
234 * @throws IOException The ping command could probably not be found
236 public Optional<PingResult> nativePing(@Nullable IpPingMethodEnum method, String hostname, int timeoutInMS)
237 throws IOException, InterruptedException {
238 double execStartTimeInMS = System.currentTimeMillis();
241 if (method == null) {
242 return Optional.empty();
244 // Yes, all supported operating systems have their own ping utility with a different command line
246 case IPUTILS_LINUX_PING:
247 proc = new ProcessBuilder("ping", "-w", String.valueOf(timeoutInMS / 1000), "-c", "1", hostname)
251 proc = new ProcessBuilder("ping", "-t", String.valueOf(timeoutInMS / 1000), "-c", "1", hostname)
255 proc = new ProcessBuilder("ping", "-w", String.valueOf(timeoutInMS), "-n", "1", hostname).start();
259 // We cannot estimate the command line for any other operating system and just return false
260 return Optional.empty();
263 // The return code is 0 for a successful ping, 1 if device didn't
264 // respond, and 2 if there is another error like network interface
266 // Exception: return code is also 0 in Windows for all requests on the local subnet.
267 // see https://superuser.com/questions/403905/ping-from-windows-7-get-no-reply-but-sets-errorlevel-to-0
269 int result = proc.waitFor();
271 return Optional.of(new PingResult(false, System.currentTimeMillis() - execStartTimeInMS));
274 try (BufferedReader r = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
275 String line = r.readLine();
277 throw new IOException("Received no output from ping process.");
280 // Because of the Windows issue, we need to check this. We assume that the ping was successful whenever
281 // this specific string is contained in the output
282 if (line.contains("TTL=") || line.contains("ttl=")) {
283 PingResult pingResult = new PingResult(true, System.currentTimeMillis() - execStartTimeInMS);
284 latencyParser.parseLatency(line).ifPresent(pingResult::setResponseTimeInMS);
285 return Optional.of(pingResult);
288 } while (line != null);
290 return Optional.of(new PingResult(false, System.currentTimeMillis() - execStartTimeInMS));
294 public enum ArpPingUtilEnum {
295 DISABLED("Disabled", false),
296 DISABLED_INVALID_IP("Destination is not a valid IPv4 address", false),
297 DISABLED_UNKNOWN_TOOL("Unknown arping tool", false),
298 IPUTILS_ARPING("Iputils Arping", true),
299 THOMAS_HABERT_ARPING("Arping tool by Thomas Habets", true),
300 THOMAS_HABERT_ARPING_WITHOUT_TIMEOUT("Arping tool by Thomas Habets (old version)", true),
301 ELI_FULKERSON_ARP_PING_FOR_WINDOWS("Eli Fulkerson ARPing tool for Windows", true);
303 public final String description;
304 public final boolean canProceed;
306 ArpPingUtilEnum(String description, boolean canProceed) {
307 this.description = description;
308 this.canProceed = canProceed;
313 * Execute the arping tool to perform an ARP ping (only for IPv4 addresses).
314 * There exist two different arping utils with the same name unfortunatelly.
315 * * iputils arping which is sometimes preinstalled on fedora/ubuntu and the
316 * * https://github.com/ThomasHabets/arping which also works on Windows and MacOS.
318 * @param arpUtilPath The arping absolute path including filename. Example: "arping" or "/usr/bin/arping" or
319 * "C:\something\arping.exe" or "arp-ping.exe"
320 * @param interfaceName An interface name, on linux for example "wlp58s0", shown by ifconfig. Must not be null.
321 * @param ipV4address The ipV4 address. Must not be null.
322 * @param timeoutInMS A timeout in milliseconds
323 * @return Ping result information. Optional is empty if ping command was not executed.
324 * @throws IOException The ping command could probably not be found
326 public Optional<PingResult> nativeARPPing(@Nullable ArpPingUtilEnum arpingTool, @Nullable String arpUtilPath,
327 String interfaceName, String ipV4address, int timeoutInMS) throws IOException, InterruptedException {
328 double execStartTimeInMS = System.currentTimeMillis();
330 if (arpUtilPath == null || arpingTool == null || !arpingTool.canProceed) {
331 return Optional.empty();
334 if (arpingTool == ArpPingUtilEnum.THOMAS_HABERT_ARPING_WITHOUT_TIMEOUT) {
335 proc = new ProcessBuilder(arpUtilPath, "-c", "1", "-i", interfaceName, ipV4address).start();
336 } else if (arpingTool == ArpPingUtilEnum.THOMAS_HABERT_ARPING) {
337 proc = new ProcessBuilder(arpUtilPath, "-w", String.valueOf(timeoutInMS / 1000), "-C", "1", "-i",
338 interfaceName, ipV4address).start();
339 } else if (arpingTool == ArpPingUtilEnum.ELI_FULKERSON_ARP_PING_FOR_WINDOWS) {
340 proc = new ProcessBuilder(arpUtilPath, "-w", String.valueOf(timeoutInMS), "-x", ipV4address).start();
342 proc = new ProcessBuilder(arpUtilPath, "-w", String.valueOf(timeoutInMS / 1000), "-c", "1", "-I",
343 interfaceName, ipV4address).start();
346 // The return code is 0 for a successful ping. 1 if device didn't respond and 2 if there is another error like
347 // network interface not ready.
348 return Optional.of(new PingResult(proc.waitFor() == 0, System.currentTimeMillis() - execStartTimeInMS));
352 * Execute a Java ping.
354 * @param timeoutInMS A timeout in milliseconds
355 * @param destinationAddress The address to check
356 * @return Ping result information. Optional is empty if ping command was not executed.
358 public Optional<PingResult> javaPing(int timeoutInMS, InetAddress destinationAddress) {
359 double execStartTimeInMS = System.currentTimeMillis();
362 if (destinationAddress.isReachable(timeoutInMS)) {
363 return Optional.of(new PingResult(true, System.currentTimeMillis() - execStartTimeInMS));
365 return Optional.of(new PingResult(false, System.currentTimeMillis() - execStartTimeInMS));
367 } catch (IOException e) {
368 return Optional.of(new PingResult(false, System.currentTimeMillis() - execStartTimeInMS));
373 * iOS devices are in a deep sleep mode, where they only listen to UDP traffic on port 5353 (Bonjour service
374 * discovery). A packet on port 5353 will wake up the network stack to respond to ARP pings at least.
376 * @throws IOException
378 public void wakeUpIOS(InetAddress address) throws IOException {
379 try (DatagramSocket s = new DatagramSocket()) {
380 byte[] buffer = new byte[0];
381 s.send(new DatagramPacket(buffer, buffer.length, address, 5353));
382 } catch (PortUnreachableException ignored) {
383 // We ignore the port unreachable error