views:

419

answers:

4

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?

A: 

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.

Kevin Montrose
Thanks for the explanation Kevin. I know what the loopback adapter is for, I just want to be able to detect it. Why did they include the MIB_IF_TYPE_LOOPBACK type if it is never returned?
BlueSkies
I suppose you could conceive of a loopback adapter that was not indistinguishable from a "normal" adapter. There's a fair chance that whoever was responsible for `MIB_IF_TYPE_LOOPBACK` was in no-way involved with the Microsoft Loopback adapter anyway. Might just be an inconvenient naming overlap.
Kevin Montrose
A: 

You could use WMI to check the driver of each network interface.

Aidan Ryan
A: 

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?

magnus.wissler
A: 

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

lsalamon