]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
1 /* HM-CFG-LAN emulation for HM-CFG-USB
3 * Copyright (c) 2013-16 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
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <libusb-1.0/libusb.h>
48 #define PID_FILE "/var/run/hmland.pid"
50 #define POLL_TIMEOUT_MS 250 /* Wake up device/bus at least once every 250ms */
51 #define DEFAULT_REBOOT_SECONDS 86400
52 #define LAN_READ_CHUNK_SIZE 2048
53 /* Don't allow remote clients to consume all of our memory */
54 #define LAN_MAX_LINE_LENGTH 4096
55 #define LAN_MAX_BUF_LENGTH 1048576
59 static int impersonate_hmlanif
= 0;
61 static int verbose
= 0;
62 static FILE *logfile
= NULL
;
63 static int reboot_seconds
= 0;
64 static int reboot_at_hour
= -1;
65 static int reboot_at_minute
= -1;
66 static int reboot_set
= 0;
67 static uint8_t *lan_read_buf
= NULL
;
68 static int lan_read_buflen
= 0;
69 static char *serial
= NULL
;
74 struct queued_rx
*next
;
77 static struct queued_rx
*qrx
= NULL
;
78 static int wait_for_h
= 0;
80 #define FLAG_LENGTH_BYTE (1<<0)
81 #define FLAG_FORMAT_HEX (1<<1)
82 #define FLAG_COMMA_BEFORE (1<<2)
83 #define FLAG_COMMA_AFTER (1<<3)
84 #define FLAG_NL (1<<4)
85 #define FLAG_IGNORE_COMMAS (1<<5)
87 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
88 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
90 static void print_timestamp(FILE *f
)
96 gettimeofday(&tv
, NULL
);
97 tmp
= localtime(&tv
.tv_sec
);
98 memset(ts
, 0, sizeof(ts
));
99 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
100 fprintf(f
, "%s.%06ld: ", ts
, tv
.tv_usec
);
103 static void write_log(const char *buf
, int len
, const char *fmt
, ...)
108 if ((!logfile
) && (!verbose
))
112 print_timestamp(logfile
);
114 print_timestamp(stdout
);
119 #pragma GCC diagnostic push
120 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
121 vfprintf(logfile
, fmt
, ap
);
122 #pragma GCC diagnostic pop
127 #pragma GCC diagnostic push
128 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
130 #pragma GCC diagnostic pop
136 for (i
= 0; i
< len
; i
++) {
138 fprintf(logfile
, "%c", buf
[i
]);
140 printf("%c", buf
[i
]);
143 fprintf(logfile
, "\n");
151 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, size_t len
, int flags
)
153 uint8_t *buf_out
= *outpos
;
154 uint8_t *outend
= *outpos
+ outlen
;
155 uint8_t *inend
= *inpos
+ inlen
;
158 if (flags
& FLAG_COMMA_BEFORE
) {
164 if (flags
& FLAG_LENGTH_BYTE
) {
170 if (flags
& FLAG_FORMAT_HEX
) {
173 for (i
= 0; i
< len
; i
++) {
174 **outpos
= nibble_to_ascii(((**inpos
) & 0xf0) >> 4);
176 **outpos
= nibble_to_ascii(((**inpos
) & 0xf));
177 *inpos
+= 1; *outpos
+= 1;
182 memcpy(*outpos
, *inpos
, len
);
187 if (flags
& FLAG_COMMA_AFTER
) {
193 if (flags
& FLAG_NL
) {
201 return *outpos
- buf_out
;
204 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
206 uint8_t *buf_out
= *outpos
;
207 uint8_t *outend
= *outpos
+ outlen
;
208 uint8_t *inend
= *inpos
+ inlen
;
210 if (flags
& FLAG_LENGTH_BYTE
) {
218 if (!(flags
& FLAG_IGNORE_COMMAS
))
227 **outpos
= (len
/ 2);
231 while(*inpos
< inend
) {
232 if (**inpos
== ',') {
234 if (!(flags
& FLAG_IGNORE_COMMAS
))
243 **outpos
= ascii_to_nibble(**inpos
) << 4;
245 **outpos
|= ascii_to_nibble(**inpos
);
246 *inpos
+= 1; *outpos
+= 1;
249 return *outpos
- buf_out
;
252 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
258 int fd
= *((int*)data
);
264 memset(out
, 0, sizeof(out
));
268 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
271 if (impersonate_hmlanif
) {
276 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
277 version
= inpos
[0] << 8;
279 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
280 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
281 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
282 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
283 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
284 if (version
< 0x03c7) {
285 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
287 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
288 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
292 int new_reboot_seconds
;
294 if (version
< 0x03c7) {
295 new_reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
297 new_reboot_seconds
= 0;
300 if (verbose
&& new_reboot_seconds
&& (reboot_seconds
!= new_reboot_seconds
))
301 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
302 new_reboot_seconds
, version
);
304 reboot_seconds
= new_reboot_seconds
;
309 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
310 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
311 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
312 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
313 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
314 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
);
318 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
319 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
320 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
321 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
322 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
323 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
);
327 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
328 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
329 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
330 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
334 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_NL
);
338 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
339 hexdump(buf
, buf_len
, "Unknown> ");
343 /* Queue packet until first respone to 'K' is received */
344 if (wait_for_h
&& buf
[0] != 'H') {
345 struct queued_rx
**rxp
= &qrx
;
348 rxp
= &((*rxp
)->next
);
350 *rxp
= malloc(sizeof(struct queued_rx
));
356 memset(*rxp
, 0, sizeof(struct queued_rx
));
357 (*rxp
)->len
= outpos
-out
;
358 (*rxp
)->rx
= malloc((*rxp
)->len
);
363 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
364 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
369 write_log((char*)out
, outpos
-out
-2, "LAN < ");
371 w
= write(fd
, out
, outpos
-out
);
377 /* Send all queued packets */
379 struct queued_rx
*curr_rx
= qrx
;
380 struct queued_rx
*last_rx
;
383 write_log(curr_rx
->rx
, curr_rx
->len
-2, "LAN < ");
385 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
390 curr_rx
= curr_rx
->next
;
404 static int hmlan_parse_one(uint8_t *cmd
, int last
, void *data
)
406 struct hmcfgusb_dev
*dev
= data
;
407 uint8_t out
[0x40]; //FIXME!!!
409 uint8_t *inpos
= cmd
;
416 write_log((char*)cmd
, last
, "LAN > ");
418 memset(out
, 0, sizeof(out
));
419 *outpos
++ = *inpos
++;
423 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
424 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
425 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
426 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
427 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
428 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
431 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
432 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
433 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
436 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
437 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
438 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
439 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
440 // Fallthrough to default expected, no break here
442 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
446 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
451 static int hmlan_parse_in(int fd
, void *data
)
457 newbuf
= realloc(lan_read_buf
, lan_read_buflen
+ LAN_READ_CHUNK_SIZE
);
462 lan_read_buf
= newbuf
;
463 r
= read(fd
, lan_read_buf
+ lan_read_buflen
, LAN_READ_CHUNK_SIZE
);
465 lan_read_buflen
+= r
;
466 if (lan_read_buflen
> LAN_MAX_BUF_LENGTH
) {
468 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH
, lan_read_buflen
);
471 while(lan_read_buflen
> 0) {
474 for (i
= 0; i
< lan_read_buflen
; i
++) {
475 if ((lan_read_buf
[i
] == '\r') || (lan_read_buf
[i
] == '\n')) {
477 hmlan_parse_one(lan_read_buf
, i
, data
);
478 memmove(lan_read_buf
, lan_read_buf
+ i
+ 1, lan_read_buflen
- (i
+ 1));
479 lan_read_buflen
-= (i
+ 1);
483 if (i
> LAN_MAX_LINE_LENGTH
) {
485 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH
);
491 newbuf
= realloc(lan_read_buf
, lan_read_buflen
);
492 if (lan_read_buflen
&& !newbuf
) {
496 lan_read_buf
= newbuf
;
499 if (errno
!= ECONNRESET
)
509 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
511 struct hmcfgusb_dev
*dev
;
512 uint8_t out
[0x40]; //FIXME!!!
515 hmcfgusb_set_debug(debug
);
517 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
, serial
);
519 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
523 if (dev
->bootloader
) {
525 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
527 hmcfgusb_leave_bootloader(dev
);
534 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
539 tm_s
= localtime(&tm
);
545 tm_s
->tm_hour
= reboot_at_hour
;
546 tm_s
->tm_min
= reboot_at_minute
;
550 reboot_seconds
= tm
- dev
->opened_at
;
552 while (reboot_seconds
<= 0)
553 reboot_seconds
+= 86400;
556 if (verbose
&& reboot_seconds
)
557 printf("Rebooting in %u seconds\n", reboot_seconds
);
560 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
561 fprintf(stderr
, "Can't add client to pollfd!\n");
566 if (master_socket
>= 0) {
567 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
568 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
574 memset(out
, 0, sizeof(out
));
577 hmcfgusb_send_null_frame(dev
, 1);
578 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
583 fd
= hmcfgusb_poll(dev
, POLL_TIMEOUT_MS
);
585 if (fd
== master_socket
) {
588 client
= accept(master_socket
, NULL
, 0);
590 shutdown(client
, SHUT_RDWR
);
594 if (hmlan_parse_in(fd
, dev
) <= 0) {
598 } else if (fd
== -1) {
600 if (errno
!= ETIMEDOUT
) {
601 perror("hmcfgusb_poll");
604 /* periodically wakeup the device */
605 hmcfgusb_send_null_frame(dev
, 1);
607 memset(out
, 0, sizeof(out
));
609 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
615 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
617 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
618 time(NULL
) - dev
->opened_at
);
620 hmcfgusb_enter_bootloader(dev
);
628 void sigterm_handler(int sig
)
630 if (unlink(PID_FILE
) == -1)
631 perror("Can't remove PID file");
636 #define FLAG_DAEMON (1 << 0)
637 #define FLAG_PID_FILE (1 << 1)
639 static int socket_server(char *iface
, int port
, int flags
)
641 struct sigaction sact
;
642 struct sockaddr_in sin
;
647 if (flags
& FLAG_DAEMON
) {
648 FILE *pidfile
= NULL
;
650 if (flags
& FLAG_PID_FILE
) {
653 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
655 if (errno
== EEXIST
) {
657 pidfile
= fopen(PID_FILE
, "r");
659 perror("PID file " PID_FILE
" already exists, already running?");
663 if (fscanf(pidfile
, "%d", &old_pid
) != 1) {
665 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
671 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
674 perror("Can't create PID file " PID_FILE
);
678 pidfile
= fdopen(fd
, "w");
680 perror("Can't reopen PID file fd");
684 memset(&sact
, 0, sizeof(sact
));
685 sact
.sa_handler
= sigterm_handler
;
687 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
688 perror("sigaction(SIGTERM)");
696 fprintf(pidfile
, "%u\n", pid
);
700 printf("Daemon with PID %u started!\n", pid
);
702 } else if (pid
< 0) {
711 memset(&sact
, 0, sizeof(sact
));
712 sact
.sa_handler
= SIG_IGN
;
714 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
715 perror("sigaction(SIGPIPE)");
719 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
721 perror("Can't open socket");
726 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
727 perror("Can't set socket options");
731 memset(&sin
, 0, sizeof(sin
));
732 sin
.sin_family
= AF_INET
;
733 sin
.sin_port
= htons(port
);
735 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
737 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
738 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
743 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
744 perror("Can't bind socket");
748 if (listen(sock
, 1) == -1) {
749 perror("Can't listen on socket");
754 struct sockaddr_in csin
;
757 in_addr_t client_addr
;
759 memset(&csin
, 0, sizeof(csin
));
760 csinlen
= sizeof(csin
);
761 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
763 perror("Couldn't accept client");
767 /* FIXME: getnameinfo... */
768 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
770 write_log(NULL
, 0, "Client %d.%d.%d.%d connected!\n",
771 (client_addr
& 0xff000000) >> 24,
772 (client_addr
& 0x00ff0000) >> 16,
773 (client_addr
& 0x0000ff00) >> 8,
774 (client_addr
& 0x000000ff));
776 comm(client
, client
, sock
, flags
);
778 shutdown(client
, SHUT_RDWR
);
786 write_log(NULL
, 0, "Connection to %d.%d.%d.%d closed!\n",
787 (client_addr
& 0xff000000) >> 24,
788 (client_addr
& 0x00ff0000) >> 16,
789 (client_addr
& 0x0000ff00) >> 8,
790 (client_addr
& 0x000000ff));
797 static int interactive_server(int flags
)
799 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
805 void hmlan_syntax(char *prog
)
807 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
808 fprintf(stderr
, "Possible options:\n");
809 fprintf(stderr
, "\t-D\t\tdebug mode\n");
810 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
811 fprintf(stderr
, "\t-h\t\tthis help\n");
812 fprintf(stderr
, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
813 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
814 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
815 fprintf(stderr
, "\t-L logfile\tlog network-communication to logfile\n");
816 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
817 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
818 fprintf(stderr
, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u if FW < 0.967, 0 otherwise)\n", DEFAULT_REBOOT_SECONDS
);
819 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
820 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
821 fprintf(stderr
, "\t-v\t\tverbose mode\n");
822 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
826 int main(int argc
, char **argv
)
835 while((opt
= getopt(argc
, argv
, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
842 flags
|= FLAG_DAEMON
;
845 impersonate_hmlanif
= 1;
851 flags
|= FLAG_PID_FILE
;
854 port
= strtoul(optarg
, &ep
, 10);
856 fprintf(stderr
, "Can't parse port!\n");
861 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
864 reboot_seconds
= strtoul(optarg
, &ep
, 10);
867 reboot_at_hour
= reboot_seconds
;
869 reboot_at_minute
= strtoul(ep
, &ep
, 10);
871 fprintf(stderr
, "Can't parse reboot-time!\n");
877 fprintf(stderr
, "Can't parse reboot-timeout!\n");
887 logfile
= fopen(optarg
, "a");
889 perror("fopen(logfile)");
900 printf("hmland " VERSION
"\n");
901 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
907 hmlan_syntax(argv
[0]);
914 return interactive_server(flags
);
916 return socket_server(iface
, port
, flags
);