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