This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 3036b049 修复获取网卡物理地址的问题 (#704)
3036b049 is described below

commit 3036b0497cd3d13f2949778bc1102c367f1a74a9
Author: SDuo <s...@vip.qq.com>
AuthorDate: Fri Mar 15 00:34:21 2024 +0800

    修复获取网卡物理地址的问题 (#704)
    
    * Update Utilities.cs
    
    修复无法获得正确的网卡物理地址异常
    
    * Update UtilitiesTest.cs
    
    修改测试
    
    * Update UtilitiesTest.cs
    
    修复构建错误
    
    * Update Utilities.cs
    
    修复编译错误
    
    * fix: format issue
    
    ---------
    
    Co-authored-by: Zhanhui Li <lizhan...@apache.org>
---
 csharp/rocketmq-client-csharp/Utilities.cs | 25 ++++++++++++++-----------
 csharp/tests/UtilitiesTest.cs              |  4 ++--
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/csharp/rocketmq-client-csharp/Utilities.cs 
b/csharp/rocketmq-client-csharp/Utilities.cs
index 6d74777d..331d8a40 100644
--- a/csharp/rocketmq-client-csharp/Utilities.cs
+++ b/csharp/rocketmq-client-csharp/Utilities.cs
@@ -45,16 +45,19 @@ namespace Org.Apache.Rocketmq
 
         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);
-
-            return nic != null ? nic.GetPhysicalAddress().GetAddressBytes() : 
RandomMacAddressBytes;
+            var nics = NetworkInterface.GetAllNetworkInterfaces().Where(
+                x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
+                     (int)x.NetworkInterfaceType != 53);
+
+            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 @@ namespace Org.Apache.Rocketmq
             return outputStream.ToArray();
         }
     }
-}
\ No newline at end of file
+}
diff --git a/csharp/tests/UtilitiesTest.cs b/csharp/tests/UtilitiesTest.cs
index ed45bcef..74d9726d 100644
--- a/csharp/tests/UtilitiesTest.cs
+++ b/csharp/tests/UtilitiesTest.cs
@@ -51,7 +51,7 @@ namespace tests
         public void TestGetMacAddress()
         {
             var macAddress = Utilities.GetMacAddress();
-            Assert.IsNotNull(macAddress);
+            Assert.IsTrue(macAddress != null && macAddress.Length >= 6);
         }
     }
-}
\ No newline at end of file
+}

Reply via email to