18 #define CONFIG_CTRL_IFACE
19 #define CONFIG_CTRL_IFACE_UNIX
21 #ifdef CONFIG_CTRL_IFACE
23 #ifdef CONFIG_CTRL_IFACE_UNIX
31 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
32 #define CTRL_IFACE_SOCKET
46 #ifdef CONFIG_CTRL_IFACE_UDP
48 struct sockaddr_in local;
49 struct sockaddr_in dest;
52 #ifdef CONFIG_CTRL_IFACE_UNIX
54 struct sockaddr_un local;
55 struct sockaddr_un dest;
57 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
63 #ifdef CONFIG_CTRL_IFACE_UNIX
68 static int counter = 0;
73 ctrl = malloc(
sizeof(*ctrl));
76 memset(ctrl, 0,
sizeof(*ctrl));
78 ctrl->
s = socket(PF_UNIX, SOCK_DGRAM, 0);
84 ctrl->
local.sun_family = AF_UNIX;
88 "/tmp/wpa_ctrl_%d-%d", getpid(), counter);
89 if (ret < 0 || (
size_t) ret >=
sizeof(ctrl->
local.sun_path)) {
95 if (bind(ctrl->
s, (
struct sockaddr *) &ctrl->
local,
96 sizeof(ctrl->
local)) < 0) {
97 if (errno == EADDRINUSE && tries < 2) {
104 unlink(ctrl->
local.sun_path);
112 ctrl->
dest.sun_family = AF_UNIX;
118 strcpy(ctrl->
dest.sun_path, ctrl_path);
125 if (connect(ctrl->
s, (
struct sockaddr *) &ctrl->
dest,
126 sizeof(ctrl->
dest)) < 0) {
128 unlink(ctrl->
local.sun_path);
139 unlink(ctrl->
local.sun_path);
147 #ifdef CONFIG_CTRL_IFACE_UDP
160 ctrl->
s = socket(PF_INET, SOCK_DGRAM, 0);
167 ctrl->
local.sin_family = AF_INET;
168 ctrl->
local.sin_addr.s_addr = htonl((127 << 24) | 1);
169 if (bind(ctrl->
s, (
struct sockaddr *) &ctrl->
local,
170 sizeof(ctrl->
local)) < 0) {
176 ctrl->
dest.sin_family = AF_INET;
177 ctrl->
dest.sin_addr.s_addr = htonl((127 << 24) | 1);
178 ctrl->
dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
179 if (connect(ctrl->
s, (
struct sockaddr *) &ctrl->
dest,
180 sizeof(ctrl->
dest)) < 0) {
187 len =
sizeof(buf) - 1;
207 #ifdef CTRL_IFACE_SOCKET
209 char *reply,
size_t *reply_len,
210 void (*msg_cb)(
char *msg,
size_t len))
216 char *cmd_buf =
NULL;
219 #ifdef CONFIG_CTRL_IFACE_UDP
222 _cmd_len =
os_strlen(ctrl->cookie) + 1 + cmd_len;
239 if (send(ctrl->
s, _cmd, _cmd_len, 0) < 0) {
249 FD_SET(ctrl->
s, &rfds);
250 res = select(ctrl->
s + 1, &rfds,
NULL,
NULL, &tv);
251 if (FD_ISSET(ctrl->
s, &rfds)) {
252 res = recv(ctrl->
s, reply, *reply_len, 0);
255 if (res > 0 && reply[0] ==
'<') {
263 if ((
size_t) res == *reply_len)
264 res = (*reply_len) - 1;
281 static int wpa_ctrl_attach_helper(
struct wpa_ctrl *ctrl,
int attach)
291 if (len == 3 &&
os_memcmp(buf,
"OK\n", 3) == 0)
299 return wpa_ctrl_attach_helper(ctrl, 1);
305 return wpa_ctrl_attach_helper(ctrl, 0);
309 #ifdef CTRL_IFACE_SOCKET
315 res = recv(ctrl->
s, reply, *reply_len, 0);
330 FD_SET(ctrl->
s, &rfds);
331 select(ctrl->
s + 1, &rfds,
NULL,
NULL, &tv);
332 return FD_ISSET(ctrl->
s, &rfds);
344 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
346 #ifndef WPA_SUPPLICANT_NAMED_PIPE
347 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
349 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
364 if (ctrl_path ==
NULL)
365 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
367 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT(
"-%S"),
370 if (ctrl_path ==
NULL)
373 ret =
os_snprintf(name, 256, NAMED_PIPE_PREFIX
"-%s",
376 if (ret < 0 || ret >= 256) {
381 for (i = 0; i < 10; i++) {
382 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
391 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
392 GetLastError() != ERROR_PIPE_BUSY)
394 WaitNamedPipe(name, 1000);
396 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
401 mode = PIPE_READMODE_MESSAGE;
402 if (!SetNamedPipeHandleState(ctrl->pipe, &mode,
NULL,
NULL)) {
403 CloseHandle(ctrl->pipe);
414 CloseHandle(ctrl->pipe);
420 char *reply,
size_t *reply_len,
421 void (*msg_cb)(
char *msg,
size_t len))
424 DWORD readlen = *reply_len;
426 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written,
NULL))
429 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen,
NULL))
431 *reply_len = readlen;
439 DWORD len = *reply_len;
440 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len,
NULL))
451 if (!PeekNamedPipe(ctrl->pipe,
NULL, 0,
NULL, &left,
NULL))
int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
#define os_memset(s, c, n)
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len, void(*msg_cb)(char *msg, size_t len))
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
#define os_memcpy(d, s, n)
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
size_t os_strlcpy(char *dest, const char *src, size_t siz)
int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
#define os_memcmp(s1, s2, n)