1 /*
2 * Copyright (c) 2007, 2008, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
3 *
4 * OS dependent API.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <string.h>
24
25 #include "osdep.h"
26 #include "network.h"
27
28 extern struct wif * file_open(char * iface);
29
30 EXPORT int
wi_read(struct wif * wi,unsigned char * h80211,int len,struct rx_info * ri)31 wi_read(struct wif * wi, unsigned char * h80211, int len, struct rx_info * ri)
32 {
33 assert(wi->wi_read);
34 return wi->wi_read(wi, h80211, len, ri);
35 }
36
37 EXPORT int
wi_write(struct wif * wi,unsigned char * h80211,int len,struct tx_info * ti)38 wi_write(struct wif * wi, unsigned char * h80211, int len, struct tx_info * ti)
39 {
40 assert(wi->wi_write);
41 return wi->wi_write(wi, h80211, len, ti);
42 }
43
wi_set_ht_channel(struct wif * wi,int chan,unsigned int htval)44 EXPORT int wi_set_ht_channel(struct wif * wi, int chan, unsigned int htval)
45 {
46 assert(wi->wi_set_ht_channel);
47 return wi->wi_set_ht_channel(wi, chan, htval);
48 }
49
wi_set_channel(struct wif * wi,int chan)50 EXPORT int wi_set_channel(struct wif * wi, int chan)
51 {
52 assert(wi->wi_set_channel);
53 return wi->wi_set_channel(wi, chan);
54 }
55
wi_get_channel(struct wif * wi)56 EXPORT int wi_get_channel(struct wif * wi)
57 {
58 assert(wi->wi_get_channel);
59 return wi->wi_get_channel(wi);
60 }
61
wi_set_freq(struct wif * wi,int freq)62 EXPORT int wi_set_freq(struct wif * wi, int freq)
63 {
64 assert(wi->wi_set_freq);
65 return wi->wi_set_freq(wi, freq);
66 }
67
wi_get_freq(struct wif * wi)68 EXPORT int wi_get_freq(struct wif * wi)
69 {
70 assert(wi->wi_get_freq);
71 return wi->wi_get_freq(wi);
72 }
73
wi_get_monitor(struct wif * wi)74 EXPORT int wi_get_monitor(struct wif * wi)
75 {
76 assert(wi->wi_get_monitor);
77 return wi->wi_get_monitor(wi);
78 }
79
wi_get_ifname(struct wif * wi)80 EXPORT char * wi_get_ifname(struct wif * wi) { return wi->wi_interface; }
81
wi_close(struct wif * wi)82 EXPORT void wi_close(struct wif * wi)
83 {
84 assert(wi->wi_close);
85 wi->wi_close(wi);
86 }
87
wi_fd(struct wif * wi)88 EXPORT int wi_fd(struct wif * wi)
89 {
90 assert(wi->wi_fd);
91 return wi->wi_fd(wi);
92 }
93
wi_alloc(int sz)94 struct wif * wi_alloc(int sz)
95 {
96 struct wif * wi;
97 void * priv;
98
99 /* Allocate wif & private state */
100 wi = malloc(sizeof(*wi));
101 if (!wi) return NULL;
102 memset(wi, 0, sizeof(*wi));
103
104 priv = malloc(sz);
105 if (!priv)
106 {
107 free(wi);
108 return NULL;
109 }
110 memset(priv, 0, sz);
111 wi->wi_priv = priv;
112
113 return wi;
114 }
115
wi_priv(struct wif * wi)116 void * wi_priv(struct wif * wi) { return wi->wi_priv; }
117
wi_get_mac(struct wif * wi,unsigned char * mac)118 EXPORT int wi_get_mac(struct wif * wi, unsigned char * mac)
119 {
120 assert(wi->wi_get_mac);
121 return wi->wi_get_mac(wi, mac);
122 }
123
wi_set_mac(struct wif * wi,unsigned char * mac)124 EXPORT int wi_set_mac(struct wif * wi, unsigned char * mac)
125 {
126 assert(wi->wi_set_mac);
127 return wi->wi_set_mac(wi, mac);
128 }
129
wi_get_rate(struct wif * wi)130 EXPORT int wi_get_rate(struct wif * wi)
131 {
132 assert(wi->wi_get_rate);
133 return wi->wi_get_rate(wi);
134 }
135
wi_set_rate(struct wif * wi,int rate)136 EXPORT int wi_set_rate(struct wif * wi, int rate)
137 {
138 assert(wi->wi_set_rate);
139 return wi->wi_set_rate(wi, rate);
140 }
141
wi_get_mtu(struct wif * wi)142 EXPORT int wi_get_mtu(struct wif * wi)
143 {
144 assert(wi->wi_get_mtu);
145 return wi->wi_get_mtu(wi);
146 }
147
wi_set_mtu(struct wif * wi,int mtu)148 EXPORT int wi_set_mtu(struct wif * wi, int mtu)
149 {
150 assert(wi->wi_set_mtu);
151 return wi->wi_set_mtu(wi, mtu);
152 }
153
wi_open(char * iface)154 EXPORT struct wif * wi_open(char * iface)
155 {
156 struct wif * wi;
157
158 if (iface == NULL || iface[0] == 0)
159 {
160 return NULL;
161 }
162
163 wi = file_open(iface);
164 if (wi == (struct wif *) -1) return NULL;
165 if (!wi) wi = net_open(iface);
166 if (!wi) wi = wi_open_osdep(iface);
167 if (!wi) return NULL;
168
169 strncpy(wi->wi_interface, iface, sizeof(wi->wi_interface) - 1);
170 wi->wi_interface[sizeof(wi->wi_interface) - 1] = 0;
171
172 return wi;
173 }
174
175 /* tap stuff */
ti_name(struct tif * ti)176 EXPORT char * ti_name(struct tif * ti)
177 {
178 assert(ti->ti_name);
179 return ti->ti_name(ti);
180 }
181
ti_set_mtu(struct tif * ti,int mtu)182 EXPORT int ti_set_mtu(struct tif * ti, int mtu)
183 {
184 assert(ti->ti_set_mtu);
185 return ti->ti_set_mtu(ti, mtu);
186 }
187
ti_get_mtu(struct tif * ti)188 EXPORT int ti_get_mtu(struct tif * ti)
189 {
190 assert(ti->ti_get_mtu);
191 return ti->ti_get_mtu(ti);
192 }
193
ti_close(struct tif * ti)194 EXPORT void ti_close(struct tif * ti)
195 {
196 assert(ti->ti_close);
197 ti->ti_close(ti);
198 }
199
ti_fd(struct tif * ti)200 EXPORT int ti_fd(struct tif * ti)
201 {
202 assert(ti->ti_fd);
203 return ti->ti_fd(ti);
204 }
205
ti_read(struct tif * ti,void * buf,int len)206 EXPORT int ti_read(struct tif * ti, void * buf, int len)
207 {
208 assert(ti->ti_read);
209 return ti->ti_read(ti, buf, len);
210 }
211
ti_write(struct tif * ti,void * buf,int len)212 EXPORT int ti_write(struct tif * ti, void * buf, int len)
213 {
214 assert(ti->ti_write);
215 return ti->ti_write(ti, buf, len);
216 }
217
ti_set_mac(struct tif * ti,unsigned char * mac)218 EXPORT int ti_set_mac(struct tif * ti, unsigned char * mac)
219 {
220 assert(ti->ti_set_mac);
221 return ti->ti_set_mac(ti, mac);
222 }
223
ti_set_ip(struct tif * ti,struct in_addr * ip)224 EXPORT int ti_set_ip(struct tif * ti, struct in_addr * ip)
225 {
226 assert(ti->ti_set_ip);
227 return ti->ti_set_ip(ti, ip);
228 }
229
ti_alloc(int sz)230 struct tif * ti_alloc(int sz)
231 {
232 struct tif * ti;
233 void * priv;
234
235 /* Allocate tif & private state */
236 ti = malloc(sizeof(*ti));
237 if (!ti) return NULL;
238 memset(ti, 0, sizeof(*ti));
239
240 priv = malloc(sz);
241 if (!priv)
242 {
243 free(ti);
244 return NULL;
245 }
246 memset(priv, 0, sz);
247 ti->ti_priv = priv;
248
249 return ti;
250 }
251
ti_priv(struct tif * ti)252 void * ti_priv(struct tif * ti) { return ti->ti_priv; }
253