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