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