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