fixed gcc warnings and some pointer issues thanks to hunz
[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 ethhdr *e_hdr;
89         struct ip *ip_hdr;
90         struct ieee802_11_hdr *w_hdr;
91         struct info_struct *my_info_struct;
92         int i;
93
94         my_info_struct=(struct info_struct *)info;
95         ++(my_info_struct->count);
96
97         printf("\n");
98         printf("---> package %d ---- %s",my_info_struct->count,
99                         ctime((const time_t*)&(pcap_header->ts.tv_sec)));
100         printf("pcap header: ");
101         printf("capture length=%d ",pcap_header->caplen);
102         printf("length(off wire)=%d\n",pcap_header->len);
103         
104         /* is ieee802.11 ? -- we assume yes :) */
105         if((my_info_struct->mmode-0x30==1) && 
106                 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
107         printf("debug: ieee802.11\n");
108         }
109
110
111         /* ieee802.3 */
112         /* ethernet */
113         e_hdr=(struct ethhdr *)package;
114         /* what types ? */
115         printf("type = ");
116         printf("%x  ",ntohs(e_hdr->h_proto));
117         printf("dest_addr = ");
118         for(i=0;i<ETH_ALEN;i++)
119         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
120         printf(" src_addr = ");
121         for(i=0;i<ETH_ALEN;i++)
122         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
123                 /* IP ? */
124         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
125                 printf("ip protocol: ");
126                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
127                 printf("version = %x ",ntohs(ip_hdr->ip_v));
128                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
129                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
130                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
131                 printf("ip_addresses: source = ");
132                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
133                                                 inet_ntoa(ip_hdr->ip_dst));
134                 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
135                 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
136                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
137                                                 ntohs(ip_hdr->ip_p));
138                 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
139         }
140         printf("all_hex_dump:\n");
141         for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
142                 printf("%x ",*(package+i));
143         printf("\n");
144 }
145
146