wpagui.ui.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /****************************************************************************
  2. ** ui.h extension file, included from the uic-generated form implementation.
  3. **
  4. ** If you want to add, delete, or rename functions or slots, use
  5. ** Qt Designer to update this file, preserving your code.
  6. **
  7. ** You should not define a constructor or destructor in this file.
  8. ** Instead, write your code in functions called init() and destroy().
  9. ** These will automatically be called by the form's constructor and
  10. ** destructor.
  11. *****************************************************************************/
  12. #ifdef __MINGW32__
  13. /* Need to get getopt() */
  14. #include <unistd.h>
  15. #endif
  16. #include <stdlib.h>
  17. void WpaGui::init()
  18. {
  19. eh = NULL;
  20. scanres = NULL;
  21. udr = NULL;
  22. ctrl_iface = NULL;
  23. ctrl_conn = NULL;
  24. monitor_conn = NULL;
  25. msgNotifier = NULL;
  26. ctrl_iface_dir = strdup("/var/run/wpa_supplicant");
  27. parse_argv();
  28. textStatus->setText("connecting to wpa_supplicant");
  29. timer = new QTimer(this);
  30. connect(timer, SIGNAL(timeout()), SLOT(ping()));
  31. timer->start(1000, FALSE);
  32. if (openCtrlConnection(ctrl_iface) < 0) {
  33. printf("Failed to open control connection to wpa_supplicant.\n");
  34. }
  35. updateStatus();
  36. networkMayHaveChanged = true;
  37. updateNetworks();
  38. }
  39. void WpaGui::destroy()
  40. {
  41. delete msgNotifier;
  42. if (monitor_conn) {
  43. wpa_ctrl_detach(monitor_conn);
  44. wpa_ctrl_close(monitor_conn);
  45. monitor_conn = NULL;
  46. }
  47. if (ctrl_conn) {
  48. wpa_ctrl_close(ctrl_conn);
  49. ctrl_conn = NULL;
  50. }
  51. if (eh) {
  52. eh->close();
  53. delete eh;
  54. eh = NULL;
  55. }
  56. if (scanres) {
  57. scanres->close();
  58. delete scanres;
  59. scanres = NULL;
  60. }
  61. if (udr) {
  62. udr->close();
  63. delete udr;
  64. udr = NULL;
  65. }
  66. free(ctrl_iface);
  67. ctrl_iface = NULL;
  68. free(ctrl_iface_dir);
  69. ctrl_iface_dir = NULL;
  70. }
  71. void WpaGui::parse_argv()
  72. {
  73. int c;
  74. for (;;) {
  75. c = getopt(qApp->argc(), qApp->argv(), "i:p:");
  76. if (c < 0)
  77. break;
  78. switch (c) {
  79. case 'i':
  80. free(ctrl_iface);
  81. ctrl_iface = strdup(optarg);
  82. break;
  83. case 'p':
  84. free(ctrl_iface_dir);
  85. ctrl_iface_dir = strdup(optarg);
  86. break;
  87. }
  88. }
  89. }
  90. int WpaGui::openCtrlConnection(const char *ifname)
  91. {
  92. char *cfile;
  93. int flen;
  94. char buf[2048], *pos, *pos2;
  95. size_t len;
  96. if (ifname) {
  97. if (ifname != ctrl_iface) {
  98. free(ctrl_iface);
  99. ctrl_iface = strdup(ifname);
  100. }
  101. } else {
  102. #ifdef CONFIG_CTRL_IFACE_UDP
  103. free(ctrl_iface);
  104. ctrl_iface = strdup("udp");
  105. #endif /* CONFIG_CTRL_IFACE_UDP */
  106. #ifdef CONFIG_CTRL_IFACE_UNIX
  107. struct dirent *dent;
  108. DIR *dir = opendir(ctrl_iface_dir);
  109. free(ctrl_iface);
  110. ctrl_iface = NULL;
  111. if (dir) {
  112. while ((dent = readdir(dir))) {
  113. #ifdef _DIRENT_HAVE_D_TYPE
  114. /* Skip the file if it is not a socket.
  115. * Also accept DT_UNKNOWN (0) in case
  116. * the C library or underlying file
  117. * system does not support d_type. */
  118. if (dent->d_type != DT_SOCK &&
  119. dent->d_type != DT_UNKNOWN)
  120. continue;
  121. #endif /* _DIRENT_HAVE_D_TYPE */
  122. if (strcmp(dent->d_name, ".") == 0 ||
  123. strcmp(dent->d_name, "..") == 0)
  124. continue;
  125. printf("Selected interface '%s'\n", dent->d_name);
  126. ctrl_iface = strdup(dent->d_name);
  127. break;
  128. }
  129. closedir(dir);
  130. }
  131. #endif /* CONFIG_CTRL_IFACE_UNIX */
  132. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  133. struct wpa_ctrl *ctrl;
  134. int ret;
  135. free(ctrl_iface);
  136. ctrl_iface = NULL;
  137. ctrl = wpa_ctrl_open(NULL);
  138. if (ctrl) {
  139. len = sizeof(buf) - 1;
  140. ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
  141. if (ret >= 0) {
  142. buf[len] = '\0';
  143. pos = strchr(buf, '\n');
  144. if (pos)
  145. *pos = '\0';
  146. ctrl_iface = strdup(buf);
  147. }
  148. wpa_ctrl_close(ctrl);
  149. }
  150. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  151. }
  152. if (ctrl_iface == NULL)
  153. return -1;
  154. #ifdef CONFIG_CTRL_IFACE_UNIX
  155. flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2;
  156. cfile = (char *) malloc(flen);
  157. if (cfile == NULL)
  158. return -1;
  159. snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface);
  160. #else /* CONFIG_CTRL_IFACE_UNIX */
  161. flen = strlen(ctrl_iface) + 1;
  162. cfile = (char *) malloc(flen);
  163. if (cfile == NULL)
  164. return -1;
  165. snprintf(cfile, flen, "%s", ctrl_iface);
  166. #endif /* CONFIG_CTRL_IFACE_UNIX */
  167. if (ctrl_conn) {
  168. wpa_ctrl_close(ctrl_conn);
  169. ctrl_conn = NULL;
  170. }
  171. if (monitor_conn) {
  172. delete msgNotifier;
  173. msgNotifier = NULL;
  174. wpa_ctrl_detach(monitor_conn);
  175. wpa_ctrl_close(monitor_conn);
  176. monitor_conn = NULL;
  177. }
  178. printf("Trying to connect to '%s'\n", cfile);
  179. ctrl_conn = wpa_ctrl_open(cfile);
  180. if (ctrl_conn == NULL) {
  181. free(cfile);
  182. return -1;
  183. }
  184. monitor_conn = wpa_ctrl_open(cfile);
  185. free(cfile);
  186. if (monitor_conn == NULL) {
  187. wpa_ctrl_close(ctrl_conn);
  188. return -1;
  189. }
  190. if (wpa_ctrl_attach(monitor_conn)) {
  191. printf("Failed to attach to wpa_supplicant\n");
  192. wpa_ctrl_close(monitor_conn);
  193. monitor_conn = NULL;
  194. wpa_ctrl_close(ctrl_conn);
  195. ctrl_conn = NULL;
  196. return -1;
  197. }
  198. #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
  199. msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn),
  200. QSocketNotifier::Read, this);
  201. connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs()));
  202. #endif
  203. adapterSelect->clear();
  204. adapterSelect->insertItem(ctrl_iface);
  205. adapterSelect->setCurrentItem(0);
  206. len = sizeof(buf) - 1;
  207. if (wpa_ctrl_request(ctrl_conn, "INTERFACES", 10, buf, &len, NULL) >= 0) {
  208. buf[len] = '\0';
  209. pos = buf;
  210. while (*pos) {
  211. pos2 = strchr(pos, '\n');
  212. if (pos2)
  213. *pos2 = '\0';
  214. if (strcmp(pos, ctrl_iface) != 0)
  215. adapterSelect->insertItem(pos);
  216. if (pos2)
  217. pos = pos2 + 1;
  218. else
  219. break;
  220. }
  221. }
  222. return 0;
  223. }
  224. static void wpa_gui_msg_cb(char *msg, size_t)
  225. {
  226. /* This should not happen anymore since two control connections are used. */
  227. printf("missed message: %s\n", msg);
  228. }
  229. int WpaGui::ctrlRequest(const char *cmd, char *buf, size_t *buflen)
  230. {
  231. int ret;
  232. if (ctrl_conn == NULL)
  233. return -3;
  234. ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, buflen,
  235. wpa_gui_msg_cb);
  236. if (ret == -2) {
  237. printf("'%s' command timed out.\n", cmd);
  238. } else if (ret < 0) {
  239. printf("'%s' command failed.\n", cmd);
  240. }
  241. return ret;
  242. }
  243. void WpaGui::updateStatus()
  244. {
  245. char buf[2048], *start, *end, *pos;
  246. size_t len;
  247. pingsToStatusUpdate = 10;
  248. len = sizeof(buf) - 1;
  249. if (ctrl_conn == NULL || ctrlRequest("STATUS", buf, &len) < 0) {
  250. textStatus->setText("Could not get status from wpa_supplicant");
  251. textAuthentication->clear();
  252. textEncryption->clear();
  253. textSsid->clear();
  254. textBssid->clear();
  255. textIpAddress->clear();
  256. return;
  257. }
  258. buf[len] = '\0';
  259. bool auth_updated = false, ssid_updated = false;
  260. bool bssid_updated = false, ipaddr_updated = false;
  261. bool status_updated = false;
  262. char *pairwise_cipher = NULL, *group_cipher = NULL;
  263. start = buf;
  264. while (*start) {
  265. bool last = false;
  266. end = strchr(start, '\n');
  267. if (end == NULL) {
  268. last = true;
  269. end = start;
  270. while (end[0] && end[1])
  271. end++;
  272. }
  273. *end = '\0';
  274. pos = strchr(start, '=');
  275. if (pos) {
  276. *pos++ = '\0';
  277. if (strcmp(start, "bssid") == 0) {
  278. bssid_updated = true;
  279. textBssid->setText(pos);
  280. } else if (strcmp(start, "ssid") == 0) {
  281. ssid_updated = true;
  282. textSsid->setText(pos);
  283. } else if (strcmp(start, "ip_address") == 0) {
  284. ipaddr_updated = true;
  285. textIpAddress->setText(pos);
  286. } else if (strcmp(start, "wpa_state") == 0) {
  287. status_updated = true;
  288. textStatus->setText(pos);
  289. } else if (strcmp(start, "key_mgmt") == 0) {
  290. auth_updated = true;
  291. textAuthentication->setText(pos);
  292. /* TODO: could add EAP status to this */
  293. } else if (strcmp(start, "pairwise_cipher") == 0) {
  294. pairwise_cipher = pos;
  295. } else if (strcmp(start, "group_cipher") == 0) {
  296. group_cipher = pos;
  297. }
  298. }
  299. if (last)
  300. break;
  301. start = end + 1;
  302. }
  303. if (pairwise_cipher || group_cipher) {
  304. QString encr;
  305. if (pairwise_cipher && group_cipher &&
  306. strcmp(pairwise_cipher, group_cipher) != 0) {
  307. encr.append(pairwise_cipher);
  308. encr.append(" + ");
  309. encr.append(group_cipher);
  310. } else if (pairwise_cipher) {
  311. encr.append(pairwise_cipher);
  312. } else {
  313. encr.append(group_cipher);
  314. encr.append(" [group key only]");
  315. }
  316. textEncryption->setText(encr);
  317. } else
  318. textEncryption->clear();
  319. if (!status_updated)
  320. textStatus->clear();
  321. if (!auth_updated)
  322. textAuthentication->clear();
  323. if (!ssid_updated)
  324. textSsid->clear();
  325. if (!bssid_updated)
  326. textBssid->clear();
  327. if (!ipaddr_updated)
  328. textIpAddress->clear();
  329. }
  330. void WpaGui::updateNetworks()
  331. {
  332. char buf[2048], *start, *end, *id, *ssid, *bssid, *flags;
  333. size_t len;
  334. int first_active = -1;
  335. bool selected = false;
  336. if (!networkMayHaveChanged)
  337. return;
  338. networkSelect->clear();
  339. if (ctrl_conn == NULL)
  340. return;
  341. len = sizeof(buf) - 1;
  342. if (ctrlRequest("LIST_NETWORKS", buf, &len) < 0)
  343. return;
  344. buf[len] = '\0';
  345. start = strchr(buf, '\n');
  346. if (start == NULL)
  347. return;
  348. start++;
  349. while (*start) {
  350. bool last = false;
  351. end = strchr(start, '\n');
  352. if (end == NULL) {
  353. last = true;
  354. end = start;
  355. while (end[0] && end[1])
  356. end++;
  357. }
  358. *end = '\0';
  359. id = start;
  360. ssid = strchr(id, '\t');
  361. if (ssid == NULL)
  362. break;
  363. *ssid++ = '\0';
  364. bssid = strchr(ssid, '\t');
  365. if (bssid == NULL)
  366. break;
  367. *bssid++ = '\0';
  368. flags = strchr(bssid, '\t');
  369. if (flags == NULL)
  370. break;
  371. *flags++ = '\0';
  372. QString network(id);
  373. network.append(": ");
  374. network.append(ssid);
  375. networkSelect->insertItem(network);
  376. if (strstr(flags, "[CURRENT]")) {
  377. networkSelect->setCurrentItem(networkSelect->count() - 1);
  378. selected = true;
  379. } else if (first_active < 0 && strstr(flags, "[DISABLED]") == NULL)
  380. first_active = networkSelect->count() - 1;
  381. if (last)
  382. break;
  383. start = end + 1;
  384. }
  385. if (!selected && first_active >= 0)
  386. networkSelect->setCurrentItem(first_active);
  387. networkMayHaveChanged = false;
  388. }
  389. void WpaGui::helpIndex()
  390. {
  391. printf("helpIndex\n");
  392. }
  393. void WpaGui::helpContents()
  394. {
  395. printf("helpContents\n");
  396. }
  397. void WpaGui::helpAbout()
  398. {
  399. QMessageBox::about(this, "wpa_gui for wpa_supplicant",
  400. "Copyright (c) 2003-2008,\n"
  401. "Jouni Malinen <j@w1.fi>\n"
  402. "and contributors.\n"
  403. "\n"
  404. "This program is free software. You can\n"
  405. "distribute it and/or modify it under the terms of\n"
  406. "the GNU General Public License version 2.\n"
  407. "\n"
  408. "Alternatively, this software may be distributed\n"
  409. "under the terms of the BSD license.\n"
  410. "\n"
  411. "This product includes software developed\n"
  412. "by the OpenSSL Project for use in the\n"
  413. "OpenSSL Toolkit (http://www.openssl.org/)\n");
  414. }
  415. void WpaGui::disconnect()
  416. {
  417. char reply[10];
  418. size_t reply_len = sizeof(reply);
  419. ctrlRequest("DISCONNECT", reply, &reply_len);
  420. }
  421. void WpaGui::scan()
  422. {
  423. if (scanres) {
  424. scanres->close();
  425. delete scanres;
  426. }
  427. scanres = new ScanResults();
  428. if (scanres == NULL)
  429. return;
  430. scanres->setWpaGui(this);
  431. scanres->show();
  432. scanres->exec();
  433. }
  434. void WpaGui::eventHistory()
  435. {
  436. if (eh) {
  437. eh->close();
  438. delete eh;
  439. }
  440. eh = new EventHistory();
  441. if (eh == NULL)
  442. return;
  443. eh->addEvents(msgs);
  444. eh->show();
  445. eh->exec();
  446. }
  447. void WpaGui::ping()
  448. {
  449. char buf[10];
  450. size_t len;
  451. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  452. /*
  453. * QSocketNotifier cannot be used with Windows named pipes, so use a timer
  454. * to check for received messages for now. This could be optimized be doing
  455. * something specific to named pipes or Windows events, but it is not clear
  456. * what would be the best way of doing that in Qt.
  457. */
  458. receiveMsgs();
  459. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  460. if (scanres && !scanres->isVisible()) {
  461. delete scanres;
  462. scanres = NULL;
  463. }
  464. if (eh && !eh->isVisible()) {
  465. delete eh;
  466. eh = NULL;
  467. }
  468. if (udr && !udr->isVisible()) {
  469. delete udr;
  470. udr = NULL;
  471. }
  472. len = sizeof(buf) - 1;
  473. if (ctrlRequest("PING", buf, &len) < 0) {
  474. printf("PING failed - trying to reconnect\n");
  475. if (openCtrlConnection(ctrl_iface) >= 0) {
  476. printf("Reconnected successfully\n");
  477. pingsToStatusUpdate = 0;
  478. }
  479. }
  480. pingsToStatusUpdate--;
  481. if (pingsToStatusUpdate <= 0) {
  482. updateStatus();
  483. updateNetworks();
  484. }
  485. }
  486. static int str_match(const char *a, const char *b)
  487. {
  488. return strncmp(a, b, strlen(b)) == 0;
  489. }
  490. void WpaGui::processMsg(char *msg)
  491. {
  492. char *pos = msg, *pos2;
  493. int priority = 2;
  494. if (*pos == '<') {
  495. /* skip priority */
  496. pos++;
  497. priority = atoi(pos);
  498. pos = strchr(pos, '>');
  499. if (pos)
  500. pos++;
  501. else
  502. pos = msg;
  503. }
  504. WpaMsg wm(pos, priority);
  505. if (eh)
  506. eh->addEvent(wm);
  507. msgs.append(wm);
  508. while (msgs.count() > 100)
  509. msgs.pop_front();
  510. /* Update last message with truncated version of the event */
  511. if (strncmp(pos, "CTRL-", 5) == 0) {
  512. pos2 = strchr(pos, str_match(pos, WPA_CTRL_REQ) ? ':' : ' ');
  513. if (pos2)
  514. pos2++;
  515. else
  516. pos2 = pos;
  517. } else
  518. pos2 = pos;
  519. QString lastmsg = pos2;
  520. lastmsg.truncate(40);
  521. textLastMessage->setText(lastmsg);
  522. pingsToStatusUpdate = 0;
  523. networkMayHaveChanged = true;
  524. if (str_match(pos, WPA_CTRL_REQ))
  525. processCtrlReq(pos + strlen(WPA_CTRL_REQ));
  526. }
  527. void WpaGui::processCtrlReq(const char *req)
  528. {
  529. if (udr) {
  530. udr->close();
  531. delete udr;
  532. }
  533. udr = new UserDataRequest();
  534. if (udr == NULL)
  535. return;
  536. if (udr->setParams(this, req) < 0) {
  537. delete udr;
  538. udr = NULL;
  539. return;
  540. }
  541. udr->show();
  542. udr->exec();
  543. }
  544. void WpaGui::receiveMsgs()
  545. {
  546. char buf[256];
  547. size_t len;
  548. while (monitor_conn && wpa_ctrl_pending(monitor_conn) > 0) {
  549. len = sizeof(buf) - 1;
  550. if (wpa_ctrl_recv(monitor_conn, buf, &len) == 0) {
  551. buf[len] = '\0';
  552. processMsg(buf);
  553. }
  554. }
  555. }
  556. void WpaGui::connectB()
  557. {
  558. char reply[10];
  559. size_t reply_len = sizeof(reply);
  560. ctrlRequest("REASSOCIATE", reply, &reply_len);
  561. }
  562. void WpaGui::selectNetwork( const QString &sel )
  563. {
  564. QString cmd(sel);
  565. char reply[10];
  566. size_t reply_len = sizeof(reply);
  567. int pos = cmd.find(':');
  568. if (pos < 0) {
  569. printf("Invalid selectNetwork '%s'\n", cmd.ascii());
  570. return;
  571. }
  572. cmd.truncate(pos);
  573. cmd.prepend("SELECT_NETWORK ");
  574. ctrlRequest(cmd.ascii(), reply, &reply_len);
  575. }
  576. void WpaGui::editNetwork()
  577. {
  578. QString sel(networkSelect->currentText());
  579. int pos = sel.find(':');
  580. if (pos < 0) {
  581. printf("Invalid selectNetwork '%s'\n", sel.ascii());
  582. return;
  583. }
  584. sel.truncate(pos);
  585. NetworkConfig *nc = new NetworkConfig();
  586. if (nc == NULL)
  587. return;
  588. nc->setWpaGui(this);
  589. nc->paramsFromConfig(sel.toInt());
  590. nc->show();
  591. nc->exec();
  592. }
  593. void WpaGui::triggerUpdate()
  594. {
  595. updateStatus();
  596. networkMayHaveChanged = true;
  597. updateNetworks();
  598. }
  599. void WpaGui::addNetwork()
  600. {
  601. NetworkConfig *nc = new NetworkConfig();
  602. if (nc == NULL)
  603. return;
  604. nc->setWpaGui(this);
  605. nc->newNetwork();
  606. nc->show();
  607. nc->exec();
  608. }
  609. void WpaGui::selectAdapter( const QString & sel )
  610. {
  611. if (openCtrlConnection(sel.ascii()) < 0)
  612. printf("Failed to open control connection to wpa_supplicant.\n");
  613. updateStatus();
  614. updateNetworks();
  615. }