beacon work ...
[my-code/hdw-sniff.git] / main.c
1 /*
2  * main.c - main hdw-sniff
3  *
4  * Copyright (C) 2004/05 hackbard@hackdaworld.org
5  *
6  */
7
8 #include "main.h"
9
10 // void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package);
11
12 /* functions */
13
14 int display_console(t_info *info,char *string) {
15
16   int x,y;
17   t_display *display;
18
19   display=(t_display *)&(info->display);
20
21   x=display->max_x-1;
22   y=display->max_y-1;
23
24   display_line(display,0,0,2,0,'-');
25   display_string(display,4,0,"sta",3);
26   display_line(display,8,0,12,0,'-');
27
28   display_string(display,13,0,"bssid",5);
29   display_line(display,19,0,23,0,'-');
30
31   display_string(display,24,0,"M",1);
32   display_line(display,26,0,x,0,'-');
33
34   //display_line(display,0,0,0,y,'|');
35   //display_line(display,x,0,x,y,'|');
36
37   display_line(display,0,y-1,x,y-1,'-');
38
39   display_draw(display);
40
41   return 23;
42 }
43
44 int noop(t_input *input,void *ptr) {
45
46   return 23;
47 }
48
49 int get_user_interaction(t_info *info) {
50
51   char *string;
52
53   string=(char *)malloc(info->display.max_x*sizeof(char));
54
55   input_get_event(&(info->input),noop,info);
56
57   /*
58   if(info->input.content[0]=='h') {
59     display_console(info,"hdw-sniff help:");
60     display_console(info,"h - print this help");
61     display_console(info,"x - enable/disable hex output");
62     display_console(info,"a - enable/disable ascii output");
63     display_console(info,"q - quit");
64     display_console(info,"console navigation: arrow up/down");
65     display_console(info,"network list navigation: page up/down");
66   }
67   */
68
69   if(info->input.content[0]=='x') {
70     info->mode^=MODE_HEXOUT;
71     display_console(info,"toggled hex output");
72   }
73
74   else if(info->input.content[0]=='a') {
75     info->mode^=MODE_ASCIIOUT;
76     display_console(info,"toggled ascii output");
77   }
78
79   else if(info->input.content[0]=='q') {
80     event_stop(&(info->event));
81     display_console(info,"quit!");
82   }
83
84   else {
85     snprintf(string,info->display.max_x,"unknown event (%x)",
86              info->input.content[0]);
87     display_console(info,string);
88   }
89
90   return 23;
91 }
92
93 int react_on_event(t_event *event,void *ptr) {
94
95   t_info *info;
96
97   info=(t_info *)ptr;
98
99   if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
100   else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
101
102   return 23;
103 }
104
105 int usage(void) {
106   puts("usage: hdw-sniff <options>");
107   puts("\toptions:");
108   puts("\t\t-m <mode> \tmonitor and/or wlanng");
109   puts("\t\t-d <device> \twlan0,eth0");
110   puts("\t\t-l <logfile>");
111   puts("\t\t-k <key> \t(string)");
112   puts("\t\t-D <file> \t(dump packages to file)");
113   puts("\t\t-h \tdisplay this help message");
114   puts("");
115
116   return 23;
117 }
118
119 int hop_channel(t_event *event,void *ptr) {
120  
121   struct iwreq iwreq;
122   t_info *info;
123
124   info=(t_info *)ptr;
125
126   if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
127     info->current_channel=1;
128   memset(&iwreq,0,sizeof(iwreq));
129   strcpy(iwreq.ifr_name,info->device);
130   iwreq.u.freq.e=0;
131   iwreq.u.freq.m=info->current_channel;
132   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
133     puts("unable to hop channel");
134     perror("ioctl");
135     return -23;
136   }
137   ++(info->current_channel);
138
139   return 23;
140 }
141
142 int main(int argc, char **argv) {
143
144   t_info info;
145   int pcap_fd;
146   int i;
147   char sys_call[MAX_SYSCALL_CHARS];
148   char pcap_error[PCAP_ERRBUF_SIZE];
149
150   memset(&info,0,sizeof(t_info));
151  
152  /* parse arguments */
153  for(i=1;i<argc;i++) {
154   if(argv[i][0]=='-') {
155    switch(argv[i][1]) {
156     case 'h':
157      usage();
158     case 'm':
159      if(!strncmp(argv[i+1],"monitor",7)) {
160       info.mode|=MODE_MONITOR;
161       puts("will go to monitor mode.");
162      }
163      else if(!strncmp(argv[i+1],"wlanng",6)) {
164       info.mode|=MODE_WLANNG;
165       puts("expecting wlanng header in package.");
166      }
167      else {
168       printf("unknown mode: %s\n",argv[1]);
169       return -23;
170      }
171      ++i;
172      break;
173     case 'l':
174      if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
175       printf("logfile -> %s\n",argv[i+1]);
176      else
177       puts("warning: can't write to logfile.");
178      ++i;
179      break;
180     case 'D':
181      if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
182       printf("dump file -> %s\n",argv[i+1]);
183      else
184       puts("warning: can't dump to file.");
185      ++i;
186      break;
187     case 'd':
188      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
189      ++i;
190      break;
191     default:
192      usage();
193      return -23;
194    }
195   } else {
196    usage();
197    return -23;
198   }
199  }
200
201  /* setting up device */
202  if(info.mode&MODE_MONITOR) {
203   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
204   puts("set monitoring mode ...");
205   system(sys_call);
206  }
207  sprintf(sys_call,"ifconfig %s up",info.device);
208  puts("setting up device ...");
209  system(sys_call);
210
211  if(info.log_fd==0) {
212    if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT))!=0)
213      puts("using logfile /tmp/hdw-sniff.log ...");
214    else {
215      puts("failed to open logfile ...");
216      return -23;
217    }
218  }
219
220  /* pcap */
221  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
222  {
223   printf("%s: %s\n",argv[0],pcap_error);
224   return -23;
225  }
226  pcap_fd=pcap_fileno(info.pcap_handle);
227  /* -> non blocking? */
228
229  /* socket fd for channel hopping */
230  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
231
232  display_init(&(info.display),info.log_fd);
233
234  input_init(&(info.input),info.log_fd);
235  //input.mode=CONTENT_BUFFER;
236  input_ios_init(&info.input);
237
238  event_init(&(info.event),info.log_fd);
239  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
240
241  event_math(0,&(info.event),READ,ADD);
242  event_math(pcap_fd,&(info.event),READ,ADD);
243
244  list_init(&(info.sniffed_sta),info.log_fd);
245
246  display_console(&info,"foo");
247
248  event_start(&(info.event),&info,react_on_event,hop_channel);
249
250  input_shutdown(&(info.input));
251  display_shutdown(&(info.display));
252  
253  puts("");
254  puts("");
255  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
256  puts("");
257  puts("bugreports: hackbard@hackdaworld.org");
258
259  return 23;
260 }