diff --git a/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java b/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java index 822e29e2fcb..0ba7e58c8a7 100644 --- a/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java +++ b/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java @@ -79,7 +79,7 @@ /** * This is a {@link IpAddonFinder} for finding suggested add-ons by sending IP packets to the * network and collecting responses. - * + * * This finder is intended to detect devices on the network which do not announce via UPnP * or mDNS. Some devices respond to queries to defined multicast addresses and ports and thus * can be detected by sending a single frame on the IP network. @@ -363,10 +363,9 @@ private void scan() { } } String macFormat = parameters.getOrDefault(PARAMETER_MAC_FORMAT, "%02X:"); - try { - String.format(macFormat, 123); - } catch (IllegalFormatException e) { + if (!macFormatValid(macFormat)) { logger.warn("{}: discovery-parameter '{}' invalid format specifier", candidate.getUID(), macFormat); + continue; } // handle known types @@ -599,7 +598,7 @@ private boolean isAddonInstalled(String addonId) { /** * Get mac address bytes associated with the given Internet socket address - * + * * @param inetSocketAddress the Internet address * @return the mac address as an array of bytes * @throws SocketException if address is not on this PC, or no mac address is associated @@ -614,10 +613,10 @@ private byte[] macBytesFrom(InetSocketAddress inetSocketAddress) throws SocketEx /** * Use the given format specifier to format an array of mac address bytes - * + * * @param format a standard format specifier; optionally ends with a delimiter e.g. "%02x:" or "%02X" * @param bytes the mac address as an array of bytes - * + * * @return e.g. '01:02:03:04:A5:B6:C7:D8', '01-02-03-04:a5:b6:c7:d8', or '01020304A5B6C7D8' */ private String macFormat(String format, byte[] bytes) { @@ -625,7 +624,33 @@ private String macFormat(String format, byte[] bytes) { for (byte byt : bytes) { result.append(String.format(format, byt)); } - boolean isDelimited = Set.of(':', '-', '.').contains(format.charAt(format.length() - 1)); + boolean isDelimited = !Character.isLetterOrDigit(format.charAt(format.length() - 1)); return (isDelimited ? result.substring(0, result.length() - 1) : result).toString(); } + + /** + * Check if the given mac format specifier string is valid + */ + private boolean macFormatValid(String format) { + try { + String.format(format, (byte) 123); + } catch (IllegalFormatException e) { + return false; + } + int last = format.length() - 1; + int index = 0; + while (index <= last) { + if (Character.isLetter(format.charAt(index))) { + break; + } + index++; + } + switch (last - index) { + case 0: + return true; + case 1: + return !Character.isLetterOrDigit(format.charAt(last)); + } + return false; + } }