use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class RouteServiceImpl method entryToRoute.
private RouteConfig entryToRoute(String entry) {
RouteConfig route;
IPAddress destination;
IPAddress gateway;
IPAddress netmask;
int metric;
String iface;
String tmp;
try {
route = null;
StringTokenizer tok = new StringTokenizer(entry, " ");
tmp = tok.nextToken();
destination = IPAddress.parseHostAddress(tmp);
gateway = IPAddress.parseHostAddress(tok.nextToken());
netmask = IPAddress.parseHostAddress(tok.nextToken());
tok.nextToken();
metric = Integer.parseInt(tok.nextToken());
tok.nextToken();
tok.nextToken();
iface = tok.nextToken();
} catch (Exception e) {
s_logger.error("Error parsing route table entry: " + e.getMessage());
e.printStackTrace();
return null;
}
if (destination instanceof IP4Address) {
route = new RouteConfigIP4((IP4Address) destination, (IP4Address) gateway, (IP4Address) netmask, iface, metric);
} else if (destination instanceof IP6Address) {
route = new RouteConfigIP6((IP6Address) destination, (IP6Address) gateway, (IP6Address) netmask, iface, metric);
}
s_logger.trace("Route successfully read from route table entry");
return route;
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class LinuxDns method getDnServers.
public synchronized Set<IPAddress> getDnServers() {
BufferedReader br = null;
Set<IPAddress> servers = new HashSet<IPAddress>();
try {
File f = new File(DNS_FILE_NAME);
if (!f.exists()) {
f.createNewFile();
}
br = new BufferedReader(new FileReader(new File(DNS_FILE_NAME)));
String line = null;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.indexOf("nameserver") == 0) {
StringTokenizer st = new StringTokenizer(line);
st.nextToken();
servers.add(IPAddress.parseHostAddress(st.nextToken()));
}
}
if (servers.size() > 0) {
return servers;
} else {
s_logger.debug("No DNS servers found");
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class DnsMonitorServiceImpl method getConfiguredDnsServers.
// Get a list of dns servers for the specified NetInterfaceConfig
private Set<IPAddress> getConfiguredDnsServers(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
s_logger.trace("Getting dns servers for {}", interfaceName);
LinuxDns linuxDns = LinuxDns.getInstance();
LinkedHashSet<IPAddress> serverList = new LinkedHashSet<IPAddress>();
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfig.getNetInterfaceAddresses()) {
for (NetConfig netConfig : netInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof NetConfigIP4) {
NetConfigIP4 netConfigIP4 = (NetConfigIP4) netConfig;
List<IP4Address> userServers = netConfigIP4.getDnsServers();
if (netConfigIP4.isDhcp()) {
// If DHCP but there are user defined entries, use those instead
if (userServers != null && !userServers.isEmpty()) {
s_logger.debug("Configured for DHCP with user-defined servers - adding: {}", userServers);
serverList.addAll(userServers);
} else {
if (netInterfaceConfig.getType().equals(NetInterfaceType.MODEM)) {
// FIXME - don't like this
// cannot use interfaceName here because it one config behind
int pppNo = ((ModemInterfaceConfigImpl) netInterfaceConfig).getPppNum();
if (LinuxNetworkUtil.hasAddress("ppp" + pppNo)) {
List<IPAddress> servers = linuxDns.getPppDnServers();
if (servers != null) {
s_logger.debug("Adding PPP dns servers: {}", servers);
serverList.addAll(servers);
}
}
} else {
String currentAddress = LinuxNetworkUtil.getCurrentIpAddress(interfaceName);
List<IPAddress> servers = linuxDns.getDhcpDnsServers(interfaceName, currentAddress);
if (servers != null) {
s_logger.debug("Configured for DHCP - adding DHCP servers: {}", servers);
serverList.addAll(servers);
}
}
}
} else {
// If static, use the user defined entries
s_logger.debug("Configured for static - adding user-defined servers: {}", userServers);
serverList.addAll(userServers);
}
}
}
}
return serverList;
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class IfcfgConfigReader method getConfig.
private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
s_logger.debug("Getting config for {}", interfaceName);
NetInterfaceType type = netInterfaceConfig.getType();
if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI || type == NetInterfaceType.LOOPBACK) {
NetInterfaceStatus netInterfaceStatus = null;
StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.ip4.status");
if (kuraExtendedProps != null && kuraExtendedProps.getProperty(sb.toString()) != null) {
netInterfaceStatus = NetInterfaceStatus.valueOf(kuraExtendedProps.getProperty(sb.toString()));
} else {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
}
s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + netInterfaceConfig.getName());
boolean autoConnect = false;
// int mtu = -1; // MTU is not currently used
boolean dhcp = false;
IP4Address address = null;
String ipAddress = null;
String prefixString = null;
String netmask = null;
String gateway = null;
File ifcfgFile = null;
if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
ifcfgFile = new File(DEBIAN_NET_CONFIGURATION_DIRECTORY + "interfaces");
} else {
ifcfgFile = new File(REDHAT_NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
}
if (ifcfgFile.exists()) {
Properties kuraProps;
// found our match so load the properties
if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
kuraProps = parseDebianConfigFile(ifcfgFile, interfaceName);
} else {
kuraProps = parseRedhatConfigFile(ifcfgFile, interfaceName);
}
if (kuraProps != null) {
String onBoot = kuraProps.getProperty("ONBOOT");
if ("yes".equals(onBoot)) {
s_logger.debug("Setting autoConnect to true");
autoConnect = true;
} else {
s_logger.debug("Setting autoConnect to false");
autoConnect = false;
}
// override MTU with what is in config if it is present
/*
* IAB: MTU is not currently used
* String stringMtu = kuraProps.getProperty("MTU");
* if (stringMtu == null) {
* try {
* mtu = LinuxNetworkUtil.getCurrentMtu(interfaceName);
* } catch (KuraException e) {
* // just assume ???
* if (interfaceName.equals("lo")) {
* mtu = 16436;
* } else {
* mtu = 1500;
* }
* }
* } else {
* mtu = Short.parseShort(stringMtu);
* }
*/
// get the bootproto
String bootproto = kuraProps.getProperty("BOOTPROTO");
if (bootproto == null) {
bootproto = "static";
}
// get the defroute
String defroute = kuraProps.getProperty("DEFROUTE");
if (defroute == null) {
defroute = "no";
}
// actual properties
if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
if (autoConnect) {
if (defroute.equals("no")) {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
} else {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
}
}
}
// check for dhcp or static configuration
try {
ipAddress = kuraProps.getProperty("IPADDR");
prefixString = kuraProps.getProperty("PREFIX");
netmask = kuraProps.getProperty("NETMASK");
kuraProps.getProperty("BROADCAST");
try {
gateway = kuraProps.getProperty("GATEWAY");
s_logger.debug("got gateway for " + interfaceName + ": " + gateway);
} catch (Exception e) {
s_logger.warn("missing gateway stanza for " + interfaceName);
}
if (bootproto.equals("dhcp")) {
s_logger.debug("currently set for DHCP");
dhcp = true;
ipAddress = null;
netmask = null;
} else {
s_logger.debug("currently set for static address");
dhcp = false;
}
} catch (Exception e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString(), e);
}
if (ipAddress != null && !ipAddress.isEmpty()) {
try {
address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
} catch (UnknownHostException e) {
s_logger.warn("Error parsing address: " + ipAddress, e);
}
}
// make sure at least prefix or netmask is present if static
if (autoConnect && !dhcp && prefixString == null && netmask == null) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString() + " must contain NETMASK and/or PREFIX");
}
}
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs == null) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list is null");
} else if (netInterfaceAddressConfigs.size() == 0) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list has no entries");
}
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs == null) {
netConfigs = new ArrayList<NetConfig>();
if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
if (dhcp) {
// Replace with DNS provided by DHCP server
// (displayed as read-only in Denali)
List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
if (dhcpDnsServers != null) {
((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
}
}
} else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
if (dhcp) {
// Replace with DNS provided by DHCP server
// (displayed as read-only in Denali)
List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
if (dhcpDnsServers != null) {
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
}
}
}
}
NetConfigIP4 netConfig = new NetConfigIP4(netInterfaceStatus, autoConnect);
setNetConfigIP4(netConfig, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
s_logger.debug("NetConfig: {}", netConfig);
netConfigs.add(netConfig);
}
}
}
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class WifiMonitorServiceImpl method isAccessPointReachable.
private boolean isAccessPointReachable(String interfaceName, int tout) throws KuraException {
boolean ret = true;
RouteService rs = RouteServiceImpl.getInstance();
RouteConfig rconf = rs.getDefaultRoute(interfaceName);
if (rconf != null) {
IPAddress ipAddress = rconf.getGateway();
String iface = rconf.getInterfaceName();
if (ipAddress != null && iface != null && iface.equals(interfaceName)) {
try {
InetAddress inetAddress = InetAddress.getByName(ipAddress.getHostAddress());
try {
ret = inetAddress.isReachable(tout);
s_logger.info("Access point reachable? " + ret);
} catch (IOException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
} catch (UnknownHostException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
}
return ret;
}
Aggregations