typo
[hmcfgusb] / hmland.c
CommitLineData
4732a863 1/* HM-CFG-LAN emulation for HM-CFG-USB
9db2e455
MG
2 *
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <stdint.h>
28#include <string.h>
29#include <strings.h>
30#include <poll.h>
4371275b 31#include <signal.h>
9db2e455 32#include <errno.h>
e0a7146e
MG
33#include <sys/types.h>
34#include <sys/socket.h>
b0bf6ad2 35#include <sys/stat.h>
e6ab4631 36#include <fcntl.h>
fbbbfa37 37#include <time.h>
e0a7146e
MG
38#include <netinet/in.h>
39#include <arpa/inet.h>
9db2e455
MG
40#include <libusb-1.0/libusb.h>
41
c44f15b8 42#include "version.h"
9db2e455
MG
43#include "hexdump.h"
44#include "hmcfgusb.h"
45
b0bf6ad2
MG
46#define PID_FILE "/var/run/hmland.pid"
47
62f25bae
MG
48#define DEFAULT_REBOOT_SECONDS 86400
49
627e3f33
MG
50extern char *optarg;
51
e0a7146e 52static int impersonate_hmlanif = 0;
627e3f33
MG
53static int debug = 0;
54static int verbose = 0;
62f25bae 55static int reboot_seconds = 0;
30a2aa7a
MG
56static int reboot_at_hour = -1;
57static int reboot_at_minute = -1;
e0a7146e 58
d84111c4
MG
59struct queued_rx {
60 char *rx;
61 int len;
62 struct queued_rx *next;
63};
64
65static struct queued_rx *qrx = NULL;
66static int wait_for_h = 0;
67
e75295bb
MG
68#define FLAG_LENGTH_BYTE (1<<0)
69#define FLAG_FORMAT_HEX (1<<1)
70#define FLAG_COMMA_BEFORE (1<<2)
71#define FLAG_COMMA_AFTER (1<<3)
72#define FLAG_NL (1<<4)
73#define FLAG_IGNORE_COMMAS (1<<5)
74
75#define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
76#define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
77
fbbbfa37
MG
78static void print_timestamp(FILE *f)
79{
80 struct timeval tv;
81 struct tm *tmp;
82 char ts[32];
83
84 gettimeofday(&tv, NULL);
85 tmp = localtime(&tv.tv_sec);
86 memset(ts, 0, sizeof(ts));
87 strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
88 fprintf(f, "%s.%06ld: ", ts, tv.tv_usec);
89}
90
e75295bb
MG
91static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags)
92{
51d4ece6
MG
93 const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
94 'A', 'B', 'C', 'D', 'E', 'F'};
e75295bb
MG
95 uint8_t *buf_out = *outpos;
96 uint8_t *outend = *outpos + outlen;
97 uint8_t *inend = *inpos + inlen;
98 int i;
99
100 if (flags & FLAG_COMMA_BEFORE) {
101 CHECK_SPACE(1);
102 **outpos=',';
103 *outpos += 1;
104 }
105
106 if (flags & FLAG_LENGTH_BYTE) {
107 CHECK_AVAIL(1);
108 len = **inpos;
109 *inpos += 1;
110 }
111
112 if (flags & FLAG_FORMAT_HEX) {
e75295bb
MG
113 CHECK_AVAIL(len);
114 CHECK_SPACE(len*2);
115 for (i = 0; i < len; i++) {
51d4ece6
MG
116 **outpos = nibble[((**inpos) & 0xf0) >> 4];
117 *outpos += 1;
118 **outpos = nibble[((**inpos) & 0xf)];
119 *inpos += 1; *outpos += 1;
e75295bb
MG
120 }
121 } else {
122 CHECK_AVAIL(len);
123 CHECK_SPACE(len);
124 memcpy(*outpos, *inpos, len);
125 *outpos += len;
126 *inpos += len;
127 }
128
129 if (flags & FLAG_COMMA_AFTER) {
130 CHECK_SPACE(1);
131 **outpos=',';
132 *outpos += 1;
133 }
134
135 if (flags & FLAG_NL) {
136 CHECK_SPACE(2);
137 **outpos='\r';
138 *outpos += 1;
139 **outpos='\n';
140 *outpos += 1;
141 }
142
143 return *outpos - buf_out;
144}
145
51d4ece6
MG
146static uint8_t ascii_to_nibble(uint8_t a)
147{
148 uint8_t c = 0x00;
149
150 if ((a >= '0') && (a <= '9')) {
151 c = a - '0';
152 } else if ((a >= 'A') && (a <= 'F')) {
153 c = (a - 'A') + 10;
154 } else if ((a >= 'a') && (a <= 'f')) {
155 c = (a - 'a') + 10;
156 }
157
158 return c;
159}
160
e75295bb
MG
161static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags)
162{
163 uint8_t *buf_out = *outpos;
164 uint8_t *outend = *outpos + outlen;
165 uint8_t *inend = *inpos + inlen;
e75295bb
MG
166
167 if (flags & FLAG_LENGTH_BYTE) {
168 int len = 0;
169 uint8_t *ip;
170
171 ip = *inpos;
172 while(ip < inend) {
173 if (*ip == ',') {
174 ip++;
175 if (!(flags & FLAG_IGNORE_COMMAS))
176 break;
177
178 continue;
179 }
180 len++;
181 ip++;
182 }
183 CHECK_SPACE(1);
184 **outpos = (len / 2);
185 *outpos += 1;
186 }
187
188 while(*inpos < inend) {
189 if (**inpos == ',') {
190 *inpos += 1;
191 if (!(flags & FLAG_IGNORE_COMMAS))
192 break;
193
194 continue;
195 }
196
197 CHECK_SPACE(1);
198 CHECK_AVAIL(2);
e75295bb 199
51d4ece6
MG
200 **outpos = ascii_to_nibble(**inpos) << 4;
201 *inpos += 1;
202 **outpos |= ascii_to_nibble(**inpos);
203 *inpos += 1; *outpos += 1;
e75295bb
MG
204 }
205
206 return *outpos - buf_out;
207}
208
4371275b 209static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
9db2e455 210{
e75295bb
MG
211 uint8_t out[1024];
212 uint8_t *outpos;
213 uint8_t *inpos;
e0a7146e 214 int fd = *((int*)data);
4371275b 215 int w;
9db2e455
MG
216
217 if (buf_len < 1)
4371275b 218 return 1;
9db2e455 219
e0a7146e 220 memset(out, 0, sizeof(out));
e75295bb
MG
221 outpos = out;
222 inpos = buf;
e0a7146e 223
e75295bb 224 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
9db2e455
MG
225 switch(buf[0]) {
226 case 'H':
e0a7146e
MG
227 if (impersonate_hmlanif) {
228 buf[5] = 'L';
229 buf[6] = 'A';
230 buf[7] = 'N';
231 }
e75295bb
MG
232 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE);
233 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
234 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE);
235 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
236 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
237 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
238 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
9db2e455
MG
239
240 break;
241 case 'E':
e75295bb
MG
242 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX);
243 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
244 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
245 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
246 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
247 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL);
248
9db2e455
MG
249 break;
250 case 'R':
e75295bb
MG
251 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
252 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
253 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
254 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
255 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
256 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL);
257
9db2e455
MG
258 break;
259 case 'I':
e75295bb
MG
260 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
261 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
262 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
263 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
264
265 break;
9db2e455 266 default:
e75295bb 267 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
9db2e455
MG
268 hexdump(buf, buf_len, "Unknown> ");
269 break;
270 }
d84111c4
MG
271
272 /* Queue packet until first respone to 'K' is received */
273 if (wait_for_h && buf[0] != 'H') {
274 struct queued_rx **rxp = &qrx;
275
276 while (*rxp)
277 rxp = &((*rxp)->next);
278
279 *rxp = malloc(sizeof(struct queued_rx));
280 if (!*rxp) {
281 perror("malloc");
282 return 0;
283 }
284
285 memset(*rxp, 0, sizeof(struct queued_rx));
286 (*rxp)->len = outpos-out;
287 (*rxp)->rx = malloc((*rxp)->len);
288 if (!(*rxp)->rx) {
289 perror("malloc");
290 return 0;
291 }
292 memset((*rxp)->rx, 0, (*rxp)->len);
293 memcpy((*rxp)->rx, out, (*rxp)->len);
294
295 return 1;
296 }
297
62f60cc1
MG
298 if (verbose) {
299 int i;
300
fbbbfa37 301 print_timestamp(stdout);
62f60cc1 302 printf("LAN < ");
11806002 303 for (i = 0; i < outpos-out-2; i++)
62f60cc1 304 printf("%c", out[i]);
11806002 305 printf("\n");
62f60cc1 306 }
4371275b
MG
307
308 w = write(fd, out, outpos-out);
309 if (w <= 0) {
310 perror("write");
311 return 0;
312 }
313
59360394 314 /* Send all queued packets */
d84111c4
MG
315 if (wait_for_h) {
316 struct queued_rx *curr_rx = qrx;
317 struct queued_rx *last_rx;
318
319 while (curr_rx) {
320 if (verbose) {
321 int i;
322
fbbbfa37 323 print_timestamp(stdout);
d84111c4
MG
324 printf("LAN < ");
325 for (i = 0; i < curr_rx->len-2; i++)
326 printf("%c", curr_rx->rx[i]);
327 printf("\n");
328 }
329
330 w = write(fd, curr_rx->rx, curr_rx->len);
331 if (w <= 0) {
332 perror("write");
d84111c4
MG
333 }
334 last_rx = curr_rx;
335 curr_rx = curr_rx->next;
336
337 free(last_rx->rx);
338 free(last_rx);
339 }
340
341 qrx = NULL;
342
343 wait_for_h = 0;
344 }
345
4371275b 346 return 1;
9db2e455
MG
347}
348
e0a7146e 349static int hmlan_parse_in(int fd, void *data)
9db2e455
MG
350{
351 struct hmcfgusb_dev *dev = data;
627e3f33 352 uint8_t buf[1025];
e75295bb
MG
353 uint8_t out[0x40]; //FIXME!!!
354 uint8_t *outpos;
355 uint8_t *inpos;
9db2e455 356 int i;
627e3f33 357 int last;
9db2e455
MG
358 int r;
359
627e3f33
MG
360 memset(buf, 0, sizeof(buf));
361
362 r = read(fd, buf, sizeof(buf)-1);
9db2e455 363 if (r > 0) {
627e3f33
MG
364 uint8_t *inend = buf + r;
365
366 inpos = buf;
367
627e3f33
MG
368 while (inpos < inend) {
369 uint8_t *instart = inpos;
370
371 if ((*inpos == '\r') || (*inpos == '\n')) {
372 inpos++;
373 continue;
9db2e455 374 }
627e3f33
MG
375
376 outpos = out;
9db2e455 377
627e3f33
MG
378 last = inend - inpos;
379
380 for (i = 0; i < last; i++) {
381 if ((inpos[i] == '\r') || (inpos[i] == '\n')) {
382 last = i;
383 break;
384 }
385 }
386
387 if (last == 0)
388 continue;
389
62f60cc1 390 if (verbose) {
fbbbfa37 391 print_timestamp(stdout);
62f60cc1
MG
392 printf("LAN > ");
393 for (i = 0; i < last; i++)
394 printf("%c", instart[i]);
395 printf("\n");
396 }
397
627e3f33
MG
398 memset(out, 0, sizeof(out));
399 *outpos++ = *inpos++;
400
03ca736e 401 switch(*instart) {
627e3f33
MG
402 case 'S':
403 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
404 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
405 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
406 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
407 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
408 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
409 break;
d419ace3
MG
410 case 'Y':
411 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
412 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
413 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
d26506bf 414 break;
627e3f33
MG
415 default:
416 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
417 break;
418 }
e75295bb 419
33df13be 420 hmcfgusb_send(dev, out, sizeof(out), 1);
627e3f33 421 }
9db2e455 422 } else if (r < 0) {
fbbbfa37
MG
423 if (errno != ECONNRESET)
424 perror("read");
e0a7146e
MG
425 return r;
426 } else {
427 return 0;
9db2e455 428 }
e0a7146e
MG
429
430 return 1;
9db2e455
MG
431}
432
23d96b59 433static int comm(int fd_in, int fd_out, int master_socket, int flags)
9db2e455
MG
434{
435 struct hmcfgusb_dev *dev;
23d96b59 436 uint8_t out[0x40]; //FIXME!!!
9db2e455
MG
437 int quit = 0;
438
627e3f33
MG
439 hmcfgusb_set_debug(debug);
440
e0a7146e 441 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
9db2e455
MG
442 if (!dev) {
443 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
e0a7146e
MG
444 return 0;
445 }
446
62f25bae 447 if (dev->bootloader) {
0ddb3740 448 if (verbose)
30a2aa7a 449 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
0ddb3740
MG
450
451 hmcfgusb_leave_bootloader(dev);
452
62f25bae
MG
453 hmcfgusb_close(dev);
454 sleep(1);
455 return 0;
456 }
457
30a2aa7a
MG
458 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
459 struct tm *tm_s;
460 time_t tm;
461
462 tm = time(NULL);
463 tm_s = localtime(&tm);
464 if (tm_s == NULL) {
465 perror("localtime");
466 return 0;
467 }
468
30a2aa7a
MG
469 tm_s->tm_hour = reboot_at_hour;
470 tm_s->tm_min = reboot_at_minute;
471 tm_s->tm_sec = 0;
472
ec00d63b 473 tm = mktime(tm_s);
30a2aa7a 474 reboot_seconds = tm - dev->opened_at;
ec00d63b
MG
475
476 while (reboot_seconds <= 0)
477 reboot_seconds += 86400;
30a2aa7a
MG
478 }
479
480 if (verbose && reboot_seconds)
481 printf("Rebooting in %u seconds\n", reboot_seconds);
482
483
e0a7146e
MG
484 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
485 fprintf(stderr, "Can't add client to pollfd!\n");
486 hmcfgusb_close(dev);
487 return 0;
9db2e455
MG
488 }
489
e0a7146e
MG
490 if (master_socket >= 0) {
491 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
492 fprintf(stderr, "Can't add master_socket to pollfd!\n");
493 hmcfgusb_close(dev);
494 return 0;
495 }
9db2e455
MG
496 }
497
23d96b59
MG
498 memset(out, 0, sizeof(out));
499 out[0] = 'K';
d84111c4 500 wait_for_h = 1;
6262005e 501 hmcfgusb_send_null_frame(dev, 1);
23d96b59 502 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
503
504 while(!quit) {
505 int fd;
506
b55c340d 507 fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */
9db2e455 508 if (fd >= 0) {
e0a7146e
MG
509 if (fd == master_socket) {
510 int client;
511
512 client = accept(master_socket, NULL, 0);
513 if (client >= 0) {
514 shutdown(client, SHUT_RDWR);
515 close(client);
516 }
517 } else {
518 if (hmlan_parse_in(fd, dev) <= 0) {
519 quit = 1;
520 }
521 }
9db2e455
MG
522 } else if (fd == -1) {
523 if (errno) {
1e79d00a
MG
524 if (errno != ETIMEDOUT) {
525 perror("hmcfgusb_poll");
526 quit = 1;
527 } else {
528 /* periodically wakeup the device */
529 hmcfgusb_send_null_frame(dev, 1);
530 if (wait_for_h) {
531 memset(out, 0, sizeof(out));
532 out[0] = 'K';
533 hmcfgusb_send(dev, out, sizeof(out), 1);
534 }
535 }
9db2e455
MG
536 }
537 }
62f25bae
MG
538
539 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
540 if (verbose) {
30a2aa7a 541 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
62f25bae
MG
542 time(NULL) - dev->opened_at);
543 }
544 hmcfgusb_enter_bootloader(dev);
545 }
9db2e455
MG
546 }
547
813afd12 548 hmcfgusb_close(dev);
e0a7146e
MG
549 return 1;
550}
551
b0bf6ad2
MG
552void sigterm_handler(int sig)
553{
554 if (unlink(PID_FILE) == -1)
555 perror("Can't remove PID file");
556
557 exit(EXIT_SUCCESS);
558}
559
560#define FLAG_DAEMON (1 << 0)
561#define FLAG_PID_FILE (1 << 1)
562
563static int socket_server(char *iface, int port, int flags)
e0a7146e 564{
4371275b 565 struct sigaction sact;
e0a7146e
MG
566 struct sockaddr_in sin;
567 int sock;
568 int n;
627e3f33
MG
569 pid_t pid;
570
b0bf6ad2
MG
571 if (flags & FLAG_DAEMON) {
572 FILE *pidfile = NULL;
573
574 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
575 int fd;
576
577 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
578 if (fd == -1) {
579 if (errno == EEXIST) {
580 pid_t old_pid;
581 pidfile = fopen(PID_FILE, "r");
582 if (!pidfile) {
583 perror("PID file " PID_FILE " already exists, already running?");
584 exit(EXIT_FAILURE);
585 }
586
587 if (fscanf(pidfile, "%u", &old_pid) != 1) {
588 fclose(pidfile);
589 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
590 exit(EXIT_FAILURE);
591 }
592
593 fclose(pidfile);
594
595 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
596 exit(EXIT_FAILURE);
597 }
598 perror("Can't create PID file " PID_FILE);
599 exit(EXIT_FAILURE);
600 }
b0bf6ad2 601
e6ab4631 602 pidfile = fdopen(fd, "w");
b0bf6ad2 603 if (!pidfile) {
e6ab4631 604 perror("Can't reopen PID file fd");
b0bf6ad2
MG
605 exit(EXIT_FAILURE);
606 }
607
608 memset(&sact, 0, sizeof(sact));
609 sact.sa_handler = sigterm_handler;
610
611 if (sigaction(SIGTERM, &sact, NULL) == -1) {
612 perror("sigaction(SIGTERM)");
613 exit(EXIT_FAILURE);
614 }
615 }
616
627e3f33
MG
617 pid = fork();
618 if (pid > 0) {
b0bf6ad2
MG
619 if (pidfile) {
620 fprintf(pidfile, "%u\n", pid);
621 fclose(pidfile);
622 }
623
627e3f33
MG
624 printf("Daemon with PID %u started!\n", pid);
625 exit(EXIT_SUCCESS);
626 } else if (pid < 0) {
627 perror("fork");
628 exit(EXIT_FAILURE);
629 }
b0bf6ad2
MG
630
631 if (pidfile)
632 fclose(pidfile);
627e3f33 633 }
e0a7146e 634
4371275b
MG
635 memset(&sact, 0, sizeof(sact));
636 sact.sa_handler = SIG_IGN;
637
638 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 639 perror("sigaction(SIGPIPE)");
27f4063e 640 exit(EXIT_FAILURE);
4371275b
MG
641 }
642
e0a7146e
MG
643 impersonate_hmlanif = 1;
644
645 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
646 if (sock == -1) {
647 perror("Can't open socket");
648 return EXIT_FAILURE;
649 }
650
651 n = 1;
652 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
653 perror("Can't set socket options");
654 return EXIT_FAILURE;
655 }
656
657 memset(&sin, 0, sizeof(sin));
658 sin.sin_family = AF_INET;
659 sin.sin_port = htons(port);
4a8a2269
MG
660 if (!iface) {
661 sin.sin_addr.s_addr = htonl(INADDR_ANY);
662 } else {
663 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
560c3078 664 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
4a8a2269
MG
665 return EXIT_FAILURE;
666 }
667 }
e0a7146e
MG
668
669 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
670 perror("Can't bind socket");
671 return EXIT_FAILURE;
672 }
673
674 if (listen(sock, 1) == -1) {
675 perror("Can't listen on socket");
676 return EXIT_FAILURE;
677 }
678
679 while(1) {
680 struct sockaddr_in csin;
681 socklen_t csinlen;
682 int client;
627e3f33 683 in_addr_t client_addr;
e0a7146e
MG
684
685 memset(&csin, 0, sizeof(csin));
686 csinlen = sizeof(csin);
687 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
688 if (client == -1) {
689 perror("Couldn't accept client");
690 continue;
691 }
692
627e3f33
MG
693 /* FIXME: getnameinfo... */
694 client_addr = ntohl(csin.sin_addr.s_addr);
695
696 if (verbose) {
fbbbfa37 697 print_timestamp(stdout);
627e3f33
MG
698 printf("Client %d.%d.%d.%d connected!\n",
699 (client_addr & 0xff000000) >> 24,
700 (client_addr & 0x00ff0000) >> 16,
701 (client_addr & 0x0000ff00) >> 8,
702 (client_addr & 0x000000ff));
703 }
e0a7146e 704
23d96b59 705 comm(client, client, sock, flags);
e0a7146e
MG
706
707 shutdown(client, SHUT_RDWR);
708 close(client);
709
627e3f33 710 if (verbose) {
fbbbfa37 711 print_timestamp(stdout);
627e3f33
MG
712 printf("Connection to %d.%d.%d.%d closed!\n",
713 (client_addr & 0xff000000) >> 24,
714 (client_addr & 0x00ff0000) >> 16,
715 (client_addr & 0x0000ff00) >> 8,
716 (client_addr & 0x000000ff));
717 }
e09806b6 718 sleep(1);
e0a7146e
MG
719 }
720
721 return EXIT_SUCCESS;
722}
723
23d96b59 724static int interactive_server(int flags)
e0a7146e 725{
23d96b59 726 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 727 return EXIT_FAILURE;
9db2e455
MG
728
729 return EXIT_SUCCESS;
730}
e0a7146e 731
627e3f33
MG
732void hmlan_syntax(char *prog)
733{
734 fprintf(stderr, "Syntax: %s options\n\n", prog);
735 fprintf(stderr, "Possible options:\n");
30a2aa7a
MG
736 fprintf(stderr, "\t-D\t\tdebug mode\n");
737 fprintf(stderr, "\t-d\t\tdaemon mode\n");
738 fprintf(stderr, "\t-h\t\tthis help\n");
739 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
740 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
741 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
742 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
743 fprintf(stderr, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS);
744 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
745 fprintf(stderr, "\t-v\t\tverbose mode\n");
c44f15b8 746 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
627e3f33
MG
747
748}
749
e0a7146e
MG
750int main(int argc, char **argv)
751{
627e3f33 752 int port = 1000;
4a8a2269 753 char *iface = NULL;
627e3f33 754 int interactive = 0;
b0bf6ad2 755 int flags = 0;
627e3f33
MG
756 char *ep;
757 int opt;
62f25bae
MG
758
759 reboot_seconds = DEFAULT_REBOOT_SECONDS;
627e3f33 760
c44f15b8 761 while((opt = getopt(argc, argv, "DdhiPp:Rr:l:vV")) != -1) {
627e3f33
MG
762 switch (opt) {
763 case 'D':
764 debug = 1;
765 verbose = 1;
766 break;
767 case 'd':
b0bf6ad2 768 flags |= FLAG_DAEMON;
627e3f33
MG
769 break;
770 case 'i':
771 interactive = 1;
772 break;
b0bf6ad2
MG
773 case 'P':
774 flags |= FLAG_PID_FILE;
775 break;
627e3f33
MG
776 case 'p':
777 port = strtoul(optarg, &ep, 10);
778 if (*ep != '\0') {
779 fprintf(stderr, "Can't parse port!\n");
780 exit(EXIT_FAILURE);
781 }
782 break;
23d96b59 783 case 'R':
b55c340d 784 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 785 break;
62f25bae
MG
786 case 'r':
787 reboot_seconds = strtoul(optarg, &ep, 10);
788 if (*ep != '\0') {
30a2aa7a
MG
789 if (*ep == ':') {
790 reboot_at_hour = reboot_seconds;
791 ep++;
792 reboot_at_minute = strtoul(ep, &ep, 10);
793 if (*ep != '\0') {
794 fprintf(stderr, "Can't parse reboot-time!\n");
795 exit(EXIT_FAILURE);
796 }
797
798 reboot_seconds = 0;
799 } else {
800 fprintf(stderr, "Can't parse reboot-timeout!\n");
801 exit(EXIT_FAILURE);
802 }
62f25bae
MG
803 }
804 break;
4a8a2269
MG
805 case 'l':
806 iface = optarg;
807 break;
627e3f33
MG
808 case 'v':
809 verbose = 1;
810 break;
c44f15b8
MG
811 case 'V':
812 printf("hmland " VERSION "\n");
813 printf("Copyright (c) 2013 Michael Gernoth\n\n");
814 exit(EXIT_SUCCESS);
627e3f33
MG
815 case 'h':
816 case ':':
817 case '?':
818 default:
819 hmlan_syntax(argv[0]);
820 exit(EXIT_FAILURE);
821 break;
822 }
823 }
824
825 if (interactive) {
23d96b59 826 return interactive_server(flags);
627e3f33 827 } else {
b0bf6ad2 828 return socket_server(iface, port, flags);
627e3f33 829 }
e0a7146e 830}
Impressum, Datenschutz