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.
19 #ifdef CONFIG_CTRL_IFACE
21 #ifdef CONFIG_CTRL_IFACE_UNIX
23 #endif /* CONFIG_CTRL_IFACE_UNIX */
29 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
30 #define CTRL_IFACE_SOCKET
31 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
35 * struct wpa_ctrl - Internal structure for control interface library
37 * This structure is used by the wpa_supplicant/hostapd control interface
38 * library to store internal data. Programs using the library should not touch
39 * this data directly. They can only use the pointer to the data structure as
40 * an identifier for the control interface connection and use this as one of
41 * the arguments for most of the control interface library functions.
44 #ifdef CONFIG_CTRL_IFACE_UDP
46 struct sockaddr_in local;
47 struct sockaddr_in dest;
49 #endif /* CONFIG_CTRL_IFACE_UDP */
50 #ifdef CONFIG_CTRL_IFACE_UNIX
52 struct sockaddr_un local;
53 struct sockaddr_un dest;
54 #endif /* CONFIG_CTRL_IFACE_UNIX */
55 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
57 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
61 #ifdef CONFIG_CTRL_IFACE_UNIX
63 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
65 struct wpa_ctrl *ctrl;
66 static int counter = 0;
68 ctrl = os_malloc(sizeof(*ctrl));
71 os_memset(ctrl, 0, sizeof(*ctrl));
73 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
79 ctrl->local.sun_family = AF_UNIX;
80 os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
81 "/tmp/wpa_ctrl_%d-%d", getpid(), counter++);
82 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
83 sizeof(ctrl->local)) < 0) {
89 ctrl->dest.sun_family = AF_UNIX;
90 os_snprintf(ctrl->dest.sun_path, sizeof(ctrl->dest.sun_path), "%s",
92 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
93 sizeof(ctrl->dest)) < 0) {
95 unlink(ctrl->local.sun_path);
104 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
106 unlink(ctrl->local.sun_path);
111 #endif /* CONFIG_CTRL_IFACE_UNIX */
114 #ifdef CONFIG_CTRL_IFACE_UDP
116 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
118 struct wpa_ctrl *ctrl;
122 ctrl = os_malloc(sizeof(*ctrl));
125 os_memset(ctrl, 0, sizeof(*ctrl));
127 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
134 ctrl->local.sin_family = AF_INET;
135 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
136 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
137 sizeof(ctrl->local)) < 0) {
143 ctrl->dest.sin_family = AF_INET;
144 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
145 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
146 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
147 sizeof(ctrl->dest)) < 0) {
154 len = sizeof(buf) - 1;
155 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
157 ctrl->cookie = strdup(buf);
164 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
167 os_free(ctrl->cookie);
171 #endif /* CONFIG_CTRL_IFACE_UDP */
174 #ifdef CTRL_IFACE_SOCKET
175 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
176 char *reply, size_t *reply_len,
177 void (*msg_cb)(char *msg, size_t len))
183 char *cmd_buf = NULL;
186 #ifdef CONFIG_CTRL_IFACE_UDP
189 _cmd_len = strlen(ctrl->cookie) + 1 + cmd_len;
190 cmd_buf = os_malloc(_cmd_len );
195 strcpy(pos, ctrl->cookie);
196 pos += strlen(ctrl->cookie);
198 memcpy(pos, cmd, cmd_len);
200 #endif /* CONFIG_CTRL_IFACE_UDP */
206 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
216 FD_SET(ctrl->s, &rfds);
217 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
218 if (FD_ISSET(ctrl->s, &rfds)) {
219 res = recv(ctrl->s, reply, *reply_len, 0);
222 if (res > 0 && reply[0] == '<') {
223 /* This is an unsolicited message from
224 * wpa_supplicant, not the reply to the
225 * request. Use msg_cb to report this to the
228 /* Make sure the message is nul
230 if ((size_t) res == *reply_len)
231 res = (*reply_len) - 1;
245 #endif /* CTRL_IFACE_SOCKET */
248 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
254 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
258 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
264 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
266 return wpa_ctrl_attach_helper(ctrl, 1);
270 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
272 return wpa_ctrl_attach_helper(ctrl, 0);
276 #ifdef CTRL_IFACE_SOCKET
278 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
282 res = recv(ctrl->s, reply, *reply_len, 0);
290 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
297 FD_SET(ctrl->s, &rfds);
298 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
299 return FD_ISSET(ctrl->s, &rfds);
303 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
308 #endif /* CTRL_IFACE_SOCKET */
311 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
313 #ifndef WPA_SUPPLICANT_NAMED_PIPE
314 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
316 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
318 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
320 struct wpa_ctrl *ctrl;
325 ctrl = os_malloc(sizeof(*ctrl));
328 os_memset(ctrl, 0, sizeof(*ctrl));
331 if (ctrl_path == NULL)
332 _snwprintf(name, 256, NAMED_PIPE_PREFIX);
334 _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
337 if (ctrl_path == NULL)
338 os_snprintf(name, 256, NAMED_PIPE_PREFIX);
340 os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
344 for (i = 0; i < 10; i++) {
345 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
346 NULL, OPEN_EXISTING, 0, NULL);
348 * Current named pipe server side in wpa_supplicant is
349 * re-opening the pipe for new clients only after the previous
350 * one is taken into use. This leaves a small window for race
351 * conditions when two connections are being opened at almost
352 * the same time. Retry if that was the case.
354 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
355 GetLastError() != ERROR_PIPE_BUSY)
357 WaitNamedPipe(name, 1000);
359 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
364 mode = PIPE_READMODE_MESSAGE;
365 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
366 CloseHandle(ctrl->pipe);
375 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
377 CloseHandle(ctrl->pipe);
382 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
383 char *reply, size_t *reply_len,
384 void (*msg_cb)(char *msg, size_t len))
387 DWORD readlen = *reply_len;
389 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
392 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
394 *reply_len = readlen;
400 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
402 DWORD len = *reply_len;
403 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
410 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
414 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
420 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
425 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
427 #endif /* CONFIG_CTRL_IFACE */