blob: 92af0cbb1b8a6afd3c8bfc5f949073384272554d [file] [log] [blame]
San Mehat82a21162009-05-12 17:26:28 -07001/*
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 Mehat3c5a6f02009-05-22 15:36:13 -070019#include <stdlib.h>
San Mehat82a21162009-05-12 17:26:28 -070020#include <sys/types.h>
21
San Mehat3c5a6f02009-05-22 15:36:13 -070022#define LOG_TAG "WifiNetwork"
23#include <cutils/log.h>
24
25#include "NetworkManager.h"
San Mehat82a21162009-05-12 17:26:28 -070026#include "WifiNetwork.h"
27#include "Supplicant.h"
San Mehat3c5a6f02009-05-22 15:36:13 -070028#include "WifiController.h"
San Mehat82a21162009-05-12 17:26:28 -070029
San Mehat3c5a6f02009-05-22 15:36:13 -070030WifiNetwork::WifiNetwork() {
31 // This is private to restrict copy constructors
32}
33
34WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, const char *data) {
35 mController = c;
San Mehat82a21162009-05-12 17:26:28 -070036 mSuppl = suppl;
San Mehat3c5a6f02009-05-22 15:36:13 -070037
38 char *tmp = strdup(data);
39 char *next = tmp;
40 char *id;
41 char *ssid;
42 char *bssid;
43 char *flags;
44
45 if (!(id = strsep(&next, "\t")))
46 LOGE("Failed to extract network id");
47 if (!(ssid = strsep(&next, "\t")))
48 LOGE("Failed to extract ssid");
49 if (!(bssid = strsep(&next, "\t")))
50 LOGE("Failed to extract bssid");
51 if (!(flags = strsep(&next, "\t")))
52 LOGE("Failed to extract flags");
53
Steve Block8d66c492011-12-20 16:07:45 +000054 // ALOGD("id '%s', ssid '%s', bssid '%s', flags '%s'", id, ssid, bssid,
San Mehat3c5a6f02009-05-22 15:36:13 -070055 // flags ? flags :"null");
56
57 if (id)
58 mNetid = atoi(id);
59 if (ssid)
60 mSsid = strdup(ssid);
61 if (bssid)
62 mBssid = strdup(bssid);
63
64 mPsk = NULL;
65 memset(mWepKeys, 0, sizeof(mWepKeys));
66 mDefaultKeyIndex = -1;
67 mPriority = -1;
68 mHiddenSsid = NULL;
San Mehatc4a895b2009-06-23 21:10:57 -070069 mKeyManagement = KeyManagementMask::UNKNOWN;
70 mProtocols = 0;
71 mAuthAlgorithms = 0;
72 mPairwiseCiphers = 0;
73 mGroupCiphers = 0;
San Mehat3c5a6f02009-05-22 15:36:13 -070074 mEnabled = true;
75
76 if (flags && flags[0] != '\0') {
77 if (!strcmp(flags, "[DISABLED]"))
78 mEnabled = false;
79 else
80 LOGW("Unsupported flags '%s'", flags);
81 }
82
San Mehat3c5a6f02009-05-22 15:36:13 -070083 free(tmp);
San Mehatc4a895b2009-06-23 21:10:57 -070084 createProperties();
San Mehat3c5a6f02009-05-22 15:36:13 -070085}
86
87WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, int networkId) {
88 mController = c;
89 mSuppl = suppl;
90 mNetid = networkId;
San Mehat82a21162009-05-12 17:26:28 -070091 mSsid = NULL;
92 mBssid = NULL;
93 mPsk = NULL;
94 memset(mWepKeys, 0, sizeof(mWepKeys));
95 mDefaultKeyIndex = -1;
96 mPriority = -1;
97 mHiddenSsid = NULL;
San Mehatc4a895b2009-06-23 21:10:57 -070098 mKeyManagement = 0;
99 mProtocols = 0;
100 mAuthAlgorithms = 0;
101 mPairwiseCiphers = 0;
102 mGroupCiphers = 0;
San Mehat3c5a6f02009-05-22 15:36:13 -0700103 mEnabled = false;
San Mehatc4a895b2009-06-23 21:10:57 -0700104 createProperties();
San Mehat3c5a6f02009-05-22 15:36:13 -0700105}
106
107WifiNetwork *WifiNetwork::clone() {
108 WifiNetwork *r = new WifiNetwork();
109
110 r->mSuppl = mSuppl;
111 r->mNetid = mNetid;
112
113 if (mSsid)
114 r->mSsid = strdup(mSsid);
115 if (mBssid)
116 r->mBssid = strdup(mBssid);
117 if (mPsk)
118 r->mPsk = strdup(mPsk);
119
120 r->mController = mController;
121 memcpy(r->mWepKeys, mWepKeys, sizeof(mWepKeys));
122 r->mDefaultKeyIndex = mDefaultKeyIndex;
123 r->mPriority = mPriority;
124 if (mHiddenSsid)
125 r->mHiddenSsid = strdup(mHiddenSsid);
San Mehatc4a895b2009-06-23 21:10:57 -0700126 r->mKeyManagement = mKeyManagement;
127 r->mProtocols = mProtocols;
128 r->mAuthAlgorithms = mAuthAlgorithms;
129 r->mPairwiseCiphers = mPairwiseCiphers;
130 r->mGroupCiphers = mGroupCiphers;
San Mehat3c5a6f02009-05-22 15:36:13 -0700131 return r;
San Mehat82a21162009-05-12 17:26:28 -0700132}
133
San Mehatc4a895b2009-06-23 21:10:57 -0700134void WifiNetwork::createProperties() {
135 asprintf(&mPropNamespace, "wifi.net.%d", mNetid);
136
137 mStaticProperties.propEnabled = new WifiNetworkEnabledProperty(this);
138 mStaticProperties.propSsid = new WifiNetworkSsidProperty(this);
139 mStaticProperties.propBssid = new WifiNetworkBssidProperty(this);
140 mStaticProperties.propPsk = new WifiNetworkPskProperty(this);
141 mStaticProperties.propWepKey = new WifiNetworkWepKeyProperty(this);
142 mStaticProperties.propDefKeyIdx = new WifiNetworkDefaultKeyIndexProperty(this);
143 mStaticProperties.propPriority = new WifiNetworkPriorityProperty(this);
144 mStaticProperties.propKeyManagement = new WifiNetworkKeyManagementProperty(this);
145 mStaticProperties.propProtocols = new WifiNetworkProtocolsProperty(this);
146 mStaticProperties.propAuthAlgorithms = new WifiNetworkAuthAlgorithmsProperty(this);
147 mStaticProperties.propPairwiseCiphers = new WifiNetworkPairwiseCiphersProperty(this);
148 mStaticProperties.propGroupCiphers = new WifiNetworkGroupCiphersProperty(this);
149 mStaticProperties.propHiddenSsid = new WifiNetworkHiddenSsidProperty(this);
150}
151
San Mehat82a21162009-05-12 17:26:28 -0700152WifiNetwork::~WifiNetwork() {
San Mehatc4a895b2009-06-23 21:10:57 -0700153 if (mPropNamespace)
154 free(mPropNamespace);
San Mehat82a21162009-05-12 17:26:28 -0700155 if (mSsid)
156 free(mSsid);
157 if (mBssid)
158 free(mBssid);
159 if (mPsk)
160 free(mPsk);
161 for (int i = 0; i < 4; i++) {
162 if (mWepKeys[i])
163 free(mWepKeys[i]);
164 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700165
San Mehat82a21162009-05-12 17:26:28 -0700166 if (mHiddenSsid)
167 free(mHiddenSsid);
San Mehatc4a895b2009-06-23 21:10:57 -0700168
169 delete mStaticProperties.propEnabled;
170 delete mStaticProperties.propSsid;
171 delete mStaticProperties.propBssid;
172 delete mStaticProperties.propPsk;
173 delete mStaticProperties.propWepKey;
174 delete mStaticProperties.propDefKeyIdx;
175 delete mStaticProperties.propPriority;
176 delete mStaticProperties.propKeyManagement;
177 delete mStaticProperties.propProtocols;
178 delete mStaticProperties.propAuthAlgorithms;
179 delete mStaticProperties.propPairwiseCiphers;
180 delete mStaticProperties.propGroupCiphers;
181 delete mStaticProperties.propHiddenSsid;
San Mehat82a21162009-05-12 17:26:28 -0700182}
183
San Mehat3c5a6f02009-05-22 15:36:13 -0700184int WifiNetwork::refresh() {
185 char buffer[255];
186 size_t len;
San Mehatc4a895b2009-06-23 21:10:57 -0700187 uint32_t mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700188
189 len = sizeof(buffer);
190 if (mSuppl->getNetworkVar(mNetid, "psk", buffer, len))
191 mPsk = strdup(buffer);
192
193 for (int i = 0; i < 4; i++) {
194 char *name;
195
196 asprintf(&name, "wep_key%d", i);
197 len = sizeof(buffer);
198 if (mSuppl->getNetworkVar(mNetid, name, buffer, len))
199 mWepKeys[i] = strdup(buffer);
200 free(name);
201 }
202
203 len = sizeof(buffer);
204 if (mSuppl->getNetworkVar(mNetid, "wep_tx_keyidx", buffer, len))
205 mDefaultKeyIndex = atoi(buffer);
206
207 len = sizeof(buffer);
208 if (mSuppl->getNetworkVar(mNetid, "priority", buffer, len))
209 mPriority = atoi(buffer);
210
211 len = sizeof(buffer);
212 if (mSuppl->getNetworkVar(mNetid, "scan_ssid", buffer, len))
213 mHiddenSsid = strdup(buffer);
214
215 len = sizeof(buffer);
216 if (mSuppl->getNetworkVar(mNetid, "key_mgmt", buffer, len)) {
San Mehatc4a895b2009-06-23 21:10:57 -0700217 if (WifiNetwork::parseKeyManagementMask(buffer, &mask)) {
218 LOGE("Error parsing key_mgmt (%s)", strerror(errno));
219 } else {
220 mKeyManagement = mask;
221 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700222 }
223
224 len = sizeof(buffer);
225 if (mSuppl->getNetworkVar(mNetid, "proto", buffer, len)) {
San Mehatc4a895b2009-06-23 21:10:57 -0700226 if (WifiNetwork::parseProtocolsMask(buffer, &mask)) {
227 LOGE("Error parsing proto (%s)", strerror(errno));
228 } else {
229 mProtocols = mask;
230 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700231 }
232
233 len = sizeof(buffer);
234 if (mSuppl->getNetworkVar(mNetid, "auth_alg", buffer, len)) {
San Mehatc4a895b2009-06-23 21:10:57 -0700235 if (WifiNetwork::parseAuthAlgorithmsMask(buffer, &mask)) {
236 LOGE("Error parsing auth_alg (%s)", strerror(errno));
237 } else {
238 mAuthAlgorithms = mask;
239 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700240 }
241
242 len = sizeof(buffer);
243 if (mSuppl->getNetworkVar(mNetid, "pairwise", buffer, len)) {
San Mehatc4a895b2009-06-23 21:10:57 -0700244 if (WifiNetwork::parsePairwiseCiphersMask(buffer, &mask)) {
245 LOGE("Error parsing pairwise (%s)", strerror(errno));
246 } else {
247 mPairwiseCiphers = mask;
248 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700249 }
250
251 len = sizeof(buffer);
252 if (mSuppl->getNetworkVar(mNetid, "group", buffer, len)) {
San Mehatc4a895b2009-06-23 21:10:57 -0700253 if (WifiNetwork::parseGroupCiphersMask(buffer, &mask)) {
254 LOGE("Error parsing group (%s)", strerror(errno));
255 } else {
256 mGroupCiphers = mask;
257 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700258 }
259
260 return 0;
261out_err:
262 LOGE("Refresh failed (%s)",strerror(errno));
San Mehat82a21162009-05-12 17:26:28 -0700263 return -1;
264}
265
San Mehat3c5a6f02009-05-22 15:36:13 -0700266int WifiNetwork::setSsid(const char *ssid) {
San Mehatc4a895b2009-06-23 21:10:57 -0700267 char tmp[255];
268 snprintf(tmp, sizeof(tmp), "\"%s\"", ssid);
269 if (mSuppl->setNetworkVar(mNetid, "ssid", tmp))
San Mehat3c5a6f02009-05-22 15:36:13 -0700270 return -1;
271 if (mSsid)
272 free(mSsid);
273 mSsid = strdup(ssid);
274 return 0;
275}
276
277int WifiNetwork::setBssid(const char *bssid) {
278 if (mSuppl->setNetworkVar(mNetid, "bssid", bssid))
279 return -1;
280 if (mBssid)
281 free(mBssid);
282 mBssid = strdup(bssid);
283 return 0;
284}
285
286int WifiNetwork::setPsk(const char *psk) {
San Mehatc4a895b2009-06-23 21:10:57 -0700287 char tmp[255];
288 snprintf(tmp, sizeof(tmp), "\"%s\"", psk);
289 if (mSuppl->setNetworkVar(mNetid, "psk", tmp))
San Mehat3c5a6f02009-05-22 15:36:13 -0700290 return -1;
291
292 if (mPsk)
293 free(mPsk);
294 mPsk = strdup(psk);
295 return 0;
296}
297
298int WifiNetwork::setWepKey(int idx, const char *key) {
299 char *name;
300
301 asprintf(&name, "wep_key%d", idx);
302 int rc = mSuppl->setNetworkVar(mNetid, name, key);
303 free(name);
304
305 if (rc)
306 return -1;
307
308 if (mWepKeys[idx])
309 free(mWepKeys[idx]);
310 mWepKeys[idx] = strdup(key);
311 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700312}
313
314int WifiNetwork::setDefaultKeyIndex(int idx) {
San Mehat3c5a6f02009-05-22 15:36:13 -0700315 char val[16];
316 sprintf(val, "%d", idx);
317 if (mSuppl->setNetworkVar(mNetid, "wep_tx_keyidx", val))
318 return -1;
319
320 mDefaultKeyIndex = idx;
321 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700322}
323
San Mehat3c5a6f02009-05-22 15:36:13 -0700324int WifiNetwork::setPriority(int priority) {
325 char val[16];
326 sprintf(val, "%d", priority);
327 if (mSuppl->setNetworkVar(mNetid, "priority", val))
328 return -1;
329
330 mPriority = priority;
331 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700332}
333
San Mehat3c5a6f02009-05-22 15:36:13 -0700334int WifiNetwork::setHiddenSsid(const char *ssid) {
335 if (mSuppl->setNetworkVar(mNetid, "scan_ssid", ssid))
336 return -1;
337
338 if (mHiddenSsid)
339 free(mHiddenSsid);
340 mHiddenSsid = strdup(ssid);
341 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700342}
343
San Mehatc4a895b2009-06-23 21:10:57 -0700344int WifiNetwork::setKeyManagement(uint32_t mask) {
345 char accum[64] = {'\0'};
San Mehat3c5a6f02009-05-22 15:36:13 -0700346
347 if (mask == KeyManagementMask::NONE)
348 strcpy(accum, "NONE");
349 else {
San Mehatc4a895b2009-06-23 21:10:57 -0700350 if (mask & KeyManagementMask::WPA_PSK)
351 strcat(accum, "WPA-PSK");
352 if (mask & KeyManagementMask::WPA_EAP) {
353 if (accum[0] != '\0')
354 strcat(accum, " ");
355 strcat(accum, "WPA-EAP");
356 }
357 if (mask & KeyManagementMask::IEEE8021X) {
358 if (accum[0] != '\0')
359 strcat(accum, " ");
360 strcat(accum, "IEEE8021X");
361 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700362 }
363
364 if (mSuppl->setNetworkVar(mNetid, "key_mgmt", accum))
365 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700366 mKeyManagement = mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700367 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700368}
369
San Mehatc4a895b2009-06-23 21:10:57 -0700370int WifiNetwork::setProtocols(uint32_t mask) {
371 char accum[64];
San Mehat3c5a6f02009-05-22 15:36:13 -0700372
373 accum[0] = '\0';
374
375 if (mask & SecurityProtocolMask::WPA)
376 strcpy(accum, "WPA ");
377
378 if (mask & SecurityProtocolMask::RSN)
379 strcat(accum, "RSN");
380
381 if (mSuppl->setNetworkVar(mNetid, "proto", accum))
382 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700383 mProtocols = mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700384 return 0;
385}
386
San Mehatc4a895b2009-06-23 21:10:57 -0700387int WifiNetwork::setAuthAlgorithms(uint32_t mask) {
388 char accum[64];
San Mehat3c5a6f02009-05-22 15:36:13 -0700389
390 accum[0] = '\0';
391
San Mehatc4a895b2009-06-23 21:10:57 -0700392 if (mask == 0)
393 strcpy(accum, "");
394
San Mehat3c5a6f02009-05-22 15:36:13 -0700395 if (mask & AuthenticationAlgorithmMask::OPEN)
396 strcpy(accum, "OPEN ");
397
398 if (mask & AuthenticationAlgorithmMask::SHARED)
399 strcat(accum, "SHARED ");
400
401 if (mask & AuthenticationAlgorithmMask::LEAP)
402 strcat(accum, "LEAP ");
403
404 if (mSuppl->setNetworkVar(mNetid, "auth_alg", accum))
405 return -1;
406
San Mehatc4a895b2009-06-23 21:10:57 -0700407 mAuthAlgorithms = mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700408 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700409}
410
San Mehatc4a895b2009-06-23 21:10:57 -0700411int WifiNetwork::setPairwiseCiphers(uint32_t mask) {
412 char accum[64];
413
414 accum[0] = '\0';
San Mehat3c5a6f02009-05-22 15:36:13 -0700415
416 if (mask == PairwiseCiphersMask::NONE)
417 strcpy(accum, "NONE");
418 else {
419 if (mask & PairwiseCiphersMask::TKIP)
420 strcat(accum, "TKIP ");
421 if (mask & PairwiseCiphersMask::CCMP)
422 strcat(accum, "CCMP ");
423 }
424
425 if (mSuppl->setNetworkVar(mNetid, "pairwise", accum))
426 return -1;
427
San Mehatc4a895b2009-06-23 21:10:57 -0700428 mPairwiseCiphers = mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700429 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700430}
431
San Mehatc4a895b2009-06-23 21:10:57 -0700432int WifiNetwork::setGroupCiphers(uint32_t mask) {
433 char accum[64];
434
435 accum[0] = '\0';
San Mehat3c5a6f02009-05-22 15:36:13 -0700436
437 if (mask & GroupCiphersMask::WEP40)
438 strcat(accum, "WEP40 ");
439 if (mask & GroupCiphersMask::WEP104)
440 strcat(accum, "WEP104 ");
441 if (mask & GroupCiphersMask::TKIP)
442 strcat(accum, "TKIP ");
443 if (mask & GroupCiphersMask::CCMP)
444 strcat(accum, "CCMP ");
445
446 if (mSuppl->setNetworkVar(mNetid, "group", accum))
447 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700448 mGroupCiphers = mask;
San Mehat3c5a6f02009-05-22 15:36:13 -0700449 return 0;
450}
451
452int WifiNetwork::setEnabled(bool enabled) {
San Mehat21e90f02009-06-01 10:04:21 -0700453
454 if (enabled) {
455 if (getPriority() == -1) {
456 LOGE("Cannot enable network when priority is not set");
457 errno = EAGAIN;
458 return -1;
459 }
San Mehatc4a895b2009-06-23 21:10:57 -0700460 if (getKeyManagement() == KeyManagementMask::UNKNOWN) {
San Mehat21e90f02009-06-01 10:04:21 -0700461 LOGE("Cannot enable network when KeyManagement is not set");
462 errno = EAGAIN;
463 return -1;
464 }
465 }
466
San Mehat3c5a6f02009-05-22 15:36:13 -0700467 if (mSuppl->enableNetwork(mNetid, enabled))
468 return -1;
469
470 mEnabled = enabled;
471 return 0;
472}
473
San Mehatc4a895b2009-06-23 21:10:57 -0700474int WifiNetwork::attachProperties(PropertyManager *pm, const char *nsName) {
475 pm->attachProperty(nsName, mStaticProperties.propSsid);
476 pm->attachProperty(nsName, mStaticProperties.propBssid);
477 pm->attachProperty(nsName, mStaticProperties.propPsk);
478 pm->attachProperty(nsName, mStaticProperties.propWepKey);
479 pm->attachProperty(nsName, mStaticProperties.propDefKeyIdx);
480 pm->attachProperty(nsName, mStaticProperties.propPriority);
481 pm->attachProperty(nsName, mStaticProperties.propKeyManagement);
482 pm->attachProperty(nsName, mStaticProperties.propProtocols);
483 pm->attachProperty(nsName, mStaticProperties.propAuthAlgorithms);
484 pm->attachProperty(nsName, mStaticProperties.propPairwiseCiphers);
485 pm->attachProperty(nsName, mStaticProperties.propGroupCiphers);
486 pm->attachProperty(nsName, mStaticProperties.propHiddenSsid);
487 pm->attachProperty(nsName, mStaticProperties.propEnabled);
San Mehat3c5a6f02009-05-22 15:36:13 -0700488 return 0;
489}
490
San Mehatc4a895b2009-06-23 21:10:57 -0700491int WifiNetwork::detachProperties(PropertyManager *pm, const char *nsName) {
492 pm->detachProperty(nsName, mStaticProperties.propEnabled);
493 pm->detachProperty(nsName, mStaticProperties.propSsid);
494 pm->detachProperty(nsName, mStaticProperties.propBssid);
495 pm->detachProperty(nsName, mStaticProperties.propPsk);
496 pm->detachProperty(nsName, mStaticProperties.propWepKey);
497 pm->detachProperty(nsName, mStaticProperties.propDefKeyIdx);
498 pm->detachProperty(nsName, mStaticProperties.propPriority);
499 pm->detachProperty(nsName, mStaticProperties.propKeyManagement);
500 pm->detachProperty(nsName, mStaticProperties.propProtocols);
501 pm->detachProperty(nsName, mStaticProperties.propAuthAlgorithms);
502 pm->detachProperty(nsName, mStaticProperties.propPairwiseCiphers);
503 pm->detachProperty(nsName, mStaticProperties.propGroupCiphers);
504 pm->detachProperty(nsName, mStaticProperties.propHiddenSsid);
505 return 0;
506}
San Mehat3c5a6f02009-05-22 15:36:13 -0700507
San Mehatc4a895b2009-06-23 21:10:57 -0700508int WifiNetwork::parseKeyManagementMask(const char *buffer, uint32_t *mask) {
509 bool none = false;
510 char *v_tmp = strdup(buffer);
511 char *v_next = v_tmp;
512 char *v_token;
513
Steve Block8d66c492011-12-20 16:07:45 +0000514// ALOGD("parseKeyManagementMask(%s)", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700515 *mask = 0;
516
517 while((v_token = strsep(&v_next, " "))) {
518 if (!strcasecmp(v_token, "NONE")) {
519 *mask = KeyManagementMask::NONE;
520 none = true;
521 } else if (!none) {
522 if (!strcasecmp(v_token, "WPA-PSK"))
523 *mask |= KeyManagementMask::WPA_PSK;
524 else if (!strcasecmp(v_token, "WPA-EAP"))
525 *mask |= KeyManagementMask::WPA_EAP;
526 else if (!strcasecmp(v_token, "IEEE8021X"))
527 *mask |= KeyManagementMask::IEEE8021X;
528 else {
529 LOGW("Invalid KeyManagementMask value '%s'", v_token);
530 errno = EINVAL;
531 free(v_tmp);
532 return -1;
533 }
534 } else {
535 LOGW("KeyManagementMask value '%s' when NONE", v_token);
536 errno = EINVAL;
537 free(v_tmp);
538 return -1;
539 }
540 }
541 free(v_tmp);
542 return 0;
543}
544
545int WifiNetwork::parseProtocolsMask(const char *buffer, uint32_t *mask) {
546 bool none = false;
547 char *v_tmp = strdup(buffer);
548 char *v_next = v_tmp;
549 char *v_token;
550
Steve Block8d66c492011-12-20 16:07:45 +0000551// ALOGD("parseProtocolsMask(%s)", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700552 *mask = 0;
553 while((v_token = strsep(&v_next, " "))) {
554 if (!strcasecmp(v_token, "WPA"))
555 *mask |= SecurityProtocolMask::WPA;
556 else if (!strcasecmp(v_token, "RSN"))
557 *mask |= SecurityProtocolMask::RSN;
558 else {
559 LOGW("Invalid ProtocolsMask value '%s'", v_token);
560 errno = EINVAL;
561 free(v_tmp);
562 return -1;
563 }
564 }
565
566 free(v_tmp);
567 return 0;
568}
569
570int WifiNetwork::parseAuthAlgorithmsMask(const char *buffer, uint32_t *mask) {
571 bool none = false;
572 char *v_tmp = strdup(buffer);
573 char *v_next = v_tmp;
574 char *v_token;
575
Steve Block8d66c492011-12-20 16:07:45 +0000576// ALOGD("parseAuthAlgorithmsMask(%s)", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700577
578 *mask = 0;
579 if (buffer[0] == '\0')
580 return 0;
581
582 while((v_token = strsep(&v_next, " "))) {
583 if (!strcasecmp(v_token, "OPEN"))
584 *mask |= AuthenticationAlgorithmMask::OPEN;
585 else if (!strcasecmp(v_token, "SHARED"))
586 *mask |= AuthenticationAlgorithmMask::SHARED;
587 else if (!strcasecmp(v_token, "LEAP"))
588 *mask |= AuthenticationAlgorithmMask::LEAP;
589 else {
590 LOGW("Invalid AuthAlgorithmsMask value '%s'", v_token);
591 errno = EINVAL;
592 free(v_tmp);
593 return -1;
594 }
595 }
596 free(v_tmp);
597 return 0;
598}
599
600int WifiNetwork::parsePairwiseCiphersMask(const char *buffer, uint32_t *mask) {
601 bool none = false;
602 char *v_tmp = strdup(buffer);
603 char *v_next = v_tmp;
604 char *v_token;
605
Steve Block8d66c492011-12-20 16:07:45 +0000606// ALOGD("parsePairwiseCiphersMask(%s)", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700607
608 *mask = 0;
609 while((v_token = strsep(&v_next, " "))) {
610 if (!strcasecmp(v_token, "NONE")) {
611 *mask = PairwiseCiphersMask::NONE;
612 none = true;
613 } else if (!none) {
614 if (!strcasecmp(v_token, "TKIP"))
615 *mask |= PairwiseCiphersMask::TKIP;
616 else if (!strcasecmp(v_token, "CCMP"))
617 *mask |= PairwiseCiphersMask::CCMP;
618 else {
619 LOGW("PairwiseCiphersMask value '%s' when NONE", v_token);
620 errno = EINVAL;
621 free(v_tmp);
622 return -1;
623 }
624 } else {
625 LOGW("Invalid PairwiseCiphersMask value '%s'", v_token);
626 errno = EINVAL;
627 free(v_tmp);
628 return -1;
629 }
630 }
631 free(v_tmp);
632 return 0;
633}
634
635int WifiNetwork::parseGroupCiphersMask(const char *buffer, uint32_t *mask) {
636 bool none = false;
637 char *v_tmp = strdup(buffer);
638 char *v_next = v_tmp;
639 char *v_token;
640
Steve Block8d66c492011-12-20 16:07:45 +0000641// ALOGD("parseGroupCiphersMask(%s)", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700642
643 *mask = 0;
644 while((v_token = strsep(&v_next, " "))) {
645 if (!strcasecmp(v_token, "WEP40"))
646 *mask |= GroupCiphersMask::WEP40;
647 else if (!strcasecmp(v_token, "WEP104"))
648 *mask |= GroupCiphersMask::WEP104;
649 else if (!strcasecmp(v_token, "TKIP"))
650 *mask |= GroupCiphersMask::TKIP;
651 else if (!strcasecmp(v_token, "CCMP"))
652 *mask |= GroupCiphersMask::CCMP;
653 else {
654 LOGW("Invalid GroupCiphersMask value '%s'", v_token);
655 errno = EINVAL;
656 free(v_tmp);
657 return -1;
658 }
659 }
660 free(v_tmp);
661 return 0;
662}
663
664WifiNetwork::WifiNetworkIntegerProperty::WifiNetworkIntegerProperty(WifiNetwork *wn,
665 const char *name,
666 bool ro,
667 int elements) :
668 IntegerProperty(name, ro, elements) {
669 mWn = wn;
670}
671
672WifiNetwork::WifiNetworkStringProperty::WifiNetworkStringProperty(WifiNetwork *wn,
673 const char *name,
674 bool ro, int elements) :
675 StringProperty(name, ro, elements) {
676 mWn = wn;
677}
678
679WifiNetwork::WifiNetworkEnabledProperty::WifiNetworkEnabledProperty(WifiNetwork *wn) :
680 WifiNetworkIntegerProperty(wn, "Enabled", false, 1) {
681}
682
683int WifiNetwork::WifiNetworkEnabledProperty::get(int idx, int *buffer) {
684 *buffer = mWn->mEnabled;
685 return 0;
686}
687int WifiNetwork::WifiNetworkEnabledProperty::set(int idx, int value) {
688 return mWn->setEnabled(value == 1);
689}
690
691WifiNetwork::WifiNetworkSsidProperty::WifiNetworkSsidProperty(WifiNetwork *wn) :
692 WifiNetworkStringProperty(wn, "Ssid", false, 1) {
693}
694
695int WifiNetwork::WifiNetworkSsidProperty::get(int idx, char *buffer, size_t max) {
696 strncpy(buffer,
697 mWn->getSsid() ? mWn->getSsid() : "none",
698 max);
699 return 0;
700}
701int WifiNetwork::WifiNetworkSsidProperty::set(int idx, const char *value) {
702 return mWn->setSsid(value);
703}
704
705WifiNetwork::WifiNetworkBssidProperty::WifiNetworkBssidProperty(WifiNetwork *wn) :
706 WifiNetworkStringProperty(wn, "Bssid", false, 1) {
707}
708int WifiNetwork::WifiNetworkBssidProperty::get(int idx, char *buffer, size_t max) {
709 strncpy(buffer,
710 mWn->getBssid() ? mWn->getBssid() : "none",
711 max);
712 return 0;
713}
714int WifiNetwork::WifiNetworkBssidProperty::set(int idx, const char *value) {
715 return mWn->setBssid(value);
716}
717
718WifiNetwork::WifiNetworkPskProperty::WifiNetworkPskProperty(WifiNetwork *wn) :
719 WifiNetworkStringProperty(wn, "Psk", false, 1) {
720}
721int WifiNetwork::WifiNetworkPskProperty::get(int idx, char *buffer, size_t max) {
722 strncpy(buffer,
723 mWn->getPsk() ? mWn->getPsk() : "none",
724 max);
725 return 0;
726}
727int WifiNetwork::WifiNetworkPskProperty::set(int idx, const char *value) {
728 return mWn->setPsk(value);
729}
730
731WifiNetwork::WifiNetworkWepKeyProperty::WifiNetworkWepKeyProperty(WifiNetwork *wn) :
732 WifiNetworkStringProperty(wn, "WepKey", false, 4) {
733}
734
735int WifiNetwork::WifiNetworkWepKeyProperty::get(int idx, char *buffer, size_t max) {
736 const char *key = mWn->getWepKey(idx);
737
738 strncpy(buffer, (key ? key : "none"), max);
739 return 0;
740}
741int WifiNetwork::WifiNetworkWepKeyProperty::set(int idx, const char *value) {
742 return mWn->setWepKey(idx, value);
743}
744
745WifiNetwork::WifiNetworkDefaultKeyIndexProperty::WifiNetworkDefaultKeyIndexProperty(WifiNetwork *wn) :
746 WifiNetworkIntegerProperty(wn, "DefaultKeyIndex", false, 1) {
747}
748int WifiNetwork::WifiNetworkDefaultKeyIndexProperty::get(int idx, int *buffer) {
749 *buffer = mWn->getDefaultKeyIndex();
750 return 0;
751}
752int WifiNetwork::WifiNetworkDefaultKeyIndexProperty::set(int idx, int value) {
753 return mWn->setDefaultKeyIndex(value);
754}
755
756WifiNetwork::WifiNetworkPriorityProperty::WifiNetworkPriorityProperty(WifiNetwork *wn) :
757 WifiNetworkIntegerProperty(wn, "Priority", false, 1) {
758}
759int WifiNetwork::WifiNetworkPriorityProperty::get(int idx, int *buffer) {
760 *buffer = mWn->getPriority();
761 return 0;
762}
763int WifiNetwork::WifiNetworkPriorityProperty::set(int idx, int value) {
764 return mWn->setPriority(value);
765}
766
767WifiNetwork::WifiNetworkKeyManagementProperty::WifiNetworkKeyManagementProperty(WifiNetwork *wn) :
768 WifiNetworkStringProperty(wn, "KeyManagement", false, 1) {
769}
770int WifiNetwork::WifiNetworkKeyManagementProperty::get(int idx, char *buffer, size_t max) {
771
772 if (mWn->getKeyManagement() == KeyManagementMask::NONE)
773 strncpy(buffer, "NONE", max);
774 else {
775 char tmp[80] = { '\0' };
776
777 if (mWn->getKeyManagement() & KeyManagementMask::WPA_PSK)
778 strcat(tmp, "WPA-PSK");
779 if (mWn->getKeyManagement() & KeyManagementMask::WPA_EAP) {
780 if (tmp[0] != '\0')
781 strcat(tmp, " ");
782 strcat(tmp, "WPA-EAP");
783 }
784 if (mWn->getKeyManagement() & KeyManagementMask::IEEE8021X) {
785 if (tmp[0] != '\0')
786 strcat(tmp, " ");
787 strcat(tmp, "IEEE8021X");
788 }
789 if (tmp[0] == '\0') {
790 strncpy(buffer, "(internal error)", max);
791 errno = ENOENT;
792 return -1;
793 }
794 if (tmp[strlen(tmp)] == ' ')
795 tmp[strlen(tmp)] = '\0';
796
797 strncpy(buffer, tmp, max);
San Mehat3c5a6f02009-05-22 15:36:13 -0700798 }
799 return 0;
San Mehat82a21162009-05-12 17:26:28 -0700800}
San Mehatc4a895b2009-06-23 21:10:57 -0700801int WifiNetwork::WifiNetworkKeyManagementProperty::set(int idx, const char *value) {
802 uint32_t mask;
803 if (mWn->parseKeyManagementMask(value, &mask))
804 return -1;
805 return mWn->setKeyManagement(mask);
806}
807
808WifiNetwork::WifiNetworkProtocolsProperty::WifiNetworkProtocolsProperty(WifiNetwork *wn) :
809 WifiNetworkStringProperty(wn, "Protocols", false, 1) {
810}
811int WifiNetwork::WifiNetworkProtocolsProperty::get(int idx, char *buffer, size_t max) {
812 char tmp[80] = { '\0' };
813
814 if (mWn->getProtocols() & SecurityProtocolMask::WPA)
815 strcat(tmp, "WPA");
816 if (mWn->getProtocols() & SecurityProtocolMask::RSN) {
817 if (tmp[0] != '\0')
818 strcat(tmp, " ");
819 strcat(tmp, "RSN");
820 }
821
822 if (tmp[0] == '\0') {
823 strncpy(buffer, "(internal error)", max);
824 errno = ENOENT;
825 return NULL;
826 }
827 if (tmp[strlen(tmp)] == ' ')
828 tmp[strlen(tmp)] = '\0';
829
830 strncpy(buffer, tmp, max);
831 return 0;
832}
833int WifiNetwork::WifiNetworkProtocolsProperty::set(int idx, const char *value) {
834 uint32_t mask;
835 if (mWn->parseProtocolsMask(value, &mask))
836 return -1;
837 return mWn->setProtocols(mask);
838}
839
840WifiNetwork::WifiNetworkAuthAlgorithmsProperty::WifiNetworkAuthAlgorithmsProperty(WifiNetwork *wn) :
841 WifiNetworkStringProperty(wn, "AuthAlgorithms", false, 1) {
842}
843int WifiNetwork::WifiNetworkAuthAlgorithmsProperty::get(int idx, char *buffer, size_t max) {
844 char tmp[80] = { '\0' };
845
846 if (mWn->getAuthAlgorithms() == 0) {
847 strncpy(buffer, "NONE", max);
848 return 0;
849 }
850
851 if (mWn->getAuthAlgorithms() & AuthenticationAlgorithmMask::OPEN)
852 strcat(tmp, "OPEN");
853 if (mWn->getAuthAlgorithms() & AuthenticationAlgorithmMask::SHARED) {
854 if (tmp[0] != '\0')
855 strcat(tmp, " ");
856 strcat(tmp, "SHARED");
857 }
858 if (mWn->getAuthAlgorithms() & AuthenticationAlgorithmMask::LEAP) {
859 if (tmp[0] != '\0')
860 strcat(tmp, " ");
861 strcat(tmp, "LEAP");
862 }
863
864 if (tmp[0] == '\0') {
865 strncpy(buffer, "(internal error)", max);
866 errno = ENOENT;
867 return NULL;
868 }
869 if (tmp[strlen(tmp)] == ' ')
870 tmp[strlen(tmp)] = '\0';
871
872 strncpy(buffer, tmp, max);
873 return 0;
874}
875int WifiNetwork::WifiNetworkAuthAlgorithmsProperty::set(int idx, const char *value) {
876 uint32_t mask;
877 if (mWn->parseAuthAlgorithmsMask(value, &mask))
878 return -1;
879 return mWn->setAuthAlgorithms(mask);
880}
881
882WifiNetwork::WifiNetworkPairwiseCiphersProperty::WifiNetworkPairwiseCiphersProperty(WifiNetwork *wn) :
883 WifiNetworkStringProperty(wn, "PairwiseCiphers", false, 1) {
884}
885int WifiNetwork::WifiNetworkPairwiseCiphersProperty::get(int idx, char *buffer, size_t max) {
886 if (mWn->getPairwiseCiphers() == PairwiseCiphersMask::NONE)
887 strncpy(buffer, "NONE", max);
888 else {
889 char tmp[80] = { '\0' };
890
891 if (mWn->getPairwiseCiphers() & PairwiseCiphersMask::TKIP)
892 strcat(tmp, "TKIP");
893 if (mWn->getPairwiseCiphers() & PairwiseCiphersMask::CCMP) {
894 if (tmp[0] != '\0')
895 strcat(tmp, " ");
896 strcat(tmp, "CCMP");
897 }
898 if (tmp[0] == '\0') {
899 strncpy(buffer, "(internal error)", max);
900 errno = ENOENT;
901 return NULL;
902 }
903 if (tmp[strlen(tmp)] == ' ')
904 tmp[strlen(tmp)] = '\0';
905
906 strncpy(buffer, tmp, max);
907 }
908 return 0;
909}
910int WifiNetwork::WifiNetworkPairwiseCiphersProperty::set(int idx, const char *value) {
911 uint32_t mask;
912 if (mWn->parsePairwiseCiphersMask(value, &mask))
913 return -1;
914 return mWn->setPairwiseCiphers(mask);
915}
916
917WifiNetwork::WifiNetworkGroupCiphersProperty::WifiNetworkGroupCiphersProperty(WifiNetwork *wn) :
918 WifiNetworkStringProperty(wn, "GroupCiphers", false, 1) {
919}
920int WifiNetwork::WifiNetworkGroupCiphersProperty::get(int idx, char *buffer, size_t max) {
921 char tmp[80] = { '\0' };
922
923 if (mWn->getGroupCiphers() & GroupCiphersMask::WEP40)
924 strcat(tmp, "WEP40");
925 if (mWn->getGroupCiphers() & GroupCiphersMask::WEP104) {
926 if (tmp[0] != '\0')
927 strcat(tmp, " ");
928 strcat(tmp, "WEP104");
929 }
930 if (mWn->getGroupCiphers() & GroupCiphersMask::TKIP) {
931 if (tmp[0] != '\0')
932 strcat(tmp, " ");
933 strcat(tmp, "TKIP");
934 }
935 if (mWn->getGroupCiphers() & GroupCiphersMask::CCMP) {
936 if (tmp[0] != '\0')
937 strcat(tmp, " ");
938 strcat(tmp, "CCMP");
939 }
940
941 if (tmp[0] == '\0') {
942 strncpy(buffer, "(internal error)", max);
943 errno = ENOENT;
944 return -1;
945 }
946 if (tmp[strlen(tmp)] == ' ')
947 tmp[strlen(tmp)] = '\0';
948
949 strncpy(buffer, tmp, max);
950 return 0;
951}
952int WifiNetwork::WifiNetworkGroupCiphersProperty::set(int idx, const char *value) {
953 uint32_t mask;
954 if (mWn->parseGroupCiphersMask(value, &mask))
955 return -1;
956 return mWn->setGroupCiphers(mask);
957}
958
959WifiNetwork::WifiNetworkHiddenSsidProperty::WifiNetworkHiddenSsidProperty(WifiNetwork *wn) :
960 WifiNetworkStringProperty(wn, "HiddenSsid", false, 1) {
961}
962int WifiNetwork::WifiNetworkHiddenSsidProperty::get(int idx, char *buffer, size_t max) {
963 const char *scan_ssid = mWn->getHiddenSsid();
964
965 strncpy(buffer, (scan_ssid ? scan_ssid : "none"), max);
966 return 0;
967}
968int WifiNetwork::WifiNetworkHiddenSsidProperty::set(int idx, const char *value) {
969 return mWn->setHiddenSsid(value);
970}