info view mode introduced (still buggy)
[my-code/hdw-sniff.git] / main.c
diff --git a/main.c b/main.c
index a0f5575..dee4596 100644 (file)
--- a/main.c
+++ b/main.c
 /*
  * main.c - main hdw-sniff
  *
- * Copyright (C) 2004 hackbard@hackdaworld.dyndns.org
+ * Copyright (C) 2004/05 hackbard@hackdaworld.org
  *
  */
 
 #include "main.h"
 
-void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package);
+// void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package);
 
 /* functions */
 
-int get_user_event(t_info *info) {
-  char event;
-  if(read(0,&event,1)!=1) {
-   perror("reading user interaction failed");
-   return -23;
+int display_console(t_info *info,char *string) {
+
+  int x,y,size,count,cnt,i,s;
+  t_display *display;
+  t_sta *sta;
+  char help[32];
+
+  display=(t_display *)&(info->display);
+
+  x=display->max_x-1;
+  y=display->max_y-1;
+
+  size=strlen(string);
+
+  if(info->view==VIEW_ALL) {
+    count=list_count(&(info->sniffed_sta));
+    cnt=((count>y-5)?y-5:count);
+
+    display_line(display,0,0,x,0,'-');
+    display_string(display,GUI_OFFSET_STA,1,"sta/ap",6);
+    display_string(display,GUI_OFFSET_SSID,1,"| ssid",6);
+    display_string(display,GUI_OFFSET_AP,1,"| ap",4);
+    display_string(display,GUI_OFFSET_WEP,1,"| wep",5);
+    display_string(display,GUI_OFFSET_MGMT,1,"| mgmt",6);
+    display_string(display,GUI_OFFSET_CTRL,1,"| ctrl",6);
+    display_string(display,GUI_OFFSET_DATA,1,"| data",6);
+    display_string(display,GUI_OFFSET_ACTIVE,1,"| act",5);
+    display_line(display,0,2,x,2,'-');
+
+    list_reset(&(info->sniffed_sta));
+
+    for(i=0;i<cnt;i++) {
+      sta=(t_sta *)info->sniffed_sta.current->data;
+      snprintf(help,32,"%02x:%02x:%02x:%02x:%02x:%02x",
+               sta->addr[0],sta->addr[1],sta->addr[2],
+               sta->addr[3],sta->addr[4],sta->addr[5]);
+      display_string(display,GUI_OFFSET_STA,3+i,help,17);
+      s=strlen(sta->ssid);
+      s=((s>=16)?16:s);
+      help[0]='|'; help[1]=' ';
+      memcpy(&help[2],sta->ssid,s);
+      display_string(display,GUI_OFFSET_SSID,3+i,help,s+2);
+      snprintf(help,4,"| %c",sta->ap?'y':'n');
+      display_string(display,GUI_OFFSET_AP,3+i,help,3);
+      snprintf(help,5,"|  %c",sta->wep?'y':'n');
+      display_string(display,GUI_OFFSET_WEP,3+i,help,4);
+/*
+      snprintf(help,21,"| %04d | %04d | %04d",
+               sta->count_mgmt,sta->count_ctrl,
+               sta->count_data);
+*/
+      snprintf(help,21,"| %04d | n.a. | n.a.",
+               sta->count_mgmt);
+      display_string(display,GUI_OFFSET_MGMT,3+i,help,20);
+      snprintf(help,5,"|  %c",sta->active);
+      display_string(display,GUI_OFFSET_ACTIVE,3+i,help,4);
+      list_next(&(info->sniffed_sta));
+    }
+  }
+
+  else if(info->view==VIEW_SINGLE) {
+    list_reset(&(info->sniffed_sta));
+    for(i=0;i<info->c_line;i++) list_next(&(info->sniffed_sta));
+    sta=(t_sta *)info->sniffed_sta.current->data;
+    display_line(display,0,0,x,0,'-');
+    snprintf(help,22,"sta %02x:%02x:%02x:%02x:%02x:%02x",
+             sta->addr[0],sta->addr[1],sta->addr[2],
+             sta->addr[3],sta->addr[4],sta->addr[5]);
+    display_string(display,1,1,help,21);
+    display_line(display,0,2,x,2,'-');
+    display_string(display,1,3,info->clean_line,x);
+    display_string(display,1,3,"ssid: ",6);
+    s=strlen(sta->ssid);
+    memcpy(help,sta->ssid,s);
+    display_string(display,7,3,help,s);
+    display_string(display,1,5,"more specific info soon ...",27);
+  }
+
+  /* footer */
+  display_line(display,0,y-3,x,y-3,'-');
+
+  snprintf(help,14,"total: %06d",info->count);
+  display_string(display,1,y-2,help,13);
+  snprintf(help,13,"mgmt: %06d",info->count_m);
+  display_string(display,15,y-2,help,12);
+  snprintf(help,12,"ctrl: %06d",info->count_c);
+  display_string(display,28,y-2,help,11);
+  snprintf(help,13,"data: %06d",info->count_d);
+  display_string(display,40,y-2,help,12);
+
+  display_line(display,0,y-1,x,y-1,'-');
+
+  if(size!=0) {
+    display_string(display,0,y,info->clean_line,x);
+    display_string(display,1,y,string,size);
+  }
+
+  display_draw(display);
+
+  if(info->view==VIEW_ALL)
+    display_set_cursor(display,0,3+info->c_line);
+
+
+  return 23;
+}
+
+int noop(t_input *input,void *ptr) {
+
+  return 23;
+}
+
+int get_user_interaction(t_info *info) {
+
+  char *string;
+  int hlp=0;
+
+  string=(char *)malloc(info->display.max_x*sizeof(char));
+
+  input_get_event(&(info->input),noop,info);
+
+  /*
+  if(info->input.content[0]=='h') {
+    display_console(info,"hdw-sniff help:");
+    display_console(info,"h - print this help");
+    display_console(info,"x - enable/disable hex output");
+    display_console(info,"a - enable/disable ascii output");
+    display_console(info,"q - quit");
+    display_console(info,"console navigation: arrow up/down");
+    display_console(info,"network list navigation: page up/down");
+  }
+  */
+
+  if(info->input.content[0]=='x') {
+    info->mode^=MODE_HEXOUT;
+    display_console(info,"toggled hex output");
+  }
+
+  else if(info->input.content[0]=='a') {
+    info->mode^=MODE_ASCIIOUT;
+    display_console(info,"toggled ascii output");
+  }
+
+  else if(info->input.content[0]=='q') {
+    event_stop(&(info->event));
+    display_console(info,"quit!");
+  }
+  else if(info->input.content[0]=='n') {
+    hlp=list_count(&(info->sniffed_sta)); 
+    if(info->c_line<hlp-1) info->c_line++;
   }
-  printf("user event: %c ",event);
-  if(event=='h') {
-   info->mode^=MODE_HEXOUT;
-   printf("- hex output: %c\n",info->mode&MODE_HEXOUT?'a':'n');
+  else if(info->input.content[0]=='p') {
+    if(info->c_line>0) info->c_line--;
   }
-  if(event=='a') {
-   info->mode^=MODE_ASCIIOUT;
-   printf("- ascii output: %c\n",info->mode&MODE_ASCIIOUT?'a':'n');
+  else if(info->input.content[0]==0x0d) {
+    display_clear_screen(&(info->display));
+    if(info->view==VIEW_ALL) info->view=VIEW_SINGLE;
+    else info->view=VIEW_ALL;
   }
-  if(event=='q') {
-   info->mode|=MODE_QUIT;
-   printf("- shutting down!\n");
+  else {
+    snprintf(string,info->display.max_x,"unknown event (%x)",
+             info->input.content[0]);
+    display_console(info,string);
   }
 
   return 23;
 }
 
+int react_on_event(t_event *event,void *ptr) {
+
+  t_info *info;
+
+  info=(t_info *)ptr;
+
+  if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
+  else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
+
+  return 23;
+}
+
 int usage(void) {
   puts("usage: hdw-sniff <options>");
   puts("\toptions:");
@@ -41,15 +197,22 @@ int usage(void) {
   puts("\t\t-d <device> \twlan0,eth0");
   puts("\t\t-l <logfile>");
   puts("\t\t-k <key> \t(string)");
+  puts("\t\t-D <file> \t(dump packages to file)");
   puts("\t\t-h \tdisplay this help message");
   puts("");
+
+  return 23;
 }
 
-int hop_channel(t_info *info) {
+int hop_channel(t_event *event,void *ptr) {
  
   struct iwreq iwreq;
+  t_info *info;
+
+  info=(t_info *)ptr;
 
-  if(info->current_channel>=CHANNEL_MAX) info->current_channel=1;
+  if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
+    info->current_channel=1;
   memset(&iwreq,0,sizeof(iwreq));
   strcpy(iwreq.ifr_name,info->device);
   iwreq.u.freq.e=0;
@@ -59,29 +222,25 @@ int hop_channel(t_info *info) {
     perror("ioctl");
     return -23;
   }
- ++(info->current_channel);
- return 23;
+  ++(info->current_channel);
+
+  return 23;
 }
 
 int main(int argc, char **argv) {
 
   t_info info;
   int pcap_fd;
-  fd_set fds;
-  struct timeval hop_f;
   int i;
   char sys_call[MAX_SYSCALL_CHARS];
   char pcap_error[PCAP_ERRBUF_SIZE];
 
   memset(&info,0,sizeof(t_info));
+
  /* parse arguments */
- for(i=1;i<argc;i++)
- {
-  if(argv[i][0]=='-')
-  {
-   switch(argv[i][1])
-   {
+ for(i=1;i<argc;i++) {
+  if(argv[i][0]=='-') {
+   switch(argv[i][1]) {
     case 'h':
      usage();
     case 'm':
@@ -100,12 +259,19 @@ int main(int argc, char **argv) {
      ++i;
      break;
     case 'l':
-     if ((info.logfile_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
+     if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
       printf("logfile -> %s\n",argv[i+1]);
      else
       puts("warning: can't write to logfile.");
      ++i;
      break;
+    case 'D':
+     if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
+      printf("dump file -> %s\n",argv[i+1]);
+     else
+      puts("warning: can't dump to file.");
+     ++i;
+     break;
     case 'd':
      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
      ++i;
@@ -130,6 +296,15 @@ int main(int argc, char **argv) {
  puts("setting up device ...");
  system(sys_call);
 
+ if(info.log_fd==0) {
+   if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
+     puts("using logfile /tmp/hdw-sniff.log ...");
+   else {
+     puts("failed to open logfile ...");
+     return -23;
+   }
+ }
+
  /* pcap */
  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
  {
@@ -142,32 +317,53 @@ int main(int argc, char **argv) {
  /* socket fd for channel hopping */
  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
 
- /* parse packages until user breaks */
- while(!(info.mode&MODE_QUIT))
- {
-  /* watch pcap_fd and stdin (reading) */
-  FD_ZERO(&fds);
-  FD_SET(pcap_fd,&fds);
-  FD_SET(0,&fds);
-  hop_f.tv_sec=HOP_SEC;
-  hop_f.tv_usec=HOP_USEC;
-
-  if(select(pcap_fd+1,&fds,NULL,NULL,&hop_f)) {
-   if(FD_ISSET(0,&fds)) 
-    get_user_event(&info);
-   else if(FD_ISSET(pcap_fd,&fds))
-    pcap_dispatch(info.pcap_handle,-1,parse_package,(unsigned char *)&info);
-   else
-    hop_channel(&info);
-  }
+ display_init(&(info.display),info.log_fd);
+ if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
+   display_shutdown(&(info.display));
+   puts("");
+   puts("");
+   puts("fatal! please increase your terminals width");
+   puts("");
+   return -23;
  }
+ if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
+   display_shutdown(&(info.display));
+   puts("");
+   puts("");
+   puts("fatal! please increase your terminals width");
+   puts("");
+   return -23;
+ }
+ info.clean_line=(char *)malloc(info.display.max_x);
+ memset(info.clean_line,0x20,info.display.max_x);
+ info.view=VIEW_ALL;
+
+ display_console(&info,
+   "Welcome to hdw-sniff, Copyright (C) 2004/05 hackbard");
+ sleep(2);
+
+ input_init(&(info.input),info.log_fd);
+ input_ios_init(&info.input);
+
+ list_init(&(info.sniffed_sta),info.log_fd);
+
+ event_init(&(info.event),info.log_fd);
+ event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
 
+ event_math(0,&(info.event),READ,ADD);
+ event_math(pcap_fd,&(info.event),READ,ADD);
+
+ event_start(&(info.event),&info,react_on_event,hop_channel);
+
+ list_shutdown(&(info.sniffed_sta));
+ input_shutdown(&(info.input));
+ display_shutdown(&(info.display));
  puts("");
  puts("");
- puts("thanks for using hdw-sniff (C) 2005 hackbard");
+ puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
  puts("");
- puts("bugreports: hackbard@hackdaworld.dyndns.org");
+ puts("bugreports: hackbard@hackdaworld.org");
 
  return 23;
-
 }