2 * wpa_gui - AddInterface class
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include "common/wpa_ctrl.h"
18 #include <QMessageBox>
21 #include "addinterface.h"
23 #ifdef CONFIG_NATIVE_WINDOWS
27 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
29 #ifndef WPA_KEY_PREFIX
30 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
32 #endif /* CONFIG_NATIVE_WINDOWS */
35 AddInterface::AddInterface(WpaGui
*_wpagui
, QWidget
*parent
)
36 : QDialog(parent
), wpagui(_wpagui
)
38 setWindowTitle(tr("Select network interface to add"));
40 vboxLayout
= new QVBoxLayout(this);
42 interfaceWidget
= new QTreeWidget(this);
43 interfaceWidget
->setEditTriggers(QAbstractItemView::NoEditTriggers
);
44 interfaceWidget
->setUniformRowHeights(true);
45 interfaceWidget
->setSortingEnabled(true);
46 interfaceWidget
->setColumnCount(3);
47 interfaceWidget
->headerItem()->setText(0, tr("driver"));
48 interfaceWidget
->headerItem()->setText(1, tr("interface"));
49 interfaceWidget
->headerItem()->setText(2, tr("description"));
50 interfaceWidget
->setItemsExpandable(FALSE
);
51 interfaceWidget
->setRootIsDecorated(FALSE
);
52 vboxLayout
->addWidget(interfaceWidget
);
54 connect(interfaceWidget
,
55 SIGNAL(itemActivated(QTreeWidgetItem
*, int)), this,
56 SLOT(interfaceSelected(QTreeWidgetItem
*)));
62 void AddInterface::addInterfaces()
64 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
65 struct wpa_ctrl
*ctrl
;
70 ctrl
= wpa_ctrl_open(NULL
);
74 len
= sizeof(buf
) - 1;
75 ret
= wpa_ctrl_request(ctrl
, "INTERFACE_LIST", 14, buf
, &len
, NULL
);
85 QStringList lines
= ifaces
.split(QRegExp("\\n"));
86 for (QStringList::Iterator it
= lines
.begin();
87 it
!= lines
.end(); it
++) {
88 QStringList arg
= (*it
).split(QChar('\t'));
91 QTreeWidgetItem
*item
= new QTreeWidgetItem(interfaceWidget
);
95 item
->setText(0, arg
[0]);
96 item
->setText(1, arg
[1]);
97 item
->setText(2, arg
[2]);
100 interfaceWidget
->resizeColumnToContents(0);
101 interfaceWidget
->resizeColumnToContents(1);
102 interfaceWidget
->resizeColumnToContents(2);
103 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
107 #ifdef CONFIG_NATIVE_WINDOWS
108 bool AddInterface::addRegistryInterface(const QString
&ifname
)
116 ret
= RegOpenKeyEx(WPA_KEY_ROOT
, WPA_KEY_PREFIX
TEXT("\\interfaces"),
117 0, KEY_ENUMERATE_SUB_KEYS
| KEY_CREATE_SUB_KEY
,
119 if (ret
!= ERROR_SUCCESS
)
129 ret
= RegEnumKeyEx(hk
, i
, name
, &namelen
, NULL
, NULL
, NULL
,
132 if (ret
== ERROR_NO_MORE_ITEMS
)
135 if (ret
!= ERROR_SUCCESS
)
140 name
[namelen
] = '\0';
143 QString
s((QChar
*) name
, namelen
);
155 wsprintf(name
, L
"%04d", id
);
157 os_snprintf(name
, sizeof(name
), "%04d", id
);
159 ret
= RegCreateKeyEx(hk
, name
, 0, NULL
, 0, KEY_WRITE
, NULL
, &ihk
,
162 if (ret
!= ERROR_SUCCESS
)
166 RegSetValueEx(ihk
, TEXT("adapter"), 0, REG_SZ
,
167 (LPBYTE
) ifname
.unicode(),
168 (ifname
.length() + 1) * sizeof(TCHAR
));
171 RegSetValueEx(ihk
, TEXT("adapter"), 0, REG_SZ
,
172 (LPBYTE
) ifname
.toLocal8Bit(), ifname
.length() + 1);
174 RegSetValueEx(ihk
, TEXT("config"), 0, REG_SZ
,
175 (LPBYTE
) TEXT("default"), 8 * sizeof(TCHAR
));
176 RegSetValueEx(ihk
, TEXT("ctrl_interface"), 0, REG_SZ
,
177 (LPBYTE
) TEXT(""), 1 * sizeof(TCHAR
));
179 RegSetValueEx(ihk
, TEXT("skip_on_error"), 0, REG_DWORD
, (LPBYTE
) &val
,
185 #endif /* CONFIG_NATIVE_WINDOWS */
188 void AddInterface::interfaceSelected(QTreeWidgetItem
*sel
)
193 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
194 struct wpa_ctrl
*ctrl
;
196 char buf
[20], cmd
[256];
200 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
201 * <driver_param>TAB<bridge_name>
203 snprintf(cmd
, sizeof(cmd
),
204 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
205 sel
->text(1).toAscii().constData(),
207 sel
->text(0).toAscii().constData(),
209 cmd
[sizeof(cmd
) - 1] = '\0';
211 ctrl
= wpa_ctrl_open(NULL
);
215 len
= sizeof(buf
) - 1;
216 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
, NULL
);
217 wpa_ctrl_close(ctrl
);
220 QMessageBox::warning(this, "wpa_gui",
221 tr("Add interface command could not be "
227 if (buf
[0] != 'O' || buf
[1] != 'K') {
228 QMessageBox::warning(this, "wpa_gui",
229 tr("Failed to add the interface."));
233 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
235 #ifdef CONFIG_NATIVE_WINDOWS
236 if (!addRegistryInterface(sel
->text(1))) {
237 QMessageBox::information(this, "wpa_gui",
238 tr("Failed to add the interface into "
241 #endif /* CONFIG_NATIVE_WINDOWS */
243 wpagui
->selectAdapter(sel
->text(1));