Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wpa_supplicant/hostapd control interface library |
| 3 | * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | #include <errno.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/socket.h> |
| 16 | #ifdef __QNXNTO__ |
| 17 | #include <sys/select.h> |
| 18 | #endif /* __QNXNTO__ */ |
| 19 | #include <sys/stat.h> |
| 20 | |
| 21 | #define CONFIG_CTRL_IFACE |
| 22 | #define CONFIG_CTRL_IFACE_UNIX |
| 23 | #define os_malloc malloc |
| 24 | #define os_free free |
| 25 | #define os_memset memset |
| 26 | #define os_memcmp memcmp |
| 27 | #define os_snprintf snprintf |
| 28 | #define os_strlen strlen |
| 29 | #define os_strncmp strncmp |
Peng Xu | 769731a | 2017-05-10 17:27:28 -0700 | [diff] [blame] | 30 | #define os_strlcpy strlcpy |
| 31 | #include "sigma_dut.h" |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 32 | |
| 33 | #ifdef CONFIG_CTRL_IFACE |
| 34 | |
| 35 | #ifdef CONFIG_CTRL_IFACE_UNIX |
| 36 | #include <sys/un.h> |
| 37 | #endif /* CONFIG_CTRL_IFACE_UNIX */ |
| 38 | |
| 39 | #ifdef ANDROID |
| 40 | #include <dirent.h> |
| 41 | #include <cutils/sockets.h> |
| 42 | #include "private/android_filesystem_config.h" |
| 43 | #endif /* ANDROID */ |
| 44 | |
| 45 | #include "wpa_ctrl.h" |
| 46 | |
| 47 | |
| 48 | #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP) |
| 49 | #define CTRL_IFACE_SOCKET |
| 50 | #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */ |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * struct wpa_ctrl - Internal structure for control interface library |
| 55 | * |
| 56 | * This structure is used by the wpa_supplicant/hostapd control interface |
| 57 | * library to store internal data. Programs using the library should not touch |
| 58 | * this data directly. They can only use the pointer to the data structure as |
| 59 | * an identifier for the control interface connection and use this as one of |
| 60 | * the arguments for most of the control interface library functions. |
| 61 | */ |
| 62 | struct wpa_ctrl { |
| 63 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 64 | int s; |
| 65 | struct sockaddr_in local; |
| 66 | struct sockaddr_in dest; |
| 67 | char *cookie; |
| 68 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 69 | #ifdef CONFIG_CTRL_IFACE_UNIX |
| 70 | int s; |
| 71 | struct sockaddr_un local; |
| 72 | struct sockaddr_un dest; |
| 73 | #endif /* CONFIG_CTRL_IFACE_UNIX */ |
| 74 | #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE |
| 75 | HANDLE pipe; |
| 76 | #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | #ifdef CONFIG_CTRL_IFACE_UNIX |
| 81 | |
| 82 | #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR |
| 83 | #define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp" |
| 84 | #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */ |
| 85 | #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX |
| 86 | #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_" |
| 87 | #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */ |
| 88 | |
| 89 | |
| 90 | struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) |
| 91 | { |
| 92 | struct wpa_ctrl *ctrl; |
| 93 | static int counter = 0; |
| 94 | int ret; |
| 95 | size_t res; |
| 96 | int tries = 0; |
| 97 | |
Pradeep Reddy POTTETI | 5df800e | 2016-01-29 13:21:41 +0530 | [diff] [blame] | 98 | if (ctrl_path == NULL) |
| 99 | return NULL; |
| 100 | |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 101 | ctrl = os_malloc(sizeof(*ctrl)); |
| 102 | if (ctrl == NULL) |
| 103 | return NULL; |
| 104 | os_memset(ctrl, 0, sizeof(*ctrl)); |
| 105 | |
| 106 | ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 107 | if (ctrl->s < 0) { |
| 108 | os_free(ctrl); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | ctrl->local.sun_family = AF_UNIX; |
| 113 | counter++; |
| 114 | try_again: |
| 115 | ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path), |
| 116 | CONFIG_CTRL_IFACE_CLIENT_DIR "/" |
| 117 | CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", |
| 118 | (int) getpid(), counter); |
| 119 | if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) { |
| 120 | close(ctrl->s); |
| 121 | os_free(ctrl); |
| 122 | return NULL; |
| 123 | } |
| 124 | tries++; |
| 125 | if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, |
| 126 | sizeof(ctrl->local)) < 0) { |
| 127 | if (errno == EADDRINUSE && tries < 2) { |
| 128 | /* |
| 129 | * getpid() returns unique identifier for this instance |
| 130 | * of wpa_ctrl, so the existing socket file must have |
| 131 | * been left by unclean termination of an earlier run. |
| 132 | * Remove the file and try again. |
| 133 | */ |
| 134 | unlink(ctrl->local.sun_path); |
| 135 | goto try_again; |
| 136 | } |
| 137 | close(ctrl->s); |
| 138 | os_free(ctrl); |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | #ifdef ANDROID |
| 143 | chmod(ctrl->local.sun_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 144 | chown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI); |
| 145 | /* |
| 146 | * If the ctrl_path isn't an absolute pathname, assume that |
| 147 | * it's the name of a socket in the Android reserved namespace. |
| 148 | * Otherwise, it's a normal UNIX domain socket appearing in the |
| 149 | * filesystem. |
| 150 | */ |
Pradeep Reddy POTTETI | 5df800e | 2016-01-29 13:21:41 +0530 | [diff] [blame] | 151 | if (*ctrl_path != '/') { |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 152 | char buf[21]; |
| 153 | os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path); |
| 154 | if (socket_local_client_connect( |
| 155 | ctrl->s, buf, |
| 156 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 157 | SOCK_DGRAM) < 0) { |
| 158 | close(ctrl->s); |
| 159 | unlink(ctrl->local.sun_path); |
| 160 | os_free(ctrl); |
| 161 | return NULL; |
| 162 | } |
| 163 | return ctrl; |
| 164 | } |
| 165 | #endif /* ANDROID */ |
| 166 | |
| 167 | ctrl->dest.sun_family = AF_UNIX; |
| 168 | res = os_strlcpy(ctrl->dest.sun_path, ctrl_path, |
| 169 | sizeof(ctrl->dest.sun_path)); |
| 170 | if (res >= sizeof(ctrl->dest.sun_path)) { |
| 171 | close(ctrl->s); |
| 172 | os_free(ctrl); |
| 173 | return NULL; |
| 174 | } |
| 175 | if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, |
| 176 | sizeof(ctrl->dest)) < 0) { |
| 177 | close(ctrl->s); |
| 178 | unlink(ctrl->local.sun_path); |
| 179 | os_free(ctrl); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | return ctrl; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void wpa_ctrl_close(struct wpa_ctrl *ctrl) |
| 188 | { |
| 189 | if (ctrl == NULL) |
| 190 | return; |
| 191 | unlink(ctrl->local.sun_path); |
| 192 | if (ctrl->s >= 0) |
| 193 | close(ctrl->s); |
| 194 | os_free(ctrl); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | #ifdef ANDROID |
| 199 | /** |
| 200 | * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that |
| 201 | * may be left over from clients that were previously connected to |
| 202 | * wpa_supplicant. This keeps these files from being orphaned in the |
| 203 | * event of crashes that prevented them from being removed as part |
| 204 | * of the normal orderly shutdown. |
| 205 | */ |
| 206 | void wpa_ctrl_cleanup(void) |
| 207 | { |
| 208 | DIR *dir; |
| 209 | struct dirent entry; |
| 210 | struct dirent *result; |
| 211 | size_t dirnamelen; |
| 212 | int prefixlen = os_strlen(CONFIG_CTRL_IFACE_CLIENT_PREFIX); |
| 213 | size_t maxcopy; |
| 214 | char pathname[PATH_MAX]; |
| 215 | char *namep; |
| 216 | |
| 217 | if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL) |
| 218 | return; |
| 219 | |
| 220 | dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/", |
| 221 | CONFIG_CTRL_IFACE_CLIENT_DIR); |
| 222 | if (dirnamelen >= sizeof(pathname)) { |
| 223 | closedir(dir); |
| 224 | return; |
| 225 | } |
| 226 | namep = pathname + dirnamelen; |
| 227 | maxcopy = PATH_MAX - dirnamelen; |
| 228 | while (readdir_r(dir, &entry, &result) == 0 && result != NULL) { |
| 229 | if (os_strncmp(entry.d_name, CONFIG_CTRL_IFACE_CLIENT_PREFIX, |
| 230 | prefixlen) == 0) { |
| 231 | if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy) |
| 232 | unlink(pathname); |
| 233 | } |
| 234 | } |
| 235 | closedir(dir); |
| 236 | } |
| 237 | #endif /* ANDROID */ |
| 238 | |
| 239 | #else /* CONFIG_CTRL_IFACE_UNIX */ |
| 240 | |
| 241 | #ifdef ANDROID |
| 242 | void wpa_ctrl_cleanup(void) |
| 243 | { |
| 244 | } |
| 245 | #endif /* ANDROID */ |
| 246 | |
| 247 | #endif /* CONFIG_CTRL_IFACE_UNIX */ |
| 248 | |
| 249 | |
| 250 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 251 | |
| 252 | struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) |
| 253 | { |
| 254 | struct wpa_ctrl *ctrl; |
| 255 | char buf[128]; |
| 256 | size_t len; |
| 257 | |
| 258 | ctrl = os_malloc(sizeof(*ctrl)); |
| 259 | if (ctrl == NULL) |
| 260 | return NULL; |
| 261 | os_memset(ctrl, 0, sizeof(*ctrl)); |
| 262 | |
| 263 | ctrl->s = socket(PF_INET, SOCK_DGRAM, 0); |
| 264 | if (ctrl->s < 0) { |
| 265 | perror("socket"); |
| 266 | os_free(ctrl); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | ctrl->local.sin_family = AF_INET; |
| 271 | ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1); |
| 272 | if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, |
| 273 | sizeof(ctrl->local)) < 0) { |
| 274 | close(ctrl->s); |
| 275 | os_free(ctrl); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | ctrl->dest.sin_family = AF_INET; |
| 280 | ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1); |
| 281 | ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT); |
| 282 | if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, |
| 283 | sizeof(ctrl->dest)) < 0) { |
| 284 | perror("connect"); |
| 285 | close(ctrl->s); |
| 286 | os_free(ctrl); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | len = sizeof(buf) - 1; |
| 291 | if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) { |
| 292 | buf[len] = '\0'; |
| 293 | ctrl->cookie = os_strdup(buf); |
| 294 | } |
| 295 | |
| 296 | return ctrl; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | void wpa_ctrl_close(struct wpa_ctrl *ctrl) |
| 301 | { |
| 302 | close(ctrl->s); |
| 303 | os_free(ctrl->cookie); |
| 304 | os_free(ctrl); |
| 305 | } |
| 306 | |
| 307 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 308 | |
| 309 | |
| 310 | #ifdef CTRL_IFACE_SOCKET |
| 311 | int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, |
| 312 | char *reply, size_t *reply_len, |
| 313 | void (*msg_cb)(char *msg, size_t len)) |
| 314 | { |
| 315 | struct timeval tv; |
| 316 | int res; |
| 317 | fd_set rfds; |
| 318 | const char *_cmd; |
| 319 | char *cmd_buf = NULL; |
| 320 | size_t _cmd_len; |
| 321 | |
| 322 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 323 | if (ctrl->cookie) { |
| 324 | char *pos; |
| 325 | _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len; |
| 326 | cmd_buf = os_malloc(_cmd_len); |
| 327 | if (cmd_buf == NULL) |
| 328 | return -1; |
| 329 | _cmd = cmd_buf; |
| 330 | pos = cmd_buf; |
| 331 | os_strlcpy(pos, ctrl->cookie, _cmd_len); |
| 332 | pos += os_strlen(ctrl->cookie); |
| 333 | *pos++ = ' '; |
| 334 | os_memcpy(pos, cmd, cmd_len); |
| 335 | } else |
| 336 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 337 | { |
| 338 | _cmd = cmd; |
| 339 | _cmd_len = cmd_len; |
| 340 | } |
| 341 | |
| 342 | if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) { |
| 343 | os_free(cmd_buf); |
| 344 | return -1; |
| 345 | } |
| 346 | os_free(cmd_buf); |
| 347 | |
| 348 | for (;;) { |
| 349 | tv.tv_sec = 10; |
| 350 | tv.tv_usec = 0; |
| 351 | FD_ZERO(&rfds); |
| 352 | FD_SET(ctrl->s, &rfds); |
| 353 | res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); |
| 354 | if (res < 0) |
| 355 | return res; |
| 356 | if (FD_ISSET(ctrl->s, &rfds)) { |
| 357 | res = recv(ctrl->s, reply, *reply_len, 0); |
| 358 | if (res < 0) |
| 359 | return res; |
| 360 | if (res > 0 && reply[0] == '<') { |
| 361 | /* This is an unsolicited message from |
| 362 | * wpa_supplicant, not the reply to the |
| 363 | * request. Use msg_cb to report this to the |
| 364 | * caller. */ |
| 365 | if (msg_cb) { |
| 366 | /* Make sure the message is nul |
| 367 | * terminated. */ |
| 368 | if ((size_t) res == *reply_len) |
| 369 | res = (*reply_len) - 1; |
| 370 | reply[res] = '\0'; |
| 371 | msg_cb(reply, res); |
| 372 | } |
| 373 | continue; |
| 374 | } |
| 375 | *reply_len = res; |
| 376 | break; |
| 377 | } else { |
| 378 | return -2; |
| 379 | } |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | #endif /* CTRL_IFACE_SOCKET */ |
| 384 | |
| 385 | |
| 386 | static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach) |
| 387 | { |
| 388 | char buf[10]; |
| 389 | int ret; |
| 390 | size_t len = 10; |
| 391 | |
| 392 | ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, |
| 393 | buf, &len, NULL); |
| 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0) |
| 397 | return 0; |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | int wpa_ctrl_attach(struct wpa_ctrl *ctrl) |
| 403 | { |
| 404 | return wpa_ctrl_attach_helper(ctrl, 1); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | int wpa_ctrl_detach(struct wpa_ctrl *ctrl) |
| 409 | { |
| 410 | return wpa_ctrl_attach_helper(ctrl, 0); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | #ifdef CTRL_IFACE_SOCKET |
| 415 | |
| 416 | int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) |
| 417 | { |
| 418 | int res; |
| 419 | |
| 420 | res = recv(ctrl->s, reply, *reply_len, 0); |
| 421 | if (res < 0) |
| 422 | return res; |
| 423 | *reply_len = res; |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | int wpa_ctrl_pending(struct wpa_ctrl *ctrl) |
| 429 | { |
| 430 | struct timeval tv; |
| 431 | fd_set rfds; |
| 432 | tv.tv_sec = 0; |
| 433 | tv.tv_usec = 0; |
| 434 | FD_ZERO(&rfds); |
| 435 | FD_SET(ctrl->s, &rfds); |
| 436 | select(ctrl->s + 1, &rfds, NULL, NULL, &tv); |
| 437 | return FD_ISSET(ctrl->s, &rfds); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) |
| 442 | { |
| 443 | return ctrl->s; |
| 444 | } |
| 445 | |
| 446 | #endif /* CTRL_IFACE_SOCKET */ |
| 447 | |
| 448 | |
| 449 | #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE |
| 450 | |
| 451 | #ifndef WPA_SUPPLICANT_NAMED_PIPE |
| 452 | #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant" |
| 453 | #endif |
| 454 | #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE) |
| 455 | |
| 456 | struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) |
| 457 | { |
| 458 | struct wpa_ctrl *ctrl; |
| 459 | DWORD mode; |
| 460 | TCHAR name[256]; |
| 461 | int i, ret; |
| 462 | |
| 463 | ctrl = os_malloc(sizeof(*ctrl)); |
| 464 | if (ctrl == NULL) |
| 465 | return NULL; |
| 466 | os_memset(ctrl, 0, sizeof(*ctrl)); |
| 467 | |
| 468 | #ifdef UNICODE |
| 469 | if (ctrl_path == NULL) |
| 470 | ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX); |
| 471 | else |
| 472 | ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"), |
| 473 | ctrl_path); |
| 474 | #else /* UNICODE */ |
| 475 | if (ctrl_path == NULL) |
| 476 | ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX); |
| 477 | else |
| 478 | ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s", |
| 479 | ctrl_path); |
| 480 | #endif /* UNICODE */ |
| 481 | if (ret < 0 || ret >= 256) { |
| 482 | os_free(ctrl); |
| 483 | return NULL; |
| 484 | } |
| 485 | |
| 486 | for (i = 0; i < 10; i++) { |
| 487 | ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, |
| 488 | NULL, OPEN_EXISTING, 0, NULL); |
| 489 | /* |
| 490 | * Current named pipe server side in wpa_supplicant is |
| 491 | * re-opening the pipe for new clients only after the previous |
| 492 | * one is taken into use. This leaves a small window for race |
| 493 | * conditions when two connections are being opened at almost |
| 494 | * the same time. Retry if that was the case. |
| 495 | */ |
| 496 | if (ctrl->pipe != INVALID_HANDLE_VALUE || |
| 497 | GetLastError() != ERROR_PIPE_BUSY) |
| 498 | break; |
| 499 | WaitNamedPipe(name, 1000); |
| 500 | } |
| 501 | if (ctrl->pipe == INVALID_HANDLE_VALUE) { |
| 502 | os_free(ctrl); |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | mode = PIPE_READMODE_MESSAGE; |
| 507 | if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) { |
| 508 | CloseHandle(ctrl->pipe); |
| 509 | os_free(ctrl); |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | return ctrl; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | void wpa_ctrl_close(struct wpa_ctrl *ctrl) |
| 518 | { |
| 519 | CloseHandle(ctrl->pipe); |
| 520 | os_free(ctrl); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, |
| 525 | char *reply, size_t *reply_len, |
| 526 | void (*msg_cb)(char *msg, size_t len)) |
| 527 | { |
| 528 | DWORD written; |
| 529 | DWORD readlen = *reply_len; |
| 530 | |
| 531 | if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL)) |
| 532 | return -1; |
| 533 | |
| 534 | if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL)) |
| 535 | return -1; |
| 536 | *reply_len = readlen; |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) |
| 543 | { |
| 544 | DWORD len = *reply_len; |
| 545 | if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL)) |
| 546 | return -1; |
| 547 | *reply_len = len; |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | int wpa_ctrl_pending(struct wpa_ctrl *ctrl) |
| 553 | { |
| 554 | DWORD left; |
| 555 | |
| 556 | if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL)) |
| 557 | return -1; |
| 558 | return left ? 1 : 0; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) |
| 563 | { |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ |
| 568 | |
| 569 | #endif /* CONFIG_CTRL_IFACE */ |