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;
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
ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
81
"/tmp/wpa_ctrl_%d-%d", getpid(), counter++);
82
if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
87
if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
88
sizeof(ctrl->local)) < 0) {
94
ctrl->dest.sun_family = AF_UNIX;
95
res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
96
sizeof(ctrl->dest.sun_path));
97
if (res >= sizeof(ctrl->dest.sun_path)) {
102
if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
103
sizeof(ctrl->dest)) < 0) {
105
unlink(ctrl->local.sun_path);
114
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
116
unlink(ctrl->local.sun_path);
121
#endif /* CONFIG_CTRL_IFACE_UNIX */
124
#ifdef CONFIG_CTRL_IFACE_UDP
126
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
128
struct wpa_ctrl *ctrl;
132
ctrl = os_malloc(sizeof(*ctrl));
135
os_memset(ctrl, 0, sizeof(*ctrl));
137
ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
144
ctrl->local.sin_family = AF_INET;
145
ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
146
if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
147
sizeof(ctrl->local)) < 0) {
153
ctrl->dest.sin_family = AF_INET;
154
ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
155
ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
156
if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
157
sizeof(ctrl->dest)) < 0) {
164
len = sizeof(buf) - 1;
165
if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
167
ctrl->cookie = os_strdup(buf);
174
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
177
os_free(ctrl->cookie);
181
#endif /* CONFIG_CTRL_IFACE_UDP */
184
#ifdef CTRL_IFACE_SOCKET
185
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
186
char *reply, size_t *reply_len,
187
void (*msg_cb)(char *msg, size_t len))
193
char *cmd_buf = NULL;
196
#ifdef CONFIG_CTRL_IFACE_UDP
199
_cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
200
cmd_buf = os_malloc(_cmd_len);
205
os_strlcpy(pos, ctrl->cookie, _cmd_len);
206
pos += os_strlen(ctrl->cookie);
208
os_memcpy(pos, cmd, cmd_len);
210
#endif /* CONFIG_CTRL_IFACE_UDP */
216
if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
226
FD_SET(ctrl->s, &rfds);
227
res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
228
if (FD_ISSET(ctrl->s, &rfds)) {
229
res = recv(ctrl->s, reply, *reply_len, 0);
232
if (res > 0 && reply[0] == '<') {
233
/* This is an unsolicited message from
234
* wpa_supplicant, not the reply to the
235
* request. Use msg_cb to report this to the
238
/* Make sure the message is nul
240
if ((size_t) res == *reply_len)
241
res = (*reply_len) - 1;
255
#endif /* CTRL_IFACE_SOCKET */
258
static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
264
ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
268
if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
274
int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
276
return wpa_ctrl_attach_helper(ctrl, 1);
280
int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
282
return wpa_ctrl_attach_helper(ctrl, 0);
286
#ifdef CTRL_IFACE_SOCKET
288
int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
292
res = recv(ctrl->s, reply, *reply_len, 0);
300
int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
307
FD_SET(ctrl->s, &rfds);
308
select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
309
return FD_ISSET(ctrl->s, &rfds);
313
int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
318
#endif /* CTRL_IFACE_SOCKET */
321
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
323
#ifndef WPA_SUPPLICANT_NAMED_PIPE
324
#define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
326
#define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
328
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
330
struct wpa_ctrl *ctrl;
335
ctrl = os_malloc(sizeof(*ctrl));
338
os_memset(ctrl, 0, sizeof(*ctrl));
341
if (ctrl_path == NULL)
342
ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
344
ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
347
if (ctrl_path == NULL)
348
ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
350
ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
353
if (ret < 0 || ret >= 256) {
358
for (i = 0; i < 10; i++) {
359
ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
360
NULL, OPEN_EXISTING, 0, NULL);
362
* Current named pipe server side in wpa_supplicant is
363
* re-opening the pipe for new clients only after the previous
364
* one is taken into use. This leaves a small window for race
365
* conditions when two connections are being opened at almost
366
* the same time. Retry if that was the case.
368
if (ctrl->pipe != INVALID_HANDLE_VALUE ||
369
GetLastError() != ERROR_PIPE_BUSY)
371
WaitNamedPipe(name, 1000);
373
if (ctrl->pipe == INVALID_HANDLE_VALUE) {
378
mode = PIPE_READMODE_MESSAGE;
379
if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
380
CloseHandle(ctrl->pipe);
389
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
391
CloseHandle(ctrl->pipe);
396
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
397
char *reply, size_t *reply_len,
398
void (*msg_cb)(char *msg, size_t len))
401
DWORD readlen = *reply_len;
403
if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
406
if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
408
*reply_len = readlen;
414
int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
416
DWORD len = *reply_len;
417
if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
424
int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
428
if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
434
int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
439
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
441
#endif /* CONFIG_CTRL_IFACE */