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