修复获取网卡物理地址的问题 (#704)
* Update Utilities.cs
修复无法获得正确的网卡物理地址异常
* Update UtilitiesTest.cs
修改测试
* Update UtilitiesTest.cs
修复构建错误
* Update Utilities.cs
修复编译错误
* fix: format issue
---------
Co-authored-by: Zhanhui Li <lizhanhui@apache.org>
diff --git a/csharp/rocketmq-client-csharp/Utilities.cs b/csharp/rocketmq-client-csharp/Utilities.cs
index 6d74777..331d8a4 100644
--- a/csharp/rocketmq-client-csharp/Utilities.cs
+++ b/csharp/rocketmq-client-csharp/Utilities.cs
@@ -45,16 +45,19 @@
public static byte[] GetMacAddress()
{
- var nic = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
- x => x.OperationalStatus == OperationalStatus.Up &&
- x.NetworkInterfaceType != NetworkInterfaceType.Loopback) ??
- NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
- x => x.OperationalStatus == OperationalStatus.Unknown &&
- x.NetworkInterfaceType != NetworkInterfaceType.Loopback) ??
- NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
- x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback);
+ var nics = NetworkInterface.GetAllNetworkInterfaces().Where(
+ x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
+ (int)x.NetworkInterfaceType != 53);
- return nic != null ? nic.GetPhysicalAddress().GetAddressBytes() : RandomMacAddressBytes;
+ var nic = nics.FirstOrDefault(x => x.OperationalStatus == OperationalStatus.Up) ??
+ nics.FirstOrDefault(x => x.OperationalStatus == OperationalStatus.Unknown) ??
+ nics.FirstOrDefault();
+
+ if (nic == null) { return RandomMacAddressBytes; }
+
+ var mac = nic.GetPhysicalAddress().GetAddressBytes();
+
+ return mac.Length < 6 ? mac : RandomMacAddressBytes;
}
public static int GetProcessId()
@@ -146,4 +149,4 @@
return outputStream.ToArray();
}
}
-}
\ No newline at end of file
+}
diff --git a/csharp/tests/UtilitiesTest.cs b/csharp/tests/UtilitiesTest.cs
index ed45bce..74d9726 100644
--- a/csharp/tests/UtilitiesTest.cs
+++ b/csharp/tests/UtilitiesTest.cs
@@ -51,7 +51,7 @@
public void TestGetMacAddress()
{
var macAddress = Utilities.GetMacAddress();
- Assert.IsNotNull(macAddress);
+ Assert.IsTrue(macAddress != null && macAddress.Length >= 6);
}
}
-}
\ No newline at end of file
+}