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: add support for sending local mac address #4477

Merged
merged 4 commits into from
Dec 8, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
do not include colons in mac address byte form
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
  • Loading branch information
andrewfg committed Dec 6, 2024
commit c24f12d5262374fe2f99a9ae9d7c4967e3501717
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private byte[] buildRequestArrayPlain(SocketAddress address, String request)
req.replace(p, p + PARAMETER_SRC_PORT.length() + 1, "" + sock.getPort());
}
while ((p = req.indexOf("$" + PARAMETER_SRC_MAC)) != -1) {
req.replace(p, p + PARAMETER_SRC_MAC.length() + 1, macAddressFrom(sock));
req.replace(p, p + PARAMETER_SRC_MAC.length() + 1, macFormat(macBytesFrom(sock)));
}
while ((p = req.indexOf("$" + REPLACEMENT_UUID)) != -1) {
req.replace(p, p + REPLACEMENT_UUID.length() + 1, UUID.randomUUID().toString());
Expand Down Expand Up @@ -543,8 +543,8 @@ private byte[] buildRequestArray(SocketAddress address, String request) throws j
requestFrame.write((byte) (dPort & 0xff));
break;
case "$" + PARAMETER_SRC_MAC:
String mac = macAddressFrom(sock);
requestFrame.write(mac.getBytes());
byte[] mac = macBytesFrom(sock);
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
requestFrame.write(mac);
break;
case "$" + REPLACEMENT_UUID:
String uuid = UUID.randomUUID().toString();
Expand Down Expand Up @@ -584,21 +584,27 @@ private boolean isAddonInstalled(String addonId) {
}

/**
* Get mac address associated with given Internet socket address
* Get mac address bytes associated with the given Internet socket address
*
* @param inetSocketAddress the Internet address
* @return the mac address in colon delimited upper-case hex e.g. '01:02:03:04:A5:B6:C7:D8'
* @return the mac address as an array of bytes
* @throws SocketException if address is not on this PC, or no mac address is associated
*/
private String macAddressFrom(InetSocketAddress inetSocketAddress) throws SocketException {
private byte[] macBytesFrom(InetSocketAddress inetSocketAddress) throws SocketException {
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetSocketAddress.getAddress());
if (networkInterface == null) {
throw new SocketException("No network interface");
}
byte[] macBytes = networkInterface.getHardwareAddress();
if (macBytes == null) {
throw new SocketException("No mac address");
}
return networkInterface.getHardwareAddress();
}

/**
* Format an array of mac address bytes as a colon delimited upper-case hex string
*
* @param macBytes the mac address as an array of bytes
* @return e.g. '01:02:03:04:A5:B6:C7:D8'
*/
private String macFormat(byte[] macBytes) {
StringBuilder resultBuilder = new StringBuilder();
for (byte macByte : macBytes) {
resultBuilder.append(String.format("%02X:", macByte));
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
Expand Down