1e5b75505Sopenharmony_ci/* 2e5b75505Sopenharmony_ci * wpa_supplicant/hostapd control interface library 3e5b75505Sopenharmony_ci * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi> 4e5b75505Sopenharmony_ci * 5e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license. 6e5b75505Sopenharmony_ci * See README for more details. 7e5b75505Sopenharmony_ci */ 8e5b75505Sopenharmony_ci 9e5b75505Sopenharmony_ci#include "includes.h" 10e5b75505Sopenharmony_ci 11e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE 12e5b75505Sopenharmony_ci 13e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UNIX 14e5b75505Sopenharmony_ci#include <sys/stat.h> 15e5b75505Sopenharmony_ci#include <fcntl.h> 16e5b75505Sopenharmony_ci#include <sys/un.h> 17e5b75505Sopenharmony_ci#include <unistd.h> 18e5b75505Sopenharmony_ci#include <fcntl.h> 19e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UNIX */ 20e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 21e5b75505Sopenharmony_ci#include <netdb.h> 22e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 23e5b75505Sopenharmony_ci 24e5b75505Sopenharmony_ci#ifdef ANDROID 25e5b75505Sopenharmony_ci#include <dirent.h> 26e5b75505Sopenharmony_ci#include <sys/stat.h> 27e5b75505Sopenharmony_ci#include <cutils/sockets.h> 28e5b75505Sopenharmony_ci#include "private/android_filesystem_config.h" 29e5b75505Sopenharmony_ci#endif /* ANDROID */ 30e5b75505Sopenharmony_ci 31e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 32e5b75505Sopenharmony_ci#include <net/if.h> 33e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 34e5b75505Sopenharmony_ci 35e5b75505Sopenharmony_ci#include "wpa_ctrl.h" 36e5b75505Sopenharmony_ci#include "common.h" 37e5b75505Sopenharmony_ci 38e5b75505Sopenharmony_ci 39e5b75505Sopenharmony_ci#if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP) 40e5b75505Sopenharmony_ci#define CTRL_IFACE_SOCKET 41e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */ 42e5b75505Sopenharmony_ci 43e5b75505Sopenharmony_ci 44e5b75505Sopenharmony_ci/** 45e5b75505Sopenharmony_ci * struct wpa_ctrl - Internal structure for control interface library 46e5b75505Sopenharmony_ci * 47e5b75505Sopenharmony_ci * This structure is used by the wpa_supplicant/hostapd control interface 48e5b75505Sopenharmony_ci * library to store internal data. Programs using the library should not touch 49e5b75505Sopenharmony_ci * this data directly. They can only use the pointer to the data structure as 50e5b75505Sopenharmony_ci * an identifier for the control interface connection and use this as one of 51e5b75505Sopenharmony_ci * the arguments for most of the control interface library functions. 52e5b75505Sopenharmony_ci */ 53e5b75505Sopenharmony_cistruct wpa_ctrl { 54e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP 55e5b75505Sopenharmony_ci int s; 56e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 57e5b75505Sopenharmony_ci struct sockaddr_in6 local; 58e5b75505Sopenharmony_ci struct sockaddr_in6 dest; 59e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 60e5b75505Sopenharmony_ci struct sockaddr_in local; 61e5b75505Sopenharmony_ci struct sockaddr_in dest; 62e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 63e5b75505Sopenharmony_ci char *cookie; 64e5b75505Sopenharmony_ci char *remote_ifname; 65e5b75505Sopenharmony_ci char *remote_ip; 66e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP */ 67e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UNIX 68e5b75505Sopenharmony_ci int s; 69e5b75505Sopenharmony_ci struct sockaddr_un local; 70e5b75505Sopenharmony_ci struct sockaddr_un dest; 71e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UNIX */ 72e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE 73e5b75505Sopenharmony_ci HANDLE pipe; 74e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ 75e5b75505Sopenharmony_ci}; 76e5b75505Sopenharmony_ci 77e5b75505Sopenharmony_ci 78e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UNIX 79e5b75505Sopenharmony_ci 80e5b75505Sopenharmony_ci#ifndef CONFIG_CTRL_IFACE_CLIENT_DIR 81e5b75505Sopenharmony_ci#define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp" 82e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */ 83e5b75505Sopenharmony_ci#ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX 84e5b75505Sopenharmony_ci#define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_" 85e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */ 86e5b75505Sopenharmony_ci 87e5b75505Sopenharmony_ci 88e5b75505Sopenharmony_cistruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 89e5b75505Sopenharmony_ci{ 90e5b75505Sopenharmony_ci return wpa_ctrl_open2(ctrl_path, NULL); 91e5b75505Sopenharmony_ci} 92e5b75505Sopenharmony_ci 93e5b75505Sopenharmony_ci 94e5b75505Sopenharmony_cistruct wpa_ctrl * wpa_ctrl_open2(const char *ctrl_path, 95e5b75505Sopenharmony_ci const char *cli_path) 96e5b75505Sopenharmony_ci{ 97e5b75505Sopenharmony_ci struct wpa_ctrl *ctrl; 98e5b75505Sopenharmony_ci static int counter = 0; 99e5b75505Sopenharmony_ci int ret; 100e5b75505Sopenharmony_ci size_t res; 101e5b75505Sopenharmony_ci int tries = 0; 102e5b75505Sopenharmony_ci int flags; 103e5b75505Sopenharmony_ci 104e5b75505Sopenharmony_ci if (ctrl_path == NULL) 105e5b75505Sopenharmony_ci return NULL; 106e5b75505Sopenharmony_ci 107e5b75505Sopenharmony_ci ctrl = os_zalloc(sizeof(*ctrl)); 108e5b75505Sopenharmony_ci if (ctrl == NULL) 109e5b75505Sopenharmony_ci return NULL; 110e5b75505Sopenharmony_ci 111e5b75505Sopenharmony_ci ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); 112e5b75505Sopenharmony_ci if (ctrl->s < 0) { 113e5b75505Sopenharmony_ci os_free(ctrl); 114e5b75505Sopenharmony_ci return NULL; 115e5b75505Sopenharmony_ci } 116e5b75505Sopenharmony_ci 117e5b75505Sopenharmony_ci ctrl->local.sun_family = AF_UNIX; 118e5b75505Sopenharmony_ci counter++; 119e5b75505Sopenharmony_citry_again: 120e5b75505Sopenharmony_ci if (cli_path && cli_path[0] == '/') { 121e5b75505Sopenharmony_ci ret = os_snprintf(ctrl->local.sun_path, 122e5b75505Sopenharmony_ci sizeof(ctrl->local.sun_path), 123e5b75505Sopenharmony_ci "%s/" CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", 124e5b75505Sopenharmony_ci cli_path, (int) getpid(), counter); 125e5b75505Sopenharmony_ci } else { 126e5b75505Sopenharmony_ci ret = os_snprintf(ctrl->local.sun_path, 127e5b75505Sopenharmony_ci sizeof(ctrl->local.sun_path), 128e5b75505Sopenharmony_ci CONFIG_CTRL_IFACE_CLIENT_DIR "/" 129e5b75505Sopenharmony_ci CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", 130e5b75505Sopenharmony_ci (int) getpid(), counter); 131e5b75505Sopenharmony_ci } 132e5b75505Sopenharmony_ci if (os_snprintf_error(sizeof(ctrl->local.sun_path), ret)) { 133e5b75505Sopenharmony_ci close(ctrl->s); 134e5b75505Sopenharmony_ci os_free(ctrl); 135e5b75505Sopenharmony_ci return NULL; 136e5b75505Sopenharmony_ci } 137e5b75505Sopenharmony_ci tries++; 138e5b75505Sopenharmony_ci#ifdef ANDROID 139e5b75505Sopenharmony_ci /* Set client socket file permissions so that bind() creates the client 140e5b75505Sopenharmony_ci * socket with these permissions and there is no need to try to change 141e5b75505Sopenharmony_ci * them with chmod() after bind() which would have potential issues with 142e5b75505Sopenharmony_ci * race conditions. These permissions are needed to make sure the server 143e5b75505Sopenharmony_ci * side (wpa_supplicant or hostapd) can reply to the control interface 144e5b75505Sopenharmony_ci * messages. 145e5b75505Sopenharmony_ci * 146e5b75505Sopenharmony_ci * The lchown() calls below after bind() are also part of the needed 147e5b75505Sopenharmony_ci * operations to allow the response to go through. Those are using the 148e5b75505Sopenharmony_ci * no-deference-symlinks version to avoid races. */ 149e5b75505Sopenharmony_ci fchmod(ctrl->s, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 150e5b75505Sopenharmony_ci#endif /* ANDROID */ 151e5b75505Sopenharmony_ci if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, 152e5b75505Sopenharmony_ci sizeof(ctrl->local)) < 0) { 153e5b75505Sopenharmony_ci if (errno == EADDRINUSE && tries < 2) { 154e5b75505Sopenharmony_ci /* 155e5b75505Sopenharmony_ci * getpid() returns unique identifier for this instance 156e5b75505Sopenharmony_ci * of wpa_ctrl, so the existing socket file must have 157e5b75505Sopenharmony_ci * been left by unclean termination of an earlier run. 158e5b75505Sopenharmony_ci * Remove the file and try again. 159e5b75505Sopenharmony_ci */ 160e5b75505Sopenharmony_ci unlink(ctrl->local.sun_path); 161e5b75505Sopenharmony_ci goto try_again; 162e5b75505Sopenharmony_ci } 163e5b75505Sopenharmony_ci close(ctrl->s); 164e5b75505Sopenharmony_ci os_free(ctrl); 165e5b75505Sopenharmony_ci return NULL; 166e5b75505Sopenharmony_ci } 167e5b75505Sopenharmony_ci 168e5b75505Sopenharmony_ci#ifdef ANDROID 169e5b75505Sopenharmony_ci /* Set group even if we do not have privileges to change owner */ 170e5b75505Sopenharmony_ci lchown(ctrl->local.sun_path, -1, AID_WIFI); 171e5b75505Sopenharmony_ci lchown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI); 172e5b75505Sopenharmony_ci 173e5b75505Sopenharmony_ci if (os_strncmp(ctrl_path, "@android:", 9) == 0) { 174e5b75505Sopenharmony_ci if (socket_local_client_connect( 175e5b75505Sopenharmony_ci ctrl->s, ctrl_path + 9, 176e5b75505Sopenharmony_ci ANDROID_SOCKET_NAMESPACE_RESERVED, 177e5b75505Sopenharmony_ci SOCK_DGRAM) < 0) { 178e5b75505Sopenharmony_ci close(ctrl->s); 179e5b75505Sopenharmony_ci unlink(ctrl->local.sun_path); 180e5b75505Sopenharmony_ci os_free(ctrl); 181e5b75505Sopenharmony_ci return NULL; 182e5b75505Sopenharmony_ci } 183e5b75505Sopenharmony_ci return ctrl; 184e5b75505Sopenharmony_ci } 185e5b75505Sopenharmony_ci 186e5b75505Sopenharmony_ci /* 187e5b75505Sopenharmony_ci * If the ctrl_path isn't an absolute pathname, assume that 188e5b75505Sopenharmony_ci * it's the name of a socket in the Android reserved namespace. 189e5b75505Sopenharmony_ci * Otherwise, it's a normal UNIX domain socket appearing in the 190e5b75505Sopenharmony_ci * filesystem. 191e5b75505Sopenharmony_ci */ 192e5b75505Sopenharmony_ci if (*ctrl_path != '/') { 193e5b75505Sopenharmony_ci char buf[21]; 194e5b75505Sopenharmony_ci os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path); 195e5b75505Sopenharmony_ci if (socket_local_client_connect( 196e5b75505Sopenharmony_ci ctrl->s, buf, 197e5b75505Sopenharmony_ci ANDROID_SOCKET_NAMESPACE_RESERVED, 198e5b75505Sopenharmony_ci SOCK_DGRAM) < 0) { 199e5b75505Sopenharmony_ci close(ctrl->s); 200e5b75505Sopenharmony_ci unlink(ctrl->local.sun_path); 201e5b75505Sopenharmony_ci os_free(ctrl); 202e5b75505Sopenharmony_ci return NULL; 203e5b75505Sopenharmony_ci } 204e5b75505Sopenharmony_ci return ctrl; 205e5b75505Sopenharmony_ci } 206e5b75505Sopenharmony_ci#endif /* ANDROID */ 207e5b75505Sopenharmony_ci 208e5b75505Sopenharmony_ci ctrl->dest.sun_family = AF_UNIX; 209e5b75505Sopenharmony_ci if (os_strncmp(ctrl_path, "@abstract:", 10) == 0) { 210e5b75505Sopenharmony_ci ctrl->dest.sun_path[0] = '\0'; 211e5b75505Sopenharmony_ci os_strlcpy(ctrl->dest.sun_path + 1, ctrl_path + 10, 212e5b75505Sopenharmony_ci sizeof(ctrl->dest.sun_path) - 1); 213e5b75505Sopenharmony_ci } else { 214e5b75505Sopenharmony_ci res = os_strlcpy(ctrl->dest.sun_path, ctrl_path, 215e5b75505Sopenharmony_ci sizeof(ctrl->dest.sun_path)); 216e5b75505Sopenharmony_ci if (res >= sizeof(ctrl->dest.sun_path)) { 217e5b75505Sopenharmony_ci close(ctrl->s); 218e5b75505Sopenharmony_ci os_free(ctrl); 219e5b75505Sopenharmony_ci return NULL; 220e5b75505Sopenharmony_ci } 221e5b75505Sopenharmony_ci } 222e5b75505Sopenharmony_ci if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, 223e5b75505Sopenharmony_ci sizeof(ctrl->dest)) < 0) { 224e5b75505Sopenharmony_ci close(ctrl->s); 225e5b75505Sopenharmony_ci unlink(ctrl->local.sun_path); 226e5b75505Sopenharmony_ci os_free(ctrl); 227e5b75505Sopenharmony_ci return NULL; 228e5b75505Sopenharmony_ci } 229e5b75505Sopenharmony_ci 230e5b75505Sopenharmony_ci /* 231e5b75505Sopenharmony_ci * Make socket non-blocking so that we don't hang forever if 232e5b75505Sopenharmony_ci * target dies unexpectedly. 233e5b75505Sopenharmony_ci */ 234e5b75505Sopenharmony_ci flags = fcntl(ctrl->s, F_GETFL); 235e5b75505Sopenharmony_ci if (flags >= 0) { 236e5b75505Sopenharmony_ci flags |= O_NONBLOCK; 237e5b75505Sopenharmony_ci if (fcntl(ctrl->s, F_SETFL, flags) < 0) { 238e5b75505Sopenharmony_ci perror("fcntl(ctrl->s, O_NONBLOCK)"); 239e5b75505Sopenharmony_ci /* Not fatal, continue on.*/ 240e5b75505Sopenharmony_ci } 241e5b75505Sopenharmony_ci } 242e5b75505Sopenharmony_ci 243e5b75505Sopenharmony_ci return ctrl; 244e5b75505Sopenharmony_ci} 245e5b75505Sopenharmony_ci 246e5b75505Sopenharmony_ci 247e5b75505Sopenharmony_civoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 248e5b75505Sopenharmony_ci{ 249e5b75505Sopenharmony_ci if (ctrl == NULL) 250e5b75505Sopenharmony_ci return; 251e5b75505Sopenharmony_ci unlink(ctrl->local.sun_path); 252e5b75505Sopenharmony_ci if (ctrl->s >= 0) 253e5b75505Sopenharmony_ci close(ctrl->s); 254e5b75505Sopenharmony_ci os_free(ctrl); 255e5b75505Sopenharmony_ci} 256e5b75505Sopenharmony_ci 257e5b75505Sopenharmony_ci 258e5b75505Sopenharmony_ci#ifdef ANDROID 259e5b75505Sopenharmony_ci/** 260e5b75505Sopenharmony_ci * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that 261e5b75505Sopenharmony_ci * may be left over from clients that were previously connected to 262e5b75505Sopenharmony_ci * wpa_supplicant. This keeps these files from being orphaned in the 263e5b75505Sopenharmony_ci * event of crashes that prevented them from being removed as part 264e5b75505Sopenharmony_ci * of the normal orderly shutdown. 265e5b75505Sopenharmony_ci */ 266e5b75505Sopenharmony_civoid wpa_ctrl_cleanup(void) 267e5b75505Sopenharmony_ci{ 268e5b75505Sopenharmony_ci DIR *dir; 269e5b75505Sopenharmony_ci struct dirent entry; 270e5b75505Sopenharmony_ci struct dirent *result; 271e5b75505Sopenharmony_ci size_t dirnamelen; 272e5b75505Sopenharmony_ci size_t maxcopy; 273e5b75505Sopenharmony_ci char pathname[PATH_MAX]; 274e5b75505Sopenharmony_ci char *namep; 275e5b75505Sopenharmony_ci 276e5b75505Sopenharmony_ci if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL) 277e5b75505Sopenharmony_ci return; 278e5b75505Sopenharmony_ci 279e5b75505Sopenharmony_ci dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/", 280e5b75505Sopenharmony_ci CONFIG_CTRL_IFACE_CLIENT_DIR); 281e5b75505Sopenharmony_ci if (dirnamelen >= sizeof(pathname)) { 282e5b75505Sopenharmony_ci closedir(dir); 283e5b75505Sopenharmony_ci return; 284e5b75505Sopenharmony_ci } 285e5b75505Sopenharmony_ci namep = pathname + dirnamelen; 286e5b75505Sopenharmony_ci maxcopy = PATH_MAX - dirnamelen; 287e5b75505Sopenharmony_ci while (readdir_r(dir, &entry, &result) == 0 && result != NULL) { 288e5b75505Sopenharmony_ci if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy) 289e5b75505Sopenharmony_ci unlink(pathname); 290e5b75505Sopenharmony_ci } 291e5b75505Sopenharmony_ci closedir(dir); 292e5b75505Sopenharmony_ci} 293e5b75505Sopenharmony_ci#endif /* ANDROID */ 294e5b75505Sopenharmony_ci 295e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UNIX */ 296e5b75505Sopenharmony_ci 297e5b75505Sopenharmony_ci#ifdef ANDROID 298e5b75505Sopenharmony_civoid wpa_ctrl_cleanup(void) 299e5b75505Sopenharmony_ci{ 300e5b75505Sopenharmony_ci} 301e5b75505Sopenharmony_ci#endif /* ANDROID */ 302e5b75505Sopenharmony_ci 303e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UNIX */ 304e5b75505Sopenharmony_ci 305e5b75505Sopenharmony_ci#if defined(CONFIG_OPEN_HARMONY_PATCH) || defined(CONFIG_OPEN_HARMONY_PATCH_LITE) 306e5b75505Sopenharmony_ciint wpa_ctrl_port(const char *ctrl_path, struct wpa_ctrl *ctrl) 307e5b75505Sopenharmony_ci{ 308e5b75505Sopenharmony_ci if (ctrl_path == NULL || ctrl == NULL) { 309e5b75505Sopenharmony_ci return -1; 310e5b75505Sopenharmony_ci } 311e5b75505Sopenharmony_ci 312e5b75505Sopenharmony_ci if (os_strcmp(ctrl_path, "global") == 0) { 313e5b75505Sopenharmony_ci ctrl->dest.sin_port = htons(WPA_GLOBAL_CTRL_IFACE_PORT); 314e5b75505Sopenharmony_ci return 0; 315e5b75505Sopenharmony_ci } 316e5b75505Sopenharmony_ci 317e5b75505Sopenharmony_ci char *port, *name; 318e5b75505Sopenharmony_ci int port_id; 319e5b75505Sopenharmony_ci name = os_strdup(ctrl_path); 320e5b75505Sopenharmony_ci if (name == NULL) { 321e5b75505Sopenharmony_ci return -1; 322e5b75505Sopenharmony_ci } 323e5b75505Sopenharmony_ci 324e5b75505Sopenharmony_ci port = os_strchr(name, ':'); 325e5b75505Sopenharmony_ci if (port) { 326e5b75505Sopenharmony_ci port_id = atoi(&port[1]); 327e5b75505Sopenharmony_ci port[0] = '\0'; 328e5b75505Sopenharmony_ci ctrl->dest.sin_port = htons(port_id); 329e5b75505Sopenharmony_ci } 330e5b75505Sopenharmony_ci os_free(name); 331e5b75505Sopenharmony_ci return 0; 332e5b75505Sopenharmony_ci} 333e5b75505Sopenharmony_ci#endif /* CONFIG_OPEN_HARMONY_PATCH */ 334e5b75505Sopenharmony_ci 335e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP 336e5b75505Sopenharmony_ci 337e5b75505Sopenharmony_cistruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 338e5b75505Sopenharmony_ci{ 339e5b75505Sopenharmony_ci struct wpa_ctrl *ctrl; 340e5b75505Sopenharmony_ci char buf[128]; 341e5b75505Sopenharmony_ci size_t len; 342e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 343e5b75505Sopenharmony_ci struct hostent *h; 344e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 345e5b75505Sopenharmony_ci 346e5b75505Sopenharmony_ci ctrl = os_zalloc(sizeof(*ctrl)); 347e5b75505Sopenharmony_ci if (ctrl == NULL) 348e5b75505Sopenharmony_ci return NULL; 349e5b75505Sopenharmony_ci 350e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 351e5b75505Sopenharmony_ci ctrl->s = socket(PF_INET6, SOCK_DGRAM, 0); 352e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 353e5b75505Sopenharmony_ci ctrl->s = socket(PF_INET, SOCK_DGRAM, 0); 354e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 355e5b75505Sopenharmony_ci if (ctrl->s < 0) { 356e5b75505Sopenharmony_ci perror("socket"); 357e5b75505Sopenharmony_ci os_free(ctrl); 358e5b75505Sopenharmony_ci return NULL; 359e5b75505Sopenharmony_ci } 360e5b75505Sopenharmony_ci 361e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 362e5b75505Sopenharmony_ci ctrl->local.sin6_family = AF_INET6; 363e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 364e5b75505Sopenharmony_ci ctrl->local.sin6_addr = in6addr_any; 365e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 366e5b75505Sopenharmony_ci inet_pton(AF_INET6, "::1", &ctrl->local.sin6_addr); 367e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 368e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 369e5b75505Sopenharmony_ci ctrl->local.sin_family = AF_INET; 370e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 371e5b75505Sopenharmony_ci ctrl->local.sin_addr.s_addr = INADDR_ANY; 372e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 373e5b75505Sopenharmony_ci ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1); 374e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 375e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 376e5b75505Sopenharmony_ci 377e5b75505Sopenharmony_ci if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, 378e5b75505Sopenharmony_ci sizeof(ctrl->local)) < 0) { 379e5b75505Sopenharmony_ci close(ctrl->s); 380e5b75505Sopenharmony_ci os_free(ctrl); 381e5b75505Sopenharmony_ci return NULL; 382e5b75505Sopenharmony_ci } 383e5b75505Sopenharmony_ci 384e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 385e5b75505Sopenharmony_ci ctrl->dest.sin6_family = AF_INET6; 386e5b75505Sopenharmony_ci inet_pton(AF_INET6, "::1", &ctrl->dest.sin6_addr); 387e5b75505Sopenharmony_ci ctrl->dest.sin6_port = htons(WPA_CTRL_IFACE_PORT); 388e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 389e5b75505Sopenharmony_ci ctrl->dest.sin_family = AF_INET; 390e5b75505Sopenharmony_ci ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1); 391e5b75505Sopenharmony_ci ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT); 392e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 393e5b75505Sopenharmony_ci 394e5b75505Sopenharmony_ci#if defined(CONFIG_OPEN_HARMONY_PATCH) || defined(CONFIG_OPEN_HARMONY_PATCH_LITE) 395e5b75505Sopenharmony_ci if (wpa_ctrl_port(ctrl_path, ctrl) < 0) { 396e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "get port fail"); 397e5b75505Sopenharmony_ci } 398e5b75505Sopenharmony_ci#endif /* CONFIG_OPEN_HARMONY_PATCH | CONFIG_OPEN_HARMONY_PATCH_LITE */ 399e5b75505Sopenharmony_ci 400e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 401e5b75505Sopenharmony_ci if (ctrl_path) { 402e5b75505Sopenharmony_ci char *port, *name; 403e5b75505Sopenharmony_ci int port_id; 404e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 405e5b75505Sopenharmony_ci char *scope; 406e5b75505Sopenharmony_ci int scope_id = 0; 407e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 408e5b75505Sopenharmony_ci 409e5b75505Sopenharmony_ci name = os_strdup(ctrl_path); 410e5b75505Sopenharmony_ci if (name == NULL) { 411e5b75505Sopenharmony_ci close(ctrl->s); 412e5b75505Sopenharmony_ci os_free(ctrl); 413e5b75505Sopenharmony_ci return NULL; 414e5b75505Sopenharmony_ci } 415e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 416e5b75505Sopenharmony_ci port = os_strchr(name, ','); 417e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 418e5b75505Sopenharmony_ci port = os_strchr(name, ':'); 419e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 420e5b75505Sopenharmony_ci 421e5b75505Sopenharmony_ci if (port) { 422e5b75505Sopenharmony_ci port_id = atoi(&port[1]); 423e5b75505Sopenharmony_ci port[0] = '\0'; 424e5b75505Sopenharmony_ci } else 425e5b75505Sopenharmony_ci port_id = WPA_CTRL_IFACE_PORT; 426e5b75505Sopenharmony_ci 427e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 428e5b75505Sopenharmony_ci scope = os_strchr(name, '%'); 429e5b75505Sopenharmony_ci if (scope) { 430e5b75505Sopenharmony_ci scope_id = if_nametoindex(&scope[1]); 431e5b75505Sopenharmony_ci scope[0] = '\0'; 432e5b75505Sopenharmony_ci } 433e5b75505Sopenharmony_ci h = gethostbyname2(name, AF_INET6); 434e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 435e5b75505Sopenharmony_ci h = gethostbyname(name); 436e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 437e5b75505Sopenharmony_ci ctrl->remote_ip = os_strdup(name); 438e5b75505Sopenharmony_ci os_free(name); 439e5b75505Sopenharmony_ci if (h == NULL) { 440e5b75505Sopenharmony_ci perror("gethostbyname"); 441e5b75505Sopenharmony_ci close(ctrl->s); 442e5b75505Sopenharmony_ci os_free(ctrl->remote_ip); 443e5b75505Sopenharmony_ci os_free(ctrl); 444e5b75505Sopenharmony_ci return NULL; 445e5b75505Sopenharmony_ci } 446e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 447e5b75505Sopenharmony_ci ctrl->dest.sin6_scope_id = scope_id; 448e5b75505Sopenharmony_ci ctrl->dest.sin6_port = htons(port_id); 449e5b75505Sopenharmony_ci os_memcpy(&ctrl->dest.sin6_addr, h->h_addr, h->h_length); 450e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 451e5b75505Sopenharmony_ci ctrl->dest.sin_port = htons(port_id); 452e5b75505Sopenharmony_ci os_memcpy(&ctrl->dest.sin_addr.s_addr, h->h_addr, h->h_length); 453e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 454e5b75505Sopenharmony_ci } else 455e5b75505Sopenharmony_ci ctrl->remote_ip = os_strdup("localhost"); 456e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 457e5b75505Sopenharmony_ci 458e5b75505Sopenharmony_ci if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, 459e5b75505Sopenharmony_ci sizeof(ctrl->dest)) < 0) { 460e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 461e5b75505Sopenharmony_ci char addr[INET6_ADDRSTRLEN]; 462e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s", 463e5b75505Sopenharmony_ci inet_ntop(AF_INET6, &ctrl->dest.sin6_addr, addr, 464e5b75505Sopenharmony_ci sizeof(ctrl->dest)), 465e5b75505Sopenharmony_ci ntohs(ctrl->dest.sin6_port), 466e5b75505Sopenharmony_ci strerror(errno)); 467e5b75505Sopenharmony_ci#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 468e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s", 469e5b75505Sopenharmony_ci inet_ntoa(ctrl->dest.sin_addr), 470e5b75505Sopenharmony_ci ntohs(ctrl->dest.sin_port), 471e5b75505Sopenharmony_ci strerror(errno)); 472e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 473e5b75505Sopenharmony_ci close(ctrl->s); 474e5b75505Sopenharmony_ci os_free(ctrl->remote_ip); 475e5b75505Sopenharmony_ci os_free(ctrl); 476e5b75505Sopenharmony_ci return NULL; 477e5b75505Sopenharmony_ci } 478e5b75505Sopenharmony_ci 479e5b75505Sopenharmony_ci len = sizeof(buf) - 1; 480e5b75505Sopenharmony_ci if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) { 481e5b75505Sopenharmony_ci buf[len] = '\0'; 482e5b75505Sopenharmony_ci ctrl->cookie = os_strdup(buf); 483e5b75505Sopenharmony_ci } 484e5b75505Sopenharmony_ci 485e5b75505Sopenharmony_ci if (wpa_ctrl_request(ctrl, "IFNAME", 6, buf, &len, NULL) == 0) { 486e5b75505Sopenharmony_ci buf[len] = '\0'; 487e5b75505Sopenharmony_ci ctrl->remote_ifname = os_strdup(buf); 488e5b75505Sopenharmony_ci } 489e5b75505Sopenharmony_ci 490e5b75505Sopenharmony_ci return ctrl; 491e5b75505Sopenharmony_ci} 492e5b75505Sopenharmony_ci 493e5b75505Sopenharmony_ci 494e5b75505Sopenharmony_cichar * wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl) 495e5b75505Sopenharmony_ci{ 496e5b75505Sopenharmony_ci#define WPA_CTRL_MAX_PS_NAME 100 497e5b75505Sopenharmony_ci static char ps[WPA_CTRL_MAX_PS_NAME] = {}; 498e5b75505Sopenharmony_ci os_snprintf(ps, WPA_CTRL_MAX_PS_NAME, "%s/%s", 499e5b75505Sopenharmony_ci ctrl->remote_ip, ctrl->remote_ifname); 500e5b75505Sopenharmony_ci return ps; 501e5b75505Sopenharmony_ci} 502e5b75505Sopenharmony_ci 503e5b75505Sopenharmony_ci 504e5b75505Sopenharmony_civoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 505e5b75505Sopenharmony_ci{ 506e5b75505Sopenharmony_ci close(ctrl->s); 507e5b75505Sopenharmony_ci os_free(ctrl->cookie); 508e5b75505Sopenharmony_ci os_free(ctrl->remote_ifname); 509e5b75505Sopenharmony_ci os_free(ctrl->remote_ip); 510e5b75505Sopenharmony_ci os_free(ctrl); 511e5b75505Sopenharmony_ci} 512e5b75505Sopenharmony_ci 513e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP */ 514e5b75505Sopenharmony_ci 515e5b75505Sopenharmony_ci 516e5b75505Sopenharmony_ci#ifdef CTRL_IFACE_SOCKET 517e5b75505Sopenharmony_ciint wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, 518e5b75505Sopenharmony_ci char *reply, size_t *reply_len, 519e5b75505Sopenharmony_ci void (*msg_cb)(char *msg, size_t len)) 520e5b75505Sopenharmony_ci{ 521e5b75505Sopenharmony_ci struct timeval tv; 522e5b75505Sopenharmony_ci struct os_reltime started_at; 523e5b75505Sopenharmony_ci int res; 524e5b75505Sopenharmony_ci fd_set rfds; 525e5b75505Sopenharmony_ci const char *_cmd; 526e5b75505Sopenharmony_ci char *cmd_buf = NULL; 527e5b75505Sopenharmony_ci size_t _cmd_len; 528e5b75505Sopenharmony_ci#ifdef CONFIG_OPEN_HARMONY_PATCH 529e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, "wpa_ctrl_request cmd: %s", cmd); 530e5b75505Sopenharmony_ci#endif // CONFIG_OPEN_HARMONY_PATCH 531e5b75505Sopenharmony_ci 532e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_UDP 533e5b75505Sopenharmony_ci if (ctrl->cookie) { 534e5b75505Sopenharmony_ci char *pos; 535e5b75505Sopenharmony_ci _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len; 536e5b75505Sopenharmony_ci cmd_buf = os_malloc(_cmd_len); 537e5b75505Sopenharmony_ci if (cmd_buf == NULL) 538e5b75505Sopenharmony_ci return -1; 539e5b75505Sopenharmony_ci _cmd = cmd_buf; 540e5b75505Sopenharmony_ci pos = cmd_buf; 541e5b75505Sopenharmony_ci os_strlcpy(pos, ctrl->cookie, _cmd_len); 542e5b75505Sopenharmony_ci pos += os_strlen(ctrl->cookie); 543e5b75505Sopenharmony_ci *pos++ = ' '; 544e5b75505Sopenharmony_ci os_memcpy(pos, cmd, cmd_len); 545e5b75505Sopenharmony_ci } else 546e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_UDP */ 547e5b75505Sopenharmony_ci { 548e5b75505Sopenharmony_ci _cmd = cmd; 549e5b75505Sopenharmony_ci _cmd_len = cmd_len; 550e5b75505Sopenharmony_ci } 551e5b75505Sopenharmony_ci 552e5b75505Sopenharmony_ci errno = 0; 553e5b75505Sopenharmony_ci started_at.sec = 0; 554e5b75505Sopenharmony_ci started_at.usec = 0; 555e5b75505Sopenharmony_ciretry_send: 556e5b75505Sopenharmony_ci if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) { 557e5b75505Sopenharmony_ci if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK) 558e5b75505Sopenharmony_ci { 559e5b75505Sopenharmony_ci /* 560e5b75505Sopenharmony_ci * Must be a non-blocking socket... Try for a bit 561e5b75505Sopenharmony_ci * longer before giving up. 562e5b75505Sopenharmony_ci */ 563e5b75505Sopenharmony_ci if (started_at.sec == 0) 564e5b75505Sopenharmony_ci os_get_reltime(&started_at); 565e5b75505Sopenharmony_ci else { 566e5b75505Sopenharmony_ci struct os_reltime n; 567e5b75505Sopenharmony_ci os_get_reltime(&n); 568e5b75505Sopenharmony_ci /* Try for a few seconds. */ 569e5b75505Sopenharmony_ci if (os_reltime_expired(&n, &started_at, 5)) 570e5b75505Sopenharmony_ci goto send_err; 571e5b75505Sopenharmony_ci } 572e5b75505Sopenharmony_ci os_sleep(1, 0); 573e5b75505Sopenharmony_ci goto retry_send; 574e5b75505Sopenharmony_ci } 575e5b75505Sopenharmony_ci send_err: 576e5b75505Sopenharmony_ci os_free(cmd_buf); 577e5b75505Sopenharmony_ci return -1; 578e5b75505Sopenharmony_ci } 579e5b75505Sopenharmony_ci os_free(cmd_buf); 580e5b75505Sopenharmony_ci 581e5b75505Sopenharmony_ci for (;;) { 582e5b75505Sopenharmony_ci tv.tv_sec = 10; 583e5b75505Sopenharmony_ci tv.tv_usec = 0; 584e5b75505Sopenharmony_ci FD_ZERO(&rfds); 585e5b75505Sopenharmony_ci FD_SET(ctrl->s, &rfds); 586e5b75505Sopenharmony_ci res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); 587e5b75505Sopenharmony_ci if (res < 0 && errno == EINTR) 588e5b75505Sopenharmony_ci continue; 589e5b75505Sopenharmony_ci if (res < 0) 590e5b75505Sopenharmony_ci return res; 591e5b75505Sopenharmony_ci if (FD_ISSET(ctrl->s, &rfds)) { 592e5b75505Sopenharmony_ci res = recv(ctrl->s, reply, *reply_len, 0); 593e5b75505Sopenharmony_ci if (res < 0) 594e5b75505Sopenharmony_ci return res; 595e5b75505Sopenharmony_ci if ((res > 0 && reply[0] == '<') || 596e5b75505Sopenharmony_ci (res > 6 && strncmp(reply, "IFNAME=", 7) == 0)) { 597e5b75505Sopenharmony_ci /* This is an unsolicited message from 598e5b75505Sopenharmony_ci * wpa_supplicant, not the reply to the 599e5b75505Sopenharmony_ci * request. Use msg_cb to report this to the 600e5b75505Sopenharmony_ci * caller. */ 601e5b75505Sopenharmony_ci if (msg_cb) { 602e5b75505Sopenharmony_ci /* Make sure the message is nul 603e5b75505Sopenharmony_ci * terminated. */ 604e5b75505Sopenharmony_ci if ((size_t) res == *reply_len) 605e5b75505Sopenharmony_ci res = (*reply_len) - 1; 606e5b75505Sopenharmony_ci reply[res] = '\0'; 607e5b75505Sopenharmony_ci msg_cb(reply, res); 608e5b75505Sopenharmony_ci } 609e5b75505Sopenharmony_ci continue; 610e5b75505Sopenharmony_ci } 611e5b75505Sopenharmony_ci *reply_len = res; 612e5b75505Sopenharmony_ci break; 613e5b75505Sopenharmony_ci } else { 614e5b75505Sopenharmony_ci return -2; 615e5b75505Sopenharmony_ci } 616e5b75505Sopenharmony_ci } 617e5b75505Sopenharmony_ci return 0; 618e5b75505Sopenharmony_ci} 619e5b75505Sopenharmony_ci#endif /* CTRL_IFACE_SOCKET */ 620e5b75505Sopenharmony_ci 621e5b75505Sopenharmony_ci 622e5b75505Sopenharmony_cistatic int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach) 623e5b75505Sopenharmony_ci{ 624e5b75505Sopenharmony_ci char buf[10]; 625e5b75505Sopenharmony_ci int ret; 626e5b75505Sopenharmony_ci size_t len = 10; 627e5b75505Sopenharmony_ci 628e5b75505Sopenharmony_ci ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, 629e5b75505Sopenharmony_ci buf, &len, NULL); 630e5b75505Sopenharmony_ci if (ret < 0) 631e5b75505Sopenharmony_ci return ret; 632e5b75505Sopenharmony_ci if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0) 633e5b75505Sopenharmony_ci return 0; 634e5b75505Sopenharmony_ci return -1; 635e5b75505Sopenharmony_ci} 636e5b75505Sopenharmony_ci 637e5b75505Sopenharmony_ci 638e5b75505Sopenharmony_ciint wpa_ctrl_attach(struct wpa_ctrl *ctrl) 639e5b75505Sopenharmony_ci{ 640e5b75505Sopenharmony_ci return wpa_ctrl_attach_helper(ctrl, 1); 641e5b75505Sopenharmony_ci} 642e5b75505Sopenharmony_ci 643e5b75505Sopenharmony_ci 644e5b75505Sopenharmony_ciint wpa_ctrl_detach(struct wpa_ctrl *ctrl) 645e5b75505Sopenharmony_ci{ 646e5b75505Sopenharmony_ci return wpa_ctrl_attach_helper(ctrl, 0); 647e5b75505Sopenharmony_ci} 648e5b75505Sopenharmony_ci 649e5b75505Sopenharmony_ci 650e5b75505Sopenharmony_ci#ifdef CTRL_IFACE_SOCKET 651e5b75505Sopenharmony_ci 652e5b75505Sopenharmony_ciint wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) 653e5b75505Sopenharmony_ci{ 654e5b75505Sopenharmony_ci int res; 655e5b75505Sopenharmony_ci 656e5b75505Sopenharmony_ci res = recv(ctrl->s, reply, *reply_len, 0); 657e5b75505Sopenharmony_ci if (res < 0) 658e5b75505Sopenharmony_ci return res; 659e5b75505Sopenharmony_ci *reply_len = res; 660e5b75505Sopenharmony_ci return 0; 661e5b75505Sopenharmony_ci} 662e5b75505Sopenharmony_ci 663e5b75505Sopenharmony_ci 664e5b75505Sopenharmony_ciint wpa_ctrl_pending(struct wpa_ctrl *ctrl) 665e5b75505Sopenharmony_ci{ 666e5b75505Sopenharmony_ci struct timeval tv; 667e5b75505Sopenharmony_ci fd_set rfds; 668e5b75505Sopenharmony_ci tv.tv_sec = 0; 669e5b75505Sopenharmony_ci tv.tv_usec = 0; 670e5b75505Sopenharmony_ci FD_ZERO(&rfds); 671e5b75505Sopenharmony_ci FD_SET(ctrl->s, &rfds); 672e5b75505Sopenharmony_ci select(ctrl->s + 1, &rfds, NULL, NULL, &tv); 673e5b75505Sopenharmony_ci return FD_ISSET(ctrl->s, &rfds); 674e5b75505Sopenharmony_ci} 675e5b75505Sopenharmony_ci 676e5b75505Sopenharmony_ci 677e5b75505Sopenharmony_ciint wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) 678e5b75505Sopenharmony_ci{ 679e5b75505Sopenharmony_ci return ctrl->s; 680e5b75505Sopenharmony_ci} 681e5b75505Sopenharmony_ci 682e5b75505Sopenharmony_ci#endif /* CTRL_IFACE_SOCKET */ 683e5b75505Sopenharmony_ci 684e5b75505Sopenharmony_ci 685e5b75505Sopenharmony_ci#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE 686e5b75505Sopenharmony_ci 687e5b75505Sopenharmony_ci#ifndef WPA_SUPPLICANT_NAMED_PIPE 688e5b75505Sopenharmony_ci#define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant" 689e5b75505Sopenharmony_ci#endif 690e5b75505Sopenharmony_ci#define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE) 691e5b75505Sopenharmony_ci 692e5b75505Sopenharmony_cistruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 693e5b75505Sopenharmony_ci{ 694e5b75505Sopenharmony_ci struct wpa_ctrl *ctrl; 695e5b75505Sopenharmony_ci DWORD mode; 696e5b75505Sopenharmony_ci TCHAR name[256]; 697e5b75505Sopenharmony_ci int i, ret; 698e5b75505Sopenharmony_ci 699e5b75505Sopenharmony_ci ctrl = os_malloc(sizeof(*ctrl)); 700e5b75505Sopenharmony_ci if (ctrl == NULL) 701e5b75505Sopenharmony_ci return NULL; 702e5b75505Sopenharmony_ci os_memset(ctrl, 0, sizeof(*ctrl)); 703e5b75505Sopenharmony_ci 704e5b75505Sopenharmony_ci#ifdef UNICODE 705e5b75505Sopenharmony_ci if (ctrl_path == NULL) 706e5b75505Sopenharmony_ci ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX); 707e5b75505Sopenharmony_ci else 708e5b75505Sopenharmony_ci ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"), 709e5b75505Sopenharmony_ci ctrl_path); 710e5b75505Sopenharmony_ci#else /* UNICODE */ 711e5b75505Sopenharmony_ci if (ctrl_path == NULL) 712e5b75505Sopenharmony_ci ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX); 713e5b75505Sopenharmony_ci else 714e5b75505Sopenharmony_ci ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s", 715e5b75505Sopenharmony_ci ctrl_path); 716e5b75505Sopenharmony_ci#endif /* UNICODE */ 717e5b75505Sopenharmony_ci if (os_snprintf_error(256, ret)) { 718e5b75505Sopenharmony_ci os_free(ctrl); 719e5b75505Sopenharmony_ci return NULL; 720e5b75505Sopenharmony_ci } 721e5b75505Sopenharmony_ci 722e5b75505Sopenharmony_ci for (i = 0; i < 10; i++) { 723e5b75505Sopenharmony_ci ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, 724e5b75505Sopenharmony_ci NULL, OPEN_EXISTING, 0, NULL); 725e5b75505Sopenharmony_ci /* 726e5b75505Sopenharmony_ci * Current named pipe server side in wpa_supplicant is 727e5b75505Sopenharmony_ci * re-opening the pipe for new clients only after the previous 728e5b75505Sopenharmony_ci * one is taken into use. This leaves a small window for race 729e5b75505Sopenharmony_ci * conditions when two connections are being opened at almost 730e5b75505Sopenharmony_ci * the same time. Retry if that was the case. 731e5b75505Sopenharmony_ci */ 732e5b75505Sopenharmony_ci if (ctrl->pipe != INVALID_HANDLE_VALUE || 733e5b75505Sopenharmony_ci GetLastError() != ERROR_PIPE_BUSY) 734e5b75505Sopenharmony_ci break; 735e5b75505Sopenharmony_ci WaitNamedPipe(name, 1000); 736e5b75505Sopenharmony_ci } 737e5b75505Sopenharmony_ci if (ctrl->pipe == INVALID_HANDLE_VALUE) { 738e5b75505Sopenharmony_ci os_free(ctrl); 739e5b75505Sopenharmony_ci return NULL; 740e5b75505Sopenharmony_ci } 741e5b75505Sopenharmony_ci 742e5b75505Sopenharmony_ci mode = PIPE_READMODE_MESSAGE; 743e5b75505Sopenharmony_ci if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) { 744e5b75505Sopenharmony_ci CloseHandle(ctrl->pipe); 745e5b75505Sopenharmony_ci os_free(ctrl); 746e5b75505Sopenharmony_ci return NULL; 747e5b75505Sopenharmony_ci } 748e5b75505Sopenharmony_ci 749e5b75505Sopenharmony_ci return ctrl; 750e5b75505Sopenharmony_ci} 751e5b75505Sopenharmony_ci 752e5b75505Sopenharmony_ci 753e5b75505Sopenharmony_civoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 754e5b75505Sopenharmony_ci{ 755e5b75505Sopenharmony_ci CloseHandle(ctrl->pipe); 756e5b75505Sopenharmony_ci os_free(ctrl); 757e5b75505Sopenharmony_ci} 758e5b75505Sopenharmony_ci 759e5b75505Sopenharmony_ci 760e5b75505Sopenharmony_ciint wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, 761e5b75505Sopenharmony_ci char *reply, size_t *reply_len, 762e5b75505Sopenharmony_ci void (*msg_cb)(char *msg, size_t len)) 763e5b75505Sopenharmony_ci{ 764e5b75505Sopenharmony_ci DWORD written; 765e5b75505Sopenharmony_ci DWORD readlen = *reply_len; 766e5b75505Sopenharmony_ci 767e5b75505Sopenharmony_ci if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL)) 768e5b75505Sopenharmony_ci return -1; 769e5b75505Sopenharmony_ci 770e5b75505Sopenharmony_ci if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL)) 771e5b75505Sopenharmony_ci return -1; 772e5b75505Sopenharmony_ci *reply_len = readlen; 773e5b75505Sopenharmony_ci 774e5b75505Sopenharmony_ci return 0; 775e5b75505Sopenharmony_ci} 776e5b75505Sopenharmony_ci 777e5b75505Sopenharmony_ci 778e5b75505Sopenharmony_ciint wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) 779e5b75505Sopenharmony_ci{ 780e5b75505Sopenharmony_ci DWORD len = *reply_len; 781e5b75505Sopenharmony_ci if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL)) 782e5b75505Sopenharmony_ci return -1; 783e5b75505Sopenharmony_ci *reply_len = len; 784e5b75505Sopenharmony_ci return 0; 785e5b75505Sopenharmony_ci} 786e5b75505Sopenharmony_ci 787e5b75505Sopenharmony_ci 788e5b75505Sopenharmony_ciint wpa_ctrl_pending(struct wpa_ctrl *ctrl) 789e5b75505Sopenharmony_ci{ 790e5b75505Sopenharmony_ci DWORD left; 791e5b75505Sopenharmony_ci 792e5b75505Sopenharmony_ci if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL)) 793e5b75505Sopenharmony_ci return -1; 794e5b75505Sopenharmony_ci return left ? 1 : 0; 795e5b75505Sopenharmony_ci} 796e5b75505Sopenharmony_ci 797e5b75505Sopenharmony_ci 798e5b75505Sopenharmony_ciint wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) 799e5b75505Sopenharmony_ci{ 800e5b75505Sopenharmony_ci return -1; 801e5b75505Sopenharmony_ci} 802e5b75505Sopenharmony_ci 803e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ 804e5b75505Sopenharmony_ci 805e5b75505Sopenharmony_ci#endif /* CONFIG_CTRL_IFACE */ 806