aboutsummaryrefslogtreecommitdiff
path: root/contrib/wpa_supplicant/wpa_gui-qt4/wpagui.ui.h
blob: 58760e734471edaf784e0114371f49c5ed3ae0c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/


#ifdef __MINGW32__
/* Need to get getopt() */
#include <unistd.h>
#endif


void WpaGui::init()
{
    eh = NULL;
    scanres = NULL;
    udr = NULL;
    ctrl_iface = NULL;
    ctrl_conn = NULL;
    monitor_conn = NULL;
    msgNotifier = NULL;
    ctrl_iface_dir = strdup("/var/run/wpa_supplicant");
    
    parse_argv();

    textStatus->setText("connecting to wpa_supplicant");
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), SLOT(ping()));
    timer->start(1000, FALSE);
    
    if (openCtrlConnection(ctrl_iface) < 0) {
	printf("Failed to open control connection to wpa_supplicant.\n");
    }
    
    updateStatus();
    networkMayHaveChanged = true;
    updateNetworks();
}


void WpaGui::destroy()
{
    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::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;

    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");
#else /* CONFIG_CTRL_IFACE_UDP */
	struct dirent *dent;
	DIR *dir = opendir(ctrl_iface_dir);
	free(ctrl_iface);
	ctrl_iface = NULL;
	if (dir) {
	    while ((dent = readdir(dir))) {
		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_UDP */
    }
    
    if (ctrl_iface == NULL)
	return -1;

    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);

    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;
    }

    msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
				      QSocketNotifier::Read, this);
    connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));

    adapterSelect->clear();
    adapterSelect->insertItem(ctrl_iface);
    adapterSelect->setCurrentItem(0);

    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 if (group_cipher) {
	    encr.append(group_cipher);
	    encr.append(" [group key only]");
	} else {
	    encr.append("?");
	}
	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;
    bool selected = false;

    if (!networkMayHaveChanged)
	return;

    networkSelect->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->insertItem(network);
	
	if (strstr(flags, "[CURRENT]")) {
	    networkSelect->setCurrentItem(networkSelect->count() - 1);
	    selected = true;
	} else if (first_active < 0 && strstr(flags, "[DISABLED]") == NULL)
	    first_active = networkSelect->count() - 1;
	
	if (last)
	    break;
	start = end + 1;
    }

    if (!selected && first_active >= 0)
	networkSelect->setCurrentItem(first_active);

    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-2005,\n"
		       "Jouni Malinen <jkmaline@cc.hut.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;
    }

    scanres = new ScanResults();
    if (scanres == NULL)
	return;
    scanres->setWpaGui(this);
    scanres->show();
    scanres->exec();
}


void WpaGui::eventHistory()
{
    if (eh) {
	eh->close();
	delete eh;
    }

    eh = new EventHistory();
    if (eh == NULL)
	return;
    eh->addEvents(msgs);
    eh->show();
    eh->exec();
}


void WpaGui::ping()
{
    char buf[10];
    size_t len;
    
    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));
}


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 (wpa_ctrl_pending(monitor_conn)) {
	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);
    
    int pos = cmd.find(':');
    if (pos < 0) {
	printf("Invalid selectNetwork '%s'\n", cmd.ascii());
	return;
    }
    cmd.truncate(pos);
    cmd.prepend("SELECT_NETWORK ");
    ctrlRequest(cmd.ascii(), reply, &reply_len);
}


void WpaGui::editNetwork()
{
    QString sel(networkSelect->currentText());
    int pos = sel.find(':');
    if (pos < 0) {
	printf("Invalid selectNetwork '%s'\n", sel.ascii());
	return;
    }
    sel.truncate(pos);
    
    NetworkConfig *nc = new NetworkConfig();
    if (nc == NULL)
	return;
    nc->setWpaGui(this);
    
    nc->paramsFromConfig(sel.toInt());
    nc->show();
    nc->exec();
}


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();
}