]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
1 /* HM-CFG-LAN emuldation 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 uint8_t *buf_out
= *outpos
;
60 uint8_t *outend
= *outpos
+ outlen
;
61 uint8_t *inend
= *inpos
+ inlen
;
64 if (flags
& FLAG_COMMA_BEFORE
) {
70 if (flags
& FLAG_LENGTH_BYTE
) {
76 if (flags
& FLAG_FORMAT_HEX
) {
79 memset(hex
, 0, sizeof(hex
));
83 for (i
= 0; i
< len
; i
++) {
84 if (snprintf(hex
, sizeof(hex
), "%02X", **inpos
) != 2) {
85 fprintf(stderr
, "Can't format hex-string!\n");
89 memcpy(*outpos
, hex
, 2);
95 memcpy(*outpos
, *inpos
, len
);
100 if (flags
& FLAG_COMMA_AFTER
) {
106 if (flags
& FLAG_NL
) {
114 return *outpos
- buf_out
;
117 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
119 uint8_t *buf_out
= *outpos
;
120 uint8_t *outend
= *outpos
+ outlen
;
121 uint8_t *inend
= *inpos
+ inlen
;
124 memset(hex
, 0, sizeof(hex
));
126 if (flags
& FLAG_LENGTH_BYTE
) {
134 if (!(flags
& FLAG_IGNORE_COMMAS
))
143 **outpos
= (len
/ 2);
147 while(*inpos
< inend
) {
148 if (**inpos
== ',') {
150 if (!(flags
& FLAG_IGNORE_COMMAS
))
158 memcpy(hex
, *inpos
, 2);
161 **outpos
= strtoul(hex
, NULL
, 16);
165 return *outpos
- buf_out
;
168 static void hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
173 int fd
= *((int*)data
);
178 memset(out
, 0, sizeof(out
));
182 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
185 if (impersonate_hmlanif
) {
190 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
191 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
192 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
193 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
194 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
195 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
196 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
200 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
201 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, 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
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
204 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
205 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
);
209 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, 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
)), 1, FLAG_FORMAT_HEX
);
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
)), 1, 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
| FLAG_NL
);
225 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
226 hexdump(buf
, buf_len
, "Unknown> ");
229 write(fd
, out
, outpos
-out
);
231 fprintf(stderr
, "LAN < %s\n", out
);
234 static int hmlan_parse_in(int fd
, void *data
)
236 struct hmcfgusb_dev
*dev
= data
;
238 uint8_t out
[0x40]; //FIXME!!!
245 memset(buf
, 0, sizeof(buf
));
247 r
= read(fd
, buf
, sizeof(buf
)-1);
249 uint8_t *inend
= buf
+ r
;
254 fprintf(stderr
, "LAN > %s\n", buf
);
256 while (inpos
< inend
) {
257 uint8_t *instart
= inpos
;
259 if ((*inpos
== '\r') || (*inpos
== '\n')) {
266 last
= inend
- inpos
;
268 for (i
= 0; i
< last
; i
++) {
269 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
278 memset(out
, 0, sizeof(out
));
279 *outpos
++ = *inpos
++;
283 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
284 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
285 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
286 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
287 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
288 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
291 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
295 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
307 static int comm(int fd_in
, int fd_out
, int master_socket
)
309 struct hmcfgusb_dev
*dev
;
312 hmcfgusb_set_debug(debug
);
314 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
316 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
320 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
321 fprintf(stderr
, "Can't add client to pollfd!\n");
326 if (master_socket
>= 0) {
327 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
328 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
334 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
339 fd
= hmcfgusb_poll(dev
, 3600);
341 if (fd
== master_socket
) {
344 client
= accept(master_socket
, NULL
, 0);
346 shutdown(client
, SHUT_RDWR
);
350 if (hmlan_parse_in(fd
, dev
) <= 0) {
354 } else if (fd
== -1) {
356 perror("hmcfgusb_poll");
366 static int socket_server(int port
, int daemon
)
368 struct sockaddr_in sin
;
376 printf("Daemon with PID %u started!\n", pid
);
378 } else if (pid
< 0) {
384 impersonate_hmlanif
= 1;
386 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
388 perror("Can't open socket");
393 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
394 perror("Can't set socket options");
398 memset(&sin
, 0, sizeof(sin
));
399 sin
.sin_family
= AF_INET
;
400 sin
.sin_port
= htons(port
);
401 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
403 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
404 perror("Can't bind socket");
408 if (listen(sock
, 1) == -1) {
409 perror("Can't listen on socket");
414 struct sockaddr_in csin
;
417 in_addr_t client_addr
;
419 memset(&csin
, 0, sizeof(csin
));
420 csinlen
= sizeof(csin
);
421 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
423 perror("Couldn't accept client");
427 /* FIXME: getnameinfo... */
428 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
431 printf("Client %d.%d.%d.%d connected!\n",
432 (client_addr
& 0xff000000) >> 24,
433 (client_addr
& 0x00ff0000) >> 16,
434 (client_addr
& 0x0000ff00) >> 8,
435 (client_addr
& 0x000000ff));
438 comm(client
, client
, sock
);
440 shutdown(client
, SHUT_RDWR
);
444 printf("Connection to %d.%d.%d.%d closed!\n",
445 (client_addr
& 0xff000000) >> 24,
446 (client_addr
& 0x00ff0000) >> 16,
447 (client_addr
& 0x0000ff00) >> 8,
448 (client_addr
& 0x000000ff));
456 static int interactive_server(void)
458 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
464 void hmlan_syntax(char *prog
)
466 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
467 fprintf(stderr
, "Possible options:\n");
468 fprintf(stderr
, "\t-D\tdebug mode\n");
469 fprintf(stderr
, "\t-d\tdaemon mode\n");
470 fprintf(stderr
, "\t-h\tthis help\n");
471 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
472 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
473 fprintf(stderr
, "\t-v\tverbose mode\n");
477 int main(int argc
, char **argv
)
485 while((opt
= getopt(argc
, argv
, "Ddhip:v")) != -1) {
498 port
= strtoul(optarg
, &ep
, 10);
500 fprintf(stderr
, "Can't parse port!\n");
511 hmlan_syntax(argv
[0]);
518 return interactive_server();
520 return socket_server(port
, daemon
);