]> git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
519e02b997fac6c5c0114b4cf78e07c1be79b0a1
[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 <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <libusb-1.0/libusb.h>
39
40 #include "hexdump.h"
41 #include "hmcfgusb.h"
42
43 #define PID_FILE "/var/run/hmland.pid"
44
45 extern char *optarg;
46
47 static int impersonate_hmlanif = 0;
48 static int debug = 0;
49 static int verbose = 0;
50
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
61 static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags)
62 {
63 const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
64 'A', 'B', 'C', 'D', 'E', 'F'};
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) {
83 CHECK_AVAIL(len);
84 CHECK_SPACE(len*2);
85 for (i = 0; i < len; i++) {
86 **outpos = nibble[((**inpos) & 0xf0) >> 4];
87 *outpos += 1;
88 **outpos = nibble[((**inpos) & 0xf)];
89 *inpos += 1; *outpos += 1;
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
116 static 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
131 static 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;
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);
169
170 **outpos = ascii_to_nibble(**inpos) << 4;
171 *inpos += 1;
172 **outpos |= ascii_to_nibble(**inpos);
173 *inpos += 1; *outpos += 1;
174 }
175
176 return *outpos - buf_out;
177 }
178
179 static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
180 {
181 uint8_t out[1024];
182 uint8_t *outpos;
183 uint8_t *inpos;
184 int fd = *((int*)data);
185 int w;
186
187 if (buf_len < 1)
188 return 1;
189
190 memset(out, 0, sizeof(out));
191 outpos = out;
192 inpos = buf;
193
194 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
195 switch(buf[0]) {
196 case 'H':
197 if (impersonate_hmlanif) {
198 buf[5] = 'L';
199 buf[6] = 'A';
200 buf[7] = 'N';
201 }
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);
209
210 break;
211 case 'E':
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
219 break;
220 case 'R':
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
228 break;
229 case 'I':
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;
236 default:
237 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
238 hexdump(buf, buf_len, "Unknown> ");
239 break;
240 }
241 if (debug)
242 fprintf(stderr, "LAN < %s\n", out);
243
244 w = write(fd, out, outpos-out);
245 if (w <= 0) {
246 perror("write");
247 return 0;
248 }
249
250 return 1;
251 }
252
253 static int hmlan_parse_in(int fd, void *data)
254 {
255 struct hmcfgusb_dev *dev = data;
256 uint8_t buf[1025];
257 uint8_t out[0x40]; //FIXME!!!
258 uint8_t *outpos;
259 uint8_t *inpos;
260 int i;
261 int last;
262 int r;
263
264 memset(buf, 0, sizeof(buf));
265
266 r = read(fd, buf, sizeof(buf)-1);
267 if (r > 0) {
268 uint8_t *inend = buf + r;
269
270 inpos = buf;
271
272 if (debug)
273 fprintf(stderr, "\nLAN > %s", buf);
274
275 while (inpos < inend) {
276 uint8_t *instart = inpos;
277
278 if ((*inpos == '\r') || (*inpos == '\n')) {
279 inpos++;
280 continue;
281 }
282
283 outpos = out;
284
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 }
313
314 hmcfgusb_send(dev, out, sizeof(out), 1);
315 }
316 } else if (r < 0) {
317 perror("read");
318 return r;
319 } else {
320 return 0;
321 }
322
323 return 1;
324 }
325
326 static int comm(int fd_in, int fd_out, int master_socket)
327 {
328 struct hmcfgusb_dev *dev;
329 int quit = 0;
330
331 hmcfgusb_set_debug(debug);
332
333 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
334 if (!dev) {
335 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
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;
343 }
344
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 }
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) {
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 }
373 } else if (fd == -1) {
374 if (errno) {
375 perror("hmcfgusb_poll");
376 quit = 1;
377 }
378 }
379 }
380
381 hmcfgusb_close(dev);
382 return 1;
383 }
384
385 void 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
396 static int socket_server(char *iface, int port, int flags)
397 {
398 struct sigaction sact;
399 struct sockaddr_in sin;
400 int sock;
401 int n;
402 pid_t pid;
403
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
428 pid = fork();
429 if (pid > 0) {
430 if (pidfile) {
431 fprintf(pidfile, "%u\n", pid);
432 fclose(pidfile);
433 }
434
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 }
441
442 if (pidfile)
443 fclose(pidfile);
444 }
445
446 memset(&sact, 0, sizeof(sact));
447 sact.sa_handler = SIG_IGN;
448
449 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
450 perror("sigaction(SIGPIPE)");
451 exit(EXIT_FAILURE);
452 }
453
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);
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 }
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;
494 in_addr_t client_addr;
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
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 }
514
515 comm(client, client, sock);
516
517 shutdown(client, SHUT_RDWR);
518 close(client);
519
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 }
527
528 }
529
530 return EXIT_SUCCESS;
531 }
532
533 static int interactive_server(void)
534 {
535 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1))
536 return EXIT_FAILURE;
537
538 return EXIT_SUCCESS;
539 }
540
541 void 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");
549 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
550 fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
551 fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
552 fprintf(stderr, "\t-v\tverbose mode\n");
553
554 }
555
556 int main(int argc, char **argv)
557 {
558 int port = 1000;
559 char *iface = NULL;
560 int interactive = 0;
561 int flags = 0;
562 char *ep;
563 int opt;
564
565 while((opt = getopt(argc, argv, "DdhiPp:l:v")) != -1) {
566 switch (opt) {
567 case 'D':
568 debug = 1;
569 verbose = 1;
570 break;
571 case 'd':
572 flags |= FLAG_DAEMON;
573 break;
574 case 'i':
575 interactive = 1;
576 break;
577 case 'P':
578 flags |= FLAG_PID_FILE;
579 break;
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;
587 case 'l':
588 iface = optarg;
589 break;
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 {
606 return socket_server(iface, port, flags);
607 }
608 }
Impressum, Datenschutz