summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/daemon/source/daemon.cc
blob: d77d98707d0274fd46e6079bd457857046bbd31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Startup functions of wellenreiter
 *
 * $Id$
 */

#include "config.hh"
#include "daemon.hh"

/* should be parsed from cfg-file */
#define MAXCHANNEL 13
#define CHANINTERVAL 500000

/* Main function of wellenreiterd */
int main(int argc, char **argv)
{
  int sock, maxfd, retval;
  char buffer[WL_SOCKBUF];
  struct pcap_pkthdr header;
  struct sockaddr_in saddr;
  pcap_t *handletopcap;
  wl_cardtype_t cardtype;
  pthread_t sub;
  const unsigned char *packet;

  fd_set rset;

  fprintf(stderr, "wellenreiterd %s\n\n", VERSION);
  fprintf(stderr, "(c) 2002 by M-M-M\n\n");

  if(argc < 3)
    usage();
  
  /* Set sniffer device */
  memset(cardtype.iface, 0, sizeof(cardtype.iface));
  strncpy(cardtype.iface, (char *)argv[1], sizeof(cardtype.iface) - 1);

  /* Set card type */
  cardtype.type = atoi(argv[2]);
  if(cardtype.type < 1 || cardtype.type > 4)
    usage();

  /* set card into monitor mode */
  if(!card_into_monitormode(&handletopcap, cardtype.iface, 
			    cardtype.type))
  {
    wl_logerr("Cannot initialize the wireless-card, aborting");
    exit(EXIT_FAILURE);
  }
  wl_loginfo("Set card into monitor mode");

  /* setup pcap */
  if((handletopcap = pcap_open_live(cardtype.iface, 
				    BUFSIZ, 1, 0, NULL)) == NULL)
  {
    wl_logerr("pcap_open_live() failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  
#ifdef HAVE_PCAP_NONBLOCK
  pcap_setnonblock(handletopcap, 1, NULL);
#endif
  
  /* Setup socket for incoming commands */
  if((sock=wl_setupsock(DAEMONADDR, DAEMONPORT, saddr)) < 0)
  {
    wl_logerr("Cannot setup socket");
    exit(EXIT_FAILURE);
  } 
  wl_loginfo("Set up socket '%d' for GUI communication", sock);

  /* Create channelswitching thread */
  if(pthread_create(&sub, NULL, channel_switcher, 
		    (void *)&cardtype) != 0)
  {
    wl_logerr("Cannot create thread: %s", strerror(errno));
    close(sock);
    exit(EXIT_FAILURE);
  } 
  if(pthread_detach(sub))
  {
    wl_logerr("Error detaching thread");
    close(sock);
    pthread_exit((pthread_t *)sub);
    exit(EXIT_FAILURE);
  }
  wl_loginfo("Created and detached channel switching thread");
  
  FD_ZERO(&rset);

  /* Start main loop */
  wl_loginfo("Starting main loop");
  while(1)
  {

    FD_SET(sock, &rset);
    FD_SET(pcap_fileno(handletopcap), &rset);

    /* maxfd = biggest filefd */
    maxfd = (sock > pcap_fileno(handletopcap) ? 
	     sock + 1 : pcap_fileno(handletopcap)) + 1;

    if(select(maxfd, &rset, NULL, NULL, NULL) < 0)
    {
      wl_logerr("Error calling select: %s", strerror(errno));
      break;
    }     

    /* Got data on local socket from GUI */
    if(FD_ISSET(sock, &rset))
    {
      /* Receive data from socket */
      if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0)
      {
	wl_logerr("Error trying to read: %s", strerror(errno));
	break;
      }
      else
      {
	/* check type of packet and start function according to it */
	switch(retval)
	{
	  case 98:
	   wl_loginfo("Received STARTSNIFF command");
	   break;
	  case 99:
	    wl_loginfo("Received STOPSNIFF command");
	    break;
	  default:
	    wl_logerr("Received unknown command: %d", retval);
	    break;
	}
      }
    } /* FD_ISSET */

    /* Check pcap lib for packets */
    if(FD_ISSET(pcap_fileno(handletopcap), &rset))
    {

      /* Grab one single packet */
      packet = pcap_next(handletopcap, &header);

      /* process the packet */
      process_packets(&header,*&packet, GUIADDR, GUIPORT);
    }

  } /* while(1) */
  
  close(sock);
  exit(EXIT_SUCCESS);
}

void 
usage(void)
{
  fprintf(stderr, "Usage: wellenreiter <device> <cardtype>\n"      \
	  "\t<device>   = Wirelessdevice (e.g. wlan0)\n" \
	  "\t<cardtype> = Cardtype:\tCisco\t= 1\n"       \
	  "\t\t\t\tNG\t= 2\n"                             \
          "\t\t\t\tHOSTAP\t= 3\n"			  \
	  "\t\t\t\tLUCENT\t= 4\n");
  exit(EXIT_FAILURE);
}

void * 
channel_switcher(void *cardtypeptr)
{
  wl_cardtype_t *cardtype;
  int maxchan=0;
  int channel=1;
  /* Get card info struct */
  cardtype = (wl_cardtype_t *)cardtypeptr;
  /* Get from the wireless extension the amount of available channels
     this prevents a fail in switching on us cards */
  maxchan = card_detect_channels(cardtype->iface);
  
  if (maxchan < MAXCHANNEL)
  {
  	#undef MAXCHANNEL
  	#define MAXCHANNEL maxchan
  }
  
  while(1)
  {

    /* If channel bigger than maxchannel, set to 1 */
    if(channel > MAXCHANNEL)
      channel=1;
    
    /* Set channel */
    if(!card_set_channel(cardtype->iface, channel, cardtype->type))
    {
      wl_logerr("Cannot set channel, thread exiting");
      pthread_exit(NULL);
    }
    
    /* sleep */
    usleep(CHANINTERVAL);
    
    channel++;
  } /* while */
}