forked from br101/horst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.c
202 lines (158 loc) · 3.99 KB
/
channel.c
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
198
199
200
201
202
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2014 Bruno Randolf ([email protected])
*
* 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 <stdlib.h>
#include "main.h"
#include "util.h"
#include "wext.h"
#include "channel.h"
static struct chan_freq channels[MAX_CHANNELS];
#if defined(__APPLE__)
int
channel_change(__attribute__((unused)) int idx)
{
return 0;
}
int
channel_auto_change(void)
{
return 0;
}
int
channel_get_current_chan() {
return -1;
}
void
channel_init(void) {
}
#else
static struct timeval last_channelchange;
extern int mon; /* monitoring socket */
long
channel_get_remaining_dwell_time(void)
{
if (!conf.do_change_channel)
return LONG_MAX;
return conf.channel_time
- the_time.tv_sec * 1000000
- the_time.tv_usec
+ last_channelchange.tv_sec * 1000000
+ last_channelchange.tv_usec;
}
int
channel_change(int idx)
{
if (wext_set_freq(mon, conf.ifname, channels[idx].freq) == 0) {
printlog("ERROR: could not set channel %d", channels[idx].chan);
return 0;
}
conf.channel_idx = idx;
last_channelchange = the_time;
return 1;
}
int
channel_auto_change(void)
{
int new_idx;
int ret = 1;
int start_idx;
if (channel_get_remaining_dwell_time() > 0)
return 0; /* too early */
if (conf.do_change_channel) {
start_idx = new_idx = conf.channel_idx;
do {
new_idx = new_idx + 1;
if (new_idx >= conf.num_channels ||
new_idx >= MAX_CHANNELS ||
(conf.channel_max &&
channel_get_chan_from_idx(new_idx) > conf.channel_max))
new_idx = 0;
ret = channel_change(new_idx);
/* try setting different channels in case we get errors only
* on some channels (e.g. ipw2200 reports channel 14 but cannot
* be set to use it). stop if we tried all channels */
} while (ret != 1 && new_idx != start_idx);
}
return ret;
}
int
channel_get_current_chan() {
return channel_get_chan_from_idx(conf.channel_idx);
}
static int
get_current_wext_channel_idx(int mon)
{
int freq, ch;
/* get current channel & map to our channel array */
freq = wext_get_freq(mon, conf.ifname);
if (freq == 0)
return -1;
ch = channel_find_index_from_freq(freq);
DEBUG("***%d\n", ch);
return ch;
}
void
channel_init(void) {
/* get available channels */
conf.num_channels = wext_get_channels(mon, conf.ifname, channels);
conf.channel_idx = get_current_wext_channel_idx(mon);
if (conf.channel_num_initial > 0)
channel_change(channel_find_index_from_chan(conf.channel_num_initial));
else
conf.channel_num_initial = channel_get_chan_from_idx(conf.channel_idx);
conf.channel_initialized = 1;
}
#endif
int
channel_find_index_from_chan(int c)
{
int i = -1;
for (i = 0; i < conf.num_channels && i < MAX_CHANNELS; i++)
if (channels[i].chan == c)
return i;
return -1;
}
int
channel_find_index_from_freq(int f)
{
int i = -1;
for (i = 0; i < conf.num_channels && i < MAX_CHANNELS; i++)
if (channels[i].freq == f)
return i;
return -1;
}
int
channel_get_chan_from_idx(int i) {
if (i >= 0 && i < conf.num_channels && i < MAX_CHANNELS)
return channels[i].chan;
else
return -1;
}
struct chan_freq*
channel_get_struct(int i) {
if (i < conf.num_channels && i < MAX_CHANNELS)
return &channels[i];
return NULL;
}
void
channel_set(int i, int chan, int freq) {
if (i < conf.num_channels && i < MAX_CHANNELS) {
channels[i].chan = chan;
channels[i].freq = freq;
}
}