f18ba98810a4bba628920170dc8f59428c4e1ef9
[my-code/hdw-sniff.git] / parse.c
1 /*
2  * parse.c - parsing of pcap packages
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include "parse.h"
9 #include "main.h"
10
11 /* all the parsing stuff will go here
12  *
13  * different protocols should get to seperated files though ...
14  */
15
16 int switch_active_state(char *state) {
17
18   switch(*state) {
19     case '-':
20       *state='\\';
21       break;
22     case '\\':
23       *state='|';
24       break;
25     case '|':
26       *state='/';
27       break;
28     default:
29       *state='-';
30       break;
31   }
32
33   return 23;
34 }
35
36 void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package) {
37
38   t_info *info;
39   int i;
40   t_sta new_sta;
41   t_sta *sta;
42   //t_frame4_hdr *f4hdr;
43   t_frame3_hdr *f3hdr;
44   //t_frame2_hdr *f2hdr;
45   //t_frame1_hdr *f1hdr;
46   t_beacon_fb *beacon_fb;
47   int ret;
48   char string[MESSAGE_MAX];
49
50   info=(t_info *)ptr;
51
52   info->count++;
53
54   memset(&new_sta,0,sizeof(t_sta));
55
56   if(info->dump_fd!=0) {
57     ret=write(info->dump_fd,pcap_header,sizeof(struct pcap_pkthdr));
58     if(ret!=sizeof(struct pcap_pkthdr))
59       display_console(info,"warning, pcap header write failed!");
60     ret=write(info->dump_fd,package,pcap_header->caplen);
61     if(ret!=pcap_header->caplen)
62       display_console(info,"warning, package write failed!");
63   }
64   
65   /* maybe there is offset to the actual ieee802.11 frame,
66      for example prism header ...
67      in that case, hack the source! */
68
69   /* management */
70   if(FCTL_TYPE(package[0])==FCTL_TYPE_MGMT) {
71     info->count_m++;
72
73     /* beacon frames */
74     if(FCTL_STYPE(package[0])==FCTL_STYPE_BEACON) {
75       f3hdr=(t_frame3_hdr *)package;
76       beacon_fb=(t_beacon_fb *)(package+sizeof(t_frame3_hdr));
77       // check sta
78       memcpy(new_sta.addr,f3hdr->addr2,ADDR_LEN);
79       ret=list_search_data(&(info->sniffed_sta),&new_sta,ADDR_LEN);
80       if((ret==L_EMPTY_LIST)|(ret==L_NO_SUCH_ELEMENT)) {
81         list_add_element(&(info->sniffed_sta),&new_sta,sizeof(t_sta));
82         sta=(t_sta *)info->sniffed_sta.current->data;
83       }
84       else sta=(t_sta *)info->sniffed_sta.current->data;
85       // fill in stuff ...
86       memcpy(sta->ssid,beacon_fb->ssid,beacon_fb->ssid_length);
87       if((CAP_INFO_ESS(beacon_fb->cap_info))&
88          (CAP_INFO_IBSS(beacon_fb->cap_info)==0)) sta->ap=AP;
89       if(CAP_INFO_PRIVACY(beacon_fb->cap_info)) sta->wep=WEP;
90       sta->count_mgmt++;
91       switch_active_state(&(sta->active));
92       strncpy(string,"last: beacon, source: ",MESSAGE_MAX);
93       for(i=0;i<ADDR_LEN;i++)
94         snprintf(&string[22+3*i],4,"%02x%c",sta->addr[i],
95                  (i==ADDR_LEN-1)?'.':':');
96       string[22+3*ADDR_LEN+1]=0;
97       display_console(info,string);
98     }
99
100   }
101
102   /* control */
103   else if(FCTL_TYPE(package[0])==FCTL_TYPE_CTRL) {
104     info->count_c++;
105     display_console(info,"last: got control frame");
106   }
107
108   /* data */
109   else if(FCTL_TYPE(package[0])==FCTL_TYPE_DATA) {
110     info->count_d++;
111     display_console(info,"last: got data frame");
112   }
113
114
115 }