netwerk/wifi/osx_corewlan.mm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #import <Cocoa/Cocoa.h>
michael@0 6 #import <CoreWLAN/CoreWLAN.h>
michael@0 7
michael@0 8 #include <dlfcn.h>
michael@0 9 #include <unistd.h>
michael@0 10
michael@0 11 #include <objc/objc.h>
michael@0 12 #include <objc/objc-runtime.h>
michael@0 13
michael@0 14 #include "nsObjCExceptions.h"
michael@0 15 #include "nsAutoPtr.h"
michael@0 16 #include "nsCOMArray.h"
michael@0 17 #include "nsWifiMonitor.h"
michael@0 18 #include "nsWifiAccessPoint.h"
michael@0 19
michael@0 20 nsresult
michael@0 21 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
michael@0 22 {
michael@0 23 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
michael@0 24
michael@0 25 accessPoints.Clear();
michael@0 26
michael@0 27 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
michael@0 28
michael@0 29 @try {
michael@0 30 NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
michael@0 31 if (!bundle) {
michael@0 32 [pool release];
michael@0 33 return NS_ERROR_NOT_AVAILABLE;
michael@0 34 }
michael@0 35
michael@0 36 Class CWI_class = [bundle classNamed:@"CWInterface"];
michael@0 37 if (!CWI_class) {
michael@0 38 [pool release];
michael@0 39 return NS_ERROR_NOT_AVAILABLE;
michael@0 40 }
michael@0 41
michael@0 42 id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
michael@0 43 if (!scanResult) {
michael@0 44 [pool release];
michael@0 45 return NS_ERROR_NOT_AVAILABLE;
michael@0 46 }
michael@0 47
michael@0 48 NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
michael@0 49 NSEnumerator *enumerator = [scan objectEnumerator];
michael@0 50
michael@0 51 while (id anObject = [enumerator nextObject]) {
michael@0 52 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
michael@0 53 if (!ap) {
michael@0 54 [pool release];
michael@0 55 return NS_ERROR_OUT_OF_MEMORY;
michael@0 56 }
michael@0 57
michael@0 58 // [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
michael@0 59 // is a pain, so we'll use it for as long as it's available.
michael@0 60 unsigned char macData[6] = {0};
michael@0 61 if ([anObject respondsToSelector:@selector(bssidData)]) {
michael@0 62 NSData* data = [anObject bssidData];
michael@0 63 if (data) {
michael@0 64 memcpy(macData, [data bytes], 6);
michael@0 65 }
michael@0 66 } else {
michael@0 67 // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
michael@0 68 NSString* macString = [anObject bssid];
michael@0 69 if (macString && ([macString length] == 17)) {
michael@0 70 for (NSUInteger i = 0; i < 6; ++i) {
michael@0 71 NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
michael@0 72 NSScanner* scanner = [NSScanner scannerWithString:part];
michael@0 73 unsigned int data = 0;
michael@0 74 if (![scanner scanHexInt:&data]) {
michael@0 75 data = 0;
michael@0 76 }
michael@0 77 macData[i] = (unsigned char) data;
michael@0 78 }
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 // [CWInterface rssiValue] is available on OS X 10.7 and up (and
michael@0 83 // [CWInterface rssi] is deprecated).
michael@0 84 int signal = 0;
michael@0 85 if ([anObject respondsToSelector:@selector(rssiValue)]) {
michael@0 86 signal = (int) ((NSInteger) [anObject rssiValue]);
michael@0 87 } else {
michael@0 88 signal = [[anObject rssi] intValue];
michael@0 89 }
michael@0 90
michael@0 91 ap->setMac(macData);
michael@0 92 ap->setSignal(signal);
michael@0 93 ap->setSSID([[anObject ssid] UTF8String], 32);
michael@0 94
michael@0 95 accessPoints.AppendObject(ap);
michael@0 96 }
michael@0 97 }
michael@0 98 @catch(NSException *_exn) {
michael@0 99 [pool release];
michael@0 100 return NS_ERROR_NOT_AVAILABLE;
michael@0 101 }
michael@0 102
michael@0 103 [pool release];
michael@0 104
michael@0 105 return NS_OK;
michael@0 106
michael@0 107 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
michael@0 108 }

mercurial