GetBestInterface 的托管替代方案?

Managed alternative for GetBestInterface?

提问人:Steven Liekens 提问时间:4/28/2012 最后编辑:Steven Liekens 更新时间:6/7/2019 访问量:1631

问:

我完全没有 C(或任何其他非托管语言)编程的背景,但我想在我的 .NET 应用程序中使用 IP Helper API 中的 GetBestInterface 函数。我试图理解如何使用 P/invoke 调用来调用本机方法,但有很多事情对我来说没有意义(例如托管代码中的类型如何映射到非托管类型)。

是否隐藏在 System.Net 命名空间中的某个替代函数可以执行大致相同的操作?或者我可以使用现有的基类结合一些魔法来编写自己的替代方案吗?因为在我看来,这基本上就是:魔法。据我所知,对于该方法如何完成它所做的事情,没有真正的解释......

编辑

我刚刚发现了类上的 LocalEndPoint 属性,我认为这在这方面可能非常有用。据我了解,它将代表我进行最佳接口选择,我只需要获取与本地端点相对应的 NIC。System.Net.Sockets.Socket

vb.net 本机代码 iphelper

评论

0赞 Hans Passant 4/28/2012
pinvoke.net/default.aspx/iphlpapi/GetBestInterface.html
0赞 Steven Liekens 4/28/2012
@HansPassant,如果不实现 GetAdaptersInfo,它返回的索引是无用的,这甚至超出了我的能力范围。

答:

1赞 Steven Liekens 4/28/2012 #1

好吧,我自己做了一些东西,通过搭载框架自己进行界面选择的能力来工作。实质上,它只是连接一个套接字,然后使用套接字上的 LocalEndPoint 属性来获取它正在使用的 NIC。可能不是最好的方法,但它对我有用。

使用以下 import 语句:

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets

还有我超级棒的方法:

Private Function GetBestInterfaceManaged(ByVal address As IPAddress, ByVal port As Integer) As NetworkInterface

    Dim socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
    Dim outgoingInterface As NetworkInterface = Nothing

    Try
        socket.Connect(New IPEndPoint(address, port))

        If socket.Connected Then ' find the outgoing NIC

            Dim interfaces As List(Of NetworkInterface) = NetworkInterface.GetAllNetworkInterfaces.ToList()

            For Each nic As NetworkInterface In interfaces
                Dim properties As IPInterfaceProperties = nic.GetIPProperties

                For Each unicastAddress In properties.UnicastAddresses
                    If unicastAddress.Address.Equals(DirectCast(socket.LocalEndPoint, IPEndPoint).Address) Then
                        outgoingInterface = nic
                    End If
                Next
            Next

            If outgoingInterface Is Nothing Then
                Console.WriteLine("Darn... it didn't work!")
            Else
                Console.WriteLine("Outgoing interface: {0}", outgoingInterface.Name)
            End If

            Return outgoingInterface
        End If
    Catch ex As SocketException
        Console.WriteLine(ex.Message)
    End Try
    Return Nothing
End Function

这将解决问题:

Sub Main()
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry("www.stackoverflow.com")
    Dim NIC As NetworkInterface = GetBestInterfaceManaged(hostEntry.AddressList.First, 80)
    ' Other code goes down here
End Sub
3赞 Ray 6/7/2019 #2

我还需要 GetBestInterface,因为我需要一个解决方案,该解决方案在无法访问要探测的远程地址时有效(Steven 的解决方案会遇到问题),并且要与我移植到 C# 的本机应用程序 100% 兼容。

幸运的是,您不必移植 GetAdaptersInfo 和所有相应的IP_ADAPTER_INFO和子结构混乱来查找哪个网络接口具有 返回的索引,因为 .NET 也可以从接口中检索该索引GetBestInterface

因此,首先移植方法如下:GetBestInterface

[DllImport("iphlpapi")]
private static extern int GetBestInterface(uint dwDestAddr, ref uint pdwBestIfIndex);

然后,编写一个包装函数,如下所示:

private static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
{
    // Get the index of the interface with the best route to the remote address.
    uint dwDestAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes());
    uint dwBestIfIndex = 0;
    uint result = GetBestInterface(dwDestAddr, ref dwBestIfIndex);
    if (result != 0)
        throw new NetworkInformationException((int)result);

    // Find a matching .NET interface object with the given index.
    foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
        if (networkInterface.GetIPProperties().GetIPv4Properties().Index == dwBestIfIndex)
            return networkInterface;

    throw new InvalidOperationException($"Could not find best interface for {remoteAddress}.");
}

然后,您可以像这样简单地调用该方法 - 不要忘记无法访问的主机现在也可以工作:

NetworkInterface bestNetworkInterface = GetBestNetworkInterface(IPAddress.Parse("8.8.8.8"));