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