ip address support added
[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 #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 #define MAX_PACKAGES 3
22
23 int main(int argc, char *argv[]) {
24
25         int i,count;
26         char pcap_error[PCAP_ERRBUF_SIZE];
27         pcap_t *pcap_handle;
28         const u_char *package;
29         struct pcap_pkthdr pcap_header;
30         struct ethhdr *e_hdr;
31         struct ip *ip_hdr;
32         struct ieee802_11_hdr *w_hdr;
33         
34         /* parse the arguments */
35         if(argc<2) {
36                 printf("usage: %s <interface> <logfile>\n",argv[0]);
37                 return 0;
38         }
39         if(argc!=3) {
40                 printf("no logfile specified, writing to stdout ...\n");
41         }
42         
43         /* start pcap session */
44         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
45         if(pcap_handle==NULL) {
46                 printf("%s: %s\n",argv[0],pcap_error);
47                 return 1;
48         }
49         
50         /* grab a package until user breaks */
51         count=0;
52         while (count<MAX_PACKAGES) {
53         count++;
54         printf("\n");
55         if((package=pcap_next(pcap_handle,&pcap_header))!=NULL) {
56                 printf("---> package %d ---- %s",count,
57                                 ctime((const time_t*)&pcap_header.ts.tv_sec));
58                 printf("pcap header: ");
59                 printf("capture length=%d ",pcap_header.caplen);
60                 printf("length(off wire)=%d\n",pcap_header.len);
61         }
62         
63         /* is ieee802.11 ? -- we assume yes :) */
64         /* ...                                  */
65
66
67         /* ieee802.3 */
68         /* ethernet */
69         e_hdr=(struct ethhdr *)package;
70         /* what types ? */
71         printf("type = ");
72         printf("%x  ",ntohs(e_hdr->h_proto));
73         printf("dest_addr = ");
74         for(i=0;i<ETH_ALEN;i++)
75         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
76         printf(" src_addr = ");
77         for(i=0;i<ETH_ALEN;i++)
78         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
79                 /* IP ? */
80         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
81                 printf("ip protocol: ");
82                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
83                 printf("version = %x ",ntohs(ip_hdr->ip_v));
84                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
85                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
86                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
87                 printf("ip_addresses: source = ");
88                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
89                                                 inet_ntoa(ip_hdr->ip_dst));
90                 
91                 
92         }
93         printf("all_hex_dump:\n");
94         for(i=sizeof(struct ethhdr);i<pcap_header.caplen;i++)
95                 printf("%x ",*(package+i));
96         printf("\n");
97         }
98         printf("\n");
99         printf("wake up early! :)\n");
100         printf("\n");
101 }