Search in sources :

Example 1 with WifiHotspotInfo

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

the class WpaSupplicantScan method scan.

public void scan() throws KuraException {
    String line = null;
    ProcessStats processStats = null;
    BufferedReader br = null;
    String sScanCommand = formSupplicantScanCommand(this.m_iface);
    // scan for wireless networks
    try {
        processStats = LinuxProcessUtil.startWithStats(sScanCommand);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    try {
        br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
        line = br.readLine();
        if (line == null || !line.equals("OK")) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, sScanCommand + " command failed");
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        try {
            br.close();
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    // get scan results
    String sScanResultsCommand = formSupplicantScanResultsCommand(this.m_iface);
    try {
        processStats = LinuxProcessUtil.startWithStats(sScanResultsCommand);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    try {
        br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
        String[] aScanInfo = null;
        while ((line = br.readLine()) != null) {
            aScanInfo = line.split("\\s+");
            if (aScanInfo.length > 0) {
                String macAddress = aScanInfo[0];
                int frequency = Integer.parseInt(aScanInfo[1]);
                int signalLevel = Integer.parseInt(aScanInfo[2]);
                int securityCode = 0;
                String sSecurity = aScanInfo[3].substring(aScanInfo[3].indexOf("[") + 1, aScanInfo[3].lastIndexOf(']'));
                StringTokenizer st = new StringTokenizer(sSecurity, "][");
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if (token.startsWith("WEP")) {
                        securityCode |= 1;
                    } else if (token.startsWith("WPA2")) {
                        securityCode |= 4;
                    } else if (token.startsWith("WPA")) {
                        securityCode |= 2;
                    }
                }
                WifiSecurity wifiSecurity = null;
                switch(securityCode) {
                    case 1:
                        wifiSecurity = WifiSecurity.SECURITY_WEP;
                        break;
                    case 2:
                        wifiSecurity = WifiSecurity.SECURITY_WPA;
                        break;
                    case 4:
                        wifiSecurity = WifiSecurity.SECURITY_WPA2;
                        break;
                    case 6:
                        wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                        break;
                    default:
                        wifiSecurity = WifiSecurity.NONE;
                }
                String ssid = aScanInfo[4];
                WifiHotspotInfo wifiHotspotInfo = new WifiHotspotInfo(ssid, macAddress, signalLevel, frequencyMhz2Channel(frequency), frequency, wifiSecurity);
                this.m_listWifiHotspotInfo.add(wifiHotspotInfo);
            }
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        try {
            br.close();
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
}
Also used : ProcessStats(org.eclipse.kura.core.linux.util.ProcessStats) StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) KuraException(org.eclipse.kura.KuraException) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 2 with WifiHotspotInfo

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

the class NL80211 method getScanResults.

public synchronized Map<String, WifiHotspotInfo> getScanResults() {
    Map<String, WifiHotspotInfo> scanResults = null;
    String[] BSSIDs = NL80211getScanResults(this.m_ifaceName);
    if (BSSIDs != null && BSSIDs.length > 0) {
        scanResults = new HashMap<String, WifiHotspotInfo>();
        for (String bssid : BSSIDs) {
            String ssid = NL80211getSSID(bssid);
            int channel = NL80211getChannel(bssid);
            int frequency = NL80211getFrequency(bssid);
            int signal = NL80211getSignal(bssid);
            int security = NL80211getSecurity(bssid);
            WifiSecurity wifiSecurity = WifiSecurity.NONE;
            switch(security) {
                case 0:
                    wifiSecurity = WifiSecurity.NONE;
                    break;
                case 1:
                    wifiSecurity = WifiSecurity.SECURITY_WEP;
                    break;
                case 2:
                    wifiSecurity = WifiSecurity.SECURITY_WPA;
                    break;
                case 4:
                    wifiSecurity = WifiSecurity.SECURITY_WPA2;
                    break;
                case 6:
                    wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                    break;
            }
            WifiHotspotInfo wifiHotspotInfo = new WifiHotspotInfo(ssid, bssid, signal, channel, frequency, wifiSecurity);
            scanResults.put(bssid, wifiHotspotInfo);
        }
    }
    return scanResults;
}
Also used : WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 3 with WifiHotspotInfo

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

the class GwtNetworkServiceImpl method findWifiHotspots.

@Override
public ArrayList<GwtWifiHotspotEntry> findWifiHotspots(GwtXSRFToken xsrfToken, String interfaceName) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    SystemService systemService = ServiceLocator.getInstance().getService(SystemService.class);
    List<GwtWifiHotspotEntry> gwtWifiHotspotsEntries = new ArrayList<GwtWifiHotspotEntry>();
    try {
        Map<String, WifiHotspotInfo> wifiHotspotInfoMap = nas.getWifiHotspots(interfaceName);
        if (wifiHotspotInfoMap != null && !wifiHotspotInfoMap.isEmpty()) {
            Collection<WifiHotspotInfo> wifiHotspotInfoCollection = wifiHotspotInfoMap.values();
            Iterator<WifiHotspotInfo> it = wifiHotspotInfoCollection.iterator();
            while (it.hasNext()) {
                WifiHotspotInfo wifiHotspotInfo = it.next();
                String ssid = GwtSafeHtmlUtils.htmlEscape(wifiHotspotInfo.getSsid());
                // }
                if (wifiHotspotInfo.getChannel() <= systemService.getKuraWifiTopChannel() && ssid != null) {
                    GwtWifiHotspotEntry gwtWifiHotspotEntry = new GwtWifiHotspotEntry();
                    gwtWifiHotspotEntry.setMacAddress(wifiHotspotInfo.getMacAddress());
                    gwtWifiHotspotEntry.setSSID(ssid);
                    gwtWifiHotspotEntry.setsignalStrength(wifiHotspotInfo.getSignalLevel());
                    gwtWifiHotspotEntry.setChannel(wifiHotspotInfo.getChannel());
                    gwtWifiHotspotEntry.setFrequency(wifiHotspotInfo.getFrequency());
                    if (wifiHotspotInfo.getSecurity() == WifiSecurity.NONE || wifiHotspotInfo.getSecurity() == WifiSecurity.SECURITY_NONE) {
                        gwtWifiHotspotEntry.setSecurity("None");
                    } else if (wifiHotspotInfo.getSecurity() == WifiSecurity.SECURITY_WEP) {
                        gwtWifiHotspotEntry.setSecurity("WEP");
                    } else if (wifiHotspotInfo.getSecurity() == WifiSecurity.SECURITY_WPA) {
                        gwtWifiHotspotEntry.setSecurity("WPA");
                    } else if (wifiHotspotInfo.getSecurity() == WifiSecurity.SECURITY_WPA2) {
                        gwtWifiHotspotEntry.setSecurity("WPA2");
                    } else if (wifiHotspotInfo.getSecurity() == WifiSecurity.SECURITY_WPA_WPA2) {
                        gwtWifiHotspotEntry.setSecurity("WPA/WPA2");
                    }
                    GwtWifiCiphers gwtPairCiphers = null;
                    GwtWifiCiphers gwtGroupCiphers = null;
                    EnumSet<WifiSecurity> pairCiphers = wifiHotspotInfo.getPairCiphers();
                    Iterator<WifiSecurity> itPairCiphers = pairCiphers.iterator();
                    while (itPairCiphers.hasNext()) {
                        WifiSecurity cipher = itPairCiphers.next();
                        if (gwtPairCiphers == null) {
                            if (cipher == WifiSecurity.PAIR_TKIP) {
                                gwtPairCiphers = GwtWifiCiphers.netWifiCiphers_TKIP;
                            } else if (cipher == WifiSecurity.PAIR_CCMP) {
                                gwtPairCiphers = GwtWifiCiphers.netWifiCiphers_CCMP;
                            }
                        } else if (gwtPairCiphers == GwtWifiCiphers.netWifiCiphers_TKIP) {
                            if (cipher == WifiSecurity.PAIR_CCMP) {
                                gwtPairCiphers = GwtWifiCiphers.netWifiCiphers_CCMP_TKIP;
                            }
                        } else if (gwtPairCiphers == GwtWifiCiphers.netWifiCiphers_CCMP) {
                            if (cipher == WifiSecurity.PAIR_TKIP) {
                                gwtPairCiphers = GwtWifiCiphers.netWifiCiphers_CCMP_TKIP;
                            }
                        }
                    }
                    EnumSet<WifiSecurity> groupCiphers = wifiHotspotInfo.getGroupCiphers();
                    Iterator<WifiSecurity> itGroupCiphers = groupCiphers.iterator();
                    while (itGroupCiphers.hasNext()) {
                        WifiSecurity cipher = itGroupCiphers.next();
                        if (gwtGroupCiphers == null) {
                            if (cipher == WifiSecurity.GROUP_TKIP) {
                                gwtGroupCiphers = GwtWifiCiphers.netWifiCiphers_TKIP;
                            } else if (cipher == WifiSecurity.GROUP_CCMP) {
                                gwtGroupCiphers = GwtWifiCiphers.netWifiCiphers_CCMP;
                            }
                        } else if (gwtGroupCiphers == GwtWifiCiphers.netWifiCiphers_TKIP) {
                            if (cipher == WifiSecurity.GROUP_CCMP) {
                                gwtGroupCiphers = GwtWifiCiphers.netWifiCiphers_CCMP_TKIP;
                            }
                        } else if (gwtGroupCiphers == GwtWifiCiphers.netWifiCiphers_CCMP) {
                            if (cipher == WifiSecurity.GROUP_TKIP) {
                                gwtGroupCiphers = GwtWifiCiphers.netWifiCiphers_CCMP_TKIP;
                            }
                        }
                    }
                    if (gwtPairCiphers != null) {
                        gwtWifiHotspotEntry.setPairwiseCiphers(gwtPairCiphers.name());
                    }
                    if (gwtGroupCiphers != null) {
                        gwtWifiHotspotEntry.setGroupCiphers(gwtGroupCiphers.name());
                    }
                    gwtWifiHotspotsEntries.add(gwtWifiHotspotEntry);
                }
            }
        }
    } catch (Throwable t) {
        s_logger.error("Failed", t);
        KuraExceptionHandler.handle(t);
    }
    return new ArrayList<GwtWifiHotspotEntry>(gwtWifiHotspotsEntries);
}
Also used : GwtWifiCiphers(org.eclipse.kura.web.shared.model.GwtWifiCiphers) ArrayList(java.util.ArrayList) SystemService(org.eclipse.kura.system.SystemService) GwtWifiSecurity(org.eclipse.kura.web.shared.model.GwtWifiSecurity) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) GwtWifiHotspotEntry(org.eclipse.kura.web.shared.model.GwtWifiHotspotEntry) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 4 with WifiHotspotInfo

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

Aggregations

WifiHotspotInfo (org.eclipse.kura.net.wifi.WifiHotspotInfo)4 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)4 KuraException (org.eclipse.kura.KuraException)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 StringTokenizer (java.util.StringTokenizer)1 ProcessStats (org.eclipse.kura.core.linux.util.ProcessStats)1 IScanTool (org.eclipse.kura.linux.net.util.IScanTool)1 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)1 NetworkAdminService (org.eclipse.kura.net.NetworkAdminService)1 WpaSupplicantConfigWriter (org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter)1 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)1 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)1 WifiMode (org.eclipse.kura.net.wifi.WifiMode)1 SystemService (org.eclipse.kura.system.SystemService)1 GwtWifiCiphers (org.eclipse.kura.web.shared.model.GwtWifiCiphers)1 GwtWifiHotspotEntry (org.eclipse.kura.web.shared.model.GwtWifiHotspotEntry)1