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