pcap_next -> pcap_loop
[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 , inet_ntoa */
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",
33                         argv[0]);
34                 return 0;
35         }
36         if(argc!=4) {
37                 printf("no logfile specified, writing to stdout ...\n");
38         } 
39         else {
40                 if((logfile=fopen(argv[3],"w"))!=NULL) {
41                         printf("writing to logfile %s ...\n",argv[3]);
42                 }
43                 else {
44                         printf("can't open logfile!\n");
45                 }
46         }
47                         
48         /* setting up device and set monitor mode */
49         if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
50                 printf("setting to monitor mode 3\n");
51                 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
52                 system(sys_call);
53         }
54         printf("setting up interface\n");
55         sprintf(sys_call,"ifconfig %s up",argv[1]);
56         system(sys_call);
57
58         /* start pcap session */
59         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
60         if(pcap_handle==NULL) {
61                 printf("%s: %s\n",argv[0],pcap_error);
62                 return 1;
63         }
64         
65         /* grab a package until user breaks */
66         count=0;
67         p_count=&count;
68         pcap_loop(pcap_handle,-1,pcap_process,(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",*p_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         if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
95         printf("debug: ieee802.11\n");
96         }
97
98
99         /* ieee802.3 */
100         /* ethernet */
101         e_hdr=(struct ethhdr *)package;
102         /* what types ? */
103         printf("type = ");
104         printf("%x  ",ntohs(e_hdr->h_proto));
105         printf("dest_addr = ");
106         for(i=0;i<ETH_ALEN;i++)
107         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
108         printf(" src_addr = ");
109         for(i=0;i<ETH_ALEN;i++)
110         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
111                 /* IP ? */
112         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
113                 printf("ip protocol: ");
114                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
115                 printf("version = %x ",ntohs(ip_hdr->ip_v));
116                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
117                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
118                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
119                 printf("ip_addresses: source = ");
120                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
121                                                 inet_ntoa(ip_hdr->ip_dst));
122                 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
123                 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
124                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
125                                                 ntohs(ip_hdr->ip_p));
126                 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
127         }
128         printf("all_hex_dump:\n");
129         for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
130                 printf("%x ",*(package+i));
131         printf("\n");
132 }
133
134