addinterface.cpp
Go to the documentation of this file.
00001 /*
00002  * wpa_gui - AddInterface class
00003  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include <cstdio>
00016 #include "common/wpa_ctrl.h"
00017 
00018 #include <QMessageBox>
00019 
00020 #include "wpagui.h"
00021 #include "addinterface.h"
00022 
00023 #ifdef CONFIG_NATIVE_WINDOWS
00024 #include <windows.h>
00025 
00026 #ifndef WPA_KEY_ROOT
00027 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
00028 #endif
00029 #ifndef WPA_KEY_PREFIX
00030 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
00031 #endif
00032 #endif /* CONFIG_NATIVE_WINDOWS */
00033 
00034 
00035 AddInterface::AddInterface(WpaGui *_wpagui, QWidget *parent)
00036         : QDialog(parent), wpagui(_wpagui)
00037 {
00038         setWindowTitle(tr("Select network interface to add"));
00039         resize(400, 200);
00040         vboxLayout = new QVBoxLayout(this);
00041 
00042         interfaceWidget = new QTreeWidget(this);
00043         interfaceWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
00044         interfaceWidget->setUniformRowHeights(true);
00045         interfaceWidget->setSortingEnabled(true);
00046         interfaceWidget->setColumnCount(3);
00047         interfaceWidget->headerItem()->setText(0, tr("driver"));
00048         interfaceWidget->headerItem()->setText(1, tr("interface"));
00049         interfaceWidget->headerItem()->setText(2, tr("description"));
00050         interfaceWidget->setItemsExpandable(FALSE);
00051         interfaceWidget->setRootIsDecorated(FALSE);
00052         vboxLayout->addWidget(interfaceWidget);
00053 
00054         connect(interfaceWidget,
00055                 SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
00056                 SLOT(interfaceSelected(QTreeWidgetItem *)));
00057 
00058         addInterfaces();
00059 }
00060 
00061 
00062 void AddInterface::addInterfaces()
00063 {
00064 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
00065         struct wpa_ctrl *ctrl;
00066         int ret;
00067         char buf[2048];
00068         size_t len;
00069 
00070         ctrl = wpa_ctrl_open(NULL);
00071         if (ctrl == NULL)
00072                 return;
00073 
00074         len = sizeof(buf) - 1;
00075         ret = wpa_ctrl_request(ctrl, "INTERFACE_LIST", 14, buf, &len, NULL);
00076         if (ret < 0) {
00077                 wpa_ctrl_close(ctrl);
00078                 return;
00079         }
00080         buf[len] = '\0';
00081 
00082         wpa_ctrl_close(ctrl);
00083 
00084         QString ifaces(buf);
00085         QStringList lines = ifaces.split(QRegExp("\\n"));
00086         for (QStringList::Iterator it = lines.begin();
00087              it != lines.end(); it++) {
00088                 QStringList arg = (*it).split(QChar('\t'));
00089                 if (arg.size() < 3)
00090                         continue;
00091                 QTreeWidgetItem *item = new QTreeWidgetItem(interfaceWidget);
00092                 if (!item)
00093                         break;
00094 
00095                 item->setText(0, arg[0]);
00096                 item->setText(1, arg[1]);
00097                 item->setText(2, arg[2]);
00098         }
00099 
00100         interfaceWidget->resizeColumnToContents(0);
00101         interfaceWidget->resizeColumnToContents(1);
00102         interfaceWidget->resizeColumnToContents(2);
00103 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
00104 }
00105 
00106 
00107 #ifdef CONFIG_NATIVE_WINDOWS
00108 bool AddInterface::addRegistryInterface(const QString &ifname)
00109 {
00110         HKEY hk, ihk;
00111         LONG ret;
00112         int id, tmp;
00113         TCHAR name[10];
00114         DWORD val, i;
00115 
00116         ret = RegOpenKeyEx(WPA_KEY_ROOT, WPA_KEY_PREFIX TEXT("\\interfaces"),
00117                            0, KEY_ENUMERATE_SUB_KEYS | KEY_CREATE_SUB_KEY,
00118                            &hk);
00119         if (ret != ERROR_SUCCESS)
00120                 return false;
00121 
00122         id = -1;
00123 
00124         for (i = 0; ; i++) {
00125                 TCHAR name[255];
00126                 DWORD namelen;
00127 
00128                 namelen = 255;
00129                 ret = RegEnumKeyEx(hk, i, name, &namelen, NULL, NULL, NULL,
00130                                    NULL);
00131 
00132                 if (ret == ERROR_NO_MORE_ITEMS)
00133                         break;
00134 
00135                 if (ret != ERROR_SUCCESS)
00136                         break;
00137 
00138                 if (namelen >= 255)
00139                         namelen = 255 - 1;
00140                 name[namelen] = '\0';
00141 
00142 #ifdef UNICODE
00143                 QString s((QChar *) name, namelen);
00144 #else /* UNICODE */
00145                 QString s(name);
00146 #endif /* UNICODE */
00147                 tmp = s.toInt();
00148                 if (tmp > id)
00149                         id = tmp;
00150         }
00151 
00152         id += 1;
00153 
00154 #ifdef UNICODE
00155         wsprintf(name, L"%04d", id);
00156 #else /* UNICODE */
00157         os_snprintf(name, sizeof(name), "%04d", id);
00158 #endif /* UNICODE */
00159         ret = RegCreateKeyEx(hk, name, 0, NULL, 0, KEY_WRITE, NULL, &ihk,
00160                              NULL);
00161         RegCloseKey(hk);
00162         if (ret != ERROR_SUCCESS)
00163                 return false;
00164 
00165 #ifdef UNICODE
00166         RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
00167                       (LPBYTE) ifname.unicode(),
00168                       (ifname.length() + 1) * sizeof(TCHAR));
00169 
00170 #else /* UNICODE */
00171         RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
00172                       (LPBYTE) ifname.toLocal8Bit(), ifname.length() + 1);
00173 #endif /* UNICODE */
00174         RegSetValueEx(ihk, TEXT("config"), 0, REG_SZ,
00175                       (LPBYTE) TEXT("default"), 8 * sizeof(TCHAR));
00176         RegSetValueEx(ihk, TEXT("ctrl_interface"), 0, REG_SZ,
00177                       (LPBYTE) TEXT(""), 1 * sizeof(TCHAR));
00178         val = 1;
00179         RegSetValueEx(ihk, TEXT("skip_on_error"), 0, REG_DWORD, (LPBYTE) &val,
00180                       sizeof(val));
00181 
00182         RegCloseKey(ihk);
00183         return true;
00184 }
00185 #endif /* CONFIG_NATIVE_WINDOWS */
00186 
00187 
00188 void AddInterface::interfaceSelected(QTreeWidgetItem *sel)
00189 {
00190         if (!sel)
00191                 return;
00192 
00193 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
00194         struct wpa_ctrl *ctrl;
00195         int ret;
00196         char buf[20], cmd[256];
00197         size_t len;
00198 
00199         /*
00200          * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
00201          * <driver_param>TAB<bridge_name>
00202          */
00203         snprintf(cmd, sizeof(cmd),
00204                  "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
00205                  sel->text(1).toAscii().constData(),
00206                  "default",
00207                  sel->text(0).toAscii().constData(),
00208                  "yes", "", "");
00209         cmd[sizeof(cmd) - 1] = '\0';
00210 
00211         ctrl = wpa_ctrl_open(NULL);
00212         if (ctrl == NULL)
00213                 return;
00214 
00215         len = sizeof(buf) - 1;
00216         ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL);
00217         wpa_ctrl_close(ctrl);
00218 
00219         if (ret < 0) {
00220                 QMessageBox::warning(this, "wpa_gui",
00221                                      tr("Add interface command could not be "
00222                                         "completed."));
00223                 return;
00224         }
00225 
00226         buf[len] = '\0';
00227         if (buf[0] != 'O' || buf[1] != 'K') {
00228                 QMessageBox::warning(this, "wpa_gui",
00229                                      tr("Failed to add the interface."));
00230                 return;
00231         }
00232 
00233 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
00234 
00235 #ifdef CONFIG_NATIVE_WINDOWS
00236         if (!addRegistryInterface(sel->text(1))) {
00237                 QMessageBox::information(this, "wpa_gui",
00238                                          tr("Failed to add the interface into "
00239                                             "registry."));
00240         }
00241 #endif /* CONFIG_NATIVE_WINDOWS */
00242 
00243         wpagui->selectAdapter(sel->text(1));
00244         close();
00245 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:19