]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
38e4d6baa499a03536a5bfd95ac0c25bbc1dcbe1
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(char *buf
, int len
, 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
, int 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
);
435 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
439 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
444 static int hmlan_parse_in(int fd
, void *data
)
450 newbuf
= realloc(lan_read_buf
, lan_read_buflen
+ LAN_READ_CHUNK_SIZE
);
455 lan_read_buf
= newbuf
;
456 r
= read(fd
, lan_read_buf
+ lan_read_buflen
, LAN_READ_CHUNK_SIZE
);
458 lan_read_buflen
+= r
;
459 if (lan_read_buflen
> LAN_MAX_BUF_LENGTH
) {
461 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH
, lan_read_buflen
);
464 while(lan_read_buflen
> 0) {
467 for (i
= 0; i
< lan_read_buflen
; i
++) {
468 if ((lan_read_buf
[i
] == '\r') || (lan_read_buf
[i
] == '\n')) {
470 hmlan_parse_one(lan_read_buf
, i
, data
);
471 memmove(lan_read_buf
, lan_read_buf
+ i
+ 1, lan_read_buflen
- (i
+ 1));
472 lan_read_buflen
-= (i
+ 1);
476 if (i
> LAN_MAX_LINE_LENGTH
) {
478 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH
);
484 newbuf
= realloc(lan_read_buf
, lan_read_buflen
);
485 if (lan_read_buflen
&& !newbuf
) {
489 lan_read_buf
= newbuf
;
492 if (errno
!= ECONNRESET
)
502 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
504 struct hmcfgusb_dev
*dev
;
505 uint8_t out
[0x40]; //FIXME!!!
508 hmcfgusb_set_debug(debug
);
510 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
, serial
);
512 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
516 if (dev
->bootloader
) {
518 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
520 hmcfgusb_leave_bootloader(dev
);
527 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
532 tm_s
= localtime(&tm
);
538 tm_s
->tm_hour
= reboot_at_hour
;
539 tm_s
->tm_min
= reboot_at_minute
;
543 reboot_seconds
= tm
- dev
->opened_at
;
545 while (reboot_seconds
<= 0)
546 reboot_seconds
+= 86400;
549 if (verbose
&& reboot_seconds
)
550 printf("Rebooting in %u seconds\n", reboot_seconds
);
553 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
554 fprintf(stderr
, "Can't add client to pollfd!\n");
559 if (master_socket
>= 0) {
560 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
561 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
567 memset(out
, 0, sizeof(out
));
570 hmcfgusb_send_null_frame(dev
, 1);
571 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
576 fd
= hmcfgusb_poll(dev
, POLL_TIMEOUT_MS
);
578 if (fd
== master_socket
) {
581 client
= accept(master_socket
, NULL
, 0);
583 shutdown(client
, SHUT_RDWR
);
587 if (hmlan_parse_in(fd
, dev
) <= 0) {
591 } else if (fd
== -1) {
593 if (errno
!= ETIMEDOUT
) {
594 perror("hmcfgusb_poll");
597 /* periodically wakeup the device */
598 hmcfgusb_send_null_frame(dev
, 1);
600 memset(out
, 0, sizeof(out
));
602 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
608 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
610 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
611 time(NULL
) - dev
->opened_at
);
613 hmcfgusb_enter_bootloader(dev
);
621 void sigterm_handler(int sig
)
623 if (unlink(PID_FILE
) == -1)
624 perror("Can't remove PID file");
629 #define FLAG_DAEMON (1 << 0)
630 #define FLAG_PID_FILE (1 << 1)
632 static int socket_server(char *iface
, int port
, int flags
)
634 struct sigaction sact
;
635 struct sockaddr_in sin
;
640 if (flags
& FLAG_DAEMON
) {
641 FILE *pidfile
= NULL
;
643 if (flags
& FLAG_PID_FILE
) {
646 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
648 if (errno
== EEXIST
) {
650 pidfile
= fopen(PID_FILE
, "r");
652 perror("PID file " PID_FILE
" already exists, already running?");
656 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
658 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
664 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
667 perror("Can't create PID file " PID_FILE
);
671 pidfile
= fdopen(fd
, "w");
673 perror("Can't reopen PID file fd");
677 memset(&sact
, 0, sizeof(sact
));
678 sact
.sa_handler
= sigterm_handler
;
680 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
681 perror("sigaction(SIGTERM)");
689 fprintf(pidfile
, "%u\n", pid
);
693 printf("Daemon with PID %u started!\n", pid
);
695 } else if (pid
< 0) {
704 memset(&sact
, 0, sizeof(sact
));
705 sact
.sa_handler
= SIG_IGN
;
707 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
708 perror("sigaction(SIGPIPE)");
712 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
714 perror("Can't open socket");
719 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
720 perror("Can't set socket options");
724 memset(&sin
, 0, sizeof(sin
));
725 sin
.sin_family
= AF_INET
;
726 sin
.sin_port
= htons(port
);
728 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
730 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
731 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
736 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
737 perror("Can't bind socket");
741 if (listen(sock
, 1) == -1) {
742 perror("Can't listen on socket");
747 struct sockaddr_in csin
;
750 in_addr_t client_addr
;
752 memset(&csin
, 0, sizeof(csin
));
753 csinlen
= sizeof(csin
);
754 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
756 perror("Couldn't accept client");
760 /* FIXME: getnameinfo... */
761 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
763 write_log(NULL
, 0, "Client %d.%d.%d.%d connected!\n",
764 (client_addr
& 0xff000000) >> 24,
765 (client_addr
& 0x00ff0000) >> 16,
766 (client_addr
& 0x0000ff00) >> 8,
767 (client_addr
& 0x000000ff));
769 comm(client
, client
, sock
, flags
);
771 shutdown(client
, SHUT_RDWR
);
779 write_log(NULL
, 0, "Connection to %d.%d.%d.%d closed!\n",
780 (client_addr
& 0xff000000) >> 24,
781 (client_addr
& 0x00ff0000) >> 16,
782 (client_addr
& 0x0000ff00) >> 8,
783 (client_addr
& 0x000000ff));
790 static int interactive_server(int flags
)
792 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
798 void hmlan_syntax(char *prog
)
800 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
801 fprintf(stderr
, "Possible options:\n");
802 fprintf(stderr
, "\t-D\t\tdebug mode\n");
803 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
804 fprintf(stderr
, "\t-h\t\tthis help\n");
805 fprintf(stderr
, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
806 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
807 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
808 fprintf(stderr
, "\t-L logfile\tlog network-communication to logfile\n");
809 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
810 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
811 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
);
812 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
813 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
814 fprintf(stderr
, "\t-v\t\tverbose mode\n");
815 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
819 int main(int argc
, char **argv
)
828 while((opt
= getopt(argc
, argv
, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
835 flags
|= FLAG_DAEMON
;
838 impersonate_hmlanif
= 1;
844 flags
|= FLAG_PID_FILE
;
847 port
= strtoul(optarg
, &ep
, 10);
849 fprintf(stderr
, "Can't parse port!\n");
854 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
857 reboot_seconds
= strtoul(optarg
, &ep
, 10);
860 reboot_at_hour
= reboot_seconds
;
862 reboot_at_minute
= strtoul(ep
, &ep
, 10);
864 fprintf(stderr
, "Can't parse reboot-time!\n");
870 fprintf(stderr
, "Can't parse reboot-timeout!\n");
880 logfile
= fopen(optarg
, "a");
882 perror("fopen(logfile)");
893 printf("hmland " VERSION
"\n");
894 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
900 hmlan_syntax(argv
[0]);
907 return interactive_server(flags
);
909 return socket_server(iface
, port
, flags
);