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