summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/daemon/source/daemon.cc
blob: b3a37b6dd5d6d1672f3bda1fba4d2d33c8fc0e77 (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
/*
 * Startup functions of wellenreiter
 *
 * $Id$
 */

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

/* Main function of wellenreiterd */
int main(int argc, char **argv)
{
  int sock, maxfd, guiport=GUIPORT;
  char guihost[]="127.0.0.1";
  struct sockaddr_in *cliaddr;
  socklen_t len=sizeof(struct sockaddr);
  char buffer[128];
  FILE *fp=stdin; /* Will be replaced with sniffer */
  fd_set rset;

  fprintf(stderr, "wellenreiterd %s\n\n", VERSION);

  /* Setup socket for incoming commands */
  if((sock=commsock(DAEMONADDR, DAEMONPORT)) < 0)
  {
    wl_logerr("Cannot setup socket");
    exit(-1);
  }
  wl_loginfo("Set up socket '%d' for GUI communication", sock);

  FD_ZERO(&rset);

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

    FD_SET(sock, &rset);
    FD_SET(fileno(fp), &rset);
    maxfd=sock+fileno(fp)+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))
    {
      memset(buffer, 0, sizeof(buffer));
      if(recvfrom(sock, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)cliaddr, &len) < 0)
      {
	 wl_logerr("Cannot read from socket: %s", strerror(errno));
	 break;
      }
      wl_loginfo("Received command from '%s': %s", inet_ntoa(cliaddr->sin_addr), buffer); 

      /* Pass string to analyze function */
      //      sendcomm(guihost, guiport, buffer);

    }

    /* Will be replaced with sniffer ... later */
    if(FD_ISSET(fileno(fp), &rset))
    {
      memset(buffer, 0, sizeof(buffer));
      if(fgets(buffer, sizeof(buffer) - 1, fp) == NULL)
      {
	 wl_logerr("Cannot read from stdin: %s", strerror(errno));
	 break;
      }
      wl_loginfo("Sending command to '%s': %s", GUIADDR, buffer);

      /* Send string to GUI */
      sendcomm(guihost, guiport, "%d: %s", 1234, buffer);

    }
  }
  close(sock);
  exit(0);
}