factor out leaving the bootloader into own function
[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
56 struct queued_rx {
57 char *rx;
58 int len;
59 struct queued_rx *next;
60 };
61
62 static struct queued_rx *qrx = NULL;
63 static 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
75 static 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
88 static 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
143 static 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
158 static 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
206 static 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
346 static 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
430 static 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 if (verbose)
446 fprintf(stderr, "HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
447
448 hmcfgusb_leave_bootloader(dev);
449
450 hmcfgusb_close(dev);
451 sleep(1);
452 return 0;
453 }
454
455 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
456 fprintf(stderr, "Can't add client to pollfd!\n");
457 hmcfgusb_close(dev);
458 return 0;
459 }
460
461 if (master_socket >= 0) {
462 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
463 fprintf(stderr, "Can't add master_socket to pollfd!\n");
464 hmcfgusb_close(dev);
465 return 0;
466 }
467 }
468
469 memset(out, 0, sizeof(out));
470 out[0] = 'K';
471 wait_for_h = 1;
472 hmcfgusb_send_null_frame(dev, 1);
473 hmcfgusb_send(dev, out, sizeof(out), 1);
474
475 while(!quit) {
476 int fd;
477
478 fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */
479 if (fd >= 0) {
480 if (fd == master_socket) {
481 int client;
482
483 client = accept(master_socket, NULL, 0);
484 if (client >= 0) {
485 shutdown(client, SHUT_RDWR);
486 close(client);
487 }
488 } else {
489 if (hmlan_parse_in(fd, dev) <= 0) {
490 quit = 1;
491 }
492 }
493 } else if (fd == -1) {
494 if (errno) {
495 if (errno != ETIMEDOUT) {
496 perror("hmcfgusb_poll");
497 quit = 1;
498 } else {
499 /* periodically wakeup the device */
500 hmcfgusb_send_null_frame(dev, 1);
501 if (wait_for_h) {
502 memset(out, 0, sizeof(out));
503 out[0] = 'K';
504 hmcfgusb_send(dev, out, sizeof(out), 1);
505 }
506 }
507 }
508 }
509
510 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
511 if (verbose) {
512 fprintf(stderr, "HM-CFG-USB running since %lu seconds, rebooting now...\n",
513 time(NULL) - dev->opened_at);
514 }
515 hmcfgusb_enter_bootloader(dev);
516 }
517 }
518
519 hmcfgusb_close(dev);
520 return 1;
521 }
522
523 void sigterm_handler(int sig)
524 {
525 if (unlink(PID_FILE) == -1)
526 perror("Can't remove PID file");
527
528 exit(EXIT_SUCCESS);
529 }
530
531 #define FLAG_DAEMON (1 << 0)
532 #define FLAG_PID_FILE (1 << 1)
533
534 static int socket_server(char *iface, int port, int flags)
535 {
536 struct sigaction sact;
537 struct sockaddr_in sin;
538 int sock;
539 int n;
540 pid_t pid;
541
542 if (flags & FLAG_DAEMON) {
543 FILE *pidfile = NULL;
544
545 if (flags & FLAG_PID_FILE) {
546 int fd;
547
548 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
549 if (fd == -1) {
550 if (errno == EEXIST) {
551 pid_t old_pid;
552 pidfile = fopen(PID_FILE, "r");
553 if (!pidfile) {
554 perror("PID file " PID_FILE " already exists, already running?");
555 exit(EXIT_FAILURE);
556 }
557
558 if (fscanf(pidfile, "%u", &old_pid) != 1) {
559 fclose(pidfile);
560 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
561 exit(EXIT_FAILURE);
562 }
563
564 fclose(pidfile);
565
566 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
567 exit(EXIT_FAILURE);
568 }
569 perror("Can't create PID file " PID_FILE);
570 exit(EXIT_FAILURE);
571 }
572
573 pidfile = fdopen(fd, "w");
574 if (!pidfile) {
575 perror("Can't reopen PID file fd");
576 exit(EXIT_FAILURE);
577 }
578
579 memset(&sact, 0, sizeof(sact));
580 sact.sa_handler = sigterm_handler;
581
582 if (sigaction(SIGTERM, &sact, NULL) == -1) {
583 perror("sigaction(SIGTERM)");
584 exit(EXIT_FAILURE);
585 }
586 }
587
588 pid = fork();
589 if (pid > 0) {
590 if (pidfile) {
591 fprintf(pidfile, "%u\n", pid);
592 fclose(pidfile);
593 }
594
595 printf("Daemon with PID %u started!\n", pid);
596 exit(EXIT_SUCCESS);
597 } else if (pid < 0) {
598 perror("fork");
599 exit(EXIT_FAILURE);
600 }
601
602 if (pidfile)
603 fclose(pidfile);
604 }
605
606 memset(&sact, 0, sizeof(sact));
607 sact.sa_handler = SIG_IGN;
608
609 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
610 perror("sigaction(SIGPIPE)");
611 exit(EXIT_FAILURE);
612 }
613
614 impersonate_hmlanif = 1;
615
616 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
617 if (sock == -1) {
618 perror("Can't open socket");
619 return EXIT_FAILURE;
620 }
621
622 n = 1;
623 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
624 perror("Can't set socket options");
625 return EXIT_FAILURE;
626 }
627
628 memset(&sin, 0, sizeof(sin));
629 sin.sin_family = AF_INET;
630 sin.sin_port = htons(port);
631 if (!iface) {
632 sin.sin_addr.s_addr = htonl(INADDR_ANY);
633 } else {
634 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
635 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
636 return EXIT_FAILURE;
637 }
638 }
639
640 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
641 perror("Can't bind socket");
642 return EXIT_FAILURE;
643 }
644
645 if (listen(sock, 1) == -1) {
646 perror("Can't listen on socket");
647 return EXIT_FAILURE;
648 }
649
650 while(1) {
651 struct sockaddr_in csin;
652 socklen_t csinlen;
653 int client;
654 in_addr_t client_addr;
655
656 memset(&csin, 0, sizeof(csin));
657 csinlen = sizeof(csin);
658 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
659 if (client == -1) {
660 perror("Couldn't accept client");
661 continue;
662 }
663
664 /* FIXME: getnameinfo... */
665 client_addr = ntohl(csin.sin_addr.s_addr);
666
667 if (verbose) {
668 print_timestamp(stdout);
669 printf("Client %d.%d.%d.%d connected!\n",
670 (client_addr & 0xff000000) >> 24,
671 (client_addr & 0x00ff0000) >> 16,
672 (client_addr & 0x0000ff00) >> 8,
673 (client_addr & 0x000000ff));
674 }
675
676 comm(client, client, sock, flags);
677
678 shutdown(client, SHUT_RDWR);
679 close(client);
680
681 if (verbose) {
682 print_timestamp(stdout);
683 printf("Connection to %d.%d.%d.%d closed!\n",
684 (client_addr & 0xff000000) >> 24,
685 (client_addr & 0x00ff0000) >> 16,
686 (client_addr & 0x0000ff00) >> 8,
687 (client_addr & 0x000000ff));
688 }
689 sleep(1);
690 }
691
692 return EXIT_SUCCESS;
693 }
694
695 static int interactive_server(int flags)
696 {
697 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
698 return EXIT_FAILURE;
699
700 return EXIT_SUCCESS;
701 }
702
703 void hmlan_syntax(char *prog)
704 {
705 fprintf(stderr, "Syntax: %s options\n\n", prog);
706 fprintf(stderr, "Possible options:\n");
707 fprintf(stderr, "\t-D\tdebug mode\n");
708 fprintf(stderr, "\t-d\tdaemon mode\n");
709 fprintf(stderr, "\t-h\tthis help\n");
710 fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
711 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
712 fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
713 fprintf(stderr, "\t-p n\tlisten on port n (default: 1000)\n");
714 fprintf(stderr, "\t-r n\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS);
715 fprintf(stderr, "\t-v\tverbose mode\n");
716
717 }
718
719 int main(int argc, char **argv)
720 {
721 int port = 1000;
722 char *iface = NULL;
723 int interactive = 0;
724 int flags = 0;
725 char *ep;
726 int opt;
727
728 reboot_seconds = DEFAULT_REBOOT_SECONDS;
729
730 while((opt = getopt(argc, argv, "DdhiPp:Rr:l:v")) != -1) {
731 switch (opt) {
732 case 'D':
733 debug = 1;
734 verbose = 1;
735 break;
736 case 'd':
737 flags |= FLAG_DAEMON;
738 break;
739 case 'i':
740 interactive = 1;
741 break;
742 case 'P':
743 flags |= FLAG_PID_FILE;
744 break;
745 case 'p':
746 port = strtoul(optarg, &ep, 10);
747 if (*ep != '\0') {
748 fprintf(stderr, "Can't parse port!\n");
749 exit(EXIT_FAILURE);
750 }
751 break;
752 case 'R':
753 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
754 break;
755 case 'r':
756 reboot_seconds = strtoul(optarg, &ep, 10);
757 if (*ep != '\0') {
758 fprintf(stderr, "Can't parse reboot-timeout!\n");
759 exit(EXIT_FAILURE);
760 }
761 break;
762 case 'l':
763 iface = optarg;
764 break;
765 case 'v':
766 verbose = 1;
767 break;
768 case 'h':
769 case ':':
770 case '?':
771 default:
772 hmlan_syntax(argv[0]);
773 exit(EXIT_FAILURE);
774 break;
775 }
776 }
777
778 if (interactive) {
779 return interactive_server(flags);
780 } else {
781 return socket_server(iface, port, flags);
782 }
783 }
Impressum, Datenschutz