What does the method getNetInterfaceConfig(String name) of Sigar get when name is null?

Tags: , ,



What does the following codes mean?

String name = sigar.getNetInterfaceConfig(null).getName();
NetInterfaceStat stat = sigar.getNetInterfaceStat(name);

I know the function of getNetInterfaceConfig(String name) is to get a specific NetInterfaceConfig, but what does it get when it’s parameter is null?

Answer

I would normally suggest you use the documentation, except that there is none anymore for SIGAR. But you can find it on the Wayback Machine.

public NetInterfaceConfig getNetInterfaceConfig(java.lang.String name)
                                     throws SigarException 

Get network interface configuration info.

Not very useful. Probably a reasonable assumption it delegates the call to the no-arg method, which is slightly more helpful.

public NetInterfaceConfig getNetInterfaceConfig()
                                     throws SigarException 

Get default network interface configuration info. Iterates getNetInterfaceList(), returning the first available ethernet interface.

So there’s the official answer. The “first available ethernet interface”.

To figure out what this really means, use the source. From the output of the high level implementation, it claims to return the “primary” interface.

NetInterfaceConfig config = this.sigar.getNetInterfaceConfig(null);
println("primary interface....." + config.getName());

But there is no such thing as a primary interface. Still, we can look at the implementation to see what SIGAR defines as primary. For example, the Windows implementation is here. The macOS implementation looks identical. Linux inherits from the default here, which is the same, too.

if (!name) {
    return sigar_net_interface_config_primary_get(sigar, ifconfig);
}

So we can look at the source code for sigar_net_interface_config_primary_get() to see how it actually works:

  • It iterates over all interfaces
    • It excludes loopback interfaces
    • It excludes interfaces with no IP addresses or no MAC addresses
  • Once it finds an interface that it doesn’t exclude above, it returns that one as “primary”.

Whether or not the “first” such interface is actually the “primary” one is debatable, as the order they are returned is platform dependent. The “first” one may be an IPv4 interface, but if a user is connecting via IPv6 the system may choose a different one. Or you might have a different interface for an internal network vs. an external one. Each one is “primary” for one sort of traffic, as defined by routing tables. You probably want to look at your system’s routing tables to identify the IP range that you care about, and filter your network interfaces to matching ranges. Even then you may get more than one. For example, on macOS, if I’m connected with both wired and wireless, I can change the priority of which route accepts the traffic on-the-fly.

Ultimately the answer to your question is, “when the name is null, it returns the first network interface the system finds that is not loopback, and has both a MAC address and IP address.”

That may or may not be the interface you’re looking for.



Source: stackoverflow