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