San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <string.h> |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 22 | #define LOG_TAG "WifiNetwork" |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include "NetworkManager.h" |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 26 | #include "WifiNetwork.h" |
| 27 | #include "Supplicant.h" |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 28 | #include "WifiController.h" |
| 29 | #include "InterfaceConfig.h" |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 30 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 31 | const char *WifiNetwork::PropertyNames[] = { "ssid", "bssid", "psk", "wepkey.1", |
| 32 | "wepkey.2", "wepkey.3", "wepkey.4", |
| 33 | "defkeyidx", "pri", "hiddenssid", |
| 34 | "AllowedKeyManagement", |
| 35 | "AllowedProtocols", |
| 36 | "AllowedAuthAlgorithms", |
| 37 | "AllowedPairwiseCiphers", |
| 38 | "AllowedGroupCiphers", |
| 39 | "enabled", '\0' }; |
| 40 | WifiNetwork::WifiNetwork() { |
| 41 | // This is private to restrict copy constructors |
| 42 | } |
| 43 | |
| 44 | WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, const char *data) { |
| 45 | mController = c; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 46 | mSuppl = suppl; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 47 | |
| 48 | char *tmp = strdup(data); |
| 49 | char *next = tmp; |
| 50 | char *id; |
| 51 | char *ssid; |
| 52 | char *bssid; |
| 53 | char *flags; |
| 54 | |
| 55 | if (!(id = strsep(&next, "\t"))) |
| 56 | LOGE("Failed to extract network id"); |
| 57 | if (!(ssid = strsep(&next, "\t"))) |
| 58 | LOGE("Failed to extract ssid"); |
| 59 | if (!(bssid = strsep(&next, "\t"))) |
| 60 | LOGE("Failed to extract bssid"); |
| 61 | if (!(flags = strsep(&next, "\t"))) |
| 62 | LOGE("Failed to extract flags"); |
| 63 | |
| 64 | // LOGD("id '%s', ssid '%s', bssid '%s', flags '%s'", id, ssid, bssid, |
| 65 | // flags ? flags :"null"); |
| 66 | |
| 67 | if (id) |
| 68 | mNetid = atoi(id); |
| 69 | if (ssid) |
| 70 | mSsid = strdup(ssid); |
| 71 | if (bssid) |
| 72 | mBssid = strdup(bssid); |
| 73 | |
| 74 | mPsk = NULL; |
| 75 | memset(mWepKeys, 0, sizeof(mWepKeys)); |
| 76 | mDefaultKeyIndex = -1; |
| 77 | mPriority = -1; |
| 78 | mHiddenSsid = NULL; |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 79 | mAllowedKeyManagement = KeyManagementMask::UNKNOWN; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 80 | mAllowedProtocols = 0; |
| 81 | mAllowedAuthAlgorithms = 0; |
| 82 | mAllowedPairwiseCiphers = 0; |
| 83 | mAllowedGroupCiphers = 0; |
| 84 | mEnabled = true; |
| 85 | |
| 86 | if (flags && flags[0] != '\0') { |
| 87 | if (!strcmp(flags, "[DISABLED]")) |
| 88 | mEnabled = false; |
| 89 | else |
| 90 | LOGW("Unsupported flags '%s'", flags); |
| 91 | } |
| 92 | |
| 93 | char *tmp2; |
| 94 | asprintf(&tmp2, "wifi.net.%d", mNetid); |
| 95 | mIfaceCfg = new InterfaceConfig(tmp2); |
| 96 | free(tmp2); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 97 | free(tmp); |
| 98 | } |
| 99 | |
| 100 | WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, int networkId) { |
| 101 | mController = c; |
| 102 | mSuppl = suppl; |
| 103 | mNetid = networkId; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 104 | mSsid = NULL; |
| 105 | mBssid = NULL; |
| 106 | mPsk = NULL; |
| 107 | memset(mWepKeys, 0, sizeof(mWepKeys)); |
| 108 | mDefaultKeyIndex = -1; |
| 109 | mPriority = -1; |
| 110 | mHiddenSsid = NULL; |
| 111 | mAllowedKeyManagement = 0; |
| 112 | mAllowedProtocols = 0; |
| 113 | mAllowedAuthAlgorithms = 0; |
| 114 | mAllowedPairwiseCiphers = 0; |
| 115 | mAllowedGroupCiphers = 0; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 116 | mEnabled = false; |
| 117 | |
| 118 | char *tmp2; |
| 119 | asprintf(&tmp2, "wifi.net.%d", mNetid); |
| 120 | mIfaceCfg = new InterfaceConfig(tmp2); |
| 121 | free(tmp2); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | WifiNetwork *WifiNetwork::clone() { |
| 125 | WifiNetwork *r = new WifiNetwork(); |
| 126 | |
| 127 | r->mSuppl = mSuppl; |
| 128 | r->mNetid = mNetid; |
| 129 | |
| 130 | if (mSsid) |
| 131 | r->mSsid = strdup(mSsid); |
| 132 | if (mBssid) |
| 133 | r->mBssid = strdup(mBssid); |
| 134 | if (mPsk) |
| 135 | r->mPsk = strdup(mPsk); |
| 136 | |
| 137 | r->mController = mController; |
| 138 | memcpy(r->mWepKeys, mWepKeys, sizeof(mWepKeys)); |
| 139 | r->mDefaultKeyIndex = mDefaultKeyIndex; |
| 140 | r->mPriority = mPriority; |
| 141 | if (mHiddenSsid) |
| 142 | r->mHiddenSsid = strdup(mHiddenSsid); |
| 143 | r->mAllowedKeyManagement = mAllowedKeyManagement; |
| 144 | r->mAllowedProtocols = mAllowedProtocols; |
| 145 | r->mAllowedAuthAlgorithms = mAllowedAuthAlgorithms; |
| 146 | r->mAllowedPairwiseCiphers = mAllowedPairwiseCiphers; |
| 147 | r->mAllowedGroupCiphers = mAllowedGroupCiphers; |
| 148 | return r; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | WifiNetwork::~WifiNetwork() { |
| 152 | if (mSsid) |
| 153 | free(mSsid); |
| 154 | if (mBssid) |
| 155 | free(mBssid); |
| 156 | if (mPsk) |
| 157 | free(mPsk); |
| 158 | for (int i = 0; i < 4; i++) { |
| 159 | if (mWepKeys[i]) |
| 160 | free(mWepKeys[i]); |
| 161 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 162 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 163 | if (mHiddenSsid) |
| 164 | free(mHiddenSsid); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 165 | if (mIfaceCfg) |
| 166 | delete(mIfaceCfg); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 167 | } |
| 168 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 169 | int WifiNetwork::refresh() { |
| 170 | char buffer[255]; |
| 171 | size_t len; |
| 172 | |
| 173 | len = sizeof(buffer); |
| 174 | if (mSuppl->getNetworkVar(mNetid, "psk", buffer, len)) |
| 175 | mPsk = strdup(buffer); |
| 176 | |
| 177 | for (int i = 0; i < 4; i++) { |
| 178 | char *name; |
| 179 | |
| 180 | asprintf(&name, "wep_key%d", i); |
| 181 | len = sizeof(buffer); |
| 182 | if (mSuppl->getNetworkVar(mNetid, name, buffer, len)) |
| 183 | mWepKeys[i] = strdup(buffer); |
| 184 | free(name); |
| 185 | } |
| 186 | |
| 187 | len = sizeof(buffer); |
| 188 | if (mSuppl->getNetworkVar(mNetid, "wep_tx_keyidx", buffer, len)) |
| 189 | mDefaultKeyIndex = atoi(buffer); |
| 190 | |
| 191 | len = sizeof(buffer); |
| 192 | if (mSuppl->getNetworkVar(mNetid, "priority", buffer, len)) |
| 193 | mPriority = atoi(buffer); |
| 194 | |
| 195 | len = sizeof(buffer); |
| 196 | if (mSuppl->getNetworkVar(mNetid, "scan_ssid", buffer, len)) |
| 197 | mHiddenSsid = strdup(buffer); |
| 198 | |
| 199 | len = sizeof(buffer); |
| 200 | if (mSuppl->getNetworkVar(mNetid, "key_mgmt", buffer, len)) { |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 201 | if (!strcmp(buffer, "NONE")) |
| 202 | setAllowedKeyManagement(KeyManagementMask::NONE); |
| 203 | else if (index(buffer, ' ')) { |
| 204 | char *next = buffer; |
| 205 | char *token; |
| 206 | uint32_t mask = 0; |
| 207 | |
| 208 | while((token = strsep(&next, " "))) { |
| 209 | if (!strcmp(token, "WPA-PSK")) |
| 210 | mask |= KeyManagementMask::WPA_PSK; |
| 211 | else if (!strcmp(token, "WPA-EAP")) |
| 212 | mask |= KeyManagementMask::WPA_EAP; |
| 213 | else if (!strcmp(token, "IEE8021X")) |
| 214 | mask |= KeyManagementMask::IEEE8021X; |
| 215 | else |
| 216 | LOGW("Unsupported key management scheme '%s'" , token); |
| 217 | } |
| 218 | setAllowedKeyManagement(mask); |
| 219 | } else |
| 220 | LOGE("Unsupported key management '%s'", buffer); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | len = sizeof(buffer); |
| 224 | if (mSuppl->getNetworkVar(mNetid, "proto", buffer, len)) { |
| 225 | // TODO |
| 226 | } |
| 227 | |
| 228 | len = sizeof(buffer); |
| 229 | if (mSuppl->getNetworkVar(mNetid, "auth_alg", buffer, len)) { |
| 230 | // TODO |
| 231 | } |
| 232 | |
| 233 | len = sizeof(buffer); |
| 234 | if (mSuppl->getNetworkVar(mNetid, "pairwise", buffer, len)) { |
| 235 | // TODO |
| 236 | } |
| 237 | |
| 238 | len = sizeof(buffer); |
| 239 | if (mSuppl->getNetworkVar(mNetid, "group", buffer, len)) { |
| 240 | // TODO |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | out_err: |
| 245 | LOGE("Refresh failed (%s)",strerror(errno)); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 249 | int WifiNetwork::set(const char *name, const char *value) { |
| 250 | char *n_tmp = strdup(name + strlen("wifi.net.")); |
| 251 | char *n_next = n_tmp; |
| 252 | char *n_local; |
| 253 | char *n_rest; |
| 254 | int rc = 0; |
| 255 | |
| 256 | if (!strsep(&n_next, ".")) // skip net id |
| 257 | goto out_inval; |
| 258 | |
| 259 | if (!(n_local = strsep(&n_next, "."))) |
| 260 | goto out_inval; |
| 261 | |
| 262 | n_rest = n_next; |
| 263 | |
| 264 | // LOGD("set(): var '%s'(%s / %s) = %s", name, n_local, n_rest, value); |
| 265 | if (!strcasecmp(n_local, "enabled")) |
| 266 | rc = setEnabled(atoi(value)); |
| 267 | else if (!strcmp(n_local, "ssid")) |
| 268 | rc = setSsid(value); |
| 269 | else if (!strcasecmp(n_local, "bssid")) |
| 270 | rc = setBssid(value); |
| 271 | else if (!strcasecmp(n_local, "psk")) |
| 272 | rc = setPsk(value); |
| 273 | else if (!strcasecmp(n_local, "wepkey")) |
| 274 | rc = setWepKey(atoi(n_rest) -1, value); |
| 275 | else if (!strcasecmp(n_local, "defkeyidx")) |
| 276 | rc = setDefaultKeyIndex(atoi(value)); |
| 277 | else if (!strcasecmp(n_local, "pri")) |
| 278 | rc = setPriority(atoi(value)); |
| 279 | else if (!strcasecmp(n_local, "hiddenssid")) |
| 280 | rc = setHiddenSsid(value); |
| 281 | else if (!strcasecmp(n_local, "AllowedKeyManagement")) { |
| 282 | uint32_t mask = 0; |
| 283 | bool none = false; |
| 284 | char *v_tmp = strdup(value); |
| 285 | char *v_next = v_tmp; |
| 286 | char *v_token; |
| 287 | |
| 288 | while((v_token = strsep(&v_next, " "))) { |
| 289 | if (!strcasecmp(v_token, "NONE")) { |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 290 | mask = KeyManagementMask::NONE; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 291 | none = true; |
| 292 | } else if (!none) { |
| 293 | if (!strcasecmp(v_token, "WPA_PSK")) |
| 294 | mask |= KeyManagementMask::WPA_PSK; |
| 295 | else if (!strcasecmp(v_token, "WPA_EAP")) |
| 296 | mask |= KeyManagementMask::WPA_EAP; |
| 297 | else if (!strcasecmp(v_token, "IEEE8021X")) |
| 298 | mask |= KeyManagementMask::IEEE8021X; |
| 299 | else { |
| 300 | errno = EINVAL; |
| 301 | rc = -1; |
| 302 | free(v_tmp); |
| 303 | goto out; |
| 304 | } |
| 305 | } else { |
| 306 | errno = EINVAL; |
| 307 | rc = -1; |
| 308 | free(v_tmp); |
| 309 | goto out; |
| 310 | } |
| 311 | } |
| 312 | free(v_tmp); |
| 313 | } else if (!strcasecmp(n_local, "AllowedProtocols")) { |
| 314 | // TODO |
| 315 | } else if (!strcasecmp(n_local, "AllowedPairwiseCiphers")) { |
| 316 | // TODO |
| 317 | } else if (!strcasecmp(n_local, "AllowedAuthAlgorithms")) { |
| 318 | // TODO |
| 319 | } else if (!strcasecmp(n_local, "AllowedGroupCiphers")) { |
| 320 | // TODO |
| 321 | } else { |
| 322 | errno = ENOENT; |
| 323 | free(n_tmp); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | out: |
| 328 | free(n_tmp); |
| 329 | return rc; |
| 330 | |
| 331 | out_inval: |
| 332 | errno = EINVAL; |
| 333 | free(n_tmp); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 337 | const char *WifiNetwork::get(const char *name, char *buffer, size_t maxsize) { |
| 338 | char *n_tmp = strdup(name + strlen("wifi.net.")); |
| 339 | char *n_next = n_tmp; |
| 340 | char *n_local; |
| 341 | char fc[64]; |
| 342 | char rc[128]; |
| 343 | |
| 344 | if (!strsep(&n_next, ".")) // skip net id |
| 345 | goto out_inval; |
| 346 | |
| 347 | if (!(n_local = strsep(&n_next, "."))) |
| 348 | goto out_inval; |
| 349 | |
| 350 | |
| 351 | strncpy(fc, n_local, sizeof(fc)); |
| 352 | rc[0] = '\0'; |
| 353 | if (n_next) |
| 354 | strncpy(rc, n_next, sizeof(rc)); |
| 355 | |
| 356 | free(n_tmp); |
| 357 | |
| 358 | if (!strcasecmp(fc, "enabled")) |
| 359 | snprintf(buffer, maxsize, "%d", getEnabled()); |
| 360 | else if (!strcasecmp(fc, "ssid")) { |
| 361 | strncpy(buffer, |
| 362 | getSsid() ? getSsid() : "none", |
| 363 | maxsize); |
| 364 | } else if (!strcasecmp(fc, "bssid")) { |
| 365 | strncpy(buffer, |
| 366 | getBssid() ? getBssid() : "none", |
| 367 | maxsize); |
| 368 | } else if (!strcasecmp(fc, "psk")) { |
| 369 | strncpy(buffer, |
| 370 | getPsk() ? getPsk() : "none", |
| 371 | maxsize); |
| 372 | } else if (!strcasecmp(fc, "wepkey")) { |
| 373 | strncpy(buffer, |
| 374 | getWepKey(atoi(rc)-1) ? getWepKey(atoi(rc)-1) : "none", |
| 375 | maxsize); |
| 376 | } else if (!strcasecmp(fc, "defkeyidx")) |
| 377 | snprintf(buffer, maxsize, "%d", getDefaultKeyIndex()); |
| 378 | else if (!strcasecmp(fc, "pri")) |
| 379 | snprintf(buffer, maxsize, "%d", getPriority()); |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 380 | else if (!strcasecmp(fc, "AllowedKeyManagement")) { |
| 381 | if (getAllowedKeyManagement() == KeyManagementMask::NONE) |
| 382 | strncpy(buffer, "NONE", maxsize); |
| 383 | else { |
| 384 | char tmp[80] = { '\0' }; |
| 385 | |
| 386 | if (getAllowedKeyManagement() & KeyManagementMask::WPA_PSK) |
| 387 | strcat(tmp, "WPA_PSK "); |
| 388 | if (getAllowedKeyManagement() & KeyManagementMask::WPA_EAP) |
| 389 | strcat(tmp, "WPA_EAP "); |
| 390 | if (getAllowedKeyManagement() & KeyManagementMask::IEEE8021X) |
| 391 | strcat(tmp, "IEEE8021X"); |
| 392 | if (tmp[0] == '\0') { |
| 393 | strncpy(buffer, "(internal error)", maxsize); |
| 394 | errno = ENOENT; |
| 395 | return NULL; |
| 396 | } |
| 397 | if (tmp[strlen(tmp)] == ' ') |
| 398 | tmp[strlen(tmp)] = '\0'; |
| 399 | |
| 400 | strncpy(buffer, tmp, maxsize); |
| 401 | } |
| 402 | } else if (!strcasecmp(fc, "hiddenssid")) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 403 | strncpy(buffer, |
| 404 | getHiddenSsid() ? getHiddenSsid() : "none", |
| 405 | maxsize); |
| 406 | } else { |
| 407 | strncpy(buffer, "(internal error)", maxsize); |
| 408 | errno = ENOENT; |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | return buffer; |
| 413 | |
| 414 | out_inval: |
| 415 | errno = EINVAL; |
| 416 | free(n_tmp); |
| 417 | return NULL; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 418 | } |
| 419 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 420 | int WifiNetwork::setSsid(const char *ssid) { |
| 421 | if (mSuppl->setNetworkVar(mNetid, "ssid", ssid)) |
| 422 | return -1; |
| 423 | if (mSsid) |
| 424 | free(mSsid); |
| 425 | mSsid = strdup(ssid); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | int WifiNetwork::setBssid(const char *bssid) { |
| 430 | if (mSuppl->setNetworkVar(mNetid, "bssid", bssid)) |
| 431 | return -1; |
| 432 | if (mBssid) |
| 433 | free(mBssid); |
| 434 | mBssid = strdup(bssid); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | int WifiNetwork::setPsk(const char *psk) { |
| 439 | if (mSuppl->setNetworkVar(mNetid, "psk", psk)) |
| 440 | return -1; |
| 441 | |
| 442 | if (mPsk) |
| 443 | free(mPsk); |
| 444 | mPsk = strdup(psk); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int WifiNetwork::setWepKey(int idx, const char *key) { |
| 449 | char *name; |
| 450 | |
| 451 | asprintf(&name, "wep_key%d", idx); |
| 452 | int rc = mSuppl->setNetworkVar(mNetid, name, key); |
| 453 | free(name); |
| 454 | |
| 455 | if (rc) |
| 456 | return -1; |
| 457 | |
| 458 | if (mWepKeys[idx]) |
| 459 | free(mWepKeys[idx]); |
| 460 | mWepKeys[idx] = strdup(key); |
| 461 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | int WifiNetwork::setDefaultKeyIndex(int idx) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 465 | char val[16]; |
| 466 | sprintf(val, "%d", idx); |
| 467 | if (mSuppl->setNetworkVar(mNetid, "wep_tx_keyidx", val)) |
| 468 | return -1; |
| 469 | |
| 470 | mDefaultKeyIndex = idx; |
| 471 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 472 | } |
| 473 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 474 | int WifiNetwork::setPriority(int priority) { |
| 475 | char val[16]; |
| 476 | sprintf(val, "%d", priority); |
| 477 | if (mSuppl->setNetworkVar(mNetid, "priority", val)) |
| 478 | return -1; |
| 479 | |
| 480 | mPriority = priority; |
| 481 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 482 | } |
| 483 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 484 | int WifiNetwork::setHiddenSsid(const char *ssid) { |
| 485 | if (mSuppl->setNetworkVar(mNetid, "scan_ssid", ssid)) |
| 486 | return -1; |
| 487 | |
| 488 | if (mHiddenSsid) |
| 489 | free(mHiddenSsid); |
| 490 | mHiddenSsid = strdup(ssid); |
| 491 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | int WifiNetwork::setAllowedKeyManagement(uint32_t mask) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 495 | char accum[255]; |
| 496 | |
| 497 | if (mask == KeyManagementMask::NONE) |
| 498 | strcpy(accum, "NONE"); |
| 499 | else { |
| 500 | if (mask & KeyManagementMask::WPA_PSK) |
| 501 | strcat(accum, "WPA_PSK "); |
| 502 | if (mask & KeyManagementMask::WPA_EAP) |
| 503 | strcat(accum, "WPA_EAP "); |
| 504 | if (mask & KeyManagementMask::IEEE8021X) |
| 505 | strcat(accum, "IEEE8021X "); |
| 506 | } |
| 507 | |
| 508 | if (mSuppl->setNetworkVar(mNetid, "key_mgmt", accum)) |
| 509 | return -1; |
| 510 | mAllowedKeyManagement = mask; |
| 511 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | int WifiNetwork::setAllowedProtocols(uint32_t mask) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 515 | char accum[255]; |
| 516 | |
| 517 | accum[0] = '\0'; |
| 518 | |
| 519 | if (mask & SecurityProtocolMask::WPA) |
| 520 | strcpy(accum, "WPA "); |
| 521 | |
| 522 | if (mask & SecurityProtocolMask::RSN) |
| 523 | strcat(accum, "RSN"); |
| 524 | |
| 525 | if (mSuppl->setNetworkVar(mNetid, "proto", accum)) |
| 526 | return -1; |
| 527 | mAllowedProtocols = mask; |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int WifiNetwork::setAllowedAuthAlgorithms(uint32_t mask) { |
| 532 | char accum[255]; |
| 533 | |
| 534 | accum[0] = '\0'; |
| 535 | |
| 536 | if (mask & AuthenticationAlgorithmMask::OPEN) |
| 537 | strcpy(accum, "OPEN "); |
| 538 | |
| 539 | if (mask & AuthenticationAlgorithmMask::SHARED) |
| 540 | strcat(accum, "SHARED "); |
| 541 | |
| 542 | if (mask & AuthenticationAlgorithmMask::LEAP) |
| 543 | strcat(accum, "LEAP "); |
| 544 | |
| 545 | if (mSuppl->setNetworkVar(mNetid, "auth_alg", accum)) |
| 546 | return -1; |
| 547 | |
| 548 | mAllowedAuthAlgorithms = mask; |
| 549 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | int WifiNetwork::setAllowedPairwiseCiphers(uint32_t mask) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 553 | char accum[255]; |
| 554 | |
| 555 | if (mask == PairwiseCiphersMask::NONE) |
| 556 | strcpy(accum, "NONE"); |
| 557 | else { |
| 558 | if (mask & PairwiseCiphersMask::TKIP) |
| 559 | strcat(accum, "TKIP "); |
| 560 | if (mask & PairwiseCiphersMask::CCMP) |
| 561 | strcat(accum, "CCMP "); |
| 562 | } |
| 563 | |
| 564 | if (mSuppl->setNetworkVar(mNetid, "pairwise", accum)) |
| 565 | return -1; |
| 566 | |
| 567 | mAllowedPairwiseCiphers = mask; |
| 568 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | int WifiNetwork::setAllowedGroupCiphers(uint32_t mask) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 572 | char accum[255]; |
| 573 | |
| 574 | if (mask & GroupCiphersMask::WEP40) |
| 575 | strcat(accum, "WEP40 "); |
| 576 | if (mask & GroupCiphersMask::WEP104) |
| 577 | strcat(accum, "WEP104 "); |
| 578 | if (mask & GroupCiphersMask::TKIP) |
| 579 | strcat(accum, "TKIP "); |
| 580 | if (mask & GroupCiphersMask::CCMP) |
| 581 | strcat(accum, "CCMP "); |
| 582 | |
| 583 | if (mSuppl->setNetworkVar(mNetid, "group", accum)) |
| 584 | return -1; |
| 585 | mAllowedGroupCiphers = mask; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | int WifiNetwork::setEnabled(bool enabled) { |
San Mehat | 21e90f0 | 2009-06-01 10:04:21 -0700 | [diff] [blame] | 590 | |
| 591 | if (enabled) { |
| 592 | if (getPriority() == -1) { |
| 593 | LOGE("Cannot enable network when priority is not set"); |
| 594 | errno = EAGAIN; |
| 595 | return -1; |
| 596 | } |
| 597 | if (getAllowedKeyManagement() == KeyManagementMask::UNKNOWN) { |
| 598 | LOGE("Cannot enable network when KeyManagement is not set"); |
| 599 | errno = EAGAIN; |
| 600 | return -1; |
| 601 | } |
| 602 | } |
| 603 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 604 | if (mSuppl->enableNetwork(mNetid, enabled)) |
| 605 | return -1; |
| 606 | |
| 607 | mEnabled = enabled; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | int WifiNetwork::registerProperties() { |
| 612 | for (const char **p = WifiNetwork::PropertyNames; *p != '\0'; p++) { |
| 613 | char *tmp; |
| 614 | asprintf(&tmp, "wifi.net.%d.%s", mNetid, *p); |
| 615 | |
| 616 | if (NetworkManager::Instance()->getPropMngr()->registerProperty(tmp, |
| 617 | this)) { |
| 618 | free(tmp); |
| 619 | return -1; |
| 620 | } |
| 621 | free(tmp); |
| 622 | } |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | int WifiNetwork::unregisterProperties() { |
| 627 | for (const char **p = WifiNetwork::PropertyNames; *p != '\0'; p++) { |
| 628 | char *tmp; |
| 629 | asprintf(&tmp, "wifi.net.%d.%s", mNetid, *p); |
| 630 | |
| 631 | if (NetworkManager::Instance()->getPropMngr()->unregisterProperty(tmp)) |
| 632 | LOGW("Unable to remove property '%s' (%s)", tmp, strerror(errno)); |
| 633 | free(tmp); |
| 634 | } |
| 635 | return 0; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 636 | } |