/** * Oshi (https://github.com/oshi/oshi) * * Copyright (c) 2010 - 2017 The Oshi Project Team * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Maintainers: * dblock[at]dblock[dot]org * widdis[at]gmail[dot]com * enrico.bianchi[at]gmail[dot]com * * Contributors: * https://github.com/oshi/oshi/graphs/contributors */ package oshi.software.os.linux; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sun.jna.ptr.PointerByReference; import oshi.jna.platform.linux.Libc; import oshi.software.common.AbstractNetworkParams; import oshi.util.ExecutingCommand; import oshi.util.ParseUtil; public class LinuxNetworkParams extends AbstractNetworkParams { private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(LinuxNetworkParams.class); private static final String IPV4_DEFAULT_DEST = "0.0.0.0"; private static final String IPV6_DEFAULT_DEST = "::/0"; /** * {@inheritDoc} */ @Override public String getDomainName() { Libc.Addrinfo hint = new Libc.Addrinfo(); hint.ai_flags = Libc.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 = Libc.INSTANCE.getaddrinfo(hostname, null, hint, ptr); if (res > 0) { LOG.error("Failed getaddrinfo(): {}", Libc.INSTANCE.gai_strerror(res)); return ""; } Libc.Addrinfo info = new Libc.Addrinfo(ptr.getValue()); String canonname = info.ai_canonname.trim(); Libc.INSTANCE.freeaddrinfo(ptr.getValue()); return canonname; } /** * {@inheritDoc} */ @Override public String getIpv4DefaultGateway() { List<String> routes = ExecutingCommand.runNative("route -A inet -n"); if (routes.size() <= 2) { return ""; } String gateway = ""; int minMetric = Integer.MAX_VALUE; for (int i = 2; i < routes.size(); i++) { String[] fields = routes.get(i).split("\\s+"); if (fields.length > 4 && fields[0].equals(IPV4_DEFAULT_DEST)) { boolean isGateway = fields[3].indexOf('G') != -1; int metric = ParseUtil.parseIntOrDefault(fields[4], Integer.MAX_VALUE); if (isGateway && metric < minMetric) { minMetric = metric; gateway = fields[1]; } } } return gateway; } /** * {@inheritDoc} */ @Override public String getIpv6DefaultGateway() { List<String> routes = ExecutingCommand.runNative("route -A inet6 -n"); if (routes.size() <= 2) { return ""; } String gateway = ""; int minMetric = Integer.MAX_VALUE; for (int i = 2; i < routes.size(); i++) { String[] fields = routes.get(i).split("\\s+"); if (fields.length > 3 && fields[0].equals(IPV6_DEFAULT_DEST)) { boolean isGateway = fields[2].indexOf('G') != -1; int metric = ParseUtil.parseIntOrDefault(fields[3], Integer.MAX_VALUE); if (isGateway && metric < minMetric) { minMetric = metric; gateway = fields[1]; } } } return gateway; } }