add getNetInterfaceConfig() util to get default ethernet config

This commit is contained in:
Doug MacEachern 2006-02-28 18:37:55 +00:00
parent a0b3363afd
commit fb9d624b0c
3 changed files with 45 additions and 0 deletions

View File

@ -663,6 +663,46 @@ public class Sigar implements SigarProxy {
return NetInterfaceConfig.fetch(this, name);
}
/**
* Get default network interface configuration info.
* Iterates getNetInterfaceList(), returning the first
* available ethernet interface.
* @exception SigarException on failure.
*/
public NetInterfaceConfig getNetInterfaceConfig()
throws SigarException {
String[] interfaces = getNetInterfaceList();
for (int i=0; i<interfaces.length; i++) {
String name = interfaces[i];
NetInterfaceConfig ifconfig;
try {
ifconfig = getNetInterfaceConfig(name);
} catch (SigarException e) {
continue;
}
long flags = ifconfig.getFlags();
if ((flags & NetFlags.IFF_UP) <= 0) {
continue;
}
if ((flags & NetFlags.IFF_POINTOPOINT) > 0) {
continue;
}
if ((flags & NetFlags.IFF_LOOPBACK) > 0) {
continue;
}
return ifconfig;
}
String msg =
"No ethernet interface available";
throw new SigarException(msg);
}
/**
* Get network interface stats.
* @exception SigarException on failure.

View File

@ -118,6 +118,9 @@ public interface SigarProxy {
public NetInterfaceConfig getNetInterfaceConfig(String name)
throws SigarException;
public NetInterfaceConfig getNetInterfaceConfig()
throws SigarException;
public NetInterfaceStat getNetInterfaceStat(String name)
throws SigarException;

View File

@ -77,6 +77,8 @@ public class TestNetIf extends SigarTestCase {
getNetIflist(sigar, false);
getNetIflist(sigar, false);
getNetIflist(sigar, true);
traceln("Default IP=" +
sigar.getNetInterfaceConfig().getAddress());
//XXX somehow manage to trigger a segfault using the
//1.4.1_02-b06 jdk on linux, no problem with 1.4.2_02-b03