info view mode introduced (still buggy)
[my-code/hdw-sniff.git] / main.c
diff --git a/main.c b/main.c
index 9e8258f..dee4596 100644 (file)
--- a/main.c
+++ b/main.c
 /*
  * main.c - main hdw-sniff
  *
- * author: hackbard@hackdaworld.dyndns.org
+ * Copyright (C) 2004/05 hackbard@hackdaworld.org
  *
  */
 
-#include <stdio.h>
 #include "main.h"
 
+// void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package);
+
 /* functions */
-int usage(void)
-{
- puts("usage: hdw-sniff <options>");
- puts("\toptions:\t-m <mode> \t1 monitoring, 2 managed");
- puts("\t\t-d <device> \twlan0,eth0");
- puts("\t\t-l <logfile>");
- puts("\t\t-h \tdisplay this help message");
- 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 hop_channel(info_struct *info)
-{
- struct iwreq iwreq;
- if(info->current_channel>=CHANNEL_MAX) info->current_channel=1;
- memset(&iwreq,0,sizeof(iwreq));
- strcpy(iwreq.ifr_name,info->device);
- iwreq.u.freq.e=0;
- iwreq.u.freq.m=info->current_channel;
- if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0)
- {
-  puts("unable to hop channel");
-  perror("ioctl");
-  return -23;
- }
- ++(info->current_channel);
- return 23;
+int noop(t_input *input,void *ptr) {
+
+  return 23;
 }
 
-int main(int argc, char **argv)
-{
- /* local variables */
- char pcap_error[PCAP_ERRBUF_SIZE];
- char sys_call[SYSCALL_MAX];
- int pcap_fd,channel_hop_fd;;
- int i;
+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++;
+  }
+  else if(info->input.content[0]=='p') {
+    if(info->c_line>0) info->c_line--;
+  }
+  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;
+  }
+  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;
 
- fd_set pcap_fd_set;
- struct timeval pcap_fd_set_tv;
+  info=(t_info *)ptr;
 
- struct info_struct info;
+  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:");
+  puts("\t\t-m <mode> \tmonitor and/or wlanng");
+  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("");
 
- memset(&info,0,sizeof(struct info_struct));
- /* default values */
- info.caps=0;
- info.logfile_fd=0;
- info.quit=0;
+  return 23;
+}
+
+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==0))
+    info->current_channel=1;
+  memset(&iwreq,0,sizeof(iwreq));
+  strcpy(iwreq.ifr_name,info->device);
+  iwreq.u.freq.e=0;
+  iwreq.u.freq.m=info->current_channel;
+  if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
+    puts("unable to hop channel");
+    perror("ioctl");
+    return -23;
+  }
+  ++(info->current_channel);
+
+  return 23;
+}
+
+int main(int argc, char **argv) {
+
+  t_info info;
+  int pcap_fd;
+  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':
-     info.caps=((1<<atoi(argv[i+1]))&CAP_MODE_MASK)|info.caps;
+     if(!strncmp(argv[i+1],"monitor",7)) {
+      info.mode|=MODE_MONITOR;
+      puts("will go to monitor mode.");
+     }
+     else if(!strncmp(argv[i+1],"wlanng",6)) {
+      info.mode|=MODE_WLANNG;
+      puts("expecting wlanng header in package.");
+     }
+     else {
+      printf("unknown mode: %s\n",argv[1]);
+      return -23;
+     }
      ++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;
      break;
+    default:
+     usage();
+     return -23;
    }
-  } else usage();
+  } else {
+   usage();
+   return -23;
+  }
  }
 
  /* setting up device */
- if((info.caps&CAP_MODE_MASK)==MONITORING_MODE)
- {
-  sprintf(sys_call,"iwpriv %s monitor %d",info.device,IWPRIV_M_MODE);
-  system(sys_call);
+ if(info.mode&MODE_MONITOR) {
+  sprintf(sys_call,"iwconfig %s mode monitor",info.device);
   puts("set monitoring mode ...");
+  system(sys_call);
  }
  sprintf(sys_call,"ifconfig %s up",info.device);
+ puts("setting up device ...");
  system(sys_call);
- puts("device up ...");
+
+ 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)
@@ -103,27 +311,59 @@ int main(int argc, char **argv)
   printf("%s: %s\n",argv[0],pcap_error);
   return -23;
  }
- pcap_fd=pcap_fileno(pcap_handle);
+ pcap_fd=pcap_fileno(info.pcap_handle);
  /* -> non blocking? */
 
- info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
  /* socket fd for channel hopping */
- /* watch pcap_fd for reading */
- FD_ZERO(&pcap_fd);
- FD_SET(pcap_fd,&pcap_fd_set);
- fd_set_tv.tv_sec=PCAP_SELECT_SEC;
- pcap_fd_set_tv.tv_usec=PCAP_SELECT_USEC;
-
- /* parse packages until user breaks */
- while(!(info.caps&CAP_QUIT_MASK))
- {
-  if(select(pcap_fd+1,&pcap_fd_set,NULL,NULL,&pcap_fd_set_tv))
-   pcap_dispatch(pcap_handle,-1,parse_package,(unsigned char *)&info);
-  else
-   hop_channel(&info);
+ info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
+
+ 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) 2004/05 hackbard");
+ puts("");
+ puts("bugreports: hackbard@hackdaworld.org");
 
- puts("bugreports: hackbard@hackdaworld.dyndns.org");
  return 23;
 }