wlantest_cli.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * wlantest controller
  3. * Copyright (c) 2010, 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 "utils/includes.h"
  15. #include <sys/un.h>
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "utils/edit.h"
  19. #include "wlantest_ctrl.h"
  20. static int get_cmd_arg_num(const char *str, int pos)
  21. {
  22. int arg = 0, i;
  23. for (i = 0; i <= pos; i++) {
  24. if (str[i] != ' ') {
  25. arg++;
  26. while (i <= pos && str[i] != ' ')
  27. i++;
  28. }
  29. }
  30. if (arg > 0)
  31. arg--;
  32. return arg;
  33. }
  34. static int get_prev_arg_pos(const char *str, int pos)
  35. {
  36. while (pos > 0 && str[pos - 1] != ' ')
  37. pos--;
  38. while (pos > 0 && str[pos - 1] == ' ')
  39. pos--;
  40. while (pos > 0 && str[pos - 1] != ' ')
  41. pos--;
  42. return pos;
  43. }
  44. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  45. size_t *len)
  46. {
  47. u8 *pos = buf;
  48. while (pos + 8 <= buf + buflen) {
  49. enum wlantest_ctrl_attr a;
  50. size_t alen;
  51. a = WPA_GET_BE32(pos);
  52. pos += 4;
  53. alen = WPA_GET_BE32(pos);
  54. pos += 4;
  55. if (pos + alen > buf + buflen) {
  56. printf("Invalid control message attribute\n");
  57. return NULL;
  58. }
  59. if (a == attr) {
  60. *len = alen;
  61. return pos;
  62. }
  63. pos += alen;
  64. }
  65. return NULL;
  66. }
  67. static u8 * attr_hdr_add(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  68. size_t len)
  69. {
  70. if (pos == NULL || end - pos < 8 + len)
  71. return NULL;
  72. WPA_PUT_BE32(pos, attr);
  73. pos += 4;
  74. WPA_PUT_BE32(pos, len);
  75. pos += 4;
  76. return pos;
  77. }
  78. static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  79. const char *str)
  80. {
  81. size_t len = os_strlen(str);
  82. if (pos == NULL || end - pos < 8 + len)
  83. return NULL;
  84. WPA_PUT_BE32(pos, attr);
  85. pos += 4;
  86. WPA_PUT_BE32(pos, len);
  87. pos += 4;
  88. os_memcpy(pos, str, len);
  89. pos += len;
  90. return pos;
  91. }
  92. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  93. u32 val)
  94. {
  95. if (pos == NULL || end - pos < 12)
  96. return NULL;
  97. WPA_PUT_BE32(pos, attr);
  98. pos += 4;
  99. WPA_PUT_BE32(pos, 4);
  100. pos += 4;
  101. WPA_PUT_BE32(pos, val);
  102. pos += 4;
  103. return pos;
  104. }
  105. static int cmd_send_and_recv(int s, const u8 *cmd, size_t cmd_len,
  106. u8 *resp, size_t max_resp_len)
  107. {
  108. int res;
  109. enum wlantest_ctrl_cmd cmd_resp;
  110. if (send(s, cmd, cmd_len, 0) < 0)
  111. return -1;
  112. res = recv(s, resp, max_resp_len, 0);
  113. if (res < 4)
  114. return -1;
  115. cmd_resp = WPA_GET_BE32(resp);
  116. if (cmd_resp == WLANTEST_CTRL_SUCCESS)
  117. return res;
  118. if (cmd_resp == WLANTEST_CTRL_UNKNOWN_CMD)
  119. printf("Unknown command\n");
  120. else if (cmd_resp == WLANTEST_CTRL_INVALID_CMD)
  121. printf("Invalid command\n");
  122. return -1;
  123. }
  124. static int cmd_simple(int s, enum wlantest_ctrl_cmd cmd)
  125. {
  126. u8 buf[4];
  127. int res;
  128. WPA_PUT_BE32(buf, cmd);
  129. res = cmd_send_and_recv(s, buf, sizeof(buf), buf, sizeof(buf));
  130. return res < 0 ? -1 : 0;
  131. }
  132. static char ** get_bssid_list(int s)
  133. {
  134. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  135. u8 buf[4];
  136. u8 *bssid;
  137. size_t len;
  138. int rlen, i;
  139. char **res;
  140. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  141. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  142. if (rlen < 0)
  143. return NULL;
  144. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  145. if (bssid == NULL)
  146. return NULL;
  147. res = os_zalloc((len / ETH_ALEN + 1) * sizeof(char *));
  148. if (res == NULL)
  149. return NULL;
  150. for (i = 0; i < len / ETH_ALEN; i++) {
  151. res[i] = os_zalloc(18);
  152. if (res[i] == NULL)
  153. break;
  154. os_snprintf(res[i], 18, MACSTR, MAC2STR(bssid + ETH_ALEN * i));
  155. }
  156. return res;
  157. }
  158. static char ** get_sta_list(int s, const u8 *bssid, int add_bcast)
  159. {
  160. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  161. u8 buf[100], *pos, *end;
  162. u8 *addr;
  163. size_t len;
  164. int rlen, i;
  165. char **res;
  166. pos = buf;
  167. end = buf + sizeof(buf);
  168. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  169. pos += 4;
  170. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  171. os_memcpy(pos, bssid, ETH_ALEN);
  172. pos += ETH_ALEN;
  173. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  174. if (rlen < 0)
  175. return NULL;
  176. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  177. if (addr == NULL)
  178. return NULL;
  179. res = os_zalloc((len / ETH_ALEN + 1 + add_bcast) * sizeof(char *));
  180. if (res == NULL)
  181. return NULL;
  182. for (i = 0; i < len / ETH_ALEN; i++) {
  183. res[i] = os_zalloc(18);
  184. if (res[i] == NULL)
  185. break;
  186. os_snprintf(res[i], 18, MACSTR, MAC2STR(addr + ETH_ALEN * i));
  187. }
  188. if (add_bcast)
  189. res[i] = os_strdup("ff:ff:ff:ff:ff:ff");
  190. return res;
  191. }
  192. static int cmd_ping(int s, int argc, char *argv[])
  193. {
  194. int res = cmd_simple(s, WLANTEST_CTRL_PING);
  195. if (res == 0)
  196. printf("PONG\n");
  197. return res == 0;
  198. }
  199. static int cmd_terminate(int s, int argc, char *argv[])
  200. {
  201. return cmd_simple(s, WLANTEST_CTRL_TERMINATE);
  202. }
  203. static int cmd_list_bss(int s, int argc, char *argv[])
  204. {
  205. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  206. u8 buf[4];
  207. u8 *bssid;
  208. size_t len;
  209. int rlen, i;
  210. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  211. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  212. if (rlen < 0)
  213. return -1;
  214. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  215. if (bssid == NULL)
  216. return -1;
  217. for (i = 0; i < len / ETH_ALEN; i++)
  218. printf(MACSTR " ", MAC2STR(bssid + ETH_ALEN * i));
  219. printf("\n");
  220. return 0;
  221. }
  222. static int cmd_list_sta(int s, int argc, char *argv[])
  223. {
  224. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  225. u8 buf[100], *pos;
  226. u8 *addr;
  227. size_t len;
  228. int rlen, i;
  229. if (argc < 1) {
  230. printf("list_sta needs one argument: BSSID\n");
  231. return -1;
  232. }
  233. pos = buf;
  234. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  235. pos += 4;
  236. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  237. pos += 4;
  238. WPA_PUT_BE32(pos, ETH_ALEN);
  239. pos += 4;
  240. if (hwaddr_aton(argv[0], pos) < 0) {
  241. printf("Invalid BSSID '%s'\n", argv[0]);
  242. return -1;
  243. }
  244. pos += ETH_ALEN;
  245. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  246. if (rlen < 0)
  247. return -1;
  248. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  249. if (addr == NULL)
  250. return -1;
  251. for (i = 0; i < len / ETH_ALEN; i++)
  252. printf(MACSTR " ", MAC2STR(addr + ETH_ALEN * i));
  253. printf("\n");
  254. return 0;
  255. }
  256. static char ** complete_list_sta(int s, const char *str, int pos)
  257. {
  258. if (get_cmd_arg_num(str, pos) == 1)
  259. return get_bssid_list(s);
  260. return NULL;
  261. }
  262. static int cmd_flush(int s, int argc, char *argv[])
  263. {
  264. return cmd_simple(s, WLANTEST_CTRL_FLUSH);
  265. }
  266. static int cmd_clear_sta_counters(int s, int argc, char *argv[])
  267. {
  268. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  269. u8 buf[100], *pos;
  270. int rlen;
  271. if (argc < 2) {
  272. printf("clear_sta_counters needs two arguments: BSSID and "
  273. "STA address\n");
  274. return -1;
  275. }
  276. pos = buf;
  277. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_STA_COUNTERS);
  278. pos += 4;
  279. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  280. pos += 4;
  281. WPA_PUT_BE32(pos, ETH_ALEN);
  282. pos += 4;
  283. if (hwaddr_aton(argv[0], pos) < 0) {
  284. printf("Invalid BSSID '%s'\n", argv[0]);
  285. return -1;
  286. }
  287. pos += ETH_ALEN;
  288. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  289. pos += 4;
  290. WPA_PUT_BE32(pos, ETH_ALEN);
  291. pos += 4;
  292. if (hwaddr_aton(argv[1], pos) < 0) {
  293. printf("Invalid STA address '%s'\n", argv[1]);
  294. return -1;
  295. }
  296. pos += ETH_ALEN;
  297. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  298. if (rlen < 0)
  299. return -1;
  300. printf("OK\n");
  301. return 0;
  302. }
  303. static char ** complete_clear_sta_counters(int s, const char *str, int pos)
  304. {
  305. int arg = get_cmd_arg_num(str, pos);
  306. char **res = NULL;
  307. u8 addr[ETH_ALEN];
  308. switch (arg) {
  309. case 1:
  310. res = get_bssid_list(s);
  311. break;
  312. case 2:
  313. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  314. break;
  315. res = get_sta_list(s, addr, 0);
  316. break;
  317. }
  318. return res;
  319. }
  320. static int cmd_clear_bss_counters(int s, int argc, char *argv[])
  321. {
  322. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  323. u8 buf[100], *pos;
  324. int rlen;
  325. if (argc < 1) {
  326. printf("clear_bss_counters needs one argument: BSSID\n");
  327. return -1;
  328. }
  329. pos = buf;
  330. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_BSS_COUNTERS);
  331. pos += 4;
  332. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  333. pos += 4;
  334. WPA_PUT_BE32(pos, ETH_ALEN);
  335. pos += 4;
  336. if (hwaddr_aton(argv[0], pos) < 0) {
  337. printf("Invalid BSSID '%s'\n", argv[0]);
  338. return -1;
  339. }
  340. pos += ETH_ALEN;
  341. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  342. if (rlen < 0)
  343. return -1;
  344. printf("OK\n");
  345. return 0;
  346. }
  347. static char ** complete_clear_bss_counters(int s, const char *str, int pos)
  348. {
  349. if (get_cmd_arg_num(str, pos) == 1)
  350. return get_bssid_list(s);
  351. return NULL;
  352. }
  353. struct sta_counters {
  354. const char *name;
  355. enum wlantest_sta_counter num;
  356. };
  357. static const struct sta_counters sta_counters[] = {
  358. { "auth_tx", WLANTEST_STA_COUNTER_AUTH_TX },
  359. { "auth_rx", WLANTEST_STA_COUNTER_AUTH_RX },
  360. { "assocreq_tx", WLANTEST_STA_COUNTER_ASSOCREQ_TX },
  361. { "reassocreq_tx", WLANTEST_STA_COUNTER_REASSOCREQ_TX },
  362. { "ptk_learned", WLANTEST_STA_COUNTER_PTK_LEARNED },
  363. { "valid_deauth_tx", WLANTEST_STA_COUNTER_VALID_DEAUTH_TX },
  364. { "valid_deauth_rx", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX },
  365. { "invalid_deauth_tx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX },
  366. { "invalid_deauth_rx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX },
  367. { "valid_disassoc_tx", WLANTEST_STA_COUNTER_VALID_DISASSOC_TX },
  368. { "valid_disassoc_rx", WLANTEST_STA_COUNTER_VALID_DISASSOC_RX },
  369. { "invalid_disassoc_tx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX },
  370. { "invalid_disassoc_rx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX },
  371. { "valid_saqueryreq_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX },
  372. { "valid_saqueryreq_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX },
  373. { "invalid_saqueryreq_tx",
  374. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX },
  375. { "invalid_saqueryreq_rx",
  376. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX },
  377. { "valid_saqueryresp_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX },
  378. { "valid_saqueryresp_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX },
  379. { "invalid_saqueryresp_tx",
  380. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX },
  381. { "invalid_saqueryresp_rx",
  382. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX },
  383. { "ping_ok", WLANTEST_STA_COUNTER_PING_OK },
  384. { NULL, 0 }
  385. };
  386. static int cmd_get_sta_counter(int s, int argc, char *argv[])
  387. {
  388. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  389. u8 buf[100], *end, *pos;
  390. int rlen, i;
  391. size_t len;
  392. if (argc != 3) {
  393. printf("get_sta_counter needs at three arguments: "
  394. "counter name, BSSID, and STA address\n");
  395. return -1;
  396. }
  397. pos = buf;
  398. end = buf + sizeof(buf);
  399. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_STA_COUNTER);
  400. pos += 4;
  401. for (i = 0; sta_counters[i].name; i++) {
  402. if (os_strcasecmp(sta_counters[i].name, argv[0]) == 0)
  403. break;
  404. }
  405. if (sta_counters[i].name == NULL) {
  406. printf("Unknown STA counter '%s'\n", argv[0]);
  407. printf("Counters:");
  408. for (i = 0; sta_counters[i].name; i++)
  409. printf(" %s", sta_counters[i].name);
  410. printf("\n");
  411. return -1;
  412. }
  413. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_COUNTER,
  414. sta_counters[i].num);
  415. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  416. if (hwaddr_aton(argv[1], pos) < 0) {
  417. printf("Invalid BSSID '%s'\n", argv[1]);
  418. return -1;
  419. }
  420. pos += ETH_ALEN;
  421. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  422. if (hwaddr_aton(argv[2], pos) < 0) {
  423. printf("Invalid STA address '%s'\n", argv[2]);
  424. return -1;
  425. }
  426. pos += ETH_ALEN;
  427. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  428. if (rlen < 0)
  429. return -1;
  430. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  431. if (pos == NULL || len != 4)
  432. return -1;
  433. printf("%u\n", WPA_GET_BE32(pos));
  434. return 0;
  435. }
  436. static char ** complete_get_sta_counter(int s, const char *str, int pos)
  437. {
  438. int arg = get_cmd_arg_num(str, pos);
  439. char **res = NULL;
  440. int i, count;
  441. u8 addr[ETH_ALEN];
  442. switch (arg) {
  443. case 1:
  444. /* counter list */
  445. count = sizeof(sta_counters) / sizeof(sta_counters[0]);
  446. res = os_zalloc(count * sizeof(char *));
  447. if (res == NULL)
  448. return NULL;
  449. for (i = 0; sta_counters[i].name; i++) {
  450. res[i] = os_strdup(sta_counters[i].name);
  451. if (res[i] == NULL)
  452. break;
  453. }
  454. break;
  455. case 2:
  456. res = get_bssid_list(s);
  457. break;
  458. case 3:
  459. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  460. break;
  461. res = get_sta_list(s, addr, 0);
  462. break;
  463. }
  464. return res;
  465. }
  466. struct bss_counters {
  467. const char *name;
  468. enum wlantest_bss_counter num;
  469. };
  470. static const struct bss_counters bss_counters[] = {
  471. { "valid_bip_mmie", WLANTEST_BSS_COUNTER_VALID_BIP_MMIE },
  472. { "invalid_bip_mmie", WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE },
  473. { "missing_bip_mmie", WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE },
  474. { NULL, 0 }
  475. };
  476. static int cmd_get_bss_counter(int s, int argc, char *argv[])
  477. {
  478. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  479. u8 buf[100], *end, *pos;
  480. int rlen, i;
  481. size_t len;
  482. if (argc != 2) {
  483. printf("get_bss_counter needs at two arguments: "
  484. "counter name and BSSID\n");
  485. return -1;
  486. }
  487. pos = buf;
  488. end = buf + sizeof(buf);
  489. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_BSS_COUNTER);
  490. pos += 4;
  491. for (i = 0; bss_counters[i].name; i++) {
  492. if (os_strcasecmp(bss_counters[i].name, argv[0]) == 0)
  493. break;
  494. }
  495. if (bss_counters[i].name == NULL) {
  496. printf("Unknown BSS counter '%s'\n", argv[0]);
  497. printf("Counters:");
  498. for (i = 0; bss_counters[i].name; i++)
  499. printf(" %s", bss_counters[i].name);
  500. printf("\n");
  501. return -1;
  502. }
  503. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_COUNTER,
  504. bss_counters[i].num);
  505. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  506. if (hwaddr_aton(argv[1], pos) < 0) {
  507. printf("Invalid BSSID '%s'\n", argv[1]);
  508. return -1;
  509. }
  510. pos += ETH_ALEN;
  511. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  512. if (rlen < 0)
  513. return -1;
  514. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  515. if (pos == NULL || len != 4)
  516. return -1;
  517. printf("%u\n", WPA_GET_BE32(pos));
  518. return 0;
  519. }
  520. static char ** complete_get_bss_counter(int s, const char *str, int pos)
  521. {
  522. int arg = get_cmd_arg_num(str, pos);
  523. char **res = NULL;
  524. int i, count;
  525. switch (arg) {
  526. case 1:
  527. /* counter list */
  528. count = sizeof(bss_counters) / sizeof(bss_counters[0]);
  529. res = os_zalloc(count * sizeof(char *));
  530. if (res == NULL)
  531. return NULL;
  532. for (i = 0; bss_counters[i].name; i++) {
  533. res[i] = os_strdup(bss_counters[i].name);
  534. if (res[i] == NULL)
  535. break;
  536. }
  537. break;
  538. case 2:
  539. res = get_bssid_list(s);
  540. break;
  541. }
  542. return res;
  543. }
  544. struct inject_frames {
  545. const char *name;
  546. enum wlantest_inject_frame frame;
  547. };
  548. static const struct inject_frames inject_frames[] = {
  549. { "auth", WLANTEST_FRAME_AUTH },
  550. { "assocreq", WLANTEST_FRAME_ASSOCREQ },
  551. { "reassocreq", WLANTEST_FRAME_REASSOCREQ },
  552. { "deauth", WLANTEST_FRAME_DEAUTH },
  553. { "disassoc", WLANTEST_FRAME_DISASSOC },
  554. { "saqueryreq", WLANTEST_FRAME_SAQUERYREQ },
  555. { NULL, 0 }
  556. };
  557. static int cmd_inject(int s, int argc, char *argv[])
  558. {
  559. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  560. u8 buf[100], *end, *pos;
  561. int rlen, i;
  562. enum wlantest_inject_protection prot;
  563. /* <frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff> */
  564. if (argc < 5) {
  565. printf("inject needs five arguments: frame, protection, "
  566. "sender, BSSID, STA/ff:ff:ff:ff:ff:ff\n");
  567. return -1;
  568. }
  569. pos = buf;
  570. end = buf + sizeof(buf);
  571. WPA_PUT_BE32(pos, WLANTEST_CTRL_INJECT);
  572. pos += 4;
  573. for (i = 0; inject_frames[i].name; i++) {
  574. if (os_strcasecmp(inject_frames[i].name, argv[0]) == 0)
  575. break;
  576. }
  577. if (inject_frames[i].name == NULL) {
  578. printf("Unknown inject frame '%s'\n", argv[0]);
  579. printf("Frames:");
  580. for (i = 0; inject_frames[i].name; i++)
  581. printf(" %s", inject_frames[i].name);
  582. printf("\n");
  583. return -1;
  584. }
  585. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_FRAME,
  586. inject_frames[i].frame);
  587. if (os_strcasecmp(argv[1], "normal") == 0)
  588. prot = WLANTEST_INJECT_NORMAL;
  589. else if (os_strcasecmp(argv[1], "protected") == 0)
  590. prot = WLANTEST_INJECT_PROTECTED;
  591. else if (os_strcasecmp(argv[1], "unprotected") == 0)
  592. prot = WLANTEST_INJECT_UNPROTECTED;
  593. else if (os_strcasecmp(argv[1], "incorrect") == 0)
  594. prot = WLANTEST_INJECT_INCORRECT_KEY;
  595. else {
  596. printf("Unknown protection type '%s'\n", argv[1]);
  597. printf("Protection types: normal protected unprotected "
  598. "incorrect\n");
  599. return -1;
  600. }
  601. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  602. if (os_strcasecmp(argv[2], "ap") == 0) {
  603. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  604. 1);
  605. } else if (os_strcasecmp(argv[2], "sta") == 0) {
  606. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  607. 0);
  608. } else {
  609. printf("Unknown sender '%s'\n", argv[2]);
  610. printf("Sender types: ap sta\n");
  611. return -1;
  612. }
  613. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  614. if (hwaddr_aton(argv[3], pos) < 0) {
  615. printf("Invalid BSSID '%s'\n", argv[3]);
  616. return -1;
  617. }
  618. pos += ETH_ALEN;
  619. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  620. if (hwaddr_aton(argv[4], pos) < 0) {
  621. printf("Invalid STA '%s'\n", argv[4]);
  622. return -1;
  623. }
  624. pos += ETH_ALEN;
  625. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  626. if (rlen < 0)
  627. return -1;
  628. printf("OK\n");
  629. return 0;
  630. }
  631. static char ** complete_inject(int s, const char *str, int pos)
  632. {
  633. int arg = get_cmd_arg_num(str, pos);
  634. char **res = NULL;
  635. int i, count;
  636. u8 addr[ETH_ALEN];
  637. switch (arg) {
  638. case 1:
  639. /* frame list */
  640. count = sizeof(inject_frames) / sizeof(inject_frames[0]);
  641. res = os_zalloc(count * sizeof(char *));
  642. if (res == NULL)
  643. break;
  644. for (i = 0; inject_frames[i].name; i++) {
  645. res[i] = os_strdup(inject_frames[i].name);
  646. if (res[i] == NULL)
  647. break;
  648. }
  649. break;
  650. case 2:
  651. res = os_zalloc(5 * sizeof(char *));
  652. if (res == NULL)
  653. break;
  654. res[0] = os_strdup("normal");
  655. if (res[0] == NULL)
  656. break;
  657. res[1] = os_strdup("protected");
  658. if (res[1] == NULL)
  659. break;
  660. res[2] = os_strdup("unprotected");
  661. if (res[2] == NULL)
  662. break;
  663. res[3] = os_strdup("incorrect");
  664. if (res[3] == NULL)
  665. break;
  666. break;
  667. case 3:
  668. res = os_zalloc(3 * sizeof(char *));
  669. if (res == NULL)
  670. break;
  671. res[0] = os_strdup("ap");
  672. if (res[0] == NULL)
  673. break;
  674. res[1] = os_strdup("sta");
  675. if (res[1] == NULL)
  676. break;
  677. break;
  678. case 4:
  679. res = get_bssid_list(s);
  680. break;
  681. case 5:
  682. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  683. break;
  684. res = get_sta_list(s, addr, 1);
  685. break;
  686. }
  687. return res;
  688. }
  689. static int cmd_version(int s, int argc, char *argv[])
  690. {
  691. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  692. u8 buf[4];
  693. char *version;
  694. size_t len;
  695. int rlen, i;
  696. WPA_PUT_BE32(buf, WLANTEST_CTRL_VERSION);
  697. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  698. if (rlen < 0)
  699. return -1;
  700. version = (char *) attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_VERSION,
  701. &len);
  702. if (version == NULL)
  703. return -1;
  704. for (i = 0; i < len; i++)
  705. putchar(version[i]);
  706. printf("\n");
  707. return 0;
  708. }
  709. static int cmd_add_passphrase(int s, int argc, char *argv[])
  710. {
  711. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  712. u8 buf[100], *pos, *end;
  713. size_t len;
  714. int rlen;
  715. if (argc < 1) {
  716. printf("add_passphrase needs one argument: passphrase\n");
  717. return -1;
  718. }
  719. len = os_strlen(argv[0]);
  720. if (len < 8 || len > 63) {
  721. printf("Invalid passphrase '%s'\n", argv[0]);
  722. return -1;
  723. }
  724. pos = buf;
  725. end = buf + sizeof(buf);
  726. WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE);
  727. pos += 4;
  728. pos = attr_add_str(pos, end, WLANTEST_ATTR_PASSPHRASE,
  729. argv[0]);
  730. if (argc > 1) {
  731. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  732. if (hwaddr_aton(argv[1], pos) < 0) {
  733. printf("Invalid BSSID '%s'\n", argv[3]);
  734. return -1;
  735. }
  736. pos += ETH_ALEN;
  737. }
  738. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  739. if (rlen < 0)
  740. return -1;
  741. return 0;
  742. }
  743. struct sta_infos {
  744. const char *name;
  745. enum wlantest_sta_info num;
  746. };
  747. static const struct sta_infos sta_infos[] = {
  748. { "proto", WLANTEST_STA_INFO_PROTO },
  749. { "pairwise", WLANTEST_STA_INFO_PAIRWISE },
  750. { "key_mgmt", WLANTEST_STA_INFO_KEY_MGMT },
  751. { "rsn_capab", WLANTEST_STA_INFO_RSN_CAPAB },
  752. { "state", WLANTEST_STA_INFO_STATE },
  753. { NULL, 0 }
  754. };
  755. static int cmd_info_sta(int s, int argc, char *argv[])
  756. {
  757. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  758. u8 buf[100], *end, *pos;
  759. int rlen, i;
  760. size_t len;
  761. char info[100];
  762. if (argc != 3) {
  763. printf("sta_info needs at three arguments: "
  764. "counter name, BSSID, and STA address\n");
  765. return -1;
  766. }
  767. pos = buf;
  768. end = buf + sizeof(buf);
  769. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_STA);
  770. pos += 4;
  771. for (i = 0; sta_infos[i].name; i++) {
  772. if (os_strcasecmp(sta_infos[i].name, argv[0]) == 0)
  773. break;
  774. }
  775. if (sta_infos[i].name == NULL) {
  776. printf("Unknown STA info '%s'\n", argv[0]);
  777. printf("Info fields:");
  778. for (i = 0; sta_infos[i].name; i++)
  779. printf(" %s", sta_infos[i].name);
  780. printf("\n");
  781. return -1;
  782. }
  783. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_INFO,
  784. sta_infos[i].num);
  785. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  786. if (hwaddr_aton(argv[1], pos) < 0) {
  787. printf("Invalid BSSID '%s'\n", argv[1]);
  788. return -1;
  789. }
  790. pos += ETH_ALEN;
  791. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  792. if (hwaddr_aton(argv[2], pos) < 0) {
  793. printf("Invalid STA address '%s'\n", argv[2]);
  794. return -1;
  795. }
  796. pos += ETH_ALEN;
  797. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  798. if (rlen < 0)
  799. return -1;
  800. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  801. if (pos == NULL)
  802. return -1;
  803. if (len >= sizeof(info))
  804. len = sizeof(info) - 1;
  805. os_memcpy(info, pos, len);
  806. info[len] = '\0';
  807. printf("%s\n", info);
  808. return 0;
  809. }
  810. static char ** complete_info_sta(int s, const char *str, int pos)
  811. {
  812. int arg = get_cmd_arg_num(str, pos);
  813. char **res = NULL;
  814. int i, count;
  815. u8 addr[ETH_ALEN];
  816. switch (arg) {
  817. case 1:
  818. /* counter list */
  819. count = sizeof(sta_infos) / sizeof(sta_infos[0]);
  820. res = os_zalloc(count * sizeof(char *));
  821. if (res == NULL)
  822. return NULL;
  823. for (i = 0; sta_infos[i].name; i++) {
  824. res[i] = os_strdup(sta_infos[i].name);
  825. if (res[i] == NULL)
  826. break;
  827. }
  828. break;
  829. case 2:
  830. res = get_bssid_list(s);
  831. break;
  832. case 3:
  833. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  834. break;
  835. res = get_sta_list(s, addr, 0);
  836. break;
  837. }
  838. return res;
  839. }
  840. struct bss_infos {
  841. const char *name;
  842. enum wlantest_bss_info num;
  843. };
  844. static const struct bss_infos bss_infos[] = {
  845. { "proto", WLANTEST_BSS_INFO_PROTO },
  846. { "pairwise", WLANTEST_BSS_INFO_PAIRWISE },
  847. { "group", WLANTEST_BSS_INFO_GROUP },
  848. { "group_mgmt", WLANTEST_BSS_INFO_GROUP_MGMT },
  849. { "key_mgmt", WLANTEST_BSS_INFO_KEY_MGMT },
  850. { "rsn_capab", WLANTEST_BSS_INFO_RSN_CAPAB },
  851. { NULL, 0 }
  852. };
  853. static int cmd_info_bss(int s, int argc, char *argv[])
  854. {
  855. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  856. u8 buf[100], *end, *pos;
  857. int rlen, i;
  858. size_t len;
  859. char info[100];
  860. if (argc != 2) {
  861. printf("bss_info needs at two arguments: "
  862. "field name and BSSID\n");
  863. return -1;
  864. }
  865. pos = buf;
  866. end = buf + sizeof(buf);
  867. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_BSS);
  868. pos += 4;
  869. for (i = 0; bss_infos[i].name; i++) {
  870. if (os_strcasecmp(bss_infos[i].name, argv[0]) == 0)
  871. break;
  872. }
  873. if (bss_infos[i].name == NULL) {
  874. printf("Unknown BSS info '%s'\n", argv[0]);
  875. printf("Info fields:");
  876. for (i = 0; bss_infos[i].name; i++)
  877. printf(" %s", bss_infos[i].name);
  878. printf("\n");
  879. return -1;
  880. }
  881. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_INFO,
  882. bss_infos[i].num);
  883. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  884. if (hwaddr_aton(argv[1], pos) < 0) {
  885. printf("Invalid BSSID '%s'\n", argv[1]);
  886. return -1;
  887. }
  888. pos += ETH_ALEN;
  889. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  890. if (rlen < 0)
  891. return -1;
  892. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  893. if (pos == NULL)
  894. return -1;
  895. if (len >= sizeof(info))
  896. len = sizeof(info) - 1;
  897. os_memcpy(info, pos, len);
  898. info[len] = '\0';
  899. printf("%s\n", info);
  900. return 0;
  901. }
  902. static char ** complete_info_bss(int s, const char *str, int pos)
  903. {
  904. int arg = get_cmd_arg_num(str, pos);
  905. char **res = NULL;
  906. int i, count;
  907. switch (arg) {
  908. case 1:
  909. /* counter list */
  910. count = sizeof(bss_infos) / sizeof(bss_infos[0]);
  911. res = os_zalloc(count * sizeof(char *));
  912. if (res == NULL)
  913. return NULL;
  914. for (i = 0; bss_infos[i].name; i++) {
  915. res[i] = os_strdup(bss_infos[i].name);
  916. if (res[i] == NULL)
  917. break;
  918. }
  919. break;
  920. case 2:
  921. res = get_bssid_list(s);
  922. break;
  923. }
  924. return res;
  925. }
  926. struct wlantest_cli_cmd {
  927. const char *cmd;
  928. int (*handler)(int s, int argc, char *argv[]);
  929. const char *usage;
  930. char ** (*complete)(int s, const char *str, int pos);
  931. };
  932. static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
  933. { "ping", cmd_ping, "= test connection to wlantest", NULL },
  934. { "terminate", cmd_terminate, "= terminate wlantest", NULL },
  935. { "list_bss", cmd_list_bss, "= get BSS list", NULL },
  936. { "list_sta", cmd_list_sta, "<BSSID> = get STA list",
  937. complete_list_sta },
  938. { "flush", cmd_flush, "= drop all collected BSS data", NULL },
  939. { "clear_sta_counters", cmd_clear_sta_counters,
  940. "<BSSID> <STA> = clear STA counters", complete_clear_sta_counters },
  941. { "clear_bss_counters", cmd_clear_bss_counters,
  942. "<BSSID> = clear BSS counters", complete_clear_bss_counters },
  943. { "get_sta_counter", cmd_get_sta_counter,
  944. "<counter> <BSSID> <STA> = get STA counter value",
  945. complete_get_sta_counter },
  946. { "get_bss_counter", cmd_get_bss_counter,
  947. "<counter> <BSSID> = get BSS counter value",
  948. complete_get_bss_counter },
  949. { "inject", cmd_inject,
  950. "<frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff>",
  951. complete_inject },
  952. { "version", cmd_version, "= get wlantest version", NULL },
  953. { "add_passphrase", cmd_add_passphrase,
  954. "<passphrase> = add a known passphrase", NULL },
  955. { "info_sta", cmd_info_sta,
  956. "<field> <BSSID> <STA> = get STA information",
  957. complete_info_sta },
  958. { "info_bss", cmd_info_bss,
  959. "<field> <BSSID> = get BSS information",
  960. complete_info_bss },
  961. { NULL, NULL, NULL, NULL }
  962. };
  963. static int ctrl_command(int s, int argc, char *argv[])
  964. {
  965. const struct wlantest_cli_cmd *cmd, *match = NULL;
  966. int count = 0;
  967. int ret = 0;
  968. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  969. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  970. {
  971. match = cmd;
  972. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  973. /* exact match */
  974. count = 1;
  975. break;
  976. }
  977. count++;
  978. }
  979. }
  980. if (count > 1) {
  981. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  982. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  983. if (os_strncasecmp(cmd->cmd, argv[0],
  984. os_strlen(argv[0])) == 0) {
  985. printf(" %s", cmd->cmd);
  986. }
  987. }
  988. printf("\n");
  989. ret = 1;
  990. } else if (count == 0) {
  991. printf("Unknown command '%s'\n", argv[0]);
  992. ret = 1;
  993. } else {
  994. ret = match->handler(s, argc - 1, &argv[1]);
  995. }
  996. return ret;
  997. }
  998. struct wlantest_cli {
  999. int s;
  1000. };
  1001. #define max_args 10
  1002. static int tokenize_cmd(char *cmd, char *argv[])
  1003. {
  1004. char *pos;
  1005. int argc = 0;
  1006. pos = cmd;
  1007. for (;;) {
  1008. while (*pos == ' ')
  1009. pos++;
  1010. if (*pos == '\0')
  1011. break;
  1012. argv[argc] = pos;
  1013. argc++;
  1014. if (argc == max_args)
  1015. break;
  1016. if (*pos == '"') {
  1017. char *pos2 = os_strrchr(pos, '"');
  1018. if (pos2)
  1019. pos = pos2 + 1;
  1020. }
  1021. while (*pos != '\0' && *pos != ' ')
  1022. pos++;
  1023. if (*pos == ' ')
  1024. *pos++ = '\0';
  1025. }
  1026. return argc;
  1027. }
  1028. static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd)
  1029. {
  1030. struct wlantest_cli *cli = ctx;
  1031. char *argv[max_args];
  1032. int argc;
  1033. argc = tokenize_cmd(cmd, argv);
  1034. if (argc) {
  1035. int ret = ctrl_command(cli->s, argc, argv);
  1036. if (ret < 0)
  1037. printf("FAIL\n");
  1038. }
  1039. }
  1040. static void wlantest_cli_eloop_terminate(int sig, void *signal_ctx)
  1041. {
  1042. eloop_terminate();
  1043. }
  1044. static void wlantest_cli_edit_eof_cb(void *ctx)
  1045. {
  1046. eloop_terminate();
  1047. }
  1048. static char ** wlantest_cli_cmd_list(void)
  1049. {
  1050. char **res;
  1051. int i, count;
  1052. count = sizeof(wlantest_cli_commands) /
  1053. sizeof(wlantest_cli_commands[0]);
  1054. res = os_zalloc(count * sizeof(char *));
  1055. if (res == NULL)
  1056. return NULL;
  1057. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1058. res[i] = os_strdup(wlantest_cli_commands[i].cmd);
  1059. if (res[i] == NULL)
  1060. break;
  1061. }
  1062. return res;
  1063. }
  1064. static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli,
  1065. const char *cmd, const char *str,
  1066. int pos)
  1067. {
  1068. int i;
  1069. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1070. const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i];
  1071. if (os_strcasecmp(c->cmd, cmd) == 0) {
  1072. edit_clear_line();
  1073. printf("\r%s\n", c->usage);
  1074. edit_redraw();
  1075. if (c->complete)
  1076. return c->complete(cli->s, str, pos);
  1077. break;
  1078. }
  1079. }
  1080. return NULL;
  1081. }
  1082. static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str,
  1083. int pos)
  1084. {
  1085. struct wlantest_cli *cli = ctx;
  1086. char **res;
  1087. const char *end;
  1088. char *cmd;
  1089. end = os_strchr(str, ' ');
  1090. if (end == NULL || str + pos < end)
  1091. return wlantest_cli_cmd_list();
  1092. cmd = os_malloc(pos + 1);
  1093. if (cmd == NULL)
  1094. return NULL;
  1095. os_memcpy(cmd, str, pos);
  1096. cmd[end - str] = '\0';
  1097. res = wlantest_cli_cmd_completion(cli, cmd, str, pos);
  1098. os_free(cmd);
  1099. return res;
  1100. }
  1101. static void wlantest_cli_interactive(int s)
  1102. {
  1103. struct wlantest_cli cli;
  1104. char *home, *hfile = NULL;
  1105. if (eloop_init())
  1106. return;
  1107. home = getenv("HOME");
  1108. if (home) {
  1109. const char *fname = ".wlantest_cli_history";
  1110. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1111. hfile = os_malloc(hfile_len);
  1112. if (hfile)
  1113. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1114. }
  1115. cli.s = s;
  1116. eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
  1117. edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
  1118. wlantest_cli_edit_completion_cb, &cli, hfile);
  1119. eloop_run();
  1120. edit_deinit(hfile, NULL);
  1121. os_free(hfile);
  1122. eloop_destroy();
  1123. }
  1124. int main(int argc, char *argv[])
  1125. {
  1126. int s;
  1127. struct sockaddr_un addr;
  1128. int ret = 0;
  1129. if (os_program_init())
  1130. return -1;
  1131. s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  1132. if (s < 0) {
  1133. perror("socket");
  1134. return -1;
  1135. }
  1136. os_memset(&addr, 0, sizeof(addr));
  1137. addr.sun_family = AF_UNIX;
  1138. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  1139. sizeof(addr.sun_path) - 1);
  1140. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1141. perror("connect");
  1142. close(s);
  1143. return -1;
  1144. }
  1145. if (argc > 1) {
  1146. ret = ctrl_command(s, argc - 1, &argv[1]);
  1147. if (ret < 0)
  1148. printf("FAIL\n");
  1149. } else {
  1150. wlantest_cli_interactive(s);
  1151. }
  1152. close(s);
  1153. os_program_deinit();
  1154. return ret;
  1155. }