use of org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl in project kura by eclipse.
the class NetworkAdminServiceImpl method updateWifiInterfaceConfig.
@Override
public void updateWifiInterfaceConfig(String interfaceName, boolean autoConnect, WifiAccessPoint accessPoint, List<NetConfig> netConfigs) throws KuraException {
NetConfigIP4 netConfig4 = null;
NetConfigIP6 netConfig6 = null;
WifiConfig wifiConfig = null;
DhcpServerConfigIP4 dhcpServerConfigIP4 = null;
FirewallAutoNatConfig natConfig = null;
boolean hadNetConfig4 = false;
boolean hadNetConfig6 = false;
boolean hadWifiConfig = false;
boolean hadDhcpServerConfigIP4 = false;
boolean hadNatConfig = false;
if (netConfigs != null && !netConfigs.isEmpty()) {
for (NetConfig netConfig : netConfigs) {
if (!netConfig.isValid()) {
throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "NetConfig Configuration is invalid: " + netConfig.toString());
}
if (netConfig instanceof NetConfigIP4) {
s_logger.debug("got new NetConfigIP4");
netConfig4 = (NetConfigIP4) netConfig;
} else if (netConfig instanceof NetConfigIP6) {
s_logger.debug("got new NetConfigIP6");
netConfig6 = (NetConfigIP6) netConfig;
} else if (netConfig instanceof WifiConfig) {
s_logger.debug("got new WifiConfig");
wifiConfig = (WifiConfig) netConfig;
} else if (netConfig instanceof DhcpServerConfigIP4) {
s_logger.debug("got new DhcpServerConfigIP4");
dhcpServerConfigIP4 = (DhcpServerConfigIP4) netConfig;
} else if (netConfig instanceof FirewallAutoNatConfig) {
s_logger.debug("got new NatConfig");
natConfig = (FirewallAutoNatConfig) netConfig;
}
}
}
// validation
if (netConfig4 == null && netConfig6 == null) {
throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Either IPv4 or IPv6 configuration must be defined");
}
if (wifiConfig == null) {
throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "WiFi configuration must be defined");
}
List<String> modifiedInterfaceNames = new ArrayList<String>();
boolean configurationChanged = false;
ComponentConfiguration originalNetworkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
if (originalNetworkComponentConfiguration == null) {
return;
}
try {
NetworkConfiguration newNetworkConfiguration = new NetworkConfiguration(originalNetworkComponentConfiguration.getConfigurationProperties());
List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = newNetworkConfiguration.getNetInterfaceConfigs();
for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
if (netInterfaceConfig.getName().equals(interfaceName)) {
// replace existing configs
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
List<NetConfig> newNetConfigs = new ArrayList<NetConfig>();
WifiMode newWifiMode = wifiConfig != null ? wifiConfig.getMode() : null;
for (NetConfig netConfig : existingNetConfigs) {
s_logger.debug("looking at existing NetConfig for {} with value: {}", interfaceName, netConfig);
if (netConfig instanceof NetConfigIP4) {
if (netConfig4 == null) {
s_logger.debug("removing NetConfig4 for {}", interfaceName);
} else {
hadNetConfig4 = true;
newNetConfigs.add(netConfig4);
if (!netConfig.equals(netConfig4)) {
s_logger.debug("updating NetConfig4 for {}", interfaceName);
s_logger.debug("Is new State DHCP? {}", netConfig4.isDhcp());
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
s_logger.debug("not updating NetConfig4 for {} because it is unchanged", interfaceName);
}
}
} else if (netConfig instanceof NetConfig6) {
if (netConfig6 == null) {
s_logger.debug("removing NetConfig6 for {}", interfaceName);
} else {
hadNetConfig6 = true;
newNetConfigs.add(netConfig6);
if (!netConfig.equals(netConfig6)) {
s_logger.debug("updating NetConfig6 for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
s_logger.debug("not updating NetConfig6 for {} because it is unchanged", interfaceName);
}
}
} else if (netConfig instanceof WifiConfig) {
if (wifiConfig == null) {
s_logger.debug("removing wifiConfig for {}", interfaceName);
} else {
// others
if (newWifiMode.equals(((WifiConfig) netConfig).getMode())) {
hadWifiConfig = true;
newNetConfigs.add(wifiConfig);
s_logger.debug("checking WifiConfig for {} mode", wifiConfig.getMode());
if (!netConfig.equals(wifiConfig)) {
s_logger.debug("updating WifiConfig for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
s_logger.debug("not updating WifiConfig for {} because it is unchanged", interfaceName);
}
} else {
// Keep the old WifiConfig for the non-selected wifi modes
s_logger.debug("adding other WifiConfig: {}", netConfig);
newNetConfigs.add(netConfig);
}
}
} else if (netConfig instanceof DhcpServerConfigIP4) {
if (dhcpServerConfigIP4 == null) {
s_logger.debug("removing DhcpServerConfigIP4 for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
hadDhcpServerConfigIP4 = true;
newNetConfigs.add(dhcpServerConfigIP4);
if (!netConfig.equals(dhcpServerConfigIP4)) {
s_logger.debug("updating DhcpServerConfigIP4 for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
s_logger.debug("not updating DhcpServerConfigIP4 for {} because it is unchanged", interfaceName);
}
}
} else if (netConfig instanceof FirewallAutoNatConfig) {
if (natConfig == null) {
s_logger.debug("removing FirewallAutoNatConfig for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
hadNatConfig = true;
newNetConfigs.add(natConfig);
if (!netConfig.equals(natConfig)) {
s_logger.debug("updating FirewallAutoNatConfig for {}", interfaceName);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
} else {
s_logger.debug("not updating FirewallNatConfig for {} because it is unchanged", interfaceName);
}
}
} else {
s_logger.debug("Found unsupported configuration: {}", netConfig.toString());
}
}
// add configs that did not match any in the current configuration
if (netConfigs != null && !netConfigs.isEmpty()) {
for (NetConfig netConfig : netConfigs) {
if (netConfig instanceof NetConfigIP4 && !hadNetConfig4) {
s_logger.debug("adding new NetConfig4 to existing config for {}", interfaceName);
newNetConfigs.add(netConfig);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
}
if (netConfig instanceof NetConfigIP6 && !hadNetConfig6) {
s_logger.debug("adding new NetConfig6 to existing config for {}", interfaceName);
newNetConfigs.add(netConfig);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
}
if (netConfig instanceof WifiConfig && !hadWifiConfig) {
s_logger.debug("adding new WifiConfig to existing config for {}", interfaceName);
newNetConfigs.add(netConfig);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
}
if (netConfig instanceof DhcpServerConfigIP4 && !hadDhcpServerConfigIP4) {
s_logger.debug("adding new DhcpServerConfigIP4 to existing config for {}", interfaceName);
newNetConfigs.add(netConfig);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
}
if (netConfig instanceof FirewallAutoNatConfig && !hadNatConfig) {
s_logger.debug("adding new FirewallAutoNatConfig to existing config for {}", interfaceName);
newNetConfigs.add(netConfig);
configurationChanged = true;
if (!modifiedInterfaceNames.contains(interfaceName)) {
modifiedInterfaceNames.add(interfaceName);
}
}
}
}
// Update the wifi mode
if (newWifiMode != null) {
s_logger.debug("setting address config wifiMode to: {}", newWifiMode);
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setMode(newWifiMode);
}
// replace with new list
for (NetConfig netConfig : newNetConfigs) {
s_logger.debug("Current NetConfig: {} :: {}", netConfig.getClass(), netConfig);
}
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(newNetConfigs);
}
}
}
}
if (configurationChanged) {
submitNetworkConfiguration(modifiedInterfaceNames, newNetworkConfiguration);
}
} catch (UnknownHostException e) {
s_logger.warn("Exception while updating WifiInterfaceConfig", e);
}
}
use of org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl in project kura by eclipse.
the class DhcpConfigReader method getConfig.
private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
s_logger.debug("Getting DHCP server config for {}", interfaceName);
NetInterfaceType type = netInterfaceConfig.getType();
if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI) {
// StringBuffer configFilename = new
// StringBuffer(FILE_DIR).append("dhcpd-").append(interfaceName).append(".conf");
String configFilename = DhcpServerManager.getConfigFilename(interfaceName);
File dhcpConfigFile = new File(configFilename);
if (dhcpConfigFile.exists()) {
DhcpServerConfig4 dhcpServerConfig4 = populateConfig(interfaceName, dhcpConfigFile, kuraExtendedProps);
if (dhcpServerConfig4 != null) {
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs == null) {
throw KuraException.internalError("NetInterfaceAddress list is null for interface " + interfaceName);
} else if (netInterfaceAddressConfigs.size() == 0) {
throw KuraException.internalError("NetInterfaceAddress list is empty for interface " + interfaceName);
}
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs == null) {
netConfigs = new ArrayList<NetConfig>();
if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
} else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
}
}
netConfigs.add(dhcpServerConfig4);
}
}
} else {
s_logger.debug("There is no current DHCP server configuration for {}", interfaceName);
}
}
}
use of org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl in project kura by eclipse.
the class FirewallAutoNatConfigReader method getConfig.
private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraProps) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
NetInterfaceType type = netInterfaceConfig.getType();
if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI) {
s_logger.debug("Getting NAT config for {}", interfaceName);
if (kuraProps != null) {
s_logger.debug("Getting NAT config from kuraProps");
boolean natEnabled = false;
boolean useMasquerade = false;
String prop = null;
String srcIface = null;
String dstIface = null;
StringBuilder sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.enabled");
if ((prop = kuraProps.getProperty(sb.toString())) != null) {
natEnabled = Boolean.parseBoolean(prop);
}
sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.masquerade");
if ((prop = kuraProps.getProperty(sb.toString())) != null) {
useMasquerade = Boolean.parseBoolean(prop);
}
sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.src.interface");
if ((prop = kuraProps.getProperty(sb.toString())) != null) {
srcIface = prop;
}
sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.dst.interface");
if ((prop = kuraProps.getProperty(sb.toString())) != null) {
dstIface = prop;
}
if (natEnabled) {
FirewallAutoNatConfig natConfig = new FirewallAutoNatConfig(srcIface, dstIface, useMasquerade);
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs == null) {
throw KuraException.internalError("NetInterfaceAddress list is null for interface " + interfaceName);
} else if (netInterfaceAddressConfigs.size() == 0) {
throw KuraException.internalError("NetInterfaceAddress list is empty for interface " + interfaceName);
}
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs == null) {
netConfigs = new ArrayList<NetConfig>();
if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
} else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
}
}
netConfigs.add(natConfig);
}
}
} else {
// get it from the firewall file if possible
LinuxFirewall firewall = LinuxFirewall.getInstance();
Set<NATRule> natRules = firewall.getAutoNatRules();
if (natRules != null && !natRules.isEmpty()) {
Iterator<NATRule> it = natRules.iterator();
while (it.hasNext()) {
NATRule rule = it.next();
if (rule.getSourceInterface().equals(interfaceName)) {
s_logger.debug("found NAT rule: {}", rule);
// this is the one we care about
FirewallAutoNatConfig natConfig = new FirewallAutoNatConfig(rule.getSourceInterface(), rule.getDestinationInterface(), rule.isMasquerade());
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs == null) {
throw KuraException.internalError("NetInterfaceAddress list is null for interface " + interfaceName);
} else if (netInterfaceAddressConfigs.size() == 0) {
throw KuraException.internalError("NetInterfaceAddress list is empty for interface " + interfaceName);
}
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs == null) {
netConfigs = new ArrayList<NetConfig>();
if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
} else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
}
}
netConfigs.add(natConfig);
}
}
}
}
}
}
}
use of org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl in project kura by eclipse.
the class HostapdConfigReader method getConfig.
private void getConfig(WifiInterfaceConfigImpl wifiInterfaceConfig) throws KuraException {
String interfaceName = wifiInterfaceConfig.getName();
s_logger.debug("Getting hostapd config for {}", interfaceName);
List<WifiInterfaceAddressConfig> wifiInterfaceAddressConfigs = wifiInterfaceConfig.getNetInterfaceAddresses();
if (wifiInterfaceAddressConfigs == null || wifiInterfaceAddressConfigs.size() == 0) {
wifiInterfaceAddressConfigs = new ArrayList<WifiInterfaceAddressConfig>();
wifiInterfaceAddressConfigs.add(new WifiInterfaceAddressConfigImpl());
wifiInterfaceConfig.setNetInterfaceAddresses(wifiInterfaceAddressConfigs);
}
for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceAddressConfigs) {
if (wifiInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
List<NetConfig> netConfigs = wifiInterfaceAddressConfig.getConfigs();
if (netConfigs == null) {
netConfigs = new ArrayList<NetConfig>();
((WifiInterfaceAddressConfigImpl) wifiInterfaceAddressConfig).setNetConfigs(netConfigs);
}
netConfigs.add(getWifiHostConfig(interfaceName));
}
}
}
use of org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl in project kura by eclipse.
the class HostapdConfigWriter method writeConfig.
private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
WifiConfig apConfig = null;
String interfaceDriver = null;
if (netConfigs != null) {
for (NetConfig netConfig : netConfigs) {
try {
if (netConfig instanceof WifiConfig) {
if (((WifiConfig) netConfig).getMode() == WifiMode.MASTER) {
s_logger.debug("Found wifiConfig with mode set to master");
interfaceDriver = ((WifiConfig) netConfig).getDriver();
if (interfaceDriver != null) {
s_logger.debug("Writing wifiConfig: {}", netConfig);
apConfig = (WifiConfig) netConfig;
} else {
s_logger.error("Can't generate hostapd config - no driver specified");
}
}
} else if (netConfig instanceof NetConfigIP4) {
netInterfaceStatus = ((NetConfigIP4) netConfig).getStatus();
}
} catch (Exception e) {
s_logger.error("Failed to configure Hostapd");
throw KuraException.internalError(e);
}
}
if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
s_logger.info("Network interface status for {} is disabled - not overwriting hostapd configuration file", interfaceName);
return;
}
if (apConfig != null) {
try {
generateHostapdConf(apConfig, interfaceName, interfaceDriver);
} catch (Exception e) {
s_logger.error("Failed to generate hostapd configuration file for {} interface", interfaceName);
throw KuraException.internalError(e);
}
}
}
}
}
}
}
Aggregations