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