]> cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
README: fix link to BidCoS packet documentation
[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 | FLAG_NL);
293
294 if (!reboot_set) {
295 int new_reboot_seconds;
296
297 if (version < 0x03c7) {
298 new_reboot_seconds = DEFAULT_REBOOT_SECONDS;
299 } else {
300 new_reboot_seconds = 0;
301 }
302
303 if (verbose && new_reboot_seconds && (reboot_seconds != new_reboot_seconds))
304 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
305 new_reboot_seconds, version);
306
307 reboot_seconds = new_reboot_seconds;
308 }
309
310 break;
311 case 'E':
312 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, 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 'R':
321 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
322 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
323 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, 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);
325 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
326 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);
327
328 break;
329 case 'I':
330 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
331 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
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 | FLAG_NL);
334
335 break;
336 case 'G':
337 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_NL);
338
339 break;
340 default:
341 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
342 hexdump(buf, buf_len, "Unknown> ");
343 break;
344 }
345
346 /* Queue packet until first respone to 'K' is received */
347 if (wait_for_h && buf[0] != 'H') {
348 struct queued_rx **rxp = &qrx;
349
350 while (*rxp)
351 rxp = &((*rxp)->next);
352
353 *rxp = malloc(sizeof(struct queued_rx));
354 if (!*rxp) {
355 perror("malloc");
356 return 0;
357 }
358
359 memset(*rxp, 0, sizeof(struct queued_rx));
360 (*rxp)->len = outpos-out;
361 (*rxp)->rx = malloc((*rxp)->len);
362 if (!(*rxp)->rx) {
363 perror("malloc");
364 return 0;
365 }
366 memset((*rxp)->rx, 0, (*rxp)->len);
367 memcpy((*rxp)->rx, out, (*rxp)->len);
368
369 return 1;
370 }
371
372 write_log((char*)out, outpos-out-2, "LAN < ");
373
374 w = write(fd, out, outpos-out);
375 if (w <= 0) {
376 perror("write");
377 return 0;
378 }
379
380 /* Send all queued packets */
381 if (wait_for_h) {
382 struct queued_rx *curr_rx = qrx;
383 struct queued_rx *last_rx;
384
385 while (curr_rx) {
386 write_log(curr_rx->rx, curr_rx->len-2, "LAN < ");
387
388 w = write(fd, curr_rx->rx, curr_rx->len);
389 if (w <= 0) {
390 perror("write");
391 }
392 last_rx = curr_rx;
393 curr_rx = curr_rx->next;
394
395 free(last_rx->rx);
396 free(last_rx);
397 }
398
399 qrx = NULL;
400
401 wait_for_h = 0;
402 }
403
404 return 1;
405 }
406
407 static int hmlan_parse_one(uint8_t *cmd, int last, void *data)
408 {
409 struct hmcfgusb_dev *dev = data;
410 uint8_t out[0x40]; //FIXME!!!
411 uint8_t *outpos;
412 uint8_t *inpos = cmd;
413
414 outpos = out;
415
416 if (last == 0)
417 return 1;
418
419 write_log((char*)cmd, last, "LAN > ");
420
421 memset(out, 0, sizeof(out));
422 *outpos++ = *inpos++;
423
424 switch(*cmd) {
425 case 'S':
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)), 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)), FLAG_LENGTH_BYTE);
432 break;
433 case 'Y':
434 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), FLAG_LENGTH_BYTE);
437 break;
438 default:
439 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
440 break;
441 }
442
443 hmcfgusb_send(dev, out, sizeof(out), 1);
444
445 return 1;
446 }
447
448 static int hmlan_parse_in(int fd, void *data)
449 {
450 uint8_t *newbuf;
451 int r;
452 int i;
453
454 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
455 if (!newbuf) {
456 perror("realloc");
457 return 0;
458 }
459 lan_read_buf = newbuf;
460 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
461 if (r > 0) {
462 lan_read_buflen += r;
463 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
464 if (verbose)
465 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
466 return -1;
467 }
468 while(lan_read_buflen > 0) {
469 int found = 0;
470
471 for (i = 0; i < lan_read_buflen; i++) {
472 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
473 if (i > 0)
474 hmlan_parse_one(lan_read_buf, i, data);
475 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
476 lan_read_buflen -= (i + 1);
477 found = 1;
478 break;
479 }
480 if (i > LAN_MAX_LINE_LENGTH) {
481 if (verbose)
482 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
483 return -1;
484 }
485 }
486 if (!found)
487 break;
488 newbuf = realloc(lan_read_buf, lan_read_buflen);
489 if (lan_read_buflen && !newbuf) {
490 perror("realloc");
491 return 0;
492 }
493 lan_read_buf = newbuf;
494 }
495 } else if (r < 0) {
496 if (errno != ECONNRESET)
497 perror("read");
498 return r;
499 } else {
500 return 0;
501 }
502
503 return 1;
504 }
505
506 static int comm(int fd_in, int fd_out, int master_socket, int flags)
507 {
508 struct hmcfgusb_dev *dev;
509 uint8_t out[0x40]; //FIXME!!!
510 int quit = 0;
511
512 hmcfgusb_set_debug(debug);
513
514 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
515 if (!dev) {
516 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
517 return 0;
518 }
519
520 if (dev->bootloader) {
521 if (verbose)
522 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
523
524 hmcfgusb_leave_bootloader(dev);
525
526 hmcfgusb_close(dev);
527 sleep(1);
528 return 0;
529 }
530
531 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
532 struct tm *tm_s;
533 time_t tm;
534
535 tm = time(NULL);
536 tm_s = localtime(&tm);
537 if (tm_s == NULL) {
538 perror("localtime");
539 return 0;
540 }
541
542 tm_s->tm_hour = reboot_at_hour;
543 tm_s->tm_min = reboot_at_minute;
544 tm_s->tm_sec = 0;
545
546 tm = mktime(tm_s);
547 reboot_seconds = tm - dev->opened_at;
548
549 while (reboot_seconds <= 0)
550 reboot_seconds += 86400;
551 }
552
553 if (verbose && reboot_seconds)
554 printf("Rebooting in %u seconds\n", reboot_seconds);
555
556
557 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
558 fprintf(stderr, "Can't add client to pollfd!\n");
559 hmcfgusb_close(dev);
560 return 0;
561 }
562
563 if (master_socket >= 0) {
564 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
565 fprintf(stderr, "Can't add master_socket to pollfd!\n");
566 hmcfgusb_close(dev);
567 return 0;
568 }
569 }
570
571 memset(out, 0, sizeof(out));
572 out[0] = 'K';
573 wait_for_h = 1;
574 hmcfgusb_send_null_frame(dev, 1);
575 hmcfgusb_send(dev, out, sizeof(out), 1);
576
577 while(!quit) {
578 int fd;
579
580 fd = hmcfgusb_poll(dev, 1000); /* Wakeup device/bus at least once a second */
581 if (fd >= 0) {
582 if (fd == master_socket) {
583 int client;
584
585 client = accept(master_socket, NULL, 0);
586 if (client >= 0) {
587 shutdown(client, SHUT_RDWR);
588 close(client);
589 }
590 } else {
591 if (hmlan_parse_in(fd, dev) <= 0) {
592 quit = 1;
593 }
594 }
595 } else if (fd == -1) {
596 if (errno) {
597 if (errno != ETIMEDOUT) {
598 perror("hmcfgusb_poll");
599 quit = 1;
600 } else {
601 /* periodically wakeup the device */
602 hmcfgusb_send_null_frame(dev, 1);
603 if (wait_for_h) {
604 memset(out, 0, sizeof(out));
605 out[0] = 'K';
606 hmcfgusb_send(dev, out, sizeof(out), 1);
607 }
608 }
609 }
610 }
611
612 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
613 if (verbose) {
614 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
615 time(NULL) - dev->opened_at);
616 }
617 hmcfgusb_enter_bootloader(dev);
618 }
619 }
620
621 hmcfgusb_close(dev);
622 return 1;
623 }
624
625 void sigterm_handler(int sig)
626 {
627 if (unlink(PID_FILE) == -1)
628 perror("Can't remove PID file");
629
630 exit(EXIT_SUCCESS);
631 }
632
633 #define FLAG_DAEMON (1 << 0)
634 #define FLAG_PID_FILE (1 << 1)
635
636 static int socket_server(char *iface, int port, int flags)
637 {
638 struct sigaction sact;
639 struct sockaddr_in sin;
640 int sock;
641 int n;
642 pid_t pid;
643
644 if (flags & FLAG_DAEMON) {
645 FILE *pidfile = NULL;
646
647 if (flags & FLAG_PID_FILE) {
648 int fd;
649
650 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
651 if (fd == -1) {
652 if (errno == EEXIST) {
653 pid_t old_pid;
654 pidfile = fopen(PID_FILE, "r");
655 if (!pidfile) {
656 perror("PID file " PID_FILE " already exists, already running?");
657 exit(EXIT_FAILURE);
658 }
659
660 if (fscanf(pidfile, "%u", &old_pid) != 1) {
661 fclose(pidfile);
662 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
663 exit(EXIT_FAILURE);
664 }
665
666 fclose(pidfile);
667
668 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
669 exit(EXIT_FAILURE);
670 }
671 perror("Can't create PID file " PID_FILE);
672 exit(EXIT_FAILURE);
673 }
674
675 pidfile = fdopen(fd, "w");
676 if (!pidfile) {
677 perror("Can't reopen PID file fd");
678 exit(EXIT_FAILURE);
679 }
680
681 memset(&sact, 0, sizeof(sact));
682 sact.sa_handler = sigterm_handler;
683
684 if (sigaction(SIGTERM, &sact, NULL) == -1) {
685 perror("sigaction(SIGTERM)");
686 exit(EXIT_FAILURE);
687 }
688 }
689
690 pid = fork();
691 if (pid > 0) {
692 if (pidfile) {
693 fprintf(pidfile, "%u\n", pid);
694 fclose(pidfile);
695 }
696
697 printf("Daemon with PID %u started!\n", pid);
698 exit(EXIT_SUCCESS);
699 } else if (pid < 0) {
700 perror("fork");
701 exit(EXIT_FAILURE);
702 }
703
704 if (pidfile)
705 fclose(pidfile);
706 }
707
708 memset(&sact, 0, sizeof(sact));
709 sact.sa_handler = SIG_IGN;
710
711 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
712 perror("sigaction(SIGPIPE)");
713 exit(EXIT_FAILURE);
714 }
715
716 impersonate_hmlanif = 1;
717
718 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
719 if (sock == -1) {
720 perror("Can't open socket");
721 return EXIT_FAILURE;
722 }
723
724 n = 1;
725 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
726 perror("Can't set socket options");
727 return EXIT_FAILURE;
728 }
729
730 memset(&sin, 0, sizeof(sin));
731 sin.sin_family = AF_INET;
732 sin.sin_port = htons(port);
733 if (!iface) {
734 sin.sin_addr.s_addr = htonl(INADDR_ANY);
735 } else {
736 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
737 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
738 return EXIT_FAILURE;
739 }
740 }
741
742 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
743 perror("Can't bind socket");
744 return EXIT_FAILURE;
745 }
746
747 if (listen(sock, 1) == -1) {
748 perror("Can't listen on socket");
749 return EXIT_FAILURE;
750 }
751
752 while(1) {
753 struct sockaddr_in csin;
754 socklen_t csinlen;
755 int client;
756 in_addr_t client_addr;
757
758 memset(&csin, 0, sizeof(csin));
759 csinlen = sizeof(csin);
760 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
761 if (client == -1) {
762 perror("Couldn't accept client");
763 continue;
764 }
765
766 /* FIXME: getnameinfo... */
767 client_addr = ntohl(csin.sin_addr.s_addr);
768
769 write_log(NULL, 0, "Client %d.%d.%d.%d connected!\n",
770 (client_addr & 0xff000000) >> 24,
771 (client_addr & 0x00ff0000) >> 16,
772 (client_addr & 0x0000ff00) >> 8,
773 (client_addr & 0x000000ff));
774
775 comm(client, client, sock, flags);
776
777 shutdown(client, SHUT_RDWR);
778 close(client);
779
780 if (lan_read_buf)
781 free(lan_read_buf);
782 lan_read_buf = NULL;
783 lan_read_buflen = 0;
784
785 write_log(NULL, 0, "Connection to %d.%d.%d.%d closed!\n",
786 (client_addr & 0xff000000) >> 24,
787 (client_addr & 0x00ff0000) >> 16,
788 (client_addr & 0x0000ff00) >> 8,
789 (client_addr & 0x000000ff));
790 sleep(1);
791 }
792
793 return EXIT_SUCCESS;
794 }
795
796 static int interactive_server(int flags)
797 {
798 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
799 return EXIT_FAILURE;
800
801 return EXIT_SUCCESS;
802 }
803
804 void hmlan_syntax(char *prog)
805 {
806 fprintf(stderr, "Syntax: %s options\n\n", prog);
807 fprintf(stderr, "Possible options:\n");
808 fprintf(stderr, "\t-D\t\tdebug mode\n");
809 fprintf(stderr, "\t-d\t\tdaemon mode\n");
810 fprintf(stderr, "\t-h\t\tthis help\n");
811 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
812 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
813 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
814 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
815 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
816 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);
817 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
818 fprintf(stderr, "\t-v\t\tverbose mode\n");
819 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
820
821 }
822
823 int main(int argc, char **argv)
824 {
825 int port = 1000;
826 char *iface = NULL;
827 int interactive = 0;
828 int flags = 0;
829 char *ep;
830 int opt;
831
832 while((opt = getopt(argc, argv, "DdhiPp:Rr:l:L:vV")) != -1) {
833 switch (opt) {
834 case 'D':
835 debug = 1;
836 verbose = 1;
837 break;
838 case 'd':
839 flags |= FLAG_DAEMON;
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 'v':
888 verbose = 1;
889 break;
890 case 'V':
891 printf("hmland " VERSION "\n");
892 printf("Copyright (c) 2013 Michael Gernoth\n\n");
893 exit(EXIT_SUCCESS);
894 case 'h':
895 case ':':
896 case '?':
897 default:
898 hmlan_syntax(argv[0]);
899 exit(EXIT_FAILURE);
900 break;
901 }
902 }
903
904 if (interactive) {
905 return interactive_server(flags);
906 } else {
907 return socket_server(iface, port, flags);
908 }
909 }
Impressum, Datenschutz