Search in sources :

Example 16 with NetConfig

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;
}
Also used : WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)

Example 17 with NetConfig

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);
        }
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) NetConfig(org.eclipse.kura.net.NetConfig) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) File(java.io.File) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 18 with NetConfig

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);
        }
    }
}
Also used : DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) Properties(java.util.Properties) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 19 with NetConfig

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) IOException(java.io.IOException) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) File(java.io.File)

Example 20 with NetConfig

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) NATRule(org.eclipse.kura.linux.net.iptables.NATRule) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Aggregations

NetConfig (org.eclipse.kura.net.NetConfig)52 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)31 KuraException (org.eclipse.kura.KuraException)26 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)25 ArrayList (java.util.ArrayList)15 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)14 WifiMode (org.eclipse.kura.net.wifi.WifiMode)13 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)12 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)12 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)12 IOException (java.io.IOException)11 UnknownHostException (java.net.UnknownHostException)10 IP4Address (org.eclipse.kura.net.IP4Address)10 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)9 IPAddress (org.eclipse.kura.net.IPAddress)7 NetInterfaceType (org.eclipse.kura.net.NetInterfaceType)7 DhcpServerConfig4 (org.eclipse.kura.net.dhcp.DhcpServerConfig4)7 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)7 ModemInterfaceConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl)6 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)6