]> cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
add option to bind socket to specified interface (e.g. localhost) only
[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 <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <libusb-1.0/libusb.h>
38
39 #include "hexdump.h"
40 #include "hmcfgusb.h"
41
42 extern char *optarg;
43
44 static int impersonate_hmlanif = 0;
45 static int debug = 0;
46 static int verbose = 0;
47
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
58 static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags)
59 {
60 const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
61 'A', 'B', 'C', 'D', 'E', 'F'};
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) {
80 CHECK_AVAIL(len);
81 CHECK_SPACE(len*2);
82 for (i = 0; i < len; i++) {
83 **outpos = nibble[((**inpos) & 0xf0) >> 4];
84 *outpos += 1;
85 **outpos = nibble[((**inpos) & 0xf)];
86 *inpos += 1; *outpos += 1;
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
113 static 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
128 static 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;
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);
166
167 **outpos = ascii_to_nibble(**inpos) << 4;
168 *inpos += 1;
169 **outpos |= ascii_to_nibble(**inpos);
170 *inpos += 1; *outpos += 1;
171 }
172
173 return *outpos - buf_out;
174 }
175
176 static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
177 {
178 uint8_t out[1024];
179 uint8_t *outpos;
180 uint8_t *inpos;
181 int fd = *((int*)data);
182 int w;
183
184 if (buf_len < 1)
185 return 1;
186
187 memset(out, 0, sizeof(out));
188 outpos = out;
189 inpos = buf;
190
191 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
192 switch(buf[0]) {
193 case 'H':
194 if (impersonate_hmlanif) {
195 buf[5] = 'L';
196 buf[6] = 'A';
197 buf[7] = 'N';
198 }
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);
206
207 break;
208 case 'E':
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
216 break;
217 case 'R':
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
225 break;
226 case 'I':
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;
233 default:
234 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
235 hexdump(buf, buf_len, "Unknown> ");
236 break;
237 }
238 if (debug)
239 fprintf(stderr, "LAN < %s\n", out);
240
241 w = write(fd, out, outpos-out);
242 if (w <= 0) {
243 perror("write");
244 return 0;
245 }
246
247 return 1;
248 }
249
250 static int hmlan_parse_in(int fd, void *data)
251 {
252 struct hmcfgusb_dev *dev = data;
253 uint8_t buf[1025];
254 uint8_t out[0x40]; //FIXME!!!
255 uint8_t *outpos;
256 uint8_t *inpos;
257 int i;
258 int last;
259 int r;
260
261 memset(buf, 0, sizeof(buf));
262
263 r = read(fd, buf, sizeof(buf)-1);
264 if (r > 0) {
265 uint8_t *inend = buf + r;
266
267 inpos = buf;
268
269 if (debug)
270 fprintf(stderr, "\nLAN > %s", buf);
271
272 while (inpos < inend) {
273 uint8_t *instart = inpos;
274
275 if ((*inpos == '\r') || (*inpos == '\n')) {
276 inpos++;
277 continue;
278 }
279
280 outpos = out;
281
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 }
310
311 hmcfgusb_send(dev, out, sizeof(out), 1);
312 }
313 } else if (r < 0) {
314 perror("read");
315 return r;
316 } else {
317 return 0;
318 }
319
320 return 1;
321 }
322
323 static int comm(int fd_in, int fd_out, int master_socket)
324 {
325 struct hmcfgusb_dev *dev;
326 int quit = 0;
327
328 hmcfgusb_set_debug(debug);
329
330 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
331 if (!dev) {
332 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
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;
340 }
341
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 }
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) {
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 }
370 } else if (fd == -1) {
371 if (errno) {
372 perror("hmcfgusb_poll");
373 quit = 1;
374 }
375 }
376 }
377
378 hmcfgusb_close(dev);
379 return 1;
380 }
381
382 static int socket_server(char *iface, int port, int daemon)
383 {
384 struct sigaction sact;
385 struct sockaddr_in sin;
386 int sock;
387 int n;
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 }
400
401 memset(&sact, 0, sizeof(sact));
402 sact.sa_handler = SIG_IGN;
403
404 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
405 perror("sigaction");
406 exit(EXIT_FAILURE);
407 }
408
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);
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 }
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;
449 in_addr_t client_addr;
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
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 }
469
470 comm(client, client, sock);
471
472 shutdown(client, SHUT_RDWR);
473 close(client);
474
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 }
482
483 }
484
485 return EXIT_SUCCESS;
486 }
487
488 static int interactive_server(void)
489 {
490 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1))
491 return EXIT_FAILURE;
492
493 return EXIT_SUCCESS;
494 }
495
496 void 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");
504 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
505 fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
506 fprintf(stderr, "\t-v\tverbose mode\n");
507
508 }
509
510 int main(int argc, char **argv)
511 {
512 int port = 1000;
513 char *iface = NULL;
514 int interactive = 0;
515 int daemon = 0;
516 char *ep;
517 int opt;
518
519 while((opt = getopt(argc, argv, "Ddhip:l:v")) != -1) {
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;
538 case 'l':
539 iface = optarg;
540 break;
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 {
557 return socket_server(iface, port, daemon);
558 }
559 }
Impressum, Datenschutz