Search in sources :

Example 1 with WifiInterfaceAddressConfig

use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.

the class RedHatNetworkInterface method writeNewConfig.

public static void writeNewConfig(NetInterfaceConfig netInterfaceConfig) throws KuraException {
    try {
        String outputFileName = "/etc/sysconfig/network-scripts/ifcfg-" + netInterfaceConfig.getName();
        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> netInterfaceConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        s_logger.debug("There are {} NetInterfaceConfigs in this configuration", netInterfaceConfigs.size());
        boolean allowWrite = false;
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfigs) {
            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();
                        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");
                            }
                        }
                        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 {
                    mode = wifiMode.toString();
                }
                sb.append("MODE=").append(mode).append("\n");
            }
        }
        if (allowWrite) {
            FileOutputStream fos = new FileOutputStream(outputFileName);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(sb.toString());
            pw.flush();
            fos.getFD().sync();
            pw.close();
            fos.close();
        } else {
            s_logger.warn("writeNewConfig :: operation is not allowed");
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) 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) PrintWriter(java.io.PrintWriter)

Example 2 with WifiInterfaceAddressConfig

use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.

the class DebianNetworkInterface method writeNewConfig.

public static void writeNewConfig(NetInterfaceConfig<?> netInterfaceConfig) throws KuraException {
    Scanner scanner = null;
    try {
        StringBuffer sb = new StringBuffer();
        String outputFileName = NET_CONFIGURATION_DIRECTORY + "interfaces";
        kuraFile = new File(NET_CONFIGURATION_DIRECTORY + "interfaces");
        String iName = netInterfaceConfig.getName();
        if (kuraFile.exists()) {
            // found our match so load the properties
            scanner = new Scanner(new FileInputStream(kuraFile));
            // need to loop through the existing file and replace only the desired interface
            while (scanner.hasNextLine()) {
                String noTrimLine = scanner.nextLine();
                String line = noTrimLine.trim();
                // ignore comments and blank lines
                if (!line.isEmpty() && !line.startsWith("#")) {
                    String[] args = line.split("\\s+");
                    try {
                        // must be a line stating that interface starts on boot
                        if (args[1].equals(iName)) {
                            List<? extends NetInterfaceAddressConfig> netInterfaceConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                            s_logger.debug("There are {} NetInterfaceConfigs in this configuration", netInterfaceConfigs.size());
                            for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfigs) {
                                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                                if (netConfigs != null) {
                                    for (NetConfig netConfig : netConfigs) {
                                        if (netConfig instanceof NetConfigIP4) {
                                            // ONBOOT
                                            if (((NetConfigIP4) netConfig).isAutoConnect()) {
                                                sb.append("auto " + iName + "\n");
                                            }
                                            // BOOTPROTO
                                            sb.append("iface " + iName + " inet ");
                                            if (((NetConfigIP4) netConfig).isDhcp()) {
                                                s_logger.debug("new config is DHCP");
                                                sb.append("dhcp\n");
                                            } else {
                                                s_logger.debug("new config is STATIC");
                                                sb.append("static\n");
                                            }
                                            if (!((NetConfigIP4) netConfig).isDhcp()) {
                                                // IPADDR
                                                sb.append("\taddress ").append(((NetConfigIP4) netConfig).getAddress().getHostAddress()).append("\n");
                                                // NETMASK
                                                sb.append("\tnetmask ").append(((NetConfigIP4) netConfig).getSubnetMask().getHostAddress()).append("\n");
                                                // Gateway
                                                if (((NetConfigIP4) netConfig).getGateway() != null) {
                                                    sb.append("\tgateway ").append(((NetConfigIP4) netConfig).getGateway().getHostAddress()).append("\n");
                                                }
                                                // DNS
                                                List<? extends IPAddress> dnsAddresses = ((NetConfigIP4) netConfig).getDnsServers();
                                                if (!dnsAddresses.isEmpty()) {
                                                    sb.append("\tdns-nameservers ");
                                                    for (int i = 0; i < dnsAddresses.size(); i++) {
                                                        sb.append(dnsAddresses.get(i).getHostAddress() + " ");
                                                    }
                                                    sb.append("\n");
                                                }
                                            } else {
                                                // DEFROUTE
                                                if (((NetConfigIP4) netConfig).getStatus() == NetInterfaceStatus.netIPv4StatusEnabledLAN) {
                                                    sb.append("post-up route del default dev ");
                                                    sb.append(iName);
                                                    sb.append("\n");
                                                }
                                            }
                                        }
                                    }
                                } 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");
                                // TODO: Handle Wireless
                                }
                            }
                            // remove old config lines from the scanner
                            while (!(line = scanner.nextLine()).isEmpty()) {
                            }
                            sb.append("\n");
                        } else {
                            sb.append(noTrimLine + "\n");
                        }
                    } catch (Exception e) {
                    }
                } else {
                    sb.append(noTrimLine + "\n");
                }
            }
        }
        FileOutputStream fos = new FileOutputStream(outputFileName);
        PrintWriter pw = new PrintWriter(fos);
        pw.write(sb.toString());
        pw.flush();
        fos.getFD().sync();
        pw.close();
        fos.close();
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}
Also used : Scanner(java.util.Scanner) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) KuraException(org.eclipse.kura.KuraException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) KuraException(org.eclipse.kura.KuraException) FileOutputStream(java.io.FileOutputStream) NetConfig(org.eclipse.kura.net.NetConfig) File(java.io.File) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) PrintWriter(java.io.PrintWriter)

Example 3 with WifiInterfaceAddressConfig

use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.

the class WifiInterfaceAddressConfigImpl method equals.

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!super.equals(obj)) {
        return false;
    }
    if (!(obj instanceof WifiInterfaceAddressConfig)) {
        return false;
    }
    WifiInterfaceAddressConfig other = (WifiInterfaceAddressConfig) obj;
    if (!compare(getMode(), other.getMode())) {
        return false;
    }
    if (getBitrate() != other.getBitrate()) {
        return false;
    }
    if (!compare(getWifiAccessPoint(), other.getWifiAccessPoint())) {
        return false;
    }
    List<NetConfig> thisNetConfigs = getConfigs();
    List<NetConfig> otherNetConfigs = other.getConfigs();
    if (thisNetConfigs.size() != otherNetConfigs.size()) {
        return false;
    }
    if (!thisNetConfigs.containsAll(otherNetConfigs)) {
        return false;
    }
    if (!otherNetConfigs.containsAll(thisNetConfigs)) {
        return false;
    }
    return true;
}
Also used : NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)

Example 4 with WifiInterfaceAddressConfig

use of org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig in project kura by eclipse.

the class WifiMonitorServiceImpl method getReconfiguredWifiInterfaces.

private Collection<String> getReconfiguredWifiInterfaces() throws KuraException {
    Set<String> reconfiguredInterfaces = new HashSet<String>();
    for (String interfaceName : this.m_networkService.getAllNetworkInterfaceNames()) {
        // skip non-wifi interfaces
        if (LinuxNetworkUtil.getType(interfaceName) != NetInterfaceType.WIFI) {
            continue;
        }
        // ignore "mon" interface
        if (interfaceName.startsWith("mon")) {
            continue;
        }
        // Get the old wifi config
        WifiInterfaceConfigImpl currentConfig = null;
        if (this.m_currentNetworkConfiguration != null) {
            NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_currentNetworkConfiguration.getNetInterfaceConfig(interfaceName);
            if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
                currentConfig = (WifiInterfaceConfigImpl) netInterfaceConfig;
            }
        }
        // Get the new wifi config
        WifiInterfaceConfigImpl newConfig = null;
        if (this.m_newNetConfiguration != null) {
            NetInterfaceConfig<? extends NetInterfaceAddressConfig> newNetInterfaceConfig = this.m_newNetConfiguration.getNetInterfaceConfig(interfaceName);
            if (newNetInterfaceConfig instanceof WifiInterfaceConfigImpl) {
                newConfig = (WifiInterfaceConfigImpl) newNetInterfaceConfig;
            }
        }
        if (newConfig != null && currentConfig != null) {
            List<WifiInterfaceAddressConfig> currentInterfaceAddressConfigs = currentConfig.getNetInterfaceAddresses();
            List<WifiInterfaceAddressConfig> newInterfaceAddressConfigs = newConfig.getNetInterfaceAddresses();
            if (currentInterfaceAddressConfigs == null && newInterfaceAddressConfigs == null) {
                // no config changed - continue
                continue;
            }
            if (currentInterfaceAddressConfigs == null || newInterfaceAddressConfigs == null) {
                reconfiguredInterfaces.add(interfaceName);
                continue;
            }
            // TODO: compare interfaceAddressConfigs
            // FIXME - assuming one InterfaceAddressConfig for now
            WifiInterfaceAddressConfig currentInterfaceAddressConfig = currentInterfaceAddressConfigs.get(0);
            WifiInterfaceAddressConfig newInterfaceAddressConfig = newInterfaceAddressConfigs.get(0);
            if (currentInterfaceAddressConfig.getConfigs() == null && newInterfaceAddressConfig.getConfigs() == null) {
                continue;
            }
            if (currentInterfaceAddressConfig.getConfigs() == null || newInterfaceAddressConfig.getConfigs() == null) {
                reconfiguredInterfaces.add(interfaceName);
                continue;
            }
            // Remove other WifiConfigs that don't match the selected mode, for comparison purposes
            // 
            List<NetConfig> currentNetConfigs = new ArrayList<NetConfig>(currentInterfaceAddressConfig.getConfigs());
            List<NetConfig> newNetConfigs = new ArrayList<NetConfig>(newInterfaceAddressConfig.getConfigs());
            WifiMode newWifiMode = newInterfaceAddressConfig.getMode();
            WifiMode currentWifiMode = currentInterfaceAddressConfig.getMode();
            if (newWifiMode != currentWifiMode) {
                reconfiguredInterfaces.add(interfaceName);
                continue;
            }
            // Modes don't match. We need to compare configs deeply
            internalWifiConfigCompare(reconfiguredInterfaces, interfaceName, currentNetConfigs, newNetConfigs);
        } else if (newConfig != null) {
            // only newConfig - oldConfig is null
            s_logger.debug("oldConfig was null, adding newConfig");
            reconfiguredInterfaces.add(interfaceName);
        } else if (currentConfig != null) {
            s_logger.debug("Configuration for {} has changed", interfaceName);
            reconfiguredInterfaces.add(interfaceName);
            s_logger.debug("Removing {} from list of enabled interfaces because it is not configured", interfaceName);
            this.m_disabledInterfaces.add(interfaceName);
        } else {
            s_logger.debug("old and new wifi config are null...");
        }
    }
    updateInterfacesLists(reconfiguredInterfaces);
    return reconfiguredInterfaces;
}
Also used : WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) ArrayList(java.util.ArrayList) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) HashSet(java.util.HashSet)

Example 5 with WifiInterfaceAddressConfig

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

Aggregations

WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)18 NetConfig (org.eclipse.kura.net.NetConfig)14 WifiMode (org.eclipse.kura.net.wifi.WifiMode)12 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)9 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)9 KuraException (org.eclipse.kura.KuraException)8 ArrayList (java.util.ArrayList)4 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)4 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)4 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 WifiInterfaceConfigImpl (org.eclipse.kura.core.net.WifiInterfaceConfigImpl)3 IPAddress (org.eclipse.kura.net.IPAddress)3 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)3 ModemInterfaceAddressConfig (org.eclipse.kura.net.modem.ModemInterfaceAddressConfig)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 UnknownHostException (java.net.UnknownHostException)2 List (java.util.List)2