beacons recognized, minimal gui working
[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   char *blank;
21
22   display=(t_display *)&(info->display);
23
24   x=display->max_x-1;
25   y=display->max_y-1;
26
27   size=strlen(string);
28
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   /* footer */
74   display_line(display,0,y-3,x,y-3,'-');
75
76   snprintf(help,14,"total: %06d",info->count);
77   display_string(display,3,y-2,help,13);
78   snprintf(help,19,"management: %06d",info->count_m);
79   display_string(display,18,y-2,help,18);
80   snprintf(help,16,"control: %06d",info->count_c);
81   display_string(display,38,y-2,help,15);
82   snprintf(help,13,"data: %06d",info->count_d);
83   display_string(display,55,y-2,help,12);
84
85   display_line(display,0,y-1,x,y-1,'-');
86
87   if(size!=0) {
88     blank=(char *)malloc(display->max_x);
89     memset(blank,0x20,display->max_x);
90     display_string(display,0,y,blank,x);
91     display_string(display,1,y,string,size);
92     free(blank);
93   }
94
95   display_draw(display);
96
97   return 23;
98 }
99
100 int noop(t_input *input,void *ptr) {
101
102   return 23;
103 }
104
105 int get_user_interaction(t_info *info) {
106
107   char *string;
108
109   string=(char *)malloc(info->display.max_x*sizeof(char));
110
111   input_get_event(&(info->input),noop,info);
112
113   /*
114   if(info->input.content[0]=='h') {
115     display_console(info,"hdw-sniff help:");
116     display_console(info,"h - print this help");
117     display_console(info,"x - enable/disable hex output");
118     display_console(info,"a - enable/disable ascii output");
119     display_console(info,"q - quit");
120     display_console(info,"console navigation: arrow up/down");
121     display_console(info,"network list navigation: page up/down");
122   }
123   */
124
125   if(info->input.content[0]=='x') {
126     info->mode^=MODE_HEXOUT;
127     display_console(info,"toggled hex output");
128   }
129
130   else if(info->input.content[0]=='a') {
131     info->mode^=MODE_ASCIIOUT;
132     display_console(info,"toggled ascii output");
133   }
134
135   else if(info->input.content[0]=='q') {
136     event_stop(&(info->event));
137     display_console(info,"quit!");
138   }
139
140   else {
141     snprintf(string,info->display.max_x,"unknown event (%x)",
142              info->input.content[0]);
143     display_console(info,string);
144   }
145
146   return 23;
147 }
148
149 int react_on_event(t_event *event,void *ptr) {
150
151   t_info *info;
152
153   info=(t_info *)ptr;
154
155   if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
156   else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
157
158   return 23;
159 }
160
161 int usage(void) {
162   puts("usage: hdw-sniff <options>");
163   puts("\toptions:");
164   puts("\t\t-m <mode> \tmonitor and/or wlanng");
165   puts("\t\t-d <device> \twlan0,eth0");
166   puts("\t\t-l <logfile>");
167   puts("\t\t-k <key> \t(string)");
168   puts("\t\t-D <file> \t(dump packages to file)");
169   puts("\t\t-h \tdisplay this help message");
170   puts("");
171
172   return 23;
173 }
174
175 int hop_channel(t_event *event,void *ptr) {
176  
177   struct iwreq iwreq;
178   t_info *info;
179
180   info=(t_info *)ptr;
181
182   if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
183     info->current_channel=1;
184   memset(&iwreq,0,sizeof(iwreq));
185   strcpy(iwreq.ifr_name,info->device);
186   iwreq.u.freq.e=0;
187   iwreq.u.freq.m=info->current_channel;
188   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
189     puts("unable to hop channel");
190     perror("ioctl");
191     return -23;
192   }
193   ++(info->current_channel);
194
195   return 23;
196 }
197
198 int main(int argc, char **argv) {
199
200   t_info info;
201   int pcap_fd;
202   int i;
203   char sys_call[MAX_SYSCALL_CHARS];
204   char pcap_error[PCAP_ERRBUF_SIZE];
205
206   memset(&info,0,sizeof(t_info));
207
208  /* parse arguments */
209  for(i=1;i<argc;i++) {
210   if(argv[i][0]=='-') {
211    switch(argv[i][1]) {
212     case 'h':
213      usage();
214     case 'm':
215      if(!strncmp(argv[i+1],"monitor",7)) {
216       info.mode|=MODE_MONITOR;
217       puts("will go to monitor mode.");
218      }
219      else if(!strncmp(argv[i+1],"wlanng",6)) {
220       info.mode|=MODE_WLANNG;
221       puts("expecting wlanng header in package.");
222      }
223      else {
224       printf("unknown mode: %s\n",argv[1]);
225       return -23;
226      }
227      ++i;
228      break;
229     case 'l':
230      if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
231       printf("logfile -> %s\n",argv[i+1]);
232      else
233       puts("warning: can't write to logfile.");
234      ++i;
235      break;
236     case 'D':
237      if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
238       printf("dump file -> %s\n",argv[i+1]);
239      else
240       puts("warning: can't dump to file.");
241      ++i;
242      break;
243     case 'd':
244      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
245      ++i;
246      break;
247     default:
248      usage();
249      return -23;
250    }
251   } else {
252    usage();
253    return -23;
254   }
255  }
256
257  /* setting up device */
258  if(info.mode&MODE_MONITOR) {
259   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
260   puts("set monitoring mode ...");
261   system(sys_call);
262  }
263  sprintf(sys_call,"/sbin/ifconfig %s up",info.device);
264  puts("setting up device ...");
265  system(sys_call);
266
267  if(info.log_fd==0) {
268    if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
269      puts("using logfile /tmp/hdw-sniff.log ...");
270    else {
271      puts("failed to open logfile ...");
272      return -23;
273    }
274  }
275
276  /* pcap */
277  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
278  {
279   printf("%s: %s\n",argv[0],pcap_error);
280   return -23;
281  }
282  pcap_fd=pcap_fileno(info.pcap_handle);
283  /* -> non blocking? */
284
285  /* socket fd for channel hopping */
286  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
287
288  display_init(&(info.display),info.log_fd);
289  if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
290    display_shutdown(&(info.display));
291    puts("");
292    puts("");
293    puts("fatal! please increase your terminals width");
294    puts("");
295    return -23;
296  }
297  if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
298    display_shutdown(&(info.display));
299    puts("");
300    puts("");
301    puts("fatal! please increase your terminals width");
302    puts("");
303    return -23;
304  }
305
306  input_init(&(info.input),info.log_fd);
307  //input.mode=CONTENT_BUFFER;
308  input_ios_init(&info.input);
309
310  event_init(&(info.event),info.log_fd);
311  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
312
313  event_math(0,&(info.event),READ,ADD);
314  event_math(pcap_fd,&(info.event),READ,ADD);
315
316  list_init(&(info.sniffed_sta),info.log_fd);
317
318  display_console(&info,
319    "                       Welcome to hdw-sniff ... :-D");
320
321  sleep(2);
322
323  display_console(&info,
324  "    Copyright (C) 2004/05 hackbard@hackdaworld.org (press any button)");
325
326  getchar();
327
328  event_start(&(info.event),&info,react_on_event,hop_channel);
329
330  list_shutdown(&(info.sniffed_sta));
331  input_shutdown(&(info.input));
332  display_shutdown(&(info.display));
333  
334  puts("");
335  puts("");
336  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
337  puts("");
338  puts("bugreports: hackbard@hackdaworld.org");
339
340  return 23;
341 }