]> git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
d91026a8c8ee928e215ba68287f090974c768609
[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 all 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 }
312 last_rx = curr_rx;
313 curr_rx = curr_rx->next;
314
315 free(last_rx->rx);
316 free(last_rx);
317 }
318
319 qrx = NULL;
320
321 wait_for_h = 0;
322 }
323
324 return 1;
325 }
326
327 static int hmlan_parse_in(int fd, void *data)
328 {
329 struct hmcfgusb_dev *dev = data;
330 uint8_t buf[1025];
331 uint8_t out[0x40]; //FIXME!!!
332 uint8_t *outpos;
333 uint8_t *inpos;
334 int i;
335 int last;
336 int r;
337
338 memset(buf, 0, sizeof(buf));
339
340 r = read(fd, buf, sizeof(buf)-1);
341 if (r > 0) {
342 uint8_t *inend = buf + r;
343
344 inpos = buf;
345
346 while (inpos < inend) {
347 uint8_t *instart = inpos;
348
349 if ((*inpos == '\r') || (*inpos == '\n')) {
350 inpos++;
351 continue;
352 }
353
354 outpos = out;
355
356 last = inend - inpos;
357
358 for (i = 0; i < last; i++) {
359 if ((inpos[i] == '\r') || (inpos[i] == '\n')) {
360 last = i;
361 break;
362 }
363 }
364
365 if (last == 0)
366 continue;
367
368 if (verbose) {
369 printf("LAN > ");
370 for (i = 0; i < last; i++)
371 printf("%c", instart[i]);
372 printf("\n");
373 }
374
375 memset(out, 0, sizeof(out));
376 *outpos++ = *inpos++;
377
378 switch(*instart) {
379 case 'S':
380 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), FLAG_LENGTH_BYTE);
386 break;
387 case 'Y':
388 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), FLAG_LENGTH_BYTE);
391 break;
392 default:
393 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
394 break;
395 }
396
397 hmcfgusb_send(dev, out, sizeof(out), 1);
398 }
399 } else if (r < 0) {
400 perror("read");
401 return r;
402 } else {
403 return 0;
404 }
405
406 return 1;
407 }
408
409 static int comm(int fd_in, int fd_out, int master_socket, int flags)
410 {
411 struct hmcfgusb_dev *dev;
412 uint8_t out[0x40]; //FIXME!!!
413 int quit = 0;
414
415 hmcfgusb_set_debug(debug);
416
417 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
418 if (!dev) {
419 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
420 return 0;
421 }
422
423 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
424 fprintf(stderr, "Can't add client to pollfd!\n");
425 hmcfgusb_close(dev);
426 return 0;
427 }
428
429 if (master_socket >= 0) {
430 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
431 fprintf(stderr, "Can't add master_socket to pollfd!\n");
432 hmcfgusb_close(dev);
433 return 0;
434 }
435 }
436
437 memset(out, 0, sizeof(out));
438 out[0] = 'K';
439 wait_for_h = 1;
440 hmcfgusb_send_null_frame(dev);
441 hmcfgusb_send(dev, out, sizeof(out), 1);
442
443 while(!quit) {
444 int fd;
445
446 fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */
447 if (fd >= 0) {
448 if (fd == master_socket) {
449 int client;
450
451 client = accept(master_socket, NULL, 0);
452 if (client >= 0) {
453 shutdown(client, SHUT_RDWR);
454 close(client);
455 }
456 } else {
457 if (hmlan_parse_in(fd, dev) <= 0) {
458 quit = 1;
459 }
460 }
461 } else if (fd == -1) {
462 if (errno) {
463 perror("hmcfgusb_poll");
464 quit = 1;
465 } else {
466 /* periodically wakeup the device */
467 hmcfgusb_send_null_frame(dev);
468 }
469 }
470 }
471
472 hmcfgusb_close(dev);
473 return 1;
474 }
475
476 void sigterm_handler(int sig)
477 {
478 if (unlink(PID_FILE) == -1)
479 perror("Can't remove PID file");
480
481 exit(EXIT_SUCCESS);
482 }
483
484 #define FLAG_DAEMON (1 << 0)
485 #define FLAG_PID_FILE (1 << 1)
486
487 static int socket_server(char *iface, int port, int flags)
488 {
489 struct sigaction sact;
490 struct sockaddr_in sin;
491 int sock;
492 int n;
493 pid_t pid;
494
495 if (flags & FLAG_DAEMON) {
496 FILE *pidfile = NULL;
497
498 if (flags & FLAG_PID_FILE) {
499 int fd;
500
501 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
502 if (fd == -1) {
503 if (errno == EEXIST) {
504 pid_t old_pid;
505 pidfile = fopen(PID_FILE, "r");
506 if (!pidfile) {
507 perror("PID file " PID_FILE " already exists, already running?");
508 exit(EXIT_FAILURE);
509 }
510
511 if (fscanf(pidfile, "%u", &old_pid) != 1) {
512 fclose(pidfile);
513 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
514 exit(EXIT_FAILURE);
515 }
516
517 fclose(pidfile);
518
519 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
520 exit(EXIT_FAILURE);
521 }
522 perror("Can't create PID file " PID_FILE);
523 exit(EXIT_FAILURE);
524 }
525
526 pidfile = fdopen(fd, "w");
527 if (!pidfile) {
528 perror("Can't reopen PID file fd");
529 exit(EXIT_FAILURE);
530 }
531
532 memset(&sact, 0, sizeof(sact));
533 sact.sa_handler = sigterm_handler;
534
535 if (sigaction(SIGTERM, &sact, NULL) == -1) {
536 perror("sigaction(SIGTERM)");
537 exit(EXIT_FAILURE);
538 }
539 }
540
541 pid = fork();
542 if (pid > 0) {
543 if (pidfile) {
544 fprintf(pidfile, "%u\n", pid);
545 fclose(pidfile);
546 }
547
548 printf("Daemon with PID %u started!\n", pid);
549 exit(EXIT_SUCCESS);
550 } else if (pid < 0) {
551 perror("fork");
552 exit(EXIT_FAILURE);
553 }
554
555 if (pidfile)
556 fclose(pidfile);
557 }
558
559 memset(&sact, 0, sizeof(sact));
560 sact.sa_handler = SIG_IGN;
561
562 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
563 perror("sigaction(SIGPIPE)");
564 exit(EXIT_FAILURE);
565 }
566
567 impersonate_hmlanif = 1;
568
569 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
570 if (sock == -1) {
571 perror("Can't open socket");
572 return EXIT_FAILURE;
573 }
574
575 n = 1;
576 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
577 perror("Can't set socket options");
578 return EXIT_FAILURE;
579 }
580
581 memset(&sin, 0, sizeof(sin));
582 sin.sin_family = AF_INET;
583 sin.sin_port = htons(port);
584 if (!iface) {
585 sin.sin_addr.s_addr = htonl(INADDR_ANY);
586 } else {
587 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
588 perror("inet_ntop");
589 return EXIT_FAILURE;
590 }
591 }
592
593 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
594 perror("Can't bind socket");
595 return EXIT_FAILURE;
596 }
597
598 if (listen(sock, 1) == -1) {
599 perror("Can't listen on socket");
600 return EXIT_FAILURE;
601 }
602
603 while(1) {
604 struct sockaddr_in csin;
605 socklen_t csinlen;
606 int client;
607 in_addr_t client_addr;
608
609 memset(&csin, 0, sizeof(csin));
610 csinlen = sizeof(csin);
611 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
612 if (client == -1) {
613 perror("Couldn't accept client");
614 continue;
615 }
616
617 /* FIXME: getnameinfo... */
618 client_addr = ntohl(csin.sin_addr.s_addr);
619
620 if (verbose) {
621 printf("Client %d.%d.%d.%d connected!\n",
622 (client_addr & 0xff000000) >> 24,
623 (client_addr & 0x00ff0000) >> 16,
624 (client_addr & 0x0000ff00) >> 8,
625 (client_addr & 0x000000ff));
626 }
627
628 comm(client, client, sock, flags);
629
630 shutdown(client, SHUT_RDWR);
631 close(client);
632
633 if (verbose) {
634 printf("Connection to %d.%d.%d.%d closed!\n",
635 (client_addr & 0xff000000) >> 24,
636 (client_addr & 0x00ff0000) >> 16,
637 (client_addr & 0x0000ff00) >> 8,
638 (client_addr & 0x000000ff));
639 }
640 sleep(1);
641 }
642
643 return EXIT_SUCCESS;
644 }
645
646 static int interactive_server(int flags)
647 {
648 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
649 return EXIT_FAILURE;
650
651 return EXIT_SUCCESS;
652 }
653
654 void hmlan_syntax(char *prog)
655 {
656 fprintf(stderr, "Syntax: %s options\n\n", prog);
657 fprintf(stderr, "Possible options:\n");
658 fprintf(stderr, "\t-D\tdebug mode\n");
659 fprintf(stderr, "\t-d\tdaemon mode\n");
660 fprintf(stderr, "\t-h\tthis help\n");
661 fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
662 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
663 fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
664 fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
665 fprintf(stderr, "\t-v\tverbose mode\n");
666
667 }
668
669 int main(int argc, char **argv)
670 {
671 int port = 1000;
672 char *iface = NULL;
673 int interactive = 0;
674 int flags = 0;
675 char *ep;
676 int opt;
677
678 while((opt = getopt(argc, argv, "DdhiPp:Rl:v")) != -1) {
679 switch (opt) {
680 case 'D':
681 debug = 1;
682 verbose = 1;
683 break;
684 case 'd':
685 flags |= FLAG_DAEMON;
686 break;
687 case 'i':
688 interactive = 1;
689 break;
690 case 'P':
691 flags |= FLAG_PID_FILE;
692 break;
693 case 'p':
694 port = strtoul(optarg, &ep, 10);
695 if (*ep != '\0') {
696 fprintf(stderr, "Can't parse port!\n");
697 exit(EXIT_FAILURE);
698 }
699 break;
700 case 'R':
701 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
702 break;
703 case 'l':
704 iface = optarg;
705 break;
706 case 'v':
707 verbose = 1;
708 break;
709 case 'h':
710 case ':':
711 case '?':
712 default:
713 hmlan_syntax(argv[0]);
714 exit(EXIT_FAILURE);
715 break;
716 }
717 }
718
719 if (interactive) {
720 return interactive_server(flags);
721 } else {
722 return socket_server(iface, port, flags);
723 }
724 }
Impressum, Datenschutz