]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/common/wpa_ctrl.c
Use longer timeout in wpa_ctrl_request()
[thirdparty/hostap.git] / src / common / wpa_ctrl.c
1 /*
2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #ifdef CONFIG_CTRL_IFACE
18
19 #ifdef CONFIG_CTRL_IFACE_UNIX
20 #include <sys/un.h>
21 #endif /* CONFIG_CTRL_IFACE_UNIX */
22
23 #include "wpa_ctrl.h"
24 #include "common.h"
25
26
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 */
30
31
32 /**
33 * struct wpa_ctrl - Internal structure for control interface library
34 *
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.
40 */
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 */
56 };
57
58
59 #ifdef CONFIG_CTRL_IFACE_UNIX
60
61 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
62 {
63 struct wpa_ctrl *ctrl;
64 static int counter = 0;
65 int ret;
66 size_t res;
67 int tries = 0;
68
69 ctrl = os_malloc(sizeof(*ctrl));
70 if (ctrl == NULL)
71 return NULL;
72 os_memset(ctrl, 0, sizeof(*ctrl));
73
74 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
75 if (ctrl->s < 0) {
76 os_free(ctrl);
77 return NULL;
78 }
79
80 ctrl->local.sun_family = AF_UNIX;
81 counter++;
82 try_again:
83 ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
84 "/tmp/wpa_ctrl_%d-%d", (int) getpid(), counter);
85 if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
86 close(ctrl->s);
87 os_free(ctrl);
88 return NULL;
89 }
90 tries++;
91 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
92 sizeof(ctrl->local)) < 0) {
93 if (errno == EADDRINUSE && tries < 2) {
94 /*
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.
99 */
100 unlink(ctrl->local.sun_path);
101 goto try_again;
102 }
103 close(ctrl->s);
104 os_free(ctrl);
105 return NULL;
106 }
107
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)) {
112 close(ctrl->s);
113 os_free(ctrl);
114 return NULL;
115 }
116 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
117 sizeof(ctrl->dest)) < 0) {
118 close(ctrl->s);
119 unlink(ctrl->local.sun_path);
120 os_free(ctrl);
121 return NULL;
122 }
123
124 return ctrl;
125 }
126
127
128 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
129 {
130 if (ctrl == NULL)
131 return;
132 unlink(ctrl->local.sun_path);
133 if (ctrl->s >= 0)
134 close(ctrl->s);
135 os_free(ctrl);
136 }
137
138 #endif /* CONFIG_CTRL_IFACE_UNIX */
139
140
141 #ifdef CONFIG_CTRL_IFACE_UDP
142
143 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
144 {
145 struct wpa_ctrl *ctrl;
146 char buf[128];
147 size_t len;
148
149 ctrl = os_malloc(sizeof(*ctrl));
150 if (ctrl == NULL)
151 return NULL;
152 os_memset(ctrl, 0, sizeof(*ctrl));
153
154 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
155 if (ctrl->s < 0) {
156 perror("socket");
157 os_free(ctrl);
158 return NULL;
159 }
160
161 ctrl->local.sin_family = AF_INET;
162 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
163 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
164 sizeof(ctrl->local)) < 0) {
165 close(ctrl->s);
166 os_free(ctrl);
167 return NULL;
168 }
169
170 ctrl->dest.sin_family = AF_INET;
171 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
172 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
173 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
174 sizeof(ctrl->dest)) < 0) {
175 perror("connect");
176 close(ctrl->s);
177 os_free(ctrl);
178 return NULL;
179 }
180
181 len = sizeof(buf) - 1;
182 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
183 buf[len] = '\0';
184 ctrl->cookie = os_strdup(buf);
185 }
186
187 return ctrl;
188 }
189
190
191 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
192 {
193 close(ctrl->s);
194 os_free(ctrl->cookie);
195 os_free(ctrl);
196 }
197
198 #endif /* CONFIG_CTRL_IFACE_UDP */
199
200
201 #ifdef CTRL_IFACE_SOCKET
202 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
203 char *reply, size_t *reply_len,
204 void (*msg_cb)(char *msg, size_t len))
205 {
206 struct timeval tv;
207 int res;
208 fd_set rfds;
209 const char *_cmd;
210 char *cmd_buf = NULL;
211 size_t _cmd_len;
212
213 #ifdef CONFIG_CTRL_IFACE_UDP
214 if (ctrl->cookie) {
215 char *pos;
216 _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
217 cmd_buf = os_malloc(_cmd_len);
218 if (cmd_buf == NULL)
219 return -1;
220 _cmd = cmd_buf;
221 pos = cmd_buf;
222 os_strlcpy(pos, ctrl->cookie, _cmd_len);
223 pos += os_strlen(ctrl->cookie);
224 *pos++ = ' ';
225 os_memcpy(pos, cmd, cmd_len);
226 } else
227 #endif /* CONFIG_CTRL_IFACE_UDP */
228 {
229 _cmd = cmd;
230 _cmd_len = cmd_len;
231 }
232
233 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
234 os_free(cmd_buf);
235 return -1;
236 }
237 os_free(cmd_buf);
238
239 for (;;) {
240 tv.tv_sec = 10;
241 tv.tv_usec = 0;
242 FD_ZERO(&rfds);
243 FD_SET(ctrl->s, &rfds);
244 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
245 if (FD_ISSET(ctrl->s, &rfds)) {
246 res = recv(ctrl->s, reply, *reply_len, 0);
247 if (res < 0)
248 return res;
249 if (res > 0 && reply[0] == '<') {
250 /* This is an unsolicited message from
251 * wpa_supplicant, not the reply to the
252 * request. Use msg_cb to report this to the
253 * caller. */
254 if (msg_cb) {
255 /* Make sure the message is nul
256 * terminated. */
257 if ((size_t) res == *reply_len)
258 res = (*reply_len) - 1;
259 reply[res] = '\0';
260 msg_cb(reply, res);
261 }
262 continue;
263 }
264 *reply_len = res;
265 break;
266 } else {
267 return -2;
268 }
269 }
270 return 0;
271 }
272 #endif /* CTRL_IFACE_SOCKET */
273
274
275 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
276 {
277 char buf[10];
278 int ret;
279 size_t len = 10;
280
281 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
282 buf, &len, NULL);
283 if (ret < 0)
284 return ret;
285 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
286 return 0;
287 return -1;
288 }
289
290
291 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
292 {
293 return wpa_ctrl_attach_helper(ctrl, 1);
294 }
295
296
297 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
298 {
299 return wpa_ctrl_attach_helper(ctrl, 0);
300 }
301
302
303 #ifdef CTRL_IFACE_SOCKET
304
305 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
306 {
307 int res;
308
309 res = recv(ctrl->s, reply, *reply_len, 0);
310 if (res < 0)
311 return res;
312 *reply_len = res;
313 return 0;
314 }
315
316
317 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
318 {
319 struct timeval tv;
320 fd_set rfds;
321 tv.tv_sec = 0;
322 tv.tv_usec = 0;
323 FD_ZERO(&rfds);
324 FD_SET(ctrl->s, &rfds);
325 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
326 return FD_ISSET(ctrl->s, &rfds);
327 }
328
329
330 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
331 {
332 return ctrl->s;
333 }
334
335 #endif /* CTRL_IFACE_SOCKET */
336
337
338 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
339
340 #ifndef WPA_SUPPLICANT_NAMED_PIPE
341 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
342 #endif
343 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
344
345 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
346 {
347 struct wpa_ctrl *ctrl;
348 DWORD mode;
349 TCHAR name[256];
350 int i, ret;
351
352 ctrl = os_malloc(sizeof(*ctrl));
353 if (ctrl == NULL)
354 return NULL;
355 os_memset(ctrl, 0, sizeof(*ctrl));
356
357 #ifdef UNICODE
358 if (ctrl_path == NULL)
359 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
360 else
361 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
362 ctrl_path);
363 #else /* UNICODE */
364 if (ctrl_path == NULL)
365 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
366 else
367 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
368 ctrl_path);
369 #endif /* UNICODE */
370 if (ret < 0 || ret >= 256) {
371 os_free(ctrl);
372 return NULL;
373 }
374
375 for (i = 0; i < 10; i++) {
376 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
377 NULL, OPEN_EXISTING, 0, NULL);
378 /*
379 * Current named pipe server side in wpa_supplicant is
380 * re-opening the pipe for new clients only after the previous
381 * one is taken into use. This leaves a small window for race
382 * conditions when two connections are being opened at almost
383 * the same time. Retry if that was the case.
384 */
385 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
386 GetLastError() != ERROR_PIPE_BUSY)
387 break;
388 WaitNamedPipe(name, 1000);
389 }
390 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
391 os_free(ctrl);
392 return NULL;
393 }
394
395 mode = PIPE_READMODE_MESSAGE;
396 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
397 CloseHandle(ctrl->pipe);
398 os_free(ctrl);
399 return NULL;
400 }
401
402 return ctrl;
403 }
404
405
406 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
407 {
408 CloseHandle(ctrl->pipe);
409 os_free(ctrl);
410 }
411
412
413 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
414 char *reply, size_t *reply_len,
415 void (*msg_cb)(char *msg, size_t len))
416 {
417 DWORD written;
418 DWORD readlen = *reply_len;
419
420 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
421 return -1;
422
423 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
424 return -1;
425 *reply_len = readlen;
426
427 return 0;
428 }
429
430
431 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
432 {
433 DWORD len = *reply_len;
434 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
435 return -1;
436 *reply_len = len;
437 return 0;
438 }
439
440
441 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
442 {
443 DWORD left;
444
445 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
446 return -1;
447 return left ? 1 : 0;
448 }
449
450
451 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
452 {
453 return -1;
454 }
455
456 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
457
458 #endif /* CONFIG_CTRL_IFACE */