You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7KB

  1. #import <Foundation/Foundation.h>
  2. #import <CoreWLAN/CWInterface.h>
  3. #import <CoreWLAN/CWChannel.h>
  4. #import <CoreWLAN/CWWiFiClient.h>
  5. #import <net/if.h>
  6. static struct wlan_state {
  7. CWWiFiClient *client;
  8. CWInterface *iface;
  9. NSSet<CWChannel *> *supportedChannels;
  10. } state;
  11. int corewlan_init() {
  12. state.iface = NULL;
  13. state.supportedChannels = NULL;
  14. return 0;
  15. }
  16. void corewlan_free() {
  17. }
  18. int corewlan_disassociate(int ifindex) {
  19. if (!state.client) {
  20. state.client = [CWWiFiClient sharedWiFiClient];
  21. }
  22. if (!state.iface) {
  23. char iface_cstr[IFNAMSIZ];
  24. NSString *iface_str =[NSString stringWithUTF8String:if_indextoname(ifindex, iface_cstr)];
  25. state.iface = [state.client interfaceWithName:iface_str];
  26. }
  27. [state.iface disassociate];
  28. return 0;
  29. }
  30. int corewlan_set_channel(int ifindex, int channel) {
  31. if (!state.client) {
  32. state.client = [CWWiFiClient sharedWiFiClient];
  33. }
  34. if (!state.iface) {
  35. char iface_cstr[IFNAMSIZ];
  36. NSString *iface_str = [NSString stringWithUTF8String:if_indextoname(ifindex, iface_cstr)];
  37. state.iface = [state.client interfaceWithName:iface_str];
  38. }
  39. if (!state.supportedChannels)
  40. state.supportedChannels = [state.iface supportedWLANChannels];
  41. CWChannel* currentCandidate = NULL;
  42. for (CWChannel* chan in state.supportedChannels) {
  43. if ([chan channelNumber] != channel)
  44. continue;
  45. if (!currentCandidate || [chan channelWidth] > [currentCandidate channelWidth])
  46. currentCandidate = chan;
  47. }
  48. if (!currentCandidate)
  49. return -1;
  50. NSError* err;
  51. [state.iface setWLANChannel:currentCandidate error:&err];
  52. return !err;
  53. }
  54. int corewlan_get_hostname(char *name, size_t len) {
  55. NSString *computerName = [(NSString *) SCDynamicStoreCopyComputerName(NULL, NULL) autorelease];
  56. strlcpy(name, [computerName UTF8String], len);
  57. return 0;
  58. }