commit soon and often .. (nothing working by now ;)
[my-code/hdw-sniff.git] / main.c
diff --git a/main.c b/main.c
index a0f5575..2c5a41f 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;
+  t_display *display;
+
+  display=(t_display *)&(info->display);
+
+  x=display->max_x-1;
+  y=display->max_y-1;
+
+  display_line(display,0,0,2,0,'-');
+  display_string(display,4,0,"essid",5);
+  display_line(display,10,0,x,0,'-');
+
+  //display_line(display,0,0,0,y,'|');
+  //display_line(display,x,0,x,y,'|');
+
+  display_line(display,0,y-1,x,y-1,'-');
+
+  display_draw(display);
+
+  return 23;
+}
+
+int noop(t_input *input,void *ptr) {
+
+  return 23;
+}
+
+int get_user_interaction(t_info *info) {
+
+  char *string;
+
+  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");
   }
-  printf("user event: %c ",event);
-  if(event=='h') {
-   info->mode^=MODE_HEXOUT;
-   printf("- hex output: %c\n",info->mode&MODE_HEXOUT?'a':'n');
+  */
+
+  if(info->input.content[0]=='x') {
+    info->mode^=MODE_HEXOUT;
+    display_console(info,"toggled hex output");
   }
-  if(event=='a') {
-   info->mode^=MODE_ASCIIOUT;
-   printf("- ascii output: %c\n",info->mode&MODE_ASCIIOUT?'a':'n');
+
+  else if(info->input.content[0]=='a') {
+    info->mode^=MODE_ASCIIOUT;
+    display_console(info,"toggled ascii output");
   }
-  if(event=='q') {
-   info->mode|=MODE_QUIT;
-   printf("- shutting down!\n");
+
+  else if(info->input.content[0]=='q') {
+    event_stop(&(info->event));
+    display_console(info,"quit!");
+  }
+
+  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:");
@@ -43,11 +105,16 @@ int usage(void) {
   puts("\t\t-k <key> \t(string)");
   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;
   memset(&iwreq,0,sizeof(iwreq));
@@ -67,8 +134,7 @@ int main(int argc, char **argv) {
 
   t_info info;
   int pcap_fd;
-  fd_set fds;
-  struct timeval hop_f;
+  int logfd;
   int i;
   char sys_call[MAX_SYSCALL_CHARS];
   char pcap_error[PCAP_ERRBUF_SIZE];
@@ -130,6 +196,9 @@ int main(int argc, char **argv) {
  puts("setting up device ...");
  system(sys_call);
 
+ // todo
+ logfd=open("/tmp/hdw-sniff.log",O_WRONLY|O_CREAT);
+
  /* pcap */
  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
  {
@@ -142,31 +211,30 @@ 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),logfd);
+
+ input_init(&(info.input),logfd);
+ //input.mode=CONTENT_BUFFER;
+ input_ios_init(&info.input);
 
+ event_init(&(info.event),logfd);
+ event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
+
+ event_math(0,&(info.event),READ,ADD);
+ event_math(pcap_fd,&(info.event),READ,ADD);
+
+ display_console(&info,"foo");
+
+ event_start(&(info.event),&info,react_on_event,hop_channel);
+
+ 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;