Search in sources :

Example 6 with WifiSecurity

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

the class HostapdConfigReader method getWifiHostConfig.

private static WifiConfig getWifiHostConfig(String ifaceName) throws KuraException {
    try {
        WifiConfig wifiConfig = new WifiConfig();
        wifiConfig.setMode(WifiMode.MASTER);
        File configFile = new File(HostapdManager.getHostapdConfigFileName(ifaceName));
        Properties hostapdProps = new Properties();
        s_logger.debug("parsing hostapd config file: " + configFile.getAbsolutePath());
        if (configFile.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                hostapdProps.load(fis);
            } finally {
                if (null != fis) {
                    fis.close();
                }
            }
            // remove any quotes around the values
            Enumeration<Object> keys = hostapdProps.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                String val = hostapdProps.getProperty(key);
                if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
                    hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
                }
            }
            String iface = hostapdProps.getProperty("interface");
            if (ifaceName != null && ifaceName.equals(iface)) {
                String driver = hostapdProps.getProperty("driver");
                String essid = hostapdProps.getProperty("ssid");
                int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
                int ignoreSSID = Integer.parseInt(hostapdProps.getProperty("ignore_broadcast_ssid"));
                // Determine radio mode
                WifiRadioMode wifiRadioMode = null;
                String hwModeStr = hostapdProps.getProperty("hw_mode");
                if ("a".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
                } else if ("b".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
                } else if ("g".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
                    if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
                        wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
                        String ht_capab = hostapdProps.getProperty("ht_capab");
                        if (ht_capab != null) {
                            if (ht_capab.contains("HT40+")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
                            } else if (ht_capab.contains("HT40-")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
                            }
                        }
                    }
                } else {
                    throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
                }
                // Determine security and pass
                WifiSecurity security = WifiSecurity.SECURITY_NONE;
                String password = "";
                if (hostapdProps.containsKey("wpa")) {
                    if ("1".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA;
                    } else if ("2".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA2;
                    } else if ("3".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA_WPA2;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                    if (hostapdProps.containsKey("wpa_passphrase")) {
                        password = hostapdProps.getProperty("wpa_passphrase");
                    } else if (hostapdProps.containsKey("wpa_psk")) {
                        password = hostapdProps.getProperty("wpa_psk");
                    } else {
                        throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
                    }
                } else if (hostapdProps.containsKey("wep_key0")) {
                    security = WifiSecurity.SECURITY_WEP;
                    password = hostapdProps.getProperty("wep_key0");
                }
                WifiCiphers pairwise = null;
                if (hostapdProps.containsKey("wpa_pairwise")) {
                    if ("TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.TKIP;
                    } else if ("CCMP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP;
                    } else if ("CCMP TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP_TKIP;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                }
                // Populate the config
                wifiConfig.setSSID(essid);
                wifiConfig.setDriver(driver);
                wifiConfig.setChannels(new int[] { channel });
                wifiConfig.setPasskey(password);
                wifiConfig.setSecurity(security);
                wifiConfig.setPairwiseCiphers(pairwise);
                wifiConfig.setRadioMode(wifiRadioMode);
                if (ignoreSSID == 0) {
                    wifiConfig.setIgnoreSSID(false);
                    wifiConfig.setBroadcast(true);
                } else {
                    wifiConfig.setIgnoreSSID(true);
                    wifiConfig.setBroadcast(false);
                }
                // hw mode
                if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211b) {
                    wifiConfig.setHardwareMode("b");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211g) {
                    wifiConfig.setHardwareMode("g");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT20 || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40above || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
                    // TODO: specify these 'n' modes separately?
                    wifiConfig.setHardwareMode("n");
                }
            }
        } else {
            s_logger.warn("getWifiHostConfig() :: {} file doesn't exist, will generate default wifiConfig", configFile.getName());
            wifiConfig.setSSID("kura_gateway");
            wifiConfig.setDriver("nl80211");
            wifiConfig.setChannels(new int[] { 11 });
            wifiConfig.setPasskey("");
            wifiConfig.setSecurity(WifiSecurity.SECURITY_NONE);
            wifiConfig.setPairwiseCiphers(WifiCiphers.CCMP);
            wifiConfig.setRadioMode(WifiRadioMode.RADIO_MODE_80211b);
            wifiConfig.setIgnoreSSID(false);
            wifiConfig.setBroadcast(true);
            wifiConfig.setHardwareMode("b");
        }
        return wifiConfig;
    } catch (Exception e) {
        s_logger.error("Exception getting WiFi configuration", e);
        throw KuraException.internalError(e);
    }
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) KuraException(org.eclipse.kura.KuraException)

Example 7 with WifiSecurity

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

the class NetworkAdminServiceImpl method getWifiHotspots.

@Override
public Map<String, WifiHotspotInfo> getWifiHotspots(String ifaceName) throws KuraException {
    Map<String, WifiHotspotInfo> mWifiHotspotInfo = new HashMap<String, WifiHotspotInfo>();
    WifiMode wifiMode = WifiMode.UNKNOWN;
    List<? extends NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = getNetworkInterfaceConfigs();
    for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
        if (netInterfaceConfig.getName().equals(ifaceName)) {
            List<? extends NetInterfaceAddressConfig> netInterfaceAddresses = netInterfaceConfig.getNetInterfaceAddresses();
            if (netInterfaceAddresses != null) {
                for (NetInterfaceAddressConfig netInterfaceAddress : netInterfaceAddresses) {
                    if (netInterfaceAddress instanceof WifiInterfaceAddressConfig) {
                        wifiMode = ((WifiInterfaceAddressConfig) netInterfaceAddress).getMode();
                    }
                }
            }
            break;
        }
    }
    try {
        if (wifiMode == WifiMode.MASTER) {
            reloadKernelModule(ifaceName, WifiMode.INFRA);
            WpaSupplicantConfigWriter wpaSupplicantConfigWriter = WpaSupplicantConfigWriter.getInstance();
            wpaSupplicantConfigWriter.generateTempWpaSupplicantConf();
            s_logger.debug("getWifiHotspots() :: Starting temporary instance of wpa_supplicant");
            StringBuilder key = new StringBuilder("net.interface." + ifaceName + ".config.wifi.infra.driver");
            String driver = KuranetConfig.getProperty(key.toString());
            WpaSupplicantManager.startTemp(ifaceName, WifiMode.INFRA, driver);
            wifiModeWait(ifaceName, WifiMode.INFRA, 10);
        }
        s_logger.info("getWifiHotspots() :: scanning for available access points ...");
        IScanTool scanTool = ScanTool.get(ifaceName);
        if (scanTool != null) {
            List<WifiAccessPoint> wifiAccessPoints = scanTool.scan();
            for (WifiAccessPoint wap : wifiAccessPoints) {
                if (wap.getSSID() == null || wap.getSSID().length() == 0) {
                    s_logger.debug("Skipping hidden SSID");
                    continue;
                }
                // if (!wap.getSSID().matches(SSID_REGEXP)){
                // s_logger.debug("Skipping undesired SSID");
                // continue;
                // }
                s_logger.trace("getWifiHotspots() :: SSID={}", wap.getSSID());
                s_logger.trace("getWifiHotspots() :: Signal={}", wap.getStrength());
                s_logger.trace("getWifiHotspots() :: Frequency={}", wap.getFrequency());
                byte[] baMacAddress = wap.getHardwareAddress();
                StringBuffer sbMacAddress = new StringBuffer();
                for (int i = 0; i < baMacAddress.length; i++) {
                    sbMacAddress.append(String.format("%02x", baMacAddress[i] & 0x0ff).toUpperCase());
                    if (i < baMacAddress.length - 1) {
                        sbMacAddress.append(':');
                    }
                }
                WifiSecurity wifiSecurity = WifiSecurity.NONE;
                EnumSet<WifiSecurity> esWpaSecurity = wap.getWpaSecurity();
                if (esWpaSecurity != null && !esWpaSecurity.isEmpty()) {
                    wifiSecurity = WifiSecurity.SECURITY_WPA;
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        s_logger.trace("getWifiHotspots() :: WPA Security={}", itWpaSecurity.next());
                    }
                }
                EnumSet<WifiSecurity> esRsnSecurity = wap.getRsnSecurity();
                if (esRsnSecurity != null && !esRsnSecurity.isEmpty()) {
                    if (wifiSecurity == WifiSecurity.SECURITY_WPA) {
                        wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                    } else {
                        wifiSecurity = WifiSecurity.SECURITY_WPA2;
                    }
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        s_logger.trace("getWifiHotspots() :: RSN Security={}", itRsnSecurity.next());
                    }
                }
                if (wifiSecurity == WifiSecurity.NONE) {
                    List<String> capabilities = wap.getCapabilities();
                    if (capabilities != null && !capabilities.isEmpty()) {
                        for (String capab : capabilities) {
                            if (capab.equals("Privacy")) {
                                wifiSecurity = WifiSecurity.SECURITY_WEP;
                                break;
                            }
                        }
                    }
                }
                int frequency = (int) wap.getFrequency();
                int channel = frequencyMhz2Channel(frequency);
                EnumSet<WifiSecurity> pairCiphers = EnumSet.noneOf(WifiSecurity.class);
                EnumSet<WifiSecurity> groupCiphers = EnumSet.noneOf(WifiSecurity.class);
                if (wifiSecurity == WifiSecurity.SECURITY_WPA_WPA2) {
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        WifiSecurity securityEntry = itWpaSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        WifiSecurity securityEntry = itRsnSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            if (!pairCiphers.contains(securityEntry)) {
                                pairCiphers.add(securityEntry);
                            }
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            if (!groupCiphers.contains(securityEntry)) {
                                groupCiphers.add(securityEntry);
                            }
                        }
                    }
                } else if (wifiSecurity == WifiSecurity.SECURITY_WPA) {
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        WifiSecurity securityEntry = itWpaSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                } else if (wifiSecurity == WifiSecurity.SECURITY_WPA2) {
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        WifiSecurity securityEntry = itRsnSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                }
                WifiHotspotInfo wifiHotspotInfo = new WifiHotspotInfo(wap.getSSID(), sbMacAddress.toString(), 0 - wap.getStrength(), channel, frequency, wifiSecurity, pairCiphers, groupCiphers);
                mWifiHotspotInfo.put(wap.getSSID(), wifiHotspotInfo);
            }
        }
        if (wifiMode == WifiMode.MASTER) {
            if (WpaSupplicantManager.isTempRunning()) {
                s_logger.debug("getWifiHotspots() :: stoping temporary instance of wpa_supplicant");
                WpaSupplicantManager.stop(ifaceName);
            }
            reloadKernelModule(ifaceName, WifiMode.MASTER);
        }
    } catch (Throwable t) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, t, "scan operation has failed");
    }
    return mWifiHotspotInfo;
}
Also used : HashMap(java.util.HashMap) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) IScanTool(org.eclipse.kura.linux.net.util.IScanTool) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) Iterator(java.util.Iterator) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WpaSupplicantConfigWriter(org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 8 with WifiSecurity

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

the class iwlistScanTool method parse.

private List<WifiAccessPoint> parse() throws Exception {
    List<WifiAccessPoint> wifiAccessPoints = new ArrayList<WifiAccessPoint>();
    // get the output
    BufferedReader br = new BufferedReader(new InputStreamReader(this.m_process.getInputStream()));
    String line = null;
    String ssid = null;
    List<Long> bitrate = null;
    long frequency = -1;
    byte[] hardwareAddress = null;
    WifiMode mode = null;
    EnumSet<WifiSecurity> rsnSecurity = null;
    int strength = -1;
    EnumSet<WifiSecurity> wpaSecurity = null;
    while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("Cell")) {
            // new AP
            if (ssid != null) {
                WifiAccessPointImpl wifiAccessPoint = new WifiAccessPointImpl(ssid);
                wifiAccessPoint.setBitrate(bitrate);
                wifiAccessPoint.setFrequency(frequency);
                wifiAccessPoint.setHardwareAddress(hardwareAddress);
                wifiAccessPoint.setMode(mode);
                wifiAccessPoint.setRsnSecurity(rsnSecurity);
                wifiAccessPoint.setStrength(strength);
                wifiAccessPoint.setWpaSecurity(wpaSecurity);
                wifiAccessPoints.add(wifiAccessPoint);
            }
            // reset
            ssid = null;
            bitrate = null;
            frequency = -1;
            hardwareAddress = null;
            mode = null;
            rsnSecurity = null;
            strength = -1;
            wpaSecurity = null;
            // parse out the MAC
            StringTokenizer st = new StringTokenizer(line, " ");
            // eat Cell
            st.nextToken();
            // eat Cell #
            st.nextToken();
            // eat '-'
            st.nextToken();
            // eat 'Address:'
            st.nextToken();
            String macAddressString = st.nextToken();
            if (macAddressString != null) {
                hardwareAddress = NetworkUtil.macToBytes(macAddressString);
            }
        } else if (line.startsWith("ESSID:")) {
            ssid = line.substring("ESSID:".length() + 1, line.length() - 1);
        } else if (line.startsWith("Quality=")) {
            StringTokenizer st = new StringTokenizer(line, " ");
            // eat 'Quality='
            st.nextToken();
            // eat 'Signal'
            st.nextToken();
            String signalLevel = st.nextToken();
            if (signalLevel != null) {
                signalLevel = signalLevel.substring(signalLevel.indexOf('=') + 1);
                if (signalLevel.contains("/")) {
                    // Could also be of format 39/100
                    final String[] parts = signalLevel.split("/");
                    strength = (int) Float.parseFloat(parts[0]);
                    strength = SignalStrengthConversion.getRssi(strength);
                } else {
                    strength = (int) Float.parseFloat(signalLevel);
                }
                strength = Math.abs(strength);
            }
        } else if (line.startsWith("Mode:")) {
            line = line.substring("Mode:".length());
            if (line.equals("Master")) {
                mode = WifiMode.MASTER;
            }
        } else if (line.startsWith("Frequency:")) {
            line = line.substring("Frequency:".length(), line.indexOf(' '));
            frequency = (long) (Float.parseFloat(line) * 1000);
        } else if (line.startsWith("Bit Rates:")) {
            if (bitrate == null) {
                bitrate = new ArrayList<Long>();
            }
            line = line.substring("Bit Rates:".length());
            String[] bitRates = line.split(";");
            for (String rate : bitRates) {
                if (rate != null) {
                    rate = rate.trim();
                    if (rate.length() > 0) {
                        rate = rate.substring(0, rate.indexOf(' '));
                        bitrate.add((long) (Float.parseFloat(rate) * 1000000));
                    }
                }
            }
        } else if (line.contains("IE: IEEE 802.11i/WPA2")) {
            rsnSecurity = EnumSet.noneOf(WifiSecurity.class);
            boolean foundGroup = false;
            boolean foundPairwise = false;
            boolean foundAuthSuites = false;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.contains("Group Cipher")) {
                    foundGroup = true;
                    if (line.contains("CCMP")) {
                        rsnSecurity.add(WifiSecurity.GROUP_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        rsnSecurity.add(WifiSecurity.GROUP_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        rsnSecurity.add(WifiSecurity.GROUP_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        rsnSecurity.add(WifiSecurity.GROUP_WEP40);
                    }
                } else if (line.contains("Pairwise Ciphers")) {
                    foundPairwise = true;
                    if (line.contains("CCMP")) {
                        rsnSecurity.add(WifiSecurity.PAIR_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        rsnSecurity.add(WifiSecurity.PAIR_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        rsnSecurity.add(WifiSecurity.PAIR_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        rsnSecurity.add(WifiSecurity.PAIR_WEP40);
                    }
                } else if (line.contains("Authentication Suites")) {
                    foundAuthSuites = true;
                    if (line.contains("802_1X")) {
                        rsnSecurity.add(WifiSecurity.KEY_MGMT_802_1X);
                    }
                    if (line.contains("PSK")) {
                        rsnSecurity.add(WifiSecurity.KEY_MGMT_PSK);
                    }
                } else {
                    s_logger.debug("Ignoring line in RSN: {}", line);
                }
                if (foundGroup && foundPairwise && foundAuthSuites) {
                    break;
                }
            }
        } else if (line.contains("IE: WPA Version")) {
            wpaSecurity = EnumSet.noneOf(WifiSecurity.class);
            boolean foundGroup = false;
            boolean foundPairwise = false;
            boolean foundAuthSuites = false;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.contains("Group Cipher")) {
                    foundGroup = true;
                    if (line.contains("CCMP")) {
                        wpaSecurity.add(WifiSecurity.GROUP_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        wpaSecurity.add(WifiSecurity.GROUP_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        wpaSecurity.add(WifiSecurity.GROUP_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        wpaSecurity.add(WifiSecurity.GROUP_WEP40);
                    }
                } else if (line.contains("Pairwise Ciphers")) {
                    foundPairwise = true;
                    if (line.contains("CCMP")) {
                        wpaSecurity.add(WifiSecurity.PAIR_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        wpaSecurity.add(WifiSecurity.PAIR_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        wpaSecurity.add(WifiSecurity.PAIR_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        wpaSecurity.add(WifiSecurity.PAIR_WEP40);
                    }
                } else if (line.contains("Authentication Suites")) {
                    foundAuthSuites = true;
                    if (line.contains("802_1X")) {
                        wpaSecurity.add(WifiSecurity.KEY_MGMT_802_1X);
                    }
                    if (line.contains("PSK")) {
                        wpaSecurity.add(WifiSecurity.KEY_MGMT_PSK);
                    }
                } else {
                    s_logger.debug("Ignoring line in WPA: {}", line);
                }
                if (foundGroup && foundPairwise && foundAuthSuites) {
                    break;
                }
            }
        }
    }
    // store the last one
    if (ssid != null) {
        WifiAccessPointImpl wifiAccessPoint = new WifiAccessPointImpl(ssid);
        wifiAccessPoint.setBitrate(bitrate);
        wifiAccessPoint.setFrequency(frequency);
        wifiAccessPoint.setHardwareAddress(hardwareAddress);
        wifiAccessPoint.setMode(mode);
        wifiAccessPoint.setRsnSecurity(rsnSecurity);
        wifiAccessPoint.setStrength(strength);
        wifiAccessPoint.setWpaSecurity(wpaSecurity);
        wifiAccessPoints.add(wifiAccessPoint);
    }
    br.close();
    return wifiAccessPoints;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) StringTokenizer(java.util.StringTokenizer) WifiAccessPointImpl(org.eclipse.kura.core.net.WifiAccessPointImpl) WifiMode(org.eclipse.kura.net.wifi.WifiMode) BufferedReader(java.io.BufferedReader) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity)

Example 9 with WifiSecurity

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

the class Hostapd method parseHostapdConf.

/*
     * Return a Hostapd instance from a given config file
     */
private static Hostapd parseHostapdConf(String filename) throws KuraException {
    FileInputStream fis = null;
    try {
        Hostapd hostapd = null;
        File configFile = new File(filename);
        Properties hostapdProps = new Properties();
        s_logger.debug("parsing hostapd config file: {}", configFile.getAbsolutePath());
        if (configFile.exists()) {
            fis = new FileInputStream(configFile);
            hostapdProps.load(fis);
            // remove any quotes around the values
            Enumeration<Object> keys = hostapdProps.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                String val = hostapdProps.getProperty(key);
                if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
                    hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
                }
            }
            String iface = hostapdProps.getProperty("interface");
            String driver = hostapdProps.getProperty("driver");
            String essid = hostapdProps.getProperty("ssid");
            int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
            // Determine radio mode
            WifiRadioMode wifiRadioMode = null;
            String hwModeStr = hostapdProps.getProperty("hw_mode");
            if ("a".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
            } else if ("b".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
            } else if ("g".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
                if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
                    String ht_capab = hostapdProps.getProperty("ht_capab");
                    if (ht_capab != null) {
                        if (ht_capab.contains("HT40+")) {
                            wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
                        } else if (ht_capab.contains("HT40-")) {
                            wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
                        }
                    }
                }
            } else {
                throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
            }
            // Determine security and pass
            WifiSecurity security = WifiSecurity.SECURITY_NONE;
            String passwd = "";
            if (hostapdProps.containsKey("wpa")) {
                if ("1".equals(hostapdProps.getProperty("wpa"))) {
                    security = WifiSecurity.SECURITY_WPA;
                } else if ("2".equals(hostapdProps.getProperty("wpa"))) {
                    security = WifiSecurity.SECURITY_WPA2;
                } else {
                    throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                }
                if (hostapdProps.containsKey("wpa_passphrase")) {
                    passwd = hostapdProps.getProperty("wpa_passphrase");
                } else if (hostapdProps.containsKey("wpa_psk")) {
                    passwd = hostapdProps.getProperty("wpa_psk");
                } else {
                    throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
                }
            } else if (hostapdProps.containsKey("wep_key0")) {
                security = WifiSecurity.SECURITY_WEP;
                passwd = hostapdProps.getProperty("wep_key0");
            }
            hostapd = new Hostapd(iface, driver, essid, wifiRadioMode, channel, security, passwd);
        } else {
            hostapd = new Hostapd();
        }
        return hostapd;
    } catch (Exception e) {
        e.printStackTrace();
        throw KuraException.internalError(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
    }
}
Also used : WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) 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 10 with WifiSecurity

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

the class NetworkConfiguration method getWifiConfig.

private static WifiConfig getWifiConfig(String netIfConfigPrefix, WifiMode mode, Map<String, Object> properties) throws KuraException {
    String key;
    WifiConfig wifiConfig = new WifiConfig();
    StringBuilder prefix = new StringBuilder(netIfConfigPrefix).append("wifi.").append(mode.toString().toLowerCase());
    // mode
    s_logger.trace("mode is {}", mode.toString());
    wifiConfig.setMode(mode);
    // ssid
    key = prefix + ".ssid";
    String ssid = (String) properties.get(key);
    if (ssid == null) {
        ssid = "";
    }
    s_logger.trace("SSID is {}", ssid);
    wifiConfig.setSSID(ssid);
    // driver
    key = prefix + ".driver";
    String driver = (String) properties.get(key);
    if (driver == null) {
        driver = "";
    }
    s_logger.trace("driver is {}", driver);
    wifiConfig.setDriver(driver);
    // security
    key = prefix + ".securityType";
    WifiSecurity wifiSecurity = WifiSecurity.NONE;
    String securityString = (String) properties.get(key);
    s_logger.trace("securityString is {}", securityString);
    if (securityString != null && !securityString.isEmpty()) {
        try {
            wifiSecurity = WifiSecurity.valueOf(securityString);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi security " + securityString);
        }
    }
    wifiConfig.setSecurity(wifiSecurity);
    // channels
    key = prefix + ".channel";
    String channelsString = (String) properties.get(key);
    s_logger.trace("channelsString is {}", channelsString);
    if (channelsString != null) {
        channelsString = channelsString.trim();
        if (channelsString.length() > 0) {
            StringTokenizer st = new StringTokenizer(channelsString, " ");
            int tokens = st.countTokens();
            if (tokens > 0) {
                int[] channels = new int[tokens];
                for (int i = 0; i < tokens; i++) {
                    String token = st.nextToken();
                    try {
                        channels[i] = Integer.parseInt(token);
                    } catch (Exception e) {
                        s_logger.error("Error parsing channels!", e);
                    }
                }
                wifiConfig.setChannels(channels);
            }
        }
    }
    // passphrase
    key = prefix + ".passphrase";
    Object psswdObj = properties.get(key);
    Password psswd = null;
    if (psswdObj instanceof Password) {
        psswd = (Password) psswdObj;
    } else if (psswdObj instanceof String) {
        char[] tempPsswd = ((String) psswdObj).toCharArray();
        psswd = new Password(tempPsswd);
    }
    String passphrase = new String(psswd.getPassword());
    s_logger.trace("passphrase is {}", passphrase);
    wifiConfig.setPasskey(passphrase);
    // hardware mode
    key = prefix + ".hardwareMode";
    String hwMode = (String) properties.get(key);
    if (hwMode == null) {
        hwMode = "";
    }
    s_logger.trace("hwMode is {}", hwMode);
    wifiConfig.setHardwareMode(hwMode);
    // ignore SSID
    key = prefix + ".ignoreSSID";
    boolean ignoreSSID = false;
    if (properties.get(key) != null) {
        ignoreSSID = (Boolean) properties.get(key);
        s_logger.trace("Ignore SSID is {}", ignoreSSID);
    } else {
        s_logger.trace("Ignore SSID is null");
    }
    wifiConfig.setIgnoreSSID(ignoreSSID);
    key = prefix + ".pairwiseCiphers";
    String pairwiseCiphers = (String) properties.get(key);
    if (pairwiseCiphers != null) {
        wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
    }
    if (mode == WifiMode.INFRA) {
        key = prefix + ".bgscan";
        String bgscan = (String) properties.get(key);
        if (bgscan == null) {
            bgscan = "";
        }
        s_logger.trace("bgscan is {}", bgscan);
        wifiConfig.setBgscan(new WifiBgscan(bgscan));
        /*
             * key = prefix + ".pairwiseCiphers";
             * String pairwiseCiphers = (String)properties.get(key);
             * if (pairwiseCiphers != null) {
             * wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
             * }
             */
        key = prefix + ".groupCiphers";
        String groupCiphers = (String) properties.get(key);
        if (groupCiphers != null) {
            wifiConfig.setGroupCiphers(WifiCiphers.valueOf(groupCiphers));
        }
        // ping access point?
        key = prefix + ".pingAccessPoint";
        boolean pingAccessPoint = false;
        if (properties.get(key) != null) {
            pingAccessPoint = (Boolean) properties.get(key);
            s_logger.trace("Ping Access Point is {}", pingAccessPoint);
        } else {
            s_logger.trace("Ping Access Point is null");
        }
        wifiConfig.setPingAccessPoint(pingAccessPoint);
    }
    // broadcast
    key = prefix + ".broadcast";
    Boolean broadcast = (Boolean) properties.get(key);
    if (broadcast != null) {
        wifiConfig.setBroadcast(broadcast);
    }
    s_logger.trace("hwMode is {}", hwMode);
    // radio mode
    key = prefix + ".radioMode";
    WifiRadioMode radioMode;
    String radioModeString = (String) properties.get(key);
    s_logger.trace("radioModeString is {}", radioModeString);
    if (radioModeString != null && !radioModeString.isEmpty()) {
        try {
            radioMode = WifiRadioMode.valueOf(radioModeString);
            wifiConfig.setRadioMode(radioMode);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi radio mode " + radioModeString);
        }
    }
    if (!wifiConfig.isValid()) {
        return null;
    } else {
        s_logger.trace("Returning wifiConfig: {}", wifiConfig);
        return wifiConfig;
    }
}
Also used : WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException) StringTokenizer(java.util.StringTokenizer) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) KuraException(org.eclipse.kura.KuraException) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiBgscan(org.eclipse.kura.net.wifi.WifiBgscan) Password(org.eclipse.kura.configuration.Password)

Aggregations

WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)11 KuraException (org.eclipse.kura.KuraException)5 Properties (java.util.Properties)4 WifiHotspotInfo (org.eclipse.kura.net.wifi.WifiHotspotInfo)4 StringTokenizer (java.util.StringTokenizer)3 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)3 WifiBgscan (org.eclipse.kura.net.wifi.WifiBgscan)3 WifiCiphers (org.eclipse.kura.net.wifi.WifiCiphers)3 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)3 WifiMode (org.eclipse.kura.net.wifi.WifiMode)3 WifiRadioMode (org.eclipse.kura.net.wifi.WifiRadioMode)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1