nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <string.h>
5  
6 #include "osdep/osdep.h"
7 #include "osdep.h"
8  
9 #ifdef __linux__
10 #include <linux/wireless.h>
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #else
14 #warning NOT COMPILING FOR LINUX - Ghosting (IDS Evasion) will not be available
15 #endif
16  
17 //Thats the max tx power we try to set, your fault if the hardware dies :P
18 #define MAX_TX_POWER 50
19  
20 int available_txpowers[MAX_TX_POWER];
21 int available_txpowers_count = 0;
22 int osdep_sockfd = -1;
23  
24 static struct wif *_wi_in, *_wi_out;
25  
26 struct devices
27 {
28 int fd_in, arptype_in;
29 int fd_out, arptype_out;
30 int fd_rtc;
31 } dev;
32  
33 int current_channel = 0;
34 char *osdep_iface = NULL;
35  
36 int osdep_start(char *interface)
37 {
38 osdep_iface = malloc(strlen(interface) + 1);
39 strcpy(osdep_iface, interface);
40  
41 /* open the replay interface */
42 _wi_out = wi_open(interface);
43 if (!_wi_out)
44 return 1;
45 dev.fd_out = wi_fd(_wi_out);
46  
47 /* open the packet source */
48 _wi_in = _wi_out;
49 dev.fd_in = dev.fd_out;
50  
51 /* XXX */
52 dev.arptype_in = dev.arptype_out;
53  
54 return 0;
55 }
56  
57  
58 int osdep_send_packet(struct packet *pkt)
59 {
60 struct wif *wi = _wi_out; /* XXX globals suck */
61 if (wi_write(wi, pkt->data, pkt->len, NULL) == -1) {
62 switch (errno) {
63 case EAGAIN:
64 case ENOBUFS:
65 usleep(10000);
66 return 0; /* XXX not sure I like this... -sorbo */
67 }
68  
69 perror("wi_write()");
70 return -1;
71 }
72  
73 return 0;
74 }
75  
76  
77 struct packet osdep_read_packet()
78 {
79 struct wif *wi = _wi_in; /* XXX */
80 int rc;
81 struct packet pkt;
82  
83 do {
84 rc = wi_read(wi, pkt.data, MAX_PACKET_SIZE, NULL);
85 if (rc == -1) {
86 perror("wi_read()");
87 pkt.len = 0;
88 return pkt;
89 }
90 } while (rc < 1);
91  
92 pkt.len = rc;
93 return pkt;
94 }
95  
96  
97 void osdep_set_channel(int channel)
98 {
99 wi_set_channel(_wi_out, channel);
100 current_channel = channel;
101 }
102  
103  
104 int osdep_get_channel()
105 {
106 return current_channel;
107 }
108  
109  
110 void osdep_set_rate(int rate)
111 {
112 int i, valid = 0;
113  
114 for (i=0; i<VALID_RATE_COUNT; i++) {
115 if (VALID_BITRATES[i] == rate) valid = 1;
116 }
117  
118 if (!valid) printf("BUG: osdep_set_rate(): Invalid bitrate selected!\n");
119  
120 wi_set_rate(_wi_out, rate);
121 }
122  
123 #ifdef __linux__
124 void osdep_init_txpowers()
125 {
126 //Stupid? Just try rates to find working ones...
127 //Anybody know how to get a proper list of supported rates?
128  
129 if (!osdep_iface) {
130 printf("D'oh, open interface first, idiot...\n");
131 return;
132 }
133  
134 struct iwreq wreq;
135 int old_txpower, i;
136  
137 osdep_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
138 if(osdep_sockfd < 0) {
139 printf("WTF? Couldn't open socket. Something is VERY wrong...\n");
140 return;
141 }
142  
143 memset(&wreq, 0, sizeof(struct iwreq));
144 strncpy(wreq.ifr_name, osdep_iface, IFNAMSIZ);
145 wreq.u.power.flags = 0;
146  
147 if(ioctl(osdep_sockfd, SIOCGIWTXPOW, &wreq) < 0) {
148 perror("Can't get TX power from card: ");
149 return;
150 }
151  
152 old_txpower = wreq.u.txpower.value;
153 printf("Current TX power: %i dBm\n", wreq.u.txpower.value);
154  
155 for (i=0; i<MAX_TX_POWER; i++) {
156 wreq.u.txpower.value = i;
157 if(ioctl(osdep_sockfd, SIOCSIWTXPOW, &wreq) == 0) {
158 available_txpowers[available_txpowers_count] = i;
159 available_txpowers_count++;
160 }
161 }
162  
163 //Reset to initial value
164 wreq.u.txpower.value = old_txpower;
165 ioctl(osdep_sockfd, SIOCSIWTXPOW, &wreq);
166  
167 printf("Available TX powers: ");
168 for (i=0; i<available_txpowers_count; i++) {
169 printf("%i, ", available_txpowers[i]);
170 }
171 printf("\b\b dBm\n");
172 }
173  
174 void osdep_random_txpower(int min) {
175 long rnd;
176 struct iwreq wreq;
177  
178 if (! available_txpowers_count) { //This also makes sure the socket exists ;)
179 printf("Can't set random TX power since no TX power is known to me :(\n");
180 return;
181 }
182  
183 do {
184 rnd = random() % available_txpowers_count;
185 } while(available_txpowers[rnd] < min);
186  
187 memset(&wreq, 0, sizeof(struct iwreq));
188 strncpy(wreq.ifr_name, osdep_iface, IFNAMSIZ);
189  
190 ioctl(osdep_sockfd, SIOCGIWTXPOW, &wreq);
191 wreq.u.txpower.value = available_txpowers[rnd];
192 ioctl(osdep_sockfd, SIOCSIWTXPOW, &wreq);
193 }
194  
195 int osdep_get_max_txpower() {
196 int max = 0, i;
197  
198 if (! available_txpowers_count) {
199 printf("You forget to osdep_init_txpowers()!\n");
200 return 0;
201 }
202  
203 for (i=0; i<available_txpowers_count; i++) {
204 if (available_txpowers[i] > max) max = available_txpowers[i];
205 }
206  
207 return max;
208 }
209 #endif