Skip to content

Commit

Permalink
修复获取网卡物理地址的问题 (apache#704)
Browse files Browse the repository at this point in the history
* Update Utilities.cs

修复无法获得正确的网卡物理地址异常

* Update UtilitiesTest.cs

修改测试

* Update UtilitiesTest.cs

修复构建错误

* Update Utilities.cs

修复编译错误

* fix: format issue

---------

Co-authored-by: Zhanhui Li <lizhanhui@apache.org>
sduo and lizhanhui authored Mar 14, 2024
1 parent 430ed16 commit 3036b04
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 14 additions & 11 deletions csharp/rocketmq-client-csharp/Utilities.cs
Original file line number Diff line number Diff line change
@@ -45,16 +45,19 @@ public static int GetPositiveMod(int k, int n)

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 @@ public static byte[] DecompressBytesGzip(byte[] src)
return outputStream.ToArray();
}
}
}
}
4 changes: 2 additions & 2 deletions csharp/tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ public void TestComputeSha1Hash()
public void TestGetMacAddress()
{
var macAddress = Utilities.GetMacAddress();
Assert.IsNotNull(macAddress);
Assert.IsTrue(macAddress != null && macAddress.Length >= 6);
}
}
}
}

0 comments on commit 3036b04

Please sign in to comment.