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;
69 ctrl
= os_malloc(sizeof(*ctrl
));
72 os_memset(ctrl
, 0, sizeof(*ctrl
));
74 ctrl
->s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
80 ctrl
->local
.sun_family
= AF_UNIX
;
83 ret
= os_snprintf(ctrl
->local
.sun_path
, sizeof(ctrl
->local
.sun_path
),
84 "/tmp/wpa_ctrl_%d-%d", getpid(), counter
);
85 if (ret
< 0 || (size_t) ret
>= sizeof(ctrl
->local
.sun_path
)) {
91 if (bind(ctrl
->s
, (struct sockaddr
*) &ctrl
->local
,
92 sizeof(ctrl
->local
)) < 0) {
93 if (errno
== EADDRINUSE
&& tries
< 2) {
95 * getpid() returns unique identifier for this instance
96 * of wpa_ctrl, so the existing socket file must have
97 * been left by unclean termination of an earlier run.
98 * Remove the file and try again.
100 unlink(ctrl
->local
.sun_path
);
108 ctrl
->dest
.sun_family
= AF_UNIX
;
109 res
= os_strlcpy(ctrl
->dest
.sun_path
, ctrl_path
,
110 sizeof(ctrl
->dest
.sun_path
));
111 if (res
>= sizeof(ctrl
->dest
.sun_path
)) {
116 if (connect(ctrl
->s
, (struct sockaddr
*) &ctrl
->dest
,
117 sizeof(ctrl
->dest
)) < 0) {
119 unlink(ctrl
->local
.sun_path
);
128 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
130 unlink(ctrl
->local
.sun_path
);
135 #endif /* CONFIG_CTRL_IFACE_UNIX */
138 #ifdef CONFIG_CTRL_IFACE_UDP
140 struct wpa_ctrl
* wpa_ctrl_open(const char *ctrl_path
)
142 struct wpa_ctrl
*ctrl
;
146 ctrl
= os_malloc(sizeof(*ctrl
));
149 os_memset(ctrl
, 0, sizeof(*ctrl
));
151 ctrl
->s
= socket(PF_INET
, SOCK_DGRAM
, 0);
158 ctrl
->local
.sin_family
= AF_INET
;
159 ctrl
->local
.sin_addr
.s_addr
= htonl((127 << 24) | 1);
160 if (bind(ctrl
->s
, (struct sockaddr
*) &ctrl
->local
,
161 sizeof(ctrl
->local
)) < 0) {
167 ctrl
->dest
.sin_family
= AF_INET
;
168 ctrl
->dest
.sin_addr
.s_addr
= htonl((127 << 24) | 1);
169 ctrl
->dest
.sin_port
= htons(WPA_CTRL_IFACE_PORT
);
170 if (connect(ctrl
->s
, (struct sockaddr
*) &ctrl
->dest
,
171 sizeof(ctrl
->dest
)) < 0) {
178 len
= sizeof(buf
) - 1;
179 if (wpa_ctrl_request(ctrl
, "GET_COOKIE", 10, buf
, &len
, NULL
) == 0) {
181 ctrl
->cookie
= os_strdup(buf
);
188 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
191 os_free(ctrl
->cookie
);
195 #endif /* CONFIG_CTRL_IFACE_UDP */
198 #ifdef CTRL_IFACE_SOCKET
199 int wpa_ctrl_request(struct wpa_ctrl
*ctrl
, const char *cmd
, size_t cmd_len
,
200 char *reply
, size_t *reply_len
,
201 void (*msg_cb
)(char *msg
, size_t len
))
207 char *cmd_buf
= NULL
;
210 #ifdef CONFIG_CTRL_IFACE_UDP
213 _cmd_len
= os_strlen(ctrl
->cookie
) + 1 + cmd_len
;
214 cmd_buf
= os_malloc(_cmd_len
);
219 os_strlcpy(pos
, ctrl
->cookie
, _cmd_len
);
220 pos
+= os_strlen(ctrl
->cookie
);
222 os_memcpy(pos
, cmd
, cmd_len
);
224 #endif /* CONFIG_CTRL_IFACE_UDP */
230 if (send(ctrl
->s
, _cmd
, _cmd_len
, 0) < 0) {
240 FD_SET(ctrl
->s
, &rfds
);
241 res
= select(ctrl
->s
+ 1, &rfds
, NULL
, NULL
, &tv
);
242 if (FD_ISSET(ctrl
->s
, &rfds
)) {
243 res
= recv(ctrl
->s
, reply
, *reply_len
, 0);
246 if (res
> 0 && reply
[0] == '<') {
247 /* This is an unsolicited message from
248 * wpa_supplicant, not the reply to the
249 * request. Use msg_cb to report this to the
252 /* Make sure the message is nul
254 if ((size_t) res
== *reply_len
)
255 res
= (*reply_len
) - 1;
269 #endif /* CTRL_IFACE_SOCKET */
272 static int wpa_ctrl_attach_helper(struct wpa_ctrl
*ctrl
, int attach
)
278 ret
= wpa_ctrl_request(ctrl
, attach
? "ATTACH" : "DETACH", 6,
282 if (len
== 3 && os_memcmp(buf
, "OK\n", 3) == 0)
288 int wpa_ctrl_attach(struct wpa_ctrl
*ctrl
)
290 return wpa_ctrl_attach_helper(ctrl
, 1);
294 int wpa_ctrl_detach(struct wpa_ctrl
*ctrl
)
296 return wpa_ctrl_attach_helper(ctrl
, 0);
300 #ifdef CTRL_IFACE_SOCKET
302 int wpa_ctrl_recv(struct wpa_ctrl
*ctrl
, char *reply
, size_t *reply_len
)
306 res
= recv(ctrl
->s
, reply
, *reply_len
, 0);
314 int wpa_ctrl_pending(struct wpa_ctrl
*ctrl
)
321 FD_SET(ctrl
->s
, &rfds
);
322 select(ctrl
->s
+ 1, &rfds
, NULL
, NULL
, &tv
);
323 return FD_ISSET(ctrl
->s
, &rfds
);
327 int wpa_ctrl_get_fd(struct wpa_ctrl
*ctrl
)
332 #endif /* CTRL_IFACE_SOCKET */
335 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
337 #ifndef WPA_SUPPLICANT_NAMED_PIPE
338 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
340 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
342 struct wpa_ctrl
* wpa_ctrl_open(const char *ctrl_path
)
344 struct wpa_ctrl
*ctrl
;
349 ctrl
= os_malloc(sizeof(*ctrl
));
352 os_memset(ctrl
, 0, sizeof(*ctrl
));
355 if (ctrl_path
== NULL
)
356 ret
= _snwprintf(name
, 256, NAMED_PIPE_PREFIX
);
358 ret
= _snwprintf(name
, 256, NAMED_PIPE_PREFIX
TEXT("-%S"),
361 if (ctrl_path
== NULL
)
362 ret
= os_snprintf(name
, 256, NAMED_PIPE_PREFIX
);
364 ret
= os_snprintf(name
, 256, NAMED_PIPE_PREFIX
"-%s",
367 if (ret
< 0 || ret
>= 256) {
372 for (i
= 0; i
< 10; i
++) {
373 ctrl
->pipe
= CreateFile(name
, GENERIC_READ
| GENERIC_WRITE
, 0,
374 NULL
, OPEN_EXISTING
, 0, NULL
);
376 * Current named pipe server side in wpa_supplicant is
377 * re-opening the pipe for new clients only after the previous
378 * one is taken into use. This leaves a small window for race
379 * conditions when two connections are being opened at almost
380 * the same time. Retry if that was the case.
382 if (ctrl
->pipe
!= INVALID_HANDLE_VALUE
||
383 GetLastError() != ERROR_PIPE_BUSY
)
385 WaitNamedPipe(name
, 1000);
387 if (ctrl
->pipe
== INVALID_HANDLE_VALUE
) {
392 mode
= PIPE_READMODE_MESSAGE
;
393 if (!SetNamedPipeHandleState(ctrl
->pipe
, &mode
, NULL
, NULL
)) {
394 CloseHandle(ctrl
->pipe
);
403 void wpa_ctrl_close(struct wpa_ctrl
*ctrl
)
405 CloseHandle(ctrl
->pipe
);
410 int wpa_ctrl_request(struct wpa_ctrl
*ctrl
, const char *cmd
, size_t cmd_len
,
411 char *reply
, size_t *reply_len
,
412 void (*msg_cb
)(char *msg
, size_t len
))
415 DWORD readlen
= *reply_len
;
417 if (!WriteFile(ctrl
->pipe
, cmd
, cmd_len
, &written
, NULL
))
420 if (!ReadFile(ctrl
->pipe
, reply
, *reply_len
, &readlen
, NULL
))
422 *reply_len
= readlen
;
428 int wpa_ctrl_recv(struct wpa_ctrl
*ctrl
, char *reply
, size_t *reply_len
)
430 DWORD len
= *reply_len
;
431 if (!ReadFile(ctrl
->pipe
, reply
, *reply_len
, &len
, NULL
))
438 int wpa_ctrl_pending(struct wpa_ctrl
*ctrl
)
442 if (!PeekNamedPipe(ctrl
->pipe
, NULL
, 0, NULL
, &left
, NULL
))
448 int wpa_ctrl_get_fd(struct wpa_ctrl
*ctrl
)
453 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
455 #endif /* CONFIG_CTRL_IFACE */