]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
35f819280fb4ca0d36bbd5b289fe90bbeac628cf
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 vfprintf(logfile
, fmt
, ap
);
130 for (i
= 0; i
< len
; i
++) {
132 fprintf(logfile
, "%c", buf
[i
]);
134 printf("%c", buf
[i
]);
137 fprintf(logfile
, "\n");
145 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, size_t len
, int flags
)
147 uint8_t *buf_out
= *outpos
;
148 uint8_t *outend
= *outpos
+ outlen
;
149 uint8_t *inend
= *inpos
+ inlen
;
152 if (flags
& FLAG_COMMA_BEFORE
) {
158 if (flags
& FLAG_LENGTH_BYTE
) {
164 if (flags
& FLAG_FORMAT_HEX
) {
167 for (i
= 0; i
< len
; i
++) {
168 **outpos
= nibble_to_ascii(((**inpos
) & 0xf0) >> 4);
170 **outpos
= nibble_to_ascii(((**inpos
) & 0xf));
171 *inpos
+= 1; *outpos
+= 1;
176 memcpy(*outpos
, *inpos
, len
);
181 if (flags
& FLAG_COMMA_AFTER
) {
187 if (flags
& FLAG_NL
) {
195 return *outpos
- buf_out
;
198 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
200 uint8_t *buf_out
= *outpos
;
201 uint8_t *outend
= *outpos
+ outlen
;
202 uint8_t *inend
= *inpos
+ inlen
;
204 if (flags
& FLAG_LENGTH_BYTE
) {
212 if (!(flags
& FLAG_IGNORE_COMMAS
))
221 **outpos
= (len
/ 2);
225 while(*inpos
< inend
) {
226 if (**inpos
== ',') {
228 if (!(flags
& FLAG_IGNORE_COMMAS
))
237 **outpos
= ascii_to_nibble(**inpos
) << 4;
239 **outpos
|= ascii_to_nibble(**inpos
);
240 *inpos
+= 1; *outpos
+= 1;
243 return *outpos
- buf_out
;
246 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
252 int fd
= *((int*)data
);
258 memset(out
, 0, sizeof(out
));
262 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
265 if (impersonate_hmlanif
) {
270 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
271 version
= inpos
[0] << 8;
273 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
274 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
275 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
276 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
277 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
278 if (version
< 0x03c7) {
279 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
281 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
282 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
286 int new_reboot_seconds
;
288 if (version
< 0x03c7) {
289 new_reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
291 new_reboot_seconds
= 0;
294 if (verbose
&& new_reboot_seconds
&& (reboot_seconds
!= new_reboot_seconds
))
295 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
296 new_reboot_seconds
, version
);
298 reboot_seconds
= new_reboot_seconds
;
303 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
304 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
305 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
306 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
307 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
308 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
);
312 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
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
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
315 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
316 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
317 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
);
321 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
322 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
323 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
324 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
328 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_NL
);
332 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
333 hexdump(buf
, buf_len
, "Unknown> ");
337 /* Queue packet until first respone to 'K' is received */
338 if (wait_for_h
&& buf
[0] != 'H') {
339 struct queued_rx
**rxp
= &qrx
;
342 rxp
= &((*rxp
)->next
);
344 *rxp
= malloc(sizeof(struct queued_rx
));
350 memset(*rxp
, 0, sizeof(struct queued_rx
));
351 (*rxp
)->len
= outpos
-out
;
352 (*rxp
)->rx
= malloc((*rxp
)->len
);
357 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
358 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
363 write_log((char*)out
, outpos
-out
-2, "LAN < ");
365 w
= write(fd
, out
, outpos
-out
);
371 /* Send all queued packets */
373 struct queued_rx
*curr_rx
= qrx
;
374 struct queued_rx
*last_rx
;
377 write_log(curr_rx
->rx
, curr_rx
->len
-2, "LAN < ");
379 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
384 curr_rx
= curr_rx
->next
;
398 static int hmlan_parse_one(uint8_t *cmd
, int last
, void *data
)
400 struct hmcfgusb_dev
*dev
= data
;
401 uint8_t out
[0x40]; //FIXME!!!
403 uint8_t *inpos
= cmd
;
410 write_log((char*)cmd
, last
, "LAN > ");
412 memset(out
, 0, sizeof(out
));
413 *outpos
++ = *inpos
++;
417 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
418 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
419 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
420 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
421 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
422 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
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
)), FLAG_LENGTH_BYTE
);
430 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_IGNORE_COMMAS
);
440 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
445 static int hmlan_parse_in(int fd
, void *data
)
451 newbuf
= realloc(lan_read_buf
, lan_read_buflen
+ LAN_READ_CHUNK_SIZE
);
456 lan_read_buf
= newbuf
;
457 r
= read(fd
, lan_read_buf
+ lan_read_buflen
, LAN_READ_CHUNK_SIZE
);
459 lan_read_buflen
+= r
;
460 if (lan_read_buflen
> LAN_MAX_BUF_LENGTH
) {
462 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH
, lan_read_buflen
);
465 while(lan_read_buflen
> 0) {
468 for (i
= 0; i
< lan_read_buflen
; i
++) {
469 if ((lan_read_buf
[i
] == '\r') || (lan_read_buf
[i
] == '\n')) {
471 hmlan_parse_one(lan_read_buf
, i
, data
);
472 memmove(lan_read_buf
, lan_read_buf
+ i
+ 1, lan_read_buflen
- (i
+ 1));
473 lan_read_buflen
-= (i
+ 1);
477 if (i
> LAN_MAX_LINE_LENGTH
) {
479 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH
);
485 newbuf
= realloc(lan_read_buf
, lan_read_buflen
);
486 if (lan_read_buflen
&& !newbuf
) {
490 lan_read_buf
= newbuf
;
493 if (errno
!= ECONNRESET
)
503 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
505 struct hmcfgusb_dev
*dev
;
506 uint8_t out
[0x40]; //FIXME!!!
509 hmcfgusb_set_debug(debug
);
511 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
, serial
);
513 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
517 if (dev
->bootloader
) {
519 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
521 hmcfgusb_leave_bootloader(dev
);
528 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
533 tm_s
= localtime(&tm
);
539 tm_s
->tm_hour
= reboot_at_hour
;
540 tm_s
->tm_min
= reboot_at_minute
;
544 reboot_seconds
= tm
- dev
->opened_at
;
546 while (reboot_seconds
<= 0)
547 reboot_seconds
+= 86400;
550 if (verbose
&& reboot_seconds
)
551 printf("Rebooting in %u seconds\n", reboot_seconds
);
554 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
555 fprintf(stderr
, "Can't add client to pollfd!\n");
560 if (master_socket
>= 0) {
561 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
562 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
568 memset(out
, 0, sizeof(out
));
571 hmcfgusb_send_null_frame(dev
, 1);
572 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
577 fd
= hmcfgusb_poll(dev
, POLL_TIMEOUT_MS
);
579 if (fd
== master_socket
) {
582 client
= accept(master_socket
, NULL
, 0);
584 shutdown(client
, SHUT_RDWR
);
588 if (hmlan_parse_in(fd
, dev
) <= 0) {
592 } else if (fd
== -1) {
594 if (errno
!= ETIMEDOUT
) {
595 perror("hmcfgusb_poll");
598 /* periodically wakeup the device */
599 hmcfgusb_send_null_frame(dev
, 1);
601 memset(out
, 0, sizeof(out
));
603 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
609 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
611 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
612 time(NULL
) - dev
->opened_at
);
614 hmcfgusb_enter_bootloader(dev
);
622 void sigterm_handler(int sig
)
624 if (unlink(PID_FILE
) == -1)
625 perror("Can't remove PID file");
630 #define FLAG_DAEMON (1 << 0)
631 #define FLAG_PID_FILE (1 << 1)
633 static int socket_server(char *iface
, int port
, int flags
)
635 struct sigaction sact
;
636 struct sockaddr_in sin
;
641 if (flags
& FLAG_DAEMON
) {
642 FILE *pidfile
= NULL
;
644 if (flags
& FLAG_PID_FILE
) {
647 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
649 if (errno
== EEXIST
) {
651 pidfile
= fopen(PID_FILE
, "r");
653 perror("PID file " PID_FILE
" already exists, already running?");
657 if (fscanf(pidfile
, "%d", &old_pid
) != 1) {
659 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
665 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
668 perror("Can't create PID file " PID_FILE
);
672 pidfile
= fdopen(fd
, "w");
674 perror("Can't reopen PID file fd");
678 memset(&sact
, 0, sizeof(sact
));
679 sact
.sa_handler
= sigterm_handler
;
681 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
682 perror("sigaction(SIGTERM)");
690 fprintf(pidfile
, "%u\n", pid
);
694 printf("Daemon with PID %u started!\n", pid
);
696 } else if (pid
< 0) {
705 memset(&sact
, 0, sizeof(sact
));
706 sact
.sa_handler
= SIG_IGN
;
708 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
709 perror("sigaction(SIGPIPE)");
713 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
715 perror("Can't open socket");
720 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
721 perror("Can't set socket options");
725 memset(&sin
, 0, sizeof(sin
));
726 sin
.sin_family
= AF_INET
;
727 sin
.sin_port
= htons(port
);
729 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
731 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
732 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
737 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
738 perror("Can't bind socket");
742 if (listen(sock
, 1) == -1) {
743 perror("Can't listen on socket");
748 struct sockaddr_in csin
;
751 in_addr_t client_addr
;
753 memset(&csin
, 0, sizeof(csin
));
754 csinlen
= sizeof(csin
);
755 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
757 perror("Couldn't accept client");
761 /* FIXME: getnameinfo... */
762 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
764 write_log(NULL
, 0, "Client %d.%d.%d.%d connected!\n",
765 (client_addr
& 0xff000000) >> 24,
766 (client_addr
& 0x00ff0000) >> 16,
767 (client_addr
& 0x0000ff00) >> 8,
768 (client_addr
& 0x000000ff));
770 comm(client
, client
, sock
, flags
);
772 shutdown(client
, SHUT_RDWR
);
780 write_log(NULL
, 0, "Connection to %d.%d.%d.%d closed!\n",
781 (client_addr
& 0xff000000) >> 24,
782 (client_addr
& 0x00ff0000) >> 16,
783 (client_addr
& 0x0000ff00) >> 8,
784 (client_addr
& 0x000000ff));
791 static int interactive_server(int flags
)
793 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
799 void hmlan_syntax(char *prog
)
801 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
802 fprintf(stderr
, "Possible options:\n");
803 fprintf(stderr
, "\t-D\t\tdebug mode\n");
804 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
805 fprintf(stderr
, "\t-h\t\tthis help\n");
806 fprintf(stderr
, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
807 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
808 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
809 fprintf(stderr
, "\t-L logfile\tlog network-communication to logfile\n");
810 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
811 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
812 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
);
813 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
814 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
815 fprintf(stderr
, "\t-v\t\tverbose mode\n");
816 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
820 int main(int argc
, char **argv
)
829 while((opt
= getopt(argc
, argv
, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
836 flags
|= FLAG_DAEMON
;
839 impersonate_hmlanif
= 1;
845 flags
|= FLAG_PID_FILE
;
848 port
= strtoul(optarg
, &ep
, 10);
850 fprintf(stderr
, "Can't parse port!\n");
855 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
858 reboot_seconds
= strtoul(optarg
, &ep
, 10);
861 reboot_at_hour
= reboot_seconds
;
863 reboot_at_minute
= strtoul(ep
, &ep
, 10);
865 fprintf(stderr
, "Can't parse reboot-time!\n");
871 fprintf(stderr
, "Can't parse reboot-timeout!\n");
881 logfile
= fopen(optarg
, "a");
883 perror("fopen(logfile)");
894 printf("hmland " VERSION
"\n");
895 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
901 hmlan_syntax(argv
[0]);
908 return interactive_server(flags
);
910 return socket_server(iface
, port
, flags
);