]> git.zerfleddert.de Git - hmcfgusb/blame - hmland.c
hmland: add load-byte to 'H'-output
[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);
bf6d309a
MG
292 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
293 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
9db2e455 294
c8cafbea
MG
295 if (!reboot_set) {
296 int new_reboot_seconds;
297
298 if (version < 0x03c7) {
299 new_reboot_seconds = DEFAULT_REBOOT_SECONDS;
300 } else {
301 new_reboot_seconds = 0;
302 }
303
304 if (verbose && new_reboot_seconds && (reboot_seconds != new_reboot_seconds))
305 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
306 new_reboot_seconds, version);
307
308 reboot_seconds = new_reboot_seconds;
309 }
310
9db2e455
MG
311 break;
312 case 'E':
e75295bb
MG
313 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX);
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)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
316 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
317 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
318 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);
319
9db2e455
MG
320 break;
321 case 'R':
e75295bb
MG
322 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
323 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
324 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
325 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
326 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
327 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);
328
9db2e455
MG
329 break;
330 case 'I':
e75295bb
MG
331 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
332 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
333 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
334 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
335
53d7bde2
MG
336 break;
337 case 'G':
338 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_NL);
339
e75295bb 340 break;
9db2e455 341 default:
e75295bb 342 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
9db2e455
MG
343 hexdump(buf, buf_len, "Unknown> ");
344 break;
345 }
d84111c4
MG
346
347 /* Queue packet until first respone to 'K' is received */
348 if (wait_for_h && buf[0] != 'H') {
349 struct queued_rx **rxp = &qrx;
350
351 while (*rxp)
352 rxp = &((*rxp)->next);
353
354 *rxp = malloc(sizeof(struct queued_rx));
355 if (!*rxp) {
356 perror("malloc");
357 return 0;
358 }
359
360 memset(*rxp, 0, sizeof(struct queued_rx));
361 (*rxp)->len = outpos-out;
362 (*rxp)->rx = malloc((*rxp)->len);
363 if (!(*rxp)->rx) {
364 perror("malloc");
365 return 0;
366 }
367 memset((*rxp)->rx, 0, (*rxp)->len);
368 memcpy((*rxp)->rx, out, (*rxp)->len);
369
370 return 1;
371 }
372
18e63b25 373 write_log((char*)out, outpos-out-2, "LAN < ");
4371275b
MG
374
375 w = write(fd, out, outpos-out);
376 if (w <= 0) {
377 perror("write");
378 return 0;
379 }
380
59360394 381 /* Send all queued packets */
d84111c4
MG
382 if (wait_for_h) {
383 struct queued_rx *curr_rx = qrx;
384 struct queued_rx *last_rx;
385
386 while (curr_rx) {
18e63b25 387 write_log(curr_rx->rx, curr_rx->len-2, "LAN < ");
d84111c4
MG
388
389 w = write(fd, curr_rx->rx, curr_rx->len);
390 if (w <= 0) {
391 perror("write");
d84111c4
MG
392 }
393 last_rx = curr_rx;
394 curr_rx = curr_rx->next;
395
396 free(last_rx->rx);
397 free(last_rx);
398 }
399
400 qrx = NULL;
401
402 wait_for_h = 0;
403 }
404
4371275b 405 return 1;
9db2e455
MG
406}
407
b229c878 408static int hmlan_parse_one(uint8_t *cmd, int last, void *data)
9db2e455
MG
409{
410 struct hmcfgusb_dev *dev = data;
e75295bb
MG
411 uint8_t out[0x40]; //FIXME!!!
412 uint8_t *outpos;
b229c878
MG
413 uint8_t *inpos = cmd;
414
415 outpos = out;
9db2e455 416
b229c878
MG
417 if (last == 0)
418 return 1;
627e3f33 419
b229c878 420 write_log((char*)cmd, last, "LAN > ");
627e3f33 421
b229c878
MG
422 memset(out, 0, sizeof(out));
423 *outpos++ = *inpos++;
424
425 switch(*cmd) {
426 case 'S':
427 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
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)), 0);
432 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
433 break;
434 case 'Y':
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)), 0);
437 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
438 break;
439 default:
440 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
441 break;
442 }
627e3f33 443
b229c878 444 hmcfgusb_send(dev, out, sizeof(out), 1);
627e3f33 445
b229c878
MG
446 return 1;
447}
9db2e455 448
b229c878
MG
449static int hmlan_parse_in(int fd, void *data)
450{
451 uint8_t *newbuf;
452 int r;
453 int i;
627e3f33 454
b229c878
MG
455 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
456 if (!newbuf) {
457 perror("realloc");
458 return 0;
459 }
460 lan_read_buf = newbuf;
461 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
462 if (r > 0) {
463 lan_read_buflen += r;
464 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
465 if (verbose)
466 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
467 return -1;
468 }
469 while(lan_read_buflen > 0) {
470 int found = 0;
471
472 for (i = 0; i < lan_read_buflen; i++) {
473 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
474 if (i > 0)
475 hmlan_parse_one(lan_read_buf, i, data);
476 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
477 lan_read_buflen -= (i + 1);
478 found = 1;
627e3f33
MG
479 break;
480 }
b229c878
MG
481 if (i > LAN_MAX_LINE_LENGTH) {
482 if (verbose)
483 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
484 return -1;
485 }
627e3f33 486 }
b229c878
MG
487 if (!found)
488 break;
489 newbuf = realloc(lan_read_buf, lan_read_buflen);
490 if (lan_read_buflen && !newbuf) {
491 perror("realloc");
492 return 0;
627e3f33 493 }
b229c878 494 lan_read_buf = newbuf;
627e3f33 495 }
9db2e455 496 } else if (r < 0) {
fbbbfa37
MG
497 if (errno != ECONNRESET)
498 perror("read");
e0a7146e
MG
499 return r;
500 } else {
501 return 0;
9db2e455 502 }
e0a7146e
MG
503
504 return 1;
9db2e455
MG
505}
506
23d96b59 507static int comm(int fd_in, int fd_out, int master_socket, int flags)
9db2e455
MG
508{
509 struct hmcfgusb_dev *dev;
23d96b59 510 uint8_t out[0x40]; //FIXME!!!
9db2e455
MG
511 int quit = 0;
512
627e3f33
MG
513 hmcfgusb_set_debug(debug);
514
e0a7146e 515 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
9db2e455
MG
516 if (!dev) {
517 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
e0a7146e
MG
518 return 0;
519 }
520
62f25bae 521 if (dev->bootloader) {
0ddb3740 522 if (verbose)
30a2aa7a 523 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
0ddb3740
MG
524
525 hmcfgusb_leave_bootloader(dev);
526
62f25bae
MG
527 hmcfgusb_close(dev);
528 sleep(1);
529 return 0;
530 }
531
30a2aa7a
MG
532 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
533 struct tm *tm_s;
534 time_t tm;
535
536 tm = time(NULL);
537 tm_s = localtime(&tm);
538 if (tm_s == NULL) {
539 perror("localtime");
540 return 0;
541 }
542
30a2aa7a
MG
543 tm_s->tm_hour = reboot_at_hour;
544 tm_s->tm_min = reboot_at_minute;
545 tm_s->tm_sec = 0;
546
ec00d63b 547 tm = mktime(tm_s);
30a2aa7a 548 reboot_seconds = tm - dev->opened_at;
ec00d63b
MG
549
550 while (reboot_seconds <= 0)
551 reboot_seconds += 86400;
30a2aa7a
MG
552 }
553
554 if (verbose && reboot_seconds)
555 printf("Rebooting in %u seconds\n", reboot_seconds);
556
557
e0a7146e
MG
558 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
559 fprintf(stderr, "Can't add client to pollfd!\n");
560 hmcfgusb_close(dev);
561 return 0;
9db2e455
MG
562 }
563
e0a7146e
MG
564 if (master_socket >= 0) {
565 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
566 fprintf(stderr, "Can't add master_socket to pollfd!\n");
567 hmcfgusb_close(dev);
568 return 0;
569 }
9db2e455
MG
570 }
571
23d96b59
MG
572 memset(out, 0, sizeof(out));
573 out[0] = 'K';
d84111c4 574 wait_for_h = 1;
6262005e 575 hmcfgusb_send_null_frame(dev, 1);
23d96b59 576 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
577
578 while(!quit) {
579 int fd;
580
3b35a8c1 581 fd = hmcfgusb_poll(dev, 1000); /* Wakeup device/bus at least once a second */
9db2e455 582 if (fd >= 0) {
e0a7146e
MG
583 if (fd == master_socket) {
584 int client;
585
586 client = accept(master_socket, NULL, 0);
587 if (client >= 0) {
588 shutdown(client, SHUT_RDWR);
589 close(client);
590 }
591 } else {
592 if (hmlan_parse_in(fd, dev) <= 0) {
593 quit = 1;
594 }
595 }
9db2e455
MG
596 } else if (fd == -1) {
597 if (errno) {
1e79d00a
MG
598 if (errno != ETIMEDOUT) {
599 perror("hmcfgusb_poll");
600 quit = 1;
601 } else {
602 /* periodically wakeup the device */
603 hmcfgusb_send_null_frame(dev, 1);
604 if (wait_for_h) {
605 memset(out, 0, sizeof(out));
606 out[0] = 'K';
607 hmcfgusb_send(dev, out, sizeof(out), 1);
608 }
609 }
9db2e455
MG
610 }
611 }
62f25bae
MG
612
613 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
614 if (verbose) {
30a2aa7a 615 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
62f25bae
MG
616 time(NULL) - dev->opened_at);
617 }
618 hmcfgusb_enter_bootloader(dev);
619 }
9db2e455
MG
620 }
621
813afd12 622 hmcfgusb_close(dev);
e0a7146e
MG
623 return 1;
624}
625
b0bf6ad2
MG
626void sigterm_handler(int sig)
627{
628 if (unlink(PID_FILE) == -1)
629 perror("Can't remove PID file");
630
631 exit(EXIT_SUCCESS);
632}
633
634#define FLAG_DAEMON (1 << 0)
635#define FLAG_PID_FILE (1 << 1)
636
637static int socket_server(char *iface, int port, int flags)
e0a7146e 638{
4371275b 639 struct sigaction sact;
e0a7146e
MG
640 struct sockaddr_in sin;
641 int sock;
642 int n;
627e3f33
MG
643 pid_t pid;
644
b0bf6ad2
MG
645 if (flags & FLAG_DAEMON) {
646 FILE *pidfile = NULL;
647
648 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
649 int fd;
650
651 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
652 if (fd == -1) {
653 if (errno == EEXIST) {
654 pid_t old_pid;
655 pidfile = fopen(PID_FILE, "r");
656 if (!pidfile) {
657 perror("PID file " PID_FILE " already exists, already running?");
658 exit(EXIT_FAILURE);
659 }
660
661 if (fscanf(pidfile, "%u", &old_pid) != 1) {
662 fclose(pidfile);
663 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
664 exit(EXIT_FAILURE);
665 }
666
667 fclose(pidfile);
668
669 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
670 exit(EXIT_FAILURE);
671 }
672 perror("Can't create PID file " PID_FILE);
673 exit(EXIT_FAILURE);
674 }
b0bf6ad2 675
e6ab4631 676 pidfile = fdopen(fd, "w");
b0bf6ad2 677 if (!pidfile) {
e6ab4631 678 perror("Can't reopen PID file fd");
b0bf6ad2
MG
679 exit(EXIT_FAILURE);
680 }
681
682 memset(&sact, 0, sizeof(sact));
683 sact.sa_handler = sigterm_handler;
684
685 if (sigaction(SIGTERM, &sact, NULL) == -1) {
686 perror("sigaction(SIGTERM)");
687 exit(EXIT_FAILURE);
688 }
689 }
690
627e3f33
MG
691 pid = fork();
692 if (pid > 0) {
b0bf6ad2
MG
693 if (pidfile) {
694 fprintf(pidfile, "%u\n", pid);
695 fclose(pidfile);
696 }
697
627e3f33
MG
698 printf("Daemon with PID %u started!\n", pid);
699 exit(EXIT_SUCCESS);
700 } else if (pid < 0) {
701 perror("fork");
702 exit(EXIT_FAILURE);
703 }
b0bf6ad2
MG
704
705 if (pidfile)
706 fclose(pidfile);
627e3f33 707 }
e0a7146e 708
4371275b
MG
709 memset(&sact, 0, sizeof(sact));
710 sact.sa_handler = SIG_IGN;
711
712 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 713 perror("sigaction(SIGPIPE)");
27f4063e 714 exit(EXIT_FAILURE);
4371275b
MG
715 }
716
e0a7146e
MG
717 impersonate_hmlanif = 1;
718
719 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
720 if (sock == -1) {
721 perror("Can't open socket");
722 return EXIT_FAILURE;
723 }
724
725 n = 1;
726 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
727 perror("Can't set socket options");
728 return EXIT_FAILURE;
729 }
730
731 memset(&sin, 0, sizeof(sin));
732 sin.sin_family = AF_INET;
733 sin.sin_port = htons(port);
4a8a2269
MG
734 if (!iface) {
735 sin.sin_addr.s_addr = htonl(INADDR_ANY);
736 } else {
737 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
560c3078 738 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
4a8a2269
MG
739 return EXIT_FAILURE;
740 }
741 }
e0a7146e
MG
742
743 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
744 perror("Can't bind socket");
745 return EXIT_FAILURE;
746 }
747
748 if (listen(sock, 1) == -1) {
749 perror("Can't listen on socket");
750 return EXIT_FAILURE;
751 }
752
753 while(1) {
754 struct sockaddr_in csin;
755 socklen_t csinlen;
756 int client;
627e3f33 757 in_addr_t client_addr;
e0a7146e
MG
758
759 memset(&csin, 0, sizeof(csin));
760 csinlen = sizeof(csin);
761 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
762 if (client == -1) {
763 perror("Couldn't accept client");
764 continue;
765 }
766
627e3f33
MG
767 /* FIXME: getnameinfo... */
768 client_addr = ntohl(csin.sin_addr.s_addr);
769
18e63b25
MG
770 write_log(NULL, 0, "Client %d.%d.%d.%d connected!\n",
771 (client_addr & 0xff000000) >> 24,
772 (client_addr & 0x00ff0000) >> 16,
773 (client_addr & 0x0000ff00) >> 8,
774 (client_addr & 0x000000ff));
e0a7146e 775
23d96b59 776 comm(client, client, sock, flags);
e0a7146e
MG
777
778 shutdown(client, SHUT_RDWR);
779 close(client);
780
b229c878
MG
781 if (lan_read_buf)
782 free(lan_read_buf);
783 lan_read_buf = NULL;
784 lan_read_buflen = 0;
785
18e63b25
MG
786 write_log(NULL, 0, "Connection to %d.%d.%d.%d closed!\n",
787 (client_addr & 0xff000000) >> 24,
788 (client_addr & 0x00ff0000) >> 16,
789 (client_addr & 0x0000ff00) >> 8,
790 (client_addr & 0x000000ff));
e09806b6 791 sleep(1);
e0a7146e
MG
792 }
793
794 return EXIT_SUCCESS;
795}
796
23d96b59 797static int interactive_server(int flags)
e0a7146e 798{
23d96b59 799 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 800 return EXIT_FAILURE;
9db2e455
MG
801
802 return EXIT_SUCCESS;
803}
e0a7146e 804
627e3f33
MG
805void hmlan_syntax(char *prog)
806{
807 fprintf(stderr, "Syntax: %s options\n\n", prog);
808 fprintf(stderr, "Possible options:\n");
30a2aa7a
MG
809 fprintf(stderr, "\t-D\t\tdebug mode\n");
810 fprintf(stderr, "\t-d\t\tdaemon mode\n");
811 fprintf(stderr, "\t-h\t\tthis help\n");
812 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
813 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
18e63b25 814 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
30a2aa7a
MG
815 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
816 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
c8cafbea 817 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
818 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
819 fprintf(stderr, "\t-v\t\tverbose mode\n");
c44f15b8 820 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
627e3f33
MG
821
822}
823
e0a7146e
MG
824int main(int argc, char **argv)
825{
627e3f33 826 int port = 1000;
4a8a2269 827 char *iface = NULL;
627e3f33 828 int interactive = 0;
b0bf6ad2 829 int flags = 0;
627e3f33
MG
830 char *ep;
831 int opt;
62f25bae 832
18e63b25 833 while((opt = getopt(argc, argv, "DdhiPp:Rr:l:L:vV")) != -1) {
627e3f33
MG
834 switch (opt) {
835 case 'D':
836 debug = 1;
837 verbose = 1;
838 break;
839 case 'd':
b0bf6ad2 840 flags |= FLAG_DAEMON;
627e3f33
MG
841 break;
842 case 'i':
843 interactive = 1;
844 break;
b0bf6ad2
MG
845 case 'P':
846 flags |= FLAG_PID_FILE;
847 break;
627e3f33
MG
848 case 'p':
849 port = strtoul(optarg, &ep, 10);
850 if (*ep != '\0') {
851 fprintf(stderr, "Can't parse port!\n");
852 exit(EXIT_FAILURE);
853 }
854 break;
23d96b59 855 case 'R':
b55c340d 856 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 857 break;
62f25bae
MG
858 case 'r':
859 reboot_seconds = strtoul(optarg, &ep, 10);
860 if (*ep != '\0') {
30a2aa7a
MG
861 if (*ep == ':') {
862 reboot_at_hour = reboot_seconds;
863 ep++;
864 reboot_at_minute = strtoul(ep, &ep, 10);
865 if (*ep != '\0') {
866 fprintf(stderr, "Can't parse reboot-time!\n");
867 exit(EXIT_FAILURE);
868 }
869
870 reboot_seconds = 0;
871 } else {
872 fprintf(stderr, "Can't parse reboot-timeout!\n");
873 exit(EXIT_FAILURE);
874 }
62f25bae 875 }
c8cafbea 876 reboot_set = 1;
62f25bae 877 break;
4a8a2269
MG
878 case 'l':
879 iface = optarg;
880 break;
18e63b25
MG
881 case 'L':
882 logfile = fopen(optarg, "a");
883 if (!logfile) {
884 perror("fopen(logfile)");
885 exit(EXIT_FAILURE);
886 }
887 break;
627e3f33
MG
888 case 'v':
889 verbose = 1;
890 break;
c44f15b8
MG
891 case 'V':
892 printf("hmland " VERSION "\n");
893 printf("Copyright (c) 2013 Michael Gernoth\n\n");
894 exit(EXIT_SUCCESS);
627e3f33
MG
895 case 'h':
896 case ':':
897 case '?':
898 default:
899 hmlan_syntax(argv[0]);
900 exit(EXIT_FAILURE);
901 break;
902 }
903 }
904
905 if (interactive) {
23d96b59 906 return interactive_server(flags);
627e3f33 907 } else {
b0bf6ad2 908 return socket_server(iface, port, flags);
627e3f33 909 }
e0a7146e 910}
Impressum, Datenschutz