#include "WifiInterface.h" // Qt #include #include // Project #include "main.h" #include "Logger.h" #include "StorageGlobals.h" using namespace Storage; WifiInterface::WifiInterface(QObject *parent) : QObject(parent) { } void WifiInterface::onInitConnections() { connect(&_processScan, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onScanFinished(int, QProcess::ExitStatus))); connect(this, SIGNAL(didFailToConnect(const QString)), this, SLOT(onLogFailure(const QString))); } /*! * \brief WifiInterface::init * \details Initializes the class by setting the connections * \return true on first initialization, false if it has already been initialized */ bool WifiInterface::doInit() { if (_init) return false; _init = true; onInitConnections(); LOG_EVENT("UI," + tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief WifiInterface::quit * Called when the application is exiting. */ void WifiInterface::onQuit() { onQuitThread(); // verified } /*! * \brief WifiInterface::quitThread * \details Moves this object to main thread to be handled by QApplicaiton * It will also be destroyed there. */ void WifiInterface::onQuitThread() { if (!_thread ) return; moveToThread(qApp->thread()); } /*! * \brief WifiInterface::timerEvent * Built in QObject timer * \param event (QTimerEvent*) - the event timer */ void WifiInterface::timerEvent(QTimerEvent *event) { Q_UNUSED(event); } /*! * \brief WifiInterface::doScan * Scans for Wifi Access Points */ void WifiInterface::doScan() { if (_scanRunning) { LOG_DEBUG("Wifi network scan is already running."); return; } LOG_DEBUG("Scanning for Wifi Access Points..."); _processScan.setWorkingDirectory(Wifi_Scripts_Dir); _scanRunning = true; emit didScanStatusChanged(_scanRunning); _processScan.start(Wifi_Scan_For_Networks); } /*! * \brief WifiInterface::onScanFinished * Called when finished scanning for networks (success or error) * \param vPid - pid of the scan for networks process * \param vExitStatus - the status upon exit */ void WifiInterface::onScanFinished(int vPid, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("%1: %2,%3").arg(__FUNCTION__).arg(vPid).arg(vExitStatus)); QString out = _processScan.readAllStandardOutput(); QString err = _processScan.readAllStandardError(); LOG_DEBUG(out); LOG_DEBUG(err); _scanRunning = false; emit didScanStatusChanged(_scanRunning); onParseWifiScan(out); } /*! * \brief Network::onParseWifiScan * Extract desired information from the wifi scan output. Sorts by signal stength * * \param vOutput - (QString) output collected from QProcess execution */ void WifiInterface::onParseWifiScan(const QString &vOutput) { QList networks; QStringList temp = vOutput.split("Cell"); const QString signalLevelSearchTerm = "Signal level="; const QString macAddressSearchTerm = "Address:"; const QString ssidSearchTerm = "ESSID:"; const QString groupCipherSearchTerm = "Group Cipher"; const QString authSuitesSearchTerm = "Authentication Suites"; for (const QString &line : temp) { if (line.contains(ssidSearchTerm) && line.contains(macAddressSearchTerm) && line.contains(signalLevelSearchTerm)) { QString ssid = line.split(ssidSearchTerm)[1].split("\n")[0].replace("\"", "").trimmed(); if (ssid == "") continue; QString macAddress = line.split(macAddressSearchTerm)[1].split("\n")[0].trimmed(); int signalLevel = line.split(signalLevelSearchTerm)[1].split("dBm")[0].trimmed().toInt(); bool isCCMP = line.split(groupCipherSearchTerm)[1].split("\n")[0].contains("CCMP"); bool isEnterprise = line.split(authSuitesSearchTerm)[1].split("\n")[0].contains("802.1x"); bool isPersonal = line.split(authSuitesSearchTerm)[1].split("\n")[0].contains("PSK"); Network::SECURITY_LEVEL securityLevel = Network::SECURITY_LEVEL::UNSUPPORTED; if (isPersonal && isCCMP) securityLevel = Network::SECURITY_LEVEL::WPA2_AES_PERSONAL; else if (isEnterprise && isCCMP) securityLevel = Network::SECURITY_LEVEL::WPA2_AES_ENTERPRISE; // TODO: Add support for the other security levels Network network(macAddress,ssid, securityLevel, Network::STATUS::NOT_CONNECTED, signalLevel); emit didAddNetwork(network); } } } /*! * \brief WifiInterface::onLogFailure * Ensures any failures reported are logged * \param vMessage (QString) the message detail of the failure */ void WifiInterface::onLogFailure(const QString &vMessage) { LOG_DEBUG(vMessage); } /*! * \brief WifiInterface::doJoinNetwork * Handles request to join a network * \param vMacAddress - (QString) the mac address of the network to join * \param vPassword - (QString) the password for the network provided by the user */ void WifiInterface::doJoinNetwork(const Network &vNetwork, const QString &vPassword) { LOG_DEBUG(QString("Joining Network %1").arg(vNetwork.ssid())); _network = vNetwork; if (!generateWPASupplicant(_network, vPassword)) { emit didFailToConnect("Could not configure network."); return; } if (!startWPASupplicant()) { emit didFailToConnect("Could not configure network."); return; } // TODO: Add option to setup with static IP settings instead if (!requestAutoAssignedIP()) { emit didFailToConnect("Could not obtain IP Address."); return; } Network::IPSettings ipSettings; QString output = readIPSettings(); ipSettings.mIPAddress = parseIP(output); ipSettings.mBroadcast = parseBroadcast(output); ipSettings.mSubnetMask = parseSubnetMask(output); ipSettings.mGateway = readGateway(); ipSettings.mDns = readDNS(); _network.ipSettings(ipSettings); emit didConnectToNetwork(_network); } /*! * \brief WifiInterface::generateWPASupplicant * Generates the WPA Supplicant configuration file * \param vNetwork (Network) the network w/ ssid we want to connect to * \param vPassword (QString) the password the user entered for this network * \return (bool) true if writing the file completed, false otherwise */ bool WifiInterface::generateWPASupplicant(const Network &vNetwork, const QString &vPassword) { LOG_DEBUG("Generating WPA Supplicant..."); _processJoinNetwork.start(Wifi_Generate_WPA_Supplicant, QStringList() << vNetwork.ssid() << vPassword << _wpaSupplicantConfPath); return _processJoinNetwork.waitForFinished(_defaultTimeout); } /*! * \brief WifiInterface::startWPASupplicant * Starts WPA supplicant in the background. Assumes the conf file has already been written * \return true if successful, false on timeout */ bool WifiInterface::startWPASupplicant() { LOG_DEBUG("Starting wpa supplicant..."); _processJoinNetwork.start(Wifi_Start_WPA_Supplicant, QStringList() << _iface << _wpaSupplicantConfPath); return _processJoinNetwork.waitForFinished(_defaultTimeout); } /*! * \brief WifiInterface::requestAutoAssignedIP * Requests an auto-assigned IP addressed * \return true if successful, false on timeout */ bool WifiInterface::requestAutoAssignedIP() { LOG_DEBUG("Requesting auto-assigned IP address..."); _processJoinNetwork.start(Wifi_Get_Auto_Assigned_IP, QStringList() << _iface); return _processJoinNetwork.waitForFinished(_dhcpTimeout); } /*! * \brief WifiInterface::readIPSettings * Reads the IP settings of the device * \return (QString) the unparsed standard output */ QString WifiInterface::readIPSettings() { QString result = ""; _processGetIPAddress.start(Wifi_Read_IP_Settings, QStringList() << _iface); if (_processGetIPAddress.waitForFinished(_defaultTimeout)) result = _processGetIPAddress.readAllStandardOutput(); LOG_DEBUG(QString("Output with IP address information: %1").arg(result)); return result; } /*! * \brief WifiInterface::parseIP * Parses the IP from the IP address output * \param vOutput - (QString) the console output * \return (QString) the IP Address only */ QString WifiInterface::parseIP(const QString &vOutput) { if (vOutput.contains("inet addr:") && vOutput.contains("Bcast:")) return vOutput.split("inet addr:")[1].split("Bcast:")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseBroadcast * Parses the Broadcast from the IP address output * \param vOutput - (QString) the console output * \return (QString) the Broadcast IP if found, "" otherwise */ QString WifiInterface::parseBroadcast(const QString &vOutput) { if (vOutput.contains("Bcast:") && vOutput.contains("Mask:")) return vOutput.split("Bcast:")[1].split("Mask:")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseSubnetMask * Parses the subnet mask from the provided output * \param vOutput - (QString) the console output * \return (QString) the subnet mask if found, "" otherwise */ QString WifiInterface::parseSubnetMask(const QString &vOutput) { if (vOutput.contains("Mask:")) return vOutput.split("Mask:")[1].trimmed(); return ""; } /*! * \brief WifiInterface::parseGateway * Parses the gateway from the provided output * \param vOutput - (QString) the console output * \return (QString) the gateway if found, "" otherwise */ QString WifiInterface::parseGateway(const QString &vOutput) { if (vOutput.contains("default via")) return vOutput.split("default via")[1].split("dev")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseDNS * Parses the DNS from the provided output * \param vOutput - (QString) the console output * \return (QString) the first DNS found, "" otherwise */ QString WifiInterface::parseDNS(const QString &vOutput) { if (vOutput.contains("nameserver")) return vOutput.split("nameserver")[1].split("\n")[0].trimmed(); return ""; } /*! * \brief WifiInterface::readGateway * Reads the current gateway * \return (QString) the gateway if found, "" otherwise */ QString WifiInterface::readGateway() { _processGetIPAddress.start(Wifi_Read_Gateway); if (_processGetIPAddress.waitForFinished(_defaultTimeout)) return parseGateway(_processGetIPAddress.readAllStandardOutput()); return ""; } /*! * \brief WifiInterface::readDNS * Reads the DNS setting * \return (QString) the first dns found, "" otherwise */ QString WifiInterface::readDNS() { _processGetIPAddress.start(Wifi_Read_DNS); if (_processGetIPAddress.waitForFinished(_defaultTimeout)) return parseDNS(_processGetIPAddress.readAllStandardOutput()); return ""; }