wpa_helpers.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * wpa_supplicant ctrl_iface helpers
  3. * Copyright (c) 2010-2011, Atheros Communications, Inc.
  4. * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include <time.h>
  11. #include "common.h"
  12. #include "wpa_ctrl.h"
  13. #include "wpa_helpers.h"
  14. char *wpas_ctrl_path = "/var/run/wpa_supplicant/";
  15. static int default_timeout = 60;
  16. static struct wpa_ctrl * wpa_open_ctrl(const char *ifname)
  17. {
  18. char buf[128];
  19. struct wpa_ctrl *ctrl;
  20. os_snprintf(buf, sizeof(buf), "%s%s", wpas_ctrl_path, ifname);
  21. ctrl = wpa_ctrl_open(buf);
  22. if (ctrl == NULL)
  23. printf("wpa_command: wpa_ctrl_open(%s) failed\n", buf);
  24. return ctrl;
  25. }
  26. int wpa_command(const char *ifname, const char *cmd)
  27. {
  28. struct wpa_ctrl *ctrl;
  29. char buf[128];
  30. size_t len;
  31. printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
  32. ctrl = wpa_open_ctrl(ifname);
  33. if (ctrl == NULL)
  34. return -1;
  35. len = sizeof(buf);
  36. if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
  37. printf("wpa_command: wpa_ctrl_request failed\n");
  38. wpa_ctrl_close(ctrl);
  39. return -1;
  40. }
  41. wpa_ctrl_close(ctrl);
  42. buf[len] = '\0';
  43. if (strncmp(buf, "FAIL", 4) == 0) {
  44. printf("wpa_command: Command failed (FAIL received)\n");
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. int wpa_command_resp(const char *ifname, const char *cmd,
  50. char *resp, size_t resp_size)
  51. {
  52. struct wpa_ctrl *ctrl;
  53. size_t len;
  54. printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
  55. ctrl = wpa_open_ctrl(ifname);
  56. if (ctrl == NULL)
  57. return -1;
  58. len = resp_size;
  59. if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), resp, &len, NULL) < 0) {
  60. printf("wpa_command: wpa_ctrl_request failed\n");
  61. wpa_ctrl_close(ctrl);
  62. return -1;
  63. }
  64. wpa_ctrl_close(ctrl);
  65. resp[len] = '\0';
  66. return 0;
  67. }
  68. struct wpa_ctrl * open_wpa_mon(const char *ifname)
  69. {
  70. struct wpa_ctrl *ctrl;
  71. ctrl = wpa_open_ctrl(ifname);
  72. if (ctrl == NULL)
  73. return NULL;
  74. if (wpa_ctrl_attach(ctrl) < 0) {
  75. wpa_ctrl_close(ctrl);
  76. return NULL;
  77. }
  78. return ctrl;
  79. }
  80. int get_wpa_cli_event2(struct wpa_ctrl *mon,
  81. const char *event, const char *event2,
  82. char *buf, size_t buf_size)
  83. {
  84. int fd, ret;
  85. fd_set rfd;
  86. char *pos;
  87. struct timeval tv;
  88. time_t start, now;
  89. printf("Waiting for wpa_cli event %s\n", event);
  90. fd = wpa_ctrl_get_fd(mon);
  91. if (fd < 0)
  92. return -1;
  93. time(&start);
  94. while (1) {
  95. size_t len;
  96. FD_ZERO(&rfd);
  97. FD_SET(fd, &rfd);
  98. tv.tv_sec = default_timeout;
  99. tv.tv_usec = 0;
  100. ret = select(fd + 1, &rfd, NULL, NULL, &tv);
  101. if (ret == 0) {
  102. printf("Timeout on waiting for event %s\n", event);
  103. return -1;
  104. }
  105. if (ret < 0) {
  106. printf("select: %s\n", strerror(errno));
  107. return -1;
  108. }
  109. len = buf_size;
  110. if (wpa_ctrl_recv(mon, buf, &len) < 0) {
  111. printf("Failure while waiting for event %s\n", event);
  112. return -1;
  113. }
  114. if (len == buf_size)
  115. len--;
  116. buf[len] = '\0';
  117. pos = strchr(buf, '>');
  118. if (pos &&
  119. (strncmp(pos + 1, event, strlen(event)) == 0 ||
  120. (event2 &&
  121. strncmp(pos + 1, event2, strlen(event2)) == 0)))
  122. return 0; /* Event found */
  123. time(&now);
  124. if ((int) (now - start) > default_timeout) {
  125. printf("Timeout on waiting for event %s\n", event);
  126. return -1;
  127. }
  128. }
  129. }
  130. int get_wpa_cli_event(struct wpa_ctrl *mon,
  131. const char *event, char *buf, size_t buf_size)
  132. {
  133. return get_wpa_cli_event2(mon, event, NULL, buf, buf_size);
  134. }
  135. int get_wpa_status(const char *ifname, const char *field, char *obuf,
  136. size_t obuf_size)
  137. {
  138. struct wpa_ctrl *ctrl;
  139. char buf[4096];
  140. char *pos, *end;
  141. size_t len, flen;
  142. ctrl = wpa_open_ctrl(ifname);
  143. if (ctrl == NULL)
  144. return -1;
  145. len = sizeof(buf);
  146. if (wpa_ctrl_request(ctrl, "STATUS-NO_EVENTS", 16, buf, &len,
  147. NULL) < 0) {
  148. wpa_ctrl_close(ctrl);
  149. return -1;
  150. }
  151. wpa_ctrl_close(ctrl);
  152. buf[len] = '\0';
  153. flen = strlen(field);
  154. pos = buf;
  155. while (pos + flen < buf + len) {
  156. if (pos > buf) {
  157. if (*pos != '\n') {
  158. pos++;
  159. continue;
  160. }
  161. pos++;
  162. }
  163. if (strncmp(pos, field, flen) != 0 || pos[flen] != '=') {
  164. pos++;
  165. continue;
  166. }
  167. pos += flen + 1;
  168. end = strchr(pos, '\n');
  169. if (end == NULL)
  170. return -1;
  171. *end++ = '\0';
  172. if (end - pos > (int) obuf_size)
  173. return -1;
  174. memcpy(obuf, pos, end - pos);
  175. return 0;
  176. }
  177. return -1;
  178. }
  179. int wait_ip_addr(const char *ifname, int timeout)
  180. {
  181. char ip[30];
  182. int count = timeout;
  183. struct wpa_ctrl *ctrl;
  184. while (count > 0) {
  185. printf("%s: ifname='%s' - %d seconds remaining\n",
  186. __func__, ifname, count);
  187. count--;
  188. if (get_wpa_status(ifname, "ip_address", ip, sizeof(ip)) == 0
  189. && strlen(ip) > 0) {
  190. printf("IP address found: '%s'\n", ip);
  191. return 0;
  192. }
  193. ctrl = wpa_open_ctrl(ifname);
  194. if (ctrl == NULL)
  195. return -1;
  196. wpa_ctrl_close(ctrl);
  197. sleep(1);
  198. }
  199. printf("%s: Could not get IP address for ifname='%s'", __func__,
  200. ifname);
  201. return -1;
  202. }
  203. int add_network(const char *ifname)
  204. {
  205. char res[30];
  206. if (wpa_command_resp(ifname, "ADD_NETWORK", res, sizeof(res)) < 0)
  207. return -1;
  208. return atoi(res);
  209. }
  210. int set_network(const char *ifname, int id, const char *field,
  211. const char *value)
  212. {
  213. char buf[200];
  214. snprintf(buf, sizeof(buf), "SET_NETWORK %d %s %s", id, field, value);
  215. return wpa_command(ifname, buf);
  216. }
  217. int set_network_quoted(const char *ifname, int id, const char *field,
  218. const char *value)
  219. {
  220. char buf[200];
  221. snprintf(buf, sizeof(buf), "SET_NETWORK %d %s \"%s\"",
  222. id, field, value);
  223. return wpa_command(ifname, buf);
  224. }
  225. int add_cred(const char *ifname)
  226. {
  227. char res[30];
  228. if (wpa_command_resp(ifname, "ADD_CRED", res, sizeof(res)) < 0)
  229. return -1;
  230. return atoi(res);
  231. }
  232. int set_cred(const char *ifname, int id, const char *field, const char *value)
  233. {
  234. char buf[200];
  235. snprintf(buf, sizeof(buf), "SET_CRED %d %s %s", id, field, value);
  236. return wpa_command(ifname, buf);
  237. }
  238. int set_cred_quoted(const char *ifname, int id, const char *field,
  239. const char *value)
  240. {
  241. char buf[200];
  242. snprintf(buf, sizeof(buf), "SET_CRED %d %s \"%s\"",
  243. id, field, value);
  244. return wpa_command(ifname, buf);
  245. }