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