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