2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, 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
12 * See README and COPYING for more details.
17 #ifdef CONFIG_CTRL_IFACE
19 #ifdef CONFIG_CTRL_IFACE_UNIX
21 #endif /* CONFIG_CTRL_IFACE_UNIX */
27 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
28 #define CTRL_IFACE_SOCKET
29 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
33 * struct wpa_ctrl - Internal structure for control interface library
35 * This structure is used by the wpa_supplicant/hostapd control interface
36 * library to store internal data. Programs using the library should not touch
37 * this data directly. They can only use the pointer to the data structure as
38 * an identifier for the control interface connection and use this as one of
39 * the arguments for most of the control interface library functions.
42 #ifdef CONFIG_CTRL_IFACE_UDP
44 struct sockaddr_in local
;
45 struct sockaddr_in dest
;
47 #endif /* CONFIG_CTRL_IFACE_UDP */
48 #ifdef CONFIG_CTRL_IFACE_UNIX
50 struct sockaddr_un local
;
51 struct sockaddr_un dest
;
52 #endif /* CONFIG_CTRL_IFACE_UNIX */
53 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
55 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
59 #ifdef CONFIG_CTRL_IFACE_UNIX
61 struct wpa_ctrl
* wpa_ctrl_open(const char *ctrl_path
)
63 struct wpa_ctrl
*ctrl
;
64 static int counter
= 0;
66 ctrl
= os_malloc(sizeof(*ctrl
));
69 os_memset(ctrl
, 0, sizeof(*ctrl
));
71 ctrl
->s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
77 ctrl
->local
.sun_family
= AF_UNIX
;
78 os_snprintf(ctrl
->local
.sun_path
, sizeof(ctrl
->local
.sun_path
),
79 "/tmp/wpa_ctrl_%d-%d", getpid(), counter
++);
80 if (bind(ctrl
->s
, (struct sockaddr
*) &ctrl
->local
,
81 sizeof(ctrl
->local
)) < 0) {
87 ctrl
->dest
.sun_family
= AF_UNIX
;
88 os_snprintf(ctrl
->dest
.sun_path
, sizeof(ctrl
->dest
.sun_path
), "%s",
90 if (connect(ctrl
->s
, (struct sockaddr
*) &ctrl
->dest
,
91 sizeof(ctrl
->dest
)) < 0) {
93 unlink(ctrl
->local
.sun_path
);
102 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
104 unlink(ctrl
->local
.sun_path
);
109 #endif /* CONFIG_CTRL_IFACE_UNIX */
112 #ifdef CONFIG_CTRL_IFACE_UDP
114 struct wpa_ctrl
* wpa_ctrl_open(const char *ctrl_path
)
116 struct wpa_ctrl
*ctrl
;
120 ctrl
= os_malloc(sizeof(*ctrl
));
123 os_memset(ctrl
, 0, sizeof(*ctrl
));
125 ctrl
->s
= socket(PF_INET
, SOCK_DGRAM
, 0);
132 ctrl
->local
.sin_family
= AF_INET
;
133 ctrl
->local
.sin_addr
.s_addr
= htonl((127 << 24) | 1);
134 if (bind(ctrl
->s
, (struct sockaddr
*) &ctrl
->local
,
135 sizeof(ctrl
->local
)) < 0) {
141 ctrl
->dest
.sin_family
= AF_INET
;
142 ctrl
->dest
.sin_addr
.s_addr
= htonl((127 << 24) | 1);
143 ctrl
->dest
.sin_port
= htons(WPA_CTRL_IFACE_PORT
);
144 if (connect(ctrl
->s
, (struct sockaddr
*) &ctrl
->dest
,
145 sizeof(ctrl
->dest
)) < 0) {
152 len
= sizeof(buf
) - 1;
153 if (wpa_ctrl_request(ctrl
, "GET_COOKIE", 10, buf
, &len
, NULL
) == 0) {
155 ctrl
->cookie
= strdup(buf
);
162 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
165 os_free(ctrl
->cookie
);
169 #endif /* CONFIG_CTRL_IFACE_UDP */
172 #ifdef CTRL_IFACE_SOCKET
173 int wpa_ctrl_request(struct wpa_ctrl
*ctrl
, const char *cmd
, size_t cmd_len
,
174 char *reply
, size_t *reply_len
,
175 void (*msg_cb
)(char *msg
, size_t len
))
181 char *cmd_buf
= NULL
;
184 #ifdef CONFIG_CTRL_IFACE_UDP
187 _cmd_len
= strlen(ctrl
->cookie
) + 1 + cmd_len
;
188 cmd_buf
= os_malloc(_cmd_len
);
193 strcpy(pos
, ctrl
->cookie
);
194 pos
+= strlen(ctrl
->cookie
);
196 memcpy(pos
, cmd
, cmd_len
);
198 #endif /* CONFIG_CTRL_IFACE_UDP */
204 if (send(ctrl
->s
, _cmd
, _cmd_len
, 0) < 0) {
214 FD_SET(ctrl
->s
, &rfds
);
215 res
= select(ctrl
->s
+ 1, &rfds
, NULL
, NULL
, &tv
);
216 if (FD_ISSET(ctrl
->s
, &rfds
)) {
217 res
= recv(ctrl
->s
, reply
, *reply_len
, 0);
220 if (res
> 0 && reply
[0] == '<') {
221 /* This is an unsolicited message from
222 * wpa_supplicant, not the reply to the
223 * request. Use msg_cb to report this to the
226 /* Make sure the message is nul
228 if ((size_t) res
== *reply_len
)
229 res
= (*reply_len
) - 1;
243 #endif /* CTRL_IFACE_SOCKET */
246 static int wpa_ctrl_attach_helper(struct wpa_ctrl
*ctrl
, int attach
)
252 ret
= wpa_ctrl_request(ctrl
, attach
? "ATTACH" : "DETACH", 6,
256 if (len
== 3 && os_memcmp(buf
, "OK\n", 3) == 0)
262 int wpa_ctrl_attach(struct wpa_ctrl
*ctrl
)
264 return wpa_ctrl_attach_helper(ctrl
, 1);
268 int wpa_ctrl_detach(struct wpa_ctrl
*ctrl
)
270 return wpa_ctrl_attach_helper(ctrl
, 0);
274 #ifdef CTRL_IFACE_SOCKET
276 int wpa_ctrl_recv(struct wpa_ctrl
*ctrl
, char *reply
, size_t *reply_len
)
280 res
= recv(ctrl
->s
, reply
, *reply_len
, 0);
288 int wpa_ctrl_pending(struct wpa_ctrl
*ctrl
)
295 FD_SET(ctrl
->s
, &rfds
);
296 select(ctrl
->s
+ 1, &rfds
, NULL
, NULL
, &tv
);
297 return FD_ISSET(ctrl
->s
, &rfds
);
301 int wpa_ctrl_get_fd(struct wpa_ctrl
*ctrl
)
306 #endif /* CTRL_IFACE_SOCKET */
309 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
311 #ifndef WPA_SUPPLICANT_NAMED_PIPE
312 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
314 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
316 struct wpa_ctrl
* wpa_ctrl_open(const char *ctrl_path
)
318 struct wpa_ctrl
*ctrl
;
323 ctrl
= os_malloc(sizeof(*ctrl
));
326 os_memset(ctrl
, 0, sizeof(*ctrl
));
329 if (ctrl_path
== NULL
)
330 _snwprintf(name
, 256, NAMED_PIPE_PREFIX
);
332 _snwprintf(name
, 256, NAMED_PIPE_PREFIX
TEXT("-%S"),
335 if (ctrl_path
== NULL
)
336 os_snprintf(name
, 256, NAMED_PIPE_PREFIX
);
338 os_snprintf(name
, 256, NAMED_PIPE_PREFIX
"-%s",
342 for (i
= 0; i
< 10; i
++) {
343 ctrl
->pipe
= CreateFile(name
, GENERIC_READ
| GENERIC_WRITE
, 0,
344 NULL
, OPEN_EXISTING
, 0, NULL
);
346 * Current named pipe server side in wpa_supplicant is
347 * re-opening the pipe for new clients only after the previous
348 * one is taken into use. This leaves a small window for race
349 * conditions when two connections are being opened at almost
350 * the same time. Retry if that was the case.
352 if (ctrl
->pipe
!= INVALID_HANDLE_VALUE
||
353 GetLastError() != ERROR_PIPE_BUSY
)
355 WaitNamedPipe(name
, 1000);
357 if (ctrl
->pipe
== INVALID_HANDLE_VALUE
) {
362 mode
= PIPE_READMODE_MESSAGE
;
363 if (!SetNamedPipeHandleState(ctrl
->pipe
, &mode
, NULL
, NULL
)) {
364 CloseHandle(ctrl
->pipe
);
373 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
375 CloseHandle(ctrl
->pipe
);
380 int wpa_ctrl_request(struct wpa_ctrl
*ctrl
, const char *cmd
, size_t cmd_len
,
381 char *reply
, size_t *reply_len
,
382 void (*msg_cb
)(char *msg
, size_t len
))
385 DWORD readlen
= *reply_len
;
387 if (!WriteFile(ctrl
->pipe
, cmd
, cmd_len
, &written
, NULL
))
390 if (!ReadFile(ctrl
->pipe
, reply
, *reply_len
, &readlen
, NULL
))
392 *reply_len
= readlen
;
398 int wpa_ctrl_recv(struct wpa_ctrl
*ctrl
, char *reply
, size_t *reply_len
)
400 DWORD len
= *reply_len
;
401 if (!ReadFile(ctrl
->pipe
, reply
, *reply_len
, &len
, NULL
))
408 int wpa_ctrl_pending(struct wpa_ctrl
*ctrl
)
412 if (!PeekNamedPipe(ctrl
->pipe
, NULL
, 0, NULL
, &left
, NULL
))
418 int wpa_ctrl_get_fd(struct wpa_ctrl
*ctrl
)
423 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
425 #endif /* CONFIG_CTRL_IFACE */