factor out leaving the bootloader into own function
[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 444 if (dev->bootloader) {
0ddb3740
MG
445 if (verbose)
446 fprintf(stderr, "HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
447
448 hmcfgusb_leave_bootloader(dev);
449
62f25bae
MG
450 hmcfgusb_close(dev);
451 sleep(1);
452 return 0;
453 }
454
e0a7146e
MG
455 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
456 fprintf(stderr, "Can't add client to pollfd!\n");
457 hmcfgusb_close(dev);
458 return 0;
9db2e455
MG
459 }
460
e0a7146e
MG
461 if (master_socket >= 0) {
462 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
463 fprintf(stderr, "Can't add master_socket to pollfd!\n");
464 hmcfgusb_close(dev);
465 return 0;
466 }
9db2e455
MG
467 }
468
23d96b59
MG
469 memset(out, 0, sizeof(out));
470 out[0] = 'K';
d84111c4 471 wait_for_h = 1;
6262005e 472 hmcfgusb_send_null_frame(dev, 1);
23d96b59 473 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
474
475 while(!quit) {
476 int fd;
477
b55c340d 478 fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */
9db2e455 479 if (fd >= 0) {
e0a7146e
MG
480 if (fd == master_socket) {
481 int client;
482
483 client = accept(master_socket, NULL, 0);
484 if (client >= 0) {
485 shutdown(client, SHUT_RDWR);
486 close(client);
487 }
488 } else {
489 if (hmlan_parse_in(fd, dev) <= 0) {
490 quit = 1;
491 }
492 }
9db2e455
MG
493 } else if (fd == -1) {
494 if (errno) {
1e79d00a
MG
495 if (errno != ETIMEDOUT) {
496 perror("hmcfgusb_poll");
497 quit = 1;
498 } else {
499 /* periodically wakeup the device */
500 hmcfgusb_send_null_frame(dev, 1);
501 if (wait_for_h) {
502 memset(out, 0, sizeof(out));
503 out[0] = 'K';
504 hmcfgusb_send(dev, out, sizeof(out), 1);
505 }
506 }
9db2e455
MG
507 }
508 }
62f25bae
MG
509
510 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
511 if (verbose) {
512 fprintf(stderr, "HM-CFG-USB running since %lu seconds, rebooting now...\n",
513 time(NULL) - dev->opened_at);
514 }
515 hmcfgusb_enter_bootloader(dev);
516 }
9db2e455
MG
517 }
518
813afd12 519 hmcfgusb_close(dev);
e0a7146e
MG
520 return 1;
521}
522
b0bf6ad2
MG
523void sigterm_handler(int sig)
524{
525 if (unlink(PID_FILE) == -1)
526 perror("Can't remove PID file");
527
528 exit(EXIT_SUCCESS);
529}
530
531#define FLAG_DAEMON (1 << 0)
532#define FLAG_PID_FILE (1 << 1)
533
534static int socket_server(char *iface, int port, int flags)
e0a7146e 535{
4371275b 536 struct sigaction sact;
e0a7146e
MG
537 struct sockaddr_in sin;
538 int sock;
539 int n;
627e3f33
MG
540 pid_t pid;
541
b0bf6ad2
MG
542 if (flags & FLAG_DAEMON) {
543 FILE *pidfile = NULL;
544
545 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
546 int fd;
547
548 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
549 if (fd == -1) {
550 if (errno == EEXIST) {
551 pid_t old_pid;
552 pidfile = fopen(PID_FILE, "r");
553 if (!pidfile) {
554 perror("PID file " PID_FILE " already exists, already running?");
555 exit(EXIT_FAILURE);
556 }
557
558 if (fscanf(pidfile, "%u", &old_pid) != 1) {
559 fclose(pidfile);
560 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
561 exit(EXIT_FAILURE);
562 }
563
564 fclose(pidfile);
565
566 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
567 exit(EXIT_FAILURE);
568 }
569 perror("Can't create PID file " PID_FILE);
570 exit(EXIT_FAILURE);
571 }
b0bf6ad2 572
e6ab4631 573 pidfile = fdopen(fd, "w");
b0bf6ad2 574 if (!pidfile) {
e6ab4631 575 perror("Can't reopen PID file fd");
b0bf6ad2
MG
576 exit(EXIT_FAILURE);
577 }
578
579 memset(&sact, 0, sizeof(sact));
580 sact.sa_handler = sigterm_handler;
581
582 if (sigaction(SIGTERM, &sact, NULL) == -1) {
583 perror("sigaction(SIGTERM)");
584 exit(EXIT_FAILURE);
585 }
586 }
587
627e3f33
MG
588 pid = fork();
589 if (pid > 0) {
b0bf6ad2
MG
590 if (pidfile) {
591 fprintf(pidfile, "%u\n", pid);
592 fclose(pidfile);
593 }
594
627e3f33
MG
595 printf("Daemon with PID %u started!\n", pid);
596 exit(EXIT_SUCCESS);
597 } else if (pid < 0) {
598 perror("fork");
599 exit(EXIT_FAILURE);
600 }
b0bf6ad2
MG
601
602 if (pidfile)
603 fclose(pidfile);
627e3f33 604 }
e0a7146e 605
4371275b
MG
606 memset(&sact, 0, sizeof(sact));
607 sact.sa_handler = SIG_IGN;
608
609 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 610 perror("sigaction(SIGPIPE)");
27f4063e 611 exit(EXIT_FAILURE);
4371275b
MG
612 }
613
e0a7146e
MG
614 impersonate_hmlanif = 1;
615
616 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
617 if (sock == -1) {
618 perror("Can't open socket");
619 return EXIT_FAILURE;
620 }
621
622 n = 1;
623 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
624 perror("Can't set socket options");
625 return EXIT_FAILURE;
626 }
627
628 memset(&sin, 0, sizeof(sin));
629 sin.sin_family = AF_INET;
630 sin.sin_port = htons(port);
4a8a2269
MG
631 if (!iface) {
632 sin.sin_addr.s_addr = htonl(INADDR_ANY);
633 } else {
634 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
560c3078 635 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
4a8a2269
MG
636 return EXIT_FAILURE;
637 }
638 }
e0a7146e
MG
639
640 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
641 perror("Can't bind socket");
642 return EXIT_FAILURE;
643 }
644
645 if (listen(sock, 1) == -1) {
646 perror("Can't listen on socket");
647 return EXIT_FAILURE;
648 }
649
650 while(1) {
651 struct sockaddr_in csin;
652 socklen_t csinlen;
653 int client;
627e3f33 654 in_addr_t client_addr;
e0a7146e
MG
655
656 memset(&csin, 0, sizeof(csin));
657 csinlen = sizeof(csin);
658 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
659 if (client == -1) {
660 perror("Couldn't accept client");
661 continue;
662 }
663
627e3f33
MG
664 /* FIXME: getnameinfo... */
665 client_addr = ntohl(csin.sin_addr.s_addr);
666
667 if (verbose) {
fbbbfa37 668 print_timestamp(stdout);
627e3f33
MG
669 printf("Client %d.%d.%d.%d connected!\n",
670 (client_addr & 0xff000000) >> 24,
671 (client_addr & 0x00ff0000) >> 16,
672 (client_addr & 0x0000ff00) >> 8,
673 (client_addr & 0x000000ff));
674 }
e0a7146e 675
23d96b59 676 comm(client, client, sock, flags);
e0a7146e
MG
677
678 shutdown(client, SHUT_RDWR);
679 close(client);
680
627e3f33 681 if (verbose) {
fbbbfa37 682 print_timestamp(stdout);
627e3f33
MG
683 printf("Connection to %d.%d.%d.%d closed!\n",
684 (client_addr & 0xff000000) >> 24,
685 (client_addr & 0x00ff0000) >> 16,
686 (client_addr & 0x0000ff00) >> 8,
687 (client_addr & 0x000000ff));
688 }
e09806b6 689 sleep(1);
e0a7146e
MG
690 }
691
692 return EXIT_SUCCESS;
693}
694
23d96b59 695static int interactive_server(int flags)
e0a7146e 696{
23d96b59 697 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 698 return EXIT_FAILURE;
9db2e455
MG
699
700 return EXIT_SUCCESS;
701}
e0a7146e 702
627e3f33
MG
703void hmlan_syntax(char *prog)
704{
705 fprintf(stderr, "Syntax: %s options\n\n", prog);
706 fprintf(stderr, "Possible options:\n");
707 fprintf(stderr, "\t-D\tdebug mode\n");
708 fprintf(stderr, "\t-d\tdaemon mode\n");
709 fprintf(stderr, "\t-h\tthis help\n");
710 fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
4a8a2269 711 fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
b0bf6ad2 712 fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
0ddb3740 713 fprintf(stderr, "\t-p n\tlisten on port n (default: 1000)\n");
62f25bae 714 fprintf(stderr, "\t-r n\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS);
627e3f33
MG
715 fprintf(stderr, "\t-v\tverbose mode\n");
716
717}
718
e0a7146e
MG
719int main(int argc, char **argv)
720{
627e3f33 721 int port = 1000;
4a8a2269 722 char *iface = NULL;
627e3f33 723 int interactive = 0;
b0bf6ad2 724 int flags = 0;
627e3f33
MG
725 char *ep;
726 int opt;
62f25bae
MG
727
728 reboot_seconds = DEFAULT_REBOOT_SECONDS;
627e3f33 729
62f25bae 730 while((opt = getopt(argc, argv, "DdhiPp:Rr:l:v")) != -1) {
627e3f33
MG
731 switch (opt) {
732 case 'D':
733 debug = 1;
734 verbose = 1;
735 break;
736 case 'd':
b0bf6ad2 737 flags |= FLAG_DAEMON;
627e3f33
MG
738 break;
739 case 'i':
740 interactive = 1;
741 break;
b0bf6ad2
MG
742 case 'P':
743 flags |= FLAG_PID_FILE;
744 break;
627e3f33
MG
745 case 'p':
746 port = strtoul(optarg, &ep, 10);
747 if (*ep != '\0') {
748 fprintf(stderr, "Can't parse port!\n");
749 exit(EXIT_FAILURE);
750 }
751 break;
23d96b59 752 case 'R':
b55c340d 753 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 754 break;
62f25bae
MG
755 case 'r':
756 reboot_seconds = strtoul(optarg, &ep, 10);
757 if (*ep != '\0') {
758 fprintf(stderr, "Can't parse reboot-timeout!\n");
759 exit(EXIT_FAILURE);
760 }
761 break;
4a8a2269
MG
762 case 'l':
763 iface = optarg;
764 break;
627e3f33
MG
765 case 'v':
766 verbose = 1;
767 break;
768 case 'h':
769 case ':':
770 case '?':
771 default:
772 hmlan_syntax(argv[0]);
773 exit(EXIT_FAILURE);
774 break;
775 }
776 }
777
778 if (interactive) {
23d96b59 779 return interactive_server(flags);
627e3f33 780 } else {
b0bf6ad2 781 return socket_server(iface, port, flags);
627e3f33 782 }
e0a7146e 783}
Impressum, Datenschutz