a0f557584e3a5ff19da5b02d5f67bd9dae20c95d
[my-code/hdw-sniff.git] / main.c
1 /*
2  * main.c - main hdw-sniff
3  *
4  * Copyright (C) 2004 hackbard@hackdaworld.dyndns.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 get_user_event(t_info *info) {
15   char event;
16   if(read(0,&event,1)!=1) {
17    perror("reading user interaction failed");
18    return -23;
19   }
20   printf("user event: %c ",event);
21   if(event=='h') {
22    info->mode^=MODE_HEXOUT;
23    printf("- hex output: %c\n",info->mode&MODE_HEXOUT?'a':'n');
24   }
25   if(event=='a') {
26    info->mode^=MODE_ASCIIOUT;
27    printf("- ascii output: %c\n",info->mode&MODE_ASCIIOUT?'a':'n');
28   }
29   if(event=='q') {
30    info->mode|=MODE_QUIT;
31    printf("- shutting down!\n");
32   }
33
34   return 23;
35 }
36
37 int usage(void) {
38   puts("usage: hdw-sniff <options>");
39   puts("\toptions:");
40   puts("\t\t-m <mode> \tmonitor and/or wlanng");
41   puts("\t\t-d <device> \twlan0,eth0");
42   puts("\t\t-l <logfile>");
43   puts("\t\t-k <key> \t(string)");
44   puts("\t\t-h \tdisplay this help message");
45   puts("");
46 }
47
48 int hop_channel(t_info *info) {
49  
50   struct iwreq iwreq;
51
52   if(info->current_channel>=CHANNEL_MAX) info->current_channel=1;
53   memset(&iwreq,0,sizeof(iwreq));
54   strcpy(iwreq.ifr_name,info->device);
55   iwreq.u.freq.e=0;
56   iwreq.u.freq.m=info->current_channel;
57   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
58     puts("unable to hop channel");
59     perror("ioctl");
60     return -23;
61   }
62  ++(info->current_channel);
63  return 23;
64 }
65
66 int main(int argc, char **argv) {
67
68   t_info info;
69   int pcap_fd;
70   fd_set fds;
71   struct timeval hop_f;
72   int i;
73   char sys_call[MAX_SYSCALL_CHARS];
74   char pcap_error[PCAP_ERRBUF_SIZE];
75
76   memset(&info,0,sizeof(t_info));
77  
78  /* parse arguments */
79  for(i=1;i<argc;i++)
80  {
81   if(argv[i][0]=='-')
82   {
83    switch(argv[i][1])
84    {
85     case 'h':
86      usage();
87     case 'm':
88      if(!strncmp(argv[i+1],"monitor",7)) {
89       info.mode|=MODE_MONITOR;
90       puts("will go to monitor mode.");
91      }
92      else if(!strncmp(argv[i+1],"wlanng",6)) {
93       info.mode|=MODE_WLANNG;
94       puts("expecting wlanng header in package.");
95      }
96      else {
97       printf("unknown mode: %s\n",argv[1]);
98       return -23;
99      }
100      ++i;
101      break;
102     case 'l':
103      if ((info.logfile_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
104       printf("logfile -> %s\n",argv[i+1]);
105      else
106       puts("warning: can't write to logfile.");
107      ++i;
108      break;
109     case 'd':
110      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
111      ++i;
112      break;
113     default:
114      usage();
115      return -23;
116    }
117   } else {
118    usage();
119    return -23;
120   }
121  }
122
123  /* setting up device */
124  if(info.mode&MODE_MONITOR) {
125   sprintf(sys_call,"iwconfig %s mode monitor",info.device);
126   puts("set monitoring mode ...");
127   system(sys_call);
128  }
129  sprintf(sys_call,"ifconfig %s up",info.device);
130  puts("setting up device ...");
131  system(sys_call);
132
133  /* pcap */
134  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
135  {
136   printf("%s: %s\n",argv[0],pcap_error);
137   return -23;
138  }
139  pcap_fd=pcap_fileno(info.pcap_handle);
140  /* -> non blocking? */
141
142  /* socket fd for channel hopping */
143  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
144
145  /* parse packages until user breaks */
146  while(!(info.mode&MODE_QUIT))
147  {
148   /* watch pcap_fd and stdin (reading) */
149   FD_ZERO(&fds);
150   FD_SET(pcap_fd,&fds);
151   FD_SET(0,&fds);
152   hop_f.tv_sec=HOP_SEC;
153   hop_f.tv_usec=HOP_USEC;
154
155   if(select(pcap_fd+1,&fds,NULL,NULL,&hop_f)) {
156    if(FD_ISSET(0,&fds)) 
157     get_user_event(&info);
158    else if(FD_ISSET(pcap_fd,&fds))
159     pcap_dispatch(info.pcap_handle,-1,parse_package,(unsigned char *)&info);
160    else
161     hop_channel(&info);
162   }
163  }
164
165  puts("");
166  puts("");
167  puts("thanks for using hdw-sniff (C) 2005 hackbard");
168  puts("");
169  puts("bugreports: hackbard@hackdaworld.dyndns.org");
170
171  return 23;
172
173 }