use of org.eclipse.kura.net.NetConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method getWifiConfig.
private WifiConfig getWifiConfig(WifiInterfaceConfigImpl wifiInterfaceConfig) {
WifiConfig selectedWifiConfig = null;
WifiMode wifiMode = WifiMode.UNKNOWN;
if (wifiInterfaceConfig != null) {
loop: for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceConfig.getNetInterfaceAddresses()) {
wifiMode = wifiInterfaceAddressConfig.getMode();
for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof WifiConfig) {
WifiConfig wifiConfig = (WifiConfig) netConfig;
if (wifiMode.equals(wifiConfig.getMode())) {
selectedWifiConfig = wifiConfig;
break loop;
}
}
}
}
}
return selectedWifiConfig;
}
use of org.eclipse.kura.net.NetConfig 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.net.NetConfig in project kura by eclipse.
the class DhcpConfigWriter method writeKuraExtendedConfig.
private void writeKuraExtendedConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
boolean enabled = false;
boolean passDns = false;
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = null;
if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
netInterfaceAddressConfigs = ((EthernetInterfaceConfigImpl) netInterfaceConfig).getNetInterfaceAddresses();
} else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
netInterfaceAddressConfigs = ((WifiInterfaceConfigImpl) netInterfaceConfig).getNetInterfaceAddresses();
} else {
s_logger.error("not adding config for " + netInterfaceConfig.getName());
}
if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs != null && netConfigs.size() > 0) {
for (int i = 0; i < netConfigs.size(); i++) {
NetConfig netConfig = netConfigs.get(i);
if (netConfig instanceof DhcpServerConfig4) {
enabled = ((DhcpServerConfig4) netConfig).isEnabled();
passDns = ((DhcpServerConfig4) netConfig).isPassDns();
}
}
}
}
}
// set it all
if (kuraExtendedProps == null) {
s_logger.debug("kuraExtendedProps was null");
kuraExtendedProps = new Properties();
}
StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.dhcpServer4.enabled");
kuraExtendedProps.put(sb.toString(), Boolean.toString(enabled));
sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.dhcpServer4.passDns");
kuraExtendedProps.put(sb.toString(), Boolean.toString(passDns));
// write it
if (kuraExtendedProps != null && !kuraExtendedProps.isEmpty()) {
try {
KuranetConfig.storeProperties(kuraExtendedProps);
} catch (Exception e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
}
use of org.eclipse.kura.net.NetConfig in project kura by eclipse.
the class DhcpConfigWriter method writeConfig.
/*
* private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws
* KuraException {
* DhcpServerTool dhcpServerTool = DhcpServerManager.getTool();
* if (dhcpServerTool == DhcpServerTool.DHCPD) {
* writeDhcpdConfig(netInterfaceConfig);
* } else if (dhcpServerTool == DhcpServerTool.DHCPD) {
* writeUdhcpdConfig(netInterfaceConfig);
* }
* }
*/
private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
/*
* String dhcpConfigFileName = new
* StringBuffer().append(FILE_DIR).append("dhcpd-").append(interfaceName).append(".conf").toString();
* String tmpDhcpConfigFileName = new
* StringBuffer().append(FILE_DIR).append("dhcpd-").append(interfaceName).append(".conf").append(".tmp").
* toString();
*/
String dhcpConfigFileName = DhcpServerManager.getConfigFilename(interfaceName);
String tmpDhcpConfigFileName = new StringBuilder(dhcpConfigFileName).append(".tmp").toString();
s_logger.debug("Writing DHCP config for {}", interfaceName);
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs != null) {
for (NetConfig netConfig : netConfigs) {
if (netConfig instanceof DhcpServerConfig4) {
DhcpServerConfig4 dhcpServerConfig = (DhcpServerConfig4) netConfig;
writeConfigFile(tmpDhcpConfigFileName, interfaceName, dhcpServerConfig);
// move the file if we made it this far and they are different
File tmpDhcpConfigFile = new File(tmpDhcpConfigFileName);
File dhcpConfigFile = new File(dhcpConfigFileName);
try {
if (!FileUtils.contentEquals(tmpDhcpConfigFile, dhcpConfigFile)) {
if (tmpDhcpConfigFile.renameTo(dhcpConfigFile)) {
s_logger.trace("Successfully wrote DHCP config file");
} else {
s_logger.error("Failed to write DHCP config file for " + interfaceName);
throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "error while building up new configuration files for dhcp server: " + interfaceName);
}
} else {
s_logger.info("Not rewriting DHCP config file for " + interfaceName + " because it is the same");
}
} catch (IOException e) {
throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "error while building up new configuration files for dhcp servers", e);
}
}
}
}
}
}
}
use of org.eclipse.kura.net.NetConfig 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);
}
}
}
}
}
}
}
Aggregations