1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264 |
- /*
- * wpa_gui - WpaGui class
- * Copyright (c) 2005-2008, Jouni Malinen <j@w1.fi>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
- */
- #ifdef __MINGW32__
- /* Need to get getopt() */
- #include <unistd.h>
- #endif
- #include <QMessageBox>
- #include <QCloseEvent>
- #include "wpagui.h"
- #include "dirent.h"
- #include "wpa_ctrl.h"
- #include "userdatarequest.h"
- #include "networkconfig.h"
- WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
- : QMainWindow(parent)
- {
- setupUi(this);
- (void) statusBar();
- connect(fileEventHistoryAction, SIGNAL(triggered()), this,
- SLOT(eventHistory()));
- connect(fileSaveConfigAction, SIGNAL(triggered()), this,
- SLOT(saveConfig()));
- connect(fileExitAction, SIGNAL(triggered()), this, SLOT(fileExit()));
- connect(networkAddAction, SIGNAL(triggered()), this,
- SLOT(addNetwork()));
- connect(networkEditAction, SIGNAL(triggered()), this,
- SLOT(editSelectedNetwork()));
- connect(networkRemoveAction, SIGNAL(triggered()), this,
- SLOT(removeSelectedNetwork()));
- connect(networkEnableAllAction, SIGNAL(triggered()), this,
- SLOT(enableAllNetworks()));
- connect(networkDisableAllAction, SIGNAL(triggered()), this,
- SLOT(disableAllNetworks()));
- connect(networkRemoveAllAction, SIGNAL(triggered()), this,
- SLOT(removeAllNetworks()));
- connect(helpIndexAction, SIGNAL(triggered()), this, SLOT(helpIndex()));
- connect(helpContentsAction, SIGNAL(triggered()), this,
- SLOT(helpContents()));
- connect(helpAboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()));
- connect(disconnectButton, SIGNAL(clicked()), this, SLOT(disconnect()));
- connect(scanButton, SIGNAL(clicked()), this, SLOT(scan()));
- connect(connectButton, SIGNAL(clicked()), this, SLOT(connectB()));
- connect(adapterSelect, SIGNAL(activated(const QString&)), this,
- SLOT(selectAdapter(const QString&)));
- connect(networkSelect, SIGNAL(activated(const QString&)), this,
- SLOT(selectNetwork(const QString&)));
- connect(addNetworkButton, SIGNAL(clicked()), this, SLOT(addNetwork()));
- connect(editNetworkButton, SIGNAL(clicked()), this,
- SLOT(editListedNetwork()));
- connect(removeNetworkButton, SIGNAL(clicked()), this,
- SLOT(removeListedNetwork()));
- connect(networkList, SIGNAL(itemSelectionChanged()), this,
- SLOT(updateNetworkDisabledStatus()));
- connect(enableRadioButton, SIGNAL(toggled(bool)), this,
- SLOT(enableListedNetwork(bool)));
- connect(disableRadioButton, SIGNAL(toggled(bool)), this,
- SLOT(disableListedNetwork(bool)));
- connect(scanNetworkButton, SIGNAL(clicked()), this, SLOT(scan()));
- connect(networkList, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
- this, SLOT(editListedNetwork()));
- eh = NULL;
- scanres = NULL;
- udr = NULL;
- tray_icon = NULL;
- ctrl_iface = NULL;
- ctrl_conn = NULL;
- monitor_conn = NULL;
- msgNotifier = NULL;
- ctrl_iface_dir = strdup("/var/run/wpa_supplicant");
- if (QSystemTrayIcon::isSystemTrayAvailable())
- createTrayIcon();
- parse_argv();
- textStatus->setText("connecting to wpa_supplicant");
- timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), SLOT(ping()));
- timer->setSingleShot(FALSE);
- timer->start(1000);
- if (openCtrlConnection(ctrl_iface) < 0) {
- printf("Failed to open control connection to "
- "wpa_supplicant.\n");
- }
- updateStatus();
- networkMayHaveChanged = true;
- updateNetworks();
- if (tray_icon)
- tray_icon->show();
- }
- WpaGui::~WpaGui()
- {
- delete msgNotifier;
- if (monitor_conn) {
- wpa_ctrl_detach(monitor_conn);
- wpa_ctrl_close(monitor_conn);
- monitor_conn = NULL;
- }
- if (ctrl_conn) {
- wpa_ctrl_close(ctrl_conn);
- ctrl_conn = NULL;
- }
- if (eh) {
- eh->close();
- delete eh;
- eh = NULL;
- }
- if (scanres) {
- scanres->close();
- delete scanres;
- scanres = NULL;
- }
- if (udr) {
- udr->close();
- delete udr;
- udr = NULL;
- }
- free(ctrl_iface);
- ctrl_iface = NULL;
- free(ctrl_iface_dir);
- ctrl_iface_dir = NULL;
- }
- void WpaGui::languageChange()
- {
- retranslateUi(this);
- }
- void WpaGui::parse_argv()
- {
- int c;
- for (;;) {
- c = getopt(qApp->argc(), qApp->argv(), "i:p:");
- if (c < 0)
- break;
- switch (c) {
- case 'i':
- free(ctrl_iface);
- ctrl_iface = strdup(optarg);
- break;
- case 'p':
- free(ctrl_iface_dir);
- ctrl_iface_dir = strdup(optarg);
- break;
- }
- }
- }
- int WpaGui::openCtrlConnection(const char *ifname)
- {
- char *cfile;
- int flen;
- char buf[2048], *pos, *pos2;
- size_t len;
- if (ifname) {
- if (ifname != ctrl_iface) {
- free(ctrl_iface);
- ctrl_iface = strdup(ifname);
- }
- } else {
- #ifdef CONFIG_CTRL_IFACE_UDP
- free(ctrl_iface);
- ctrl_iface = strdup("udp");
- #endif /* CONFIG_CTRL_IFACE_UDP */
- #ifdef CONFIG_CTRL_IFACE_UNIX
- struct dirent *dent;
- DIR *dir = opendir(ctrl_iface_dir);
- free(ctrl_iface);
- ctrl_iface = NULL;
- if (dir) {
- while ((dent = readdir(dir))) {
- #ifdef _DIRENT_HAVE_D_TYPE
- /* Skip the file if it is not a socket.
- * Also accept DT_UNKNOWN (0) in case
- * the C library or underlying file
- * system does not support d_type. */
- if (dent->d_type != DT_SOCK &&
- dent->d_type != DT_UNKNOWN)
- continue;
- #endif /* _DIRENT_HAVE_D_TYPE */
- if (strcmp(dent->d_name, ".") == 0 ||
- strcmp(dent->d_name, "..") == 0)
- continue;
- printf("Selected interface '%s'\n",
- dent->d_name);
- ctrl_iface = strdup(dent->d_name);
- break;
- }
- closedir(dir);
- }
- #endif /* CONFIG_CTRL_IFACE_UNIX */
- #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
- struct wpa_ctrl *ctrl;
- int ret;
- free(ctrl_iface);
- ctrl_iface = NULL;
- ctrl = wpa_ctrl_open(NULL);
- if (ctrl) {
- len = sizeof(buf) - 1;
- ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf,
- &len, NULL);
- if (ret >= 0) {
- buf[len] = '\0';
- pos = strchr(buf, '\n');
- if (pos)
- *pos = '\0';
- ctrl_iface = strdup(buf);
- }
- wpa_ctrl_close(ctrl);
- }
- #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
- }
- if (ctrl_iface == NULL)
- return -1;
- #ifdef CONFIG_CTRL_IFACE_UNIX
- flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2;
- cfile = (char *) malloc(flen);
- if (cfile == NULL)
- return -1;
- snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface);
- #else /* CONFIG_CTRL_IFACE_UNIX */
- flen = strlen(ctrl_iface) + 1;
- cfile = (char *) malloc(flen);
- if (cfile == NULL)
- return -1;
- snprintf(cfile, flen, "%s", ctrl_iface);
- #endif /* CONFIG_CTRL_IFACE_UNIX */
- if (ctrl_conn) {
- wpa_ctrl_close(ctrl_conn);
- ctrl_conn = NULL;
- }
- if (monitor_conn) {
- delete msgNotifier;
- msgNotifier = NULL;
- wpa_ctrl_detach(monitor_conn);
- wpa_ctrl_close(monitor_conn);
- monitor_conn = NULL;
- }
- printf("Trying to connect to '%s'\n", cfile);
- ctrl_conn = wpa_ctrl_open(cfile);
- if (ctrl_conn == NULL) {
- free(cfile);
- return -1;
- }
- monitor_conn = wpa_ctrl_open(cfile);
- free(cfile);
- if (monitor_conn == NULL) {
- wpa_ctrl_close(ctrl_conn);
- return -1;
- }
- if (wpa_ctrl_attach(monitor_conn)) {
- printf("Failed to attach to wpa_supplicant\n");
- wpa_ctrl_close(monitor_conn);
- monitor_conn = NULL;
- wpa_ctrl_close(ctrl_conn);
- ctrl_conn = NULL;
- return -1;
- }
- #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
- msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
- QSocketNotifier::Read, this);
- connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));
- #endif
- adapterSelect->clear();
- adapterSelect->addItem(ctrl_iface);
- adapterSelect->setCurrentIndex(0);
- len = sizeof(buf) - 1;
- if (wpa_ctrl_request(ctrl_conn, "INTERFACES", 10, buf, &len, NULL) >=
- 0) {
- buf[len] = '\0';
- pos = buf;
- while (*pos) {
- pos2 = strchr(pos, '\n');
- if (pos2)
- *pos2 = '\0';
- if (strcmp(pos, ctrl_iface) != 0)
- adapterSelect->addItem(pos);
- if (pos2)
- pos = pos2 + 1;
- else
- break;
- }
- }
- return 0;
- }
- static void wpa_gui_msg_cb(char *msg, size_t)
- {
- /* This should not happen anymore since two control connections are
- * used. */
- printf("missed message: %s\n", msg);
- }
- int WpaGui::ctrlRequest(const char *cmd, char *buf, size_t *buflen)
- {
- int ret;
- if (ctrl_conn == NULL)
- return -3;
- ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, buflen,
- wpa_gui_msg_cb);
- if (ret == -2)
- printf("'%s' command timed out.\n", cmd);
- else if (ret < 0)
- printf("'%s' command failed.\n", cmd);
- return ret;
- }
- void WpaGui::updateStatus()
- {
- char buf[2048], *start, *end, *pos;
- size_t len;
- pingsToStatusUpdate = 10;
- len = sizeof(buf) - 1;
- if (ctrl_conn == NULL || ctrlRequest("STATUS", buf, &len) < 0) {
- textStatus->setText("Could not get status from "
- "wpa_supplicant");
- textAuthentication->clear();
- textEncryption->clear();
- textSsid->clear();
- textBssid->clear();
- textIpAddress->clear();
- return;
- }
- buf[len] = '\0';
- bool auth_updated = false, ssid_updated = false;
- bool bssid_updated = false, ipaddr_updated = false;
- bool status_updated = false;
- char *pairwise_cipher = NULL, *group_cipher = NULL;
- start = buf;
- while (*start) {
- bool last = false;
- end = strchr(start, '\n');
- if (end == NULL) {
- last = true;
- end = start;
- while (end[0] && end[1])
- end++;
- }
- *end = '\0';
- pos = strchr(start, '=');
- if (pos) {
- *pos++ = '\0';
- if (strcmp(start, "bssid") == 0) {
- bssid_updated = true;
- textBssid->setText(pos);
- } else if (strcmp(start, "ssid") == 0) {
- ssid_updated = true;
- textSsid->setText(pos);
- } else if (strcmp(start, "ip_address") == 0) {
- ipaddr_updated = true;
- textIpAddress->setText(pos);
- } else if (strcmp(start, "wpa_state") == 0) {
- status_updated = true;
- textStatus->setText(pos);
- } else if (strcmp(start, "key_mgmt") == 0) {
- auth_updated = true;
- textAuthentication->setText(pos);
- /* TODO: could add EAP status to this */
- } else if (strcmp(start, "pairwise_cipher") == 0) {
- pairwise_cipher = pos;
- } else if (strcmp(start, "group_cipher") == 0) {
- group_cipher = pos;
- }
- }
- if (last)
- break;
- start = end + 1;
- }
- if (pairwise_cipher || group_cipher) {
- QString encr;
- if (pairwise_cipher && group_cipher &&
- strcmp(pairwise_cipher, group_cipher) != 0) {
- encr.append(pairwise_cipher);
- encr.append(" + ");
- encr.append(group_cipher);
- } else if (pairwise_cipher) {
- encr.append(pairwise_cipher);
- } else {
- encr.append(group_cipher);
- encr.append(" [group key only]");
- }
- textEncryption->setText(encr);
- } else
- textEncryption->clear();
- if (!status_updated)
- textStatus->clear();
- if (!auth_updated)
- textAuthentication->clear();
- if (!ssid_updated)
- textSsid->clear();
- if (!bssid_updated)
- textBssid->clear();
- if (!ipaddr_updated)
- textIpAddress->clear();
- }
- void WpaGui::updateNetworks()
- {
- char buf[2048], *start, *end, *id, *ssid, *bssid, *flags;
- size_t len;
- int first_active = -1;
- int was_selected = -1;
- bool current = false;
- if (!networkMayHaveChanged)
- return;
- if (networkList->currentRow() >= 0)
- was_selected = networkList->currentRow();
- networkSelect->clear();
- networkList->clear();
- if (ctrl_conn == NULL)
- return;
- len = sizeof(buf) - 1;
- if (ctrlRequest("LIST_NETWORKS", buf, &len) < 0)
- return;
- buf[len] = '\0';
- start = strchr(buf, '\n');
- if (start == NULL)
- return;
- start++;
- while (*start) {
- bool last = false;
- end = strchr(start, '\n');
- if (end == NULL) {
- last = true;
- end = start;
- while (end[0] && end[1])
- end++;
- }
- *end = '\0';
- id = start;
- ssid = strchr(id, '\t');
- if (ssid == NULL)
- break;
- *ssid++ = '\0';
- bssid = strchr(ssid, '\t');
- if (bssid == NULL)
- break;
- *bssid++ = '\0';
- flags = strchr(bssid, '\t');
- if (flags == NULL)
- break;
- *flags++ = '\0';
- QString network(id);
- network.append(": ");
- network.append(ssid);
- networkSelect->addItem(network);
- networkList->addItem(network);
- if (strstr(flags, "[CURRENT]")) {
- networkSelect->setCurrentIndex(networkSelect->count() -
- 1);
- current = true;
- } else if (first_active < 0 &&
- strstr(flags, "[DISABLED]") == NULL)
- first_active = networkSelect->count() - 1;
- if (last)
- break;
- start = end + 1;
- }
- if (networkSelect->count() > 1)
- networkSelect->addItem("Select any network");
- if (!current && first_active >= 0)
- networkSelect->setCurrentIndex(first_active);
- if (was_selected >= 0 && networkList->count() > 0) {
- if (was_selected < networkList->count())
- networkList->setCurrentRow(was_selected);
- else
- networkList->setCurrentRow(networkList->count() - 1);
- }
- else
- networkList->setCurrentRow(networkSelect->currentIndex());
- networkMayHaveChanged = false;
- }
- void WpaGui::helpIndex()
- {
- printf("helpIndex\n");
- }
- void WpaGui::helpContents()
- {
- printf("helpContents\n");
- }
- void WpaGui::helpAbout()
- {
- QMessageBox::about(this, "wpa_gui for wpa_supplicant",
- "Copyright (c) 2003-2008,\n"
- "Jouni Malinen <j@w1.fi>\n"
- "and contributors.\n"
- "\n"
- "This program is free software. You can\n"
- "distribute it and/or modify it under the terms "
- "of\n"
- "the GNU General Public License version 2.\n"
- "\n"
- "Alternatively, this software may be distributed\n"
- "under the terms of the BSD license.\n"
- "\n"
- "This product includes software developed\n"
- "by the OpenSSL Project for use in the\n"
- "OpenSSL Toolkit (http://www.openssl.org/)\n");
- }
- void WpaGui::disconnect()
- {
- char reply[10];
- size_t reply_len = sizeof(reply);
- ctrlRequest("DISCONNECT", reply, &reply_len);
- }
- void WpaGui::scan()
- {
- if (scanres) {
- scanres->close();
- delete scanres;
- }
- if (!isVisible())
- show();
- scanres = new ScanResults();
- if (scanres == NULL)
- return;
- scanres->setWpaGui(this);
- scanres->show();
- scanres->exec();
- }
- void WpaGui::eventHistory()
- {
- if (eh) {
- eh->close();
- delete eh;
- }
- if (!isVisible())
- show();
- eh = new EventHistory();
- if (eh == NULL)
- return;
- eh->addEvents(msgs);
- eh->show();
- eh->exec();
- }
- void WpaGui::ping()
- {
- char buf[10];
- size_t len;
- #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
- /*
- * QSocketNotifier cannot be used with Windows named pipes, so use a
- * timer to check for received messages for now. This could be
- * optimized be doing something specific to named pipes or Windows
- * events, but it is not clear what would be the best way of doing that
- * in Qt.
- */
- receiveMsgs();
- #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
- if (scanres && !scanres->isVisible()) {
- delete scanres;
- scanres = NULL;
- }
- if (eh && !eh->isVisible()) {
- delete eh;
- eh = NULL;
- }
- if (udr && !udr->isVisible()) {
- delete udr;
- udr = NULL;
- }
- len = sizeof(buf) - 1;
- if (ctrlRequest("PING", buf, &len) < 0) {
- printf("PING failed - trying to reconnect\n");
- if (openCtrlConnection(ctrl_iface) >= 0) {
- printf("Reconnected successfully\n");
- pingsToStatusUpdate = 0;
- }
- }
- pingsToStatusUpdate--;
- if (pingsToStatusUpdate <= 0) {
- updateStatus();
- updateNetworks();
- }
- }
- static int str_match(const char *a, const char *b)
- {
- return strncmp(a, b, strlen(b)) == 0;
- }
- void WpaGui::processMsg(char *msg)
- {
- char *pos = msg, *pos2;
- int priority = 2;
- if (*pos == '<') {
- /* skip priority */
- pos++;
- priority = atoi(pos);
- pos = strchr(pos, '>');
- if (pos)
- pos++;
- else
- pos = msg;
- }
- WpaMsg wm(pos, priority);
- if (eh)
- eh->addEvent(wm);
- msgs.append(wm);
- while (msgs.count() > 100)
- msgs.pop_front();
- /* Update last message with truncated version of the event */
- if (strncmp(pos, "CTRL-", 5) == 0) {
- pos2 = strchr(pos, str_match(pos, WPA_CTRL_REQ) ? ':' : ' ');
- if (pos2)
- pos2++;
- else
- pos2 = pos;
- } else
- pos2 = pos;
- QString lastmsg = pos2;
- lastmsg.truncate(40);
- textLastMessage->setText(lastmsg);
- pingsToStatusUpdate = 0;
- networkMayHaveChanged = true;
- if (str_match(pos, WPA_CTRL_REQ))
- processCtrlReq(pos + strlen(WPA_CTRL_REQ));
- else if (str_match(pos, WPA_EVENT_SCAN_RESULTS) && scanres)
- scanres->updateResults();
- else if (str_match(pos, WPA_EVENT_DISCONNECTED))
- showTrayMessage(QSystemTrayIcon::Information, 3,
- "Disconnected from network.");
- else if (str_match(pos, WPA_EVENT_CONNECTED)) {
- showTrayMessage(QSystemTrayIcon::Information, 3,
- "Connection to network established.");
- QTimer::singleShot(5 * 1000, this, SLOT(showTrayStatus()));
- }
- }
- void WpaGui::processCtrlReq(const char *req)
- {
- if (udr) {
- udr->close();
- delete udr;
- }
- udr = new UserDataRequest();
- if (udr == NULL)
- return;
- if (udr->setParams(this, req) < 0) {
- delete udr;
- udr = NULL;
- return;
- }
- udr->show();
- udr->exec();
- }
- void WpaGui::receiveMsgs()
- {
- char buf[256];
- size_t len;
- while (monitor_conn && wpa_ctrl_pending(monitor_conn) > 0) {
- len = sizeof(buf) - 1;
- if (wpa_ctrl_recv(monitor_conn, buf, &len) == 0) {
- buf[len] = '\0';
- processMsg(buf);
- }
- }
- }
- void WpaGui::connectB()
- {
- char reply[10];
- size_t reply_len = sizeof(reply);
- ctrlRequest("REASSOCIATE", reply, &reply_len);
- }
- void WpaGui::selectNetwork( const QString &sel )
- {
- QString cmd(sel);
- char reply[10];
- size_t reply_len = sizeof(reply);
- if (cmd.startsWith("Select any")) {
- cmd = "any";
- } else {
- int pos = cmd.indexOf(':');
- if (pos < 0) {
- printf("Invalid selectNetwork '%s'\n",
- cmd.toAscii().constData());
- return;
- }
- cmd.truncate(pos);
- }
- cmd.prepend("SELECT_NETWORK ");
- ctrlRequest(cmd.toAscii().constData(), reply, &reply_len);
- triggerUpdate();
- }
- void WpaGui::enableNetwork(const QString &sel)
- {
- QString cmd(sel);
- char reply[10];
- size_t reply_len = sizeof(reply);
- if (!cmd.startsWith("all")) {
- int pos = cmd.indexOf(':');
- if (pos < 0) {
- printf("Invalid enableNetwork '%s'\n",
- cmd.toAscii().constData());
- return;
- }
- cmd.truncate(pos);
- }
- cmd.prepend("ENABLE_NETWORK ");
- ctrlRequest(cmd.toAscii().constData(), reply, &reply_len);
- triggerUpdate();
- }
- void WpaGui::disableNetwork(const QString &sel)
- {
- QString cmd(sel);
- char reply[10];
- size_t reply_len = sizeof(reply);
- if (!cmd.startsWith("all")) {
- int pos = cmd.indexOf(':');
- if (pos < 0) {
- printf("Invalid disableNetwork '%s'\n",
- cmd.toAscii().constData());
- return;
- }
- cmd.truncate(pos);
- }
- cmd.prepend("DISABLE_NETWORK ");
- ctrlRequest(cmd.toAscii().constData(), reply, &reply_len);
- triggerUpdate();
- }
- void WpaGui::editNetwork(const QString &sel)
- {
- QString cmd(sel);
- int id = -1;
- if (!cmd.startsWith("Select any")) {
- int pos = sel.indexOf(':');
- if (pos < 0) {
- printf("Invalid editNetwork '%s'\n",
- cmd.toAscii().constData());
- return;
- }
- cmd.truncate(pos);
- id = cmd.toInt();
- }
- NetworkConfig *nc = new NetworkConfig();
- if (nc == NULL)
- return;
- nc->setWpaGui(this);
- if (id >= 0)
- nc->paramsFromConfig(id);
- else
- nc->newNetwork();
- nc->show();
- nc->exec();
- }
- void WpaGui::editSelectedNetwork()
- {
- if (networkSelect->count() < 1) {
- QMessageBox::information(this, "No Networks",
- "There are no networks to edit.\n");
- return;
- }
- QString sel(networkSelect->currentText());
- editNetwork(sel);
- }
- void WpaGui::editListedNetwork()
- {
- if (networkList->currentRow() < 0) {
- QMessageBox::information(this, "Select A Network",
- "Select a network from the list to"
- " edit it.\n");
- return;
- }
- QString sel(networkList->currentItem()->text());
- editNetwork(sel);
- }
- void WpaGui::triggerUpdate()
- {
- updateStatus();
- networkMayHaveChanged = true;
- updateNetworks();
- }
- void WpaGui::addNetwork()
- {
- NetworkConfig *nc = new NetworkConfig();
- if (nc == NULL)
- return;
- nc->setWpaGui(this);
- nc->newNetwork();
- nc->show();
- nc->exec();
- }
- void WpaGui::removeNetwork(const QString &sel)
- {
- QString cmd(sel);
- char reply[10];
- size_t reply_len = sizeof(reply);
- if (cmd.startsWith("Select any"))
- return;
- if (!cmd.startsWith("all")) {
- int pos = cmd.indexOf(':');
- if (pos < 0) {
- printf("Invalid removeNetwork '%s'\n",
- cmd.toAscii().constData());
- return;
- }
- cmd.truncate(pos);
- }
- cmd.prepend("REMOVE_NETWORK ");
- ctrlRequest(cmd.toAscii().constData(), reply, &reply_len);
- triggerUpdate();
- }
- void WpaGui::removeSelectedNetwork()
- {
- if (networkSelect->count() < 1) {
- QMessageBox::information(this, "No Networks",
- "There are no networks to remove.\n");
- return;
- }
- QString sel(networkSelect->currentText());
- removeNetwork(sel);
- }
- void WpaGui::removeListedNetwork()
- {
- if (networkList->currentRow() < 0) {
- QMessageBox::information(this, "Select A Network",
- "Select a network from the list to"
- " remove it.\n");
- return;
- }
- QString sel(networkList->currentItem()->text());
- removeNetwork(sel);
- }
- void WpaGui::enableAllNetworks()
- {
- QString sel("all");
- enableNetwork(sel);
- }
- void WpaGui::disableAllNetworks()
- {
- QString sel("all");
- disableNetwork(sel);
- }
- void WpaGui::removeAllNetworks()
- {
- QString sel("all");
- removeNetwork(sel);
- }
- int WpaGui::getNetworkDisabled(const QString &sel)
- {
- QString cmd(sel);
- char reply[10];
- size_t reply_len = sizeof(reply) - 1;
- int pos = cmd.indexOf(':');
- if (pos < 0) {
- printf("Invalid getNetworkDisabled '%s'\n",
- cmd.toAscii().constData());
- return -1;
- }
- cmd.truncate(pos);
- cmd.prepend("GET_NETWORK ");
- cmd.append(" disabled");
- if (ctrlRequest(cmd.toAscii().constData(), reply, &reply_len) >= 0
- && reply_len >= 1) {
- reply[reply_len] = '\0';
- if (!str_match(reply, "FAIL"))
- return atoi(reply);
- }
- return -1;
- }
- void WpaGui::updateNetworkDisabledStatus()
- {
- if (networkList->currentRow() < 0)
- return;
- QString sel(networkList->currentItem()->text());
- switch (getNetworkDisabled(sel)) {
- case 0:
- if (!enableRadioButton->isChecked())
- enableRadioButton->setChecked(true);
- return;
- case 1:
- if (!disableRadioButton->isChecked())
- disableRadioButton->setChecked(true);
- return;
- }
- }
- void WpaGui::enableListedNetwork(bool enabled)
- {
- if (networkList->currentRow() < 0 || !enabled)
- return;
- QString sel(networkList->currentItem()->text());
- if (getNetworkDisabled(sel) == 1)
- enableNetwork(sel);
- }
- void WpaGui::disableListedNetwork(bool disabled)
- {
- if (networkList->currentRow() < 0 || !disabled)
- return;
- QString sel(networkList->currentItem()->text());
- if (getNetworkDisabled(sel) == 0)
- disableNetwork(sel);
- }
- void WpaGui::saveConfig()
- {
- char buf[10];
- size_t len;
- len = sizeof(buf) - 1;
- ctrlRequest("SAVE_CONFIG", buf, &len);
- buf[len] = '\0';
- if (str_match(buf, "FAIL"))
- QMessageBox::warning(this, "Failed to save configuration",
- "The configuration could not be saved.\n"
- "\n"
- "The update_config=1 configuration option\n"
- "must be used for configuration saving to\n"
- "be permitted.\n");
- else
- QMessageBox::information(this, "Saved configuration",
- "The current configuration was saved."
- "\n");
- }
- void WpaGui::selectAdapter( const QString & sel )
- {
- if (openCtrlConnection(sel.toAscii().constData()) < 0)
- printf("Failed to open control connection to "
- "wpa_supplicant.\n");
- updateStatus();
- updateNetworks();
- }
- void WpaGui::createTrayIcon()
- {
- tray_icon = new QSystemTrayIcon(this);
- tray_icon->setToolTip(qAppName() + " - wpa_supplicant user interface");
- tray_icon->setIcon(QIcon(":/icons/wpa_gui.svg"));
- connect(tray_icon,
- SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
- this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
- ackTrayIcon = false;
- tray_menu = new QMenu(this);
- disconnectAction = new QAction("&Disconnect", this);
- reconnectAction = new QAction("Re&connect", this);
- connect(disconnectAction, SIGNAL(triggered()), this,
- SLOT(disconnect()));
- connect(reconnectAction, SIGNAL(triggered()), this,
- SLOT(connectB()));
- tray_menu->addAction(disconnectAction);
- tray_menu->addAction(reconnectAction);
- tray_menu->addSeparator();
- eventAction = new QAction("&Event History", this);
- scanAction = new QAction("Scan &Results", this);
- statAction = new QAction("S&tatus", this);
- connect(eventAction, SIGNAL(triggered()), this, SLOT(eventHistory()));
- connect(scanAction, SIGNAL(triggered()), this, SLOT(scan()));
- connect(statAction, SIGNAL(triggered()), this, SLOT(showTrayStatus()));
- tray_menu->addAction(eventAction);
- tray_menu->addAction(scanAction);
- tray_menu->addAction(statAction);
- tray_menu->addSeparator();
- showAction = new QAction("&Show Window", this);
- hideAction = new QAction("&Hide Window", this);
- quitAction = new QAction("&Quit", this);
- connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
- connect(hideAction, SIGNAL(triggered()), this, SLOT(hide()));
- connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
- tray_menu->addAction(showAction);
- tray_menu->addAction(hideAction);
- tray_menu->addSeparator();
- tray_menu->addAction(quitAction);
- tray_icon->setContextMenu(tray_menu);
- }
- void WpaGui::showTrayMessage(QSystemTrayIcon::MessageIcon type, int sec,
- const QString & msg)
- {
- if (!QSystemTrayIcon::supportsMessages())
- return;
- if (isVisible() || !tray_icon || !tray_icon->isVisible())
- return;
- tray_icon->showMessage(qAppName(), msg, type, sec * 1000);
- }
- void WpaGui::trayActivated(QSystemTrayIcon::ActivationReason how)
- {
- switch (how) {
- /* use close() here instead of hide() and allow the
- * custom closeEvent handler take care of children */
- case QSystemTrayIcon::Trigger:
- if (isVisible())
- close();
- else
- show();
- break;
- case QSystemTrayIcon::MiddleClick:
- showTrayStatus();
- break;
- default:
- break;
- }
- }
- void WpaGui::showTrayStatus()
- {
- char buf[2048];
- size_t len;
- len = sizeof(buf) - 1;
- if (ctrlRequest("STATUS", buf, &len) < 0)
- return;
- buf[len] = '\0';
- QString msg, status(buf);
- QStringList lines = status.split(QRegExp("\\n"));
- for (QStringList::Iterator it = lines.begin();
- it != lines.end(); it++) {
- int pos = (*it).indexOf('=') + 1;
- if (pos < 1)
- continue;
- if ((*it).startsWith("bssid="))
- msg.append("BSSID:\t" + (*it).mid(pos) + "\n");
- else if ((*it).startsWith("ssid="))
- msg.append("SSID: \t" + (*it).mid(pos) + "\n");
- else if ((*it).startsWith("pairwise_cipher="))
- msg.append("PAIR: \t" + (*it).mid(pos) + "\n");
- else if ((*it).startsWith("group_cipher="))
- msg.append("GROUP:\t" + (*it).mid(pos) + "\n");
- else if ((*it).startsWith("key_mgmt="))
- msg.append("AUTH: \t" + (*it).mid(pos) + "\n");
- else if ((*it).startsWith("ip_address="))
- msg.append("IP: \t" + (*it).mid(pos) + "\n");
- }
- showTrayMessage(QSystemTrayIcon::Information, 10, msg);
- }
- void WpaGui::fileExit()
- {
- if (tray_icon)
- tray_icon->hide();
- close();
- }
- void WpaGui::closeEvent(QCloseEvent *event)
- {
- if (eh) {
- eh->close();
- delete eh;
- eh = NULL;
- }
- if (scanres) {
- scanres->close();
- delete scanres;
- scanres = NULL;
- }
- if (udr) {
- udr->close();
- delete udr;
- udr = NULL;
- }
- if (tray_icon && tray_icon->isVisible()) {
- /* give user a visual hint that the tray icon exists */
- if (QSystemTrayIcon::supportsMessages()) {
- hide();
- QTimer::singleShot(1 * 1000, this,
- SLOT(showTrayStatus()));
- } else if (!ackTrayIcon) {
- QMessageBox::information(this, qAppName() + " systray",
- "The program will keep "
- "running in the system tray."
- " To terminate the program, "
- "choose <b>Quit</b> in the "
- "context menu of the system "
- "tray icon.");
- ackTrayIcon = true;
- hide();
- }
- event->ignore();
- return;
- }
- event->accept();
- }
|