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