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