How can I identify whether the NetworkInterface object is of a physical NIC

1.2k views Asked by At

How can I identify whether the NetworkInterface object is of a physical NIC and not a software/emulation of NIC.

I know there are methods like NetworkInterface#isVirtual and NetworkInterface#getParent which, theoretically, tells whether or not this is a physical interface.

But clearly this is not giving me right answer because I get below o/p when I use these methods, and 127.0.0.1 is a loop back software interface.

Am I missing something?

Code:

 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netIf : Collections.list(nets)) {
        System.out.printf("Display name: %s\n", netIf.getDisplayName());
        System.out.printf("Name: %s\n", netIf.getName());
        System.out.printf("Up? %s\n", netIf.isUp());
        System.out.printf("Loopback? %s\n", netIf.isLoopback());
        System.out.printf("PointToPoint? %s\n", netIf.isPointToPoint());
        System.out.printf("Supports multicast? %s\n", netIf.supportsMulticast());
        System.out.printf("Virtual? %s\n", netIf.isVirtual());
        System.out.printf("Hardware address: %s\n", Arrays.toString(netIf.getHardwareAddress()));
        System.out.printf("MTU: %s\n", netIf.getMTU());
        System.out.printf("Parent: %s\n", netIf.getParent());
        System.out.println("InetAddress:");
        Enumeration<InetAddress> inetAddresses  = netIf.getInetAddresses();
        int count = 1;
        for(InetAddress inetAddress : Collections.list(inetAddresses)){
            System.out.println("\tInetAddress #" + count);
            printInetAddressInfo(inetAddress, "\t\t");
            count++;
        }
        System.out.println("SubInterfaces:");
        displaySubInterfaces(netIf);
        netIf = null;
        System.out.printf("\n");
    }

Results:

Display name: Software Loopback Interface 1
Name: lo
Up? true
Loopback? true
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: null
MTU: -1
Parent: null
InetAddress:
    InetAddress #1
        inetAddress: /127.0.0.1
    InetAddress #2
        inetAddress: /0:0:0:0:0:0:0:1
SubInterfaces:
1

There are 1 answers

3
Stephen C On

Am I missing something?

I think the problem is that you are interpreting isVirtual incorrectly. The javadoc says:

public boolean isVirtual()

Returns whether this interface is a virtual interface (also called subinterface). Virtual interfaces are, on some systems, interfaces created as a child of a physical interface and given different settings (like address or MTU). Usually the name of the interface will the name of the parent followed by a colon (:) and a number identifying the child since there can be several virtual interfaces attached to a single physical interface.

As you can see, the javadoc is using "virtual interface" to mean the same thing as "subinterface"; i.e. a second IP address associated with a NIC. This is not the same as any non-physical interface.

The 127.0.0.1 is actually the primary IP address of the software loopback device. That is clearly a non-physical device, but it is not a subinterface for some other primary interface, either physical or virtual.

This is a bit confusing, but then the word "virtual" is rubbery in lots of IT-related contexts.

For the record, this "subinterface == virtual interface" nomenclature is not standard either. Cisco use "virtual interface" to mean "Loopback interfaces, Null interfaces, Subinterfaces or Tunnel interfaces"; e.g. http://www.cisco.com/c/en/us/td/docs/ios/12_4/interface/configuration/guide/inb_virt.html#wp1027188