hm: formatting
[hmcfgusb] / hmland.c
CommitLineData
4732a863 1/* HM-CFG-LAN emulation for HM-CFG-USB
9db2e455 2 *
b229c878 3 * Copyright (c) 2013-15 Michael Gernoth <michael@gernoth.net>
9db2e455
MG
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <stdint.h>
18e63b25 28#include <stdarg.h>
9db2e455
MG
29#include <string.h>
30#include <strings.h>
31#include <poll.h>
4371275b 32#include <signal.h>
9db2e455 33#include <errno.h>
e0a7146e
MG
34#include <sys/types.h>
35#include <sys/socket.h>
b0bf6ad2 36#include <sys/stat.h>
e6ab4631 37#include <fcntl.h>
fbbbfa37 38#include <time.h>
e0a7146e
MG
39#include <netinet/in.h>
40#include <arpa/inet.h>
9db2e455
MG
41#include <libusb-1.0/libusb.h>
42
c44f15b8 43#include "version.h"
9db2e455
MG
44#include "hexdump.h"
45#include "hmcfgusb.h"
bcbce09d 46#include "util.h"
9db2e455 47
b0bf6ad2
MG
48#define PID_FILE "/var/run/hmland.pid"
49
62f25bae 50#define DEFAULT_REBOOT_SECONDS 86400
b229c878
MG
51#define LAN_READ_CHUNK_SIZE 2048
52/* Don't allow remote clients to consume all of our memory */
53#define LAN_MAX_LINE_LENGTH 4096
54#define LAN_MAX_BUF_LENGTH 1048576
62f25bae 55
627e3f33
MG
56extern char *optarg;
57
e0a7146e 58static int impersonate_hmlanif = 0;
627e3f33
MG
59static int debug = 0;
60static int verbose = 0;
18e63b25 61static FILE *logfile = NULL;
62f25bae 62static int reboot_seconds = 0;
30a2aa7a
MG
63static int reboot_at_hour = -1;
64static int reboot_at_minute = -1;
c8cafbea 65static int reboot_set = 0;
b229c878
MG
66static uint8_t *lan_read_buf = NULL;
67static int lan_read_buflen = 0;
e0a7146e 68
d84111c4
MG
69struct queued_rx {
70 char *rx;
71 int len;
72 struct queued_rx *next;
73};
74
75static struct queued_rx *qrx = NULL;
76static int wait_for_h = 0;
77
e75295bb
MG
78#define FLAG_LENGTH_BYTE (1<<0)
79#define FLAG_FORMAT_HEX (1<<1)
80#define FLAG_COMMA_BEFORE (1<<2)
81#define FLAG_COMMA_AFTER (1<<3)
82#define FLAG_NL (1<<4)
83#define FLAG_IGNORE_COMMAS (1<<5)
84
85#define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
86#define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
87
fbbbfa37
MG
88static void print_timestamp(FILE *f)
89{
90 struct timeval tv;
91 struct tm *tmp;
92 char ts[32];
93
94 gettimeofday(&tv, NULL);
95 tmp = localtime(&tv.tv_sec);
96 memset(ts, 0, sizeof(ts));
97 strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
98 fprintf(f, "%s.%06ld: ", ts, tv.tv_usec);
99}
100
18e63b25
MG
101static void write_log(char *buf, int len, char *fmt, ...)
102{
103 va_list ap;
104 int i;
105
106 if ((!logfile) && (!verbose))
107 return;
108
109 if (logfile)
110 print_timestamp(logfile);
111 if (verbose)
112 print_timestamp(stdout);
113
114 if (fmt) {
115 if (logfile) {
116 va_start(ap, fmt);
117 vfprintf(logfile, fmt, ap);
118 va_end(ap);
119 }
120 if (verbose) {
121 va_start(ap, fmt);
122 vprintf(fmt, ap);
123 va_end(ap);
124 }
125 }
126
127 if (buf && len) {
128 for (i = 0; i < len; i++) {
129 if (logfile)
130 fprintf(logfile, "%c", buf[i]);
131 if (verbose)
132 printf("%c", buf[i]);
133 }
134 if (logfile)
135 fprintf(logfile, "\n");
136 if (verbose)
137 printf("\n");
138 }
139 if (logfile)
140 fflush(logfile);
141}
142
e75295bb
MG
143static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags)
144{
145 uint8_t *buf_out = *outpos;
146 uint8_t *outend = *outpos + outlen;
147 uint8_t *inend = *inpos + inlen;
148 int i;
149
150 if (flags & FLAG_COMMA_BEFORE) {
151 CHECK_SPACE(1);
152 **outpos=',';
153 *outpos += 1;
154 }
155
156 if (flags & FLAG_LENGTH_BYTE) {
157 CHECK_AVAIL(1);
158 len = **inpos;
159 *inpos += 1;
160 }
161
162 if (flags & FLAG_FORMAT_HEX) {
e75295bb
MG
163 CHECK_AVAIL(len);
164 CHECK_SPACE(len*2);
165 for (i = 0; i < len; i++) {
bcbce09d 166 **outpos = nibble_to_ascii(((**inpos) & 0xf0) >> 4);
51d4ece6 167 *outpos += 1;
bcbce09d 168 **outpos = nibble_to_ascii(((**inpos) & 0xf));
51d4ece6 169 *inpos += 1; *outpos += 1;
e75295bb
MG
170 }
171 } else {
172 CHECK_AVAIL(len);
173 CHECK_SPACE(len);
174 memcpy(*outpos, *inpos, len);
175 *outpos += len;
176 *inpos += len;
177 }
178
179 if (flags & FLAG_COMMA_AFTER) {
180 CHECK_SPACE(1);
181 **outpos=',';
182 *outpos += 1;
183 }
184
185 if (flags & FLAG_NL) {
186 CHECK_SPACE(2);
187 **outpos='\r';
188 *outpos += 1;
189 **outpos='\n';
190 *outpos += 1;
191 }
192
193 return *outpos - buf_out;
194}
195
196static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags)
197{
198 uint8_t *buf_out = *outpos;
199 uint8_t *outend = *outpos + outlen;
200 uint8_t *inend = *inpos + inlen;
e75295bb
MG
201
202 if (flags & FLAG_LENGTH_BYTE) {
203 int len = 0;
204 uint8_t *ip;
205
206 ip = *inpos;
207 while(ip < inend) {
208 if (*ip == ',') {
209 ip++;
210 if (!(flags & FLAG_IGNORE_COMMAS))
211 break;
212
213 continue;
214 }
215 len++;
216 ip++;
217 }
218 CHECK_SPACE(1);
219 **outpos = (len / 2);
220 *outpos += 1;
221 }
222
223 while(*inpos < inend) {
224 if (**inpos == ',') {
225 *inpos += 1;
226 if (!(flags & FLAG_IGNORE_COMMAS))
227 break;
228
229 continue;
230 }
231
232 CHECK_SPACE(1);
233 CHECK_AVAIL(2);
e75295bb 234
51d4ece6
MG
235 **outpos = ascii_to_nibble(**inpos) << 4;
236 *inpos += 1;
237 **outpos |= ascii_to_nibble(**inpos);
238 *inpos += 1; *outpos += 1;
e75295bb
MG
239 }
240
241 return *outpos - buf_out;
242}
243
4371275b 244static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
9db2e455 245{
e75295bb
MG
246 uint8_t out[1024];
247 uint8_t *outpos;
248 uint8_t *inpos;
c8cafbea 249 uint16_t version;
e0a7146e 250 int fd = *((int*)data);
4371275b 251 int w;
9db2e455
MG
252
253 if (buf_len < 1)
4371275b 254 return 1;
9db2e455 255
e0a7146e 256 memset(out, 0, sizeof(out));
e75295bb
MG
257 outpos = out;
258 inpos = buf;
e0a7146e 259
e75295bb 260 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
9db2e455
MG
261 switch(buf[0]) {
262 case 'H':
e0a7146e
MG
263 if (impersonate_hmlanif) {
264 buf[5] = 'L';
265 buf[6] = 'A';
266 buf[7] = 'N';
267 }
e75295bb 268 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE);
c8cafbea
MG
269 version = inpos[0] << 8;
270 version |= inpos[1];
e75295bb
MG
271 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
272 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE);
273 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
274 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
275 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
dff10a33
MG
276 if (version < 0x03c7) {
277 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
278 } else {
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)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
281 }
9db2e455 282
c8cafbea
MG
283 if (!reboot_set) {
284 int new_reboot_seconds;
285
286 if (version < 0x03c7) {
287 new_reboot_seconds = DEFAULT_REBOOT_SECONDS;
288 } else {
289 new_reboot_seconds = 0;
290 }
291
292 if (verbose && new_reboot_seconds && (reboot_seconds != new_reboot_seconds))
293 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
294 new_reboot_seconds, version);
295
296 reboot_seconds = new_reboot_seconds;
297 }
298
9db2e455
MG
299 break;
300 case 'E':
e75295bb
MG
301 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX);
302 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
303 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
304 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
305 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
306 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);
307
9db2e455
MG
308 break;
309 case 'R':
e75295bb
MG
310 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
311 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
312 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
313 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
314 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
315 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);
316
9db2e455
MG
317 break;
318 case 'I':
e75295bb
MG
319 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
320 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 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)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
323
53d7bde2
MG
324 break;
325 case 'G':
326 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_NL);
327
e75295bb 328 break;
9db2e455 329 default:
e75295bb 330 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
9db2e455
MG
331 hexdump(buf, buf_len, "Unknown> ");
332 break;
333 }
d84111c4
MG
334
335 /* Queue packet until first respone to 'K' is received */
336 if (wait_for_h && buf[0] != 'H') {
337 struct queued_rx **rxp = &qrx;
338
339 while (*rxp)
340 rxp = &((*rxp)->next);
341
342 *rxp = malloc(sizeof(struct queued_rx));
343 if (!*rxp) {
344 perror("malloc");
345 return 0;
346 }
347
348 memset(*rxp, 0, sizeof(struct queued_rx));
349 (*rxp)->len = outpos-out;
350 (*rxp)->rx = malloc((*rxp)->len);
351 if (!(*rxp)->rx) {
352 perror("malloc");
353 return 0;
354 }
355 memset((*rxp)->rx, 0, (*rxp)->len);
356 memcpy((*rxp)->rx, out, (*rxp)->len);
357
358 return 1;
359 }
360
18e63b25 361 write_log((char*)out, outpos-out-2, "LAN < ");
4371275b
MG
362
363 w = write(fd, out, outpos-out);
364 if (w <= 0) {
365 perror("write");
366 return 0;
367 }
368
59360394 369 /* Send all queued packets */
d84111c4
MG
370 if (wait_for_h) {
371 struct queued_rx *curr_rx = qrx;
372 struct queued_rx *last_rx;
373
374 while (curr_rx) {
18e63b25 375 write_log(curr_rx->rx, curr_rx->len-2, "LAN < ");
d84111c4
MG
376
377 w = write(fd, curr_rx->rx, curr_rx->len);
378 if (w <= 0) {
379 perror("write");
d84111c4
MG
380 }
381 last_rx = curr_rx;
382 curr_rx = curr_rx->next;
383
384 free(last_rx->rx);
385 free(last_rx);
386 }
387
388 qrx = NULL;
389
390 wait_for_h = 0;
391 }
392
4371275b 393 return 1;
9db2e455
MG
394}
395
b229c878 396static int hmlan_parse_one(uint8_t *cmd, int last, void *data)
9db2e455
MG
397{
398 struct hmcfgusb_dev *dev = data;
e75295bb
MG
399 uint8_t out[0x40]; //FIXME!!!
400 uint8_t *outpos;
b229c878
MG
401 uint8_t *inpos = cmd;
402
403 outpos = out;
9db2e455 404
b229c878
MG
405 if (last == 0)
406 return 1;
627e3f33 407
b229c878 408 write_log((char*)cmd, last, "LAN > ");
627e3f33 409
b229c878
MG
410 memset(out, 0, sizeof(out));
411 *outpos++ = *inpos++;
412
413 switch(*cmd) {
414 case 'S':
415 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
416 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), FLAG_LENGTH_BYTE);
421 break;
422 case 'Y':
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)), FLAG_LENGTH_BYTE);
426 break;
6b663018
MG
427 case '+':
428 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
429 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), FLAG_LENGTH_BYTE);
b229c878
MG
432 default:
433 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
434 break;
435 }
627e3f33 436
b229c878 437 hmcfgusb_send(dev, out, sizeof(out), 1);
627e3f33 438
b229c878
MG
439 return 1;
440}
9db2e455 441
b229c878
MG
442static int hmlan_parse_in(int fd, void *data)
443{
444 uint8_t *newbuf;
445 int r;
446 int i;
627e3f33 447
b229c878
MG
448 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
449 if (!newbuf) {
450 perror("realloc");
451 return 0;
452 }
453 lan_read_buf = newbuf;
454 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
455 if (r > 0) {
456 lan_read_buflen += r;
457 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
458 if (verbose)
459 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
460 return -1;
461 }
462 while(lan_read_buflen > 0) {
463 int found = 0;
464
465 for (i = 0; i < lan_read_buflen; i++) {
466 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
467 if (i > 0)
468 hmlan_parse_one(lan_read_buf, i, data);
469 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
470 lan_read_buflen -= (i + 1);
471 found = 1;
627e3f33
MG
472 break;
473 }
b229c878
MG
474 if (i > LAN_MAX_LINE_LENGTH) {
475 if (verbose)
476 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
477 return -1;
478 }
627e3f33 479 }
b229c878
MG
480 if (!found)
481 break;
482 newbuf = realloc(lan_read_buf, lan_read_buflen);
483 if (lan_read_buflen && !newbuf) {
484 perror("realloc");
485 return 0;
627e3f33 486 }
b229c878 487 lan_read_buf = newbuf;
627e3f33 488 }
9db2e455 489 } else if (r < 0) {
fbbbfa37
MG
490 if (errno != ECONNRESET)
491 perror("read");
e0a7146e
MG
492 return r;
493 } else {
494 return 0;
9db2e455 495 }
e0a7146e
MG
496
497 return 1;
9db2e455
MG
498}
499
23d96b59 500static int comm(int fd_in, int fd_out, int master_socket, int flags)
9db2e455
MG
501{
502 struct hmcfgusb_dev *dev;
23d96b59 503 uint8_t out[0x40]; //FIXME!!!
9db2e455
MG
504 int quit = 0;
505
627e3f33
MG
506 hmcfgusb_set_debug(debug);
507
e0a7146e 508 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
9db2e455
MG
509 if (!dev) {
510 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
e0a7146e
MG
511 return 0;
512 }
513
62f25bae 514 if (dev->bootloader) {
0ddb3740 515 if (verbose)
30a2aa7a 516 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
0ddb3740
MG
517
518 hmcfgusb_leave_bootloader(dev);
519
62f25bae
MG
520 hmcfgusb_close(dev);
521 sleep(1);
522 return 0;
523 }
524
30a2aa7a
MG
525 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
526 struct tm *tm_s;
527 time_t tm;
528
529 tm = time(NULL);
530 tm_s = localtime(&tm);
531 if (tm_s == NULL) {
532 perror("localtime");
533 return 0;
534 }
535
30a2aa7a
MG
536 tm_s->tm_hour = reboot_at_hour;
537 tm_s->tm_min = reboot_at_minute;
538 tm_s->tm_sec = 0;
539
ec00d63b 540 tm = mktime(tm_s);
30a2aa7a 541 reboot_seconds = tm - dev->opened_at;
ec00d63b
MG
542
543 while (reboot_seconds <= 0)
544 reboot_seconds += 86400;
30a2aa7a
MG
545 }
546
547 if (verbose && reboot_seconds)
548 printf("Rebooting in %u seconds\n", reboot_seconds);
549
550
e0a7146e
MG
551 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
552 fprintf(stderr, "Can't add client to pollfd!\n");
553 hmcfgusb_close(dev);
554 return 0;
9db2e455
MG
555 }
556
e0a7146e
MG
557 if (master_socket >= 0) {
558 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
559 fprintf(stderr, "Can't add master_socket to pollfd!\n");
560 hmcfgusb_close(dev);
561 return 0;
562 }
9db2e455
MG
563 }
564
23d96b59
MG
565 memset(out, 0, sizeof(out));
566 out[0] = 'K';
d84111c4 567 wait_for_h = 1;
6262005e 568 hmcfgusb_send_null_frame(dev, 1);
23d96b59 569 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
570
571 while(!quit) {
572 int fd;
573
3b35a8c1 574 fd = hmcfgusb_poll(dev, 1000); /* Wakeup device/bus at least once a second */
9db2e455 575 if (fd >= 0) {
e0a7146e
MG
576 if (fd == master_socket) {
577 int client;
578
579 client = accept(master_socket, NULL, 0);
580 if (client >= 0) {
581 shutdown(client, SHUT_RDWR);
582 close(client);
583 }
584 } else {
585 if (hmlan_parse_in(fd, dev) <= 0) {
586 quit = 1;
587 }
588 }
9db2e455
MG
589 } else if (fd == -1) {
590 if (errno) {
1e79d00a
MG
591 if (errno != ETIMEDOUT) {
592 perror("hmcfgusb_poll");
593 quit = 1;
594 } else {
595 /* periodically wakeup the device */
596 hmcfgusb_send_null_frame(dev, 1);
597 if (wait_for_h) {
598 memset(out, 0, sizeof(out));
599 out[0] = 'K';
600 hmcfgusb_send(dev, out, sizeof(out), 1);
601 }
602 }
9db2e455
MG
603 }
604 }
62f25bae
MG
605
606 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
607 if (verbose) {
30a2aa7a 608 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
62f25bae
MG
609 time(NULL) - dev->opened_at);
610 }
611 hmcfgusb_enter_bootloader(dev);
612 }
9db2e455
MG
613 }
614
813afd12 615 hmcfgusb_close(dev);
e0a7146e
MG
616 return 1;
617}
618
b0bf6ad2
MG
619void sigterm_handler(int sig)
620{
621 if (unlink(PID_FILE) == -1)
622 perror("Can't remove PID file");
623
624 exit(EXIT_SUCCESS);
625}
626
627#define FLAG_DAEMON (1 << 0)
628#define FLAG_PID_FILE (1 << 1)
629
630static int socket_server(char *iface, int port, int flags)
e0a7146e 631{
4371275b 632 struct sigaction sact;
e0a7146e
MG
633 struct sockaddr_in sin;
634 int sock;
635 int n;
627e3f33
MG
636 pid_t pid;
637
b0bf6ad2
MG
638 if (flags & FLAG_DAEMON) {
639 FILE *pidfile = NULL;
640
641 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
642 int fd;
643
644 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
645 if (fd == -1) {
646 if (errno == EEXIST) {
647 pid_t old_pid;
648 pidfile = fopen(PID_FILE, "r");
649 if (!pidfile) {
650 perror("PID file " PID_FILE " already exists, already running?");
651 exit(EXIT_FAILURE);
652 }
653
654 if (fscanf(pidfile, "%u", &old_pid) != 1) {
655 fclose(pidfile);
656 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
657 exit(EXIT_FAILURE);
658 }
659
660 fclose(pidfile);
661
662 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
663 exit(EXIT_FAILURE);
664 }
665 perror("Can't create PID file " PID_FILE);
666 exit(EXIT_FAILURE);
667 }
b0bf6ad2 668
e6ab4631 669 pidfile = fdopen(fd, "w");
b0bf6ad2 670 if (!pidfile) {
e6ab4631 671 perror("Can't reopen PID file fd");
b0bf6ad2
MG
672 exit(EXIT_FAILURE);
673 }
674
675 memset(&sact, 0, sizeof(sact));
676 sact.sa_handler = sigterm_handler;
677
678 if (sigaction(SIGTERM, &sact, NULL) == -1) {
679 perror("sigaction(SIGTERM)");
680 exit(EXIT_FAILURE);
681 }
682 }
683
627e3f33
MG
684 pid = fork();
685 if (pid > 0) {
b0bf6ad2
MG
686 if (pidfile) {
687 fprintf(pidfile, "%u\n", pid);
688 fclose(pidfile);
689 }
690
627e3f33
MG
691 printf("Daemon with PID %u started!\n", pid);
692 exit(EXIT_SUCCESS);
693 } else if (pid < 0) {
694 perror("fork");
695 exit(EXIT_FAILURE);
696 }
b0bf6ad2
MG
697
698 if (pidfile)
699 fclose(pidfile);
627e3f33 700 }
e0a7146e 701
4371275b
MG
702 memset(&sact, 0, sizeof(sact));
703 sact.sa_handler = SIG_IGN;
704
705 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 706 perror("sigaction(SIGPIPE)");
27f4063e 707 exit(EXIT_FAILURE);
4371275b
MG
708 }
709
e0a7146e
MG
710 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
711 if (sock == -1) {
712 perror("Can't open socket");
713 return EXIT_FAILURE;
714 }
715
716 n = 1;
717 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
718 perror("Can't set socket options");
719 return EXIT_FAILURE;
720 }
721
722 memset(&sin, 0, sizeof(sin));
723 sin.sin_family = AF_INET;
724 sin.sin_port = htons(port);
4a8a2269
MG
725 if (!iface) {
726 sin.sin_addr.s_addr = htonl(INADDR_ANY);
727 } else {
728 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
560c3078 729 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
4a8a2269
MG
730 return EXIT_FAILURE;
731 }
732 }
e0a7146e
MG
733
734 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
735 perror("Can't bind socket");
736 return EXIT_FAILURE;
737 }
738
739 if (listen(sock, 1) == -1) {
740 perror("Can't listen on socket");
741 return EXIT_FAILURE;
742 }
743
744 while(1) {
745 struct sockaddr_in csin;
746 socklen_t csinlen;
747 int client;
627e3f33 748 in_addr_t client_addr;
e0a7146e
MG
749
750 memset(&csin, 0, sizeof(csin));
751 csinlen = sizeof(csin);
752 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
753 if (client == -1) {
754 perror("Couldn't accept client");
755 continue;
756 }
757
627e3f33
MG
758 /* FIXME: getnameinfo... */
759 client_addr = ntohl(csin.sin_addr.s_addr);
760
18e63b25
MG
761 write_log(NULL, 0, "Client %d.%d.%d.%d connected!\n",
762 (client_addr & 0xff000000) >> 24,
763 (client_addr & 0x00ff0000) >> 16,
764 (client_addr & 0x0000ff00) >> 8,
765 (client_addr & 0x000000ff));
e0a7146e 766
23d96b59 767 comm(client, client, sock, flags);
e0a7146e
MG
768
769 shutdown(client, SHUT_RDWR);
770 close(client);
771
b229c878
MG
772 if (lan_read_buf)
773 free(lan_read_buf);
774 lan_read_buf = NULL;
775 lan_read_buflen = 0;
776
18e63b25
MG
777 write_log(NULL, 0, "Connection to %d.%d.%d.%d closed!\n",
778 (client_addr & 0xff000000) >> 24,
779 (client_addr & 0x00ff0000) >> 16,
780 (client_addr & 0x0000ff00) >> 8,
781 (client_addr & 0x000000ff));
e09806b6 782 sleep(1);
e0a7146e
MG
783 }
784
785 return EXIT_SUCCESS;
786}
787
23d96b59 788static int interactive_server(int flags)
e0a7146e 789{
23d96b59 790 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 791 return EXIT_FAILURE;
9db2e455
MG
792
793 return EXIT_SUCCESS;
794}
e0a7146e 795
627e3f33
MG
796void hmlan_syntax(char *prog)
797{
798 fprintf(stderr, "Syntax: %s options\n\n", prog);
799 fprintf(stderr, "Possible options:\n");
30a2aa7a
MG
800 fprintf(stderr, "\t-D\t\tdebug mode\n");
801 fprintf(stderr, "\t-d\t\tdaemon mode\n");
802 fprintf(stderr, "\t-h\t\tthis help\n");
dc9f58f0 803 fprintf(stderr, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
30a2aa7a
MG
804 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
805 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
18e63b25 806 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
30a2aa7a
MG
807 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
808 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
c8cafbea 809 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);
30a2aa7a
MG
810 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
811 fprintf(stderr, "\t-v\t\tverbose mode\n");
c44f15b8 812 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
627e3f33
MG
813
814}
815
e0a7146e
MG
816int main(int argc, char **argv)
817{
627e3f33 818 int port = 1000;
4a8a2269 819 char *iface = NULL;
627e3f33 820 int interactive = 0;
b0bf6ad2 821 int flags = 0;
627e3f33
MG
822 char *ep;
823 int opt;
62f25bae 824
dc9f58f0 825 while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:vV")) != -1) {
627e3f33
MG
826 switch (opt) {
827 case 'D':
828 debug = 1;
829 verbose = 1;
830 break;
831 case 'd':
b0bf6ad2 832 flags |= FLAG_DAEMON;
627e3f33 833 break;
dc9f58f0
MG
834 case 'I':
835 impersonate_hmlanif = 1;
836 break;
627e3f33
MG
837 case 'i':
838 interactive = 1;
839 break;
b0bf6ad2
MG
840 case 'P':
841 flags |= FLAG_PID_FILE;
842 break;
627e3f33
MG
843 case 'p':
844 port = strtoul(optarg, &ep, 10);
845 if (*ep != '\0') {
846 fprintf(stderr, "Can't parse port!\n");
847 exit(EXIT_FAILURE);
848 }
849 break;
23d96b59 850 case 'R':
b55c340d 851 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 852 break;
62f25bae
MG
853 case 'r':
854 reboot_seconds = strtoul(optarg, &ep, 10);
855 if (*ep != '\0') {
30a2aa7a
MG
856 if (*ep == ':') {
857 reboot_at_hour = reboot_seconds;
858 ep++;
859 reboot_at_minute = strtoul(ep, &ep, 10);
860 if (*ep != '\0') {
861 fprintf(stderr, "Can't parse reboot-time!\n");
862 exit(EXIT_FAILURE);
863 }
864
865 reboot_seconds = 0;
866 } else {
867 fprintf(stderr, "Can't parse reboot-timeout!\n");
868 exit(EXIT_FAILURE);
869 }
62f25bae 870 }
c8cafbea 871 reboot_set = 1;
62f25bae 872 break;
4a8a2269
MG
873 case 'l':
874 iface = optarg;
875 break;
18e63b25
MG
876 case 'L':
877 logfile = fopen(optarg, "a");
878 if (!logfile) {
879 perror("fopen(logfile)");
880 exit(EXIT_FAILURE);
881 }
882 break;
627e3f33
MG
883 case 'v':
884 verbose = 1;
885 break;
c44f15b8
MG
886 case 'V':
887 printf("hmland " VERSION "\n");
dc9f58f0 888 printf("Copyright (c) 2013-15 Michael Gernoth\n\n");
c44f15b8 889 exit(EXIT_SUCCESS);
627e3f33
MG
890 case 'h':
891 case ':':
892 case '?':
893 default:
894 hmlan_syntax(argv[0]);
895 exit(EXIT_FAILURE);
896 break;
897 }
898 }
899
900 if (interactive) {
23d96b59 901 return interactive_server(flags);
627e3f33 902 } else {
b0bf6ad2 903 return socket_server(iface, port, flags);
627e3f33 904 }
e0a7146e 905}
Impressum, Datenschutz