aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/wpa_gui-qt4/addinterface.cpp
blob: 88d4603d03f951826ac6a1d51f8922b63d0287f4 (plain)
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
/*
 * wpa_gui - AddInterface class
 * Copyright (c) 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.
 */

#include <cstdio>
#include "common/wpa_ctrl.h"

#include <QMessageBox>

#include "wpagui.h"
#include "addinterface.h"

#ifdef CONFIG_NATIVE_WINDOWS
#include <windows.h>

#ifndef WPA_KEY_ROOT
#define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
#endif
#ifndef WPA_KEY_PREFIX
#define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
#endif
#endif /* CONFIG_NATIVE_WINDOWS */


AddInterface::AddInterface(WpaGui *_wpagui, QWidget *parent)
	: QDialog(parent), wpagui(_wpagui)
{
	setWindowTitle(tr("Select network interface to add"));
	resize(400, 200);
	vboxLayout = new QVBoxLayout(this);

	interfaceWidget = new QTreeWidget(this);
	interfaceWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
	interfaceWidget->setUniformRowHeights(true);
	interfaceWidget->setSortingEnabled(true);
	interfaceWidget->setColumnCount(3);
	interfaceWidget->headerItem()->setText(0, tr("driver"));
	interfaceWidget->headerItem()->setText(1, tr("interface"));
	interfaceWidget->headerItem()->setText(2, tr("description"));
	interfaceWidget->setItemsExpandable(FALSE);
	interfaceWidget->setRootIsDecorated(FALSE);
	vboxLayout->addWidget(interfaceWidget);

	connect(interfaceWidget,
		SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
		SLOT(interfaceSelected(QTreeWidgetItem *)));

	addInterfaces();
}


void AddInterface::addInterfaces()
{
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
	struct wpa_ctrl *ctrl;
	int ret;
	char buf[2048];
	size_t len;

	ctrl = wpa_ctrl_open(NULL);
	if (ctrl == NULL)
		return;

	len = sizeof(buf) - 1;
	ret = wpa_ctrl_request(ctrl, "INTERFACE_LIST", 14, buf, &len, NULL);
	if (ret < 0) {
		wpa_ctrl_close(ctrl);
		return;
	}
	buf[len] = '\0';

	wpa_ctrl_close(ctrl);

	QString ifaces(buf);
	QStringList lines = ifaces.split(QRegExp("\\n"));
	for (QStringList::Iterator it = lines.begin();
	     it != lines.end(); it++) {
		QStringList arg = (*it).split(QChar('\t'));
		if (arg.size() < 3)
			continue;
		QTreeWidgetItem *item = new QTreeWidgetItem(interfaceWidget);
		if (!item)
			break;

		item->setText(0, arg[0]);
		item->setText(1, arg[1]);
		item->setText(2, arg[2]);
	}

	interfaceWidget->resizeColumnToContents(0);
	interfaceWidget->resizeColumnToContents(1);
	interfaceWidget->resizeColumnToContents(2);
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
}


#ifdef CONFIG_NATIVE_WINDOWS
bool AddInterface::addRegistryInterface(const QString &ifname)
{
	HKEY hk, ihk;
	LONG ret;
	int id, tmp;
	TCHAR name[10];
	DWORD val, i;

	ret = RegOpenKeyEx(WPA_KEY_ROOT, WPA_KEY_PREFIX TEXT("\\interfaces"),
			   0, KEY_ENUMERATE_SUB_KEYS | KEY_CREATE_SUB_KEY,
			   &hk);
	if (ret != ERROR_SUCCESS)
		return false;

	id = -1;

	for (i = 0; ; i++) {
		TCHAR name[255];
		DWORD namelen;

		namelen = 255;
		ret = RegEnumKeyEx(hk, i, name, &namelen, NULL, NULL, NULL,
				   NULL);

		if (ret == ERROR_NO_MORE_ITEMS)
			break;

		if (ret != ERROR_SUCCESS)
			break;

		if (namelen >= 255)
			namelen = 255 - 1;
		name[namelen] = '\0';

#ifdef UNICODE
		QString s((QChar *) name, namelen);
#else /* UNICODE */
		QString s(name);
#endif /* UNICODE */
		tmp = s.toInt();
		if (tmp > id)
			id = tmp;
	}

	id += 1;

#ifdef UNICODE
	wsprintf(name, L"%04d", id);
#else /* UNICODE */
	os_snprintf(name, sizeof(name), "%04d", id);
#endif /* UNICODE */
	ret = RegCreateKeyEx(hk, name, 0, NULL, 0, KEY_WRITE, NULL, &ihk,
			     NULL);
	RegCloseKey(hk);
	if (ret != ERROR_SUCCESS)
		return false;

#ifdef UNICODE
	RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
		      (LPBYTE) ifname.unicode(),
		      (ifname.length() + 1) * sizeof(TCHAR));

#else /* UNICODE */
	RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
		      (LPBYTE) ifname.toLocal8Bit(), ifname.length() + 1);
#endif /* UNICODE */
	RegSetValueEx(ihk, TEXT("config"), 0, REG_SZ,
		      (LPBYTE) TEXT("default"), 8 * sizeof(TCHAR));
	RegSetValueEx(ihk, TEXT("ctrl_interface"), 0, REG_SZ,
		      (LPBYTE) TEXT(""), 1 * sizeof(TCHAR));
	val = 1;
	RegSetValueEx(ihk, TEXT("skip_on_error"), 0, REG_DWORD, (LPBYTE) &val,
		      sizeof(val));

	RegCloseKey(ihk);
	return true;
}
#endif /* CONFIG_NATIVE_WINDOWS */


void AddInterface::interfaceSelected(QTreeWidgetItem *sel)
{
	if (!sel)
		return;

#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
	struct wpa_ctrl *ctrl;
	int ret;
	char buf[20], cmd[256];
	size_t len;

	/*
	 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
	 * <driver_param>TAB<bridge_name>
	 */
	snprintf(cmd, sizeof(cmd),
		 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
		 sel->text(1).toAscii().constData(),
		 "default",
		 sel->text(0).toAscii().constData(),
		 "yes", "", "");
	cmd[sizeof(cmd) - 1] = '\0';

	ctrl = wpa_ctrl_open(NULL);
	if (ctrl == NULL)
		return;

	len = sizeof(buf) - 1;
	ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL);
	wpa_ctrl_close(ctrl);

	if (ret < 0) {
		QMessageBox::warning(this, "wpa_gui",
				     tr("Add interface command could not be "
					"completed."));
		return;
	}

	buf[len] = '\0';
	if (buf[0] != 'O' || buf[1] != 'K') {
		QMessageBox::warning(this, "wpa_gui",
				     tr("Failed to add the interface."));
		return;
	}

#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */

#ifdef CONFIG_NATIVE_WINDOWS
	if (!addRegistryInterface(sel->text(1))) {
		QMessageBox::information(this, "wpa_gui",
					 tr("Failed to add the interface into "
					    "registry."));
	}
#endif /* CONFIG_NATIVE_WINDOWS */

	wpagui->selectAdapter(sel->text(1));
	close();
}