moved old stuff to old/, added new stuff
[my-code/hdw-sniff.git] / main.c
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..2926a76
--- /dev/null
+++ b/main.c
@@ -0,0 +1,129 @@
+/*
+ * main.c - main hdw-sniff
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#include <stdio.h>
+#include "main.h"
+
+/* 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 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 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;
+
+ fd_set pcap_fd_set;
+ struct timeval pcap_fd_set_tv;
+
+ struct info_struct info;
+
+
+ memset(&info,0,sizeof(struct info_struct));
+ /* default values */
+ info.caps=0;
+ info.logfile_fd=0;
+ info.quit=0;
+ /* parse arguments */
+ 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;
+     ++i;
+     break;
+    case 'l':
+     if ((info.logfile_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
+      printf("logfile -> %s\n",argv[i+1]);
+     else
+      puts("warning: can't write to logfile.");
+     ++i;
+     break;
+    case 'd':
+     strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
+     ++i;
+     break;
+   }
+  } else usage();
+ }
+
+ /* 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);
+  puts("set monitoring mode ...");
+ }
+ sprintf(sys_call,"ifconfig %s up",info.device);
+ system(sys_call);
+ puts("device up ...");
+
+ /* pcap */
+ if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
+ {
+  printf("%s: %s\n",argv[0],pcap_error);
+  return -23;
+ }
+ pcap_fd=pcap_fileno(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);
+ }
+
+ puts("bugreports: hackbard@hackdaworld.dyndns.org");
+ return 23;
+}