34
39
* the arguments for most of the control interface library functions.
42
#ifdef CONFIG_CTRL_IFACE_UDP
38
#ifdef CONFIG_CTRL_IFACE_UDP
39
44
struct sockaddr_in local;
40
45
struct sockaddr_in dest;
41
#else /* CONFIG_CTRL_IFACE_UDP */
47
#endif /* CONFIG_CTRL_IFACE_UDP */
48
#ifdef CONFIG_CTRL_IFACE_UNIX
42
50
struct sockaddr_un local;
43
51
struct sockaddr_un dest;
44
#endif /* CONFIG_CTRL_IFACE_UDP */
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
48
61
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
50
63
struct wpa_ctrl *ctrl;
51
#ifndef CONFIG_CTRL_IFACE_UDP
52
64
static int counter = 0;
53
#endif /* CONFIG_CTRL_IFACE_UDP */
55
66
ctrl = malloc(sizeof(*ctrl));
58
69
memset(ctrl, 0, sizeof(*ctrl));
60
#ifdef CONFIG_CTRL_IFACE_UDP
61
ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
68
ctrl->local.sin_family = AF_INET;
69
ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
70
if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
71
sizeof(ctrl->local)) < 0) {
77
ctrl->dest.sin_family = AF_INET;
78
ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
79
ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
80
if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
81
sizeof(ctrl->dest)) < 0) {
87
#else /* CONFIG_CTRL_IFACE_UDP */
88
71
ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
120
102
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
122
#ifndef CONFIG_CTRL_IFACE_UDP
123
104
unlink(ctrl->local.sun_path);
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;
120
ctrl = malloc(sizeof(*ctrl));
123
memset(ctrl, 0, sizeof(*ctrl));
125
ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
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) {
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) {
152
len = sizeof(buf) - 1;
153
if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
155
ctrl->cookie = strdup(buf);
162
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
124
169
#endif /* CONFIG_CTRL_IFACE_UDP */
172
#ifdef CTRL_IFACE_SOCKET
130
173
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
131
174
char *reply, size_t *reply_len,
132
175
void (*msg_cb)(char *msg, size_t len))
134
177
struct timeval tv;
138
if (send(ctrl->s, cmd, cmd_len, 0) < 0)
181
char *cmd_buf = NULL;
184
#ifdef CONFIG_CTRL_IFACE_UDP
187
_cmd_len = strlen(ctrl->cookie) + 1 + cmd_len;
188
cmd_buf = malloc(_cmd_len );
193
strcpy(pos, ctrl->cookie);
194
pos += strlen(ctrl->cookie);
196
memcpy(pos, cmd, cmd_len);
198
#endif /* CONFIG_CTRL_IFACE_UDP */
204
if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
307
#endif /* CTRL_IFACE_SOCKET */
310
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
312
#ifndef WPA_SUPPLICANT_NAMED_PIPE
313
#define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
315
#define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
317
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
319
struct wpa_ctrl *ctrl;
324
ctrl = malloc(sizeof(*ctrl));
327
memset(ctrl, 0, sizeof(*ctrl));
330
if (ctrl_path == NULL)
331
_snwprintf(name, 256, NAMED_PIPE_PREFIX);
333
_snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
336
if (ctrl_path == NULL)
337
snprintf(name, 256, NAMED_PIPE_PREFIX);
339
snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
343
for (i = 0; i < 10; i++) {
344
ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
345
NULL, OPEN_EXISTING, 0, NULL);
347
* Current named pipe server side in wpa_supplicant is
348
* re-opening the pipe for new clients only after the previous
349
* one is taken into use. This leaves a small window for race
350
* conditions when two connections are being opened at almost
351
* the same time. Retry if that was the case.
353
if (ctrl->pipe != INVALID_HANDLE_VALUE ||
354
GetLastError() != ERROR_PIPE_BUSY)
356
WaitNamedPipe(name, 1000);
358
if (ctrl->pipe == INVALID_HANDLE_VALUE) {
363
mode = PIPE_READMODE_MESSAGE;
364
if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
365
CloseHandle(ctrl->pipe);
374
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
376
CloseHandle(ctrl->pipe);
381
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
382
char *reply, size_t *reply_len,
383
void (*msg_cb)(char *msg, size_t len))
386
DWORD readlen = *reply_len;
388
if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
391
if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
393
*reply_len = readlen;
399
int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
401
DWORD len = *reply_len;
402
if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
409
int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
413
if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
419
int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
424
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
235
426
#endif /* CONFIG_CTRL_IFACE */