| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the plugins of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and Digia. For licensing terms and |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information |
| 15 | ** use the contact form at http://qt.digia.com/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 24 | ** |
| 25 | ** In addition, as a special exception, Digia gives you certain additional |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 28 | ** |
| 29 | ** GNU General Public License Usage |
| 30 | ** Alternatively, this file may be used under the terms of the GNU |
| 31 | ** General Public License version 3.0 as published by the Free Software |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
| 33 | ** packaging of this file. Please review the following information to |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
| 36 | ** |
| 37 | ** |
| 38 | ** $QT_END_LICENSE$ |
| 39 | ** |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | #include <SystemConfiguration/SCNetworkConfiguration.h> |
| 43 | |
| 44 | @interface QT_MANGLE_NAMESPACE(QNSListener) : NSObject |
| 45 | { |
| 46 | NSNotificationCenter *notificationCenter; |
| 47 | CWInterface *currentInterface; |
| 48 | QCoreWlanEngine *engine; |
| 49 | NSLock *locker; |
| 50 | } |
| 51 | - (void)notificationHandler;//:(NSNotification *)notification; |
| 52 | - (void)remove; |
| 53 | - (void)setEngine:(QCoreWlanEngine *)coreEngine; |
| 54 | - (QCoreWlanEngine *)engine; |
| 55 | - (void)dealloc; |
| 56 | |
| 57 | @property (assign) QCoreWlanEngine* engine; |
| 58 | |
| 59 | @end |
| 60 | |
| 61 | @implementation QT_MANGLE_NAMESPACE(QNSListener) |
| 62 | |
| 63 | - (id) init |
| 64 | { |
| 65 | [locker lock]; |
| 66 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 67 | notificationCenter = [NSNotificationCenter defaultCenter]; |
| 68 | currentInterface = [CWInterface interfaceWithName:nil]; |
| 69 | [notificationCenter addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil]; |
| 70 | [locker unlock]; |
| 71 | [autoreleasepool release]; |
| 72 | return self; |
| 73 | } |
| 74 | |
| 75 | -(void)dealloc |
| 76 | { |
| 77 | [super dealloc]; |
| 78 | } |
| 79 | |
| 80 | -(void)setEngine:(QCoreWlanEngine *)coreEngine |
| 81 | { |
| 82 | [locker lock]; |
| 83 | if(!engine) |
| 84 | engine = coreEngine; |
| 85 | [locker unlock]; |
| 86 | } |
| 87 | |
| 88 | -(QCoreWlanEngine *)engine |
| 89 | { |
| 90 | return engine; |
| 91 | } |
| 92 | |
| 93 | -(void)remove |
| 94 | { |
| 95 | [locker lock]; |
| 96 | [notificationCenter removeObserver:self]; |
| 97 | [locker unlock]; |
| 98 | } |
| 99 | |
| 100 | - (void)notificationHandler//:(NSNotification *)notification |
| 101 | { |
| 102 | engine->requestUpdate(); |
| 103 | } |
| 104 | @end |
| 105 | |
| 106 | static QT_MANGLE_NAMESPACE(QNSListener) *listener = 0; |
| 107 | |
| 108 | QT_BEGIN_NAMESPACE |
| 109 | |
| 110 | void networkChangeCallback(SCDynamicStoreRef/* store*/, CFArrayRef changedKeys, void *info) |
| 111 | { |
| 112 | for ( long i = 0; i < CFArrayGetCount(changedKeys); i++) { |
| 113 | |
| 114 | QString changed = QCFString::toQString(CFStringRef((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i))); |
| 115 | if( changed.contains("/Network/Global/IPv4")) { |
| 116 | QCoreWlanEngine* wlanEngine = static_cast<QCoreWlanEngine*>(info); |
| 117 | wlanEngine->requestUpdate(); |
| 118 | } |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | QScanThread::QScanThread(QObject *parent) |
| 125 | :QThread(parent) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | QScanThread::~QScanThread() |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | void QScanThread::quit() |
| 134 | { |
| 135 | wait(); |
| 136 | } |
| 137 | |
| 138 | void QScanThread::run() |
| 139 | { |
| 140 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 141 | QStringList found; |
| 142 | mutex.lock(); |
| 143 | CWInterface *currentInterface = [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceName)]; |
| 144 | mutex.unlock(); |
| 145 | |
| 146 | if([currentInterface power]) { |
| 147 | NSError *err = nil; |
| 148 | NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 149 | [NSNumber numberWithBool:YES], kCWScanKeyMerge, |
| 150 | [NSNumber numberWithInt:kCWScanTypeFast], kCWScanKeyScanType, |
| 151 | [NSNumber numberWithInteger:100], kCWScanKeyRestTime, nil]; |
| 152 | |
| 153 | NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err]; |
| 154 | CWNetwork *apNetwork; |
| 155 | |
| 156 | if (!err) { |
| 157 | |
| 158 | for(uint row=0; row < [apArray count]; row++ ) { |
| 159 | apNetwork = [apArray objectAtIndex:row]; |
| 160 | |
| 161 | const QString networkSsid = QCFString::toQString(CFStringRef([apNetwork ssid])); |
| 162 | const QString id = QString::number(qHash(QLatin1String("corewlan:") + networkSsid)); |
| 163 | found.append(id); |
| 164 | |
| 165 | QNetworkConfiguration::StateFlags state = QNetworkConfiguration::Undefined; |
| 166 | bool known = isKnownSsid(networkSsid); |
| 167 | if( [currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { |
| 168 | if( networkSsid == QCFString::toQString(CFStringRef([currentInterface ssid]))) { |
| 169 | state = QNetworkConfiguration::Active; |
| 170 | } |
| 171 | } |
| 172 | if(state == QNetworkConfiguration::Undefined) { |
| 173 | if(known) { |
| 174 | state = QNetworkConfiguration::Discovered; |
| 175 | } else { |
| 176 | state = QNetworkConfiguration::Undefined; |
| 177 | } |
| 178 | } |
| 179 | QNetworkConfiguration::Purpose purpose = QNetworkConfiguration::UnknownPurpose; |
| 180 | if([[apNetwork securityMode] intValue] == kCWSecurityModeOpen) { |
| 181 | purpose = QNetworkConfiguration::PublicPurpose; |
| 182 | } else { |
| 183 | purpose = QNetworkConfiguration::PrivatePurpose; |
| 184 | } |
| 185 | |
| 186 | found.append(foundNetwork(id, networkSsid, state, interfaceName, purpose)); |
| 187 | |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | // add known configurations that are not around. |
| 192 | QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
| 193 | while (i.hasNext()) { |
| 194 | i.next(); |
| 195 | |
| 196 | QString networkName = i.key(); |
| 197 | const QString id = QString::number(qHash(QLatin1String("corewlan:") + networkName)); |
| 198 | |
| 199 | if(!found.contains(id)) { |
| 200 | QString networkSsid = getSsidFromNetworkName(networkName); |
| 201 | const QString ssidId = QString::number(qHash(QLatin1String("corewlan:") + networkSsid)); |
| 202 | QNetworkConfiguration::StateFlags state = QNetworkConfiguration::Undefined; |
| 203 | QString interfaceName; |
| 204 | QMapIterator<QString, QString> ij(i.value()); |
| 205 | while (ij.hasNext()) { |
| 206 | ij.next(); |
| 207 | interfaceName = ij.value(); |
| 208 | } |
| 209 | |
| 210 | if( [currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { |
| 211 | if( networkSsid == QCFString::toQString(CFStringRef([currentInterface ssid]))) { |
| 212 | state = QNetworkConfiguration::Active; |
| 213 | } |
| 214 | } |
| 215 | if(state == QNetworkConfiguration::Undefined) { |
| 216 | if( userProfiles.contains(networkName) |
| 217 | && found.contains(ssidId)) { |
| 218 | state = QNetworkConfiguration::Discovered; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if(state == QNetworkConfiguration::Undefined) { |
| 223 | state = QNetworkConfiguration::Defined; |
| 224 | } |
| 225 | |
| 226 | found.append(foundNetwork(id, networkName, state, interfaceName, QNetworkConfiguration::UnknownPurpose)); |
| 227 | } |
| 228 | } |
| 229 | emit networksChanged(); |
| 230 | [autoreleasepool release]; |
| 231 | } |
| 232 | |
| 233 | QStringList QScanThread::foundNetwork(const QString &id, const QString &name, const QNetworkConfiguration::StateFlags state, const QString &interfaceName, const QNetworkConfiguration::Purpose purpose) |
| 234 | { |
| 235 | QStringList found; |
| 236 | QMutexLocker locker(&mutex); |
| 237 | QNetworkConfigurationPrivate *ptr = new QNetworkConfigurationPrivate; |
| 238 | |
| 239 | ptr->name = name; |
| 240 | ptr->isValid = true; |
| 241 | ptr->id = id; |
| 242 | ptr->state = state; |
| 243 | ptr->type = QNetworkConfiguration::InternetAccessPoint; |
| 244 | ptr->bearerType = QNetworkConfiguration::BearerWLAN; |
| 245 | ptr->purpose = purpose; |
| 246 | |
| 247 | fetchedConfigurations.append( ptr); |
| 248 | configurationInterface.insert(ptr->id, interfaceName); |
| 249 | |
| 250 | locker.unlock(); |
| 251 | locker.relock(); |
| 252 | found.append(id); |
| 253 | return found; |
| 254 | } |
| 255 | |
| 256 | QList<QNetworkConfigurationPrivate *> QScanThread::getConfigurations() |
| 257 | { |
| 258 | QMutexLocker locker(&mutex); |
| 259 | |
| 260 | QList<QNetworkConfigurationPrivate *> foundConfigurations = fetchedConfigurations; |
| 261 | fetchedConfigurations.clear(); |
| 262 | |
| 263 | return foundConfigurations; |
| 264 | } |
| 265 | |
| 266 | void QScanThread::getUserConfigurations() |
| 267 | { |
| 268 | QMutexLocker locker(&mutex); |
| 269 | |
| 270 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 271 | userProfiles.clear(); |
| 272 | |
| 273 | NSArray *wifiInterfaces = [CWInterface supportedInterfaces]; |
| 274 | for(uint row=0; row < [wifiInterfaces count]; row++ ) { |
| 275 | |
| 276 | CWInterface *wifiInterface = [CWInterface interfaceWithName: [wifiInterfaces objectAtIndex:row]]; |
| 277 | if ( ![wifiInterface power] ) |
| 278 | continue; |
| 279 | |
| 280 | NSString *nsInterfaceName = [wifiInterface name]; |
| 281 | // add user configured system networks |
| 282 | SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Qt corewlan", nil, nil); |
| 283 | NSDictionary * airportPlist = (NSDictionary *)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName]); |
| 284 | CFRelease(dynRef); |
| 285 | if(airportPlist != nil) { |
| 286 | NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"]; |
| 287 | |
| 288 | NSArray *thisSsidarray = [prefNetDict valueForKey:@"SSID_STR"]; |
| 289 | for(NSString *ssidkey in thisSsidarray) { |
| 290 | QString thisSsid = QCFString::toQString(CFStringRef(ssidkey)); |
| 291 | if(!userProfiles.contains(thisSsid)) { |
| 292 | QMap <QString,QString> map; |
| 293 | map.insert(thisSsid, QCFString::toQString(CFStringRef(nsInterfaceName))); |
| 294 | userProfiles.insert(thisSsid, map); |
| 295 | } |
| 296 | } |
| 297 | CFRelease(airportPlist); |
| 298 | } |
| 299 | |
| 300 | // 802.1X user profiles |
| 301 | QString userProfilePath = QDir::homePath() + "/Library/Preferences/com.apple.eap.profiles.plist"; |
| 302 | NSDictionary* eapDict = [[[NSDictionary alloc] initWithContentsOfFile: (NSString *)QCFString::toCFStringRef(userProfilePath)] autorelease]; |
| 303 | if(eapDict != nil) { |
| 304 | NSString *profileStr= @"Profiles"; |
| 305 | NSString *nameStr = @"UserDefinedName"; |
| 306 | NSString *networkSsidStr = @"Wireless Network"; |
| 307 | for (id profileKey in eapDict) { |
| 308 | if ([profileStr isEqualToString:profileKey]) { |
| 309 | NSDictionary *itemDict = [eapDict objectForKey:profileKey]; |
| 310 | for (id itemKey in itemDict) { |
| 311 | |
| 312 | NSInteger dictSize = [itemKey count]; |
| 313 | id objects[dictSize]; |
| 314 | id keys[dictSize]; |
| 315 | |
| 316 | [itemKey getObjects:objects andKeys:keys]; |
| 317 | QString networkName; |
| 318 | QString ssid; |
| 319 | for(int i = 0; i < dictSize; i++) { |
| 320 | if([nameStr isEqualToString:keys[i]]) { |
| 321 | networkName = QCFString::toQString(CFStringRef(objects[i])); |
| 322 | } |
| 323 | if([networkSsidStr isEqualToString:keys[i]]) { |
| 324 | ssid = QCFString::toQString(CFStringRef(objects[i])); |
| 325 | } |
| 326 | if(!userProfiles.contains(networkName) |
| 327 | && !ssid.isEmpty()) { |
| 328 | QMap<QString,QString> map; |
| 329 | map.insert(ssid, QCFString::toQString(CFStringRef(nsInterfaceName))); |
| 330 | userProfiles.insert(networkName, map); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | [autoreleasepool release]; |
| 339 | } |
| 340 | |
| 341 | QString QScanThread::getSsidFromNetworkName(const QString &name) |
| 342 | { |
| 343 | QMutexLocker locker(&mutex); |
| 344 | |
| 345 | QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
| 346 | while (i.hasNext()) { |
| 347 | i.next(); |
| 348 | QMap<QString,QString> map = i.value(); |
| 349 | QMapIterator<QString, QString> ij(i.value()); |
| 350 | while (ij.hasNext()) { |
| 351 | ij.next(); |
| 352 | const QString networkNameHash = QString::number(qHash(QLatin1String("corewlan:") +i.key())); |
| 353 | if(name == i.key() || name == networkNameHash) { |
| 354 | return ij.key(); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | return QString(); |
| 359 | } |
| 360 | |
| 361 | QString QScanThread::getNetworkNameFromSsid(const QString &ssid) |
| 362 | { |
| 363 | QMutexLocker locker(&mutex); |
| 364 | |
| 365 | QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
| 366 | while (i.hasNext()) { |
| 367 | i.next(); |
| 368 | QMap<QString,QString> map = i.value(); |
| 369 | QMapIterator<QString, QString> ij(i.value()); |
| 370 | while (ij.hasNext()) { |
| 371 | ij.next(); |
| 372 | if(ij.key() == ssid) { |
| 373 | return i.key(); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | return QString(); |
| 378 | } |
| 379 | |
| 380 | bool QScanThread::isKnownSsid(const QString &ssid) |
| 381 | { |
| 382 | QMutexLocker locker(&mutex); |
| 383 | |
| 384 | QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
| 385 | while (i.hasNext()) { |
| 386 | i.next(); |
| 387 | QMap<QString,QString> map = i.value(); |
| 388 | if(map.keys().contains(ssid)) { |
| 389 | return true; |
| 390 | } |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | QCoreWlanEngine::QCoreWlanEngine(QObject *parent) |
| 397 | : QBearerEngineImpl(parent), scanThread(0) |
| 398 | { |
| 399 | scanThread = new QScanThread(this); |
| 400 | connect(scanThread, SIGNAL(networksChanged()), |
| 401 | this, SLOT(networksChanged())); |
| 402 | } |
| 403 | |
| 404 | QCoreWlanEngine::~QCoreWlanEngine() |
| 405 | { |
| 406 | while (!foundConfigurations.isEmpty()) |
| 407 | delete foundConfigurations.takeFirst(); |
| 408 | [listener remove]; |
| 409 | [listener release]; |
| 410 | } |
| 411 | |
| 412 | void QCoreWlanEngine::initialize() |
| 413 | { |
| 414 | QMutexLocker locker(&mutex); |
| 415 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 416 | |
| 417 | if([[CWInterface supportedInterfaces] count] > 0 && !listener) { |
| 418 | listener = [[QT_MANGLE_NAMESPACE(QNSListener) alloc] init]; |
| 419 | listener.engine = this; |
| 420 | hasWifi = true; |
| 421 | } else { |
| 422 | hasWifi = false; |
| 423 | } |
| 424 | storeSession = NULL; |
| 425 | |
| 426 | startNetworkChangeLoop(); |
| 427 | [autoreleasepool release]; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | QString QCoreWlanEngine::getInterfaceFromId(const QString &id) |
| 432 | { |
| 433 | QMutexLocker locker(&mutex); |
| 434 | |
| 435 | return scanThread->configurationInterface.value(id); |
| 436 | } |
| 437 | |
| 438 | bool QCoreWlanEngine::hasIdentifier(const QString &id) |
| 439 | { |
| 440 | QMutexLocker locker(&mutex); |
| 441 | |
| 442 | return scanThread->configurationInterface.contains(id); |
| 443 | } |
| 444 | |
| 445 | void QCoreWlanEngine::connectToId(const QString &id) |
| 446 | { |
| 447 | QMutexLocker locker(&mutex); |
| 448 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 449 | QString interfaceString = getInterfaceFromId(id); |
| 450 | |
| 451 | CWInterface *wifiInterface = |
| 452 | [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceString)]; |
| 453 | |
| 454 | if ([wifiInterface power]) { |
| 455 | NSError *err = nil; |
| 456 | NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0]; |
| 457 | |
| 458 | QString wantedSsid; |
| 459 | |
| 460 | QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id); |
| 461 | |
| 462 | const QString idHash = QString::number(qHash(QLatin1String("corewlan:") + ptr->name)); |
| 463 | const QString idHash2 = QString::number(qHash(QLatin1String("corewlan:") + scanThread->getNetworkNameFromSsid(ptr->name))); |
| 464 | |
| 465 | bool using8021X = false; |
| 466 | if (idHash2 != id) { |
| 467 | NSArray *array = [CW8021XProfile allUser8021XProfiles]; |
| 468 | |
| 469 | for (NSUInteger i = 0; i < [array count]; ++i) { |
| 470 | const QString networkNameHashCheck = QString::number(qHash(QLatin1String("corewlan:") + QCFString::toQString(CFStringRef([[array objectAtIndex:i] userDefinedName])))); |
| 471 | |
| 472 | const QString ssidHash = QString::number(qHash(QLatin1String("corewlan:") + QCFString::toQString(CFStringRef([[array objectAtIndex:i] ssid])))); |
| 473 | |
| 474 | if (id == networkNameHashCheck || id == ssidHash) { |
| 475 | const QString thisName = scanThread->getSsidFromNetworkName(id); |
| 476 | if (thisName.isEmpty()) |
| 477 | wantedSsid = id; |
| 478 | else |
| 479 | wantedSsid = thisName; |
| 480 | |
| 481 | [params setValue: [array objectAtIndex:i] forKey:kCWAssocKey8021XProfile]; |
| 482 | using8021X = true; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (!using8021X) { |
| 489 | QString wantedNetwork; |
| 490 | QMapIterator<QString, QMap<QString,QString> > i(scanThread->userProfiles); |
| 491 | while (i.hasNext()) { |
| 492 | i.next(); |
| 493 | wantedNetwork = i.key(); |
| 494 | const QString networkNameHash = QString::number(qHash(QLatin1String("corewlan:") + wantedNetwork)); |
| 495 | if (id == networkNameHash) { |
| 496 | wantedSsid = scanThread->getSsidFromNetworkName(wantedNetwork); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | NSDictionary *scanParameters = [NSDictionary dictionaryWithObjectsAndKeys: |
| 502 | [NSNumber numberWithBool:YES], kCWScanKeyMerge, |
| 503 | [NSNumber numberWithInt:kCWScanTypeFast], kCWScanKeyScanType, |
| 504 | [NSNumber numberWithInteger:100], kCWScanKeyRestTime, |
| 505 | (NSString *)QCFString::toCFStringRef(wantedSsid), kCWScanKeySSID, |
| 506 | nil]; |
| 507 | |
| 508 | NSArray *scanArray = [wifiInterface scanForNetworksWithParameters:scanParameters error:&err]; |
| 509 | |
| 510 | if(!err) { |
| 511 | for(uint row=0; row < [scanArray count]; row++ ) { |
| 512 | CWNetwork *apNetwork = [scanArray objectAtIndex:row]; |
| 513 | |
| 514 | if(wantedSsid == QCFString::toQString(CFStringRef([apNetwork ssid]))) { |
| 515 | |
| 516 | if(!using8021X) { |
| 517 | SecKeychainAttribute attributes[3]; |
| 518 | |
| 519 | NSString *account = [apNetwork ssid]; |
| 520 | NSString *keyKind = @"AirPort network password"; |
| 521 | NSString *keyName = account; |
| 522 | |
| 523 | attributes[0].tag = kSecAccountItemAttr; |
| 524 | attributes[0].data = (void *)[account UTF8String]; |
| 525 | attributes[0].length = [account length]; |
| 526 | |
| 527 | attributes[1].tag = kSecDescriptionItemAttr; |
| 528 | attributes[1].data = (void *)[keyKind UTF8String]; |
| 529 | attributes[1].length = [keyKind length]; |
| 530 | |
| 531 | attributes[2].tag = kSecLabelItemAttr; |
| 532 | attributes[2].data = (void *)[keyName UTF8String]; |
| 533 | attributes[2].length = [keyName length]; |
| 534 | |
| 535 | SecKeychainAttributeList attributeList = {3,attributes}; |
| 536 | |
| 537 | SecKeychainSearchRef searchRef; |
| 538 | SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &attributeList, &searchRef); |
| 539 | |
| 540 | NSString *password = @""; |
| 541 | SecKeychainItemRef searchItem; |
| 542 | |
| 543 | if (SecKeychainSearchCopyNext(searchRef, &searchItem) == noErr) { |
| 544 | UInt32 realPasswordLength; |
| 545 | SecKeychainAttribute attributesW[8]; |
| 546 | attributesW[0].tag = kSecAccountItemAttr; |
| 547 | SecKeychainAttributeList listW = {1,attributesW}; |
| 548 | char *realPassword; |
| 549 | OSStatus status = SecKeychainItemCopyContent(searchItem, NULL, &listW, &realPasswordLength,(void **)&realPassword); |
| 550 | |
| 551 | if (status == noErr) { |
| 552 | if (realPassword != NULL) { |
| 553 | |
| 554 | QByteArray pBuf; |
| 555 | pBuf.resize(realPasswordLength); |
| 556 | pBuf.prepend(realPassword); |
| 557 | pBuf.insert(realPasswordLength,'\0'); |
| 558 | |
| 559 | password = [NSString stringWithUTF8String:pBuf]; |
| 560 | } |
| 561 | SecKeychainItemFreeContent(&listW, realPassword); |
| 562 | } |
| 563 | |
| 564 | CFRelease(searchItem); |
| 565 | } else { |
| 566 | qDebug() << "SecKeychainSearchCopyNext error"; |
| 567 | } |
| 568 | [params setValue: password forKey: kCWAssocKeyPassphrase]; |
| 569 | } // end using8021X |
| 570 | |
| 571 | |
| 572 | bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err]; |
| 573 | |
| 574 | if(!err) { |
| 575 | if(!result) { |
| 576 | emit connectionError(id, ConnectError); |
| 577 | } else { |
| 578 | return; |
| 579 | } |
| 580 | } else { |
| 581 | qDebug() <<"associate ERROR"<< QCFString::toQString(CFStringRef([err localizedDescription ])); |
| 582 | } |
| 583 | } |
| 584 | } //end scan network |
| 585 | } else { |
| 586 | qDebug() <<"scan ERROR"<< QCFString::toQString(CFStringRef([err localizedDescription ])); |
| 587 | } |
| 588 | emit connectionError(id, InterfaceLookupError); |
| 589 | } |
| 590 | |
| 591 | locker.unlock(); |
| 592 | emit connectionError(id, InterfaceLookupError); |
| 593 | [autoreleasepool release]; |
| 594 | } |
| 595 | |
| 596 | void QCoreWlanEngine::disconnectFromId(const QString &id) |
| 597 | { |
| 598 | QMutexLocker locker(&mutex); |
| 599 | |
| 600 | QString interfaceString = getInterfaceFromId(id); |
| 601 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 602 | |
| 603 | CWInterface *wifiInterface = |
| 604 | [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceString)]; |
| 605 | |
| 606 | [wifiInterface disassociate]; |
| 607 | if ([[wifiInterface interfaceState]intValue] != kCWInterfaceStateInactive) { |
| 608 | locker.unlock(); |
| 609 | emit connectionError(id, DisconnectionError); |
| 610 | locker.relock(); |
| 611 | } |
| 612 | [autoreleasepool release]; |
| 613 | } |
| 614 | |
| 615 | void QCoreWlanEngine::requestUpdate() |
| 616 | { |
| 617 | scanThread->getUserConfigurations(); |
| 618 | doRequestUpdate(); |
| 619 | } |
| 620 | |
| 621 | void QCoreWlanEngine::doRequestUpdate() |
| 622 | { |
| 623 | QMutexLocker locker(&mutex); |
| 624 | |
| 625 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 626 | |
| 627 | NSArray *wifiInterfaces = [CWInterface supportedInterfaces]; |
| 628 | for (uint row = 0; row < [wifiInterfaces count]; ++row) { |
| 629 | scanThread->interfaceName = QCFString::toQString(CFStringRef([wifiInterfaces objectAtIndex:row])); |
| 630 | scanThread->start(); |
| 631 | } |
| 632 | locker.unlock(); |
| 633 | [autoreleasepool release]; |
| 634 | } |
| 635 | |
| 636 | bool QCoreWlanEngine::isWifiReady(const QString &wifiDeviceName) |
| 637 | { |
| 638 | QMutexLocker locker(&mutex); |
| 639 | bool haswifi = false; |
| 640 | if(hasWifi) { |
| 641 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 642 | CWInterface *defaultInterface = [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(wifiDeviceName)]; |
| 643 | if([defaultInterface power]) { |
| 644 | haswifi = true; |
| 645 | } |
| 646 | [autoreleasepool release]; |
| 647 | } |
| 648 | return haswifi; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | QNetworkSession::State QCoreWlanEngine::sessionStateForId(const QString &id) |
| 653 | { |
| 654 | QMutexLocker locker(&mutex); |
| 655 | QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id); |
| 656 | |
| 657 | if (!ptr) |
| 658 | return QNetworkSession::Invalid; |
| 659 | |
| 660 | if (!ptr->isValid) { |
| 661 | return QNetworkSession::Invalid; |
| 662 | } else if ((ptr->state & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) { |
| 663 | return QNetworkSession::Connected; |
| 664 | } else if ((ptr->state & QNetworkConfiguration::Discovered) == |
| 665 | QNetworkConfiguration::Discovered) { |
| 666 | return QNetworkSession::Disconnected; |
| 667 | } else if ((ptr->state & QNetworkConfiguration::Defined) == QNetworkConfiguration::Defined) { |
| 668 | return QNetworkSession::NotAvailable; |
| 669 | } else if ((ptr->state & QNetworkConfiguration::Undefined) == |
| 670 | QNetworkConfiguration::Undefined) { |
| 671 | return QNetworkSession::NotAvailable; |
| 672 | } |
| 673 | |
| 674 | return QNetworkSession::Invalid; |
| 675 | } |
| 676 | |
| 677 | QNetworkConfigurationManager::Capabilities QCoreWlanEngine::capabilities() const |
| 678 | { |
| 679 | return QNetworkConfigurationManager::ForcedRoaming; |
| 680 | } |
| 681 | |
| 682 | void QCoreWlanEngine::startNetworkChangeLoop() |
| 683 | { |
| 684 | |
| 685 | SCDynamicStoreContext dynStoreContext = { 0, this/*(void *)storeSession*/, NULL, NULL, NULL }; |
| 686 | storeSession = SCDynamicStoreCreate(NULL, |
| 687 | CFSTR("networkChangeCallback"), |
| 688 | networkChangeCallback, |
| 689 | &dynStoreContext); |
| 690 | if (!storeSession ) { |
| 691 | qWarning() << "could not open dynamic store: error:" << SCErrorString(SCError()); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | CFMutableArrayRef notificationKeys; |
| 696 | notificationKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 697 | CFMutableArrayRef patternsArray; |
| 698 | patternsArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 699 | |
| 700 | CFStringRef storeKey; |
| 701 | storeKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, |
| 702 | kSCDynamicStoreDomainState, |
| 703 | kSCEntNetIPv4); |
| 704 | CFArrayAppendValue(notificationKeys, storeKey); |
| 705 | CFRelease(storeKey); |
| 706 | |
| 707 | storeKey = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, |
| 708 | kSCDynamicStoreDomainState, |
| 709 | kSCCompAnyRegex, |
| 710 | kSCEntNetIPv4); |
| 711 | CFArrayAppendValue(patternsArray, storeKey); |
| 712 | CFRelease(storeKey); |
| 713 | |
| 714 | if (!SCDynamicStoreSetNotificationKeys(storeSession , notificationKeys, patternsArray)) { |
| 715 | qWarning() << "register notification error:"<< SCErrorString(SCError()); |
| 716 | CFRelease(storeSession ); |
| 717 | CFRelease(notificationKeys); |
| 718 | CFRelease(patternsArray); |
| 719 | return; |
| 720 | } |
| 721 | CFRelease(notificationKeys); |
| 722 | CFRelease(patternsArray); |
| 723 | |
| 724 | runloopSource = SCDynamicStoreCreateRunLoopSource(NULL, storeSession , 0); |
| 725 | if (!runloopSource) { |
| 726 | qWarning() << "runloop source error:"<< SCErrorString(SCError()); |
| 727 | CFRelease(storeSession ); |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | CFRunLoopAddSource(CFRunLoopGetCurrent(), runloopSource, kCFRunLoopDefaultMode); |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | QNetworkSessionPrivate *QCoreWlanEngine::createSessionBackend() |
| 736 | { |
| 737 | return new QNetworkSessionPrivateImpl; |
| 738 | } |
| 739 | |
| 740 | QNetworkConfigurationPrivatePointer QCoreWlanEngine::defaultConfiguration() |
| 741 | { |
| 742 | return QNetworkConfigurationPrivatePointer(); |
| 743 | } |
| 744 | |
| 745 | bool QCoreWlanEngine::requiresPolling() const |
| 746 | { |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | void QCoreWlanEngine::networksChanged() |
| 751 | { |
| 752 | QMutexLocker locker(&mutex); |
| 753 | |
| 754 | QStringList previous = accessPointConfigurations.keys(); |
| 755 | |
| 756 | QList<QNetworkConfigurationPrivate *> foundConfigurations = scanThread->getConfigurations(); |
| 757 | while (!foundConfigurations.isEmpty()) { |
| 758 | QNetworkConfigurationPrivate *cpPriv = foundConfigurations.takeFirst(); |
| 759 | |
| 760 | previous.removeAll(cpPriv->id); |
| 761 | |
| 762 | if (accessPointConfigurations.contains(cpPriv->id)) { |
| 763 | QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(cpPriv->id); |
| 764 | |
| 765 | bool changed = false; |
| 766 | |
| 767 | ptr->mutex.lock(); |
| 768 | |
| 769 | if (ptr->isValid != cpPriv->isValid) { |
| 770 | ptr->isValid = cpPriv->isValid; |
| 771 | changed = true; |
| 772 | } |
| 773 | |
| 774 | if (ptr->name != cpPriv->name) { |
| 775 | ptr->name = cpPriv->name; |
| 776 | changed = true; |
| 777 | } |
| 778 | |
| 779 | if (ptr->bearerType != cpPriv->bearerType) { |
| 780 | ptr->bearerType = cpPriv->bearerType; |
| 781 | changed = true; |
| 782 | } |
| 783 | |
| 784 | if (ptr->state != cpPriv->state) { |
| 785 | ptr->state = cpPriv->state; |
| 786 | changed = true; |
| 787 | } |
| 788 | |
| 789 | ptr->mutex.unlock(); |
| 790 | |
| 791 | if (changed) { |
| 792 | locker.unlock(); |
| 793 | emit configurationChanged(ptr); |
| 794 | locker.relock(); |
| 795 | } |
| 796 | |
| 797 | delete cpPriv; |
| 798 | } else { |
| 799 | QNetworkConfigurationPrivatePointer ptr(cpPriv); |
| 800 | |
| 801 | accessPointConfigurations.insert(ptr->id, ptr); |
| 802 | |
| 803 | locker.unlock(); |
| 804 | emit configurationAdded(ptr); |
| 805 | locker.relock(); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | while (!previous.isEmpty()) { |
| 810 | QNetworkConfigurationPrivatePointer ptr = |
| 811 | accessPointConfigurations.take(previous.takeFirst()); |
| 812 | |
| 813 | locker.unlock(); |
| 814 | emit configurationRemoved(ptr); |
| 815 | locker.relock(); |
| 816 | } |
| 817 | |
| 818 | locker.unlock(); |
| 819 | emit updateCompleted(); |
| 820 | |
| 821 | } |
| 822 | |
| 823 | quint64 QCoreWlanEngine::bytesWritten(const QString &id) |
| 824 | { |
| 825 | QMutexLocker locker(&mutex); |
| 826 | const QString interfaceStr = getInterfaceFromId(id); |
| 827 | return getBytes(interfaceStr,false); |
| 828 | } |
| 829 | |
| 830 | quint64 QCoreWlanEngine::bytesReceived(const QString &id) |
| 831 | { |
| 832 | QMutexLocker locker(&mutex); |
| 833 | const QString interfaceStr = getInterfaceFromId(id); |
| 834 | return getBytes(interfaceStr,true); |
| 835 | } |
| 836 | |
| 837 | quint64 QCoreWlanEngine::startTime(const QString &identifier) |
| 838 | { |
| 839 | QMutexLocker locker(&mutex); |
| 840 | NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 841 | quint64 timestamp = 0; |
| 842 | |
| 843 | NSString *filePath = @"/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist"; |
| 844 | NSDictionary* plistDict = [[[NSDictionary alloc] initWithContentsOfFile:filePath] autorelease]; |
| 845 | if(plistDict == nil) |
| 846 | return timestamp; |
| 847 | NSString *input = @"KnownNetworks"; |
| 848 | NSString *timeStampStr = @"_timeStamp"; |
| 849 | |
| 850 | NSString *ssidStr = @"SSID_STR"; |
| 851 | |
| 852 | for (id key in plistDict) { |
| 853 | if ([input isEqualToString:key]) { |
| 854 | |
| 855 | NSDictionary *knownNetworksDict = [plistDict objectForKey:key]; |
| 856 | if(knownNetworksDict == nil) |
| 857 | return timestamp; |
| 858 | for (id networkKey in knownNetworksDict) { |
| 859 | bool isFound = false; |
| 860 | NSDictionary *itemDict = [knownNetworksDict objectForKey:networkKey]; |
| 861 | if(itemDict == nil) |
| 862 | return timestamp; |
| 863 | NSInteger dictSize = [itemDict count]; |
| 864 | id objects[dictSize]; |
| 865 | id keys[dictSize]; |
| 866 | |
| 867 | [itemDict getObjects:objects andKeys:keys]; |
| 868 | bool ok = false; |
| 869 | for(int i = 0; i < dictSize; i++) { |
| 870 | if([ssidStr isEqualToString:keys[i]]) { |
| 871 | const QString ident = QString::number(qHash(QLatin1String("corewlan:") + QCFString::toQString(CFStringRef(objects[i])))); |
| 872 | if(ident == identifier) { |
| 873 | ok = true; |
| 874 | } |
| 875 | } |
| 876 | if(ok && [timeStampStr isEqualToString:keys[i]]) { |
| 877 | timestamp = (quint64)[objects[i] timeIntervalSince1970]; |
| 878 | isFound = true; |
| 879 | break; |
| 880 | } |
| 881 | } |
| 882 | if(isFound) |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | [autoreleasepool release]; |
| 888 | return timestamp; |
| 889 | } |
| 890 | |
| 891 | quint64 QCoreWlanEngine::getBytes(const QString &interfaceName, bool b) |
| 892 | { |
| 893 | struct ifaddrs *ifAddressList, *ifAddress; |
| 894 | struct if_data *if_data; |
| 895 | |
| 896 | quint64 bytes = 0; |
| 897 | ifAddressList = nil; |
| 898 | if(getifaddrs(&ifAddressList) == 0) { |
| 899 | for(ifAddress = ifAddressList; ifAddress; ifAddress = ifAddress->ifa_next) { |
| 900 | if(interfaceName == ifAddress->ifa_name) { |
| 901 | if_data = (struct if_data*)ifAddress->ifa_data; |
| 902 | if(b) { |
| 903 | bytes = if_data->ifi_ibytes; |
| 904 | break; |
| 905 | } else { |
| 906 | bytes = if_data->ifi_obytes; |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | freeifaddrs(ifAddressList); |
| 912 | } |
| 913 | return bytes; |
| 914 | } |
| 915 | |
| 916 | QT_END_NAMESPACE |