use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.
the class RedHatNetworkInterface method writeNewConfig.
public static void writeNewConfig(NetInterfaceConfig netInterfaceConfig) throws KuraException {
try {
String outputFileName = "/etc/sysconfig/network-scripts/ifcfg-" + netInterfaceConfig.getName();
StringBuffer sb = new StringBuffer();
sb.append("# Networking Interface\n");
// DEVICE
sb.append("DEVICE=").append(netInterfaceConfig.getName()).append("\n");
// NAME
sb.append("NAME=").append(netInterfaceConfig.getName()).append("\n");
// TYPE
sb.append("TYPE=").append(netInterfaceConfig.getType()).append("\n");
List<? extends NetInterfaceAddressConfig> netInterfaceConfigs = netInterfaceConfig.getNetInterfaceAddresses();
s_logger.debug("There are {} NetInterfaceConfigs in this configuration", netInterfaceConfigs.size());
boolean allowWrite = false;
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs != null) {
for (NetConfig netConfig : netConfigs) {
if (netConfig instanceof NetConfigIP4) {
// ONBOOT
sb.append("ONBOOT=");
if (((NetConfigIP4) netConfig).isAutoConnect()) {
sb.append("yes");
} else {
sb.append("no");
}
sb.append("\n");
if (((NetConfigIP4) netConfig).isDhcp()) {
// BOOTPROTO
sb.append("BOOTPROTO=");
s_logger.debug("new config is DHCP");
sb.append("dhcp");
sb.append("\n");
} else {
// BOOTPROTO
sb.append("BOOTPROTO=");
s_logger.debug("new config is STATIC");
sb.append("static");
sb.append("\n");
// IPADDR
sb.append("IPADDR=").append(((NetConfigIP4) netConfig).getAddress().getHostAddress()).append("\n");
// PREFIX
sb.append("PREFIX=").append(((NetConfigIP4) netConfig).getNetworkPrefixLength()).append("\n");
// Gateway
if (((NetConfigIP4) netConfig).getGateway() != null) {
sb.append("GATEWAY=").append(((NetConfigIP4) netConfig).getGateway().getHostAddress()).append("\n");
}
}
// DEFROUTE
if (((NetConfigIP4) netConfig).getStatus() == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
sb.append("DEFROUTE=yes\n");
} else {
sb.append("DEFROUTE=no\n");
}
// DNS
List<? extends IPAddress> dnsAddresses = ((NetConfigIP4) netConfig).getDnsServers();
for (int i = 0; i < dnsAddresses.size(); i++) {
IPAddress ipAddr = dnsAddresses.get(i);
if (!(ipAddr.isLoopbackAddress() || ipAddr.isLinkLocalAddress() || ipAddr.isMulticastAddress())) {
sb.append("DNS").append(i + 1).append("=").append(ipAddr.getHostAddress()).append("\n");
}
}
allowWrite = true;
}
}
} else {
s_logger.debug("netConfigs is null");
}
// WIFI
if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfig) {
s_logger.debug("new config is a WifiInterfaceAddressConfig");
sb.append("\n#Wireless configuration\n");
// MODE
String mode = null;
WifiMode wifiMode = ((WifiInterfaceAddressConfig) netInterfaceAddressConfig).getMode();
if (wifiMode == WifiMode.INFRA) {
mode = "Managed";
} else if (wifiMode == WifiMode.MASTER) {
mode = "Master";
} else if (wifiMode == WifiMode.ADHOC) {
mode = "Ad-Hoc";
} else {
mode = wifiMode.toString();
}
sb.append("MODE=").append(mode).append("\n");
}
}
if (allowWrite) {
FileOutputStream fos = new FileOutputStream(outputFileName);
PrintWriter pw = new PrintWriter(fos);
pw.write(sb.toString());
pw.flush();
fos.getFD().sync();
pw.close();
fos.close();
} else {
s_logger.warn("writeNewConfig :: operation is not allowed");
}
} catch (Exception e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.
the class DebianNetworkInterface method writeNewConfig.
public static void writeNewConfig(NetInterfaceConfig<?> netInterfaceConfig) throws KuraException {
Scanner scanner = null;
try {
StringBuffer sb = new StringBuffer();
String outputFileName = NET_CONFIGURATION_DIRECTORY + "interfaces";
kuraFile = new File(NET_CONFIGURATION_DIRECTORY + "interfaces");
String iName = netInterfaceConfig.getName();
if (kuraFile.exists()) {
// found our match so load the properties
scanner = new Scanner(new FileInputStream(kuraFile));
// need to loop through the existing file and replace only the desired interface
while (scanner.hasNextLine()) {
String noTrimLine = scanner.nextLine();
String line = noTrimLine.trim();
// ignore comments and blank lines
if (!line.isEmpty() && !line.startsWith("#")) {
String[] args = line.split("\\s+");
try {
// must be a line stating that interface starts on boot
if (args[1].equals(iName)) {
List<? extends NetInterfaceAddressConfig> netInterfaceConfigs = netInterfaceConfig.getNetInterfaceAddresses();
s_logger.debug("There are {} NetInterfaceConfigs in this configuration", netInterfaceConfigs.size());
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs != null) {
for (NetConfig netConfig : netConfigs) {
if (netConfig instanceof NetConfigIP4) {
// ONBOOT
if (((NetConfigIP4) netConfig).isAutoConnect()) {
sb.append("auto " + iName + "\n");
}
// BOOTPROTO
sb.append("iface " + iName + " inet ");
if (((NetConfigIP4) netConfig).isDhcp()) {
s_logger.debug("new config is DHCP");
sb.append("dhcp\n");
} else {
s_logger.debug("new config is STATIC");
sb.append("static\n");
}
if (!((NetConfigIP4) netConfig).isDhcp()) {
// IPADDR
sb.append("\taddress ").append(((NetConfigIP4) netConfig).getAddress().getHostAddress()).append("\n");
// NETMASK
sb.append("\tnetmask ").append(((NetConfigIP4) netConfig).getSubnetMask().getHostAddress()).append("\n");
// Gateway
if (((NetConfigIP4) netConfig).getGateway() != null) {
sb.append("\tgateway ").append(((NetConfigIP4) netConfig).getGateway().getHostAddress()).append("\n");
}
// DNS
List<? extends IPAddress> dnsAddresses = ((NetConfigIP4) netConfig).getDnsServers();
if (!dnsAddresses.isEmpty()) {
sb.append("\tdns-nameservers ");
for (int i = 0; i < dnsAddresses.size(); i++) {
sb.append(dnsAddresses.get(i).getHostAddress() + " ");
}
sb.append("\n");
}
} else {
// DEFROUTE
if (((NetConfigIP4) netConfig).getStatus() == NetInterfaceStatus.netIPv4StatusEnabledLAN) {
sb.append("post-up route del default dev ");
sb.append(iName);
sb.append("\n");
}
}
}
}
} else {
s_logger.debug("netConfigs is null");
}
// WIFI
if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfig) {
s_logger.debug("new config is a WifiInterfaceAddressConfig");
sb.append("\n#Wireless configuration\n");
// TODO: Handle Wireless
}
}
// remove old config lines from the scanner
while (!(line = scanner.nextLine()).isEmpty()) {
}
sb.append("\n");
} else {
sb.append(noTrimLine + "\n");
}
} catch (Exception e) {
}
} else {
sb.append(noTrimLine + "\n");
}
}
}
FileOutputStream fos = new FileOutputStream(outputFileName);
PrintWriter pw = new PrintWriter(fos);
pw.write(sb.toString());
pw.flush();
fos.getFD().sync();
pw.close();
fos.close();
} catch (Exception e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
} finally {
if (scanner != null) {
scanner.close();
}
}
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.
the class WifiInterfaceAddressConfigImpl method equals.
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof WifiInterfaceAddressConfig)) {
return false;
}
WifiInterfaceAddressConfig other = (WifiInterfaceAddressConfig) obj;
if (!compare(getMode(), other.getMode())) {
return false;
}
if (getBitrate() != other.getBitrate()) {
return false;
}
if (!compare(getWifiAccessPoint(), other.getWifiAccessPoint())) {
return false;
}
List<NetConfig> thisNetConfigs = getConfigs();
List<NetConfig> otherNetConfigs = other.getConfigs();
if (thisNetConfigs.size() != otherNetConfigs.size()) {
return false;
}
if (!thisNetConfigs.containsAll(otherNetConfigs)) {
return false;
}
if (!otherNetConfigs.containsAll(thisNetConfigs)) {
return false;
}
return true;
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method getReconfiguredWifiInterfaces.
private Collection<String> getReconfiguredWifiInterfaces() throws KuraException {
Set<String> reconfiguredInterfaces = new HashSet<String>();
for (String interfaceName : this.m_networkService.getAllNetworkInterfaceNames()) {
// skip non-wifi interfaces
if (LinuxNetworkUtil.getType(interfaceName) != NetInterfaceType.WIFI) {
continue;
}
// ignore "mon" interface
if (interfaceName.startsWith("mon")) {
continue;
}
// Get the old wifi config
WifiInterfaceConfigImpl currentConfig = null;
if (this.m_currentNetworkConfiguration != null) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_currentNetworkConfiguration.getNetInterfaceConfig(interfaceName);
if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
currentConfig = (WifiInterfaceConfigImpl) netInterfaceConfig;
}
}
// Get the new wifi config
WifiInterfaceConfigImpl newConfig = null;
if (this.m_newNetConfiguration != null) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> newNetInterfaceConfig = this.m_newNetConfiguration.getNetInterfaceConfig(interfaceName);
if (newNetInterfaceConfig instanceof WifiInterfaceConfigImpl) {
newConfig = (WifiInterfaceConfigImpl) newNetInterfaceConfig;
}
}
if (newConfig != null && currentConfig != null) {
List<WifiInterfaceAddressConfig> currentInterfaceAddressConfigs = currentConfig.getNetInterfaceAddresses();
List<WifiInterfaceAddressConfig> newInterfaceAddressConfigs = newConfig.getNetInterfaceAddresses();
if (currentInterfaceAddressConfigs == null && newInterfaceAddressConfigs == null) {
// no config changed - continue
continue;
}
if (currentInterfaceAddressConfigs == null || newInterfaceAddressConfigs == null) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// TODO: compare interfaceAddressConfigs
// FIXME - assuming one InterfaceAddressConfig for now
WifiInterfaceAddressConfig currentInterfaceAddressConfig = currentInterfaceAddressConfigs.get(0);
WifiInterfaceAddressConfig newInterfaceAddressConfig = newInterfaceAddressConfigs.get(0);
if (currentInterfaceAddressConfig.getConfigs() == null && newInterfaceAddressConfig.getConfigs() == null) {
continue;
}
if (currentInterfaceAddressConfig.getConfigs() == null || newInterfaceAddressConfig.getConfigs() == null) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// Remove other WifiConfigs that don't match the selected mode, for comparison purposes
//
List<NetConfig> currentNetConfigs = new ArrayList<NetConfig>(currentInterfaceAddressConfig.getConfigs());
List<NetConfig> newNetConfigs = new ArrayList<NetConfig>(newInterfaceAddressConfig.getConfigs());
WifiMode newWifiMode = newInterfaceAddressConfig.getMode();
WifiMode currentWifiMode = currentInterfaceAddressConfig.getMode();
if (newWifiMode != currentWifiMode) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// Modes don't match. We need to compare configs deeply
internalWifiConfigCompare(reconfiguredInterfaces, interfaceName, currentNetConfigs, newNetConfigs);
} else if (newConfig != null) {
// only newConfig - oldConfig is null
s_logger.debug("oldConfig was null, adding newConfig");
reconfiguredInterfaces.add(interfaceName);
} else if (currentConfig != null) {
s_logger.debug("Configuration for {} has changed", interfaceName);
reconfiguredInterfaces.add(interfaceName);
s_logger.debug("Removing {} from list of enabled interfaces because it is not configured", interfaceName);
this.m_disabledInterfaces.add(interfaceName);
} else {
s_logger.debug("old and new wifi config are null...");
}
}
updateInterfacesLists(reconfiguredInterfaces);
return reconfiguredInterfaces;
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method isWifiEnabled.
private boolean isWifiEnabled(WifiInterfaceConfigImpl wifiInterfaceConfig) {
WifiMode wifiMode = WifiMode.UNKNOWN;
NetInterfaceStatus status = NetInterfaceStatus.netIPv4StatusUnknown;
if (wifiInterfaceConfig != null) {
for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceConfig.getNetInterfaceAddresses()) {
wifiMode = wifiInterfaceAddressConfig.getMode();
for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof NetConfigIP4) {
status = ((NetConfigIP4) netConfig).getStatus();
}
}
}
} else {
s_logger.debug("wifiInterfaceConfig is null");
}
boolean statusEnabled = status.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN) || status.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN);
boolean wifiEnabled = wifiMode.equals(WifiMode.INFRA) || wifiMode.equals(WifiMode.MASTER);
s_logger.debug("statusEnabled: " + statusEnabled);
s_logger.debug("wifiEnabled: " + wifiEnabled);
return statusEnabled && wifiEnabled;
}
Aggregations