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