699eeac021eac760d34d45012b76319eb19e5df4
[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,"essid",5);
26   display_line(display,10,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 logfd;
139   int i;
140   char sys_call[MAX_SYSCALL_CHARS];
141   char pcap_error[PCAP_ERRBUF_SIZE];
142
143   memset(&info,0,sizeof(t_info));
144  
145  /* parse arguments */
146  for(i=1;i<argc;i++)
147  {
148   if(argv[i][0]=='-')
149   {
150    switch(argv[i][1])
151    {
152     case 'h':
153      usage();
154     case 'm':
155      if(!strncmp(argv[i+1],"monitor",7)) {
156       info.mode|=MODE_MONITOR;
157       puts("will go to monitor mode.");
158      }
159      else if(!strncmp(argv[i+1],"wlanng",6)) {
160       info.mode|=MODE_WLANNG;
161       puts("expecting wlanng header in package.");
162      }
163      else {
164       printf("unknown mode: %s\n",argv[1]);
165       return -23;
166      }
167      ++i;
168      break;
169     case 'l':
170      if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
171       printf("logfile -> %s\n",argv[i+1]);
172      else
173       puts("warning: can't write to logfile.");
174      ++i;
175      break;
176     case 'D':
177      if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
178       printf("dump file -> %s\n",argv[i+1]);
179      else
180       puts("warning: can't dump to file.");
181      ++i;
182      break;
183     case 'd':
184      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
185      ++i;
186      break;
187     default:
188      usage();
189      return -23;
190    }
191   } else {
192    usage();
193    return -23;
194   }
195  }
196
197  /* setting up device */
198  if(info.mode&MODE_MONITOR) {
199   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
200   puts("set monitoring mode ...");
201   system(sys_call);
202  }
203  sprintf(sys_call,"ifconfig %s up",info.device);
204  puts("setting up device ...");
205  system(sys_call);
206
207  if(info.log_fd==0) {
208  if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT))!=0)
209    puts("using logfile /tmp/hdw-sniff.log ...");
210  else {
211    puts("failed to open logfile ...");
212    return -23;
213  }
214
215  /* pcap */
216  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
217  {
218   printf("%s: %s\n",argv[0],pcap_error);
219   return -23;
220  }
221  pcap_fd=pcap_fileno(info.pcap_handle);
222  /* -> non blocking? */
223
224  /* socket fd for channel hopping */
225  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
226
227  display_init(&(info.display),info.log_fd);
228
229  input_init(&(info.input),info.log_fd);
230  //input.mode=CONTENT_BUFFER;
231  input_ios_init(&info.input);
232
233  event_init(&(info.event),info.log_fd);
234  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
235
236  event_math(0,&(info.event),READ,ADD);
237  event_math(pcap_fd,&(info.event),READ,ADD);
238
239  list_init(&(info.sniffed_sta),info.log_fd);
240
241  display_console(&info,"foo");
242
243  event_start(&(info.event),&info,react_on_event,hop_channel);
244
245  input_shutdown(&(info.input));
246  display_shutdown(&(info.display));
247  
248  puts("");
249  puts("");
250  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
251  puts("");
252  puts("bugreports: hackbard@hackdaworld.org");
253
254  return 23;
255
256 }