]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
519e02b997fac6c5c0114b4cf78e07c1be79b0a1
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>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <libusb-1.0/libusb.h>
43 #define PID_FILE "/var/run/hmland.pid"
47 static int impersonate_hmlanif
= 0;
49 static int verbose
= 0;
51 #define FLAG_LENGTH_BYTE (1<<0)
52 #define FLAG_FORMAT_HEX (1<<1)
53 #define FLAG_COMMA_BEFORE (1<<2)
54 #define FLAG_COMMA_AFTER (1<<3)
55 #define FLAG_NL (1<<4)
56 #define FLAG_IGNORE_COMMAS (1<<5)
58 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
59 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
61 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
63 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
64 'A', 'B', 'C', 'D', 'E', 'F'};
65 uint8_t *buf_out
= *outpos
;
66 uint8_t *outend
= *outpos
+ outlen
;
67 uint8_t *inend
= *inpos
+ inlen
;
70 if (flags
& FLAG_COMMA_BEFORE
) {
76 if (flags
& FLAG_LENGTH_BYTE
) {
82 if (flags
& FLAG_FORMAT_HEX
) {
85 for (i
= 0; i
< len
; i
++) {
86 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
88 **outpos
= nibble
[((**inpos
) & 0xf)];
89 *inpos
+= 1; *outpos
+= 1;
94 memcpy(*outpos
, *inpos
, len
);
99 if (flags
& FLAG_COMMA_AFTER
) {
105 if (flags
& FLAG_NL
) {
113 return *outpos
- buf_out
;
116 static uint8_t ascii_to_nibble(uint8_t a
)
120 if ((a
>= '0') && (a
<= '9')) {
122 } else if ((a
>= 'A') && (a
<= 'F')) {
124 } else if ((a
>= 'a') && (a
<= 'f')) {
131 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
133 uint8_t *buf_out
= *outpos
;
134 uint8_t *outend
= *outpos
+ outlen
;
135 uint8_t *inend
= *inpos
+ inlen
;
137 if (flags
& FLAG_LENGTH_BYTE
) {
145 if (!(flags
& FLAG_IGNORE_COMMAS
))
154 **outpos
= (len
/ 2);
158 while(*inpos
< inend
) {
159 if (**inpos
== ',') {
161 if (!(flags
& FLAG_IGNORE_COMMAS
))
170 **outpos
= ascii_to_nibble(**inpos
) << 4;
172 **outpos
|= ascii_to_nibble(**inpos
);
173 *inpos
+= 1; *outpos
+= 1;
176 return *outpos
- buf_out
;
179 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
184 int fd
= *((int*)data
);
190 memset(out
, 0, sizeof(out
));
194 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
197 if (impersonate_hmlanif
) {
202 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
203 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
204 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
205 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
206 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
207 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
208 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
212 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
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
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
215 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
216 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
217 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
);
221 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
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
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
224 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
225 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
226 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
);
230 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
231 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
233 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
237 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
238 hexdump(buf
, buf_len
, "Unknown> ");
242 fprintf(stderr
, "LAN < %s\n", out
);
244 w
= write(fd
, out
, outpos
-out
);
253 static int hmlan_parse_in(int fd
, void *data
)
255 struct hmcfgusb_dev
*dev
= data
;
257 uint8_t out
[0x40]; //FIXME!!!
264 memset(buf
, 0, sizeof(buf
));
266 r
= read(fd
, buf
, sizeof(buf
)-1);
268 uint8_t *inend
= buf
+ r
;
273 fprintf(stderr
, "\nLAN > %s", buf
);
275 while (inpos
< inend
) {
276 uint8_t *instart
= inpos
;
278 if ((*inpos
== '\r') || (*inpos
== '\n')) {
285 last
= inend
- inpos
;
287 for (i
= 0; i
< last
; i
++) {
288 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
297 memset(out
, 0, sizeof(out
));
298 *outpos
++ = *inpos
++;
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
)), 0);
305 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
306 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
307 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
310 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
314 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
326 static int comm(int fd_in
, int fd_out
, int master_socket
)
328 struct hmcfgusb_dev
*dev
;
331 hmcfgusb_set_debug(debug
);
333 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
335 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
339 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
340 fprintf(stderr
, "Can't add client to pollfd!\n");
345 if (master_socket
>= 0) {
346 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
347 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
353 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
358 fd
= hmcfgusb_poll(dev
, 3600);
360 if (fd
== master_socket
) {
363 client
= accept(master_socket
, NULL
, 0);
365 shutdown(client
, SHUT_RDWR
);
369 if (hmlan_parse_in(fd
, dev
) <= 0) {
373 } else if (fd
== -1) {
375 perror("hmcfgusb_poll");
385 void sigterm_handler(int sig
)
387 if (unlink(PID_FILE
) == -1)
388 perror("Can't remove PID file");
393 #define FLAG_DAEMON (1 << 0)
394 #define FLAG_PID_FILE (1 << 1)
396 static int socket_server(char *iface
, int port
, int flags
)
398 struct sigaction sact
;
399 struct sockaddr_in sin
;
404 if (flags
& FLAG_DAEMON
) {
405 FILE *pidfile
= NULL
;
407 if (flags
& FLAG_PID_FILE
) {
410 old_umask
= umask(022);
411 pidfile
= fopen(PID_FILE
, "w");
415 perror("Can't create PID file " PID_FILE
);
419 memset(&sact
, 0, sizeof(sact
));
420 sact
.sa_handler
= sigterm_handler
;
422 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
423 perror("sigaction(SIGTERM)");
431 fprintf(pidfile
, "%u\n", pid
);
435 printf("Daemon with PID %u started!\n", pid
);
437 } else if (pid
< 0) {
446 memset(&sact
, 0, sizeof(sact
));
447 sact
.sa_handler
= SIG_IGN
;
449 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
450 perror("sigaction(SIGPIPE)");
454 impersonate_hmlanif
= 1;
456 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
458 perror("Can't open socket");
463 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
464 perror("Can't set socket options");
468 memset(&sin
, 0, sizeof(sin
));
469 sin
.sin_family
= AF_INET
;
470 sin
.sin_port
= htons(port
);
472 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
474 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
480 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
481 perror("Can't bind socket");
485 if (listen(sock
, 1) == -1) {
486 perror("Can't listen on socket");
491 struct sockaddr_in csin
;
494 in_addr_t client_addr
;
496 memset(&csin
, 0, sizeof(csin
));
497 csinlen
= sizeof(csin
);
498 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
500 perror("Couldn't accept client");
504 /* FIXME: getnameinfo... */
505 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
508 printf("Client %d.%d.%d.%d connected!\n",
509 (client_addr
& 0xff000000) >> 24,
510 (client_addr
& 0x00ff0000) >> 16,
511 (client_addr
& 0x0000ff00) >> 8,
512 (client_addr
& 0x000000ff));
515 comm(client
, client
, sock
);
517 shutdown(client
, SHUT_RDWR
);
521 printf("Connection to %d.%d.%d.%d closed!\n",
522 (client_addr
& 0xff000000) >> 24,
523 (client_addr
& 0x00ff0000) >> 16,
524 (client_addr
& 0x0000ff00) >> 8,
525 (client_addr
& 0x000000ff));
533 static int interactive_server(void)
535 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
541 void hmlan_syntax(char *prog
)
543 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
544 fprintf(stderr
, "Possible options:\n");
545 fprintf(stderr
, "\t-D\tdebug mode\n");
546 fprintf(stderr
, "\t-d\tdaemon mode\n");
547 fprintf(stderr
, "\t-h\tthis help\n");
548 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
549 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
550 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
551 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
552 fprintf(stderr
, "\t-v\tverbose mode\n");
556 int main(int argc
, char **argv
)
565 while((opt
= getopt(argc
, argv
, "DdhiPp:l:v")) != -1) {
572 flags
|= FLAG_DAEMON
;
578 flags
|= FLAG_PID_FILE
;
581 port
= strtoul(optarg
, &ep
, 10);
583 fprintf(stderr
, "Can't parse port!\n");
597 hmlan_syntax(argv
[0]);
604 return interactive_server();
606 return socket_server(iface
, port
, flags
);