Search in sources :

Example 6 with NetConfigIP4

use of org.eclipse.kura.net.NetConfigIP4 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;
}
Also used : 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) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 7 with NetConfigIP4

use of org.eclipse.kura.net.NetConfigIP4 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 8 with NetConfigIP4

use of org.eclipse.kura.net.NetConfigIP4 in project kura by eclipse.

the class PppConfigReader method getNetConfigIP4.

private static NetConfigIP4 getNetConfigIP4(String interfaceName) throws KuraException {
    NetConfigIP4 netConfigIP4 = null;
    NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
    StringBuilder key = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.ip4.status");
    String statusString = KuranetConfig.getProperty(key.toString());
    if (statusString != null && !statusString.isEmpty()) {
        netInterfaceStatus = NetInterfaceStatus.valueOf(statusString);
    }
    s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + interfaceName);
    netConfigIP4 = new NetConfigIP4(netInterfaceStatus, true, true);
    key = new StringBuilder("net.interface.").append(interfaceName).append(".config.dnsServers");
    String dnsServersStr = KuranetConfig.getProperty(key.toString());
    List<IP4Address> dnsServersList = new ArrayList<IP4Address>();
    if (dnsServersStr != null && !dnsServersStr.isEmpty()) {
        String[] serversArr = dnsServersStr.split(PppConfigWriter.DNS_DELIM);
        for (String server : serversArr) {
            try {
                dnsServersList.add((IP4Address) IPAddress.parseHostAddress(server));
            } catch (UnknownHostException e) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
            }
        }
        netConfigIP4.setDnsServers(dnsServersList);
    }
    return netConfigIP4;
}
Also used : UnknownHostException(java.net.UnknownHostException) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 9 with NetConfigIP4

use of org.eclipse.kura.net.NetConfigIP4 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);
                        }
                    }
                }
            }
        }
    }
}
Also used : NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 10 with NetConfigIP4

use of org.eclipse.kura.net.NetConfigIP4 in project kura by eclipse.

the class IfcfgConfigWriter method writeRedhatConfig.

private void writeRedhatConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    String outputFileName = new StringBuffer().append(REDHAT_NET_CONFIGURATION_DIRECTORY).append("ifcfg-").append(interfaceName).toString();
    String tmpOutputFileName = new StringBuffer().append(REDHAT_NET_CONFIGURATION_DIRECTORY).append("ifcfg-").append(interfaceName).append(".tmp").toString();
    s_logger.debug("Writing config for {}", interfaceName);
    NetInterfaceType type = netInterfaceConfig.getType();
    if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI || type == NetInterfaceType.LOOPBACK) {
        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> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        s_logger.debug("There are " + netInterfaceAddressConfigs.size() + " NetInterfaceConfigs in this configuration");
        boolean allowWrite = false;
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            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();
                        if (dnsAddresses != null && dnsAddresses.size() > 0) {
                            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");
                                }
                            }
                        } else {
                            s_logger.debug("no DNS entries");
                        }
                        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 if (wifiMode == null) {
                    s_logger.error("WifiMode is null");
                    mode = "null";
                } else {
                    mode = wifiMode.toString();
                }
                sb.append("MODE=").append(mode).append("\n");
            }
        }
        if (allowWrite) {
            FileOutputStream fos = null;
            PrintWriter pw = null;
            try {
                fos = new FileOutputStream(tmpOutputFileName);
                pw = new PrintWriter(fos);
                pw.write(sb.toString());
                pw.flush();
                fos.getFD().sync();
            } catch (Exception e) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ex) {
                        s_logger.error("I/O Exception while closing BufferedReader!");
                    }
                }
                if (pw != null) {
                    pw.close();
                }
            }
            // move the file if we made it this far
            File tmpFile = new File(tmpOutputFileName);
            File outputFile = new File(outputFileName);
            try {
                if (!FileUtils.contentEquals(tmpFile, outputFile)) {
                    if (tmpFile.renameTo(outputFile)) {
                        s_logger.trace("Successfully wrote network interface file for {}", interfaceName);
                    } else {
                        s_logger.error("Failed to write network interface file");
                        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "error while building up new configuration file for network interface " + interfaceName);
                    }
                } else {
                    s_logger.info("Not rewriting network interfaces file for " + interfaceName + " because it is the same");
                }
            } catch (IOException e) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
            }
        } else {
            s_logger.warn("writeNewConfig :: operation is not allowed");
        }
    }
}
Also used : WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) FileOutputStream(java.io.FileOutputStream) NetConfig(org.eclipse.kura.net.NetConfig) IPAddress(org.eclipse.kura.net.IPAddress) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)29 NetConfig (org.eclipse.kura.net.NetConfig)25 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)20 KuraException (org.eclipse.kura.KuraException)19 ArrayList (java.util.ArrayList)13 IP4Address (org.eclipse.kura.net.IP4Address)11 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)11 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)11 WifiMode (org.eclipse.kura.net.wifi.WifiMode)11 UnknownHostException (java.net.UnknownHostException)10 IOException (java.io.IOException)9 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)9 IPAddress (org.eclipse.kura.net.IPAddress)7 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)7 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)7 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)6 List (java.util.List)5 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)5 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)4 NetInterfaceType (org.eclipse.kura.net.NetInterfaceType)4