]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
04ee740c2855104e4b42eeb4f2c7cad221be2c72
1 /* HM-CFG-LAN emulation for HM-CFG-USB
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
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>
44 static int impersonate_hmlanif
= 0;
46 static int verbose
= 0;
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)
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; }
58 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
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
;
67 if (flags
& FLAG_COMMA_BEFORE
) {
73 if (flags
& FLAG_LENGTH_BYTE
) {
79 if (flags
& FLAG_FORMAT_HEX
) {
82 for (i
= 0; i
< len
; i
++) {
83 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
85 **outpos
= nibble
[((**inpos
) & 0xf)];
86 *inpos
+= 1; *outpos
+= 1;
91 memcpy(*outpos
, *inpos
, len
);
96 if (flags
& FLAG_COMMA_AFTER
) {
102 if (flags
& FLAG_NL
) {
110 return *outpos
- buf_out
;
113 static uint8_t ascii_to_nibble(uint8_t a
)
117 if ((a
>= '0') && (a
<= '9')) {
119 } else if ((a
>= 'A') && (a
<= 'F')) {
121 } else if ((a
>= 'a') && (a
<= 'f')) {
128 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
130 uint8_t *buf_out
= *outpos
;
131 uint8_t *outend
= *outpos
+ outlen
;
132 uint8_t *inend
= *inpos
+ inlen
;
134 if (flags
& FLAG_LENGTH_BYTE
) {
142 if (!(flags
& FLAG_IGNORE_COMMAS
))
151 **outpos
= (len
/ 2);
155 while(*inpos
< inend
) {
156 if (**inpos
== ',') {
158 if (!(flags
& FLAG_IGNORE_COMMAS
))
167 **outpos
= ascii_to_nibble(**inpos
) << 4;
169 **outpos
|= ascii_to_nibble(**inpos
);
170 *inpos
+= 1; *outpos
+= 1;
173 return *outpos
- buf_out
;
176 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
181 int fd
= *((int*)data
);
187 memset(out
, 0, sizeof(out
));
191 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
194 if (impersonate_hmlanif
) {
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
);
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
);
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
);
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
);
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> ");
239 fprintf(stderr
, "LAN < %s\n", out
);
241 w
= write(fd
, out
, outpos
-out
);
250 static int hmlan_parse_in(int fd
, void *data
)
252 struct hmcfgusb_dev
*dev
= data
;
254 uint8_t out
[0x40]; //FIXME!!!
261 memset(buf
, 0, sizeof(buf
));
263 r
= read(fd
, buf
, sizeof(buf
)-1);
265 uint8_t *inend
= buf
+ r
;
270 fprintf(stderr
, "\nLAN > %s", buf
);
272 while (inpos
< inend
) {
273 uint8_t *instart
= inpos
;
275 if ((*inpos
== '\r') || (*inpos
== '\n')) {
282 last
= inend
- inpos
;
284 for (i
= 0; i
< last
; i
++) {
285 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
294 memset(out
, 0, sizeof(out
));
295 *outpos
++ = *inpos
++;
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
);
307 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
311 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
323 static int comm(int fd_in
, int fd_out
, int master_socket
)
325 struct hmcfgusb_dev
*dev
;
328 hmcfgusb_set_debug(debug
);
330 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
332 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
336 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
337 fprintf(stderr
, "Can't add client to pollfd!\n");
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");
350 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
355 fd
= hmcfgusb_poll(dev
, 3600);
357 if (fd
== master_socket
) {
360 client
= accept(master_socket
, NULL
, 0);
362 shutdown(client
, SHUT_RDWR
);
366 if (hmlan_parse_in(fd
, dev
) <= 0) {
370 } else if (fd
== -1) {
372 perror("hmcfgusb_poll");
382 static int socket_server(int port
, int daemon
)
384 struct sigaction sact
;
385 struct sockaddr_in sin
;
393 printf("Daemon with PID %u started!\n", pid
);
395 } else if (pid
< 0) {
401 memset(&sact
, 0, sizeof(sact
));
402 sact
.sa_handler
= SIG_IGN
;
404 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
408 impersonate_hmlanif
= 1;
410 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
412 perror("Can't open socket");
417 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
418 perror("Can't set socket options");
422 memset(&sin
, 0, sizeof(sin
));
423 sin
.sin_family
= AF_INET
;
424 sin
.sin_port
= htons(port
);
425 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
427 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
428 perror("Can't bind socket");
432 if (listen(sock
, 1) == -1) {
433 perror("Can't listen on socket");
438 struct sockaddr_in csin
;
441 in_addr_t client_addr
;
443 memset(&csin
, 0, sizeof(csin
));
444 csinlen
= sizeof(csin
);
445 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
447 perror("Couldn't accept client");
451 /* FIXME: getnameinfo... */
452 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
455 printf("Client %d.%d.%d.%d connected!\n",
456 (client_addr
& 0xff000000) >> 24,
457 (client_addr
& 0x00ff0000) >> 16,
458 (client_addr
& 0x0000ff00) >> 8,
459 (client_addr
& 0x000000ff));
462 comm(client
, client
, sock
);
464 shutdown(client
, SHUT_RDWR
);
468 printf("Connection to %d.%d.%d.%d closed!\n",
469 (client_addr
& 0xff000000) >> 24,
470 (client_addr
& 0x00ff0000) >> 16,
471 (client_addr
& 0x0000ff00) >> 8,
472 (client_addr
& 0x000000ff));
480 static int interactive_server(void)
482 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
488 void hmlan_syntax(char *prog
)
490 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
491 fprintf(stderr
, "Possible options:\n");
492 fprintf(stderr
, "\t-D\tdebug mode\n");
493 fprintf(stderr
, "\t-d\tdaemon mode\n");
494 fprintf(stderr
, "\t-h\tthis help\n");
495 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
496 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
497 fprintf(stderr
, "\t-v\tverbose mode\n");
501 int main(int argc
, char **argv
)
509 while((opt
= getopt(argc
, argv
, "Ddhip:v")) != -1) {
522 port
= strtoul(optarg
, &ep
, 10);
524 fprintf(stderr
, "Can't parse port!\n");
535 hmlan_syntax(argv
[0]);
542 return interactive_server();
544 return socket_server(port
, daemon
);