Search in sources :

Example 21 with NetConfig

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

the class FirewallAutoNatConfigWriter method writeConfig.

private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraProps) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    s_logger.debug("Writing NAT config for {}", interfaceName);
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = null;
    netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    boolean natEnabled = false;
    boolean useMasquerade = false;
    String srcIface = null;
    String dstIface = null;
    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 FirewallAutoNatConfig) {
                        natEnabled = true;
                        srcIface = ((FirewallAutoNatConfig) netConfig).getSourceInterface();
                        dstIface = ((FirewallAutoNatConfig) netConfig).getDestinationInterface();
                        useMasquerade = ((FirewallAutoNatConfig) netConfig).isMasquerade();
                    }
                }
            }
        }
    }
    // set it all
    if (kuraProps == null) {
        s_logger.debug("kuraExtendedProps was null");
        kuraProps = new Properties();
    }
    StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.nat.enabled");
    kuraProps.put(sb.toString(), Boolean.toString(natEnabled));
    if (natEnabled && srcIface != null && dstIface != null) {
        sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.nat.dst.interface");
        kuraProps.put(sb.toString(), dstIface);
        sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.nat.masquerade");
        kuraProps.put(sb.toString(), Boolean.toString(useMasquerade));
    }
    // write it
    if (kuraProps != null && !kuraProps.isEmpty()) {
        try {
            KuranetConfig.storeProperties(kuraProps);
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) Properties(java.util.Properties) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) KuraException(org.eclipse.kura.KuraException)

Example 22 with NetConfig

use of org.eclipse.kura.net.NetConfig 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));
        }
    }
}
Also used : NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 23 with NetConfig

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

the class PppConfigReader method getConfig.

private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    s_logger.debug("Getting ppp config for {}", interfaceName);
    if (netInterfaceConfig instanceof ModemInterfaceConfigImpl) {
        StringBuilder key = new StringBuilder("net.interface." + netInterfaceConfig.getName() + ".modem.identifier");
        String modemId = KuranetConfig.getProperty(key.toString());
        s_logger.debug("Getting modem identifier using key " + key + ": " + modemId);
        if (modemId != null) {
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setModemIdentifier(modemId);
        }
    }
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
        if (netInterfaceAddressConfig instanceof ModemInterfaceAddressConfigImpl) {
            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
            if (netConfigs == null) {
                netConfigs = new ArrayList<NetConfig>();
                ((ModemInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
            }
            // Create a ModemConfig
            ModemConfig modemConfig = getModemConfig(interfaceName, netInterfaceConfig.getUsbDevice());
            if (modemConfig != null) {
                netConfigs.add(modemConfig);
            }
            // Create a NetConfigIP4
            netConfigs.add(getNetConfigIP4(interfaceName));
            // Populate with DNS provided by PPP (displayed as read-only in Denali)
            if (LinuxNetworkUtil.hasAddress("ppp" + modemConfig.getPppNumber())) {
                List<? extends IPAddress> pppDnsServers = LinuxDns.getInstance().getPppDnServers();
                if (pppDnsServers != null) {
                    ((ModemInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(pppDnsServers);
                }
            }
        }
    }
}
Also used : ModemInterfaceAddressConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 24 with NetConfig

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

use of org.eclipse.kura.net.NetConfig 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

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