author | mjm <mjm> | 2002-11-23 21:42:41 (UTC) |
---|---|---|
committer | mjm <mjm> | 2002-11-23 21:42:41 (UTC) |
commit | c93ded2c1256817b9c974c792cd143315b98fff7 (patch) (unidiff) | |
tree | 7de5b3fc8eee930f72c4e3ef662f48948a60d935 /noncore/net/wellenreiter/daemon/source/sniffer.cc | |
parent | 4e24ece4607d3b2f9e3252fa561fabaa9cdddd63 (diff) | |
download | opie-c93ded2c1256817b9c974c792cd143315b98fff7.zip opie-c93ded2c1256817b9c974c792cd143315b98fff7.tar.gz opie-c93ded2c1256817b9c974c792cd143315b98fff7.tar.bz2 |
implemented sniffer function in daemon.cc
Diffstat (limited to 'noncore/net/wellenreiter/daemon/source/sniffer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/daemon/source/sniffer.cc | 41 |
1 files changed, 1 insertions, 40 deletions
diff --git a/noncore/net/wellenreiter/daemon/source/sniffer.cc b/noncore/net/wellenreiter/daemon/source/sniffer.cc index be64d67..66d5b6f 100644 --- a/noncore/net/wellenreiter/daemon/source/sniffer.cc +++ b/noncore/net/wellenreiter/daemon/source/sniffer.cc | |||
@@ -1,101 +1,62 @@ | |||
1 | /* | 1 | /* |
2 | * rfmon mode sniffer | 2 | * rfmon mode sniffer |
3 | * This works only with cisco wireless cards with an rfmon | 3 | * This works only with cisco wireless cards with an rfmon |
4 | * able driver and not with wifi stuff. | 4 | * able driver and not with wifi stuff. |
5 | * | 5 | * |
6 | * $Id$ | 6 | * $Id$ |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "config.hh" | 9 | #include "config.hh" |
10 | #include "cardmode.hh" | 10 | #include "cardmode.hh" |
11 | #include "sniffer.hh" | 11 | #include "sniffer.hh" |
12 | #include "ieee802_11.hh" | 12 | #include "ieee802_11.hh" |
13 | #include "extract.hh" | 13 | #include "extract.hh" |
14 | 14 | ||
15 | int main(void) | ||
16 | { | ||
17 | if(card_into_monitormode (SNIFFER_DEVICE, CARD_TYPE_NG) < 0) | ||
18 | return 0; | ||
19 | start_sniffing (SNIFFER_DEVICE); | ||
20 | |||
21 | return 1; | ||
22 | } | ||
23 | |||
24 | int start_sniffing (char * device) | ||
25 | { | ||
26 | |||
27 | pcap_t *handletopcap; /* The handle to the libpcap */ | ||
28 | char errbuf[PCAP_ERRBUF_SIZE]; /* The errorbuffer of libpacap */ | ||
29 | struct pcap_pkthdr header; /* The packet header from pcap*/ | ||
30 | const u_char *packet; /* The actual packet content*/ | ||
31 | |||
32 | /* opening the pcap for sniffing */ | ||
33 | handletopcap = pcap_open_live(device, BUFSIZ, 1, 1000, errbuf); | ||
34 | |||
35 | #ifdef HAVE_PCAP_NONBLOCK | ||
36 | pcap_setnonblock(handletopcap, 1, errstr); | ||
37 | #endif | ||
38 | /*start scanning */ | ||
39 | //pcap_loop(handletopcap,-1,process_packets,NULL); | ||
40 | /* Loope endless */ | ||
41 | while(1) | ||
42 | { | ||
43 | /* Grab one single packet */ | ||
44 | packet = pcap_next(handletopcap, &header); | ||
45 | |||
46 | /* process the packet */ | ||
47 | process_packets(NULL,&header,*&packet); | ||
48 | } | ||
49 | |||
50 | printf("\nDone processing packets... wheew!\n"); | ||
51 | return 1; | ||
52 | } | ||
53 | |||
54 | void process_packets(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char* packet) | 15 | void process_packets(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char* packet) |
55 | { | 16 | { |
56 | u_int caplen = pkthdr->caplen; | 17 | u_int caplen = pkthdr->caplen; |
57 | u_int length = pkthdr->len; | 18 | u_int length = pkthdr->len; |
58 | u_int16_t fc; | 19 | u_int16_t fc; |
59 | u_int HEADER_LENGTH; | 20 | u_int HEADER_LENGTH; |
60 | 21 | ||
61 | /* pinfo holds all interresting information for us */ | 22 | /* pinfo holds all interresting information for us */ |
62 | struct packetinfo pinfo; | 23 | struct packetinfo pinfo; |
63 | struct packetinfo *pinfoptr; | 24 | struct packetinfo *pinfoptr; |
64 | pinfoptr=&pinfo; | 25 | pinfoptr=&pinfo; |
65 | 26 | ||
66 | pinfoptr->isvalid = 0; | 27 | pinfoptr->isvalid = 0; |
67 | pinfoptr->pktlen = pkthdr->len; | 28 | pinfoptr->pktlen = pkthdr->len; |
68 | 29 | ||
69 | if (caplen < IEEE802_11_FC_LEN) | 30 | if (caplen < IEEE802_11_FC_LEN) |
70 | { | 31 | { |
71 | /* This is a garbage packet, because is does not long enough | 32 | /* This is a garbage packet, because is does not long enough |
72 | to hold a 802.11b header */ | 33 | to hold a 802.11b header */ |
73 | pinfoptr->isvalid = 0; | 34 | pinfoptr->isvalid = 0; |
74 | return; | 35 | return; |
75 | } | 36 | } |
76 | 37 | ||
77 | /* Gets the framecontrol bits (2bytes long) */ | 38 | /* Gets the framecontrol bits (2bytes long) */ |
78 | fc = EXTRACT_LE_16BITS(packet); | 39 | fc = EXTRACT_LE_16BITS(packet); |
79 | 40 | ||
80 | HEADER_LENGTH = GetHeaderLength(fc); | 41 | HEADER_LENGTH = GetHeaderLength(fc); |
81 | 42 | ||
82 | if (caplen < HEADER_LENGTH) | 43 | if (caplen < HEADER_LENGTH) |
83 | { | 44 | { |
84 | /* This is a garbage packet, because it is not long enough | 45 | /* This is a garbage packet, because it is not long enough |
85 | to hold a correct header of its type */ | 46 | to hold a correct header of its type */ |
86 | pinfoptr->isvalid = 0; | 47 | pinfoptr->isvalid = 0; |
87 | return; | 48 | return; |
88 | } | 49 | } |
89 | 50 | ||
90 | /* Decode 802.11b header out of the packet */ | 51 | /* Decode 802.11b header out of the packet */ |
91 | if (decode_80211b_hdr(packet,pinfoptr) == 0) | 52 | if (decode_80211b_hdr(packet,pinfoptr) == 0) |
92 | { | 53 | { |
93 | /* Justification of the ofset to further process the packet */ | 54 | /* Justification of the ofset to further process the packet */ |
94 | length -= HEADER_LENGTH; | 55 | length -= HEADER_LENGTH; |
95 | caplen -= HEADER_LENGTH; | 56 | caplen -= HEADER_LENGTH; |
96 | packet += HEADER_LENGTH; | 57 | packet += HEADER_LENGTH; |
97 | } | 58 | } |
98 | else | 59 | else |
99 | { /* Something is wrong,could not be a correct packet */ | 60 | { /* Something is wrong,could not be a correct packet */ |
100 | return; | 61 | return; |
101 | } | 62 | } |
@@ -253,95 +214,95 @@ int handle_beacon(u_int16_t fc, const u_char *p,struct packetinfo *ppinfo) | |||
253 | { | 214 | { |
254 | ppinfo->ssid = (char *)pbody.ssid.ssid; | 215 | ppinfo->ssid = (char *)pbody.ssid.ssid; |
255 | } | 216 | } |
256 | ppinfo->ssid_len = pbody.ssid.length; | 217 | ppinfo->ssid_len = pbody.ssid.length; |
257 | } | 218 | } |
258 | break; | 219 | break; |
259 | 220 | ||
260 | case E_CHALLENGE: | 221 | case E_CHALLENGE: |
261 | memcpy(&(pbody.challenge),p+offset,2); offset += 2; | 222 | memcpy(&(pbody.challenge),p+offset,2); offset += 2; |
262 | if (pbody.challenge.length > 0) | 223 | if (pbody.challenge.length > 0) |
263 | { | 224 | { |
264 | memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length); offset += pbody.challenge.length; | 225 | memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length); offset += pbody.challenge.length; |
265 | pbody.challenge.text[pbody.challenge.length]='\0'; | 226 | pbody.challenge.text[pbody.challenge.length]='\0'; |
266 | } | 227 | } |
267 | break; | 228 | break; |
268 | case E_RATES: | 229 | case E_RATES: |
269 | memcpy(&(pbody.rates),p+offset,2); offset += 2; | 230 | memcpy(&(pbody.rates),p+offset,2); offset += 2; |
270 | if (pbody.rates.length > 0) { | 231 | if (pbody.rates.length > 0) { |
271 | memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length); offset += pbody.rates.length; | 232 | memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length); offset += pbody.rates.length; |
272 | } | 233 | } |
273 | break; | 234 | break; |
274 | case E_DS: | 235 | case E_DS: |
275 | memcpy(&(pbody.ds),p+offset,3); offset +=3; | 236 | memcpy(&(pbody.ds),p+offset,3); offset +=3; |
276 | ppinfo->channel = pbody.ds.channel; | 237 | ppinfo->channel = pbody.ds.channel; |
277 | break; | 238 | break; |
278 | case E_CF: | 239 | case E_CF: |
279 | memcpy(&(pbody.cf),p+offset,8); offset +=8; | 240 | memcpy(&(pbody.cf),p+offset,8); offset +=8; |
280 | break; | 241 | break; |
281 | case E_TIM: | 242 | case E_TIM: |
282 | memcpy(&(pbody.tim),p+offset,2); offset +=2; | 243 | memcpy(&(pbody.tim),p+offset,2); offset +=2; |
283 | memcpy(&(pbody.tim.count),p+offset,3); offset +=3; | 244 | memcpy(&(pbody.tim.count),p+offset,3); offset +=3; |
284 | if ((pbody.tim.length -3) > 0) | 245 | if ((pbody.tim.length -3) > 0) |
285 | { | 246 | { |
286 | memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3)); | 247 | memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3)); |
287 | offset += pbody.tim.length -3; | 248 | offset += pbody.tim.length -3; |
288 | } | 249 | } |
289 | break; | 250 | break; |
290 | default: | 251 | default: |
291 | 252 | ||
292 | offset+= *(p+offset+1) + 2; | 253 | offset+= *(p+offset+1) + 2; |
293 | break; | 254 | break; |
294 | } /* end of switch*/ | 255 | } /* end of switch*/ |
295 | } /* end of for loop */ | 256 | } /* end of for loop */ |
296 | return 0; | 257 | return 0; |
297 | 258 | ||
298 | } /* End of handle_beacon */ | 259 | } /* End of handle_beacon */ |
299 | 260 | ||
300 | 261 | ||
301 | static int GetHeaderLength(u_int16_t fc) | 262 | int GetHeaderLength(u_int16_t fc) |
302 | { | 263 | { |
303 | int iLength=0; | 264 | int iLength=0; |
304 | 265 | ||
305 | switch (FC_TYPE(fc)) { | 266 | switch (FC_TYPE(fc)) { |
306 | case T_MGMT: | 267 | case T_MGMT: |
307 | iLength = MGMT_HEADER_LEN; | 268 | iLength = MGMT_HEADER_LEN; |
308 | break; | 269 | break; |
309 | case T_CTRL: | 270 | case T_CTRL: |
310 | switch (FC_SUBTYPE(fc)) { | 271 | switch (FC_SUBTYPE(fc)) { |
311 | case CTRL_PS_POLL: | 272 | case CTRL_PS_POLL: |
312 | iLength = CTRL_PS_POLL_LEN; | 273 | iLength = CTRL_PS_POLL_LEN; |
313 | break; | 274 | break; |
314 | case CTRL_RTS: | 275 | case CTRL_RTS: |
315 | iLength = CTRL_RTS_LEN; | 276 | iLength = CTRL_RTS_LEN; |
316 | break; | 277 | break; |
317 | case CTRL_CTS: | 278 | case CTRL_CTS: |
318 | iLength = CTRL_CTS_LEN; | 279 | iLength = CTRL_CTS_LEN; |
319 | break; | 280 | break; |
320 | case CTRL_ACK: | 281 | case CTRL_ACK: |
321 | iLength = CTRL_ACK_LEN; | 282 | iLength = CTRL_ACK_LEN; |
322 | break; | 283 | break; |
323 | case CTRL_CF_END: | 284 | case CTRL_CF_END: |
324 | iLength = CTRL_END_LEN; | 285 | iLength = CTRL_END_LEN; |
325 | break; | 286 | break; |
326 | case CTRL_END_ACK: | 287 | case CTRL_END_ACK: |
327 | iLength = CTRL_END_ACK_LEN; | 288 | iLength = CTRL_END_ACK_LEN; |
328 | break; | 289 | break; |
329 | default: | 290 | default: |
330 | iLength = 0; | 291 | iLength = 0; |
331 | break; | 292 | break; |
332 | } | 293 | } |
333 | break; | 294 | break; |
334 | case T_DATA: | 295 | case T_DATA: |
335 | if (FC_TO_DS(fc) && FC_FROM_DS(fc)) | 296 | if (FC_TO_DS(fc) && FC_FROM_DS(fc)) |
336 | iLength = 30; | 297 | iLength = 30; |
337 | else | 298 | else |
338 | iLength = 24; | 299 | iLength = 24; |
339 | break; | 300 | break; |
340 | default: | 301 | default: |
341 | printf("unknown IEEE802.11 frame type (%d)", | 302 | printf("unknown IEEE802.11 frame type (%d)", |
342 | FC_TYPE(fc)); | 303 | FC_TYPE(fc)); |
343 | break; | 304 | break; |
344 | } | 305 | } |
345 | 306 | ||
346 | return iLength; | 307 | return iLength; |
347 | } | 308 | } |