don't print crnl to console
[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 }
62f60cc1
MG
242 if (verbose) {
243 int i;
244
245 printf("LAN < ");
11806002 246 for (i = 0; i < outpos-out-2; i++)
62f60cc1 247 printf("%c", out[i]);
11806002 248 printf("\n");
62f60cc1 249 }
4371275b
MG
250
251 w = write(fd, out, outpos-out);
252 if (w <= 0) {
253 perror("write");
254 return 0;
255 }
256
257 return 1;
9db2e455
MG
258}
259
e0a7146e 260static int hmlan_parse_in(int fd, void *data)
9db2e455
MG
261{
262 struct hmcfgusb_dev *dev = data;
627e3f33 263 uint8_t buf[1025];
e75295bb
MG
264 uint8_t out[0x40]; //FIXME!!!
265 uint8_t *outpos;
266 uint8_t *inpos;
9db2e455 267 int i;
627e3f33 268 int last;
9db2e455
MG
269 int r;
270
627e3f33
MG
271 memset(buf, 0, sizeof(buf));
272
273 r = read(fd, buf, sizeof(buf)-1);
9db2e455 274 if (r > 0) {
627e3f33
MG
275 uint8_t *inend = buf + r;
276
277 inpos = buf;
278
627e3f33
MG
279 while (inpos < inend) {
280 uint8_t *instart = inpos;
281
282 if ((*inpos == '\r') || (*inpos == '\n')) {
283 inpos++;
284 continue;
9db2e455 285 }
627e3f33
MG
286
287 outpos = out;
9db2e455 288
627e3f33
MG
289 last = inend - inpos;
290
291 for (i = 0; i < last; i++) {
292 if ((inpos[i] == '\r') || (inpos[i] == '\n')) {
293 last = i;
294 break;
295 }
296 }
297
298 if (last == 0)
299 continue;
300
62f60cc1
MG
301 if (verbose) {
302 printf("LAN > ");
303 for (i = 0; i < last; i++)
304 printf("%c", instart[i]);
305 printf("\n");
306 }
307
627e3f33
MG
308 memset(out, 0, sizeof(out));
309 *outpos++ = *inpos++;
310
03ca736e 311 switch(*instart) {
627e3f33
MG
312 case 'S':
313 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
314 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
315 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
316 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
317 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
318 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
319 break;
d419ace3
MG
320 case 'Y':
321 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
322 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0);
323 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
d26506bf 324 break;
627e3f33
MG
325 default:
326 parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
327 break;
328 }
e75295bb 329
33df13be 330 hmcfgusb_send(dev, out, sizeof(out), 1);
627e3f33 331 }
9db2e455
MG
332 } else if (r < 0) {
333 perror("read");
e0a7146e
MG
334 return r;
335 } else {
336 return 0;
9db2e455 337 }
e0a7146e
MG
338
339 return 1;
9db2e455
MG
340}
341
23d96b59 342static int comm(int fd_in, int fd_out, int master_socket, int flags)
9db2e455
MG
343{
344 struct hmcfgusb_dev *dev;
23d96b59 345 uint8_t out[0x40]; //FIXME!!!
9db2e455
MG
346 int quit = 0;
347
627e3f33
MG
348 hmcfgusb_set_debug(debug);
349
e0a7146e 350 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
9db2e455
MG
351 if (!dev) {
352 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
e0a7146e
MG
353 return 0;
354 }
355
356 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
357 fprintf(stderr, "Can't add client to pollfd!\n");
358 hmcfgusb_close(dev);
359 return 0;
9db2e455
MG
360 }
361
e0a7146e
MG
362 if (master_socket >= 0) {
363 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
364 fprintf(stderr, "Can't add master_socket to pollfd!\n");
365 hmcfgusb_close(dev);
366 return 0;
367 }
9db2e455
MG
368 }
369
23d96b59
MG
370 memset(out, 0, sizeof(out));
371 out[0] = 'K';
e09806b6 372 hmcfgusb_send_null_frame(dev);
23d96b59 373 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
374
375 while(!quit) {
376 int fd;
377
b55c340d 378 fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */
9db2e455 379 if (fd >= 0) {
e0a7146e
MG
380 if (fd == master_socket) {
381 int client;
382
383 client = accept(master_socket, NULL, 0);
384 if (client >= 0) {
385 shutdown(client, SHUT_RDWR);
386 close(client);
387 }
388 } else {
389 if (hmlan_parse_in(fd, dev) <= 0) {
390 quit = 1;
391 }
392 }
9db2e455
MG
393 } else if (fd == -1) {
394 if (errno) {
395 perror("hmcfgusb_poll");
396 quit = 1;
23d96b59
MG
397 } else {
398 /* periodically wakeup the device */
399 hmcfgusb_send_null_frame(dev);
9db2e455
MG
400 }
401 }
402 }
403
813afd12 404 hmcfgusb_close(dev);
e0a7146e
MG
405 return 1;
406}
407
b0bf6ad2
MG
408void sigterm_handler(int sig)
409{
410 if (unlink(PID_FILE) == -1)
411 perror("Can't remove PID file");
412
413 exit(EXIT_SUCCESS);
414}
415
416#define FLAG_DAEMON (1 << 0)
417#define FLAG_PID_FILE (1 << 1)
418
419static int socket_server(char *iface, int port, int flags)
e0a7146e 420{
4371275b 421 struct sigaction sact;
e0a7146e
MG
422 struct sockaddr_in sin;
423 int sock;
424 int n;
627e3f33
MG
425 pid_t pid;
426
b0bf6ad2
MG
427 if (flags & FLAG_DAEMON) {
428 FILE *pidfile = NULL;
429
430 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
431 int fd;
432
433 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
434 if (fd == -1) {
435 if (errno == EEXIST) {
436 pid_t old_pid;
437 pidfile = fopen(PID_FILE, "r");
438 if (!pidfile) {
439 perror("PID file " PID_FILE " already exists, already running?");
440 exit(EXIT_FAILURE);
441 }
442
443 if (fscanf(pidfile, "%u", &old_pid) != 1) {
444 fclose(pidfile);
445 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
446 exit(EXIT_FAILURE);
447 }
448
449 fclose(pidfile);
450
451 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
452 exit(EXIT_FAILURE);
453 }
454 perror("Can't create PID file " PID_FILE);
455 exit(EXIT_FAILURE);
456 }
b0bf6ad2 457
e6ab4631 458 pidfile = fdopen(fd, "w");
b0bf6ad2 459 if (!pidfile) {
e6ab4631 460 perror("Can't reopen PID file fd");
b0bf6ad2
MG
461 exit(EXIT_FAILURE);
462 }
463
464 memset(&sact, 0, sizeof(sact));
465 sact.sa_handler = sigterm_handler;
466
467 if (sigaction(SIGTERM, &sact, NULL) == -1) {
468 perror("sigaction(SIGTERM)");
469 exit(EXIT_FAILURE);
470 }
471 }
472
627e3f33
MG
473 pid = fork();
474 if (pid > 0) {
b0bf6ad2
MG
475 if (pidfile) {
476 fprintf(pidfile, "%u\n", pid);
477 fclose(pidfile);
478 }
479
627e3f33
MG
480 printf("Daemon with PID %u started!\n", pid);
481 exit(EXIT_SUCCESS);
482 } else if (pid < 0) {
483 perror("fork");
484 exit(EXIT_FAILURE);
485 }
b0bf6ad2
MG
486
487 if (pidfile)
488 fclose(pidfile);
627e3f33 489 }
e0a7146e 490
4371275b
MG
491 memset(&sact, 0, sizeof(sact));
492 sact.sa_handler = SIG_IGN;
493
494 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 495 perror("sigaction(SIGPIPE)");
27f4063e 496 exit(EXIT_FAILURE);
4371275b
MG
497 }
498
e0a7146e
MG
499 impersonate_hmlanif = 1;
500
501 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
502 if (sock == -1) {
503 perror("Can't open socket");
504 return EXIT_FAILURE;
505 }
506
507 n = 1;
508 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
509 perror("Can't set socket options");
510 return EXIT_FAILURE;
511 }
512
513 memset(&sin, 0, sizeof(sin));
514 sin.sin_family = AF_INET;
515 sin.sin_port = htons(port);
4a8a2269
MG
516 if (!iface) {
517 sin.sin_addr.s_addr = htonl(INADDR_ANY);
518 } else {
519 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
520 perror("inet_ntop");
521 return EXIT_FAILURE;
522 }
523 }
e0a7146e
MG
524
525 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
526 perror("Can't bind socket");
527 return EXIT_FAILURE;
528 }
529
530 if (listen(sock, 1) == -1) {
531 perror("Can't listen on socket");
532 return EXIT_FAILURE;
533 }
534
535 while(1) {
536 struct sockaddr_in csin;
537 socklen_t csinlen;
538 int client;
627e3f33 539 in_addr_t client_addr;
e0a7146e
MG
540
541 memset(&csin, 0, sizeof(csin));
542 csinlen = sizeof(csin);
543 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
544 if (client == -1) {
545 perror("Couldn't accept client");
546 continue;
547 }
548
627e3f33
MG
549 /* FIXME: getnameinfo... */
550 client_addr = ntohl(csin.sin_addr.s_addr);
551
552 if (verbose) {
553 printf("Client %d.%d.%d.%d connected!\n",
554 (client_addr & 0xff000000) >> 24,
555 (client_addr & 0x00ff0000) >> 16,
556 (client_addr & 0x0000ff00) >> 8,
557 (client_addr & 0x000000ff));
558 }
e0a7146e 559
23d96b59 560 comm(client, client, sock, flags);
e0a7146e
MG
561
562 shutdown(client, SHUT_RDWR);
563 close(client);
564
627e3f33
MG
565 if (verbose) {
566 printf("Connection to %d.%d.%d.%d closed!\n",
567 (client_addr & 0xff000000) >> 24,
568 (client_addr & 0x00ff0000) >> 16,
569 (client_addr & 0x0000ff00) >> 8,
570 (client_addr & 0x000000ff));
571 }
e09806b6 572 sleep(1);
e0a7146e
MG
573 }
574
575 return EXIT_SUCCESS;
576}
577
23d96b59 578static int interactive_server(int flags)
e0a7146e 579{
23d96b59 580 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 581 return EXIT_FAILURE;
9db2e455
MG
582
583 return EXIT_SUCCESS;
584}
e0a7146e 585
627e3f33
MG
586void hmlan_syntax(char *prog)
587{
588 fprintf(stderr, "Syntax: %s options\n\n", prog);
589 fprintf(stderr, "Possible options:\n");
590 fprintf(stderr, "\t-D\tdebug mode\n");
591 fprintf(stderr, "\t-d\tdaemon mode\n");
592 fprintf(stderr, "\t-h\tthis help\n");
593 fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
4a8a2269 594 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
b0bf6ad2 595 fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
627e3f33
MG
596 fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
597 fprintf(stderr, "\t-v\tverbose mode\n");
598
599}
600
e0a7146e
MG
601int main(int argc, char **argv)
602{
627e3f33 603 int port = 1000;
4a8a2269 604 char *iface = NULL;
627e3f33 605 int interactive = 0;
b0bf6ad2 606 int flags = 0;
627e3f33
MG
607 char *ep;
608 int opt;
609
23d96b59 610 while((opt = getopt(argc, argv, "DdhiPp:Rl:v")) != -1) {
627e3f33
MG
611 switch (opt) {
612 case 'D':
613 debug = 1;
614 verbose = 1;
615 break;
616 case 'd':
b0bf6ad2 617 flags |= FLAG_DAEMON;
627e3f33
MG
618 break;
619 case 'i':
620 interactive = 1;
621 break;
b0bf6ad2
MG
622 case 'P':
623 flags |= FLAG_PID_FILE;
624 break;
627e3f33
MG
625 case 'p':
626 port = strtoul(optarg, &ep, 10);
627 if (*ep != '\0') {
628 fprintf(stderr, "Can't parse port!\n");
629 exit(EXIT_FAILURE);
630 }
631 break;
23d96b59 632 case 'R':
b55c340d 633 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 634 break;
4a8a2269
MG
635 case 'l':
636 iface = optarg;
637 break;
627e3f33
MG
638 case 'v':
639 verbose = 1;
640 break;
641 case 'h':
642 case ':':
643 case '?':
644 default:
645 hmlan_syntax(argv[0]);
646 exit(EXIT_FAILURE);
647 break;
648 }
649 }
650
651 if (interactive) {
23d96b59 652 return interactive_server(flags);
627e3f33 653 } else {
b0bf6ad2 654 return socket_server(iface, port, flags);
627e3f33 655 }
e0a7146e 656}
Impressum, Datenschutz