Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP addon finder mac address format tweak #4481

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -614,18 +613,44 @@ 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) {
StringBuilder result = new StringBuilder();
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;
}
}