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