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