qtmobility/src/bearer/qcorewlanengine_mac.mm
branchRCL_3
changeset 3 87be51aa5b5b
parent 2 5822d84012fb
child 10 4ea83c148e84
equal deleted inserted replaced
2:5822d84012fb 3:87be51aa5b5b
    79 @implementation QNSListener
    79 @implementation QNSListener
    80 - (id) init
    80 - (id) init
    81 {
    81 {
    82     [super init];
    82     [super init];
    83     center = [NSNotificationCenter defaultCenter];
    83     center = [NSNotificationCenter defaultCenter];
    84     currentInterface = [CWInterface interface];
    84     currentInterface = [CWInterface interfaceWithName:nil];
    85     [center addObserver:self selector:@selector(notificationHandler:) name:kCWLinkDidChangeNotification object:nil];
    85     [center addObserver:self selector:@selector(notificationHandler:) name:kCWLinkDidChangeNotification object:nil];
    86     [center addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil];
    86     [center addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil];
    87 
    87 
    88     return self;
    88     return self;
    89 }
    89 }
   280 
   280 
   281         if([wifiInterface power]) {
   281         if([wifiInterface power]) {
   282             NSError *err = nil;
   282             NSError *err = nil;
   283             NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
   283             NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
   284 
   284 
   285             NSString *wantedSsid = 0;
   285             QString wantedSsid = 0;
   286             bool okToProceed = true;
   286             bool using8021X = false;
   287 
   287 
   288             if(getNetworkNameFromSsid(id) != id) {
   288             if(getNetworkNameFromSsid(id) != id) {
   289                 NSArray *array = [CW8021XProfile allUser8021XProfiles];
   289                 NSArray *array = [CW8021XProfile allUser8021XProfiles];
   290                 for (NSUInteger i=0; i<[array count]; ++i) {
   290                 for (NSUInteger i=0; i<[array count]; ++i) {
       
   291 
   291                     if(id == nsstringToQString([[array objectAtIndex:i] userDefinedName])
   292                     if(id == nsstringToQString([[array objectAtIndex:i] userDefinedName])
   292                         || id == nsstringToQString([[array objectAtIndex:i] ssid]) ) {
   293                         || id == nsstringToQString([[array objectAtIndex:i] ssid]) ) {
   293                         QString thisName = getSsidFromNetworkName(id);
   294                         QString thisName = getSsidFromNetworkName(id);
   294                         if(thisName.isEmpty()) {
   295                         if(thisName.isEmpty()) {
   295                             wantedSsid = qstringToNSString(id);
   296                             wantedSsid = id;
   296                         } else {
   297                         } else {
   297                             wantedSsid = qstringToNSString(thisName);
   298                             wantedSsid = thisName;
   298                         }
   299                         }
   299                         okToProceed = false;
       
   300                         [params setValue: [array objectAtIndex:i] forKey:kCWAssocKey8021XProfile];
   300                         [params setValue: [array objectAtIndex:i] forKey:kCWAssocKey8021XProfile];
       
   301                         using8021X = true;
   301                         break;
   302                         break;
   302                     }
   303                     }
   303                 }
   304                 }
   304             }
   305             }
   305 
   306 
   306             if(okToProceed) {
   307             if(!using8021X) {
   307                 NSUInteger index = 0;
   308                 QString wantedNetwork;
   308 
   309                 QMapIterator<QString, QMap<QString,QString> > i(userProfiles);
   309                 CWConfiguration *userConfig = [ wifiInterface configuration];
   310                 while (i.hasNext()) {
   310                 NSSet *remNets = [userConfig rememberedNetworks];
   311                     i.next();
   311                 NSEnumerator *enumerator = [remNets objectEnumerator];
   312                     wantedNetwork = i.key();
   312                 CWWirelessProfile *wProfile;
   313                     if(id == wantedNetwork) {
   313 
   314                         wantedSsid = getSsidFromNetworkName(wantedNetwork);
   314                 while ((wProfile = [enumerator nextObject])) {
       
   315                     if(id == nsstringToQString([wProfile ssid])) {
       
   316 
       
   317                         wantedSsid = [wProfile ssid];
       
   318                         [params setValue: [wProfile passphrase] forKey: kCWAssocKeyPassphrase];
       
   319                         break;
   315                         break;
   320                     }
   316                     }
   321                     index++;
       
   322                 }
   317                 }
   323             }
   318             }
   324 
   319 
   325             NSDictionary *parametersDict = nil;
   320             NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys:
   326             NSArray *apArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]];
   321                                        [NSNumber numberWithBool:YES], kCWScanKeyMerge,
   327 
   322                                        [NSNumber numberWithInteger:100], kCWScanKeyRestTime,
       
   323                                        qstringToNSString(wantedSsid), kCWScanKeySSID,
       
   324                                        nil];
       
   325 
       
   326 
       
   327             NSArray *scanArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]];
   328             if(!err) {
   328             if(!err) {
   329                 for(uint row=0; row < [apArray count]; row++ ) {
   329                 for(uint row=0; row < [scanArray count]; row++ ) {
   330                     CWNetwork *apNetwork = [apArray objectAtIndex:row];
   330                     CWNetwork *apNetwork = [scanArray objectAtIndex:row];
   331                     if([[apNetwork ssid] compare:wantedSsid] == NSOrderedSame) {
   331                     if(wantedSsid == nsstringToQString([apNetwork ssid])) {
       
   332 
       
   333                         if(!using8021X) {
       
   334                             SecKeychainAttribute attributes[3];
       
   335 
       
   336                             NSString *account = [apNetwork ssid];
       
   337                             NSString *keyKind = @"AirPort network password";
       
   338                             NSString *keyName = account;
       
   339 
       
   340                             attributes[0].tag = kSecAccountItemAttr;
       
   341                             attributes[0].data = (void *)[account UTF8String];
       
   342                             attributes[0].length = [account length];
       
   343 
       
   344                             attributes[1].tag = kSecDescriptionItemAttr;
       
   345                             attributes[1].data = (void *)[keyKind UTF8String];
       
   346                             attributes[1].length = [keyKind length];
       
   347 
       
   348                             attributes[2].tag = kSecLabelItemAttr;
       
   349                             attributes[2].data = (void *)[keyName UTF8String];
       
   350                             attributes[2].length = [keyName length];
       
   351 
       
   352                             SecKeychainAttributeList attributeList = {3,attributes};
       
   353 
       
   354                             SecKeychainSearchRef searchRef;
       
   355                             OSErr result = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &attributeList, &searchRef);
       
   356 
       
   357                             NSString *password = @"";
       
   358                             SecKeychainItemRef searchItem;
       
   359 
       
   360                             if (SecKeychainSearchCopyNext(searchRef, &searchItem) == noErr) {
       
   361                                 UInt32 realPasswordLength;
       
   362                                 SecKeychainAttribute attributesW[8];
       
   363                                 attributesW[0].tag = kSecAccountItemAttr;
       
   364                                 SecKeychainAttributeList listW = {1,attributesW};
       
   365                                 char *realPassword;
       
   366                                 OSStatus status = SecKeychainItemCopyContent(searchItem, NULL, &listW, &realPasswordLength,(void **)&realPassword);
       
   367 
       
   368                                 if (status == noErr) {
       
   369                                     if (realPassword != NULL) {
       
   370 
       
   371                                         QByteArray pBuf;
       
   372                                         pBuf.resize(realPasswordLength);
       
   373                                         pBuf.prepend(realPassword);
       
   374                                         pBuf.insert(realPasswordLength,'\0');
       
   375 
       
   376                                         password = [NSString stringWithUTF8String:pBuf];
       
   377                                     }
       
   378                                 }
       
   379 
       
   380                                 CFRelease(searchItem);
       
   381                                 SecKeychainItemFreeContent(&listW, realPassword);
       
   382                             } else {
       
   383                                 qDebug() << "SecKeychainSearchCopyNext error";
       
   384                             }
       
   385                             [params setValue: password forKey: kCWAssocKeyPassphrase];
       
   386                         } // end using8021X
       
   387 
   332                         bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err];
   388                         bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err];
   333 
   389 
   334                         if(!result) {
   390                         if(!result) {
   335                             emit connectionError(id, ConnectError);
   391                             emit connectionError(id, ConnectError);
   336                         } else {
   392                         } else {
   413          while (ij.hasNext()) {
   469          while (ij.hasNext()) {
   414              ij.next();
   470              ij.next();
   415              if(ij.key() == ssid) {
   471              if(ij.key() == ssid) {
   416                  return i.key();
   472                  return i.key();
   417              }
   473              }
   418 
       
   419          }
   474          }
   420             return map.key(ssid);
       
   421     }
   475     }
   422     return QString();
   476     return QString();
   423 }
   477 }
   424 
   478 
   425 QList<QNetworkConfigurationPrivate *> QCoreWlanEngine::scanForSsids(const QString &interfaceName)
   479 QList<QNetworkConfigurationPrivate *> QCoreWlanEngine::scanForSsids(const QString &interfaceName)
   433     CWInterface *currentInterface = [CWInterface interfaceWithName:qstringToNSString(interfaceName)];
   487     CWInterface *currentInterface = [CWInterface interfaceWithName:qstringToNSString(interfaceName)];
   434     QStringList addedConfigs;
   488     QStringList addedConfigs;
   435 
   489 
   436     if([currentInterface power]) {
   490     if([currentInterface power]) {
   437         NSError *err = nil;
   491         NSError *err = nil;
   438         NSDictionary *parametersDict = nil;
   492         NSDictionary *parametersDict =  [NSDictionary dictionaryWithObjectsAndKeys:
       
   493                                    [NSNumber numberWithBool:YES], kCWScanKeyMerge,
       
   494                                    [NSNumber numberWithInt:kCWScanTypeFast], kCWScanKeyScanType, // get the networks in the scan cache
       
   495                                    [NSNumber numberWithInteger:100], kCWScanKeyRestTime, nil];
   439         NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err];
   496         NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err];
   440 
       
   441         CWNetwork *apNetwork;
   497         CWNetwork *apNetwork;
   442         if(!err) {
   498         if(!err) {
   443             for(uint row=0; row < [apArray count]; row++ ) {
   499             for(uint row=0; row < [apArray count]; row++ ) {
   444                 apNetwork = [apArray objectAtIndex:row];
   500                 apNetwork = [apArray objectAtIndex:row];
   445 
   501 
   664 
   720 
   665         CWInterface *wifiInterface = [CWInterface interfaceWithName: [wifiInterfaces objectAtIndex:row]];
   721         CWInterface *wifiInterface = [CWInterface interfaceWithName: [wifiInterfaces objectAtIndex:row]];
   666         NSString *nsInterfaceName = [wifiInterface name];
   722         NSString *nsInterfaceName = [wifiInterface name];
   667 // add user configured system networks
   723 // add user configured system networks
   668         SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Qt corewlan", nil, nil);
   724         SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Qt corewlan", nil, nil);
   669         CFDictionaryRef airportPlist = (const __CFDictionary*)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName]);
   725         NSDictionary *airportPlist = (NSDictionary *)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName] autorelease]);
   670         CFRelease(dynRef);
   726         CFRelease(dynRef);
   671 
   727 
   672         NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"];
   728         NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"];
   673 
   729 
   674         NSArray *thisSsidarray = [prefNetDict valueForKey:@"SSID_STR"];
   730         NSArray *thisSsidarray = [prefNetDict valueForKey:@"SSID_STR"];
   682         }
   738         }
   683         CFRelease(airportPlist);
   739         CFRelease(airportPlist);
   684 
   740 
   685         // 802.1X user profiles
   741         // 802.1X user profiles
   686         QString userProfilePath = QDir::homePath() + "/Library/Preferences/com.apple.eap.profiles.plist";
   742         QString userProfilePath = QDir::homePath() + "/Library/Preferences/com.apple.eap.profiles.plist";
   687         NSDictionary* eapDict = [[NSMutableDictionary alloc] initWithContentsOfFile:qstringToNSString(userProfilePath)];
   743         NSDictionary* eapDict = [[NSDictionary alloc] initWithContentsOfFile:qstringToNSString(userProfilePath)];
   688         NSString *profileStr= @"Profiles";
   744         NSString *profileStr= @"Profiles";
   689         NSString *nameStr = @"UserDefinedName";
   745         NSString *nameStr = @"UserDefinedName";
   690         NSString *networkSsidStr = @"Wireless Network";
   746         NSString *networkSsidStr = @"Wireless Network";
   691         for (id profileKey in eapDict) {
   747         for (id profileKey in eapDict) {
   692             if ([profileStr isEqualToString:profileKey]) {
   748             if ([profileStr isEqualToString:profileKey]) {
   713                             map.insert(ssid, nsstringToQString(nsInterfaceName));
   769                             map.insert(ssid, nsstringToQString(nsInterfaceName));
   714                             userProfiles.insert(networkName, map);
   770                             userProfiles.insert(networkName, map);
   715                         }
   771                         }
   716                     }
   772                     }
   717                 }
   773                 }
   718             }
   774                 [itemDict release];
   719         }
   775             }
       
   776         }
       
   777         [eapDict release];
   720     }
   778     }
   721     [autoreleasepool release];
   779     [autoreleasepool release];
   722 #endif
   780 #endif
   723 
   781 
   724 }
   782 }