addinterface.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * wpa_gui - AddInterface class
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <cstdio>
  9. #include "common/wpa_ctrl.h"
  10. #include <QMessageBox>
  11. #include "wpagui.h"
  12. #include "addinterface.h"
  13. #ifdef CONFIG_NATIVE_WINDOWS
  14. #include <windows.h>
  15. #ifndef WPA_KEY_ROOT
  16. #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
  17. #endif
  18. #ifndef WPA_KEY_PREFIX
  19. #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
  20. #endif
  21. #endif /* CONFIG_NATIVE_WINDOWS */
  22. AddInterface::AddInterface(WpaGui *_wpagui, QWidget *parent)
  23. : QDialog(parent), wpagui(_wpagui)
  24. {
  25. setWindowTitle(tr("Select network interface to add"));
  26. resize(400, 200);
  27. vboxLayout = new QVBoxLayout(this);
  28. interfaceWidget = new QTreeWidget(this);
  29. interfaceWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  30. interfaceWidget->setUniformRowHeights(true);
  31. interfaceWidget->setSortingEnabled(true);
  32. interfaceWidget->setColumnCount(3);
  33. interfaceWidget->headerItem()->setText(0, tr("driver"));
  34. interfaceWidget->headerItem()->setText(1, tr("interface"));
  35. interfaceWidget->headerItem()->setText(2, tr("description"));
  36. interfaceWidget->setItemsExpandable(false);
  37. interfaceWidget->setRootIsDecorated(false);
  38. vboxLayout->addWidget(interfaceWidget);
  39. connect(interfaceWidget,
  40. SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
  41. SLOT(interfaceSelected(QTreeWidgetItem *)));
  42. addInterfaces();
  43. }
  44. void AddInterface::addInterfaces()
  45. {
  46. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  47. struct wpa_ctrl *ctrl;
  48. int ret;
  49. char buf[2048];
  50. size_t len;
  51. ctrl = wpa_ctrl_open(NULL);
  52. if (ctrl == NULL)
  53. return;
  54. len = sizeof(buf) - 1;
  55. ret = wpa_ctrl_request(ctrl, "INTERFACE_LIST", 14, buf, &len, NULL);
  56. if (ret < 0) {
  57. wpa_ctrl_close(ctrl);
  58. return;
  59. }
  60. buf[len] = '\0';
  61. wpa_ctrl_close(ctrl);
  62. QString ifaces(buf);
  63. QStringList lines = ifaces.split(QRegExp("\\n"));
  64. for (QStringList::Iterator it = lines.begin();
  65. it != lines.end(); it++) {
  66. QStringList arg = (*it).split(QChar('\t'));
  67. if (arg.size() < 3)
  68. continue;
  69. QTreeWidgetItem *item = new QTreeWidgetItem(interfaceWidget);
  70. if (!item)
  71. break;
  72. item->setText(0, arg[0]);
  73. item->setText(1, arg[1]);
  74. item->setText(2, arg[2]);
  75. }
  76. interfaceWidget->resizeColumnToContents(0);
  77. interfaceWidget->resizeColumnToContents(1);
  78. interfaceWidget->resizeColumnToContents(2);
  79. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  80. }
  81. #ifdef CONFIG_NATIVE_WINDOWS
  82. bool AddInterface::addRegistryInterface(const QString &ifname)
  83. {
  84. HKEY hk, ihk;
  85. LONG ret;
  86. int id, tmp;
  87. TCHAR name[10];
  88. DWORD val, i;
  89. ret = RegOpenKeyEx(WPA_KEY_ROOT, WPA_KEY_PREFIX TEXT("\\interfaces"),
  90. 0, KEY_ENUMERATE_SUB_KEYS | KEY_CREATE_SUB_KEY,
  91. &hk);
  92. if (ret != ERROR_SUCCESS)
  93. return false;
  94. id = -1;
  95. for (i = 0; ; i++) {
  96. TCHAR name[255];
  97. DWORD namelen;
  98. namelen = 255;
  99. ret = RegEnumKeyEx(hk, i, name, &namelen, NULL, NULL, NULL,
  100. NULL);
  101. if (ret == ERROR_NO_MORE_ITEMS)
  102. break;
  103. if (ret != ERROR_SUCCESS)
  104. break;
  105. if (namelen >= 255)
  106. namelen = 255 - 1;
  107. name[namelen] = '\0';
  108. #ifdef UNICODE
  109. QString s((QChar *) name, namelen);
  110. #else /* UNICODE */
  111. QString s(name);
  112. #endif /* UNICODE */
  113. tmp = s.toInt();
  114. if (tmp > id)
  115. id = tmp;
  116. }
  117. id += 1;
  118. #ifdef UNICODE
  119. wsprintf(name, L"%04d", id);
  120. #else /* UNICODE */
  121. os_snprintf(name, sizeof(name), "%04d", id);
  122. #endif /* UNICODE */
  123. ret = RegCreateKeyEx(hk, name, 0, NULL, 0, KEY_WRITE, NULL, &ihk,
  124. NULL);
  125. RegCloseKey(hk);
  126. if (ret != ERROR_SUCCESS)
  127. return false;
  128. #ifdef UNICODE
  129. RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
  130. (LPBYTE) ifname.unicode(),
  131. (ifname.length() + 1) * sizeof(TCHAR));
  132. #else /* UNICODE */
  133. RegSetValueEx(ihk, TEXT("adapter"), 0, REG_SZ,
  134. (LPBYTE) ifname.toLocal8Bit(), ifname.length() + 1);
  135. #endif /* UNICODE */
  136. RegSetValueEx(ihk, TEXT("config"), 0, REG_SZ,
  137. (LPBYTE) TEXT("default"), 8 * sizeof(TCHAR));
  138. RegSetValueEx(ihk, TEXT("ctrl_interface"), 0, REG_SZ,
  139. (LPBYTE) TEXT(""), 1 * sizeof(TCHAR));
  140. val = 1;
  141. RegSetValueEx(ihk, TEXT("skip_on_error"), 0, REG_DWORD, (LPBYTE) &val,
  142. sizeof(val));
  143. RegCloseKey(ihk);
  144. return true;
  145. }
  146. #endif /* CONFIG_NATIVE_WINDOWS */
  147. void AddInterface::interfaceSelected(QTreeWidgetItem *sel)
  148. {
  149. if (!sel)
  150. return;
  151. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  152. struct wpa_ctrl *ctrl;
  153. int ret;
  154. char buf[20], cmd[256];
  155. size_t len;
  156. /*
  157. * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
  158. * <driver_param>TAB<bridge_name>
  159. */
  160. snprintf(cmd, sizeof(cmd),
  161. "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
  162. sel->text(1).toLocal8Bit().constData(),
  163. "default",
  164. sel->text(0).toLocal8Bit().constData(),
  165. "yes", "", "");
  166. cmd[sizeof(cmd) - 1] = '\0';
  167. ctrl = wpa_ctrl_open(NULL);
  168. if (ctrl == NULL)
  169. return;
  170. len = sizeof(buf) - 1;
  171. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL);
  172. wpa_ctrl_close(ctrl);
  173. if (ret < 0) {
  174. QMessageBox::warning(this, "wpa_gui",
  175. tr("Add interface command could not be "
  176. "completed."));
  177. return;
  178. }
  179. buf[len] = '\0';
  180. if (buf[0] != 'O' || buf[1] != 'K') {
  181. QMessageBox::warning(this, "wpa_gui",
  182. tr("Failed to add the interface."));
  183. return;
  184. }
  185. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  186. #ifdef CONFIG_NATIVE_WINDOWS
  187. if (!addRegistryInterface(sel->text(1))) {
  188. QMessageBox::information(this, "wpa_gui",
  189. tr("Failed to add the interface into "
  190. "registry."));
  191. }
  192. #endif /* CONFIG_NATIVE_WINDOWS */
  193. wpagui->selectAdapter(sel->text(1));
  194. close();
  195. }