Diffstat (limited to 'noncore/net/wellenreiter/daemon/source/daemon.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/daemon/source/daemon.cc | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/noncore/net/wellenreiter/daemon/source/daemon.cc b/noncore/net/wellenreiter/daemon/source/daemon.cc new file mode 100644 index 0000000..7972c0f --- a/dev/null +++ b/noncore/net/wellenreiter/daemon/source/daemon.cc | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | * Startup functions of wellenreiter | ||
3 | * | ||
4 | * $Id$ | ||
5 | */ | ||
6 | |||
7 | #include "config.hh" | ||
8 | #include "daemon.hh" | ||
9 | #include "log.hh" | ||
10 | #include "sendgui.hh" | ||
11 | #include "getgui.hh" | ||
12 | |||
13 | /* Main function of wellenreiterd */ | ||
14 | int main(int argc, char **argv) | ||
15 | { | ||
16 | int sock, maxfd; | ||
17 | struct sockaddr_in *cliaddr; | ||
18 | socklen_t len=sizeof(struct sockaddr); | ||
19 | char buffer[128]; | ||
20 | FILE *fp=stdin; /* Will be replaced with sniffer */ | ||
21 | fd_set rset; | ||
22 | |||
23 | fprintf(stderr, "wellenreiterd %s\n\n", VERSION); | ||
24 | |||
25 | /* Setup socket for incoming commands */ | ||
26 | if(!commsock(&sock)) | ||
27 | return 0; | ||
28 | |||
29 | log_info("Set up socket '%d' for GUI communication", sock); | ||
30 | |||
31 | FD_ZERO(&rset); | ||
32 | |||
33 | /* Start main loop */ | ||
34 | log_info("Starting main loop"); | ||
35 | while(1) | ||
36 | { | ||
37 | |||
38 | FD_SET(sock, &rset); | ||
39 | FD_SET(fileno(fp), &rset); | ||
40 | maxfd=sock+fileno(fp)+1; | ||
41 | if(select(maxfd, &rset, NULL, NULL, NULL) < 0) | ||
42 | { | ||
43 | log_err("Error calling select: %s", strerror(errno)); | ||
44 | break; | ||
45 | } | ||
46 | |||
47 | /* Got data on local socket from GUI */ | ||
48 | if(FD_ISSET(sock, &rset)) | ||
49 | { | ||
50 | memset(buffer, 0, sizeof(buffer)); | ||
51 | if(recvfrom(sock, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)cliaddr, &len) < 0) | ||
52 | { | ||
53 | log_err("Cannot read from socket: %s", strerror(errno)); | ||
54 | break; | ||
55 | } | ||
56 | log_info("Received command from '%s': %s", inet_ntoa(cliaddr->sin_addr), buffer); | ||
57 | |||
58 | /* Pass string to analyze function */ | ||
59 | commstring(buffer); | ||
60 | |||
61 | } | ||
62 | |||
63 | /* Will be replaced with sniffer ... later */ | ||
64 | if(FD_ISSET(fileno(fp), &rset)) | ||
65 | { | ||
66 | memset(buffer, 0, sizeof(buffer)); | ||
67 | if(fgets(buffer, sizeof(buffer) - 1, fp) == NULL) | ||
68 | { | ||
69 | log_err("Cannot read from stdin: %s", strerror(errno)); | ||
70 | break; | ||
71 | } | ||
72 | |||
73 | /* Send string to GUI */ | ||
74 | sendgui("%d: %s", 1234, buffer); | ||
75 | |||
76 | } | ||
77 | |||
78 | } | ||
79 | |||
80 | close(sock); | ||
81 | return 0; | ||
82 | } | ||