]>
Commit | Line | Data |
---|---|---|
4732a863 | 1 | /* HM-CFG-LAN emulation for HM-CFG-USB |
9db2e455 | 2 | * |
b229c878 | 3 | * Copyright (c) 2013-15 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 | ||
62f25bae | 50 | #define DEFAULT_REBOOT_SECONDS 86400 |
b229c878 MG |
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 | |
62f25bae | 55 | |
627e3f33 MG |
56 | extern char *optarg; |
57 | ||
e0a7146e | 58 | static int impersonate_hmlanif = 0; |
627e3f33 MG |
59 | static int debug = 0; |
60 | static int verbose = 0; | |
18e63b25 | 61 | static FILE *logfile = NULL; |
62f25bae | 62 | static int reboot_seconds = 0; |
30a2aa7a MG |
63 | static int reboot_at_hour = -1; |
64 | static int reboot_at_minute = -1; | |
c8cafbea | 65 | static int reboot_set = 0; |
b229c878 MG |
66 | static uint8_t *lan_read_buf = NULL; |
67 | static int lan_read_buflen = 0; | |
f51714be | 68 | static char *serial = NULL; |
e0a7146e | 69 | |
d84111c4 MG |
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 | ||
e75295bb MG |
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 | ||
fbbbfa37 MG |
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 | ||
18e63b25 MG |
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 | ||
e75295bb MG |
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) { | |
e75295bb MG |
164 | CHECK_AVAIL(len); |
165 | CHECK_SPACE(len*2); | |
166 | for (i = 0; i < len; i++) { | |
bcbce09d | 167 | **outpos = nibble_to_ascii(((**inpos) & 0xf0) >> 4); |
51d4ece6 | 168 | *outpos += 1; |
bcbce09d | 169 | **outpos = nibble_to_ascii(((**inpos) & 0xf)); |
51d4ece6 | 170 | *inpos += 1; *outpos += 1; |
e75295bb MG |
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; | |
e75295bb MG |
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); | |
e75295bb | 235 | |
51d4ece6 MG |
236 | **outpos = ascii_to_nibble(**inpos) << 4; |
237 | *inpos += 1; | |
238 | **outpos |= ascii_to_nibble(**inpos); | |
239 | *inpos += 1; *outpos += 1; | |
e75295bb MG |
240 | } |
241 | ||
242 | return *outpos - buf_out; | |
243 | } | |
244 | ||
4371275b | 245 | static int hmlan_format_out(uint8_t *buf, int buf_len, void *data) |
9db2e455 | 246 | { |
e75295bb MG |
247 | uint8_t out[1024]; |
248 | uint8_t *outpos; | |
249 | uint8_t *inpos; | |
c8cafbea | 250 | uint16_t version; |
e0a7146e | 251 | int fd = *((int*)data); |
4371275b | 252 | int w; |
9db2e455 MG |
253 | |
254 | if (buf_len < 1) | |
4371275b | 255 | return 1; |
9db2e455 | 256 | |
e0a7146e | 257 | memset(out, 0, sizeof(out)); |
e75295bb MG |
258 | outpos = out; |
259 | inpos = buf; | |
e0a7146e | 260 | |
e75295bb | 261 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0); |
9db2e455 MG |
262 | switch(buf[0]) { |
263 | case 'H': | |
e0a7146e MG |
264 | if (impersonate_hmlanif) { |
265 | buf[5] = 'L'; | |
266 | buf[6] = 'A'; | |
267 | buf[7] = 'N'; | |
268 | } | |
e75295bb | 269 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE); |
c8cafbea MG |
270 | version = inpos[0] << 8; |
271 | version |= inpos[1]; | |
e75295bb MG |
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); | |
dff10a33 MG |
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 | } | |
9db2e455 | 283 | |
c8cafbea MG |
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 | ||
9db2e455 MG |
300 | break; |
301 | case 'E': | |
e75295bb MG |
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 | ||
9db2e455 MG |
309 | break; |
310 | case 'R': | |
e75295bb MG |
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 | ||
9db2e455 MG |
318 | break; |
319 | case 'I': | |
e75295bb MG |
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 | ||
53d7bde2 MG |
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 | ||
e75295bb | 329 | break; |
9db2e455 | 330 | default: |
e75295bb | 331 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL); |
9db2e455 MG |
332 | hexdump(buf, buf_len, "Unknown> "); |
333 | break; | |
334 | } | |
d84111c4 MG |
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 | ||
18e63b25 | 362 | write_log((char*)out, outpos-out-2, "LAN < "); |
4371275b MG |
363 | |
364 | w = write(fd, out, outpos-out); | |
365 | if (w <= 0) { | |
366 | perror("write"); | |
367 | return 0; | |
368 | } | |
369 | ||
59360394 | 370 | /* Send all queued packets */ |
d84111c4 MG |
371 | if (wait_for_h) { |
372 | struct queued_rx *curr_rx = qrx; | |
373 | struct queued_rx *last_rx; | |
374 | ||
375 | while (curr_rx) { | |
18e63b25 | 376 | write_log(curr_rx->rx, curr_rx->len-2, "LAN < "); |
d84111c4 MG |
377 | |
378 | w = write(fd, curr_rx->rx, curr_rx->len); | |
379 | if (w <= 0) { | |
380 | perror("write"); | |
d84111c4 MG |
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 | ||
4371275b | 394 | return 1; |
9db2e455 MG |
395 | } |
396 | ||
b229c878 | 397 | static int hmlan_parse_one(uint8_t *cmd, int last, void *data) |
9db2e455 MG |
398 | { |
399 | struct hmcfgusb_dev *dev = data; | |
e75295bb MG |
400 | uint8_t out[0x40]; //FIXME!!! |
401 | uint8_t *outpos; | |
b229c878 MG |
402 | uint8_t *inpos = cmd; |
403 | ||
404 | outpos = out; | |
9db2e455 | 405 | |
b229c878 MG |
406 | if (last == 0) |
407 | return 1; | |
627e3f33 | 408 | |
b229c878 | 409 | write_log((char*)cmd, last, "LAN > "); |
627e3f33 | 410 | |
b229c878 MG |
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; | |
6b663018 MG |
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); | |
b229c878 MG |
433 | default: |
434 | parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS); | |
435 | break; | |
436 | } | |
627e3f33 | 437 | |
b229c878 | 438 | hmcfgusb_send(dev, out, sizeof(out), 1); |
627e3f33 | 439 | |
b229c878 MG |
440 | return 1; |
441 | } | |
9db2e455 | 442 | |
b229c878 MG |
443 | static int hmlan_parse_in(int fd, void *data) |
444 | { | |
445 | uint8_t *newbuf; | |
446 | int r; | |
447 | int i; | |
627e3f33 | 448 | |
b229c878 MG |
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; | |
627e3f33 MG |
473 | break; |
474 | } | |
b229c878 MG |
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 | } | |
627e3f33 | 480 | } |
b229c878 MG |
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; | |
627e3f33 | 487 | } |
b229c878 | 488 | lan_read_buf = newbuf; |
627e3f33 | 489 | } |
9db2e455 | 490 | } else if (r < 0) { |
fbbbfa37 MG |
491 | if (errno != ECONNRESET) |
492 | perror("read"); | |
e0a7146e MG |
493 | return r; |
494 | } else { | |
495 | return 0; | |
9db2e455 | 496 | } |
e0a7146e MG |
497 | |
498 | return 1; | |
9db2e455 MG |
499 | } |
500 | ||
23d96b59 | 501 | static int comm(int fd_in, int fd_out, int master_socket, int flags) |
9db2e455 MG |
502 | { |
503 | struct hmcfgusb_dev *dev; | |
23d96b59 | 504 | uint8_t out[0x40]; //FIXME!!! |
9db2e455 MG |
505 | int quit = 0; |
506 | ||
627e3f33 MG |
507 | hmcfgusb_set_debug(debug); |
508 | ||
f51714be | 509 | dev = hmcfgusb_init(hmlan_format_out, &fd_out, serial); |
9db2e455 MG |
510 | if (!dev) { |
511 | fprintf(stderr, "Can't initialize HM-CFG-USB!\n"); | |
e0a7146e MG |
512 | return 0; |
513 | } | |
514 | ||
62f25bae | 515 | if (dev->bootloader) { |
0ddb3740 | 516 | if (verbose) |
30a2aa7a | 517 | printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n"); |
0ddb3740 MG |
518 | |
519 | hmcfgusb_leave_bootloader(dev); | |
520 | ||
62f25bae MG |
521 | hmcfgusb_close(dev); |
522 | sleep(1); | |
523 | return 0; | |
524 | } | |
525 | ||
30a2aa7a MG |
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 | ||
30a2aa7a MG |
537 | tm_s->tm_hour = reboot_at_hour; |
538 | tm_s->tm_min = reboot_at_minute; | |
539 | tm_s->tm_sec = 0; | |
540 | ||
ec00d63b | 541 | tm = mktime(tm_s); |
30a2aa7a | 542 | reboot_seconds = tm - dev->opened_at; |
ec00d63b MG |
543 | |
544 | while (reboot_seconds <= 0) | |
545 | reboot_seconds += 86400; | |
30a2aa7a MG |
546 | } |
547 | ||
548 | if (verbose && reboot_seconds) | |
549 | printf("Rebooting in %u seconds\n", reboot_seconds); | |
550 | ||
551 | ||
e0a7146e MG |
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; | |
9db2e455 MG |
556 | } |
557 | ||
e0a7146e MG |
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 | } | |
9db2e455 MG |
564 | } |
565 | ||
23d96b59 MG |
566 | memset(out, 0, sizeof(out)); |
567 | out[0] = 'K'; | |
d84111c4 | 568 | wait_for_h = 1; |
6262005e | 569 | hmcfgusb_send_null_frame(dev, 1); |
23d96b59 | 570 | hmcfgusb_send(dev, out, sizeof(out), 1); |
9db2e455 MG |
571 | |
572 | while(!quit) { | |
573 | int fd; | |
574 | ||
3b35a8c1 | 575 | fd = hmcfgusb_poll(dev, 1000); /* Wakeup device/bus at least once a second */ |
9db2e455 | 576 | if (fd >= 0) { |
e0a7146e MG |
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 | } | |
9db2e455 MG |
590 | } else if (fd == -1) { |
591 | if (errno) { | |
1e79d00a MG |
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 | } | |
9db2e455 MG |
604 | } |
605 | } | |
62f25bae MG |
606 | |
607 | if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) { | |
608 | if (verbose) { | |
30a2aa7a | 609 | printf("HM-CFG-USB running since %lu seconds, rebooting now...\n", |
62f25bae MG |
610 | time(NULL) - dev->opened_at); |
611 | } | |
612 | hmcfgusb_enter_bootloader(dev); | |
613 | } | |
9db2e455 MG |
614 | } |
615 | ||
813afd12 | 616 | hmcfgusb_close(dev); |
e0a7146e MG |
617 | return 1; |
618 | } | |
619 | ||
b0bf6ad2 MG |
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) | |
e0a7146e | 632 | { |
4371275b | 633 | struct sigaction sact; |
e0a7146e MG |
634 | struct sockaddr_in sin; |
635 | int sock; | |
636 | int n; | |
627e3f33 MG |
637 | pid_t pid; |
638 | ||
b0bf6ad2 MG |
639 | if (flags & FLAG_DAEMON) { |
640 | FILE *pidfile = NULL; | |
641 | ||
642 | if (flags & FLAG_PID_FILE) { | |
e6ab4631 MG |
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 | } | |
b0bf6ad2 | 669 | |
e6ab4631 | 670 | pidfile = fdopen(fd, "w"); |
b0bf6ad2 | 671 | if (!pidfile) { |
e6ab4631 | 672 | perror("Can't reopen PID file fd"); |
b0bf6ad2 MG |
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 | ||
627e3f33 MG |
685 | pid = fork(); |
686 | if (pid > 0) { | |
b0bf6ad2 MG |
687 | if (pidfile) { |
688 | fprintf(pidfile, "%u\n", pid); | |
689 | fclose(pidfile); | |
690 | } | |
691 | ||
627e3f33 MG |
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 | } | |
b0bf6ad2 MG |
698 | |
699 | if (pidfile) | |
700 | fclose(pidfile); | |
627e3f33 | 701 | } |
e0a7146e | 702 | |
4371275b MG |
703 | memset(&sact, 0, sizeof(sact)); |
704 | sact.sa_handler = SIG_IGN; | |
705 | ||
706 | if (sigaction(SIGPIPE, &sact, NULL) == -1) { | |
b0bf6ad2 | 707 | perror("sigaction(SIGPIPE)"); |
27f4063e | 708 | exit(EXIT_FAILURE); |
4371275b MG |
709 | } |
710 | ||
e0a7146e MG |
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); | |
4a8a2269 MG |
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) { | |
560c3078 | 730 | fprintf(stderr, "Can't convert IP %s, aborting!\n", iface); |
4a8a2269 MG |
731 | return EXIT_FAILURE; |
732 | } | |
733 | } | |
e0a7146e MG |
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; | |
627e3f33 | 749 | in_addr_t client_addr; |
e0a7146e MG |
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 | ||
627e3f33 MG |
759 | /* FIXME: getnameinfo... */ |
760 | client_addr = ntohl(csin.sin_addr.s_addr); | |
761 | ||
18e63b25 MG |
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)); | |
e0a7146e | 767 | |
23d96b59 | 768 | comm(client, client, sock, flags); |
e0a7146e MG |
769 | |
770 | shutdown(client, SHUT_RDWR); | |
771 | close(client); | |
772 | ||
b229c878 MG |
773 | if (lan_read_buf) |
774 | free(lan_read_buf); | |
775 | lan_read_buf = NULL; | |
776 | lan_read_buflen = 0; | |
777 | ||
18e63b25 MG |
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)); | |
e09806b6 | 783 | sleep(1); |
e0a7146e MG |
784 | } |
785 | ||
786 | return EXIT_SUCCESS; | |
787 | } | |
788 | ||
23d96b59 | 789 | static int interactive_server(int flags) |
e0a7146e | 790 | { |
23d96b59 | 791 | if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags)) |
e0a7146e | 792 | return EXIT_FAILURE; |
9db2e455 MG |
793 | |
794 | return EXIT_SUCCESS; | |
795 | } | |
e0a7146e | 796 | |
627e3f33 MG |
797 | void hmlan_syntax(char *prog) |
798 | { | |
799 | fprintf(stderr, "Syntax: %s options\n\n", prog); | |
800 | fprintf(stderr, "Possible options:\n"); | |
30a2aa7a MG |
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"); | |
dc9f58f0 | 804 | fprintf(stderr, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n"); |
30a2aa7a MG |
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"); | |
18e63b25 | 807 | fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n"); |
30a2aa7a MG |
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"); | |
c8cafbea | 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); |
30a2aa7a | 811 | fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n"); |
f51714be | 812 | fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n"); |
30a2aa7a | 813 | fprintf(stderr, "\t-v\t\tverbose mode\n"); |
c44f15b8 | 814 | fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n"); |
627e3f33 MG |
815 | |
816 | } | |
817 | ||
e0a7146e MG |
818 | int main(int argc, char **argv) |
819 | { | |
627e3f33 | 820 | int port = 1000; |
4a8a2269 | 821 | char *iface = NULL; |
627e3f33 | 822 | int interactive = 0; |
b0bf6ad2 | 823 | int flags = 0; |
627e3f33 MG |
824 | char *ep; |
825 | int opt; | |
62f25bae | 826 | |
f51714be | 827 | while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:S:vV")) != -1) { |
627e3f33 MG |
828 | switch (opt) { |
829 | case 'D': | |
830 | debug = 1; | |
831 | verbose = 1; | |
832 | break; | |
833 | case 'd': | |
b0bf6ad2 | 834 | flags |= FLAG_DAEMON; |
627e3f33 | 835 | break; |
dc9f58f0 MG |
836 | case 'I': |
837 | impersonate_hmlanif = 1; | |
838 | break; | |
627e3f33 MG |
839 | case 'i': |
840 | interactive = 1; | |
841 | break; | |
b0bf6ad2 MG |
842 | case 'P': |
843 | flags |= FLAG_PID_FILE; | |
844 | break; | |
627e3f33 MG |
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; | |
23d96b59 | 852 | case 'R': |
b55c340d | 853 | fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n"); |
23d96b59 | 854 | break; |
62f25bae MG |
855 | case 'r': |
856 | reboot_seconds = strtoul(optarg, &ep, 10); | |
857 | if (*ep != '\0') { | |
30a2aa7a MG |
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 | } | |
62f25bae | 872 | } |
c8cafbea | 873 | reboot_set = 1; |
62f25bae | 874 | break; |
4a8a2269 MG |
875 | case 'l': |
876 | iface = optarg; | |
877 | break; | |
18e63b25 MG |
878 | case 'L': |
879 | logfile = fopen(optarg, "a"); | |
880 | if (!logfile) { | |
881 | perror("fopen(logfile)"); | |
882 | exit(EXIT_FAILURE); | |
883 | } | |
884 | break; | |
f51714be MG |
885 | case 'S': |
886 | serial = optarg; | |
887 | break; | |
627e3f33 MG |
888 | case 'v': |
889 | verbose = 1; | |
890 | break; | |
c44f15b8 MG |
891 | case 'V': |
892 | printf("hmland " VERSION "\n"); | |
dc9f58f0 | 893 | printf("Copyright (c) 2013-15 Michael Gernoth\n\n"); |
c44f15b8 | 894 | exit(EXIT_SUCCESS); |
627e3f33 MG |
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) { | |
23d96b59 | 906 | return interactive_server(flags); |
627e3f33 | 907 | } else { |
b0bf6ad2 | 908 | return socket_server(iface, port, flags); |
627e3f33 | 909 | } |
e0a7146e | 910 | } |