info view mode introduced (still buggy)
[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,size,count,cnt,i,s;
17   t_display *display;
18   t_sta *sta;
19   char help[32];
20
21   display=(t_display *)&(info->display);
22
23   x=display->max_x-1;
24   y=display->max_y-1;
25
26   size=strlen(string);
27
28   if(info->view==VIEW_ALL) {
29     count=list_count(&(info->sniffed_sta));
30     cnt=((count>y-5)?y-5:count);
31
32     display_line(display,0,0,x,0,'-');
33     display_string(display,GUI_OFFSET_STA,1,"sta/ap",6);
34     display_string(display,GUI_OFFSET_SSID,1,"| ssid",6);
35     display_string(display,GUI_OFFSET_AP,1,"| ap",4);
36     display_string(display,GUI_OFFSET_WEP,1,"| wep",5);
37     display_string(display,GUI_OFFSET_MGMT,1,"| mgmt",6);
38     display_string(display,GUI_OFFSET_CTRL,1,"| ctrl",6);
39     display_string(display,GUI_OFFSET_DATA,1,"| data",6);
40     display_string(display,GUI_OFFSET_ACTIVE,1,"| act",5);
41     display_line(display,0,2,x,2,'-');
42
43     list_reset(&(info->sniffed_sta));
44
45     for(i=0;i<cnt;i++) {
46       sta=(t_sta *)info->sniffed_sta.current->data;
47       snprintf(help,32,"%02x:%02x:%02x:%02x:%02x:%02x",
48                sta->addr[0],sta->addr[1],sta->addr[2],
49                sta->addr[3],sta->addr[4],sta->addr[5]);
50       display_string(display,GUI_OFFSET_STA,3+i,help,17);
51       s=strlen(sta->ssid);
52       s=((s>=16)?16:s);
53       help[0]='|'; help[1]=' ';
54       memcpy(&help[2],sta->ssid,s);
55       display_string(display,GUI_OFFSET_SSID,3+i,help,s+2);
56       snprintf(help,4,"| %c",sta->ap?'y':'n');
57       display_string(display,GUI_OFFSET_AP,3+i,help,3);
58       snprintf(help,5,"|  %c",sta->wep?'y':'n');
59       display_string(display,GUI_OFFSET_WEP,3+i,help,4);
60 /*
61       snprintf(help,21,"| %04d | %04d | %04d",
62                sta->count_mgmt,sta->count_ctrl,
63                sta->count_data);
64 */
65       snprintf(help,21,"| %04d | n.a. | n.a.",
66                sta->count_mgmt);
67       display_string(display,GUI_OFFSET_MGMT,3+i,help,20);
68       snprintf(help,5,"|  %c",sta->active);
69       display_string(display,GUI_OFFSET_ACTIVE,3+i,help,4);
70       list_next(&(info->sniffed_sta));
71     }
72   }
73
74   else if(info->view==VIEW_SINGLE) {
75     list_reset(&(info->sniffed_sta));
76     for(i=0;i<info->c_line;i++) list_next(&(info->sniffed_sta));
77     sta=(t_sta *)info->sniffed_sta.current->data;
78     display_line(display,0,0,x,0,'-');
79     snprintf(help,22,"sta %02x:%02x:%02x:%02x:%02x:%02x",
80              sta->addr[0],sta->addr[1],sta->addr[2],
81              sta->addr[3],sta->addr[4],sta->addr[5]);
82     display_string(display,1,1,help,21);
83     display_line(display,0,2,x,2,'-');
84     display_string(display,1,3,info->clean_line,x);
85     display_string(display,1,3,"ssid: ",6);
86     s=strlen(sta->ssid);
87     memcpy(help,sta->ssid,s);
88     display_string(display,7,3,help,s);
89     display_string(display,1,5,"more specific info soon ...",27);
90   }
91
92   /* footer */
93   display_line(display,0,y-3,x,y-3,'-');
94
95   snprintf(help,14,"total: %06d",info->count);
96   display_string(display,1,y-2,help,13);
97   snprintf(help,13,"mgmt: %06d",info->count_m);
98   display_string(display,15,y-2,help,12);
99   snprintf(help,12,"ctrl: %06d",info->count_c);
100   display_string(display,28,y-2,help,11);
101   snprintf(help,13,"data: %06d",info->count_d);
102   display_string(display,40,y-2,help,12);
103
104   display_line(display,0,y-1,x,y-1,'-');
105
106   if(size!=0) {
107     display_string(display,0,y,info->clean_line,x);
108     display_string(display,1,y,string,size);
109   }
110
111   display_draw(display);
112
113   if(info->view==VIEW_ALL)
114     display_set_cursor(display,0,3+info->c_line);
115
116
117   return 23;
118 }
119
120 int noop(t_input *input,void *ptr) {
121
122   return 23;
123 }
124
125 int get_user_interaction(t_info *info) {
126
127   char *string;
128   int hlp=0;
129
130   string=(char *)malloc(info->display.max_x*sizeof(char));
131
132   input_get_event(&(info->input),noop,info);
133
134   /*
135   if(info->input.content[0]=='h') {
136     display_console(info,"hdw-sniff help:");
137     display_console(info,"h - print this help");
138     display_console(info,"x - enable/disable hex output");
139     display_console(info,"a - enable/disable ascii output");
140     display_console(info,"q - quit");
141     display_console(info,"console navigation: arrow up/down");
142     display_console(info,"network list navigation: page up/down");
143   }
144   */
145
146   if(info->input.content[0]=='x') {
147     info->mode^=MODE_HEXOUT;
148     display_console(info,"toggled hex output");
149   }
150
151   else if(info->input.content[0]=='a') {
152     info->mode^=MODE_ASCIIOUT;
153     display_console(info,"toggled ascii output");
154   }
155
156   else if(info->input.content[0]=='q') {
157     event_stop(&(info->event));
158     display_console(info,"quit!");
159   }
160   else if(info->input.content[0]=='n') {
161     hlp=list_count(&(info->sniffed_sta)); 
162     if(info->c_line<hlp-1) info->c_line++;
163   }
164   else if(info->input.content[0]=='p') {
165     if(info->c_line>0) info->c_line--;
166   }
167   else if(info->input.content[0]==0x0d) {
168     display_clear_screen(&(info->display));
169     if(info->view==VIEW_ALL) info->view=VIEW_SINGLE;
170     else info->view=VIEW_ALL;
171   }
172   else {
173     snprintf(string,info->display.max_x,"unknown event (%x)",
174              info->input.content[0]);
175     display_console(info,string);
176   }
177
178   return 23;
179 }
180
181 int react_on_event(t_event *event,void *ptr) {
182
183   t_info *info;
184
185   info=(t_info *)ptr;
186
187   if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
188   else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
189
190   return 23;
191 }
192
193 int usage(void) {
194   puts("usage: hdw-sniff <options>");
195   puts("\toptions:");
196   puts("\t\t-m <mode> \tmonitor and/or wlanng");
197   puts("\t\t-d <device> \twlan0,eth0");
198   puts("\t\t-l <logfile>");
199   puts("\t\t-k <key> \t(string)");
200   puts("\t\t-D <file> \t(dump packages to file)");
201   puts("\t\t-h \tdisplay this help message");
202   puts("");
203
204   return 23;
205 }
206
207 int hop_channel(t_event *event,void *ptr) {
208  
209   struct iwreq iwreq;
210   t_info *info;
211
212   info=(t_info *)ptr;
213
214   if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
215     info->current_channel=1;
216   memset(&iwreq,0,sizeof(iwreq));
217   strcpy(iwreq.ifr_name,info->device);
218   iwreq.u.freq.e=0;
219   iwreq.u.freq.m=info->current_channel;
220   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
221     puts("unable to hop channel");
222     perror("ioctl");
223     return -23;
224   }
225   ++(info->current_channel);
226
227   return 23;
228 }
229
230 int main(int argc, char **argv) {
231
232   t_info info;
233   int pcap_fd;
234   int i;
235   char sys_call[MAX_SYSCALL_CHARS];
236   char pcap_error[PCAP_ERRBUF_SIZE];
237
238   memset(&info,0,sizeof(t_info));
239
240  /* parse arguments */
241  for(i=1;i<argc;i++) {
242   if(argv[i][0]=='-') {
243    switch(argv[i][1]) {
244     case 'h':
245      usage();
246     case 'm':
247      if(!strncmp(argv[i+1],"monitor",7)) {
248       info.mode|=MODE_MONITOR;
249       puts("will go to monitor mode.");
250      }
251      else if(!strncmp(argv[i+1],"wlanng",6)) {
252       info.mode|=MODE_WLANNG;
253       puts("expecting wlanng header in package.");
254      }
255      else {
256       printf("unknown mode: %s\n",argv[1]);
257       return -23;
258      }
259      ++i;
260      break;
261     case 'l':
262      if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
263       printf("logfile -> %s\n",argv[i+1]);
264      else
265       puts("warning: can't write to logfile.");
266      ++i;
267      break;
268     case 'D':
269      if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
270       printf("dump file -> %s\n",argv[i+1]);
271      else
272       puts("warning: can't dump to file.");
273      ++i;
274      break;
275     case 'd':
276      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
277      ++i;
278      break;
279     default:
280      usage();
281      return -23;
282    }
283   } else {
284    usage();
285    return -23;
286   }
287  }
288
289  /* setting up device */
290  if(info.mode&MODE_MONITOR) {
291   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
292   puts("set monitoring mode ...");
293   system(sys_call);
294  }
295  sprintf(sys_call,"ifconfig %s up",info.device);
296  puts("setting up device ...");
297  system(sys_call);
298
299  if(info.log_fd==0) {
300    if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
301      puts("using logfile /tmp/hdw-sniff.log ...");
302    else {
303      puts("failed to open logfile ...");
304      return -23;
305    }
306  }
307
308  /* pcap */
309  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
310  {
311   printf("%s: %s\n",argv[0],pcap_error);
312   return -23;
313  }
314  pcap_fd=pcap_fileno(info.pcap_handle);
315  /* -> non blocking? */
316
317  /* socket fd for channel hopping */
318  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
319
320  display_init(&(info.display),info.log_fd);
321  if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
322    display_shutdown(&(info.display));
323    puts("");
324    puts("");
325    puts("fatal! please increase your terminals width");
326    puts("");
327    return -23;
328  }
329  if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
330    display_shutdown(&(info.display));
331    puts("");
332    puts("");
333    puts("fatal! please increase your terminals width");
334    puts("");
335    return -23;
336  }
337  info.clean_line=(char *)malloc(info.display.max_x);
338  memset(info.clean_line,0x20,info.display.max_x);
339  info.view=VIEW_ALL;
340
341  display_console(&info,
342    "Welcome to hdw-sniff, Copyright (C) 2004/05 hackbard");
343  sleep(2);
344
345  input_init(&(info.input),info.log_fd);
346  input_ios_init(&info.input);
347
348  list_init(&(info.sniffed_sta),info.log_fd);
349
350  event_init(&(info.event),info.log_fd);
351  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
352
353  event_math(0,&(info.event),READ,ADD);
354  event_math(pcap_fd,&(info.event),READ,ADD);
355
356  event_start(&(info.event),&info,react_on_event,hop_channel);
357
358  list_shutdown(&(info.sniffed_sta));
359  input_shutdown(&(info.input));
360  display_shutdown(&(info.display));
361  
362  puts("");
363  puts("");
364  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
365  puts("");
366  puts("bugreports: hackbard@hackdaworld.org");
367
368  return 23;
369 }