Sun, 18 May 2014 22:56:43 +0200
Initial server code added
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2008-2014 |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with EC-65K; see the file COPYING. If not, write to the Free |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | #include "../lib/mbselib.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #include "server.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern bool my_shutdown; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern bool debug; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
43 | 32 | extern sys_config Config; |
33 | extern int clients; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
43 | 35 | int s; /* connected socket */ |
36 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | |
43 | 38 | struct sockaddr_in myaddr_in; /* for local socket address */ |
39 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
40 | ||
41 | struct hostent *hp; | |
42 | ||
43 | #define SS_BUFSIZE 1024 | |
44 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | char *exe_cmd(char *); |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | char *exe_cmd(char *cmd) |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | { |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | static char obuf[1024]; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | int i; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | if (strncmp(cmd, "lcd", 3) == 0) { |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | for (i = 0; i < 20; i++) { |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | obuf[i] = lcdbuf[lcdHandle][i][0]; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | obuf[i+20] = lcdbuf[lcdHandle][i][1]; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | obuf[i+40] = lcdbuf[lcdHandle][i][2]; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | obuf[i+60] = lcdbuf[lcdHandle][i][3]; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | obuf[80] = '\0'; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | return obuf; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | return (char *)"ERROR"; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | |
43 | 71 | int server_init(void) |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | { |
43 | 73 | int optval = 1; |
74 | ||
75 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); | |
76 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
77 | myaddr_in.sin_family = AF_INET; | |
78 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
79 | myaddr_in.sin_port = htons(Config.my_port); | |
80 | ||
81 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
82 | if (ls == -1) { | |
83 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
84 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
85 | return 1; | |
86 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | |
43 | 88 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { |
89 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
90 | close(ls); | |
91 | return 1; | |
92 | } | |
93 | ||
94 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
95 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
96 | close(ls); | |
97 | return 1; | |
98 | } | |
99 | ||
100 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
101 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
102 | close(ls); | |
103 | return 1; | |
104 | } | |
105 | ||
106 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | if (debug) |
43 | 108 | fprintf(stdout, "listen socket created %d\n", ls); |
109 | ||
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | ||
115 | ||
116 | /* | |
117 | * Send message to client | |
118 | */ | |
119 | int srv_send(const char *format, ...) | |
120 | { | |
121 | char out[SS_BUFSIZE]; | |
122 | va_list va_ptr; | |
123 | ||
124 | if (s == -1) | |
125 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | |
43 | 127 | va_start(va_ptr, format); |
128 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
129 | va_end(va_ptr); | |
130 | ||
131 | if (send(s, out, strlen(out), 0) != strlen(out)) { | |
132 | syslog(LOG_NOTICE, "srv_send failed"); | |
133 | return -1; | |
134 | } | |
135 | ||
136 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
137 | syslog(LOG_NOTICE, "srv_send failed"); | |
138 | return -1; | |
139 | } | |
140 | ||
141 | syslog(LOG_NOTICE, "send: \"%s\"", out); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
146 | |
43 | 147 | void cmd_die(int onsig) |
148 | { | |
149 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
150 | close(s); | |
151 | exit(0); | |
152 | } | |
153 | ||
154 | ||
155 | ||
156 | void cmd_server(void) | |
157 | { | |
158 | char *hostname, buf[SS_BUFSIZE]; | |
159 | int i, rc, rlen, timer; | |
160 | socklen_t fromlen; | |
161 | struct pollfd pfd[1]; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | |
43 | 163 | /* |
164 | * Close listen socket | |
165 | */ | |
166 | close(ls); | |
167 | /* | |
168 | * Install private signal handler. | |
169 | */ | |
170 | for (i = 0; i < NSIG; i++) { | |
171 | if ((i == SIGHUP) || (i == SIGPIPE) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV)) | |
172 | signal(i, (void (*))cmd_die); | |
173 | else | |
174 | signal(i, SIG_IGN); | |
175 | } | |
176 | ||
177 | hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr, sizeof(struct in_addr), peeraddr_in.sin_family); | |
178 | if (hp == NULL) { | |
179 | hostname = inet_ntoa(peeraddr_in.sin_addr); | |
180 | } else { | |
181 | hostname = hp->h_name; | |
182 | } | |
183 | ||
184 | clients++; | |
185 | syslog(LOG_NOTICE, "Start new client connection (%d) from %s port %u", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
186 | timer = SS_TIMEOUT * 4; | |
187 | ||
188 | /* | |
189 | * Receive loop | |
190 | */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | for (;;) { |
43 | 192 | /* |
193 | * Poll socket until a define timeout of 0,25 second. | |
194 | */ | |
195 | pfd[0].fd = s; | |
196 | pfd[0].events = POLLIN; | |
197 | pfd[0].revents = 0; | |
198 | rc = poll(pfd, 1, 250); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | |
43 | 200 | if (rc == -1) { |
201 | /* | |
202 | * Poll can be interrupted by a finished child so that's not a real error. | |
203 | */ | |
204 | if (errno != EINTR) { | |
205 | syslog(LOG_NOTICE, "poll() rc=%d sock=%d events=%04x", rc, s, pfd[0].revents); | |
206 | } | |
207 | } else if (rc) { | |
208 | if (pfd[0].revents & POLLIN) { | |
209 | memset((char *)&buf, 0, SS_BUFSIZE); | |
210 | fromlen = sizeof(peeraddr_in); | |
211 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
212 | if (rlen == -1) { | |
213 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
214 | } else { | |
215 | for (i = 0; i < strlen(buf); i++) { | |
216 | if (buf[i] == '\n') | |
217 | buf[i] = '\0'; | |
218 | if (buf[i] == '\r') | |
219 | buf[i] = '\0'; | |
220 | } | |
221 | timer = SS_TIMEOUT * 4; | |
222 | if (strlen(buf)) { | |
223 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
224 | ||
225 | /* | |
226 | * Process command from the client | |
227 | */ | |
228 | srv_send(exe_cmd(buf)); | |
229 | } | |
230 | } | |
231 | } else { | |
232 | syslog(LOG_NOTICE, "poll other event"); | |
233 | } | |
234 | ||
235 | } else { | |
236 | /* | |
237 | * Poll timeout, do some housekeeping | |
238 | */ | |
239 | if (timer) { | |
240 | timer--; | |
241 | } else { | |
242 | /* Inactivity timeout */ | |
243 | break; | |
244 | } | |
245 | if (my_shutdown) { | |
246 | break; | |
247 | } | |
248 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
249 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
250 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
251 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
252 | |
43 | 253 | |
254 | PI_THREAD (my_server_loop) | |
255 | { | |
256 | socklen_t addrlen; | |
257 | ||
258 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
259 | if (debug) | |
260 | fprintf(stdout, "Thread my_server_loop started\n"); | |
261 | ||
262 | /* | |
263 | * Loop forever until the external shutdown variable is set. | |
264 | */ | |
265 | for (;;) { | |
266 | ||
267 | addrlen = sizeof(struct sockaddr_in); | |
268 | /* | |
269 | * This call will block until a new connection | |
270 | * arrives. Then it will return the address of | |
271 | * the connecting peer, and a new socket | |
272 | * descriptor, s, for that connection. | |
273 | */ | |
274 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
275 | if (s == -1) { | |
276 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
277 | if (debug) | |
278 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
279 | return 1; | |
280 | } | |
281 | ||
282 | switch (fork()) { | |
283 | case -1: /* | |
284 | * Can't fork, just continue. | |
285 | */ | |
286 | return 1; | |
287 | case 0: /* | |
288 | * Child process, the commandline server. | |
289 | */ | |
290 | cmd_server(); | |
291 | return 0; | |
292 | default: /* | |
293 | * Daemon process comes here. The daemon | |
294 | * needs to remember to close the new | |
295 | * accept socket after forking the child. | |
296 | */ | |
297 | close(s); | |
298 | } | |
299 | ||
300 | if (my_shutdown) { | |
301 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
302 | if (debug) | |
303 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | } | |
308 | } | |
309 | ||
310 | ||
311 |