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