96ef4a22eb7488e1b82b2eaba231e9804ffdfab1
[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>  /* for ethhdr struct */
12 #include <netinet/ip.h> /* ip */
13 #include <netinet/in.h> /* in_addr */
14
15 /* IEEE 802.11 stuff -- will become one include later ... */
16 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
17 #include "ieee802_11.h" /* from pcmcia-cs */
18
19 #include "hdw-sniff.h" /* my functions */
20
21 int main(int argc, char *argv[]) {
22
23         char pcap_error[PCAP_ERRBUF_SIZE];
24         pcap_t *pcap_handle;
25         char sys_call[30];
26         FILE *logfile;
27         int count;
28         int *p_count;
29         
30         /* parse the arguments */
31         if(argc<3) {
32                 printf("usage: %s <interface> <monitor mode> <logfile>\n",argv[0]);
33                 return 0;
34         }
35         if(argc!=4) {
36                 printf("no logfile specified, writing to stdout ...\n");
37         } 
38         else {
39                 if((logfile=fopen(argv[3],"w"))!=NULL) {
40                         printf("writing to logfile %s ...\n",argv[3]);
41                 }
42                 else {
43                         printf("can't open logfile!\n");
44                 }
45         }
46                         
47         /* setting up device and set monitor mode */
48         if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
49                 printf("setting to monitor mode 3\n");
50                 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
51                 system(sys_call);
52         }
53         printf("setting up interface\n");
54         sprintf(sys_call,"ifconfig %s up",argv[1]);
55         system(sys_call);
56
57         /* start pcap session */
58         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
59         if(pcap_handle==NULL) {
60                 printf("%s: %s\n",argv[0],pcap_error);
61                 return 1;
62         }
63         
64         /* grab a package until user breaks */
65         count=0;
66         p_count=&count;
67         pcap_dispatch(pcap_handle,-1,(pcap_handler)pcap_process,
68                         (u_char *)&p_count);
69 }
70
71
72 /* pcap_process callback function */
73 void pcap_process(u_char *count,const struct pcap_pkthdr *pcap_header,
74                         const u_char *package) {
75         
76         /* local variables */
77         struct ethhdr *e_hdr;
78         struct ip *ip_hdr;
79         struct ieee802_11_hdr *w_hdr;
80         int i;
81         int *p_count;
82
83         p_count=(int *)count;
84         ++(*p_count);
85
86         printf("\n");
87         printf("---> package %d ---- %s",count,
88                         ctime((const time_t*)pcap_header->ts.tv_sec));
89         printf("pcap header: ");
90         printf("capture length=%d ",pcap_header->caplen);
91         printf("length(off wire)=%d\n",pcap_header->len);
92         
93         /* is ieee802.11 ? -- we assume yes :) */
94         /* ...                                  */
95
96
97         /* ieee802.3 */
98         /* ethernet */
99         e_hdr=(struct ethhdr *)package;
100         /* what types ? */
101         printf("type = ");
102         printf("%x  ",ntohs(e_hdr->h_proto));
103         printf("dest_addr = ");
104         for(i=0;i<ETH_ALEN;i++)
105         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
106         printf(" src_addr = ");
107         for(i=0;i<ETH_ALEN;i++)
108         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
109                 /* IP ? */
110         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
111                 printf("ip protocol: ");
112                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
113                 printf("version = %x ",ntohs(ip_hdr->ip_v));
114                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
115                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
116                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
117                 printf("ip_addresses: source = ");
118                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
119                                                 inet_ntoa(ip_hdr->ip_dst));
120                 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
121                 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
122                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
123                                                 ntohs(ip_hdr->ip_p));
124                 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
125                 
126                 
127         }
128         printf("all_hex_dump:\n");
129         for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
130                 printf("%x ",*(package+i));
131 }
132
133