introduced prism wlan ng header members
[my-code/hdw-sniff.git] / hdw-sniff.c
1 /*
2  * hdw-sniff, sniffer using pcap lib
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <sys/socket.h>
11 #include <time.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <pcap.h>
15
16 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
17 #include <netinet/if_ether.h>  /* for ethhdr struct */
18 #include <netinet/ip.h> /* ip */
19 #include <netinet/in.h> /* in_addr , inet_ntoa */
20
21 /* IEEE 802.11 stuff -- will become one include later ... */
22 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
23 #include "ieee802_11.h" /* from pcmcia-cs */
24
25 #include "hdw-sniff.h" /* my functions */
26
27 int main(int argc, char *argv[]) {
28
29         char pcap_error[PCAP_ERRBUF_SIZE];
30         pcap_t *pcap_handle;
31         char sys_call[30];
32         FILE *logfile;
33         struct info_struct my_info_struct;
34         
35         /* parse the arguments */
36         if(argc<3) {
37                 printf("usage: %s <interface> <monitor mode> <logfile>\n",
38                         argv[0]);
39                 return 0;
40         }
41         if(argc!=4) {
42                 printf("no logfile specified, writing to stdout ...\n");
43         } 
44         else {
45                 if((logfile=fopen(argv[3],"w"))!=NULL) {
46                         printf("writing to logfile %s ...\n",argv[3]);
47                 }
48                 else {
49                         printf("can't open logfile!\n");
50                 }
51         }
52                         
53         /* setting up device and set monitor mode */
54         if(atoi(argv[2])==1) {
55                 printf("setting to monitor mode\n");
56                 if(strncmp(argv[1],"wlan",4)==0)
57                         sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
58                 if(strncmp(argv[1],"eth",3)==0)
59                         sprintf(sys_call,"ifconfig %s promisc",argv[1]);
60         system(sys_call);
61         }
62         printf("setting up interface\n");
63         sprintf(sys_call,"ifconfig %s up",argv[1]);
64         system(sys_call);
65
66         /* start pcap session */
67         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
68         if(pcap_handle==NULL) {
69                 printf("%s: %s\n",argv[0],pcap_error);
70                 return 1;
71         }
72         
73         /* grab a package until user breaks */
74         my_info_struct.count=0;
75         my_info_struct.mmode=argv[2][0];
76         strcpy(my_info_struct.dev,argv[1]);
77         /* do loopp */
78         pcap_loop(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
79         return 0;
80 }
81
82
83 /* pcap_process callback function */
84 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
85                         const u_char *package) {
86         
87         /* local variables */
88         struct linux_wlan_ng_prism_hdr *prism_hdr;
89         struct ieee802_11_hdr *w_hdr;
90         struct ip *ip_hdr;
91         struct ethhdr *e_hdr;
92         struct info_struct *my_info_struct;
93         int i;
94
95         my_info_struct=(struct info_struct *)info;
96         ++(my_info_struct->count);
97
98         printf("\n");
99         printf("---> package %d ---- %s",my_info_struct->count,
100                         ctime((const time_t*)&(pcap_header->ts.tv_sec)));
101         printf("pcap header:\n");
102         printf("capture_length: %d (dec.)\t",pcap_header->caplen);
103         printf("length(off wire): %d (dec.)\n",pcap_header->len);
104         
105         /* prism wlan ng headers */
106         if((my_info_struct->mmode-0x30==1) && 
107                 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
108         printf("prism header:\n");
109         prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
110         printf("message code/length: %d/%d\n",ntohs(prism_hdr->msgcode),
111                                                 ntohs(prism_hdr->msglen));
112         printf("device: %s\n",prism_hdr->devname);
113
114         /* ieee802.11 header */
115         printf("ieee802.11 header:\n");
116         package+=sizeof(struct linux_wlan_ng_prism_hdr);
117         w_hdr=(struct ieee802_11_hdr *)package;
118         
119         printf("debug:\n");
120         for(i=0;i<16;i++) {
121                 printf("%x -> ",1<<i);
122                 printf("%x\n",(ntohs(w_hdr->frame_ctl) & (1<<i)));
123         }
124         printf("frame_control: %x duration_id: %x\n",ntohs(w_hdr->frame_ctl),
125                                                 ntohs(w_hdr->duration_id));
126         printf("version check ... %s\n",
127         ((ntohs(w_hdr->frame_ctl) & IEEE802_11_FCTL_VERS)==0x00)?
128                                                         "ok":"unknown");
129         // printf("type: 
130         /* skip ieee802.11 header */
131         package=package+sizeof(struct ieee802_11_hdr);
132         }
133
134         /* ieee802.3 */
135         /* ethernet */
136         e_hdr=(struct ethhdr *)package;
137         /* what types ? */
138         printf("type = ");
139         printf("%x  ",ntohs(e_hdr->h_proto));
140         printf("dest_addr = ");
141         for(i=0;i<ETH_ALEN;i++)
142         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
143         printf(" src_addr = ");
144         for(i=0;i<ETH_ALEN;i++)
145         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
146                 /* IP ? */
147         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
148                 printf("ip protocol: ");
149                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
150                 printf("version = %x ",ntohs(ip_hdr->ip_v));
151                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
152                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
153                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
154                 printf("ip_addresses: source = ");
155                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
156                                                 inet_ntoa(ip_hdr->ip_dst));
157                 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
158                 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
159                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
160                                                 ntohs(ip_hdr->ip_p));
161                 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
162         }
163         printf("all_hex_dump:\n");
164         for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
165                 printf("%x ",*(package+i));
166         printf("\n");
167 }
168
169