What is the best way for detecting whether a network interface is a loopback adapter?
The windows API's GetAdaptersInfo and GetAdaptersAddresses state in the documentation that they will return whether an interface is a loopback through the Type (MIB_IF_TYPE_LOOPBACK) but neither of these do for the Microsoft Loopback Adapter at least, it is reported as a standard ethernet interface.
I could try checking for the default MAC of the Loopback adapter but this can be easily spoofed.
I could check for the name "Microsoft Loopback Adapter"开发者_如何转开发 in the description but this may have translation issues and may lead to other issues.
IP addresses can also be changed.
What is the most robust method for doing this?
The Microsoft Loopback Adapter is for faking a network. Its lying to you, because that's its purpose. MSDN citation.
The Microsoft Loopback adapter is a testing tool for a virtual network environment where network access is not available. Also, you must use the Loopback adapter if there are conflicts with a network adapter or with a network adapter driver. You can bind network clients, protocols, and other network configuration items to the Loopback adapter, and you can install the network adapter driver or network adapter later while retaining the network configuration information. You can also install the Loopback adapter during the unattended installation process.
MIB_IF_TYPE_LOOPBACK
probably refers to the loopback address 127.0.0.1. It should be possible to find out whether an adapter is a Microsoft Loopback Adapter if it's got a static (or configurable) MAC address (Ethernet address). Or maybe it doesn't have one?
Use WMI, see sample at WMI Code Creator v1.0 :
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_PerfRawData_Tcpip_TCP",,48)
For Each objItem in colItems
Wscript.Echo "-----------------------------------"
Wscript.Echo "Win32_PerfRawData_Tcpip_TCP instance"
Wscript.Echo "-----------------------------------"
Wscript.Echo "Name: " & objItem.Name
Next
I can detect the loopback adapter by simply looking at the IfType
member of the IP_ADAPTER_ADDRESSES
structure. If it is IF_TYPE_SOFTWARE_LOOPBACK
, then the adapter is the loopback adapter. Works fine here on Windows 7 and 10.
You could use WMI to check the driver of each network interface.
精彩评论