]> git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
35f819280fb4ca0d36bbd5b289fe90bbeac628cf
[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(const char *buf, int len, const 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, size_t len, int flags)
146 {
147 uint8_t *buf_out = *outpos;
148 uint8_t *outend = *outpos + outlen;
149 uint8_t *inend = *inpos + inlen;
150 size_t 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 size_t 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 break;
435 default:
436 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
437 break;
438 }
439
440 hmcfgusb_send(dev, out, sizeof(out), 1);
441
442 return 1;
443 }
444
445 static int hmlan_parse_in(int fd, void *data)
446 {
447 uint8_t *newbuf;
448 int r;
449 int i;
450
451 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
452 if (!newbuf) {
453 perror("realloc");
454 return 0;
455 }
456 lan_read_buf = newbuf;
457 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
458 if (r > 0) {
459 lan_read_buflen += r;
460 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
461 if (verbose)
462 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
463 return -1;
464 }
465 while(lan_read_buflen > 0) {
466 int found = 0;
467
468 for (i = 0; i < lan_read_buflen; i++) {
469 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
470 if (i > 0)
471 hmlan_parse_one(lan_read_buf, i, data);
472 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
473 lan_read_buflen -= (i + 1);
474 found = 1;
475 break;
476 }
477 if (i > LAN_MAX_LINE_LENGTH) {
478 if (verbose)
479 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
480 return -1;
481 }
482 }
483 if (!found)
484 break;
485 newbuf = realloc(lan_read_buf, lan_read_buflen);
486 if (lan_read_buflen && !newbuf) {
487 perror("realloc");
488 return 0;
489 }
490 lan_read_buf = newbuf;
491 }
492 } else if (r < 0) {
493 if (errno != ECONNRESET)
494 perror("read");
495 return r;
496 } else {
497 return 0;
498 }
499
500 return 1;
501 }
502
503 static int comm(int fd_in, int fd_out, int master_socket, int flags)
504 {
505 struct hmcfgusb_dev *dev;
506 uint8_t out[0x40]; //FIXME!!!
507 int quit = 0;
508
509 hmcfgusb_set_debug(debug);
510
511 dev = hmcfgusb_init(hmlan_format_out, &fd_out, serial);
512 if (!dev) {
513 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
514 return 0;
515 }
516
517 if (dev->bootloader) {
518 if (verbose)
519 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
520
521 hmcfgusb_leave_bootloader(dev);
522
523 hmcfgusb_close(dev);
524 sleep(1);
525 return 0;
526 }
527
528 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
529 struct tm *tm_s;
530 time_t tm;
531
532 tm = time(NULL);
533 tm_s = localtime(&tm);
534 if (tm_s == NULL) {
535 perror("localtime");
536 return 0;
537 }
538
539 tm_s->tm_hour = reboot_at_hour;
540 tm_s->tm_min = reboot_at_minute;
541 tm_s->tm_sec = 0;
542
543 tm = mktime(tm_s);
544 reboot_seconds = tm - dev->opened_at;
545
546 while (reboot_seconds <= 0)
547 reboot_seconds += 86400;
548 }
549
550 if (verbose && reboot_seconds)
551 printf("Rebooting in %u seconds\n", reboot_seconds);
552
553
554 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
555 fprintf(stderr, "Can't add client to pollfd!\n");
556 hmcfgusb_close(dev);
557 return 0;
558 }
559
560 if (master_socket >= 0) {
561 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
562 fprintf(stderr, "Can't add master_socket to pollfd!\n");
563 hmcfgusb_close(dev);
564 return 0;
565 }
566 }
567
568 memset(out, 0, sizeof(out));
569 out[0] = 'K';
570 wait_for_h = 1;
571 hmcfgusb_send_null_frame(dev, 1);
572 hmcfgusb_send(dev, out, sizeof(out), 1);
573
574 while(!quit) {
575 int fd;
576
577 fd = hmcfgusb_poll(dev, POLL_TIMEOUT_MS);
578 if (fd >= 0) {
579 if (fd == master_socket) {
580 int client;
581
582 client = accept(master_socket, NULL, 0);
583 if (client >= 0) {
584 shutdown(client, SHUT_RDWR);
585 close(client);
586 }
587 } else {
588 if (hmlan_parse_in(fd, dev) <= 0) {
589 quit = 1;
590 }
591 }
592 } else if (fd == -1) {
593 if (errno) {
594 if (errno != ETIMEDOUT) {
595 perror("hmcfgusb_poll");
596 quit = 1;
597 } else {
598 /* periodically wakeup the device */
599 hmcfgusb_send_null_frame(dev, 1);
600 if (wait_for_h) {
601 memset(out, 0, sizeof(out));
602 out[0] = 'K';
603 hmcfgusb_send(dev, out, sizeof(out), 1);
604 }
605 }
606 }
607 }
608
609 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
610 if (verbose) {
611 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
612 time(NULL) - dev->opened_at);
613 }
614 hmcfgusb_enter_bootloader(dev);
615 }
616 }
617
618 hmcfgusb_close(dev);
619 return 1;
620 }
621
622 void sigterm_handler(int sig)
623 {
624 if (unlink(PID_FILE) == -1)
625 perror("Can't remove PID file");
626
627 exit(EXIT_SUCCESS);
628 }
629
630 #define FLAG_DAEMON (1 << 0)
631 #define FLAG_PID_FILE (1 << 1)
632
633 static int socket_server(char *iface, int port, int flags)
634 {
635 struct sigaction sact;
636 struct sockaddr_in sin;
637 int sock;
638 int n;
639 pid_t pid;
640
641 if (flags & FLAG_DAEMON) {
642 FILE *pidfile = NULL;
643
644 if (flags & FLAG_PID_FILE) {
645 int fd;
646
647 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
648 if (fd == -1) {
649 if (errno == EEXIST) {
650 pid_t old_pid;
651 pidfile = fopen(PID_FILE, "r");
652 if (!pidfile) {
653 perror("PID file " PID_FILE " already exists, already running?");
654 exit(EXIT_FAILURE);
655 }
656
657 if (fscanf(pidfile, "%d", &old_pid) != 1) {
658 fclose(pidfile);
659 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
660 exit(EXIT_FAILURE);
661 }
662
663 fclose(pidfile);
664
665 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
666 exit(EXIT_FAILURE);
667 }
668 perror("Can't create PID file " PID_FILE);
669 exit(EXIT_FAILURE);
670 }
671
672 pidfile = fdopen(fd, "w");
673 if (!pidfile) {
674 perror("Can't reopen PID file fd");
675 exit(EXIT_FAILURE);
676 }
677
678 memset(&sact, 0, sizeof(sact));
679 sact.sa_handler = sigterm_handler;
680
681 if (sigaction(SIGTERM, &sact, NULL) == -1) {
682 perror("sigaction(SIGTERM)");
683 exit(EXIT_FAILURE);
684 }
685 }
686
687 pid = fork();
688 if (pid > 0) {
689 if (pidfile) {
690 fprintf(pidfile, "%u\n", pid);
691 fclose(pidfile);
692 }
693
694 printf("Daemon with PID %u started!\n", pid);
695 exit(EXIT_SUCCESS);
696 } else if (pid < 0) {
697 perror("fork");
698 exit(EXIT_FAILURE);
699 }
700
701 if (pidfile)
702 fclose(pidfile);
703 }
704
705 memset(&sact, 0, sizeof(sact));
706 sact.sa_handler = SIG_IGN;
707
708 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
709 perror("sigaction(SIGPIPE)");
710 exit(EXIT_FAILURE);
711 }
712
713 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
714 if (sock == -1) {
715 perror("Can't open socket");
716 return EXIT_FAILURE;
717 }
718
719 n = 1;
720 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
721 perror("Can't set socket options");
722 return EXIT_FAILURE;
723 }
724
725 memset(&sin, 0, sizeof(sin));
726 sin.sin_family = AF_INET;
727 sin.sin_port = htons(port);
728 if (!iface) {
729 sin.sin_addr.s_addr = htonl(INADDR_ANY);
730 } else {
731 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
732 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
733 return EXIT_FAILURE;
734 }
735 }
736
737 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
738 perror("Can't bind socket");
739 return EXIT_FAILURE;
740 }
741
742 if (listen(sock, 1) == -1) {
743 perror("Can't listen on socket");
744 return EXIT_FAILURE;
745 }
746
747 while(1) {
748 struct sockaddr_in csin;
749 socklen_t csinlen;
750 int client;
751 in_addr_t client_addr;
752
753 memset(&csin, 0, sizeof(csin));
754 csinlen = sizeof(csin);
755 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
756 if (client == -1) {
757 perror("Couldn't accept client");
758 continue;
759 }
760
761 /* FIXME: getnameinfo... */
762 client_addr = ntohl(csin.sin_addr.s_addr);
763
764 write_log(NULL, 0, "Client %d.%d.%d.%d connected!\n",
765 (client_addr & 0xff000000) >> 24,
766 (client_addr & 0x00ff0000) >> 16,
767 (client_addr & 0x0000ff00) >> 8,
768 (client_addr & 0x000000ff));
769
770 comm(client, client, sock, flags);
771
772 shutdown(client, SHUT_RDWR);
773 close(client);
774
775 if (lan_read_buf)
776 free(lan_read_buf);
777 lan_read_buf = NULL;
778 lan_read_buflen = 0;
779
780 write_log(NULL, 0, "Connection to %d.%d.%d.%d closed!\n",
781 (client_addr & 0xff000000) >> 24,
782 (client_addr & 0x00ff0000) >> 16,
783 (client_addr & 0x0000ff00) >> 8,
784 (client_addr & 0x000000ff));
785 sleep(1);
786 }
787
788 return EXIT_SUCCESS;
789 }
790
791 static int interactive_server(int flags)
792 {
793 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
794 return EXIT_FAILURE;
795
796 return EXIT_SUCCESS;
797 }
798
799 void hmlan_syntax(char *prog)
800 {
801 fprintf(stderr, "Syntax: %s options\n\n", prog);
802 fprintf(stderr, "Possible options:\n");
803 fprintf(stderr, "\t-D\t\tdebug mode\n");
804 fprintf(stderr, "\t-d\t\tdaemon mode\n");
805 fprintf(stderr, "\t-h\t\tthis help\n");
806 fprintf(stderr, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
807 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
808 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
809 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
810 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
811 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
812 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);
813 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
814 fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
815 fprintf(stderr, "\t-v\t\tverbose mode\n");
816 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
817
818 }
819
820 int main(int argc, char **argv)
821 {
822 int port = 1000;
823 char *iface = NULL;
824 int interactive = 0;
825 int flags = 0;
826 char *ep;
827 int opt;
828
829 while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
830 switch (opt) {
831 case 'D':
832 debug = 1;
833 verbose = 1;
834 break;
835 case 'd':
836 flags |= FLAG_DAEMON;
837 break;
838 case 'I':
839 impersonate_hmlanif = 1;
840 break;
841 case 'i':
842 interactive = 1;
843 break;
844 case 'P':
845 flags |= FLAG_PID_FILE;
846 break;
847 case 'p':
848 port = strtoul(optarg, &ep, 10);
849 if (*ep != '\0') {
850 fprintf(stderr, "Can't parse port!\n");
851 exit(EXIT_FAILURE);
852 }
853 break;
854 case 'R':
855 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
856 break;
857 case 'r':
858 reboot_seconds = strtoul(optarg, &ep, 10);
859 if (*ep != '\0') {
860 if (*ep == ':') {
861 reboot_at_hour = reboot_seconds;
862 ep++;
863 reboot_at_minute = strtoul(ep, &ep, 10);
864 if (*ep != '\0') {
865 fprintf(stderr, "Can't parse reboot-time!\n");
866 exit(EXIT_FAILURE);
867 }
868
869 reboot_seconds = 0;
870 } else {
871 fprintf(stderr, "Can't parse reboot-timeout!\n");
872 exit(EXIT_FAILURE);
873 }
874 }
875 reboot_set = 1;
876 break;
877 case 'l':
878 iface = optarg;
879 break;
880 case 'L':
881 logfile = fopen(optarg, "a");
882 if (!logfile) {
883 perror("fopen(logfile)");
884 exit(EXIT_FAILURE);
885 }
886 break;
887 case 'S':
888 serial = optarg;
889 break;
890 case 'v':
891 verbose = 1;
892 break;
893 case 'V':
894 printf("hmland " VERSION "\n");
895 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
896 exit(EXIT_SUCCESS);
897 case 'h':
898 case ':':
899 case '?':
900 default:
901 hmlan_syntax(argv[0]);
902 exit(EXIT_FAILURE);
903 break;
904 }
905 }
906
907 if (interactive) {
908 return interactive_server(flags);
909 } else {
910 return socket_server(iface, port, flags);
911 }
912 }
Impressum, Datenschutz