added ignore file
[my-code/hdw-sniff.git] / main.c
1 /*
2  * main.c - main hdw-sniff
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include "main.h"
10
11 /* functions */
12 int usage(void)
13 {
14  puts("usage: hdw-sniff <options>");
15  puts("\toptions:\t-m <mode> \t1 monitoring, 2 managed");
16  puts("\t\t-d <device> \twlan0,eth0");
17  puts("\t\t-l <logfile>");
18  puts("\t\t-h \tdisplay this help message");
19  return -23;
20 }
21
22 int hop_channel(info_struct *info)
23 {
24  struct iwreq iwreq;
25  if(info->current_channel>=CHANNEL_MAX) info->current_channel=1;
26  memset(&iwreq,0,sizeof(iwreq));
27  strcpy(iwreq.ifr_name,info->device);
28  iwreq.u.freq.e=0;
29  iwreq.u.freq.m=info->current_channel;
30  if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0)
31  {
32   puts("unable to hop channel");
33   perror("ioctl");
34   return -23;
35  }
36  ++(info->current_channel);
37  return 23;
38 }
39
40 int main(int argc, char **argv)
41 {
42  /* local variables */
43  char pcap_error[PCAP_ERRBUF_SIZE];
44  char sys_call[SYSCALL_MAX];
45  
46  int pcap_fd,channel_hop_fd;;
47  int i;
48
49  fd_set pcap_fd_set;
50  struct timeval pcap_fd_set_tv;
51
52  struct info_struct info;
53
54
55  memset(&info,0,sizeof(struct info_struct));
56  /* default values */
57  info.caps=0;
58  info.logfile_fd=0;
59  info.quit=0;
60  
61  /* parse arguments */
62  for(i=1;i<argc;i++)
63  {
64   if(argv[i][0]=='-')
65   {
66    switch(argv[i][1])
67    {
68     case 'h':
69      usage();
70     case 'm':
71      info.caps=((1<<atoi(argv[i+1]))&CAP_MODE_MASK)|info.caps;
72      ++i;
73      break;
74     case 'l':
75      if ((info.logfile_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
76       printf("logfile -> %s\n",argv[i+1]);
77      else
78       puts("warning: can't write to logfile.");
79      ++i;
80      break;
81     case 'd':
82      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
83      ++i;
84      break;
85    }
86   } else usage();
87  }
88
89  /* setting up device */
90  if((info.caps&CAP_MODE_MASK)==MONITORING_MODE)
91  {
92   sprintf(sys_call,"iwpriv %s monitor %d",info.device,IWPRIV_M_MODE);
93   system(sys_call);
94   puts("set monitoring mode ...");
95  }
96  sprintf(sys_call,"ifconfig %s up",info.device);
97  system(sys_call);
98  puts("device up ...");
99
100  /* pcap */
101  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
102  {
103   printf("%s: %s\n",argv[0],pcap_error);
104   return -23;
105  }
106  pcap_fd=pcap_fileno(pcap_handle);
107  /* -> non blocking? */
108
109  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
110  /* socket fd for channel hopping */
111  
112  /* watch pcap_fd for reading */
113  FD_ZERO(&pcap_fd);
114  FD_SET(pcap_fd,&pcap_fd_set);
115  fd_set_tv.tv_sec=PCAP_SELECT_SEC;
116  pcap_fd_set_tv.tv_usec=PCAP_SELECT_USEC;
117
118  /* parse packages until user breaks */
119  while(!(info.caps&CAP_QUIT_MASK))
120  {
121   if(select(pcap_fd+1,&pcap_fd_set,NULL,NULL,&pcap_fd_set_tv))
122    pcap_dispatch(pcap_handle,-1,parse_package,(unsigned char *)&info);
123   else
124    hop_channel(&info);
125  }
126
127  puts("bugreports: hackbard@hackdaworld.dyndns.org");
128  return 23;
129 }