1 /*
2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, 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
9 #include "includes.h"
10
11 #ifdef CONFIG_CTRL_IFACE
12
13 #ifdef CONFIG_CTRL_IFACE_UNIX
14 #include <sys/un.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #endif /* CONFIG_CTRL_IFACE_UNIX */
18 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
19 #include <netdb.h>
20 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
21
22 #ifdef ANDROID
23 #include <dirent.h>
24 #include <sys/stat.h>
25 #include <cutils/sockets.h>
26 #include "private/android_filesystem_config.h"
27 #endif /* ANDROID */
28
29 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
30 #include <net/if.h>
31 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
32
33 #include "wpa_ctrl.h"
34 #include "common.h"
35
36
37 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
38 #define CTRL_IFACE_SOCKET
39 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
40
41
42 /**
43 * struct wpa_ctrl - Internal structure for control interface library
44 *
45 * This structure is used by the wpa_supplicant/hostapd control interface
46 * library to store internal data. Programs using the library should not touch
47 * this data directly. They can only use the pointer to the data structure as
48 * an identifier for the control interface connection and use this as one of
49 * the arguments for most of the control interface library functions.
50 */
51 struct wpa_ctrl {
52 #ifdef CONFIG_CTRL_IFACE_UDP
53 int s;
54 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
55 struct sockaddr_in6 local;
56 struct sockaddr_in6 dest;
57 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
58 struct sockaddr_in local;
59 struct sockaddr_in dest;
60 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
61 char *cookie;
62 char *remote_ifname;
63 char *remote_ip;
64 #endif /* CONFIG_CTRL_IFACE_UDP */
65 #ifdef CONFIG_CTRL_IFACE_UNIX
66 int s;
67 struct sockaddr_un local;
68 struct sockaddr_un dest;
69 #endif /* CONFIG_CTRL_IFACE_UNIX */
70 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
71 HANDLE pipe;
72 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
73 };
74
75
76 #ifdef CONFIG_CTRL_IFACE_UNIX
77
78 #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR
79 #define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp"
80 #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */
81 #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX
82 #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_"
83 #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */
84
85
wpa_ctrl_open(const char * ctrl_path)86 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
87 {
88 struct wpa_ctrl *ctrl;
89 static int counter = 0;
90 int ret;
91 size_t res;
92 int tries = 0;
93 int flags;
94
95 if (ctrl_path == NULL)
96 return NULL;
97
98 ctrl = os_zalloc(sizeof(*ctrl));
99 if (ctrl == NULL)
100 return NULL;
101
102 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
103 if (ctrl->s < 0) {
104 os_free(ctrl);
105 return NULL;
106 }
107
108 ctrl->local.sun_family = AF_UNIX;
109 counter++;
110 try_again:
111 ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
112 CONFIG_CTRL_IFACE_CLIENT_DIR "/"
113 CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d",
114 (int) getpid(), counter);
115 if (os_snprintf_error(sizeof(ctrl->local.sun_path), ret)) {
116 close(ctrl->s);
117 os_free(ctrl);
118 return NULL;
119 }
120 tries++;
121 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
122 sizeof(ctrl->local)) < 0) {
123 if (errno == EADDRINUSE && tries < 2) {
124 /*
125 * getpid() returns unique identifier for this instance
126 * of wpa_ctrl, so the existing socket file must have
127 * been left by unclean termination of an earlier run.
128 * Remove the file and try again.
129 */
130 unlink(ctrl->local.sun_path);
131 goto try_again;
132 }
133 close(ctrl->s);
134 os_free(ctrl);
135 return NULL;
136 }
137
138 #ifdef ANDROID
139 chmod(ctrl->local.sun_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
140 chown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI);
141
142 if (os_strncmp(ctrl_path, "@android:", 9) == 0) {
143 if (socket_local_client_connect(
144 ctrl->s, ctrl_path + 9,
145 ANDROID_SOCKET_NAMESPACE_RESERVED,
146 SOCK_DGRAM) < 0) {
147 close(ctrl->s);
148 unlink(ctrl->local.sun_path);
149 os_free(ctrl);
150 return NULL;
151 }
152 return ctrl;
153 }
154
155 /*
156 * If the ctrl_path isn't an absolute pathname, assume that
157 * it's the name of a socket in the Android reserved namespace.
158 * Otherwise, it's a normal UNIX domain socket appearing in the
159 * filesystem.
160 */
161 if (*ctrl_path != '/') {
162 char buf[21];
163 os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path);
164 if (socket_local_client_connect(
165 ctrl->s, buf,
166 ANDROID_SOCKET_NAMESPACE_RESERVED,
167 SOCK_DGRAM) < 0) {
168 close(ctrl->s);
169 unlink(ctrl->local.sun_path);
170 os_free(ctrl);
171 return NULL;
172 }
173 return ctrl;
174 }
175 #endif /* ANDROID */
176
177 ctrl->dest.sun_family = AF_UNIX;
178 if (os_strncmp(ctrl_path, "@abstract:", 10) == 0) {
179 ctrl->dest.sun_path[0] = '\0';
180 os_strlcpy(ctrl->dest.sun_path + 1, ctrl_path + 10,
181 sizeof(ctrl->dest.sun_path) - 1);
182 } else {
183 res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
184 sizeof(ctrl->dest.sun_path));
185 if (res >= sizeof(ctrl->dest.sun_path)) {
186 close(ctrl->s);
187 os_free(ctrl);
188 return NULL;
189 }
190 }
191 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
192 sizeof(ctrl->dest)) < 0) {
193 close(ctrl->s);
194 unlink(ctrl->local.sun_path);
195 os_free(ctrl);
196 return NULL;
197 }
198
199 /*
200 * Make socket non-blocking so that we don't hang forever if
201 * target dies unexpectedly.
202 */
203 flags = fcntl(ctrl->s, F_GETFL);
204 if (flags >= 0) {
205 flags |= O_NONBLOCK;
206 if (fcntl(ctrl->s, F_SETFL, flags) < 0) {
207 perror("fcntl(ctrl->s, O_NONBLOCK)");
208 /* Not fatal, continue on.*/
209 }
210 }
211
212 return ctrl;
213 }
214
215
wpa_ctrl_close(struct wpa_ctrl * ctrl)216 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
217 {
218 if (ctrl == NULL)
219 return;
220 unlink(ctrl->local.sun_path);
221 if (ctrl->s >= 0)
222 close(ctrl->s);
223 os_free(ctrl);
224 }
225
226
227 #ifdef ANDROID
228 /**
229 * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
230 * may be left over from clients that were previously connected to
231 * wpa_supplicant. This keeps these files from being orphaned in the
232 * event of crashes that prevented them from being removed as part
233 * of the normal orderly shutdown.
234 */
wpa_ctrl_cleanup(void)235 void wpa_ctrl_cleanup(void)
236 {
237 DIR *dir;
238 struct dirent entry;
239 struct dirent *result;
240 size_t dirnamelen;
241 size_t maxcopy;
242 char pathname[PATH_MAX];
243 char *namep;
244
245 if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL)
246 return;
247
248 dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/",
249 CONFIG_CTRL_IFACE_CLIENT_DIR);
250 if (dirnamelen >= sizeof(pathname)) {
251 closedir(dir);
252 return;
253 }
254 namep = pathname + dirnamelen;
255 maxcopy = PATH_MAX - dirnamelen;
256 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
257 if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy)
258 unlink(pathname);
259 }
260 closedir(dir);
261 }
262 #endif /* ANDROID */
263
264 #else /* CONFIG_CTRL_IFACE_UNIX */
265
266 #ifdef ANDROID
wpa_ctrl_cleanup(void)267 void wpa_ctrl_cleanup(void)
268 {
269 }
270 #endif /* ANDROID */
271
272 #endif /* CONFIG_CTRL_IFACE_UNIX */
273
274
275 #ifdef CONFIG_CTRL_IFACE_UDP
276
wpa_ctrl_open(const char * ctrl_path)277 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
278 {
279 struct wpa_ctrl *ctrl;
280 char buf[128];
281 size_t len;
282 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
283 struct hostent *h;
284 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
285
286 ctrl = os_zalloc(sizeof(*ctrl));
287 if (ctrl == NULL)
288 return NULL;
289
290 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
291 ctrl->s = socket(PF_INET6, SOCK_DGRAM, 0);
292 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
293 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
294 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
295 if (ctrl->s < 0) {
296 perror("socket");
297 os_free(ctrl);
298 return NULL;
299 }
300
301 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
302 ctrl->local.sin6_family = AF_INET6;
303 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
304 ctrl->local.sin6_addr = in6addr_any;
305 #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
306 inet_pton(AF_INET6, "::1", &ctrl->local.sin6_addr);
307 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
308 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
309 ctrl->local.sin_family = AF_INET;
310 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
311 ctrl->local.sin_addr.s_addr = INADDR_ANY;
312 #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
313 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
314 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
315 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
316
317 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
318 sizeof(ctrl->local)) < 0) {
319 close(ctrl->s);
320 os_free(ctrl);
321 return NULL;
322 }
323
324 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
325 ctrl->dest.sin6_family = AF_INET6;
326 inet_pton(AF_INET6, "::1", &ctrl->dest.sin6_addr);
327 ctrl->dest.sin6_port = htons(WPA_CTRL_IFACE_PORT);
328 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
329 ctrl->dest.sin_family = AF_INET;
330 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
331 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
332 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
333
334 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
335 if (ctrl_path) {
336 char *port, *name;
337 int port_id;
338 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
339 char *scope;
340 int scope_id = 0;
341 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
342
343 name = os_strdup(ctrl_path);
344 if (name == NULL) {
345 close(ctrl->s);
346 os_free(ctrl);
347 return NULL;
348 }
349 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
350 port = os_strchr(name, ',');
351 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
352 port = os_strchr(name, ':');
353 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
354
355 if (port) {
356 port_id = atoi(&port[1]);
357 port[0] = '\0';
358 } else
359 port_id = WPA_CTRL_IFACE_PORT;
360
361 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
362 scope = os_strchr(name, '%');
363 if (scope) {
364 scope_id = if_nametoindex(&scope[1]);
365 scope[0] = '\0';
366 }
367 h = gethostbyname2(name, AF_INET6);
368 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
369 h = gethostbyname(name);
370 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
371 ctrl->remote_ip = os_strdup(name);
372 os_free(name);
373 if (h == NULL) {
374 perror("gethostbyname");
375 close(ctrl->s);
376 os_free(ctrl->remote_ip);
377 os_free(ctrl);
378 return NULL;
379 }
380 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
381 ctrl->dest.sin6_scope_id = scope_id;
382 ctrl->dest.sin6_port = htons(port_id);
383 os_memcpy(&ctrl->dest.sin6_addr, h->h_addr, h->h_length);
384 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
385 ctrl->dest.sin_port = htons(port_id);
386 os_memcpy(&ctrl->dest.sin_addr.s_addr, h->h_addr, h->h_length);
387 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
388 } else
389 ctrl->remote_ip = os_strdup("localhost");
390 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
391
392 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
393 sizeof(ctrl->dest)) < 0) {
394 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
395 char addr[INET6_ADDRSTRLEN];
396 wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
397 inet_ntop(AF_INET6, &ctrl->dest.sin6_addr, addr,
398 sizeof(ctrl->dest)),
399 ntohs(ctrl->dest.sin6_port),
400 strerror(errno));
401 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
402 wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
403 inet_ntoa(ctrl->dest.sin_addr),
404 ntohs(ctrl->dest.sin_port),
405 strerror(errno));
406 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
407 close(ctrl->s);
408 os_free(ctrl->remote_ip);
409 os_free(ctrl);
410 return NULL;
411 }
412
413 len = sizeof(buf) - 1;
414 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
415 buf[len] = '\0';
416 ctrl->cookie = os_strdup(buf);
417 }
418
419 if (wpa_ctrl_request(ctrl, "IFNAME", 6, buf, &len, NULL) == 0) {
420 buf[len] = '\0';
421 ctrl->remote_ifname = os_strdup(buf);
422 }
423
424 return ctrl;
425 }
426
427
wpa_ctrl_get_remote_ifname(struct wpa_ctrl * ctrl)428 char * wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl)
429 {
430 #define WPA_CTRL_MAX_PS_NAME 100
431 static char ps[WPA_CTRL_MAX_PS_NAME] = {};
432 os_snprintf(ps, WPA_CTRL_MAX_PS_NAME, "%s/%s",
433 ctrl->remote_ip, ctrl->remote_ifname);
434 return ps;
435 }
436
437
wpa_ctrl_close(struct wpa_ctrl * ctrl)438 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
439 {
440 close(ctrl->s);
441 os_free(ctrl->cookie);
442 os_free(ctrl->remote_ifname);
443 os_free(ctrl->remote_ip);
444 os_free(ctrl);
445 }
446
447 #endif /* CONFIG_CTRL_IFACE_UDP */
448
449
450 #ifdef CTRL_IFACE_SOCKET
wpa_ctrl_request(struct wpa_ctrl * ctrl,const char * cmd,size_t cmd_len,char * reply,size_t * reply_len,void (* msg_cb)(char * msg,size_t len))451 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
452 char *reply, size_t *reply_len,
453 void (*msg_cb)(char *msg, size_t len))
454 {
455 struct timeval tv;
456 struct os_reltime started_at;
457 int res;
458 fd_set rfds;
459 const char *_cmd;
460 char *cmd_buf = NULL;
461 size_t _cmd_len;
462
463 #ifdef CONFIG_CTRL_IFACE_UDP
464 if (ctrl->cookie) {
465 char *pos;
466 _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
467 cmd_buf = os_malloc(_cmd_len);
468 if (cmd_buf == NULL)
469 return -1;
470 _cmd = cmd_buf;
471 pos = cmd_buf;
472 os_strlcpy(pos, ctrl->cookie, _cmd_len);
473 pos += os_strlen(ctrl->cookie);
474 *pos++ = ' ';
475 os_memcpy(pos, cmd, cmd_len);
476 } else
477 #endif /* CONFIG_CTRL_IFACE_UDP */
478 {
479 _cmd = cmd;
480 _cmd_len = cmd_len;
481 }
482
483 errno = 0;
484 started_at.sec = 0;
485 started_at.usec = 0;
486 retry_send:
487 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
488 if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK)
489 {
490 /*
491 * Must be a non-blocking socket... Try for a bit
492 * longer before giving up.
493 */
494 if (started_at.sec == 0)
495 os_get_reltime(&started_at);
496 else {
497 struct os_reltime n;
498 os_get_reltime(&n);
499 /* Try for a few seconds. */
500 if (os_reltime_expired(&n, &started_at, 5))
501 goto send_err;
502 }
503 os_sleep(1, 0);
504 goto retry_send;
505 }
506 send_err:
507 os_free(cmd_buf);
508 return -1;
509 }
510 os_free(cmd_buf);
511
512 for (;;) {
513 tv.tv_sec = 10;
514 tv.tv_usec = 0;
515 FD_ZERO(&rfds);
516 FD_SET(ctrl->s, &rfds);
517 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
518 if (res < 0)
519 return res;
520 if (FD_ISSET(ctrl->s, &rfds)) {
521 res = recv(ctrl->s, reply, *reply_len, 0);
522 if (res < 0)
523 return res;
524 if (res > 0 && reply[0] == '<') {
525 /* This is an unsolicited message from
526 * wpa_supplicant, not the reply to the
527 * request. Use msg_cb to report this to the
528 * caller. */
529 if (msg_cb) {
530 /* Make sure the message is nul
531 * terminated. */
532 if ((size_t) res == *reply_len)
533 res = (*reply_len) - 1;
534 reply[res] = '\0';
535 msg_cb(reply, res);
536 }
537 continue;
538 }
539 *reply_len = res;
540 break;
541 } else {
542 return -2;
543 }
544 }
545 return 0;
546 }
547 #endif /* CTRL_IFACE_SOCKET */
548
549
wpa_ctrl_attach_helper(struct wpa_ctrl * ctrl,int attach)550 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
551 {
552 char buf[10];
553 int ret;
554 size_t len = 10;
555
556 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
557 buf, &len, NULL);
558 if (ret < 0)
559 return ret;
560 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
561 return 0;
562 return -1;
563 }
564
565
wpa_ctrl_attach(struct wpa_ctrl * ctrl)566 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
567 {
568 return wpa_ctrl_attach_helper(ctrl, 1);
569 }
570
571
wpa_ctrl_detach(struct wpa_ctrl * ctrl)572 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
573 {
574 return wpa_ctrl_attach_helper(ctrl, 0);
575 }
576
577
578 #ifdef CTRL_IFACE_SOCKET
579
wpa_ctrl_recv(struct wpa_ctrl * ctrl,char * reply,size_t * reply_len)580 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
581 {
582 int res;
583
584 res = recv(ctrl->s, reply, *reply_len, 0);
585 if (res < 0)
586 return res;
587 *reply_len = res;
588 return 0;
589 }
590
591
wpa_ctrl_pending(struct wpa_ctrl * ctrl)592 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
593 {
594 struct timeval tv;
595 fd_set rfds;
596 tv.tv_sec = 0;
597 tv.tv_usec = 0;
598 FD_ZERO(&rfds);
599 FD_SET(ctrl->s, &rfds);
600 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
601 return FD_ISSET(ctrl->s, &rfds);
602 }
603
604
wpa_ctrl_get_fd(struct wpa_ctrl * ctrl)605 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
606 {
607 return ctrl->s;
608 }
609
610 #endif /* CTRL_IFACE_SOCKET */
611
612
613 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
614
615 #ifndef WPA_SUPPLICANT_NAMED_PIPE
616 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
617 #endif
618 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
619
wpa_ctrl_open(const char * ctrl_path)620 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
621 {
622 struct wpa_ctrl *ctrl;
623 DWORD mode;
624 TCHAR name[256];
625 int i, ret;
626
627 ctrl = os_malloc(sizeof(*ctrl));
628 if (ctrl == NULL)
629 return NULL;
630 os_memset(ctrl, 0, sizeof(*ctrl));
631
632 #ifdef UNICODE
633 if (ctrl_path == NULL)
634 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
635 else
636 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
637 ctrl_path);
638 #else /* UNICODE */
639 if (ctrl_path == NULL)
640 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
641 else
642 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
643 ctrl_path);
644 #endif /* UNICODE */
645 if (os_snprintf_error(256, ret)) {
646 os_free(ctrl);
647 return NULL;
648 }
649
650 for (i = 0; i < 10; i++) {
651 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
652 NULL, OPEN_EXISTING, 0, NULL);
653 /*
654 * Current named pipe server side in wpa_supplicant is
655 * re-opening the pipe for new clients only after the previous
656 * one is taken into use. This leaves a small window for race
657 * conditions when two connections are being opened at almost
658 * the same time. Retry if that was the case.
659 */
660 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
661 GetLastError() != ERROR_PIPE_BUSY)
662 break;
663 WaitNamedPipe(name, 1000);
664 }
665 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
666 os_free(ctrl);
667 return NULL;
668 }
669
670 mode = PIPE_READMODE_MESSAGE;
671 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
672 CloseHandle(ctrl->pipe);
673 os_free(ctrl);
674 return NULL;
675 }
676
677 return ctrl;
678 }
679
680
wpa_ctrl_close(struct wpa_ctrl * ctrl)681 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
682 {
683 CloseHandle(ctrl->pipe);
684 os_free(ctrl);
685 }
686
687
wpa_ctrl_request(struct wpa_ctrl * ctrl,const char * cmd,size_t cmd_len,char * reply,size_t * reply_len,void (* msg_cb)(char * msg,size_t len))688 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
689 char *reply, size_t *reply_len,
690 void (*msg_cb)(char *msg, size_t len))
691 {
692 DWORD written;
693 DWORD readlen = *reply_len;
694
695 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
696 return -1;
697
698 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
699 return -1;
700 *reply_len = readlen;
701
702 return 0;
703 }
704
705
wpa_ctrl_recv(struct wpa_ctrl * ctrl,char * reply,size_t * reply_len)706 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
707 {
708 DWORD len = *reply_len;
709 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
710 return -1;
711 *reply_len = len;
712 return 0;
713 }
714
715
wpa_ctrl_pending(struct wpa_ctrl * ctrl)716 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
717 {
718 DWORD left;
719
720 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
721 return -1;
722 return left ? 1 : 0;
723 }
724
725
wpa_ctrl_get_fd(struct wpa_ctrl * ctrl)726 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
727 {
728 return -1;
729 }
730
731 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
732
733 #endif /* CONFIG_CTRL_IFACE */
734