]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
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
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>
43 static int impersonate_hmlanif
= 0;
45 static int verbose
= 0;
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)
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; }
57 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
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
;
66 if (flags
& FLAG_COMMA_BEFORE
) {
72 if (flags
& FLAG_LENGTH_BYTE
) {
78 if (flags
& FLAG_FORMAT_HEX
) {
81 for (i
= 0; i
< len
; i
++) {
82 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
84 **outpos
= nibble
[((**inpos
) & 0xf)];
85 *inpos
+= 1; *outpos
+= 1;
90 memcpy(*outpos
, *inpos
, len
);
95 if (flags
& FLAG_COMMA_AFTER
) {
101 if (flags
& FLAG_NL
) {
109 return *outpos
- buf_out
;
112 static uint8_t ascii_to_nibble(uint8_t a
)
116 if ((a
>= '0') && (a
<= '9')) {
118 } else if ((a
>= 'A') && (a
<= 'F')) {
120 } else if ((a
>= 'a') && (a
<= 'f')) {
127 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
129 uint8_t *buf_out
= *outpos
;
130 uint8_t *outend
= *outpos
+ outlen
;
131 uint8_t *inend
= *inpos
+ inlen
;
133 if (flags
& FLAG_LENGTH_BYTE
) {
141 if (!(flags
& FLAG_IGNORE_COMMAS
))
150 **outpos
= (len
/ 2);
154 while(*inpos
< inend
) {
155 if (**inpos
== ',') {
157 if (!(flags
& FLAG_IGNORE_COMMAS
))
166 **outpos
= ascii_to_nibble(**inpos
) << 4;
168 **outpos
|= ascii_to_nibble(**inpos
);
169 *inpos
+= 1; *outpos
+= 1;
172 return *outpos
- buf_out
;
175 static void hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
180 int fd
= *((int*)data
);
185 memset(out
, 0, sizeof(out
));
189 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
192 if (impersonate_hmlanif
) {
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
);
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
);
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
);
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
);
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> ");
236 write(fd
, out
, outpos
-out
);
238 fprintf(stderr
, "LAN < %s\n", out
);
241 static int hmlan_parse_in(int fd
, void *data
)
243 struct hmcfgusb_dev
*dev
= data
;
245 uint8_t out
[0x40]; //FIXME!!!
252 memset(buf
, 0, sizeof(buf
));
254 r
= read(fd
, buf
, sizeof(buf
)-1);
256 uint8_t *inend
= buf
+ r
;
261 fprintf(stderr
, "\nLAN > %s", buf
);
263 while (inpos
< inend
) {
264 uint8_t *instart
= inpos
;
266 if ((*inpos
== '\r') || (*inpos
== '\n')) {
273 last
= inend
- inpos
;
275 for (i
= 0; i
< last
; i
++) {
276 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
285 memset(out
, 0, sizeof(out
));
286 *outpos
++ = *inpos
++;
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
);
298 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
302 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
314 static int comm(int fd_in
, int fd_out
, int master_socket
)
316 struct hmcfgusb_dev
*dev
;
319 hmcfgusb_set_debug(debug
);
321 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
323 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
327 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
328 fprintf(stderr
, "Can't add client to pollfd!\n");
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");
341 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
346 fd
= hmcfgusb_poll(dev
, 3600);
348 if (fd
== master_socket
) {
351 client
= accept(master_socket
, NULL
, 0);
353 shutdown(client
, SHUT_RDWR
);
357 if (hmlan_parse_in(fd
, dev
) <= 0) {
361 } else if (fd
== -1) {
363 perror("hmcfgusb_poll");
373 static int socket_server(int port
, int daemon
)
375 struct sockaddr_in sin
;
383 printf("Daemon with PID %u started!\n", pid
);
385 } else if (pid
< 0) {
391 impersonate_hmlanif
= 1;
393 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
395 perror("Can't open socket");
400 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
401 perror("Can't set socket options");
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
);
410 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
411 perror("Can't bind socket");
415 if (listen(sock
, 1) == -1) {
416 perror("Can't listen on socket");
421 struct sockaddr_in csin
;
424 in_addr_t client_addr
;
426 memset(&csin
, 0, sizeof(csin
));
427 csinlen
= sizeof(csin
);
428 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
430 perror("Couldn't accept client");
434 /* FIXME: getnameinfo... */
435 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
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));
445 comm(client
, client
, sock
);
447 shutdown(client
, SHUT_RDWR
);
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));
463 static int interactive_server(void)
465 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
471 void hmlan_syntax(char *prog
)
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");
484 int main(int argc
, char **argv
)
492 while((opt
= getopt(argc
, argv
, "Ddhip:v")) != -1) {
505 port
= strtoul(optarg
, &ep
, 10);
507 fprintf(stderr
, "Can't parse port!\n");
518 hmlan_syntax(argv
[0]);
525 return interactive_server();
527 return socket_server(port
, daemon
);