second day
[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 int main(int argc, char *argv[]) {
20
21         int i;
22         char pcap_error[PCAP_ERRBUF_SIZE];
23         pcap_t *pcap_handle;
24         const u_char *package;
25         struct pcap_pkthdr pcap_header;
26         struct ethhdr *e_hdr;
27         struct ieee802_11_hdr *w_hdr;
28         
29         /* parse the arguments */
30         if(argc<2) {
31                 printf("usage: %s <interface> <logfile>\n",argv[0]);
32                 return 0;
33         }
34         if(argc!=3) {
35                 printf("no logfile specified, writing to stdout ...\n");
36         }
37         
38         /* start pcap session */
39         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
40         if(pcap_handle==NULL) {
41                 printf("%s: %s\n",argv[0],pcap_error);
42                 return 1;
43         }
44         
45         /* grab a package until user breaks */
46         while (1) {
47         printf("\n");
48
49         printf("----| new package |----| hdw - sniff |----\n");
50         if((package=pcap_next(pcap_handle,&pcap_header))!=NULL) {
51                 printf("package received at: %s",
52                         ctime((const time_t*)&pcap_header.ts.tv_sec));
53                 printf("pcap header: ");
54                 printf("capture length=%d ",pcap_header.caplen);
55                 printf("length(off wire)=%d\n",pcap_header.len);
56         }
57         
58         /* 802.11 or 802.3 -- not quite sure about 'D' & '\0' */
59         if(*package=='D') {
60                 printf("ieee802.11: ");
61         }
62         else if(*package=='\0') {
63                 printf("ethernet: ");
64                 /* reading ethernet header */
65                 e_hdr=(struct ethhdr *)package;
66                 /* what types ? */
67                 printf("types = ");
68                 // for(i=0;i<2;i++)
69                 printf("%x %x\n",
70                         /* i==0?"0x":" ", */
71                         *(e_hdr->h_proto)
72                         /* i==1?"\n":"" */
73                 );
74                 printf("dest_addr = ");
75                 for(i=0;i<ETH_ALEN;i++)
76                 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
77                 printf(" src_addr = ");
78                 for(i=0;i<ETH_ALEN;i++)
79                 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
80                 printf("rest:\n");
81                 for(i=sizeof(struct ethhdr);i<pcap_header.caplen;i++)
82                         printf("%x ",*(package+i));
83                 printf("\n");
84         }
85         else {
86                 printf("unknown: ");
87                 /* print the whole package in hex */
88                 for(i=0;i<=pcap_header.caplen;i++) printf("%x ",*(package+i));
89                 printf("\n");
90                 printf("%c <- identifier for unknown!\n",*package);
91         }
92         }
93         printf("just beacon frames, ha? - wake up early! :)");
94 }