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