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