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