forked from br101/horst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifctrl-osx.m
197 lines (165 loc) · 6.52 KB
/
ifctrl-osx.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2015 Bruno Randolf <br1@einfach.org>
* Copyright (C) 2015 Jeromy Fu <fuji246@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ifctrl.h"
#include "main.h"
#include "ieee80211_util.h"
#import <CoreWLAN/CoreWLAN.h>
bool osx_set_freq(const char *interface, unsigned int freq)
{
int channel = ieee80211_freq2channel(freq);
CWWiFiClient * wifiClient = [CWWiFiClient sharedWiFiClient];
NSString * interfaceName = [[NSString alloc] initWithUTF8String: interface];
CWInterface * currentInterface = [wifiClient interfaceWithName: interfaceName];
[interfaceName release];
NSSet * channels = [currentInterface supportedWLANChannels];
CWChannel * wlanChannel = nil;
for (CWChannel * _wlanChannel in channels) {
if ([_wlanChannel channelNumber] == channel)
wlanChannel = _wlanChannel;
}
bool ret = true;
if (wlanChannel != nil) {
NSError *err = nil;
BOOL result = [currentInterface setWLANChannel:wlanChannel error:&err];
if( !result ) {
printlog("set channel %ld err: %s", (long)[wlanChannel channelNumber], [[err localizedDescription] UTF8String]);
ret = false;
}
}
return ret;
}
enum chan_width get_channel_width(CWChannelWidth width)
{
enum chan_width current_width = CHAN_WIDTH_UNSPEC;
switch (width) {
case kCWChannelWidth20MHz:
current_width = CHAN_WIDTH_20;
break;
case kCWChannelWidth40MHz:
current_width = CHAN_WIDTH_40;
break;
case kCWChannelWidth80MHz:
current_width = CHAN_WIDTH_80;
break;
case kCWChannelWidth160MHz:
current_width = CHAN_WIDTH_160;
break;
default:
break;
}
return current_width;
}
int osx_get_channels(const char* devname, struct channel_list* channels) {
CWWiFiClient * wifiClient = [CWWiFiClient sharedWiFiClient];
NSString * interfaceName = [[NSString alloc] initWithUTF8String: devname];
CWInterface * currentInterface = [wifiClient interfaceWithName: interfaceName];
[interfaceName release];
NSSet * supportedChannelsSet = [currentInterface supportedWLANChannels];
NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"channelNumber" ascending:YES];
NSArray * sortedChannels = [supportedChannelsSet sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
int i = 0;
for (int i = 0; i < MAX_BANDS; ++i) {
channels->band[i].num_channels = 0;
channels->band[i].streams_rx = 0;
channels->band[i].streams_tx = 0;
channels->band[i].max_chan_width = CHAN_WIDTH_20;
}
channels->num_bands = MAX_BANDS;
i = 0;
NSInteger lastNum = -1;
for( id eachChannel in sortedChannels )
{
NSInteger num = [eachChannel channelNumber];
CWChannelBand band = [eachChannel channelBand];
CWChannelWidth width = [eachChannel channelWidth];
printlog("num: %ld, band: %ld, width: %ld", num, (long)band, (long)width);
if (lastNum != num ) {
channel_list_add(ieee80211_channel2freq(num));
}
int bandIdx = -1;
if( kCWChannelBand2GHz == band ) {
bandIdx = 0;
} else if( kCWChannelBand5GHz == band ) {
bandIdx = 1;
}
if( bandIdx >= 0) {
if (lastNum != num ) {
++(channels->band[bandIdx].num_channels);
}
enum chan_width w = get_channel_width(width);
channels->band[bandIdx].max_chan_width = \
channels->band[bandIdx].max_chan_width < w ? w : channels->band[bandIdx].max_chan_width;
}
lastNum = num;
if( ++i > MAX_CHANNELS) {
break;
}
}
printlog("band 0 channels: %d", channels->band[0].num_channels);
printlog("band 1 channels: %d", channels->band[1].num_channels);
return i;
}
bool ifctrl_init() {
CWWiFiClient * wifiClient = [CWWiFiClient sharedWiFiClient];
NSString * interfaceName = [[NSString alloc] initWithUTF8String: conf.ifname];
CWInterface * currentInterface = [wifiClient interfaceWithName: interfaceName];
[interfaceName release];
[currentInterface disassociate];
return true;
};
void ifctrl_finish() {
CWWiFiClient * wifiClient = [CWWiFiClient sharedWiFiClient];
NSString * interfaceName = [[NSString alloc] initWithUTF8String: conf.ifname];
CWInterface * currentInterface = [wifiClient interfaceWithName: interfaceName];
[interfaceName release];
CWNetwork * _network = [[CWNetwork alloc] init];
[currentInterface associateToNetwork:_network password:nil error:nil];
};
bool ifctrl_iwadd_monitor(__attribute__((unused))const char *interface, __attribute__((unused))const char *monitor_interface) {
printlog("add monitor: not implemented");
return false;
};
bool ifctrl_iwdel(__attribute__((unused))const char *interface) {
printlog("iwdel: not implemented");
return false;
};
bool ifctrl_iwset_monitor(__attribute__((unused))const char *interface) {
printlog("set monitor: not implemented");
return false;
};
bool ifctrl_iwset_freq(__attribute__((unused))const char *interface, __attribute__((unused))unsigned int freq,
__attribute__((unused))enum chan_width width, __attribute__((unused))unsigned int center) {
if (osx_set_freq(interface, freq))
return true;
return false;
};
bool ifctrl_iwget_interface_info(__attribute__((unused))const char *interface) {
printlog("get interface info: not implemented");
return false;
};
bool ifctrl_iwget_freqlist(__attribute__((unused))int phy, struct channel_list* channels) {
int num_channels = osx_get_channels(conf.ifname, channels);
if (num_channels)
return true;
return false;
};
bool ifctrl_is_monitor() {
return true;
};