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