| 1 |
|
| /* |
| 2 |
|
| * wpa_supplicant/hostapd control interface library |
| 3 |
|
| * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi> |
| 4 |
|
| * |
| 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. |
| 8 |
|
| * |
| 9 |
|
| * Alternatively, this software may be distributed under the terms of BSD |
| 10 |
|
| * license. |
| 11 |
|
| * |
| 12 |
|
| * See README and COPYING for more details. |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #include <stdlib.h> |
| 16 |
|
| #include <stdio.h> |
| 17 |
|
| #include <string.h> |
| 18 |
|
| #include <unistd.h> |
| 19 |
|
| #include <sys/types.h> |
| 20 |
|
| #include <sys/time.h> |
| 21 |
|
| #ifndef CONFIG_NATIVE_WINDOWS |
| 22 |
|
| #include <sys/socket.h> |
| 23 |
|
| #include <netinet/in.h> |
| 24 |
|
| #include <sys/un.h> |
| 25 |
|
| #endif /* CONFIG_NATIVE_WINDOWS */ |
| 26 |
|
| |
| 27 |
|
| #include "wpa_ctrl.h" |
| 28 |
|
| #ifdef CONFIG_NATIVE_WINDOWS |
| 29 |
|
| #include "common.h" |
| 30 |
|
| #endif /* CONFIG_NATIVE_WINDOWS */ |
| 31 |
|
| |
| 32 |
|
| |
| 33 |
|
| /** |
| 34 |
|
| * struct wpa_ctrl - Internal structure for control interface library |
| 35 |
|
| * |
| 36 |
|
| * This structure is used by the wpa_supplicant/hostapd control interface |
| 37 |
|
| * library to store internal data. Programs using the library should not touch |
| 38 |
|
| * this data directly. They can only use the pointer to the data structure as |
| 39 |
|
| * an identifier for the control interface connection and use this as one of |
| 40 |
|
| * the arguments for most of the control interface library functions. |
| 41 |
|
| */ |
| 42 |
|
| struct wpa_ctrl { |
| 43 |
|
| int s; |
| 44 |
|
| #ifdef CONFIG_CTRL_IFACE_UDP |
| 45 |
|
| struct sockaddr_in local; |
| 46 |
|
| struct sockaddr_in dest; |
| 47 |
|
| #else /* CONFIG_CTRL_IFACE_UDP */ |
| 48 |
|
| struct sockaddr_un local; |
| 49 |
|
| struct sockaddr_un dest; |
| 50 |
|
| #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 51 |
|
| }; |
| 52 |
|
| |
| 53 |
|
| |
| 54 |
0 |
| struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) |
| 55 |
|
| { |
| 56 |
|
| struct wpa_ctrl *ctrl; |
| 57 |
|
| #ifndef CONFIG_CTRL_IFACE_UDP |
| 58 |
|
| static int counter = 0; |
| 59 |
|
| #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 60 |
|
| |
| 61 |
0 |
| ctrl = malloc(sizeof(*ctrl)); |
| 62 |
0 |
▶ | if (ctrl == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctrl == NULL) |
0 |
0 |
0 |
|
| 63 |
0 |
| return NULL; |
| 64 |
0 |
| memset(ctrl, 0, sizeof(*ctrl)); |
| 65 |
|
| |
| 66 |
|
| #ifdef CONFIG_CTRL_IFACE_UDP |
| 67 |
|
| ctrl->s = socket(PF_INET, SOCK_DGRAM, 0); |
| 68 |
|
| if (ctrl->s < 0) { |
| 69 |
|
| perror("socket"); |
| 70 |
|
| free(ctrl); |
| 71 |
|
| return NULL; |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| ctrl->local.sin_family = AF_INET; |
| 75 |
|
| ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1); |
| 76 |
|
| if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, |
| 77 |
|
| sizeof(ctrl->local)) < 0) { |
| 78 |
|
| close(ctrl->s); |
| 79 |
|
| free(ctrl); |
| 80 |
|
| return NULL; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| ctrl->dest.sin_family = AF_INET; |
| 84 |
|
| ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1); |
| 85 |
|
| ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT); |
| 86 |
|
| if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, |
| 87 |
|
| sizeof(ctrl->dest)) < 0) { |
| 88 |
|
| perror("connect"); |
| 89 |
|
| close(ctrl->s); |
| 90 |
|
| free(ctrl); |
| 91 |
|
| return NULL; |
| 92 |
|
| } |
| 93 |
|
| #else /* CONFIG_CTRL_IFACE_UDP */ |
| 94 |
0 |
| ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 95 |
0 |
▶ | if (ctrl->s < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctrl->s < 0) { |
0 |
0 |
0 |
|
| 96 |
0 |
| free(ctrl); |
| 97 |
0 |
| return NULL; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| ctrl->local.sun_family = AF_UNIX; |
| 101 |
0 |
| snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path), |
| 102 |
|
| "/tmp/wpa_ctrl_%d-%d", getpid(), counter++); |
| 103 |
0 |
▶ | if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, |
Branch: 1 branch
| Branch | True | False | Total |
| if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, |
0 |
0 |
0 |
|
| 104 |
|
| sizeof(ctrl->local)) < 0) { |
| 105 |
0 |
| close(ctrl->s); |
| 106 |
0 |
| free(ctrl); |
| 107 |
0 |
| return NULL; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
0 |
| ctrl->dest.sun_family = AF_UNIX; |
| 111 |
0 |
| snprintf(ctrl->dest.sun_path, sizeof(ctrl->dest.sun_path), "%s", |
| 112 |
|
| ctrl_path); |
| 113 |
0 |
▶ | if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, |
Branch: 1 branch
| Branch | True | False | Total |
| if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, |
0 |
0 |
0 |
|
| 114 |
|
| sizeof(ctrl->dest)) < 0) { |
| 115 |
0 |
| close(ctrl->s); |
| 116 |
0 |
| unlink(ctrl->local.sun_path); |
| 117 |
0 |
| free(ctrl); |
| 118 |
0 |
| return NULL; |
| 119 |
|
| } |
| 120 |
|
| #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 121 |
|
| |
| 122 |
0 |
| return ctrl; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
|
| |
| 126 |
0 |
| void wpa_ctrl_close(struct wpa_ctrl *ctrl) |
| 127 |
|
| { |
| 128 |
|
| #ifndef CONFIG_CTRL_IFACE_UDP |
| 129 |
0 |
| unlink(ctrl->local.sun_path); |
| 130 |
|
| #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 131 |
0 |
| close(ctrl->s); |
| 132 |
0 |
| free(ctrl); |
| 133 |
0 |
| } |
| 134 |
|
| |
| 135 |
|
| |
| 136 |
0 |
| int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, |
| 137 |
|
| char *reply, size_t *reply_len, |
| 138 |
|
| void (*msg_cb)(char *msg, size_t len)) |
| 139 |
|
| { |
| 140 |
|
| struct timeval tv; |
| 141 |
|
| int res; |
| 142 |
|
| fd_set rfds; |
| 143 |
|
| |
| 144 |
0 |
▶ | if (send(ctrl->s, cmd, cmd_len, 0) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (send(ctrl->s, cmd, cmd_len, 0) < 0) |
0 |
0 |
0 |
|
| 145 |
0 |
| return -1; |
| 146 |
|
| |
| 147 |
|
| for (;;) { |
| 148 |
0 |
| tv.tv_sec = 2; |
| 149 |
0 |
| tv.tv_usec = 0; |
| 150 |
0 |
▶ | FD_ZERO(&rfds); |
Branch: 1 branch
| Branch | True | False | Total |
| FD_ZERO(&rfds); |
0 |
0 |
0 |
|
| 151 |
0 |
| FD_SET(ctrl->s, &rfds); |
| 152 |
0 |
| res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); |
| 153 |
0 |
▶ | if (FD_ISSET(ctrl->s, &rfds)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (FD_ISSET(ctrl->s, &rfds)) { |
0 |
0 |
0 |
|
| 154 |
0 |
| res = recv(ctrl->s, reply, *reply_len, 0); |
| 155 |
0 |
▶ | if (res < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) |
0 |
0 |
0 |
|
| 156 |
0 |
| return res; |
| 157 |
0 |
▶ | if (res > 0 && reply[0] == '<') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (res > 0 && reply[0] == '<') { |
0 |
0 |
0 |
| if (res > 0 && reply[0] == '<') { |
0 |
0 |
0 |
|
| 158 |
|
| /* This is an unsolicited message from |
| 159 |
|
| * wpa_supplicant, not the reply to the |
| 160 |
|
| * request. Use msg_cb to report this to the |
| 161 |
|
| * caller. */ |
| 162 |
0 |
▶ | if (msg_cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_cb) { |
0 |
0 |
0 |
|
| 163 |
|
| /* Make sure the message is nul |
| 164 |
|
| * terminated. */ |
| 165 |
0 |
▶ | if ((size_t) res == *reply_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t) res == *reply_len) |
0 |
0 |
0 |
|
| 166 |
0 |
| res = (*reply_len) - 1; |
| 167 |
0 |
| reply[res] = '\0'; |
| 168 |
0 |
| msg_cb(reply, res); |
| 169 |
|
| } |
| 170 |
0 |
| continue; |
| 171 |
|
| } |
| 172 |
0 |
| *reply_len = res; |
| 173 |
0 |
| break; |
| 174 |
|
| } else { |
| 175 |
0 |
| return -2; |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
0 |
| return 0; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| |
| 182 |
0 |
| static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach) |
| 183 |
|
| { |
| 184 |
|
| char buf[10]; |
| 185 |
|
| int ret; |
| 186 |
0 |
| size_t len = 10; |
| 187 |
|
| |
| 188 |
0 |
▶ | ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, |
Branch: 1 branch
| Branch | True | False | Total |
| ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, |
0 |
0 |
0 |
|
| 189 |
|
| buf, &len, NULL); |
| 190 |
0 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
0 |
0 |
0 |
|
| 191 |
0 |
| return ret; |
| 192 |
0 |
▶ | if (len == 3 && memcmp(buf, "OK\n", 3) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 3 && memcmp(buf, "OK\n", 3) == 0) |
0 |
0 |
0 |
| if (len == 3 && memcmp(buf, "OK\n", 3) == 0) |
0 |
0 |
0 |
|
| 193 |
0 |
| return 0; |
| 194 |
0 |
| return -1; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| |
| 198 |
0 |
| int wpa_ctrl_attach(struct wpa_ctrl *ctrl) |
| 199 |
|
| { |
| 200 |
0 |
| return wpa_ctrl_attach_helper(ctrl, 1); |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| |
| 204 |
0 |
| int wpa_ctrl_detach(struct wpa_ctrl *ctrl) |
| 205 |
|
| { |
| 206 |
0 |
| return wpa_ctrl_attach_helper(ctrl, 0); |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| |
| 210 |
0 |
| int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) |
| 211 |
|
| { |
| 212 |
|
| int res; |
| 213 |
|
| |
| 214 |
0 |
| res = recv(ctrl->s, reply, *reply_len, 0); |
| 215 |
0 |
▶ | if (res < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) |
0 |
0 |
0 |
|
| 216 |
0 |
| return res; |
| 217 |
0 |
| *reply_len = res; |
| 218 |
0 |
| return 0; |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
|
| |
| 222 |
0 |
| int wpa_ctrl_pending(struct wpa_ctrl *ctrl) |
| 223 |
|
| { |
| 224 |
|
| struct timeval tv; |
| 225 |
|
| int res; |
| 226 |
|
| fd_set rfds; |
| 227 |
0 |
| tv.tv_sec = 0; |
| 228 |
0 |
| tv.tv_usec = 0; |
| 229 |
0 |
▶ | FD_ZERO(&rfds); |
Branch: 1 branch
| Branch | True | False | Total |
| FD_ZERO(&rfds); |
0 |
0 |
0 |
|
| 230 |
0 |
| FD_SET(ctrl->s, &rfds); |
| 231 |
0 |
| res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); |
| 232 |
0 |
| return FD_ISSET(ctrl->s, &rfds); |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
|
| |
| 236 |
0 |
| int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) |
| 237 |
|
| { |
| 238 |
0 |
| return ctrl->s; |
| 239 |
|
| } |