Search in sources :

Example 1 with DhcpServerConfig4

use of org.eclipse.kura.net.dhcp.DhcpServerConfig4 in project kura by eclipse.

the class WifiMonitorServiceImpl method enableInterface.

private void enableInterface(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    s_logger.debug("enableInterface: {}", netInterfaceConfig);
    WifiInterfaceConfigImpl wifiInterfaceConfig = null;
    if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
        wifiInterfaceConfig = (WifiInterfaceConfigImpl) netInterfaceConfig;
    } else {
        return;
    }
    String interfaceName = wifiInterfaceConfig.getName();
    WifiMode wifiMode = WifiMode.UNKNOWN;
    NetInterfaceStatus status = NetInterfaceStatus.netIPv4StatusUnknown;
    boolean isDhcpClient = false;
    boolean enableDhcpServer = false;
    if (wifiInterfaceConfig != null) {
        for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceConfig.getNetInterfaceAddresses()) {
            wifiMode = wifiInterfaceAddressConfig.getMode();
            for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
                if (netConfig instanceof NetConfigIP4) {
                    status = ((NetConfigIP4) netConfig).getStatus();
                    isDhcpClient = ((NetConfigIP4) netConfig).isDhcp();
                } else if (netConfig instanceof DhcpServerConfig4) {
                    enableDhcpServer = ((DhcpServerConfig4) netConfig).isEnabled();
                }
            }
        }
    }
    if (status.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN) || status.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN)) {
        if (wifiMode.equals(WifiMode.INFRA) || wifiMode.equals(WifiMode.MASTER)) {
            this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
            if (enableDhcpServer) {
                this.m_netAdminService.manageDhcpServer(interfaceName, true);
            }
        }
    }
}
Also used : WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 2 with DhcpServerConfig4

use of org.eclipse.kura.net.dhcp.DhcpServerConfig4 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 3 with DhcpServerConfig4

use of org.eclipse.kura.net.dhcp.DhcpServerConfig4 in project kura by eclipse.

the class DhcpConfigReader method populateConfig.

private DhcpServerConfig4 populateConfig(String interfaceName, File dhcpConfigFile, Properties kuraExtendedProps) throws KuraException {
    DhcpServerConfig4 dhcpServerConfig4 = null;
    DhcpServerTool dhcpServerTool = DhcpServerManager.getTool();
    if (dhcpServerTool == DhcpServerTool.DHCPD) {
        dhcpServerConfig4 = populateDhcpdConfig(interfaceName, dhcpConfigFile, kuraExtendedProps);
    } else if (dhcpServerTool == DhcpServerTool.UDHCPD) {
        dhcpServerConfig4 = populateUdhcpdConfig(interfaceName, dhcpConfigFile, kuraExtendedProps);
    }
    return dhcpServerConfig4;
}
Also used : DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) DhcpServerTool(org.eclipse.kura.linux.net.dhcp.DhcpServerTool)

Example 4 with DhcpServerConfig4

use of org.eclipse.kura.net.dhcp.DhcpServerConfig4 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 5 with DhcpServerConfig4

use of org.eclipse.kura.net.dhcp.DhcpServerConfig4 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)

Aggregations

DhcpServerConfig4 (org.eclipse.kura.net.dhcp.DhcpServerConfig4)8 NetConfig (org.eclipse.kura.net.NetConfig)7 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)6 KuraException (org.eclipse.kura.KuraException)4 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)4 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)3 WifiInterfaceConfigImpl (org.eclipse.kura.core.net.WifiInterfaceConfigImpl)3 WifiMode (org.eclipse.kura.net.wifi.WifiMode)3 File (java.io.File)2 IOException (java.io.IOException)2 NetInterfaceAddressConfigImpl (org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl)2 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)2 IP4Address (org.eclipse.kura.net.IP4Address)2 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)2 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)2 ModemInterfaceAddress (org.eclipse.kura.net.modem.ModemInterfaceAddress)2 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)2 Capability (org.eclipse.kura.net.wifi.WifiInterface.Capability)2 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)2 UnknownHostException (java.net.UnknownHostException)1