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