Search in sources :

Example 26 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxFirewall method deleteAllAutoNatRules.

public void deleteAllAutoNatRules() throws KuraException {
    try {
        this.m_autoNatRules.clear();
        if (this.m_natRules != null && this.m_natRules.isEmpty() && this.m_portForwardRules != null && this.m_portForwardRules.isEmpty()) {
            this.m_allowForwarding = false;
        }
        update();
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Example 27 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxNetworkUtil method getWifiBitrate.

/*
     * Returns 0 if the interface is not found or on error
     */
public static long getWifiBitrate(String ifaceName) throws KuraException {
    long bitRate = 0;
    // ignore logical interfaces like "1-1.2"
    if (Character.isDigit(ifaceName.charAt(0))) {
        return bitRate;
    }
    SafeProcess proc = null;
    BufferedReader br = null;
    String line = null;
    try {
        if (toolExists("iw")) {
            // start the process
            proc = ProcessUtil.exec("iw dev " + ifaceName + " link");
            if (proc.waitFor() != 0) {
                s_logger.warn("error executing command --- iw --- exit value = {}", proc.exitValue());
                // FIXME: why don't we fallback to iwconfig like in the case of getWifiMode()?
                return bitRate;
            } else {
                // get the output
                br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                line = null;
                while ((line = br.readLine()) != null) {
                    int index = line.indexOf("tx bitrate: ");
                    if (index > -1) {
                        s_logger.debug("line: " + line);
                        StringTokenizer st = new StringTokenizer(line.substring(index));
                        // skip 'tx'
                        st.nextToken();
                        // skip 'bitrate:'
                        st.nextToken();
                        Double rate = Double.parseDouble(st.nextToken());
                        int mult = 1;
                        String unit = st.nextToken();
                        if (unit.startsWith("kb")) {
                            mult = 1000;
                        } else if (unit.startsWith("Mb")) {
                            mult = 1000000;
                        } else if (unit.startsWith("Gb")) {
                            mult = 1000000000;
                        }
                        bitRate = (long) (rate * mult);
                        return bitRate;
                    }
                }
            }
        } else if (toolExists("iwconfig")) {
            // start the process
            proc = ProcessUtil.exec("iwconfig " + ifaceName);
            if (proc.waitFor() != 0) {
                s_logger.warn("error executing command --- iwconfig --- exit value = {}", proc.exitValue());
                // FIXME: throw exception
                return bitRate;
            }
            // get the output
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            line = null;
            while ((line = br.readLine()) != null) {
                int index = line.indexOf("Bit Rate=");
                if (index > -1) {
                    s_logger.debug("line: {}", line);
                    StringTokenizer st = new StringTokenizer(line.substring(index));
                    // skip 'Bit'
                    st.nextToken();
                    Double rate = Double.parseDouble(st.nextToken().substring(5));
                    int mult = 1;
                    String unit = st.nextToken();
                    if (unit.startsWith("kb")) {
                        mult = 1000;
                    } else if (unit.startsWith("Mb")) {
                        mult = 1000000;
                    } else if (unit.startsWith("Gb")) {
                        mult = 1000000000;
                    }
                    bitRate = (long) (rate * mult);
                    return bitRate;
                }
            }
        }
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } catch (InterruptedException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    return bitRate;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 28 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxNetworkUtil method isKernelModuleLoaded.

public static boolean isKernelModuleLoaded(String interfaceName, WifiMode wifiMode) throws KuraException {
    boolean result = false;
    if (KuraConstants.ReliaGATE_10_05.getTargetName().equals(TARGET_NAME) && "wlan0".equals(interfaceName)) {
        SafeProcess proc = null;
        BufferedReader br = null;
        String cmd = "lsmod";
        try {
            s_logger.debug("Executing '{}'", cmd);
            proc = ProcessUtil.exec(cmd);
            if (proc.waitFor() != 0) {
                throw new KuraException(KuraErrorCode.OS_COMMAND_ERROR, cmd, proc.exitValue());
            }
            // get the output
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.contains("bcmdhd")) {
                    result = true;
                    break;
                }
            }
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e, "'" + cmd + "' failed");
        } catch (InterruptedException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e, "'" + cmd + "' interrupted");
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    s_logger.warn("Failed to close process input stream", e);
                }
            }
            if (proc != null) {
                proc.destroy();
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 29 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class RedHatNetworkInterface method getCurrentConfiguration.

public static NetInterfaceConfig getCurrentConfiguration(String interfaceName, NetInterfaceType type, NetInterfaceStatus status, boolean dhcpServerEnabled, boolean passDns) throws KuraException {
    NET_CONFIGURATION_DIRECTORY = "/etc/sysconfig/network-scripts/";
    NetInterfaceConfig netInterfaceConfig = null;
    FileInputStream fis = null;
    try {
        // build up the configuration
        Properties kuraProps = new Properties();
        kuraFile = new File(NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
        if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI || type == NetInterfaceType.LOOPBACK) {
            if (kuraFile.exists()) {
                // found our match so load the properties
                fis = new FileInputStream(kuraFile);
                kuraProps.load(fis);
                s_logger.debug("getting args for {}", interfaceName);
                netInterfaceConfig = getCurrentConfig(interfaceName, type, status, dhcpServerEnabled, passDns, kuraProps);
            } else {
                netInterfaceConfig = getCurrentConfig(interfaceName, type, NetInterfaceStatus.netIPv4StatusDisabled, dhcpServerEnabled, passDns, null);
            }
        } else if (type == NetInterfaceType.MODEM) {
            s_logger.debug("getting args for {}", interfaceName);
            kuraProps.setProperty("BOOTPROTO", "dhcp");
            kuraProps.setProperty("ONBOOT", "yes");
            netInterfaceConfig = getCurrentConfig(interfaceName, type, status, dhcpServerEnabled, passDns, kuraProps);
        } else {
            s_logger.error("Unsupported type: " + type.toString() + " for network interface: " + interfaceName);
        }
    } catch (Exception e) {
        s_logger.error("Error getting configuration for interface: " + interfaceName, e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing FileInputStream!");
            }
        }
    }
    return netInterfaceConfig;
}
Also used : NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Example 30 with KuraException

use of org.eclipse.kura.KuraException 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)

Aggregations

KuraException (org.eclipse.kura.KuraException)315 IOException (java.io.IOException)191 CommConnection (org.eclipse.kura.comm.CommConnection)54 SafeProcess (org.eclipse.kura.core.util.SafeProcess)49 ArrayList (java.util.ArrayList)46 File (java.io.File)44 BufferedReader (java.io.BufferedReader)42 InputStreamReader (java.io.InputStreamReader)30 UnknownHostException (java.net.UnknownHostException)30 FileNotFoundException (java.io.FileNotFoundException)25 NetConfig (org.eclipse.kura.net.NetConfig)23 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)20 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)19 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)19 Properties (java.util.Properties)18 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)17 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)17 StringTokenizer (java.util.StringTokenizer)16 FileOutputStream (java.io.FileOutputStream)15 IP4Address (org.eclipse.kura.net.IP4Address)15