addinterface.cpp 5.6 KB

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