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