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