1 /****************************************************************************
2 ** ui.h extension file, included from the uic-generated form implementation.
4 ** If you want to add, delete, or rename functions or slots, use
5 ** Qt Designer to update this file, preserving your code.
7 ** You should not define a constructor or destructor in this file.
8 ** Instead, write your code in functions called init() and destroy().
9 ** These will automatically be called by the form's constructor and
11 *****************************************************************************/
15 /* Need to get getopt() */
29 ctrl_iface_dir = strdup("/var/run/wpa_supplicant");
33 textStatus->setText("connecting to wpa_supplicant");
34 timer = new QTimer(this);
35 connect(timer, SIGNAL(timeout()), SLOT(ping()));
36 timer->start(1000, FALSE);
38 if (openCtrlConnection(ctrl_iface) < 0) {
39 printf("Failed to open control connection to wpa_supplicant.\n");
43 networkMayHaveChanged = true;
48 void WpaGui::destroy()
53 wpa_ctrl_detach(monitor_conn);
54 wpa_ctrl_close(monitor_conn);
58 wpa_ctrl_close(ctrl_conn);
84 ctrl_iface_dir = NULL;
88 void WpaGui::parse_argv()
92 c = getopt(qApp->argc(), qApp->argv(), "i:p:");
98 ctrl_iface = strdup(optarg);
101 free(ctrl_iface_dir);
102 ctrl_iface_dir = strdup(optarg);
109 int WpaGui::openCtrlConnection(const char *ifname)
115 if (ifname != ctrl_iface) {
117 ctrl_iface = strdup(ifname);
120 #ifdef CONFIG_CTRL_IFACE_UDP
122 ctrl_iface = strdup("udp");
123 #else /* CONFIG_CTRL_IFACE_UDP */
125 DIR *dir = opendir(ctrl_iface_dir);
129 while ((dent = readdir(dir))) {
130 if (strcmp(dent->d_name, ".") == 0 ||
131 strcmp(dent->d_name, "..") == 0)
133 printf("Selected interface '%s'\n", dent->d_name);
134 ctrl_iface = strdup(dent->d_name);
139 #endif /* CONFIG_CTRL_IFACE_UDP */
142 if (ctrl_iface == NULL)
145 flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2;
146 cfile = (char *) malloc(flen);
149 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface);
152 wpa_ctrl_close(ctrl_conn);
159 wpa_ctrl_detach(monitor_conn);
160 wpa_ctrl_close(monitor_conn);
164 printf("Trying to connect to '%s'\n", cfile);
165 ctrl_conn = wpa_ctrl_open(cfile);
166 if (ctrl_conn == NULL) {
170 monitor_conn = wpa_ctrl_open(cfile);
172 if (monitor_conn == NULL) {
173 wpa_ctrl_close(ctrl_conn);
176 if (wpa_ctrl_attach(monitor_conn)) {
177 printf("Failed to attach to wpa_supplicant\n");
178 wpa_ctrl_close(monitor_conn);
180 wpa_ctrl_close(ctrl_conn);
185 msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
186 QSocketNotifier::Read, this);
187 connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));
189 adapterSelect->clear();
190 adapterSelect->insertItem(ctrl_iface);
191 adapterSelect->setCurrentItem(0);
197 static void wpa_gui_msg_cb(char *msg, size_t)
199 /* This should not happen anymore since two control connections are used. */
200 printf("missed message: %s\n", msg);
204 int WpaGui::ctrlRequest(const char *cmd, char *buf, size_t *buflen)
208 if (ctrl_conn == NULL)
210 ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, buflen,
213 printf("'%s' command timed out.\n", cmd);
214 } else if (ret < 0) {
215 printf("'%s' command failed.\n", cmd);
222 void WpaGui::updateStatus()
224 char buf[2048], *start, *end, *pos;
227 pingsToStatusUpdate = 10;
229 len = sizeof(buf) - 1;
230 if (ctrl_conn == NULL || ctrlRequest("STATUS", buf, &len) < 0) {
231 textStatus->setText("Could not get status from wpa_supplicant");
232 textAuthentication->clear();
233 textEncryption->clear();
236 textIpAddress->clear();
242 bool auth_updated = false, ssid_updated = false;
243 bool bssid_updated = false, ipaddr_updated = false;
244 bool status_updated = false;
245 char *pairwise_cipher = NULL, *group_cipher = NULL;
250 end = strchr(start, '\n');
254 while (end[0] && end[1])
259 pos = strchr(start, '=');
262 if (strcmp(start, "bssid") == 0) {
263 bssid_updated = true;
264 textBssid->setText(pos);
265 } else if (strcmp(start, "ssid") == 0) {
267 textSsid->setText(pos);
268 } else if (strcmp(start, "ip_address") == 0) {
269 ipaddr_updated = true;
270 textIpAddress->setText(pos);
271 } else if (strcmp(start, "wpa_state") == 0) {
272 status_updated = true;
273 textStatus->setText(pos);
274 } else if (strcmp(start, "key_mgmt") == 0) {
276 textAuthentication->setText(pos);
277 /* TODO: could add EAP status to this */
278 } else if (strcmp(start, "pairwise_cipher") == 0) {
279 pairwise_cipher = pos;
280 } else if (strcmp(start, "group_cipher") == 0) {
290 if (pairwise_cipher || group_cipher) {
292 if (pairwise_cipher && group_cipher &&
293 strcmp(pairwise_cipher, group_cipher) != 0) {
294 encr.append(pairwise_cipher);
296 encr.append(group_cipher);
297 } else if (pairwise_cipher) {
298 encr.append(pairwise_cipher);
299 } else if (group_cipher) {
300 encr.append(group_cipher);
301 encr.append(" [group key only]");
305 textEncryption->setText(encr);
307 textEncryption->clear();
312 textAuthentication->clear();
318 textIpAddress->clear();
322 void WpaGui::updateNetworks()
324 char buf[2048], *start, *end, *id, *ssid, *bssid, *flags;
326 int first_active = -1;
327 bool selected = false;
329 if (!networkMayHaveChanged)
332 networkSelect->clear();
334 if (ctrl_conn == NULL)
337 len = sizeof(buf) - 1;
338 if (ctrlRequest("LIST_NETWORKS", buf, &len) < 0)
342 start = strchr(buf, '\n');
349 end = strchr(start, '\n');
353 while (end[0] && end[1])
359 ssid = strchr(id, '\t');
363 bssid = strchr(ssid, '\t');
367 flags = strchr(bssid, '\t');
373 network.append(": ");
374 network.append(ssid);
375 networkSelect->insertItem(network);
377 if (strstr(flags, "[CURRENT]")) {
378 networkSelect->setCurrentItem(networkSelect->count() - 1);
380 } else if (first_active < 0 && strstr(flags, "[DISABLED]") == NULL)
381 first_active = networkSelect->count() - 1;
388 if (!selected && first_active >= 0)
389 networkSelect->setCurrentItem(first_active);
391 networkMayHaveChanged = false;
395 void WpaGui::helpIndex()
397 printf("helpIndex\n");
401 void WpaGui::helpContents()
403 printf("helpContents\n");
407 void WpaGui::helpAbout()
409 QMessageBox::about(this, "wpa_gui for wpa_supplicant",
410 "Copyright (c) 2003-2005,\n"
411 "Jouni Malinen <jkmaline@cc.hut.fi>\n"
412 "and contributors.\n"
414 "This program is free software. You can\n"
415 "distribute it and/or modify it under the terms of\n"
416 "the GNU General Public License version 2.\n"
418 "Alternatively, this software may be distributed\n"
419 "under the terms of the BSD license.\n"
421 "This product includes software developed\n"
422 "by the OpenSSL Project for use in the\n"
423 "OpenSSL Toolkit (http://www.openssl.org/)\n");
427 void WpaGui::disconnect()
430 size_t reply_len = sizeof(reply);
431 ctrlRequest("DISCONNECT", reply, &reply_len);
442 scanres = new ScanResults();
445 scanres->setWpaGui(this);
451 void WpaGui::eventHistory()
458 eh = new EventHistory();
472 if (scanres && !scanres->isVisible()) {
477 if (eh && !eh->isVisible()) {
482 if (udr && !udr->isVisible()) {
487 len = sizeof(buf) - 1;
488 if (ctrlRequest("PING", buf, &len) < 0) {
489 printf("PING failed - trying to reconnect\n");
490 if (openCtrlConnection(ctrl_iface) >= 0) {
491 printf("Reconnected successfully\n");
492 pingsToStatusUpdate = 0;
496 pingsToStatusUpdate--;
497 if (pingsToStatusUpdate <= 0) {
504 static int str_match(const char *a, const char *b)
506 return strncmp(a, b, strlen(b)) == 0;
510 void WpaGui::processMsg(char *msg)
512 char *pos = msg, *pos2;
518 priority = atoi(pos);
519 pos = strchr(pos, '>');
526 WpaMsg wm(pos, priority);
530 while (msgs.count() > 100)
533 /* Update last message with truncated version of the event */
534 if (strncmp(pos, "CTRL-", 5) == 0) {
535 pos2 = strchr(pos, str_match(pos, WPA_CTRL_REQ) ? ':' : ' ');
542 QString lastmsg = pos2;
543 lastmsg.truncate(40);
544 textLastMessage->setText(lastmsg);
546 pingsToStatusUpdate = 0;
547 networkMayHaveChanged = true;
549 if (str_match(pos, WPA_CTRL_REQ))
550 processCtrlReq(pos + strlen(WPA_CTRL_REQ));
554 void WpaGui::processCtrlReq(const char *req)
560 udr = new UserDataRequest();
563 if (udr->setParams(this, req) < 0) {
573 void WpaGui::receiveMsgs()
578 while (wpa_ctrl_pending(monitor_conn)) {
579 len = sizeof(buf) - 1;
580 if (wpa_ctrl_recv(monitor_conn, buf, &len) == 0) {
588 void WpaGui::connectB()
591 size_t reply_len = sizeof(reply);
592 ctrlRequest("REASSOCIATE", reply, &reply_len);
596 void WpaGui::selectNetwork( const QString &sel )
600 size_t reply_len = sizeof(reply);
602 int pos = cmd.find(':');
604 printf("Invalid selectNetwork '%s'\n", cmd.ascii());
608 cmd.prepend("SELECT_NETWORK ");
609 ctrlRequest(cmd.ascii(), reply, &reply_len);
613 void WpaGui::editNetwork()
615 QString sel(networkSelect->currentText());
616 int pos = sel.find(':');
618 printf("Invalid selectNetwork '%s'\n", sel.ascii());
623 NetworkConfig *nc = new NetworkConfig();
628 nc->paramsFromConfig(sel.toInt());
634 void WpaGui::triggerUpdate()
637 networkMayHaveChanged = true;
642 void WpaGui::addNetwork()
644 NetworkConfig *nc = new NetworkConfig();