revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / ctrl_iface_named_pipe.c
blob5f7e24de794369b3359add8b07e01d08c52a9d7c
1 /*
2 * WPA Supplicant / Windows Named Pipe -based control interface
3 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "wpa_supplicant_i.h"
22 #include "ctrl_iface.h"
23 #include "common/wpa_ctrl.h"
25 #ifdef __MINGW32_VERSION
26 /* mingw-w32api v3.1 does not yet include sddl.h, so define needed parts here
28 #define SDDL_REVISION_1 1
29 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorA(
30 LPCSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
31 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorW(
32 LPCWSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
33 #ifdef UNICODE
34 #define ConvertStringSecurityDescriptorToSecurityDescriptor \
35 ConvertStringSecurityDescriptorToSecurityDescriptorW
36 #else
37 #define ConvertStringSecurityDescriptorToSecurityDescriptor \
38 ConvertStringSecurityDescriptorToSecurityDescriptorA
39 #endif
40 #else /* __MINGW32_VERSION */
41 #ifndef _WIN32_WINNT
42 #define _WIN32_WINNT 0x0500
43 #endif
44 #include <sddl.h>
45 #endif /* __MINGW32_VERSION */
47 #ifndef WPA_SUPPLICANT_NAMED_PIPE
48 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
49 #endif
50 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
52 /* Per-interface ctrl_iface */
54 #define REQUEST_BUFSIZE 256
55 #define REPLY_BUFSIZE 4096
57 struct ctrl_iface_priv;
59 /**
60 * struct wpa_ctrl_dst - Internal data structure of control interface clients
62 * This structure is used to store information about registered control
63 * interface monitors into struct wpa_supplicant. This data is private to
64 * ctrl_iface_named_pipe.c and should not be touched directly from other files.
66 struct wpa_ctrl_dst {
67 /* Note: OVERLAPPED must be the first member of struct wpa_ctrl_dst */
68 OVERLAPPED overlap;
69 struct wpa_ctrl_dst *next, *prev;
70 struct ctrl_iface_priv *priv;
71 HANDLE pipe;
72 int attached;
73 int debug_level;
74 int errors;
75 char req_buf[REQUEST_BUFSIZE];
76 char *rsp_buf;
77 int used;
81 struct ctrl_iface_priv {
82 struct wpa_supplicant *wpa_s;
83 struct wpa_ctrl_dst *ctrl_dst;
84 SECURITY_ATTRIBUTES attr;
85 int sec_attr_set;
89 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
90 int level, const char *buf,
91 size_t len);
93 static void ctrl_close_pipe(struct wpa_ctrl_dst *dst);
94 static void wpa_supplicant_ctrl_iface_receive(void *, void *);
95 static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
96 LPOVERLAPPED overlap);
98 struct wpa_global_dst;
99 static void global_close_pipe(struct wpa_global_dst *dst);
100 static void wpa_supplicant_global_iface_receive(void *eloop_data,
101 void *user_ctx);
102 static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
103 LPOVERLAPPED overlap);
106 static int ctrl_broken_pipe(HANDLE pipe, int used)
108 DWORD err;
110 if (PeekNamedPipe(pipe, NULL, 0, NULL, NULL, NULL))
111 return 0;
113 err = GetLastError();
114 if (err == ERROR_BROKEN_PIPE || (err == ERROR_BAD_PIPE && used))
115 return 1;
116 return 0;
120 static void ctrl_flush_broken_pipes(struct ctrl_iface_priv *priv)
122 struct wpa_ctrl_dst *dst, *next;
124 dst = priv->ctrl_dst;
126 while (dst) {
127 next = dst->next;
128 if (ctrl_broken_pipe(dst->pipe, dst->used)) {
129 wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
130 dst);
131 ctrl_close_pipe(dst);
133 dst = next;
138 static int ctrl_open_pipe(struct ctrl_iface_priv *priv)
140 struct wpa_ctrl_dst *dst;
141 DWORD err;
142 TCHAR name[256];
144 dst = os_zalloc(sizeof(*dst));
145 if (dst == NULL)
146 return -1;
147 wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
149 dst->priv = priv;
150 dst->debug_level = MSG_INFO;
151 dst->pipe = INVALID_HANDLE_VALUE;
153 dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
154 if (dst->overlap.hEvent == NULL) {
155 wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
156 (int) GetLastError());
157 goto fail;
160 eloop_register_event(dst->overlap.hEvent,
161 sizeof(dst->overlap.hEvent),
162 wpa_supplicant_ctrl_iface_receive, dst, NULL);
164 #ifdef UNICODE
165 _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
166 priv->wpa_s->ifname);
167 #else /* UNICODE */
168 os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
169 priv->wpa_s->ifname);
170 #endif /* UNICODE */
172 /* TODO: add support for configuring access list for the pipe */
173 dst->pipe = CreateNamedPipe(name,
174 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
175 PIPE_TYPE_MESSAGE |
176 PIPE_READMODE_MESSAGE |
177 PIPE_WAIT,
178 15, REPLY_BUFSIZE, REQUEST_BUFSIZE,
179 1000,
180 priv->sec_attr_set ? &priv->attr : NULL);
181 if (dst->pipe == INVALID_HANDLE_VALUE) {
182 wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
183 (int) GetLastError());
184 goto fail;
187 if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
188 wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
189 (int) GetLastError());
190 CloseHandle(dst->pipe);
191 os_free(dst);
192 return -1;
195 err = GetLastError();
196 switch (err) {
197 case ERROR_IO_PENDING:
198 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
199 "progress");
200 break;
201 case ERROR_PIPE_CONNECTED:
202 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
203 "connected");
204 if (SetEvent(dst->overlap.hEvent))
205 break;
206 /* fall through */
207 default:
208 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
209 (int) err);
210 CloseHandle(dst->pipe);
211 os_free(dst);
212 return -1;
215 dst->next = priv->ctrl_dst;
216 if (dst->next)
217 dst->next->prev = dst;
218 priv->ctrl_dst = dst;
220 return 0;
222 fail:
223 ctrl_close_pipe(dst);
224 return -1;
228 static void ctrl_close_pipe(struct wpa_ctrl_dst *dst)
230 wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
232 if (dst->overlap.hEvent) {
233 eloop_unregister_event(dst->overlap.hEvent,
234 sizeof(dst->overlap.hEvent));
235 CloseHandle(dst->overlap.hEvent);
238 if (dst->pipe != INVALID_HANDLE_VALUE) {
240 * Could use FlushFileBuffers() here to guarantee that all data
241 * gets delivered to the client, but that can block, so let's
242 * not do this for now.
243 * FlushFileBuffers(dst->pipe);
245 CloseHandle(dst->pipe);
248 if (dst->prev)
249 dst->prev->next = dst->next;
250 else
251 dst->priv->ctrl_dst = dst->next;
252 if (dst->next)
253 dst->next->prev = dst->prev;
255 os_free(dst->rsp_buf);
256 os_free(dst);
260 static VOID WINAPI ctrl_iface_write_completed(DWORD err, DWORD bytes,
261 LPOVERLAPPED overlap)
263 struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
264 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
265 "err=%d bytes=%d", dst, (int) err, (int) bytes);
266 if (err) {
267 ctrl_close_pipe(dst);
268 return;
271 os_free(dst->rsp_buf);
272 dst->rsp_buf = NULL;
274 if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
275 &dst->overlap, ctrl_iface_read_completed)) {
276 wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
277 (int) GetLastError());
278 ctrl_close_pipe(dst);
279 return;
281 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
285 static void wpa_supplicant_ctrl_iface_rx(struct wpa_ctrl_dst *dst, size_t len)
287 struct wpa_supplicant *wpa_s = dst->priv->wpa_s;
288 char *reply = NULL, *send_buf;
289 size_t reply_len = 0, send_len;
290 int new_attached = 0;
291 char *buf = dst->req_buf;
293 dst->used = 1;
294 if (len >= REQUEST_BUFSIZE)
295 len = REQUEST_BUFSIZE - 1;
296 buf[len] = '\0';
298 if (os_strcmp(buf, "ATTACH") == 0) {
299 dst->attached = 1;
300 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached");
301 new_attached = 1;
302 reply_len = 2;
303 } else if (os_strcmp(buf, "DETACH") == 0) {
304 dst->attached = 0;
305 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached");
306 reply_len = 2;
307 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
308 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", buf + 6);
309 dst->debug_level = atoi(buf + 6);
310 reply_len = 2;
311 } else {
312 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
313 &reply_len);
316 if (reply) {
317 send_buf = reply;
318 send_len = reply_len;
319 } else if (reply_len == 2) {
320 send_buf = "OK\n";
321 send_len = 3;
322 } else {
323 send_buf = "FAIL\n";
324 send_len = 5;
327 os_free(dst->rsp_buf);
328 dst->rsp_buf = os_malloc(send_len);
329 if (dst->rsp_buf == NULL) {
330 ctrl_close_pipe(dst);
331 os_free(reply);
332 return;
334 os_memcpy(dst->rsp_buf, send_buf, send_len);
335 os_free(reply);
337 if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
338 ctrl_iface_write_completed)) {
339 wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
340 (int) GetLastError());
341 ctrl_close_pipe(dst);
342 } else {
343 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
344 dst);
347 if (new_attached)
348 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
352 static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
353 LPOVERLAPPED overlap)
355 struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
356 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
357 "bytes=%d", dst, (int) err, (int) bytes);
358 if (err == 0 && bytes > 0)
359 wpa_supplicant_ctrl_iface_rx(dst, bytes);
363 static void wpa_supplicant_ctrl_iface_receive(void *eloop_data, void *user_ctx)
365 struct wpa_ctrl_dst *dst = eloop_data;
366 struct ctrl_iface_priv *priv = dst->priv;
367 DWORD bytes;
369 wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_ctrl_iface_receive");
370 ResetEvent(dst->overlap.hEvent);
372 if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
373 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
374 (int) GetLastError());
375 return;
377 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
378 "connected");
380 /* Open a new named pipe for the next client. */
381 ctrl_open_pipe(priv);
383 /* Use write completion function to start reading a command */
384 ctrl_iface_write_completed(0, 0, &dst->overlap);
386 ctrl_flush_broken_pipes(priv);
390 static int ctrl_iface_parse(struct ctrl_iface_priv *priv, const char *params)
392 const char *sddl = NULL;
393 TCHAR *t_sddl;
395 if (os_strncmp(params, "SDDL=", 5) == 0)
396 sddl = params + 5;
397 if (!sddl) {
398 sddl = os_strstr(params, " SDDL=");
399 if (sddl)
400 sddl += 6;
403 if (!sddl)
404 return 0;
406 wpa_printf(MSG_DEBUG, "CTRL: SDDL='%s'", sddl);
407 os_memset(&priv->attr, 0, sizeof(priv->attr));
408 priv->attr.nLength = sizeof(priv->attr);
409 priv->attr.bInheritHandle = FALSE;
410 t_sddl = wpa_strdup_tchar(sddl);
411 if (t_sddl == NULL)
412 return -1;
413 if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
414 t_sddl, SDDL_REVISION_1,
415 (PSECURITY_DESCRIPTOR *) (void *)
416 &priv->attr.lpSecurityDescriptor,
417 NULL)) {
418 os_free(t_sddl);
419 wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
420 "security descriptor: %d",
421 sddl, (int) GetLastError());
422 return -1;
424 os_free(t_sddl);
426 priv->sec_attr_set = 1;
428 return 0;
432 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
433 const char *txt, size_t len)
435 struct wpa_supplicant *wpa_s = ctx;
436 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
437 return;
438 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
442 struct ctrl_iface_priv *
443 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
445 struct ctrl_iface_priv *priv;
447 priv = os_zalloc(sizeof(*priv));
448 if (priv == NULL)
449 return NULL;
450 priv->wpa_s = wpa_s;
452 if (wpa_s->conf->ctrl_interface == NULL)
453 return priv;
455 if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
456 os_free(priv);
457 return NULL;
460 if (ctrl_open_pipe(priv) < 0) {
461 os_free(priv);
462 return NULL;
465 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
467 return priv;
471 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
473 while (priv->ctrl_dst)
474 ctrl_close_pipe(priv->ctrl_dst);
475 if (priv->sec_attr_set)
476 LocalFree(priv->attr.lpSecurityDescriptor);
477 os_free(priv);
481 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
482 int level, const char *buf,
483 size_t len)
485 struct wpa_ctrl_dst *dst, *next;
486 char levelstr[10];
487 int idx;
488 char *sbuf;
489 int llen;
490 DWORD written;
492 dst = priv->ctrl_dst;
493 if (dst == NULL)
494 return;
496 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
498 llen = os_strlen(levelstr);
499 sbuf = os_malloc(llen + len);
500 if (sbuf == NULL)
501 return;
503 os_memcpy(sbuf, levelstr, llen);
504 os_memcpy(sbuf + llen, buf, len);
506 idx = 0;
507 while (dst) {
508 next = dst->next;
509 if (dst->attached && level >= dst->debug_level) {
510 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
511 dst);
512 if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
513 NULL)) {
514 wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
515 "%p failed: %d",
516 dst, (int) GetLastError());
517 dst->errors++;
518 if (dst->errors > 10)
519 ctrl_close_pipe(dst);
520 } else
521 dst->errors = 0;
523 idx++;
524 dst = next;
526 os_free(sbuf);
530 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
532 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
533 priv->wpa_s->ifname);
534 if (priv->ctrl_dst == NULL)
535 return;
536 WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
540 /* Global ctrl_iface */
542 struct ctrl_iface_global_priv;
544 struct wpa_global_dst {
545 /* Note: OVERLAPPED must be the first member of struct wpa_global_dst
547 OVERLAPPED overlap;
548 struct wpa_global_dst *next, *prev;
549 struct ctrl_iface_global_priv *priv;
550 HANDLE pipe;
551 char req_buf[REQUEST_BUFSIZE];
552 char *rsp_buf;
553 int used;
556 struct ctrl_iface_global_priv {
557 struct wpa_global *global;
558 struct wpa_global_dst *ctrl_dst;
562 static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
564 struct wpa_global_dst *dst, *next;
566 dst = priv->ctrl_dst;
568 while (dst) {
569 next = dst->next;
570 if (ctrl_broken_pipe(dst->pipe, dst->used)) {
571 wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
572 dst);
573 global_close_pipe(dst);
575 dst = next;
580 static int global_open_pipe(struct ctrl_iface_global_priv *priv)
582 struct wpa_global_dst *dst;
583 DWORD err;
585 dst = os_zalloc(sizeof(*dst));
586 if (dst == NULL)
587 return -1;
588 wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
590 dst->priv = priv;
591 dst->pipe = INVALID_HANDLE_VALUE;
593 dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
594 if (dst->overlap.hEvent == NULL) {
595 wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
596 (int) GetLastError());
597 goto fail;
600 eloop_register_event(dst->overlap.hEvent,
601 sizeof(dst->overlap.hEvent),
602 wpa_supplicant_global_iface_receive, dst, NULL);
604 /* TODO: add support for configuring access list for the pipe */
605 dst->pipe = CreateNamedPipe(NAMED_PIPE_PREFIX,
606 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
607 PIPE_TYPE_MESSAGE |
608 PIPE_READMODE_MESSAGE |
609 PIPE_WAIT,
610 10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
611 1000, NULL);
612 if (dst->pipe == INVALID_HANDLE_VALUE) {
613 wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
614 (int) GetLastError());
615 goto fail;
618 if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
619 wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
620 (int) GetLastError());
621 CloseHandle(dst->pipe);
622 os_free(dst);
623 return -1;
626 err = GetLastError();
627 switch (err) {
628 case ERROR_IO_PENDING:
629 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
630 "progress");
631 break;
632 case ERROR_PIPE_CONNECTED:
633 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
634 "connected");
635 if (SetEvent(dst->overlap.hEvent))
636 break;
637 /* fall through */
638 default:
639 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
640 (int) err);
641 CloseHandle(dst->pipe);
642 os_free(dst);
643 return -1;
646 dst->next = priv->ctrl_dst;
647 if (dst->next)
648 dst->next->prev = dst;
649 priv->ctrl_dst = dst;
651 return 0;
653 fail:
654 global_close_pipe(dst);
655 return -1;
659 static void global_close_pipe(struct wpa_global_dst *dst)
661 wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
663 if (dst->overlap.hEvent) {
664 eloop_unregister_event(dst->overlap.hEvent,
665 sizeof(dst->overlap.hEvent));
666 CloseHandle(dst->overlap.hEvent);
669 if (dst->pipe != INVALID_HANDLE_VALUE) {
671 * Could use FlushFileBuffers() here to guarantee that all data
672 * gets delivered to the client, but that can block, so let's
673 * not do this for now.
674 * FlushFileBuffers(dst->pipe);
676 CloseHandle(dst->pipe);
679 if (dst->prev)
680 dst->prev->next = dst->next;
681 else
682 dst->priv->ctrl_dst = dst->next;
683 if (dst->next)
684 dst->next->prev = dst->prev;
686 os_free(dst->rsp_buf);
687 os_free(dst);
691 static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
692 LPOVERLAPPED overlap)
694 struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
695 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
696 "err=%d bytes=%d", dst, (int) err, (int) bytes);
697 if (err) {
698 global_close_pipe(dst);
699 return;
702 os_free(dst->rsp_buf);
703 dst->rsp_buf = NULL;
705 if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
706 &dst->overlap, global_iface_read_completed)) {
707 wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
708 (int) GetLastError());
709 global_close_pipe(dst);
710 /* FIX: if this was the pipe waiting for new global
711 * connections, at this point there are no open global pipes..
712 * Should try to open a new pipe.. */
713 return;
715 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
719 static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
720 size_t len)
722 struct wpa_global *global = dst->priv->global;
723 char *reply = NULL, *send_buf;
724 size_t reply_len = 0, send_len;
725 char *buf = dst->req_buf;
727 dst->used = 1;
728 if (len >= REQUEST_BUFSIZE)
729 len = REQUEST_BUFSIZE - 1;
730 buf[len] = '\0';
732 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
733 &reply_len);
734 if (reply) {
735 send_buf = reply;
736 send_len = reply_len;
737 } else if (reply_len) {
738 send_buf = "FAIL\n";
739 send_len = 5;
740 } else {
741 os_free(dst->rsp_buf);
742 dst->rsp_buf = NULL;
743 return;
746 os_free(dst->rsp_buf);
747 dst->rsp_buf = os_malloc(send_len);
748 if (dst->rsp_buf == NULL) {
749 global_close_pipe(dst);
750 os_free(reply);
751 return;
753 os_memcpy(dst->rsp_buf, send_buf, send_len);
754 os_free(reply);
756 if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
757 global_iface_write_completed)) {
758 wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
759 (int) GetLastError());
760 global_close_pipe(dst);
761 } else {
762 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
763 dst);
768 static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
769 LPOVERLAPPED overlap)
771 struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
772 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
773 "bytes=%d", dst, (int) err, (int) bytes);
774 if (err == 0 && bytes > 0)
775 wpa_supplicant_global_iface_rx(dst, bytes);
779 static void wpa_supplicant_global_iface_receive(void *eloop_data,
780 void *user_ctx)
782 struct wpa_global_dst *dst = eloop_data;
783 struct ctrl_iface_global_priv *priv = dst->priv;
784 DWORD bytes;
786 wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
787 ResetEvent(dst->overlap.hEvent);
789 if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
790 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
791 (int) GetLastError());
792 return;
794 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
795 "connected");
797 /* Open a new named pipe for the next client. */
798 if (global_open_pipe(priv) < 0) {
799 wpa_printf(MSG_DEBUG, "CTRL: global_open_pipe failed");
800 return;
803 /* Use write completion function to start reading a command */
804 global_iface_write_completed(0, 0, &dst->overlap);
806 global_flush_broken_pipes(priv);
810 struct ctrl_iface_global_priv *
811 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
813 struct ctrl_iface_global_priv *priv;
815 priv = os_zalloc(sizeof(*priv));
816 if (priv == NULL)
817 return NULL;
818 priv->global = global;
820 if (global_open_pipe(priv) < 0) {
821 os_free(priv);
822 return NULL;
825 return priv;
829 void
830 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
832 while (priv->ctrl_dst)
833 global_close_pipe(priv->ctrl_dst);
834 os_free(priv);