summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/sniff.cc48
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc7
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh9
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc15
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh7
5 files changed, 45 insertions, 41 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
index e516177..6e512c4 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
@@ -1,338 +1,348 @@
1/* 1/*
2 * rfmon mode sniffer 2 * rfmon mode sniffer
3 * 3 *
4 * $Id$ 4 * $Id$
5 */ 5 */
6 6
7#include "sniff.hh" 7#include "sniff.hh"
8#include "ieee802_11.hh" 8#include "ieee802_11.hh"
9#include "extract.hh" 9#include "extract.hh"
10#include "wl_log.hh" 10#include "wl_log.hh"
11#include "wl_types.hh"
11#include "wl_proto.hh" 12#include "wl_proto.hh"
12 13
13/* Main function, checks packets */ 14/* Main function, checks packets */
14void process_packets(const struct pcap_pkthdr *pkthdr, const unsigned char *packet, char *guihost, int guiport) 15void process_packets(const struct pcap_pkthdr *pkthdr,
16 const unsigned char *packet,
17 char *guihost,
18 int guiport)
15{ 19{
16 unsigned int caplen = pkthdr->caplen; 20 unsigned int caplen = pkthdr->caplen;
17 unsigned int length = pkthdr->len; 21 unsigned int length = pkthdr->len;
18 u_int16_t fc; 22 u_int16_t fc;
19 unsigned int HEADER_LENGTH; 23 unsigned int HEADER_LENGTH;
20 24
21 /* pinfo holds all interresting information for us */ 25 /* pinfo holds all interresting information for us */
22 struct packetinfo pinfo; 26 struct packetinfo pinfo;
23 struct packetinfo *pinfoptr; 27 struct packetinfo *pinfoptr;
24 28
25 /* wl_network_t will finally be set and send to the ui */ 29 /* wl_network_t will finally be set and send to the ui */
26 wl_network_t wl_net; 30 wl_network_t wl_net;
27 31
28 pinfoptr=&pinfo; 32 pinfoptr=&pinfo;
29 33
30 pinfoptr->isvalid = 0; 34 pinfoptr->isvalid = 0;
31 pinfoptr->pktlen = pkthdr->len; 35 pinfoptr->pktlen = pkthdr->len;
32 36
33 if (caplen < IEEE802_11_FC_LEN) 37 if (caplen < IEEE802_11_FC_LEN)
34 { 38 {
35 /* This is a garbage packet, because is does not long enough 39 /* This is a garbage packet, because is does not long enough
36 to hold a 802.11b header */ 40 to hold a 802.11b header */
37 pinfoptr->isvalid = 0; 41 pinfoptr->isvalid = 0;
38 return; 42 return;
39 } 43 }
40 44
41 /* Gets the framecontrol bits (2bytes long) */ 45 /* Gets the framecontrol bits (2bytes long) */
42 fc = EXTRACT_LE_16BITS(packet); 46 fc = EXTRACT_LE_16BITS(packet);
43 47
44 HEADER_LENGTH = GetHeaderLength(fc); 48 HEADER_LENGTH = GetHeaderLength(fc);
45 49
46 if (caplen < HEADER_LENGTH) 50 if (caplen < HEADER_LENGTH)
47 { 51 {
48 /* This is a garbage packet, because it is not long enough 52 /* This is a garbage packet, because it is not long enough
49 to hold a correct header of its type */ 53 to hold a correct header of its type */
50 pinfoptr->isvalid = 0; 54 pinfoptr->isvalid = 0;
51 return; 55 return;
52 } 56 }
53 57
54 /* Decode 802.11b header out of the packet */ 58 /* Decode 802.11b header out of the packet */
55 if (decode_80211b_hdr(packet,pinfoptr) == 0) 59 if (decode_80211b_hdr(packet,pinfoptr) == 0)
56 { 60 {
57 /* Justification of the ofset to further process the packet */ 61 /* Justification of the ofset to further process the packet */
58 length -= HEADER_LENGTH; 62 length -= HEADER_LENGTH;
59 caplen -= HEADER_LENGTH; 63 caplen -= HEADER_LENGTH;
60 packet += HEADER_LENGTH; 64 packet += HEADER_LENGTH;
61 } 65 }
62 else /* Something is wrong,could not be a correct packet */ 66 else /* Something is wrong,could not be a correct packet */
63 return; 67 return;
64 68
65 switch (FC_TYPE(fc)) 69 switch (FC_TYPE(fc))
66 { 70 {
67 /* Is it a managemnet frame? */ 71 /* Is it a managemnet frame? */
68 case T_MGMT: 72 case T_MGMT:
69 switch (FC_SUBTYPE(fc)) 73 switch (FC_SUBTYPE(fc))
70 { 74 {
71 case ST_BEACON: 75 case ST_BEACON:
72 if (handle_beacon(fc, packet,pinfoptr) ==0) 76 if (handle_beacon(fc, packet,pinfoptr) ==0)
73 { 77 {
74 if (!strcmp(pinfoptr->desthwaddr,"ff:ff:ff:ff:ff:ff") == 0) 78 if (!strcmp(pinfoptr->desthwaddr,"ff:ff:ff:ff:ff:ff") == 0)
75 { 79 {
76 /* Every beacon must have the broadcast as destination 80 /* Every beacon must have the broadcast as destination
77 so it must be a shitti packet */ 81 so it must be a shitti packet */
78 pinfoptr->isvalid = 0; 82 pinfoptr->isvalid = 0;
79 return; 83 return;
80 } 84 }
81 85
82 if (pinfoptr->cap_ESS == pinfoptr->cap_IBSS) 86 if (pinfoptr->cap_ESS == pinfoptr->cap_IBSS)
83 { 87 {
84 /* Only one of both are possible, so must be 88 /* Only one of both are possible, so must be
85 a noise packet, if this comes up */ 89 a noise packet, if this comes up */
86 pinfoptr->isvalid = 0; 90 pinfoptr->isvalid = 0;
87 return; 91 return;
88 } 92 }
89 if (pinfoptr->channel < 1 || pinfoptr->channel > 14) 93 if (pinfoptr->channel < 1 || pinfoptr->channel > 14)
90 { 94 {
91 /* Only channels between 1 and 14 are possible 95 /* Only channels between 1 and 14 are possible
92 others must be noise packets */ 96 others must be noise packets */
93 pinfoptr->isvalid = 0; 97 pinfoptr->isvalid = 0;
94 return; 98 return;
95 } 99 }
96 100
97 /* Here should be the infos to the gui issued */ 101 /* Here should be the infos to the gui issued */
98 if (pinfoptr->cap_ESS == 1 &&pinfoptr->cap_IBSS ==0) 102 if (pinfoptr->cap_ESS == 1 && pinfoptr->cap_IBSS ==0)
99 { 103 {
100 wl_loginfo("Found an access point"); 104 wl_loginfo("Found an access point");
101 wl_net.net_type=1; 105 wl_net.net_type=1;
102 } 106 }
103 else if(pinfoptr->cap_ESS == 0 && pinfoptr->cap_IBSS == 2) 107 else if(pinfoptr->cap_ESS == 0 && pinfoptr->cap_IBSS == 2)
104 { 108 {
105 wl_loginfo("Found an ad-hoc network"); 109 wl_loginfo("Found an ad-hoc network");
106 wl_net.net_type=2; 110 wl_net.net_type=2;
107 } 111 }
108
109 memset(wl_net.bssid, 0, sizeof(wl_net.bssid));
110 112
111 if (strcmp (pinfoptr->ssid,NONBROADCASTING) ==0) 113 if (strcmp (pinfoptr->ssid,NONBROADCASTING) ==0)
112 wl_loginfo("Net is a non-broadcasting network"); 114 wl_loginfo("Net is a non-broadcasting network");
113 else 115 else
114 wl_loginfo("SSID is: %s", pinfoptr->ssid); 116 wl_loginfo("SSID is: %s", pinfoptr->ssid);
115 117
116 wl_loginfo("SSID is: %s", pinfoptr->ssid); 118 wl_loginfo("SSID is: %s", pinfoptr->ssid);
119 memset(wl_net.bssid, 0, sizeof(wl_net.bssid));
117 memcpy(wl_net.bssid, pinfoptr->ssid, sizeof(wl_net.bssid)-1); 120 memcpy(wl_net.bssid, pinfoptr->ssid, sizeof(wl_net.bssid)-1);
118 121
119 wl_loginfo("SSID length is: %d", pinfoptr->ssid_len); 122 wl_loginfo("SSID length is: %d", pinfoptr->ssid_len);
120 wl_net.ssid_len=pinfoptr->ssid_len; 123 wl_net.ssid_len=pinfoptr->ssid_len;
121 124
122 wl_loginfo("Channel is: %d", pinfoptr->channel); 125 wl_loginfo("Channel is: %d", pinfoptr->channel);
123 wl_net.channel=pinfoptr->channel; 126 wl_net.channel=pinfoptr->channel;
124 wl_net.wep=pinfoptr->cap_WEP; 127 wl_net.wep=pinfoptr->cap_WEP;
125 128
126 wl_loginfo("Mac is: %s", pinfoptr->sndhwaddr); 129 wl_loginfo("Mac is: %s", pinfoptr->sndhwaddr);
127 memset(wl_net.mac, 0, sizeof(wl_net.mac)); 130 memset(wl_net.mac, 0, sizeof(wl_net.mac));
128 memcpy(wl_net.mac, pinfoptr->sndhwaddr, sizeof(wl_net.mac)-1); 131 memcpy(wl_net.mac, pinfoptr->sndhwaddr, sizeof(wl_net.mac)-1);
129 132
130 if(!send_network_found((char *)guihost, guiport, &wl_net)) 133 if(!send_network_found((char *)guihost, guiport, &wl_net))
131 { 134 {
132 wl_logerr("Error sending data to UI: %s", strerror(errno)); 135 wl_logerr("Error sending data to UI: %s", strerror(errno));
133 break; 136 break;
134 } 137 }
135 wl_loginfo("Sent network to GUI '%s:%d'", guihost, guiport); 138 wl_loginfo("Sent network to GUI '%s:%d'", guihost, guiport);
136 } 139 }
137 break; 140 break;
138 141
139 default: 142 default:
140 wl_logerr("Unknown IEEE802.11 frame subtype (%d)", FC_SUBTYPE(fc)); 143 wl_logerr("Unknown IEEE802.11 frame subtype (%d)", FC_SUBTYPE(fc));
141 break; 144 break;
142 } /* End of switch over different mgt frame types */ 145 } /* End of switch over different mgt frame types */
143 146
144 break; 147 break;
145 148
146 case T_CTRL: 149 case T_CTRL:
147 wl_loginfo("Received control frame, not implemented yet"); 150 wl_loginfo("Received control frame, not implemented yet");
148 break; 151 break;
149 152
150 case T_DATA: 153 case T_DATA:
151 wl_loginfo("Received date frame, not implemented yet"); 154 wl_loginfo("Received date frame, not implemented yet");
152 break; 155 break;
153 156
154 default: 157 default:
155 wl_logerr("Unknown IEEE802.11 frame type (%d)", FC_TYPE(fc)); 158 wl_logerr("Unknown IEEE802.11 frame type (%d)", FC_TYPE(fc));
156 break; 159 break;
157 } 160 }
158} 161}
159 162
160/* This decodes the 802.11b frame header out of the 802.11b packet 163/* This decodes the 802.11b frame header out of the 802.11b packet
161 all the infos is placed into the packetinfo structure */ 164 all the infos is placed into the packetinfo structure */
162int decode_80211b_hdr(const u_char *p,struct packetinfo *ppinfo) 165int decode_80211b_hdr(const u_char *p,struct packetinfo *ppinfo)
163{ 166{
164 const struct mgmt_header_t *mgthdr = (const struct mgmt_header_t *) p; 167 const struct mgmt_header_t *mgthdr = (const struct mgmt_header_t *) p;
165 ppinfo->fcsubtype = FC_SUBTYPE(mgthdr->fc); 168 ppinfo->fcsubtype = FC_SUBTYPE(mgthdr->fc);
166 169
167 /* Get the sender, bssid and dest mac address */ 170 /* Get the sender, bssid and dest mac address */
168 etheraddr_string(mgthdr->bssid,ppinfo->bssid); 171 etheraddr_string(mgthdr->bssid,ppinfo->bssid);
169 etheraddr_string(mgthdr->da,ppinfo->desthwaddr); 172 etheraddr_string(mgthdr->da,ppinfo->desthwaddr);
170 etheraddr_string(mgthdr->sa,ppinfo->sndhwaddr); 173 etheraddr_string(mgthdr->sa,ppinfo->sndhwaddr);
171 ppinfo->fc_wep = FC_WEP(mgthdr->fc); 174 ppinfo->fc_wep = FC_WEP(mgthdr->fc);
172 return 0; 175 return 0;
173} 176}
174 177
175 178
176void etheraddr_string(register const u_char *ep, char *text) 179void etheraddr_string(register const u_char *ep, char *text)
177{ 180{
178 static char hex[] = "0123456789abcdef"; 181 static char hex[] = "0123456789abcdef";
179 register unsigned int i, j; 182 register unsigned int i, j;
180 register char *cp; 183 register char *cp;
181 char buf[sizeof("00:00:00:00:00:00\0")]; 184 char buf[sizeof("00:00:00:00:00:00\0")];
182 cp = buf; 185 cp = buf;
183 if ((j = *ep >> 4) != 0) 186 if ((j = *ep >> 4) != 0)
184 { 187 {
185 *cp++ = hex[j]; 188 *cp++ = hex[j];
186 } 189 }
187 else 190 else
188 { 191 {
189 *cp++ = '0'; 192 *cp++ = '0';
190 } 193 }
191 *cp++ = hex[*ep++ & 0xf]; 194 *cp++ = hex[*ep++ & 0xf];
192 195
193 for (i = 5; (int)--i >= 0;) 196 for (i = 5; (int)--i >= 0;)
194 { 197 {
195 *cp++ = ':'; 198 *cp++ = ':';
196 if ((j = *ep >> 4) != 0) 199 if ((j = *ep >> 4) != 0)
197 { 200 {
198 *cp++ = hex[j]; 201 *cp++ = hex[j];
199 } 202 }
200 else 203 else
201 { 204 {
202 *cp++ = '0'; 205 *cp++ = '0';
203 } 206 }
204 207
205 *cp++ = hex[*ep++ & 0xf]; 208 *cp++ = hex[*ep++ & 0xf];
206 } 209 }
207 *cp = '\0'; 210 *cp = '\0';
208 strcpy(text,buf); 211 strcpy(text,buf);
209} 212}
210 213
211/* beacon handler */ 214/* beacon handler */
212int handle_beacon(u_int16_t fc, const u_char *p,struct packetinfo *ppinfo) 215int handle_beacon(u_int16_t fc, const u_char *p,struct packetinfo *ppinfo)
213{ 216{
214 struct mgmt_body_t pbody; 217 struct mgmt_body_t pbody;
215 int offset = 0; 218 int offset = 0;
216 219
217 /* Get the static informations out of the packet */ 220 /* Get the static informations out of the packet */
218 memset(&pbody, 0, sizeof(pbody)); 221 memset(&pbody, 0, sizeof(pbody));
219 memcpy(&pbody.timestamp, p, 8); 222 memcpy(&pbody.timestamp, p, 8);
220 offset += 8; 223 offset += 8;
221 pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset); 224 pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset);
222 offset += 2; 225 offset += 2;
223 pbody.capability_info = EXTRACT_LE_16BITS(p+offset); 226 pbody.capability_info = EXTRACT_LE_16BITS(p+offset);
224 offset += 2; 227 offset += 2;
225 228
226 /* Gets the different flags out of the capabilities */ 229 /* Gets the different flags out of the capabilities */
227 ppinfo->cap_ESS = CAPABILITY_ESS(pbody.capability_info); 230 ppinfo->cap_ESS = CAPABILITY_ESS(pbody.capability_info);
228 ppinfo->cap_IBSS = CAPABILITY_IBSS(pbody.capability_info); 231 ppinfo->cap_IBSS = CAPABILITY_IBSS(pbody.capability_info);
229 ppinfo->cap_WEP = CAPABILITY_PRIVACY(pbody.capability_info); 232 ppinfo->cap_WEP = CAPABILITY_PRIVACY(pbody.capability_info);
230 233
231 /* Gets the tagged elements out of the packets */ 234 /* Gets the tagged elements out of the packets */
232 while (offset + 1 < ppinfo->pktlen) 235 while (offset + 1 < ppinfo->pktlen)
233 { 236 {
234 switch (*(p + offset)) 237 switch (*(p + offset))
235 { 238 {
236 case E_SSID: 239 case E_SSID:
237 memcpy(&(pbody.ssid),p+offset,2); offset += 2; 240 memcpy(&(pbody.ssid),p+offset,2); offset += 2;
238 if (pbody.ssid.length > 0) 241 if (pbody.ssid.length > 0)
239 { 242 {
240 memcpy(&(pbody.ssid.ssid),p+offset,pbody.ssid.length); offset += pbody.ssid.length; 243 memcpy(&(pbody.ssid.ssid),p+offset,pbody.ssid.length);
244 offset += pbody.ssid.length;
241 pbody.ssid.ssid[pbody.ssid.length]='\0'; 245 pbody.ssid.ssid[pbody.ssid.length]='\0';
242 if (strcmp((char *)pbody.ssid.ssid,"")==0) 246 if (strcmp((char *)pbody.ssid.ssid,"")==0)
243 memcpy(ppinfo->ssid, NONBROADCASTING, sizeof(ppinfo->ssid)); 247 memcpy(ppinfo->ssid, NONBROADCASTING, sizeof(ppinfo->ssid));
244 else 248 else
245 memcpy(ppinfo->ssid, pbody.ssid.ssid, sizeof(ppinfo->ssid)); 249 memcpy(ppinfo->ssid, pbody.ssid.ssid, sizeof(ppinfo->ssid));
246 ppinfo->ssid_len = pbody.ssid.length; 250 ppinfo->ssid_len = pbody.ssid.length;
247 } 251 }
248 break; 252 break;
249 253
250 case E_CHALLENGE: 254 case E_CHALLENGE:
251 memcpy(&(pbody.challenge),p+offset,2); offset += 2; 255 memcpy(&(pbody.challenge),p+offset,2); offset += 2;
252 if (pbody.challenge.length > 0) 256 if (pbody.challenge.length > 0)
253 { 257 {
254 memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length); offset += pbody.challenge.length; 258 memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length);
259 offset += pbody.challenge.length;
255 pbody.challenge.text[pbody.challenge.length]='\0'; 260 pbody.challenge.text[pbody.challenge.length]='\0';
256 } 261 }
257 break; 262 break;
258 case E_RATES: 263 case E_RATES:
259 memcpy(&(pbody.rates),p+offset,2); offset += 2; 264 memcpy(&(pbody.rates),p+offset,2); offset += 2;
260 if (pbody.rates.length > 0) 265 if (pbody.rates.length > 0)
261 { 266 {
262 memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length); offset += pbody.rates.length; 267 memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length);
268 offset += pbody.rates.length;
263 } 269 }
264 break; 270 break;
265 case E_DS: 271 case E_DS:
266 memcpy(&(pbody.ds),p+offset,3); offset +=3; 272 memcpy(&(pbody.ds),p+offset,3);
273 offset +=3;
267 ppinfo->channel = pbody.ds.channel; 274 ppinfo->channel = pbody.ds.channel;
268 break; 275 break;
269 case E_CF: 276 case E_CF:
270 memcpy(&(pbody.cf),p+offset,8); offset +=8; 277 memcpy(&(pbody.cf),p+offset,8);
278 offset +=8;
271 break; 279 break;
272 case E_TIM: 280 case E_TIM:
273 memcpy(&(pbody.tim),p+offset,2); offset +=2; 281 memcpy(&(pbody.tim),p+offset,2);
274 memcpy(&(pbody.tim.count),p+offset,3); offset +=3; 282 offset +=2;
283 memcpy(&(pbody.tim.count),p+offset,3);
284 offset +=3;
275 if ((pbody.tim.length -3) > 0) 285 if ((pbody.tim.length -3) > 0)
276 { 286 {
277 memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3)); 287 memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3));
278 offset += pbody.tim.length -3; 288 offset += pbody.tim.length -3;
279 } 289 }
280 break; 290 break;
281 default: 291 default:
282 292
283 offset+= *(p+offset+1) + 2; 293 offset+= *(p+offset+1) + 2;
284 break; 294 break;
285 } /* end of switch*/ 295 } /* end of switch*/
286 } /* end of for loop */ 296 } /* end of for loop */
287 return 0; 297 return 0;
288 298
289} /* End of handle_beacon */ 299} /* End of handle_beacon */
290 300
291 301
292int GetHeaderLength(u_int16_t fc) 302int GetHeaderLength(u_int16_t fc)
293{ 303{
294 int iLength=0; 304 int iLength=0;
295 305
296 switch (FC_TYPE(fc)) 306 switch (FC_TYPE(fc))
297 { 307 {
298 case T_MGMT: 308 case T_MGMT:
299 iLength = MGMT_HEADER_LEN; 309 iLength = MGMT_HEADER_LEN;
300 break; 310 break;
301 case T_CTRL: 311 case T_CTRL:
302 switch (FC_SUBTYPE(fc)) 312 switch (FC_SUBTYPE(fc))
303 { 313 {
304 case CTRL_PS_POLL: 314 case CTRL_PS_POLL:
305 iLength = CTRL_PS_POLL_LEN; 315 iLength = CTRL_PS_POLL_LEN;
306 break; 316 break;
307 case CTRL_RTS: 317 case CTRL_RTS:
308 iLength = CTRL_RTS_LEN; 318 iLength = CTRL_RTS_LEN;
309 break; 319 break;
310 case CTRL_CTS: 320 case CTRL_CTS:
311 iLength = CTRL_CTS_LEN; 321 iLength = CTRL_CTS_LEN;
312 break; 322 break;
313 case CTRL_ACK: 323 case CTRL_ACK:
314 iLength = CTRL_ACK_LEN; 324 iLength = CTRL_ACK_LEN;
315 break; 325 break;
316 case CTRL_CF_END: 326 case CTRL_CF_END:
317 iLength = CTRL_END_LEN; 327 iLength = CTRL_END_LEN;
318 break; 328 break;
319 case CTRL_END_ACK: 329 case CTRL_END_ACK:
320 iLength = CTRL_END_ACK_LEN; 330 iLength = CTRL_END_ACK_LEN;
321 break; 331 break;
322 default: 332 default:
323 iLength = 0; 333 iLength = 0;
324 break; 334 break;
325 } 335 }
326 break; 336 break;
327 case T_DATA: 337 case T_DATA:
328 if (FC_TO_DS(fc) && FC_FROM_DS(fc)) 338 if (FC_TO_DS(fc) && FC_FROM_DS(fc))
329 iLength = 30; 339 iLength = 30;
330 else 340 else
331 iLength = 24; 341 iLength = 24;
332 break; 342 break;
333 default: 343 default:
334 wl_logerr("unknown IEEE802.11 frame type (%d)", FC_TYPE(fc)); 344 wl_logerr("unknown IEEE802.11 frame type (%d)", FC_TYPE(fc));
335 break; 345 break;
336 } 346 }
337 return iLength; 347 return iLength;
338} 348}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
index 0630d04..664ba92 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
@@ -1,127 +1,128 @@
1/* 1/*
2 * Communication protocol 2 * Communication protocol
3 * 3 *
4 * $Id$ 4 * $Id$
5 */ 5 */
6 6
7#include "wl_types.hh"
7#include "wl_proto.hh" 8#include "wl_proto.hh"
8#include "wl_log.hh" 9#include "wl_log.hh"
9#include "wl_sock.hh" 10#include "wl_sock.hh"
10 11
11/* Adds a field to the buffer */ 12/* Adds a field to the buffer */
12int add_field(char *buffer, const char *string, int len) 13int add_field(char *buffer, const char *string, int len)
13{ 14{
14 char newlen[5]; 15 char newlen[5];
15 16
16 /* 3 Byte = Length */ 17 /* 3 Byte = Length */
17 snprintf(newlen, sizeof(newlen) - 1, "%.3d", len); 18 snprintf(newlen, sizeof(newlen) - 1, "%.3d", len);
18 memcpy(buffer, newlen, 3); 19 memcpy(buffer, newlen, 3);
19 20
20 /* Length bytes = Value */ 21 /* Length bytes = Value */
21 memcpy(buffer + 3, string, atoi(newlen)); 22 memcpy(buffer + 3, string, atoi(newlen));
22 23
23 /* Return length of attached field */ 24 /* Return length of attached field */
24 return (atoi(newlen) + 3); 25 return (atoi(newlen) + 3);
25} 26}
26 27
27int get_field(const char *buffer, char *out, int maxlen) 28int get_field(const char *buffer, char *out, int maxlen)
28{ 29{
29 char len[5]; 30 char len[5];
30 31
31 /* Get length of value */ 32 /* Get length of value */
32 memcpy(len, buffer, 3); 33 memcpy(len, buffer, 3);
33 34
34 /* Copy buffer to out pointer */ 35 /* Copy buffer to out pointer */
35 memset(out, 0, maxlen); 36 memset(out, 0, maxlen);
36 37
37 if(atoi(len)-3 > maxlen -1) 38 if(atoi(len) > maxlen -1)
38 memcpy(out, buffer + 3, maxlen - 1); 39 memcpy(out, buffer + 3, maxlen - 1);
39 else 40 else
40 memcpy(out, buffer + 3, atoi(len)); 41 memcpy(out, buffer + 3, atoi(len));
41 42
42 /* Return length of whole field (including 3 byte length) */ 43 /* Return length of whole field (including 3 byte length) */
43 return (atoi(len) + 3); 44 return (atoi(len) + 3);
44} 45}
45 46
46/* Send found network to UI */ 47/* Send found network to UI */
47int send_network_found (const char *guihost, int guiport, void *structure) 48int send_network_found (const char *guihost, int guiport, void *structure)
48{ 49{
49 wl_network_t *ptr; 50 wl_network_t *ptr;
50 char buffer[2048], temp[5]; 51 char buffer[2048], temp[5];
51 unsigned int len = 0; 52 unsigned int len = 0;
52 53
53 ptr = (wl_network_t *)structure; 54 ptr = (wl_network_t *)structure;
54 55
55 /* Type = Found new net (without length field) */ 56 /* Type = Found new net (without length field) */
56 memset(temp, 0, sizeof(temp)); 57 memset(temp, 0, sizeof(temp));
57 snprintf(temp, sizeof(temp), "%.2d", NETFOUND); 58 snprintf(temp, sizeof(temp), "%.2d", WL_NETFOUND);
58 memcpy(buffer, temp, 2); 59 memcpy(buffer, temp, 2);
59 len += 2; 60 len += 2;
60 61
61 /* Set Net-type */ 62 /* Set Net-type */
62 memset(temp, 0, sizeof(temp)); 63 memset(temp, 0, sizeof(temp));
63 snprintf(temp, sizeof(temp), "%d", ptr->net_type); 64 snprintf(temp, sizeof(temp), "%d", ptr->net_type);
64 len += add_field(buffer + len, temp, 1); 65 len += add_field(buffer + len, temp, 1);
65 66
66 /* Set channel */ 67 /* Set channel */
67 memset(temp, 0, sizeof(temp)); 68 memset(temp, 0, sizeof(temp));
68 snprintf(temp, sizeof(temp), "%.2d", ptr->channel); 69 snprintf(temp, sizeof(temp), "%.2d", ptr->channel);
69 len += add_field(buffer + len, temp, 2); 70 len += add_field(buffer + len, temp, 2);
70 71
71 /* Set WEP y/n */ 72 /* Set WEP y/n */
72 memset(temp, 0, sizeof(temp)); 73 memset(temp, 0, sizeof(temp));
73 snprintf(temp, sizeof(temp), "%d", ptr->wep); 74 snprintf(temp, sizeof(temp), "%d", ptr->wep);
74 len += add_field(buffer + len, temp, 1); 75 len += add_field(buffer + len, temp, 1);
75 76
76 /* Set Mac */ 77 /* Set Mac */
77 len += add_field(buffer + len, ptr->mac, 17); 78 len += add_field(buffer + len, ptr->mac, 17);
78 79
79 /* Set ssid */ 80 /* Set ssid */
80 if(len + ptr->ssid_len < sizeof(buffer) - 1) 81 if(len + ptr->ssid_len < sizeof(buffer) - 1)
81 len += add_field(buffer + len, ptr->bssid, ptr->ssid_len); 82 len += add_field(buffer + len, ptr->bssid, ptr->ssid_len);
82 else 83 else
83 len += add_field(buffer + len, ptr->bssid, sizeof(buffer) - len - 1); 84 len += add_field(buffer + len, ptr->bssid, sizeof(buffer) - len - 1);
84 85
85 /* Send prepared buffer to UI */ 86 /* Send prepared buffer to UI */
86#ifdef DEBUG 87#ifdef DEBUG
87 wl_loginfo("Sending network to UI: '%s'", buffer); 88 wl_loginfo("Sending network to UI: '%s'", buffer);
88#endif 89#endif
89 90
90 return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1); 91 return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1);
91} 92}
92 93
93/* Fill buffer into structur */ 94/* Fill buffer into structur */
94int get_network_found (void *structure, const char *buffer) 95int get_network_found (void *structure, const char *buffer)
95{ 96{
96 wl_network_t *ptr; 97 wl_network_t *ptr;
97 char temp[5]; 98 char temp[5];
98 unsigned int len = 0; 99 unsigned int len = 0;
99 100
100 ptr = (wl_network_t *)structure; 101 ptr = (wl_network_t *)structure;
101 102
102 /* packet type already determined, skip check */ 103 /* packet type already determined, skip check */
103 len += 2; 104 len += 2;
104 105
105 /* Get net type (accesspoint || ad-hoc || ...) */ 106 /* Get net type (accesspoint || ad-hoc || ...) */
106 memset(temp, 0, sizeof(temp)); 107 memset(temp, 0, sizeof(temp));
107 len += get_field(buffer + len, temp, sizeof(temp)); 108 len += get_field(buffer + len, temp, sizeof(temp));
108 ptr->net_type = atoi(temp); 109 ptr->net_type = atoi(temp);
109 110
110 /* Get channel */ 111 /* Get channel */
111 memset(temp, 0, sizeof(temp)); 112 memset(temp, 0, sizeof(temp));
112 len += get_field(buffer + len, temp, sizeof(temp)); 113 len += get_field(buffer + len, temp, sizeof(temp));
113 ptr->channel = atoi(temp); 114 ptr->channel = atoi(temp);
114 115
115 /* Set WEP y/n */ 116 /* Set WEP y/n */
116 memset(temp, 0, sizeof(temp)); 117 memset(temp, 0, sizeof(temp));
117 len += get_field(buffer + len, temp, sizeof(temp)); 118 len += get_field(buffer + len, temp, sizeof(temp));
118 ptr->wep = atoi(temp); 119 ptr->wep = atoi(temp);
119 120
120 /* Set MAC address */ 121 /* Set MAC address */
121 len += get_field(buffer + len, ptr->mac, sizeof(ptr->mac)); 122 len += get_field(buffer + len, ptr->mac, sizeof(ptr->mac));
122 123
123 /* Set BSSID */ 124 /* Set BSSID */
124 len += get_field(buffer + len, ptr->bssid, sizeof(ptr->bssid)); 125 len += get_field(buffer + len, ptr->bssid, sizeof(ptr->bssid));
125 126
126 return 1; 127 return 1;
127} 128}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
index f645f58..46ef8e5 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
@@ -1,30 +1,21 @@
1/* $Id$ */ 1/* $Id$ */
2 2
3#ifndef WLPROTO_HH 3#ifndef WLPROTO_HH
4#define WLPROTO_HH 4#define WLPROTO_HH
5 5
6#include <stdio.h> 6#include <stdio.h>
7#include <string.h> 7#include <string.h>
8#include <stdlib.h> 8#include <stdlib.h>
9 9
10/* Type definitions, to be continued */ 10/* Type definitions, to be continued */
11#define NETFOUND 01 11#define NETFOUND 01
12#define NETLOST 02 12#define NETLOST 02
13#define STARTSNIFF 98 13#define STARTSNIFF 98
14#define STOPSNIFF 99 14#define STOPSNIFF 99
15 15
16int add_field(char *, const char *, int); 16int add_field(char *, const char *, int);
17int get_field(const char *, char *, int); 17int get_field(const char *, char *, int);
18int send_network_found (const char *, int, void *); 18int send_network_found (const char *, int, void *);
19int get_network_found (void *, const char *); 19int get_network_found (void *, const char *);
20 20
21typedef struct {
22 int net_type; /* 1 = Accesspoint ; 2 = Ad-Hoc */
23 int ssid_len; /* Length of SSID */
24 int channel; /* Channel */
25 int wep; /* 1 = WEP enabled ; 0 = disabled */
26 char mac[64]; /* MAC address of Accesspoint */
27 char bssid[128]; /* BSSID of Net */
28} wl_network_t;
29
30#endif /* WLPROTO_HH */ 21#endif /* WLPROTO_HH */
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
index db6b7b8..6e26cb8 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
@@ -1,91 +1,92 @@
1/* 1/*
2 * Socket operations for wellenreiter 2 * Socket operations for wellenreiter
3 * 3 *
4 * $Id$ 4 * $Id$
5 */ 5 */
6 6
7#include "wl_sock.hh" 7#include "wl_sock.hh"
8#include "wl_log.hh" 8#include "wl_log.hh"
9 9
10/* Setup UDP Socket for incoming commands */ 10/* Setup UDP Socket for incoming commands */
11int wl_setupsock(const char *host, int port) 11int wl_setupsock(const char *host, int port, struct sockaddr_in saddr)
12{ 12{
13 struct sockaddr_in saddr;
14 int sock; 13 int sock;
15 14
16 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 15 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
17 { 16 {
18 wl_logerr("Cannot set up socket: %s", strerror(errno)); 17 wl_logerr("Cannot set up socket: %s", strerror(errno));
19 return -1; 18 return -1;
20 } 19 }
21 20
22 memset(&saddr, 0, sizeof(saddr)); 21 memset(&saddr, 0, sizeof(saddr));
23 saddr.sin_family = PF_INET; 22 saddr.sin_family = PF_INET;
24 saddr.sin_port = htons(port); 23 saddr.sin_port = htons(port);
25 saddr.sin_addr.s_addr = inet_addr(host); 24 saddr.sin_addr.s_addr = inet_addr(host);
26 25
27 if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0) 26 if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0)
28 { 27 {
29 wl_logerr("Cannot bind socket: %s", strerror(errno)); 28 wl_logerr("Cannot bind socket: %s", strerror(errno));
30 close(sock); 29 close(sock);
31 return -1; 30 return -1;
32 } 31 }
33 32
34 return sock; 33 return sock;
35} 34}
36 35
37/* Send a string to commsock */ 36/* Send a string to commsock */
38int wl_send(const char *host, int port, const char *string, ...) 37int wl_send(const char *host, int port, const char *string, ...)
39{ 38{
40 int sock; 39 int sock;
41 char buffer[4096]; 40 char buffer[4096];
42 struct sockaddr_in saddr; 41 struct sockaddr_in saddr;
43 va_list ap; 42 va_list ap;
44 43
45 /* Generate string */ 44 /* Generate string */
46 memset(buffer, 0, sizeof(buffer)); 45 memset(buffer, 0, sizeof(buffer));
47 va_start(ap, string); 46 va_start(ap, string);
48 vsnprintf(buffer, sizeof(buffer)-1, string, ap); 47 vsnprintf(buffer, sizeof(buffer)-1, string, ap);
49 va_end(ap); 48 va_end(ap);
50 49
51 saddr.sin_family = AF_INET; 50 saddr.sin_family = AF_INET;
52 saddr.sin_port = htons(port); 51 saddr.sin_port = htons(port);
53 saddr.sin_addr.s_addr = inet_addr(host); 52 saddr.sin_addr.s_addr = inet_addr(host);
54 53
55 /* Setup socket */ 54 /* Setup socket */
56 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 55 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
57 { 56 {
58 wl_logerr("Cannot set up socket: %s", strerror(errno)); 57 wl_logerr("Cannot set up socket: %s", strerror(errno));
59 return 0; 58 return 0;
60 } 59 }
61 60
62 if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) 61 if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
63 { 62 {
64 wl_logerr("Cannot write to socket: %s", strerror(errno)); 63 wl_logerr("Cannot write to socket: %s", strerror(errno));
65 close(sock); 64 close(sock);
66 return 0; 65 return 0;
67 } 66 }
68 67
69 if(close(sock) < 0) 68 if(close(sock) < 0)
70 wl_logerr("Cannot close socket: %s", strerror(errno)); 69 wl_logerr("Cannot close socket: %s", strerror(errno));
71 70
72 return 1; 71 return 1;
73} 72}
74 73
75/* Check for new messages on commsock */ 74/* Check for new messages on commsock */
76int wl_recv(int *sock, char *out, int maxlen) 75int wl_recv(int *sock, struct sockaddr_in cliaddr, char *out, int maxlen)
77{ 76{
78 struct sockaddr_in *cliaddr;
79 socklen_t len = sizeof(struct sockaddr); 77 socklen_t len = sizeof(struct sockaddr);
80 char retval[3]; 78 char retval[3];
81 79
82 memset(out, 0, maxlen); 80 memset(out, 0, maxlen);
83 if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)cliaddr, &len) < 0) 81 if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)&cliaddr, &len) < 0)
82 {
83 wl_logerr("Cannot receive from socket: %s", strerror(errno));
84 return -1; 84 return -1;
85 }
85 86
86 /* Get packet type and return it */ 87 /* Get packet type and return it */
87 memset(retval, 0, sizeof(retval)); 88 memset(retval, 0, sizeof(retval));
88 memcpy(retval, out, 2); 89 memcpy(retval, out, 2);
89 90
90 return atoi(retval); 91 return atoi(retval);
91} 92}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
index 6ddbaef..e7d9747 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
@@ -1,20 +1,21 @@
1/* $id */ 1/* $Id */
2 2
3#ifndef WLSOCK_HH 3#ifndef WLSOCK_HH
4#define WLSOCK_HH 4#define WLSOCK_HH
5 5
6#include <stdarg.h>
6#include <sys/types.h> 7#include <sys/types.h>
7#include <sys/socket.h> 8#include <sys/socket.h>
8#include <netinet/in.h> 9#include <netinet/in.h>
9#include <arpa/inet.h> 10#include <arpa/inet.h>
10#include <signal.h> 11#include <signal.h>
11#include <string.h> 12#include <string.h>
12#include <unistd.h> 13#include <unistd.h>
13#include <stdlib.h> 14#include <stdlib.h>
14#include <errno.h> 15#include <errno.h>
15 16
16int wl_setupsock(const char *, int); 17int wl_setupsock(const char *, int, struct sockaddr_in);
17int wl_send(const char *, int, const char *, ...); 18int wl_send(const char *, int, const char *, ...);
18int wl_recv(int *, char *, int); 19int wl_recv(int *, struct sockaddr_in, char *, int);
19 20
20#endif /* WLSOCK_HH */ 21#endif /* WLSOCK_HH */