]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/wpa_supplicant/wpa_gui/wpagui.ui.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / wpa_supplicant / wpa_gui / wpagui.ui.h
1 /****************************************************************************
2 ** ui.h extension file, included from the uic-generated form implementation.
3 **
4 ** If you want to add, delete, or rename functions or slots, use
5 ** Qt Designer to update this file, preserving your code.
6 **
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
10 ** destructor.
11 *****************************************************************************/
12
13
14 #ifdef __MINGW32__
15 /* Need to get getopt() */
16 #include <unistd.h>
17 #endif
18
19
20 void WpaGui::init()
21 {
22     eh = NULL;
23     scanres = NULL;
24     udr = NULL;
25     ctrl_iface = NULL;
26     ctrl_conn = NULL;
27     monitor_conn = NULL;
28     msgNotifier = NULL;
29     ctrl_iface_dir = strdup("/var/run/wpa_supplicant");
30     
31     parse_argv();
32
33     textStatus->setText("connecting to wpa_supplicant");
34     timer = new QTimer(this);
35     connect(timer, SIGNAL(timeout()), SLOT(ping()));
36     timer->start(1000, FALSE);
37     
38     if (openCtrlConnection(ctrl_iface) < 0) {
39         printf("Failed to open control connection to wpa_supplicant.\n");
40     }
41     
42     updateStatus();
43     networkMayHaveChanged = true;
44     updateNetworks();
45 }
46
47
48 void WpaGui::destroy()
49 {
50     delete msgNotifier;
51
52     if (monitor_conn) {
53         wpa_ctrl_detach(monitor_conn);
54         wpa_ctrl_close(monitor_conn);
55         monitor_conn = NULL;
56     }
57     if (ctrl_conn) {
58         wpa_ctrl_close(ctrl_conn);
59         ctrl_conn = NULL;
60     }
61     
62     if (eh) {
63         eh->close();
64         delete eh;
65         eh = NULL;
66     }
67     
68     if (scanres) {
69         scanres->close();
70         delete scanres;
71         scanres = NULL;
72     }
73     
74     if (udr) {
75         udr->close();
76         delete udr;
77         udr = NULL;
78     }
79     
80     free(ctrl_iface);
81     ctrl_iface = NULL;
82     
83     free(ctrl_iface_dir);
84     ctrl_iface_dir = NULL;
85 }
86
87
88 void WpaGui::parse_argv()
89 {
90     int c;
91     for (;;) {
92         c = getopt(qApp->argc(), qApp->argv(), "i:p:");
93         if (c < 0)
94             break;
95         switch (c) {
96         case 'i':
97             free(ctrl_iface);
98             ctrl_iface = strdup(optarg);
99             break;
100         case 'p':
101             free(ctrl_iface_dir);
102             ctrl_iface_dir = strdup(optarg);
103             break;
104         }
105     }
106 }
107
108
109 int WpaGui::openCtrlConnection(const char *ifname)
110 {
111     char *cfile;
112     int flen;
113     char buf[2048], *pos, *pos2;
114     size_t len;
115
116     if (ifname) {
117         if (ifname != ctrl_iface) {
118             free(ctrl_iface);
119             ctrl_iface = strdup(ifname);
120         }
121     } else {
122 #ifdef CONFIG_CTRL_IFACE_UDP
123         free(ctrl_iface);
124         ctrl_iface = strdup("udp");
125 #endif /* CONFIG_CTRL_IFACE_UDP */
126 #ifdef CONFIG_CTRL_IFACE_UNIX
127         struct dirent *dent;
128         DIR *dir = opendir(ctrl_iface_dir);
129         free(ctrl_iface);
130         ctrl_iface = NULL;
131         if (dir) {
132             while ((dent = readdir(dir))) {
133 #ifdef _DIRENT_HAVE_D_TYPE
134                 /* Skip the file if it is not a socket.
135                  * Also accept DT_UNKNOWN (0) in case
136                  * the C library or underlying file
137                  * system does not support d_type. */
138                 if (dent->d_type != DT_SOCK &&
139                     dent->d_type != DT_UNKNOWN)
140                     continue;
141 #endif /* _DIRENT_HAVE_D_TYPE */
142
143                 if (strcmp(dent->d_name, ".") == 0 ||
144                     strcmp(dent->d_name, "..") == 0)
145                     continue;
146                 printf("Selected interface '%s'\n", dent->d_name);
147                 ctrl_iface = strdup(dent->d_name);
148                 break;
149             }
150             closedir(dir);
151         }
152 #endif /* CONFIG_CTRL_IFACE_UNIX */
153 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
154         struct wpa_ctrl *ctrl;
155         int ret;
156
157         free(ctrl_iface);
158         ctrl_iface = NULL;
159
160         ctrl = wpa_ctrl_open(NULL);
161         if (ctrl) {
162             len = sizeof(buf) - 1;
163             ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
164             if (ret >= 0) {
165                 buf[len] = '\0';
166                 pos = strchr(buf, '\n');
167                 if (pos)
168                     *pos = '\0';
169                 ctrl_iface = strdup(buf);
170             }
171             wpa_ctrl_close(ctrl);
172         }
173 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
174     }
175     
176     if (ctrl_iface == NULL)
177         return -1;
178
179 #ifdef CONFIG_CTRL_IFACE_UNIX
180     flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2;
181     cfile = (char *) malloc(flen);
182     if (cfile == NULL)
183         return -1;
184     snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface);
185 #else /* CONFIG_CTRL_IFACE_UNIX */
186     flen = strlen(ctrl_iface) + 1;
187     cfile = (char *) malloc(flen);
188     if (cfile == NULL)
189         return -1;
190     snprintf(cfile, flen, "%s", ctrl_iface);
191 #endif /* CONFIG_CTRL_IFACE_UNIX */
192
193     if (ctrl_conn) {
194         wpa_ctrl_close(ctrl_conn);
195         ctrl_conn = NULL;
196     }
197
198     if (monitor_conn) {
199         delete msgNotifier;
200         msgNotifier = NULL;
201         wpa_ctrl_detach(monitor_conn);
202         wpa_ctrl_close(monitor_conn);
203         monitor_conn = NULL;
204     }
205
206     printf("Trying to connect to '%s'\n", cfile);
207     ctrl_conn = wpa_ctrl_open(cfile);
208     if (ctrl_conn == NULL) {
209         free(cfile);
210         return -1;
211     }
212     monitor_conn = wpa_ctrl_open(cfile);
213     free(cfile);
214     if (monitor_conn == NULL) {
215         wpa_ctrl_close(ctrl_conn);
216         return -1;
217     }
218     if (wpa_ctrl_attach(monitor_conn)) {
219         printf("Failed to attach to wpa_supplicant\n");
220         wpa_ctrl_close(monitor_conn);
221         monitor_conn = NULL;
222         wpa_ctrl_close(ctrl_conn);
223         ctrl_conn = NULL;
224         return -1;
225     }
226
227 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
228     msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
229                                       QSocketNotifier::Read, this);
230     connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));
231 #endif
232
233     adapterSelect->clear();
234     adapterSelect->insertItem(ctrl_iface);
235     adapterSelect->setCurrentItem(0);
236
237     len = sizeof(buf) - 1;
238     if (wpa_ctrl_request(ctrl_conn, "INTERFACES", 10, buf, &len, NULL) >= 0) {
239         buf[len] = '\0';
240         pos = buf;
241         while (*pos) {
242                 pos2 = strchr(pos, '\n');
243                 if (pos2)
244                         *pos2 = '\0';
245                 if (strcmp(pos, ctrl_iface) != 0)
246                         adapterSelect->insertItem(pos);
247                 if (pos2)
248                         pos = pos2 + 1;
249                 else
250                         break;
251         }
252     }
253
254     return 0;
255 }
256
257
258 static void wpa_gui_msg_cb(char *msg, size_t)
259 {
260     /* This should not happen anymore since two control connections are used. */
261     printf("missed message: %s\n", msg);
262 }
263
264
265 int WpaGui::ctrlRequest(const char *cmd, char *buf, size_t *buflen)
266 {
267     int ret;
268     
269     if (ctrl_conn == NULL)
270         return -3;
271     ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, buflen,
272                            wpa_gui_msg_cb);
273     if (ret == -2) {
274         printf("'%s' command timed out.\n", cmd);
275     } else if (ret < 0) {
276         printf("'%s' command failed.\n", cmd);
277     }
278     
279     return ret;
280 }
281
282
283 void WpaGui::updateStatus()
284 {
285     char buf[2048], *start, *end, *pos;
286     size_t len;
287
288     pingsToStatusUpdate = 10;
289
290     len = sizeof(buf) - 1;
291     if (ctrl_conn == NULL || ctrlRequest("STATUS", buf, &len) < 0) {
292         textStatus->setText("Could not get status from wpa_supplicant");
293         textAuthentication->clear();
294         textEncryption->clear();
295         textSsid->clear();
296         textBssid->clear();
297         textIpAddress->clear();
298         return;
299     }
300     
301     buf[len] = '\0';
302     
303     bool auth_updated = false, ssid_updated = false;
304     bool bssid_updated = false, ipaddr_updated = false;
305     bool status_updated = false;
306     char *pairwise_cipher = NULL, *group_cipher = NULL;
307     
308     start = buf;
309     while (*start) {
310         bool last = false;
311         end = strchr(start, '\n');
312         if (end == NULL) {
313             last = true;
314             end = start;
315             while (end[0] && end[1])
316                 end++;
317         }
318         *end = '\0';
319         
320         pos = strchr(start, '=');
321         if (pos) {
322             *pos++ = '\0';
323             if (strcmp(start, "bssid") == 0) {
324                 bssid_updated = true;
325                 textBssid->setText(pos);
326             } else if (strcmp(start, "ssid") == 0) {
327                 ssid_updated = true;
328                 textSsid->setText(pos);
329             } else if (strcmp(start, "ip_address") == 0) {
330                 ipaddr_updated = true;
331                 textIpAddress->setText(pos);
332             } else if (strcmp(start, "wpa_state") == 0) {
333                 status_updated = true;
334                 textStatus->setText(pos);
335             } else if (strcmp(start, "key_mgmt") == 0) {
336                 auth_updated = true;
337                 textAuthentication->setText(pos);
338                 /* TODO: could add EAP status to this */
339             } else if (strcmp(start, "pairwise_cipher") == 0) {
340                 pairwise_cipher = pos;
341             } else if (strcmp(start, "group_cipher") == 0) {
342                 group_cipher = pos;
343             }
344         }
345         
346         if (last)
347             break;
348         start = end + 1;
349     }
350     
351     if (pairwise_cipher || group_cipher) {
352         QString encr;
353         if (pairwise_cipher && group_cipher &&
354             strcmp(pairwise_cipher, group_cipher) != 0) {
355             encr.append(pairwise_cipher);
356             encr.append(" + ");
357             encr.append(group_cipher);
358         } else if (pairwise_cipher) {
359             encr.append(pairwise_cipher);
360         } else if (group_cipher) {
361             encr.append(group_cipher);
362             encr.append(" [group key only]");
363         } else {
364             encr.append("?");
365         }
366         textEncryption->setText(encr);
367     } else
368         textEncryption->clear();
369
370     if (!status_updated)
371         textStatus->clear();
372     if (!auth_updated)
373         textAuthentication->clear();
374     if (!ssid_updated)
375         textSsid->clear();
376     if (!bssid_updated)
377         textBssid->clear();
378     if (!ipaddr_updated)
379         textIpAddress->clear();
380 }
381
382
383 void WpaGui::updateNetworks()
384 {
385     char buf[2048], *start, *end, *id, *ssid, *bssid, *flags;
386     size_t len;
387     int first_active = -1;
388     bool selected = false;
389
390     if (!networkMayHaveChanged)
391         return;
392
393     networkSelect->clear();
394
395     if (ctrl_conn == NULL)
396         return;
397     
398     len = sizeof(buf) - 1;
399     if (ctrlRequest("LIST_NETWORKS", buf, &len) < 0)
400         return;
401     
402     buf[len] = '\0';
403     start = strchr(buf, '\n');
404     if (start == NULL)
405         return;
406     start++;
407
408     while (*start) {
409         bool last = false;
410         end = strchr(start, '\n');
411         if (end == NULL) {
412             last = true;
413             end = start;
414             while (end[0] && end[1])
415                 end++;
416         }
417         *end = '\0';
418         
419         id = start;
420         ssid = strchr(id, '\t');
421         if (ssid == NULL)
422             break;
423         *ssid++ = '\0';
424         bssid = strchr(ssid, '\t');
425         if (bssid == NULL)
426             break;
427         *bssid++ = '\0';
428         flags = strchr(bssid, '\t');
429         if (flags == NULL)
430             break;
431         *flags++ = '\0';
432         
433         QString network(id);
434         network.append(": ");
435         network.append(ssid);
436         networkSelect->insertItem(network);
437         
438         if (strstr(flags, "[CURRENT]")) {
439             networkSelect->setCurrentItem(networkSelect->count() - 1);
440             selected = true;
441         } else if (first_active < 0 && strstr(flags, "[DISABLED]") == NULL)
442             first_active = networkSelect->count() - 1;
443         
444         if (last)
445             break;
446         start = end + 1;
447     }
448
449     if (!selected && first_active >= 0)
450         networkSelect->setCurrentItem(first_active);
451
452     networkMayHaveChanged = false;
453 }
454
455
456 void WpaGui::helpIndex()
457 {
458     printf("helpIndex\n");
459 }
460
461
462 void WpaGui::helpContents()
463 {
464     printf("helpContents\n");
465 }
466
467
468 void WpaGui::helpAbout()
469 {
470     QMessageBox::about(this, "wpa_gui for wpa_supplicant",
471                        "Copyright (c) 2003-2008,\n"
472                        "Jouni Malinen <j@w1.fi>\n"
473                        "and contributors.\n"
474                        "\n"
475                        "This program is free software. You can\n"
476                        "distribute it and/or modify it under the terms of\n"
477                        "the GNU General Public License version 2.\n"
478                        "\n"
479                        "Alternatively, this software may be distributed\n"
480                        "under the terms of the BSD license.\n"
481                        "\n"
482                        "This product includes software developed\n"
483                        "by the OpenSSL Project for use in the\n"
484                        "OpenSSL Toolkit (http://www.openssl.org/)\n");
485 }
486
487
488 void WpaGui::disconnect()
489 {
490     char reply[10];
491     size_t reply_len = sizeof(reply);
492     ctrlRequest("DISCONNECT", reply, &reply_len);
493 }
494
495
496 void WpaGui::scan()
497 {
498     if (scanres) {
499         scanres->close();
500         delete scanres;
501     }
502
503     scanres = new ScanResults();
504     if (scanres == NULL)
505         return;
506     scanres->setWpaGui(this);
507     scanres->show();
508     scanres->exec();
509 }
510
511
512 void WpaGui::eventHistory()
513 {
514     if (eh) {
515         eh->close();
516         delete eh;
517     }
518
519     eh = new EventHistory();
520     if (eh == NULL)
521         return;
522     eh->addEvents(msgs);
523     eh->show();
524     eh->exec();
525 }
526
527
528 void WpaGui::ping()
529 {
530     char buf[10];
531     size_t len;
532     
533 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
534     /*
535      * QSocketNotifier cannot be used with Windows named pipes, so use a timer
536      * to check for received messages for now. This could be optimized be doing
537      * something specific to named pipes or Windows events, but it is not clear
538      * what would be the best way of doing that in Qt.
539      */
540     receiveMsgs();
541 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
542
543     if (scanres && !scanres->isVisible()) {
544         delete scanres;
545         scanres = NULL;
546     }
547     
548     if (eh && !eh->isVisible()) {
549         delete eh;
550         eh = NULL;
551     }
552     
553     if (udr && !udr->isVisible()) {
554         delete udr;
555         udr = NULL;
556     }
557     
558     len = sizeof(buf) - 1;
559     if (ctrlRequest("PING", buf, &len) < 0) {
560         printf("PING failed - trying to reconnect\n");
561         if (openCtrlConnection(ctrl_iface) >= 0) {
562             printf("Reconnected successfully\n");
563             pingsToStatusUpdate = 0;
564         }
565     }
566
567     pingsToStatusUpdate--;
568     if (pingsToStatusUpdate <= 0) {
569         updateStatus();
570         updateNetworks();
571     }
572 }
573
574
575 static int str_match(const char *a, const char *b)
576 {
577     return strncmp(a, b, strlen(b)) == 0;
578 }
579
580
581 void WpaGui::processMsg(char *msg)
582 {
583     char *pos = msg, *pos2;
584     int priority = 2;
585     
586     if (*pos == '<') {
587         /* skip priority */
588         pos++;
589         priority = atoi(pos);
590         pos = strchr(pos, '>');
591         if (pos)
592             pos++;
593         else
594             pos = msg;
595     }
596
597     WpaMsg wm(pos, priority);
598     if (eh)
599         eh->addEvent(wm);
600     msgs.append(wm);
601     while (msgs.count() > 100)
602         msgs.pop_front();
603     
604     /* Update last message with truncated version of the event */
605     if (strncmp(pos, "CTRL-", 5) == 0) {
606         pos2 = strchr(pos, str_match(pos, WPA_CTRL_REQ) ? ':' : ' ');
607         if (pos2)
608             pos2++;
609         else
610             pos2 = pos;
611     } else
612         pos2 = pos;
613     QString lastmsg = pos2;
614     lastmsg.truncate(40);
615     textLastMessage->setText(lastmsg);
616     
617     pingsToStatusUpdate = 0;
618     networkMayHaveChanged = true;
619     
620     if (str_match(pos, WPA_CTRL_REQ))
621         processCtrlReq(pos + strlen(WPA_CTRL_REQ));
622 }
623
624
625 void WpaGui::processCtrlReq(const char *req)
626 {
627     if (udr) {
628         udr->close();
629         delete udr;
630     }
631     udr = new UserDataRequest();
632     if (udr == NULL)
633         return;
634     if (udr->setParams(this, req) < 0) {
635         delete udr;
636         udr = NULL;
637         return;
638     }
639     udr->show();
640     udr->exec();
641 }
642
643
644 void WpaGui::receiveMsgs()
645 {
646     char buf[256];
647     size_t len;
648     
649     while (monitor_conn && wpa_ctrl_pending(monitor_conn) > 0) {
650         len = sizeof(buf) - 1;
651         if (wpa_ctrl_recv(monitor_conn, buf, &len) == 0) {
652             buf[len] = '\0';
653             processMsg(buf);
654         }
655     }
656 }
657
658
659 void WpaGui::connectB()
660 {
661     char reply[10];
662     size_t reply_len = sizeof(reply);
663     ctrlRequest("REASSOCIATE", reply, &reply_len);
664 }
665
666
667 void WpaGui::selectNetwork( const QString &sel )
668 {
669     QString cmd(sel);
670     char reply[10];
671     size_t reply_len = sizeof(reply);
672     
673     int pos = cmd.find(':');
674     if (pos < 0) {
675         printf("Invalid selectNetwork '%s'\n", cmd.ascii());
676         return;
677     }
678     cmd.truncate(pos);
679     cmd.prepend("SELECT_NETWORK ");
680     ctrlRequest(cmd.ascii(), reply, &reply_len);
681 }
682
683
684 void WpaGui::editNetwork()
685 {
686     QString sel(networkSelect->currentText());
687     int pos = sel.find(':');
688     if (pos < 0) {
689         printf("Invalid selectNetwork '%s'\n", sel.ascii());
690         return;
691     }
692     sel.truncate(pos);
693     
694     NetworkConfig *nc = new NetworkConfig();
695     if (nc == NULL)
696         return;
697     nc->setWpaGui(this);
698     
699     nc->paramsFromConfig(sel.toInt());
700     nc->show();
701     nc->exec();
702 }
703
704
705 void WpaGui::triggerUpdate()
706 {
707     updateStatus();
708     networkMayHaveChanged = true;
709     updateNetworks();
710 }
711
712
713 void WpaGui::addNetwork()
714 {
715     NetworkConfig *nc = new NetworkConfig();
716     if (nc == NULL)
717         return;
718     nc->setWpaGui(this);
719     nc->newNetwork();
720     nc->show();
721     nc->exec();
722 }
723
724
725 void WpaGui::selectAdapter( const QString & sel )
726 {
727     if (openCtrlConnection(sel.ascii()) < 0)
728         printf("Failed to open control connection to wpa_supplicant.\n");
729     updateStatus();
730     updateNetworks();
731 }