oshi.software.os.mac.MacNetworkParams Maven / Gradle / Ivy
package oshi.software.os.mac;
import oshi.jna.platform.mac.SystemB;
import oshi.software.common.AbstractNetworkParams;
import oshi.util.ExecutingCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.ptr.PointerByReference;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
/**
* @author angju
*
*/
public class MacNetworkParams extends AbstractNetworkParams {
private final static Logger LOG = LoggerFactory.getLogger(MacNetworkParams.class.getName());
private static final long serialVersionUID = 1L;
private static final String IPV6_ROUTE_HEADER = "Internet6:";
private static final String DEFAULT_GATEWAY = "default";
/**
* {@inheritDoc}
*/
@Override
public String getDomainName() {
SystemB.Addrinfo hint = new SystemB.Addrinfo();
hint.ai_flags = SystemB.AI_CANONNAME;
String hostname = "";
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
LOG.error("Unknown host exception when getting address of local host: {}", e);
return "";
}
PointerByReference ptr = new PointerByReference();
int res = SystemB.INSTANCE.getaddrinfo(hostname, null, hint, ptr);
if (res > 0) {
LOG.error("Failed getaddrinfo(): {}", SystemB.INSTANCE.gai_strerror(res));
return "";
}
SystemB.Addrinfo info = new SystemB.Addrinfo(ptr.getValue());
String canonname = info.ai_canonname.trim();
SystemB.INSTANCE.freeaddrinfo(ptr.getValue());
return canonname;
}
/**
* {@inheritDoc}
*/
@Override
public String getIpv4DefaultGateway() {
return searchGateway(ExecutingCommand.runNative("route -n get default"));
}
/**
* {@inheritDoc}
*/
@Override
public String getIpv6DefaultGateway() {
List lines = ExecutingCommand.runNative("netstat -nr");
boolean v6Table = false;
for (String line : lines) {
if (v6Table && line.startsWith(DEFAULT_GATEWAY)) {
String[] fields = line.split("\\s+");
if (fields.length > 2 && fields[2].contains("G")) {
return fields[1].split("%")[0];
}
} else if (line.startsWith(IPV6_ROUTE_HEADER)) {
v6Table = true;
}
}
return "";
}
}