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