added ip header support
[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 <pcap.h>
9
10 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
11 #include <netinet/if_ether.h>
12
13 /* IEEE 802.11 stuff -- will become one include later ... */
14 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
15 #include "ieee802_11.h" /* from pcmcia-cs */
16
17 #include "hdw-sniff.h" /* my functions */
18
19 #define MAX_PACKAGES 3
20
21 int main(int argc, char *argv[]) {
22
23         int i,count;
24         char pcap_error[PCAP_ERRBUF_SIZE];
25         pcap_t *pcap_handle;
26         const u_char *package;
27         struct pcap_pkthdr pcap_header;
28         struct ethhdr *e_hdr;
29         struct ip *ip_hdr;
30         struct ieee802_11_hdr *w_hdr;
31         
32         /* parse the arguments */
33         if(argc<2) {
34                 printf("usage: %s <interface> <logfile>\n",argv[0]);
35                 return 0;
36         }
37         if(argc!=3) {
38                 printf("no logfile specified, writing to stdout ...\n");
39         }
40         
41         /* start pcap session */
42         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
43         if(pcap_handle==NULL) {
44                 printf("%s: %s\n",argv[0],pcap_error);
45                 return 1;
46         }
47         
48         /* grab a package until user breaks */
49         count=0;
50         while (count<MAX_PACKAGES) {
51         count++;
52         printf("\n");
53         if((package=pcap_next(pcap_handle,&pcap_header))!=NULL) {
54                 printf("---> package %d ---- %s",count,
55                                 ctime((const time_t*)&pcap_header.ts.tv_sec));
56                 printf("pcap header: ");
57                 printf("capture length=%d ",pcap_header.caplen);
58                 printf("length(off wire)=%d\n",pcap_header.len);
59         }
60         
61         /* is ieee802.11 ? -- we assume yes :) */
62         
63         
64         /* ethernet */
65         e_hdr=(struct ethhdr *)package;
66         /* what types ? */
67         printf("types = ");
68         printf("%x  ",ntohs(e_hdr->h_proto));
69         printf("dest_addr = ");
70         for(i=0;i<ETH_ALEN;i++)
71         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
72         printf(" src_addr = ");
73         for(i=0;i<ETH_ALEN;i++)
74         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
75         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
76                 printf("ip protocol: ");
77                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
78                 printf("version = %x ",ntohs(ip_hdr->ip_v:4));
79                 printf("header length = %x\n",ntohs(ip_hdr->ip_hl:4));
80         }
81         printf("rest:\n");
82         for(i=sizeof(struct ethhdr);i<pcap_header.caplen;i++)
83                 printf("%x ",*(package+i));
84         printf("\n");
85         }
86         printf("\n");
87         printf("just beacon frames, ha? - wake up early! :)\n");
88         printf("\n");
89 }