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