]> cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
Suppress warnings about non-literal format strings
[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 #pragma GCC diagnostic push
120 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
121 vfprintf(logfile, fmt, ap);
122 #pragma GCC diagnostic pop
123 va_end(ap);
124 }
125 if (verbose) {
126 va_start(ap, fmt);
127 #pragma GCC diagnostic push
128 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
129 vprintf(fmt, ap);
130 #pragma GCC diagnostic pop
131 va_end(ap);
132 }
133 }
134
135 if (buf && len) {
136 for (i = 0; i < len; i++) {
137 if (logfile)
138 fprintf(logfile, "%c", buf[i]);
139 if (verbose)
140 printf("%c", buf[i]);
141 }
142 if (logfile)
143 fprintf(logfile, "\n");
144 if (verbose)
145 printf("\n");
146 }
147 if (logfile)
148 fflush(logfile);
149 }
150
151 static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, size_t len, int flags)
152 {
153 uint8_t *buf_out = *outpos;
154 uint8_t *outend = *outpos + outlen;
155 uint8_t *inend = *inpos + inlen;
156 size_t i;
157
158 if (flags & FLAG_COMMA_BEFORE) {
159 CHECK_SPACE(1);
160 **outpos=',';
161 *outpos += 1;
162 }
163
164 if (flags & FLAG_LENGTH_BYTE) {
165 CHECK_AVAIL(1);
166 len = **inpos;
167 *inpos += 1;
168 }
169
170 if (flags & FLAG_FORMAT_HEX) {
171 CHECK_AVAIL(len);
172 CHECK_SPACE(len*2);
173 for (i = 0; i < len; i++) {
174 **outpos = nibble_to_ascii(((**inpos) & 0xf0) >> 4);
175 *outpos += 1;
176 **outpos = nibble_to_ascii(((**inpos) & 0xf));
177 *inpos += 1; *outpos += 1;
178 }
179 } else {
180 CHECK_AVAIL(len);
181 CHECK_SPACE(len);
182 memcpy(*outpos, *inpos, len);
183 *outpos += len;
184 *inpos += len;
185 }
186
187 if (flags & FLAG_COMMA_AFTER) {
188 CHECK_SPACE(1);
189 **outpos=',';
190 *outpos += 1;
191 }
192
193 if (flags & FLAG_NL) {
194 CHECK_SPACE(2);
195 **outpos='\r';
196 *outpos += 1;
197 **outpos='\n';
198 *outpos += 1;
199 }
200
201 return *outpos - buf_out;
202 }
203
204 static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags)
205 {
206 uint8_t *buf_out = *outpos;
207 uint8_t *outend = *outpos + outlen;
208 uint8_t *inend = *inpos + inlen;
209
210 if (flags & FLAG_LENGTH_BYTE) {
211 size_t len = 0;
212 uint8_t *ip;
213
214 ip = *inpos;
215 while(ip < inend) {
216 if (*ip == ',') {
217 ip++;
218 if (!(flags & FLAG_IGNORE_COMMAS))
219 break;
220
221 continue;
222 }
223 len++;
224 ip++;
225 }
226 CHECK_SPACE(1);
227 **outpos = (len / 2);
228 *outpos += 1;
229 }
230
231 while(*inpos < inend) {
232 if (**inpos == ',') {
233 *inpos += 1;
234 if (!(flags & FLAG_IGNORE_COMMAS))
235 break;
236
237 continue;
238 }
239
240 CHECK_SPACE(1);
241 CHECK_AVAIL(2);
242
243 **outpos = ascii_to_nibble(**inpos) << 4;
244 *inpos += 1;
245 **outpos |= ascii_to_nibble(**inpos);
246 *inpos += 1; *outpos += 1;
247 }
248
249 return *outpos - buf_out;
250 }
251
252 static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
253 {
254 uint8_t out[1024];
255 uint8_t *outpos;
256 uint8_t *inpos;
257 uint16_t version;
258 int fd = *((int*)data);
259 int w;
260
261 if (buf_len < 1)
262 return 1;
263
264 memset(out, 0, sizeof(out));
265 outpos = out;
266 inpos = buf;
267
268 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
269 switch(buf[0]) {
270 case 'H':
271 if (impersonate_hmlanif) {
272 buf[5] = 'L';
273 buf[6] = 'A';
274 buf[7] = 'N';
275 }
276 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE);
277 version = inpos[0] << 8;
278 version |= inpos[1];
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)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE);
281 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
282 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
283 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
284 if (version < 0x03c7) {
285 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
286 } else {
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)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
289 }
290
291 if (!reboot_set) {
292 int new_reboot_seconds;
293
294 if (version < 0x03c7) {
295 new_reboot_seconds = DEFAULT_REBOOT_SECONDS;
296 } else {
297 new_reboot_seconds = 0;
298 }
299
300 if (verbose && new_reboot_seconds && (reboot_seconds != new_reboot_seconds))
301 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
302 new_reboot_seconds, version);
303
304 reboot_seconds = new_reboot_seconds;
305 }
306
307 break;
308 case 'E':
309 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX);
310 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
311 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
312 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
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)), 0, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL);
315
316 break;
317 case 'R':
318 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
319 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
320 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, 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)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
323 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);
324
325 break;
326 case 'I':
327 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
328 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
329 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
330 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
331
332 break;
333 case 'G':
334 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_NL);
335
336 break;
337 default:
338 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
339 hexdump(buf, buf_len, "Unknown> ");
340 break;
341 }
342
343 /* Queue packet until first respone to 'K' is received */
344 if (wait_for_h && buf[0] != 'H') {
345 struct queued_rx **rxp = &qrx;
346
347 while (*rxp)
348 rxp = &((*rxp)->next);
349
350 *rxp = malloc(sizeof(struct queued_rx));
351 if (!*rxp) {
352 perror("malloc");
353 return 0;
354 }
355
356 memset(*rxp, 0, sizeof(struct queued_rx));
357 (*rxp)->len = outpos-out;
358 (*rxp)->rx = malloc((*rxp)->len);
359 if (!(*rxp)->rx) {
360 perror("malloc");
361 return 0;
362 }
363 memset((*rxp)->rx, 0, (*rxp)->len);
364 memcpy((*rxp)->rx, out, (*rxp)->len);
365
366 return 1;
367 }
368
369 write_log((char*)out, outpos-out-2, "LAN < ");
370
371 w = write(fd, out, outpos-out);
372 if (w <= 0) {
373 perror("write");
374 return 0;
375 }
376
377 /* Send all queued packets */
378 if (wait_for_h) {
379 struct queued_rx *curr_rx = qrx;
380 struct queued_rx *last_rx;
381
382 while (curr_rx) {
383 write_log(curr_rx->rx, curr_rx->len-2, "LAN < ");
384
385 w = write(fd, curr_rx->rx, curr_rx->len);
386 if (w <= 0) {
387 perror("write");
388 }
389 last_rx = curr_rx;
390 curr_rx = curr_rx->next;
391
392 free(last_rx->rx);
393 free(last_rx);
394 }
395
396 qrx = NULL;
397
398 wait_for_h = 0;
399 }
400
401 return 1;
402 }
403
404 static int hmlan_parse_one(uint8_t *cmd, int last, void *data)
405 {
406 struct hmcfgusb_dev *dev = data;
407 uint8_t out[0x40]; //FIXME!!!
408 uint8_t *outpos;
409 uint8_t *inpos = cmd;
410
411 outpos = out;
412
413 if (last == 0)
414 return 1;
415
416 write_log((char*)cmd, last, "LAN > ");
417
418 memset(out, 0, sizeof(out));
419 *outpos++ = *inpos++;
420
421 switch(*cmd) {
422 case 'S':
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)), 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)), 0);
428 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
429 break;
430 case 'Y':
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 case '+':
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)), 0);
438 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
439 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
440 break;
441 default:
442 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
443 break;
444 }
445
446 hmcfgusb_send(dev, out, sizeof(out), 1);
447
448 return 1;
449 }
450
451 static int hmlan_parse_in(int fd, void *data)
452 {
453 uint8_t *newbuf;
454 int r;
455 int i;
456
457 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
458 if (!newbuf) {
459 perror("realloc");
460 return 0;
461 }
462 lan_read_buf = newbuf;
463 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
464 if (r > 0) {
465 lan_read_buflen += r;
466 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
467 if (verbose)
468 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
469 return -1;
470 }
471 while(lan_read_buflen > 0) {
472 int found = 0;
473
474 for (i = 0; i < lan_read_buflen; i++) {
475 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
476 if (i > 0)
477 hmlan_parse_one(lan_read_buf, i, data);
478 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
479 lan_read_buflen -= (i + 1);
480 found = 1;
481 break;
482 }
483 if (i > LAN_MAX_LINE_LENGTH) {
484 if (verbose)
485 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
486 return -1;
487 }
488 }
489 if (!found)
490 break;
491 newbuf = realloc(lan_read_buf, lan_read_buflen);
492 if (lan_read_buflen && !newbuf) {
493 perror("realloc");
494 return 0;
495 }
496 lan_read_buf = newbuf;
497 }
498 } else if (r < 0) {
499 if (errno != ECONNRESET)
500 perror("read");
501 return r;
502 } else {
503 return 0;
504 }
505
506 return 1;
507 }
508
509 static int comm(int fd_in, int fd_out, int master_socket, int flags)
510 {
511 struct hmcfgusb_dev *dev;
512 uint8_t out[0x40]; //FIXME!!!
513 int quit = 0;
514
515 hmcfgusb_set_debug(debug);
516
517 dev = hmcfgusb_init(hmlan_format_out, &fd_out, serial);
518 if (!dev) {
519 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
520 return 0;
521 }
522
523 if (dev->bootloader) {
524 if (verbose)
525 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
526
527 hmcfgusb_leave_bootloader(dev);
528
529 hmcfgusb_close(dev);
530 sleep(1);
531 return 0;
532 }
533
534 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
535 struct tm *tm_s;
536 time_t tm;
537
538 tm = time(NULL);
539 tm_s = localtime(&tm);
540 if (tm_s == NULL) {
541 perror("localtime");
542 return 0;
543 }
544
545 tm_s->tm_hour = reboot_at_hour;
546 tm_s->tm_min = reboot_at_minute;
547 tm_s->tm_sec = 0;
548
549 tm = mktime(tm_s);
550 reboot_seconds = tm - dev->opened_at;
551
552 while (reboot_seconds <= 0)
553 reboot_seconds += 86400;
554 }
555
556 if (verbose && reboot_seconds)
557 printf("Rebooting in %u seconds\n", reboot_seconds);
558
559
560 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
561 fprintf(stderr, "Can't add client to pollfd!\n");
562 hmcfgusb_close(dev);
563 return 0;
564 }
565
566 if (master_socket >= 0) {
567 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
568 fprintf(stderr, "Can't add master_socket to pollfd!\n");
569 hmcfgusb_close(dev);
570 return 0;
571 }
572 }
573
574 memset(out, 0, sizeof(out));
575 out[0] = 'K';
576 wait_for_h = 1;
577 hmcfgusb_send_null_frame(dev, 1);
578 hmcfgusb_send(dev, out, sizeof(out), 1);
579
580 while(!quit) {
581 int fd;
582
583 fd = hmcfgusb_poll(dev, POLL_TIMEOUT_MS);
584 if (fd >= 0) {
585 if (fd == master_socket) {
586 int client;
587
588 client = accept(master_socket, NULL, 0);
589 if (client >= 0) {
590 shutdown(client, SHUT_RDWR);
591 close(client);
592 }
593 } else {
594 if (hmlan_parse_in(fd, dev) <= 0) {
595 quit = 1;
596 }
597 }
598 } else if (fd == -1) {
599 if (errno) {
600 if (errno != ETIMEDOUT) {
601 perror("hmcfgusb_poll");
602 quit = 1;
603 } else {
604 /* periodically wakeup the device */
605 hmcfgusb_send_null_frame(dev, 1);
606 if (wait_for_h) {
607 memset(out, 0, sizeof(out));
608 out[0] = 'K';
609 hmcfgusb_send(dev, out, sizeof(out), 1);
610 }
611 }
612 }
613 }
614
615 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
616 if (verbose) {
617 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
618 time(NULL) - dev->opened_at);
619 }
620 hmcfgusb_enter_bootloader(dev);
621 }
622 }
623
624 hmcfgusb_close(dev);
625 return 1;
626 }
627
628 void sigterm_handler(int sig)
629 {
630 if (unlink(PID_FILE) == -1)
631 perror("Can't remove PID file");
632
633 exit(EXIT_SUCCESS);
634 }
635
636 #define FLAG_DAEMON (1 << 0)
637 #define FLAG_PID_FILE (1 << 1)
638
639 static int socket_server(char *iface, int port, int flags)
640 {
641 struct sigaction sact;
642 struct sockaddr_in sin;
643 int sock;
644 int n;
645 pid_t pid;
646
647 if (flags & FLAG_DAEMON) {
648 FILE *pidfile = NULL;
649
650 if (flags & FLAG_PID_FILE) {
651 int fd;
652
653 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
654 if (fd == -1) {
655 if (errno == EEXIST) {
656 pid_t old_pid;
657 pidfile = fopen(PID_FILE, "r");
658 if (!pidfile) {
659 perror("PID file " PID_FILE " already exists, already running?");
660 exit(EXIT_FAILURE);
661 }
662
663 if (fscanf(pidfile, "%d", &old_pid) != 1) {
664 fclose(pidfile);
665 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
666 exit(EXIT_FAILURE);
667 }
668
669 fclose(pidfile);
670
671 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
672 exit(EXIT_FAILURE);
673 }
674 perror("Can't create PID file " PID_FILE);
675 exit(EXIT_FAILURE);
676 }
677
678 pidfile = fdopen(fd, "w");
679 if (!pidfile) {
680 perror("Can't reopen PID file fd");
681 exit(EXIT_FAILURE);
682 }
683
684 memset(&sact, 0, sizeof(sact));
685 sact.sa_handler = sigterm_handler;
686
687 if (sigaction(SIGTERM, &sact, NULL) == -1) {
688 perror("sigaction(SIGTERM)");
689 exit(EXIT_FAILURE);
690 }
691 }
692
693 pid = fork();
694 if (pid > 0) {
695 if (pidfile) {
696 fprintf(pidfile, "%u\n", pid);
697 fclose(pidfile);
698 }
699
700 printf("Daemon with PID %u started!\n", pid);
701 exit(EXIT_SUCCESS);
702 } else if (pid < 0) {
703 perror("fork");
704 exit(EXIT_FAILURE);
705 }
706
707 if (pidfile)
708 fclose(pidfile);
709 }
710
711 memset(&sact, 0, sizeof(sact));
712 sact.sa_handler = SIG_IGN;
713
714 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
715 perror("sigaction(SIGPIPE)");
716 exit(EXIT_FAILURE);
717 }
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\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
813 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
814 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
815 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
816 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
817 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
818 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);
819 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
820 fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
821 fprintf(stderr, "\t-v\t\tverbose mode\n");
822 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
823
824 }
825
826 int main(int argc, char **argv)
827 {
828 int port = 1000;
829 char *iface = NULL;
830 int interactive = 0;
831 int flags = 0;
832 char *ep;
833 int opt;
834
835 while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
836 switch (opt) {
837 case 'D':
838 debug = 1;
839 verbose = 1;
840 break;
841 case 'd':
842 flags |= FLAG_DAEMON;
843 break;
844 case 'I':
845 impersonate_hmlanif = 1;
846 break;
847 case 'i':
848 interactive = 1;
849 break;
850 case 'P':
851 flags |= FLAG_PID_FILE;
852 break;
853 case 'p':
854 port = strtoul(optarg, &ep, 10);
855 if (*ep != '\0') {
856 fprintf(stderr, "Can't parse port!\n");
857 exit(EXIT_FAILURE);
858 }
859 break;
860 case 'R':
861 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
862 break;
863 case 'r':
864 reboot_seconds = strtoul(optarg, &ep, 10);
865 if (*ep != '\0') {
866 if (*ep == ':') {
867 reboot_at_hour = reboot_seconds;
868 ep++;
869 reboot_at_minute = strtoul(ep, &ep, 10);
870 if (*ep != '\0') {
871 fprintf(stderr, "Can't parse reboot-time!\n");
872 exit(EXIT_FAILURE);
873 }
874
875 reboot_seconds = 0;
876 } else {
877 fprintf(stderr, "Can't parse reboot-timeout!\n");
878 exit(EXIT_FAILURE);
879 }
880 }
881 reboot_set = 1;
882 break;
883 case 'l':
884 iface = optarg;
885 break;
886 case 'L':
887 logfile = fopen(optarg, "a");
888 if (!logfile) {
889 perror("fopen(logfile)");
890 exit(EXIT_FAILURE);
891 }
892 break;
893 case 'S':
894 serial = optarg;
895 break;
896 case 'v':
897 verbose = 1;
898 break;
899 case 'V':
900 printf("hmland " VERSION "\n");
901 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
902 exit(EXIT_SUCCESS);
903 case 'h':
904 case ':':
905 case '?':
906 default:
907 hmlan_syntax(argv[0]);
908 exit(EXIT_FAILURE);
909 break;
910 }
911 }
912
913 if (interactive) {
914 return interactive_server(flags);
915 } else {
916 return socket_server(iface, port, flags);
917 }
918 }
Impressum, Datenschutz