]> git.basschouten.com Git - openhab-addons.git/blob
913e8230665125e694aadf33c17ddefb124b6ed1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lgwebos.internal;
14
15 import java.io.IOException;
16 import java.net.DatagramPacket;
17 import java.net.DatagramSocket;
18 import java.net.InetAddress;
19 import java.net.InterfaceAddress;
20 import java.net.NetworkInterface;
21 import java.time.Duration;
22 import java.util.Enumeration;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import java.util.stream.Stream;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.core.io.net.exec.ExecUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Class with utility functions to support Wake On Lan (WOL)
35  *
36  * @author Arjan Mels - Initial contribution
37  * @author Sebastian Prehn - Modification to getMACAddress
38  *
39  */
40 @NonNullByDefault
41 public class WakeOnLanUtility {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(WakeOnLanUtility.class);
44     private static final Pattern MAC_REGEX = Pattern.compile("(([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2})");
45     private static final int CMD_TIMEOUT_MS = 1000;
46
47     private static final String COMMAND;
48     static {
49         String os = System.getProperty("os.name").toLowerCase();
50         LOGGER.debug("os: {}", os);
51         if ((os.indexOf("win") >= 0)) {
52             COMMAND = "arp -a %s";
53         } else if ((os.indexOf("mac") >= 0)) {
54             COMMAND = "arp %s";
55         } else { // linux
56             if (checkIfLinuxCommandExists("arp")) {
57                 COMMAND = "arp %s";
58             } else if (checkIfLinuxCommandExists("arping")) { // typically OH provided docker image
59                 COMMAND = "arping -r -c 1 -C 1 %s";
60             } else {
61                 COMMAND = "";
62             }
63         }
64     }
65
66     /**
67      * Get MAC address for host
68      *
69      * @param hostName Host Name (or IP address) of host to retrieve MAC address for
70      * @return MAC address
71      */
72     public static @Nullable String getMACAddress(String hostName) {
73         if (COMMAND.isEmpty()) {
74             LOGGER.debug("MAC address detection not possible. No command to identify MAC found.");
75             return null;
76         }
77
78         String[] cmds = Stream.of(COMMAND.split(" ")).map(arg -> String.format(arg, hostName)).toArray(String[]::new);
79         String response = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofMillis(CMD_TIMEOUT_MS), cmds);
80         Matcher matcher = MAC_REGEX.matcher(response);
81         String macAddress = null;
82
83         while (matcher.find()) {
84             String group = matcher.group();
85
86             if (group.length() == 17) {
87                 macAddress = group;
88                 break;
89             }
90         }
91
92         if (macAddress != null) {
93             LOGGER.debug("MAC address of host {} is {}", hostName, macAddress);
94         } else {
95             LOGGER.debug("Problem executing command {} to retrieve MAC address for {}: {}",
96                     String.format(COMMAND, hostName), hostName, response);
97         }
98         return macAddress;
99     }
100
101     /**
102      * Send single WOL (Wake On Lan) package on all interfaces
103      *
104      * @macAddress MAC address to send WOL package to
105      */
106     public static void sendWOLPacket(String macAddress) {
107         byte[] bytes = getWOLPackage(macAddress);
108
109         try {
110             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
111             while (interfaces.hasMoreElements()) {
112                 NetworkInterface networkInterface = interfaces.nextElement();
113                 if (networkInterface.isLoopback()) {
114                     continue; // Do not want to use the loopback interface.
115                 }
116                 for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
117                     InetAddress broadcast = interfaceAddress.getBroadcast();
118                     if (broadcast == null) {
119                         continue;
120                     }
121
122                     DatagramPacket packet = new DatagramPacket(bytes, bytes.length, broadcast, 9);
123                     try (DatagramSocket socket = new DatagramSocket()) {
124                         socket.send(packet);
125                         LOGGER.trace("Sent WOL packet to {} {}", broadcast, macAddress);
126                     } catch (IOException e) {
127                         LOGGER.warn("Problem sending WOL packet to {} {}", broadcast, macAddress);
128                     }
129                 }
130             }
131
132         } catch (IOException e) {
133             LOGGER.warn("Problem with interface while sending WOL packet to {}", macAddress);
134         }
135     }
136
137     /**
138      * Create WOL UDP package: 6 bytes 0xff and then 16 times the 6 byte mac address repeated
139      *
140      * @param macStr String representation of the MAC address (either with : or -)
141      * @return byte array with the WOL package
142      * @throws IllegalArgumentException
143      */
144     private static byte[] getWOLPackage(String macStr) throws IllegalArgumentException {
145         byte[] macBytes = new byte[6];
146         String[] hex = macStr.split("(\\:|\\-)");
147         if (hex.length != 6) {
148             throw new IllegalArgumentException("Invalid MAC address.");
149         }
150         try {
151             for (int i = 0; i < 6; i++) {
152                 macBytes[i] = (byte) Integer.parseInt(hex[i], 16);
153             }
154         } catch (NumberFormatException e) {
155             throw new IllegalArgumentException("Invalid hex digit in MAC address.");
156         }
157
158         byte[] bytes = new byte[6 + 16 * macBytes.length];
159         for (int i = 0; i < 6; i++) {
160             bytes[i] = (byte) 0xff;
161         }
162         for (int i = 6; i < bytes.length; i += macBytes.length) {
163             System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
164         }
165
166         return bytes;
167     }
168
169     private static boolean checkIfLinuxCommandExists(String cmd) {
170         try {
171             return 0 == Runtime.getRuntime().exec(String.format("which %s", cmd)).waitFor();
172         } catch (InterruptedException | IOException e) {
173             LOGGER.debug("Error trying to check if command {} exists: {}", cmd, e.getMessage());
174         }
175         return false;
176     }
177 }