commit soon and often .. (nothing working by now ;)
[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-h \tdisplay this help message");
107   puts("");
108
109   return 23;
110 }
111
112 int hop_channel(t_event *event,void *ptr) {
113  
114   struct iwreq iwreq;
115   t_info *info;
116
117   info=(t_info *)ptr;
118
119   if(info->current_channel>=CHANNEL_MAX) info->current_channel=1;
120   memset(&iwreq,0,sizeof(iwreq));
121   strcpy(iwreq.ifr_name,info->device);
122   iwreq.u.freq.e=0;
123   iwreq.u.freq.m=info->current_channel;
124   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
125     puts("unable to hop channel");
126     perror("ioctl");
127     return -23;
128   }
129  ++(info->current_channel);
130  return 23;
131 }
132
133 int main(int argc, char **argv) {
134
135   t_info info;
136   int pcap_fd;
137   int logfd;
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  {
147   if(argv[i][0]=='-')
148   {
149    switch(argv[i][1])
150    {
151     case 'h':
152      usage();
153     case 'm':
154      if(!strncmp(argv[i+1],"monitor",7)) {
155       info.mode|=MODE_MONITOR;
156       puts("will go to monitor mode.");
157      }
158      else if(!strncmp(argv[i+1],"wlanng",6)) {
159       info.mode|=MODE_WLANNG;
160       puts("expecting wlanng header in package.");
161      }
162      else {
163       printf("unknown mode: %s\n",argv[1]);
164       return -23;
165      }
166      ++i;
167      break;
168     case 'l':
169      if ((info.logfile_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
170       printf("logfile -> %s\n",argv[i+1]);
171      else
172       puts("warning: can't write to logfile.");
173      ++i;
174      break;
175     case 'd':
176      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
177      ++i;
178      break;
179     default:
180      usage();
181      return -23;
182    }
183   } else {
184    usage();
185    return -23;
186   }
187  }
188
189  /* setting up device */
190  if(info.mode&MODE_MONITOR) {
191   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
192   puts("set monitoring mode ...");
193   system(sys_call);
194  }
195  sprintf(sys_call,"ifconfig %s up",info.device);
196  puts("setting up device ...");
197  system(sys_call);
198
199  // todo
200  logfd=open("/tmp/hdw-sniff.log",O_WRONLY|O_CREAT);
201
202  /* pcap */
203  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
204  {
205   printf("%s: %s\n",argv[0],pcap_error);
206   return -23;
207  }
208  pcap_fd=pcap_fileno(info.pcap_handle);
209  /* -> non blocking? */
210
211  /* socket fd for channel hopping */
212  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
213
214  display_init(&(info.display),logfd);
215
216  input_init(&(info.input),logfd);
217  //input.mode=CONTENT_BUFFER;
218  input_ios_init(&info.input);
219
220  event_init(&(info.event),logfd);
221  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
222
223  event_math(0,&(info.event),READ,ADD);
224  event_math(pcap_fd,&(info.event),READ,ADD);
225
226  display_console(&info,"foo");
227
228  event_start(&(info.event),&info,react_on_event,hop_channel);
229
230  input_shutdown(&(info.input));
231  display_shutdown(&(info.display));
232  
233  puts("");
234  puts("");
235  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
236  puts("");
237  puts("bugreports: hackbard@hackdaworld.org");
238
239  return 23;
240
241 }