Skip to content

Instantly share code, notes, and snippets.

@niw
Created August 25, 2012 12:48
Show Gist options
  • Save niw/3465147 to your computer and use it in GitHub Desktop.
Save niw/3465147 to your computer and use it in GitHub Desktop.
Toggle Host AP Mode on Mac OS X, Using undocumented APIs.
#import <CoreWLAN/CoreWLAN.h>
#import <objc/message.h>
int main(int argc, char* argv[]) {
@autoreleasepool {
int ch;
NSString *ssid = nil, *password = nil;
while((ch = getopt(argc, argv, "s:w:h")) != -1) {
switch(ch) {
case 's':
ssid = [NSString stringWithUTF8String:optarg];
break;
case 'w':
password = [NSString stringWithUTF8String:optarg];
break;
case '?':
case 'h':
default:
printf("USAGE: %s [-swh] command\n", argv[0]);
printf("\nOPTIONS:\n");
printf(" -s ssid SSID\n");
printf(" -w password WEP password\n");
printf(" -h Print help\n");
printf("\nCOMMAND:\n");
printf(" status Print interface mode\n");
printf(" start Start Host AP mode\n");
printf(" stop Stop Host AP mode\n");
return 0;
}
}
NSString *command = nil;
if(argv[optind]) {
command = [NSString stringWithUTF8String:argv[optind]];
}
CWInterface *iface = [CWInterface interface];
if(!command || [command isEqualToString:@"status"]) {
NSString *mode = nil;
switch(iface.interfaceMode) {
case kCWInterfaceModeStation:
mode = @"Station";
break;
case kCWInterfaceModeIBSS:
mode = @"IBSS";
break;
case kCWInterfaceModeHostAP:
mode = @"HostAP";
break;
case kCWInterfaceModeNone:
default:
mode = @"None";
}
printf("%s\n", [mode UTF8String]);
} else if([command isEqualToString:@"stop"]) {
// Stop Host AP mode
if(getuid() != 0) {
printf("You may need to run this command with root priviledge.\n");
}
objc_msgSend(iface, @selector(stopHostAPMode));
} else if([command isEqualToString:@"start"]) {
// Channel = 11(2GHz), Channel width = 20MHz
CWChannel *channel = [CWChannel alloc];
objc_msgSend(channel, @selector(initWithInfo:), @{@"CHANNEL": @11, @"CHANNEL_FLAGS": @10});
// securityType = 2: None, 32: WEP
unsigned long long securityType = 2;
if(password) {
securityType = 32;
}
// Start Host AP mode
NSError *error = nil;
objc_msgSend(iface,
@selector(startHostAPModeWithSSID:securityType:channel:password:error:),
[ssid dataUsingEncoding:NSUTF8StringEncoding],
securityType,
channel,
password,
&error);
if(error) {
printf("%s\n", [error.localizedDescription UTF8String]);
return 1;
}
}
return 0;
}
}
all: hostap
hostap: hostap.m
clang -framework Foundation -framework CoreWLAN -o $@ $<
clean:
rm -rf hostap
Copyright (C) 2012 Yoshimasa Niwa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@wolever
Copy link

wolever commented Dec 31, 2012

Ah, I've spent hours looking for exactly this! Unfortunately it doesn't seem to be working, though… Which version of OS X have you tested it on? On 10.8.2, I get this compile error:

clang -framework Foundation -framework CoreWLAN -o hostap hostap.m
hostap.m:66:55: error: unexpected '@' in program
      objc_msgSend(channel, @selector(initWithInfo:), @{@"CHANNEL": @11, ...
                                                      ^
hostap.m:66:67: error: expected ';' after expression
      objc_msgSend(channel, @selector(initWithInfo:), @{@"CHANNEL": @11, ...
                                                                  ^
                                                                  ;      
hostap.m:66:69: error: unexpected '@' in program
      objc_msgSend(channel, @selector(initWithInfo:), @{@"CHANNEL": @11, ...
                                                                    ^
hostap.m:66:90: error: expected ';' after expression
  ...@selector(initWithInfo:), @{@"CHANNEL": @11, @"CHANNEL_FLAGS": @10});
                                                                  ^
                                                                  ;       
hostap.m:66:92: error: unexpected '@' in program
  ...@selector(initWithInfo:), @{@"CHANNEL": @11, @"CHANNEL_FLAGS": @10});
                                                                    ^
hostap.m:66:57: warning: expression result unused [-Wunused-value]
      objc_msgSend(channel, @selector(initWithInfo:), @{@"CHANNEL": @11, ...
                                                        ^~~~~~~~~~
hostap.m:66:74: warning: expression result unused [-Wunused-value]
  ...@selector(initWithInfo:), @{@"CHANNEL": @11, @"CHANNEL_FLAGS": @10});
                                                  ^~~~~~~~~~~~~~~~
hostap.m:66:96: error: expected expression
  ...@selector(initWithInfo:), @{@"CHANNEL": @11, @"CHANNEL_FLAGS": @10});
                                                                        ^
hostap.m:80:20: error: use of undeclared identifier 'channel'
                   channel,
                   ^
hostap.m:91:1: error: expected external declaration
}
^
2 warnings and 8 errors generated.
make: *** [hostap] Error 1

And getting a channel using:

      NSSet *chans = [iface supportedWLANChannels];
      CWChannel *channel = [chans anyObject];

Yields:

$ ./hostap -s foo
startHostAPModeWithSSID error: Parameter error
$

@wolever
Copy link

wolever commented Dec 31, 2012

Ok, see my fork for a version that works with 10.8: https://gist.github.com/4418079

@niw
Copy link
Author

niw commented Feb 3, 2013

Ah, it was not tested on 10.8 yet. Thanks! and it's really difficult to know the comment event on Github... :(((

@niw
Copy link
Author

niw commented Aug 24, 2014

Just update this from my recent investigation around this API on OS X 10.9 and later.

Simply saying, this API seems like no longer work without Apple's permission.
If you use this command, you'll see an error.

The operation couldn’t be completed. (com.apple.coreWLAN.error error -6768.)

Which means the caller doesn't have a permission to change WiFi interface to Host AP mode.

I don't have any good solutions for this issue yet, it must be difficult to break this rule since it's like a challenging OS X security sandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment