Use the length of MAC address as the last property to compare to get the best MAC address
Motivation: Some operating systems like Windows 7 uses a valid globally unique EUI-64 MAC address for a virtual device (e.g. 00:00:00:00:00:00:00:E0), and because it's usually longer than the legit MAC-48 address, we should not use the length of MAC address when two MAC addresses are of the same quality. Instead, we should compare the INET address of the NICs before comparing the length of the MAC addresses. Modification: Compare the length of MAC addresses as a last resort. Result: Correct MAC address detection in Windows with IPv6 enabled.
This commit is contained in:
parent
6cb238a673
commit
d641b6a97a
@ -169,17 +169,30 @@ final class DefaultChannelId implements ChannelId {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean replace = false;
|
||||
int res = compareAddresses(bestMacAddr, macAddr);
|
||||
if (res < 0) {
|
||||
bestMacAddr = macAddr;
|
||||
bestInetAddr = inetAddr;
|
||||
// Found a better MAC address.
|
||||
replace = true;
|
||||
} else if (res == 0) {
|
||||
if (compareAddresses(bestInetAddr, inetAddr) < 0) {
|
||||
// Two MAC addresses are of pretty much same quality.
|
||||
res = compareAddresses(bestInetAddr, inetAddr);
|
||||
if (res < 0) {
|
||||
// Found a MAC address with better INET address.
|
||||
replace = true;
|
||||
} else if (res == 0) {
|
||||
// Cannot tell the difference. Choose the longer one.
|
||||
if (bestMacAddr.length < macAddr.length) {
|
||||
replace = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (replace) {
|
||||
bestMacAddr = macAddr;
|
||||
bestInetAddr = inetAddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestMacAddr == NOT_FOUND) {
|
||||
bestMacAddr = new byte[MACHINE_ID_LEN];
|
||||
@ -236,18 +249,22 @@ final class DefaultChannelId implements ChannelId {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Prefer longer globally unique addresses.
|
||||
// Prefer globally unique address.
|
||||
if ((current[0] & 2) == 0) {
|
||||
if ((candidate[0] & 2) == 0) {
|
||||
return current.length - candidate.length;
|
||||
// Both current and candidate are globally unique addresses.
|
||||
return 0;
|
||||
} else {
|
||||
// Only current is globally unique.
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if ((candidate[0] & 2) == 0) {
|
||||
// Only candidate is globally unique.
|
||||
return -1;
|
||||
} else {
|
||||
return current.length - candidate.length;
|
||||
// Both current and candidate are non-unique.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user