hostapd: remove version tag from directory
[dragonfly.git] / contrib / hostapd / wpa_ctrl.c
blobdcec537fc7a26b8f4288c2dcb07cf637a7d6c313
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #ifdef CONFIG_CTRL_IFACE
19 #ifdef CONFIG_CTRL_IFACE_UNIX
20 #include <sys/un.h>
21 #endif /* CONFIG_CTRL_IFACE_UNIX */
23 #include "wpa_ctrl.h"
24 #include "common.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 */
32 /**
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.
41 struct wpa_ctrl {
42 #ifdef CONFIG_CTRL_IFACE_UDP
43 int s;
44 struct sockaddr_in local;
45 struct sockaddr_in dest;
46 char *cookie;
47 #endif /* CONFIG_CTRL_IFACE_UDP */
48 #ifdef CONFIG_CTRL_IFACE_UNIX
49 int s;
50 struct sockaddr_un local;
51 struct sockaddr_un dest;
52 #endif /* CONFIG_CTRL_IFACE_UNIX */
53 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
54 HANDLE 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));
67 if (ctrl == NULL)
68 return NULL;
69 os_memset(ctrl, 0, sizeof(*ctrl));
71 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
72 if (ctrl->s < 0) {
73 os_free(ctrl);
74 return NULL;
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) {
82 close(ctrl->s);
83 os_free(ctrl);
84 return NULL;
87 ctrl->dest.sun_family = AF_UNIX;
88 os_snprintf(ctrl->dest.sun_path, sizeof(ctrl->dest.sun_path), "%s",
89 ctrl_path);
90 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
91 sizeof(ctrl->dest)) < 0) {
92 close(ctrl->s);
93 unlink(ctrl->local.sun_path);
94 os_free(ctrl);
95 return NULL;
98 return ctrl;
102 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
104 unlink(ctrl->local.sun_path);
105 close(ctrl->s);
106 os_free(ctrl);
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;
117 char buf[128];
118 size_t len;
120 ctrl = os_malloc(sizeof(*ctrl));
121 if (ctrl == NULL)
122 return NULL;
123 os_memset(ctrl, 0, sizeof(*ctrl));
125 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
126 if (ctrl->s < 0) {
127 perror("socket");
128 os_free(ctrl);
129 return NULL;
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) {
136 close(ctrl->s);
137 os_free(ctrl);
138 return NULL;
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) {
146 perror("connect");
147 close(ctrl->s);
148 os_free(ctrl);
149 return NULL;
152 len = sizeof(buf) - 1;
153 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
154 buf[len] = '\0';
155 ctrl->cookie = strdup(buf);
158 return ctrl;
162 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
164 close(ctrl->s);
165 os_free(ctrl->cookie);
166 os_free(ctrl);
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))
177 struct timeval tv;
178 int res;
179 fd_set rfds;
180 const char *_cmd;
181 char *cmd_buf = NULL;
182 size_t _cmd_len;
184 #ifdef CONFIG_CTRL_IFACE_UDP
185 if (ctrl->cookie) {
186 char *pos;
187 _cmd_len = strlen(ctrl->cookie) + 1 + cmd_len;
188 cmd_buf = os_malloc(_cmd_len );
189 if (cmd_buf == NULL)
190 return -1;
191 _cmd = cmd_buf;
192 pos = cmd_buf;
193 strcpy(pos, ctrl->cookie);
194 pos += strlen(ctrl->cookie);
195 *pos++ = ' ';
196 memcpy(pos, cmd, cmd_len);
197 } else
198 #endif /* CONFIG_CTRL_IFACE_UDP */
200 _cmd = cmd;
201 _cmd_len = cmd_len;
204 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
205 os_free(cmd_buf);
206 return -1;
208 os_free(cmd_buf);
210 for (;;) {
211 tv.tv_sec = 2;
212 tv.tv_usec = 0;
213 FD_ZERO(&rfds);
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);
218 if (res < 0)
219 return res;
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
224 * caller. */
225 if (msg_cb) {
226 /* Make sure the message is nul
227 * terminated. */
228 if ((size_t) res == *reply_len)
229 res = (*reply_len) - 1;
230 reply[res] = '\0';
231 msg_cb(reply, res);
233 continue;
235 *reply_len = res;
236 break;
237 } else {
238 return -2;
241 return 0;
243 #endif /* CTRL_IFACE_SOCKET */
246 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
248 char buf[10];
249 int ret;
250 size_t len = 10;
252 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
253 buf, &len, NULL);
254 if (ret < 0)
255 return ret;
256 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
257 return 0;
258 return -1;
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)
278 int res;
280 res = recv(ctrl->s, reply, *reply_len, 0);
281 if (res < 0)
282 return res;
283 *reply_len = res;
284 return 0;
288 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
290 struct timeval tv;
291 fd_set rfds;
292 tv.tv_sec = 0;
293 tv.tv_usec = 0;
294 FD_ZERO(&rfds);
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)
303 return ctrl->s;
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"
313 #endif
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;
319 DWORD mode;
320 TCHAR name[256];
321 int i;
323 ctrl = os_malloc(sizeof(*ctrl));
324 if (ctrl == NULL)
325 return NULL;
326 os_memset(ctrl, 0, sizeof(*ctrl));
328 #ifdef UNICODE
329 if (ctrl_path == NULL)
330 _snwprintf(name, 256, NAMED_PIPE_PREFIX);
331 else
332 _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
333 ctrl_path);
334 #else /* UNICODE */
335 if (ctrl_path == NULL)
336 os_snprintf(name, 256, NAMED_PIPE_PREFIX);
337 else
338 os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
339 ctrl_path);
340 #endif /* UNICODE */
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)
354 break;
355 WaitNamedPipe(name, 1000);
357 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
358 os_free(ctrl);
359 return NULL;
362 mode = PIPE_READMODE_MESSAGE;
363 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
364 CloseHandle(ctrl->pipe);
365 os_free(ctrl);
366 return NULL;
369 return ctrl;
373 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
375 CloseHandle(ctrl->pipe);
376 os_free(ctrl);
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))
384 DWORD written;
385 DWORD readlen = *reply_len;
387 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
388 return -1;
390 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
391 return -1;
392 *reply_len = readlen;
394 return 0;
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))
402 return -1;
403 *reply_len = len;
404 return 0;
408 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
410 DWORD left;
412 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
413 return -1;
414 return left ? 1 : 0;
418 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
420 return -1;
423 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
425 #endif /* CONFIG_CTRL_IFACE */