]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/wpa_gui/wpagui.ui.h
This commit was generated by cvs2svn to compensate for changes in r171322,
[FreeBSD/FreeBSD.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
114     if (ifname) {
115         if (ifname != ctrl_iface) {
116             free(ctrl_iface);
117             ctrl_iface = strdup(ifname);
118         }
119     } else {
120 #ifdef CONFIG_CTRL_IFACE_UDP
121         free(ctrl_iface);
122         ctrl_iface = strdup("udp");
123 #else /* CONFIG_CTRL_IFACE_UDP */
124         struct dirent *dent;
125         DIR *dir = opendir(ctrl_iface_dir);
126         free(ctrl_iface);
127         ctrl_iface = NULL;
128         if (dir) {
129             while ((dent = readdir(dir))) {
130                 if (strcmp(dent->d_name, ".") == 0 ||
131                     strcmp(dent->d_name, "..") == 0)
132                     continue;
133                 printf("Selected interface '%s'\n", dent->d_name);
134                 ctrl_iface = strdup(dent->d_name);
135                 break;
136             }
137             closedir(dir);
138         }
139 #endif /* CONFIG_CTRL_IFACE_UDP */
140     }
141     
142     if (ctrl_iface == NULL)
143         return -1;
144
145     flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2;
146     cfile = (char *) malloc(flen);
147     if (cfile == NULL)
148         return -1;
149     snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface);
150
151     if (ctrl_conn) {
152         wpa_ctrl_close(ctrl_conn);
153         ctrl_conn = NULL;
154     }
155
156     if (monitor_conn) {
157         delete msgNotifier;
158         msgNotifier = NULL;
159         wpa_ctrl_detach(monitor_conn);
160         wpa_ctrl_close(monitor_conn);
161         monitor_conn = NULL;
162     }
163
164     printf("Trying to connect to '%s'\n", cfile);
165     ctrl_conn = wpa_ctrl_open(cfile);
166     if (ctrl_conn == NULL) {
167         free(cfile);
168         return -1;
169     }
170     monitor_conn = wpa_ctrl_open(cfile);
171     free(cfile);
172     if (monitor_conn == NULL) {
173         wpa_ctrl_close(ctrl_conn);
174         return -1;
175     }
176     if (wpa_ctrl_attach(monitor_conn)) {
177         printf("Failed to attach to wpa_supplicant\n");
178         wpa_ctrl_close(monitor_conn);
179         monitor_conn = NULL;
180         wpa_ctrl_close(ctrl_conn);
181         ctrl_conn = NULL;
182         return -1;
183     }
184
185     msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
186                                       QSocketNotifier::Read, this);
187     connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));
188
189     adapterSelect->clear();
190     adapterSelect->insertItem(ctrl_iface);
191     adapterSelect->setCurrentItem(0);
192
193     return 0;
194 }
195
196
197 static void wpa_gui_msg_cb(char *msg, size_t)
198 {
199     /* This should not happen anymore since two control connections are used. */
200     printf("missed message: %s\n", msg);
201 }
202
203
204 int WpaGui::ctrlRequest(const char *cmd, char *buf, size_t *buflen)
205 {
206     int ret;
207     
208     if (ctrl_conn == NULL)
209         return -3;
210     ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, buflen,
211                            wpa_gui_msg_cb);
212     if (ret == -2) {
213         printf("'%s' command timed out.\n", cmd);
214     } else if (ret < 0) {
215         printf("'%s' command failed.\n", cmd);
216     }
217     
218     return ret;
219 }
220
221
222 void WpaGui::updateStatus()
223 {
224     char buf[2048], *start, *end, *pos;
225     size_t len;
226
227     pingsToStatusUpdate = 10;
228
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();
234         textSsid->clear();
235         textBssid->clear();
236         textIpAddress->clear();
237         return;
238     }
239     
240     buf[len] = '\0';
241     
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;
246     
247     start = buf;
248     while (*start) {
249         bool last = false;
250         end = strchr(start, '\n');
251         if (end == NULL) {
252             last = true;
253             end = start;
254             while (end[0] && end[1])
255                 end++;
256         }
257         *end = '\0';
258         
259         pos = strchr(start, '=');
260         if (pos) {
261             *pos++ = '\0';
262             if (strcmp(start, "bssid") == 0) {
263                 bssid_updated = true;
264                 textBssid->setText(pos);
265             } else if (strcmp(start, "ssid") == 0) {
266                 ssid_updated = true;
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) {
275                 auth_updated = true;
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) {
281                 group_cipher = pos;
282             }
283         }
284         
285         if (last)
286             break;
287         start = end + 1;
288     }
289     
290     if (pairwise_cipher || group_cipher) {
291         QString encr;
292         if (pairwise_cipher && group_cipher &&
293             strcmp(pairwise_cipher, group_cipher) != 0) {
294             encr.append(pairwise_cipher);
295             encr.append(" + ");
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]");
302         } else {
303             encr.append("?");
304         }
305         textEncryption->setText(encr);
306     } else
307         textEncryption->clear();
308
309     if (!status_updated)
310         textStatus->clear();
311     if (!auth_updated)
312         textAuthentication->clear();
313     if (!ssid_updated)
314         textSsid->clear();
315     if (!bssid_updated)
316         textBssid->clear();
317     if (!ipaddr_updated)
318         textIpAddress->clear();
319 }
320
321
322 void WpaGui::updateNetworks()
323 {
324     char buf[2048], *start, *end, *id, *ssid, *bssid, *flags;
325     size_t len;
326     int first_active = -1;
327     bool selected = false;
328
329     if (!networkMayHaveChanged)
330         return;
331
332     networkSelect->clear();
333
334     if (ctrl_conn == NULL)
335         return;
336     
337     len = sizeof(buf) - 1;
338     if (ctrlRequest("LIST_NETWORKS", buf, &len) < 0)
339         return;
340     
341     buf[len] = '\0';
342     start = strchr(buf, '\n');
343     if (start == NULL)
344         return;
345     start++;
346
347     while (*start) {
348         bool last = false;
349         end = strchr(start, '\n');
350         if (end == NULL) {
351             last = true;
352             end = start;
353             while (end[0] && end[1])
354                 end++;
355         }
356         *end = '\0';
357         
358         id = start;
359         ssid = strchr(id, '\t');
360         if (ssid == NULL)
361             break;
362         *ssid++ = '\0';
363         bssid = strchr(ssid, '\t');
364         if (bssid == NULL)
365             break;
366         *bssid++ = '\0';
367         flags = strchr(bssid, '\t');
368         if (flags == NULL)
369             break;
370         *flags++ = '\0';
371         
372         QString network(id);
373         network.append(": ");
374         network.append(ssid);
375         networkSelect->insertItem(network);
376         
377         if (strstr(flags, "[CURRENT]")) {
378             networkSelect->setCurrentItem(networkSelect->count() - 1);
379             selected = true;
380         } else if (first_active < 0 && strstr(flags, "[DISABLED]") == NULL)
381             first_active = networkSelect->count() - 1;
382         
383         if (last)
384             break;
385         start = end + 1;
386     }
387
388     if (!selected && first_active >= 0)
389         networkSelect->setCurrentItem(first_active);
390
391     networkMayHaveChanged = false;
392 }
393
394
395 void WpaGui::helpIndex()
396 {
397     printf("helpIndex\n");
398 }
399
400
401 void WpaGui::helpContents()
402 {
403     printf("helpContents\n");
404 }
405
406
407 void WpaGui::helpAbout()
408 {
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"
413                        "\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"
417                        "\n"
418                        "Alternatively, this software may be distributed\n"
419                        "under the terms of the BSD license.\n"
420                        "\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");
424 }
425
426
427 void WpaGui::disconnect()
428 {
429     char reply[10];
430     size_t reply_len = sizeof(reply);
431     ctrlRequest("DISCONNECT", reply, &reply_len);
432 }
433
434
435 void WpaGui::scan()
436 {
437     if (scanres) {
438         scanres->close();
439         delete scanres;
440     }
441
442     scanres = new ScanResults();
443     if (scanres == NULL)
444         return;
445     scanres->setWpaGui(this);
446     scanres->show();
447     scanres->exec();
448 }
449
450
451 void WpaGui::eventHistory()
452 {
453     if (eh) {
454         eh->close();
455         delete eh;
456     }
457
458     eh = new EventHistory();
459     if (eh == NULL)
460         return;
461     eh->addEvents(msgs);
462     eh->show();
463     eh->exec();
464 }
465
466
467 void WpaGui::ping()
468 {
469     char buf[10];
470     size_t len;
471     
472     if (scanres && !scanres->isVisible()) {
473         delete scanres;
474         scanres = NULL;
475     }
476     
477     if (eh && !eh->isVisible()) {
478         delete eh;
479         eh = NULL;
480     }
481     
482     if (udr && !udr->isVisible()) {
483         delete udr;
484         udr = NULL;
485     }
486     
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;
493         }
494     }
495
496     pingsToStatusUpdate--;
497     if (pingsToStatusUpdate <= 0) {
498         updateStatus();
499         updateNetworks();
500     }
501 }
502
503
504 static int str_match(const char *a, const char *b)
505 {
506     return strncmp(a, b, strlen(b)) == 0;
507 }
508
509
510 void WpaGui::processMsg(char *msg)
511 {
512     char *pos = msg, *pos2;
513     int priority = 2;
514     
515     if (*pos == '<') {
516         /* skip priority */
517         pos++;
518         priority = atoi(pos);
519         pos = strchr(pos, '>');
520         if (pos)
521             pos++;
522         else
523             pos = msg;
524     }
525
526     WpaMsg wm(pos, priority);
527     if (eh)
528         eh->addEvent(wm);
529     msgs.append(wm);
530     while (msgs.count() > 100)
531         msgs.pop_front();
532     
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) ? ':' : ' ');
536         if (pos2)
537             pos2++;
538         else
539             pos2 = pos;
540     } else
541         pos2 = pos;
542     QString lastmsg = pos2;
543     lastmsg.truncate(40);
544     textLastMessage->setText(lastmsg);
545     
546     pingsToStatusUpdate = 0;
547     networkMayHaveChanged = true;
548     
549     if (str_match(pos, WPA_CTRL_REQ))
550         processCtrlReq(pos + strlen(WPA_CTRL_REQ));
551 }
552
553
554 void WpaGui::processCtrlReq(const char *req)
555 {
556     if (udr) {
557         udr->close();
558         delete udr;
559     }
560     udr = new UserDataRequest();
561     if (udr == NULL)
562         return;
563     if (udr->setParams(this, req) < 0) {
564         delete udr;
565         udr = NULL;
566         return;
567     }
568     udr->show();
569     udr->exec();
570 }
571
572
573 void WpaGui::receiveMsgs()
574 {
575     char buf[256];
576     size_t len;
577     
578     while (wpa_ctrl_pending(monitor_conn)) {
579         len = sizeof(buf) - 1;
580         if (wpa_ctrl_recv(monitor_conn, buf, &len) == 0) {
581             buf[len] = '\0';
582             processMsg(buf);
583         }
584     }
585 }
586
587
588 void WpaGui::connectB()
589 {
590     char reply[10];
591     size_t reply_len = sizeof(reply);
592     ctrlRequest("REASSOCIATE", reply, &reply_len);
593 }
594
595
596 void WpaGui::selectNetwork( const QString &sel )
597 {
598     QString cmd(sel);
599     char reply[10];
600     size_t reply_len = sizeof(reply);
601     
602     int pos = cmd.find(':');
603     if (pos < 0) {
604         printf("Invalid selectNetwork '%s'\n", cmd.ascii());
605         return;
606     }
607     cmd.truncate(pos);
608     cmd.prepend("SELECT_NETWORK ");
609     ctrlRequest(cmd.ascii(), reply, &reply_len);
610 }
611
612
613 void WpaGui::editNetwork()
614 {
615     QString sel(networkSelect->currentText());
616     int pos = sel.find(':');
617     if (pos < 0) {
618         printf("Invalid selectNetwork '%s'\n", sel.ascii());
619         return;
620     }
621     sel.truncate(pos);
622     
623     NetworkConfig *nc = new NetworkConfig();
624     if (nc == NULL)
625         return;
626     nc->setWpaGui(this);
627     
628     nc->paramsFromConfig(sel.toInt());
629     nc->show();
630     nc->exec();
631 }
632
633
634 void WpaGui::triggerUpdate()
635 {
636     updateStatus();
637     networkMayHaveChanged = true;
638     updateNetworks();
639 }
640
641
642 void WpaGui::addNetwork()
643 {
644     NetworkConfig *nc = new NetworkConfig();
645     if (nc == NULL)
646         return;
647     nc->setWpaGui(this);
648     nc->newNetwork();
649     nc->show();
650     nc->exec();
651 }