2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifdef CONFIG_CTRL_IFACE
13 #ifdef CONFIG_CTRL_IFACE_UNIX
15 #endif /* CONFIG_CTRL_IFACE_UNIX */
19 #include <cutils/sockets.h>
20 #include "private/android_filesystem_config.h"
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 #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR
62 #define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp"
63 #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */
64 #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX
65 #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_"
66 #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */
69 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
71 struct wpa_ctrl *ctrl;
72 static int counter = 0;
77 ctrl = os_malloc(sizeof(*ctrl));
80 os_memset(ctrl, 0, sizeof(*ctrl));
82 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
88 ctrl->local.sun_family = AF_UNIX;
91 ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
92 CONFIG_CTRL_IFACE_CLIENT_DIR "/"
93 CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d",
94 (int) getpid(), counter);
95 if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
101 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
102 sizeof(ctrl->local)) < 0) {
103 if (errno == EADDRINUSE && tries < 2) {
105 * getpid() returns unique identifier for this instance
106 * of wpa_ctrl, so the existing socket file must have
107 * been left by unclean termination of an earlier run.
108 * Remove the file and try again.
110 unlink(ctrl->local.sun_path);
119 chmod(ctrl->local.sun_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
120 chown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI);
122 * If the ctrl_path isn't an absolute pathname, assume that
123 * it's the name of a socket in the Android reserved namespace.
124 * Otherwise, it's a normal UNIX domain socket appearing in the
127 if (ctrl_path != NULL && *ctrl_path != '/') {
129 os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path);
130 if (socket_local_client_connect(
132 ANDROID_SOCKET_NAMESPACE_RESERVED,
135 unlink(ctrl->local.sun_path);
143 ctrl->dest.sun_family = AF_UNIX;
144 res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
145 sizeof(ctrl->dest.sun_path));
146 if (res >= sizeof(ctrl->dest.sun_path)) {
151 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
152 sizeof(ctrl->dest)) < 0) {
154 unlink(ctrl->local.sun_path);
163 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
167 unlink(ctrl->local.sun_path);
176 * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
177 * may be left over from clients that were previously connected to
178 * wpa_supplicant. This keeps these files from being orphaned in the
179 * event of crashes that prevented them from being removed as part
180 * of the normal orderly shutdown.
182 void wpa_ctrl_cleanup(void)
186 struct dirent *result;
188 int prefixlen = os_strlen(CONFIG_CTRL_IFACE_CLIENT_PREFIX);
190 char pathname[PATH_MAX];
193 if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL)
196 dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/",
197 CONFIG_CTRL_IFACE_CLIENT_DIR);
198 if (dirnamelen >= sizeof(pathname)) {
202 namep = pathname + dirnamelen;
203 maxcopy = PATH_MAX - dirnamelen;
204 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
205 if (os_strncmp(entry.d_name, CONFIG_CTRL_IFACE_CLIENT_PREFIX,
207 if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy)
215 #else /* CONFIG_CTRL_IFACE_UNIX */
218 void wpa_ctrl_cleanup(void)
223 #endif /* CONFIG_CTRL_IFACE_UNIX */
226 #ifdef CONFIG_CTRL_IFACE_UDP
228 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
230 struct wpa_ctrl *ctrl;
234 ctrl = os_malloc(sizeof(*ctrl));
237 os_memset(ctrl, 0, sizeof(*ctrl));
239 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
246 ctrl->local.sin_family = AF_INET;
247 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
248 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
249 sizeof(ctrl->local)) < 0) {
255 ctrl->dest.sin_family = AF_INET;
256 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
257 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
258 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
259 sizeof(ctrl->dest)) < 0) {
266 len = sizeof(buf) - 1;
267 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
269 ctrl->cookie = os_strdup(buf);
276 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
279 os_free(ctrl->cookie);
283 #endif /* CONFIG_CTRL_IFACE_UDP */
286 #ifdef CTRL_IFACE_SOCKET
287 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
288 char *reply, size_t *reply_len,
289 void (*msg_cb)(char *msg, size_t len))
295 char *cmd_buf = NULL;
298 #ifdef CONFIG_CTRL_IFACE_UDP
301 _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
302 cmd_buf = os_malloc(_cmd_len);
307 os_strlcpy(pos, ctrl->cookie, _cmd_len);
308 pos += os_strlen(ctrl->cookie);
310 os_memcpy(pos, cmd, cmd_len);
312 #endif /* CONFIG_CTRL_IFACE_UDP */
318 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
328 FD_SET(ctrl->s, &rfds);
329 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
332 if (FD_ISSET(ctrl->s, &rfds)) {
333 res = recv(ctrl->s, reply, *reply_len, 0);
336 if (res > 0 && reply[0] == '<') {
337 /* This is an unsolicited message from
338 * wpa_supplicant, not the reply to the
339 * request. Use msg_cb to report this to the
342 /* Make sure the message is nul
344 if ((size_t) res == *reply_len)
345 res = (*reply_len) - 1;
359 #endif /* CTRL_IFACE_SOCKET */
362 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
368 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
372 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
378 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
380 return wpa_ctrl_attach_helper(ctrl, 1);
384 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
386 return wpa_ctrl_attach_helper(ctrl, 0);
390 #ifdef CTRL_IFACE_SOCKET
392 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
396 res = recv(ctrl->s, reply, *reply_len, 0);
404 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
411 FD_SET(ctrl->s, &rfds);
412 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
413 return FD_ISSET(ctrl->s, &rfds);
417 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
422 #endif /* CTRL_IFACE_SOCKET */
425 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
427 #ifndef WPA_SUPPLICANT_NAMED_PIPE
428 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
430 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
432 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
434 struct wpa_ctrl *ctrl;
439 ctrl = os_malloc(sizeof(*ctrl));
442 os_memset(ctrl, 0, sizeof(*ctrl));
445 if (ctrl_path == NULL)
446 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
448 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
451 if (ctrl_path == NULL)
452 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
454 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
457 if (ret < 0 || ret >= 256) {
462 for (i = 0; i < 10; i++) {
463 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
464 NULL, OPEN_EXISTING, 0, NULL);
466 * Current named pipe server side in wpa_supplicant is
467 * re-opening the pipe for new clients only after the previous
468 * one is taken into use. This leaves a small window for race
469 * conditions when two connections are being opened at almost
470 * the same time. Retry if that was the case.
472 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
473 GetLastError() != ERROR_PIPE_BUSY)
475 WaitNamedPipe(name, 1000);
477 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
482 mode = PIPE_READMODE_MESSAGE;
483 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
484 CloseHandle(ctrl->pipe);
493 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
495 CloseHandle(ctrl->pipe);
500 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
501 char *reply, size_t *reply_len,
502 void (*msg_cb)(char *msg, size_t len))
505 DWORD readlen = *reply_len;
507 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
510 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
512 *reply_len = readlen;
518 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
520 DWORD len = *reply_len;
521 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
528 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
532 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
538 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
543 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
545 #endif /* CONFIG_CTRL_IFACE */