]>
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> |
e0a7146e MG |
37 | #include <netinet/in.h> |
38 | #include <arpa/inet.h> | |
9db2e455 MG |
39 | #include <libusb-1.0/libusb.h> |
40 | ||
41 | #include "hexdump.h" | |
42 | #include "hmcfgusb.h" | |
43 | ||
b0bf6ad2 MG |
44 | #define PID_FILE "/var/run/hmland.pid" |
45 | ||
627e3f33 MG |
46 | extern char *optarg; |
47 | ||
e0a7146e | 48 | static int impersonate_hmlanif = 0; |
627e3f33 MG |
49 | static int debug = 0; |
50 | static int verbose = 0; | |
e0a7146e | 51 | |
d84111c4 MG |
52 | struct queued_rx { |
53 | char *rx; | |
54 | int len; | |
55 | struct queued_rx *next; | |
56 | }; | |
57 | ||
58 | static struct queued_rx *qrx = NULL; | |
59 | static int wait_for_h = 0; | |
60 | ||
e75295bb MG |
61 | #define FLAG_LENGTH_BYTE (1<<0) |
62 | #define FLAG_FORMAT_HEX (1<<1) | |
63 | #define FLAG_COMMA_BEFORE (1<<2) | |
64 | #define FLAG_COMMA_AFTER (1<<3) | |
65 | #define FLAG_NL (1<<4) | |
66 | #define FLAG_IGNORE_COMMAS (1<<5) | |
67 | ||
68 | #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; } | |
69 | #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; } | |
70 | ||
71 | static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags) | |
72 | { | |
51d4ece6 MG |
73 | const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
74 | 'A', 'B', 'C', 'D', 'E', 'F'}; | |
e75295bb MG |
75 | uint8_t *buf_out = *outpos; |
76 | uint8_t *outend = *outpos + outlen; | |
77 | uint8_t *inend = *inpos + inlen; | |
78 | int i; | |
79 | ||
80 | if (flags & FLAG_COMMA_BEFORE) { | |
81 | CHECK_SPACE(1); | |
82 | **outpos=','; | |
83 | *outpos += 1; | |
84 | } | |
85 | ||
86 | if (flags & FLAG_LENGTH_BYTE) { | |
87 | CHECK_AVAIL(1); | |
88 | len = **inpos; | |
89 | *inpos += 1; | |
90 | } | |
91 | ||
92 | if (flags & FLAG_FORMAT_HEX) { | |
e75295bb MG |
93 | CHECK_AVAIL(len); |
94 | CHECK_SPACE(len*2); | |
95 | for (i = 0; i < len; i++) { | |
51d4ece6 MG |
96 | **outpos = nibble[((**inpos) & 0xf0) >> 4]; |
97 | *outpos += 1; | |
98 | **outpos = nibble[((**inpos) & 0xf)]; | |
99 | *inpos += 1; *outpos += 1; | |
e75295bb MG |
100 | } |
101 | } else { | |
102 | CHECK_AVAIL(len); | |
103 | CHECK_SPACE(len); | |
104 | memcpy(*outpos, *inpos, len); | |
105 | *outpos += len; | |
106 | *inpos += len; | |
107 | } | |
108 | ||
109 | if (flags & FLAG_COMMA_AFTER) { | |
110 | CHECK_SPACE(1); | |
111 | **outpos=','; | |
112 | *outpos += 1; | |
113 | } | |
114 | ||
115 | if (flags & FLAG_NL) { | |
116 | CHECK_SPACE(2); | |
117 | **outpos='\r'; | |
118 | *outpos += 1; | |
119 | **outpos='\n'; | |
120 | *outpos += 1; | |
121 | } | |
122 | ||
123 | return *outpos - buf_out; | |
124 | } | |
125 | ||
51d4ece6 MG |
126 | static uint8_t ascii_to_nibble(uint8_t a) |
127 | { | |
128 | uint8_t c = 0x00; | |
129 | ||
130 | if ((a >= '0') && (a <= '9')) { | |
131 | c = a - '0'; | |
132 | } else if ((a >= 'A') && (a <= 'F')) { | |
133 | c = (a - 'A') + 10; | |
134 | } else if ((a >= 'a') && (a <= 'f')) { | |
135 | c = (a - 'a') + 10; | |
136 | } | |
137 | ||
138 | return c; | |
139 | } | |
140 | ||
e75295bb MG |
141 | static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags) |
142 | { | |
143 | uint8_t *buf_out = *outpos; | |
144 | uint8_t *outend = *outpos + outlen; | |
145 | uint8_t *inend = *inpos + inlen; | |
e75295bb MG |
146 | |
147 | if (flags & FLAG_LENGTH_BYTE) { | |
148 | int len = 0; | |
149 | uint8_t *ip; | |
150 | ||
151 | ip = *inpos; | |
152 | while(ip < inend) { | |
153 | if (*ip == ',') { | |
154 | ip++; | |
155 | if (!(flags & FLAG_IGNORE_COMMAS)) | |
156 | break; | |
157 | ||
158 | continue; | |
159 | } | |
160 | len++; | |
161 | ip++; | |
162 | } | |
163 | CHECK_SPACE(1); | |
164 | **outpos = (len / 2); | |
165 | *outpos += 1; | |
166 | } | |
167 | ||
168 | while(*inpos < inend) { | |
169 | if (**inpos == ',') { | |
170 | *inpos += 1; | |
171 | if (!(flags & FLAG_IGNORE_COMMAS)) | |
172 | break; | |
173 | ||
174 | continue; | |
175 | } | |
176 | ||
177 | CHECK_SPACE(1); | |
178 | CHECK_AVAIL(2); | |
e75295bb | 179 | |
51d4ece6 MG |
180 | **outpos = ascii_to_nibble(**inpos) << 4; |
181 | *inpos += 1; | |
182 | **outpos |= ascii_to_nibble(**inpos); | |
183 | *inpos += 1; *outpos += 1; | |
e75295bb MG |
184 | } |
185 | ||
186 | return *outpos - buf_out; | |
187 | } | |
188 | ||
4371275b | 189 | static int hmlan_format_out(uint8_t *buf, int buf_len, void *data) |
9db2e455 | 190 | { |
e75295bb MG |
191 | uint8_t out[1024]; |
192 | uint8_t *outpos; | |
193 | uint8_t *inpos; | |
e0a7146e | 194 | int fd = *((int*)data); |
4371275b | 195 | int w; |
9db2e455 MG |
196 | |
197 | if (buf_len < 1) | |
4371275b | 198 | return 1; |
9db2e455 | 199 | |
e0a7146e | 200 | memset(out, 0, sizeof(out)); |
e75295bb MG |
201 | outpos = out; |
202 | inpos = buf; | |
e0a7146e | 203 | |
e75295bb | 204 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0); |
9db2e455 MG |
205 | switch(buf[0]) { |
206 | case 'H': | |
e0a7146e MG |
207 | if (impersonate_hmlanif) { |
208 | buf[5] = 'L'; | |
209 | buf[6] = 'A'; | |
210 | buf[7] = 'N'; | |
211 | } | |
e75295bb MG |
212 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE); |
213 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
214 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE); | |
215 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
216 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
217 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
218 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL); | |
9db2e455 MG |
219 | |
220 | break; | |
221 | case 'E': | |
e75295bb MG |
222 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX); |
223 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
224 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
225 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
226 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
227 | 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); | |
228 | ||
9db2e455 MG |
229 | break; |
230 | case 'R': | |
e75295bb MG |
231 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX); |
232 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
233 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
234 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
235 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
236 | 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); | |
237 | ||
9db2e455 MG |
238 | break; |
239 | case 'I': | |
e75295bb MG |
240 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX); |
241 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
242 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
243 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL); | |
244 | ||
245 | break; | |
9db2e455 | 246 | default: |
e75295bb | 247 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL); |
9db2e455 MG |
248 | hexdump(buf, buf_len, "Unknown> "); |
249 | break; | |
250 | } | |
d84111c4 MG |
251 | |
252 | /* Queue packet until first respone to 'K' is received */ | |
253 | if (wait_for_h && buf[0] != 'H') { | |
254 | struct queued_rx **rxp = &qrx; | |
255 | ||
256 | while (*rxp) | |
257 | rxp = &((*rxp)->next); | |
258 | ||
259 | *rxp = malloc(sizeof(struct queued_rx)); | |
260 | if (!*rxp) { | |
261 | perror("malloc"); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | memset(*rxp, 0, sizeof(struct queued_rx)); | |
266 | (*rxp)->len = outpos-out; | |
267 | (*rxp)->rx = malloc((*rxp)->len); | |
268 | if (!(*rxp)->rx) { | |
269 | perror("malloc"); | |
270 | return 0; | |
271 | } | |
272 | memset((*rxp)->rx, 0, (*rxp)->len); | |
273 | memcpy((*rxp)->rx, out, (*rxp)->len); | |
274 | ||
275 | return 1; | |
276 | } | |
277 | ||
62f60cc1 MG |
278 | if (verbose) { |
279 | int i; | |
280 | ||
281 | printf("LAN < "); | |
11806002 | 282 | for (i = 0; i < outpos-out-2; i++) |
62f60cc1 | 283 | printf("%c", out[i]); |
11806002 | 284 | printf("\n"); |
62f60cc1 | 285 | } |
4371275b MG |
286 | |
287 | w = write(fd, out, outpos-out); | |
288 | if (w <= 0) { | |
289 | perror("write"); | |
290 | return 0; | |
291 | } | |
292 | ||
d84111c4 MG |
293 | /* Send al queued packets */ |
294 | if (wait_for_h) { | |
295 | struct queued_rx *curr_rx = qrx; | |
296 | struct queued_rx *last_rx; | |
297 | ||
298 | while (curr_rx) { | |
299 | if (verbose) { | |
300 | int i; | |
301 | ||
302 | printf("LAN < "); | |
303 | for (i = 0; i < curr_rx->len-2; i++) | |
304 | printf("%c", curr_rx->rx[i]); | |
305 | printf("\n"); | |
306 | } | |
307 | ||
308 | w = write(fd, curr_rx->rx, curr_rx->len); | |
309 | if (w <= 0) { | |
310 | perror("write"); | |
311 | return 0; | |
312 | } | |
313 | last_rx = curr_rx; | |
314 | curr_rx = curr_rx->next; | |
315 | ||
316 | free(last_rx->rx); | |
317 | free(last_rx); | |
318 | } | |
319 | ||
320 | qrx = NULL; | |
321 | ||
322 | wait_for_h = 0; | |
323 | } | |
324 | ||
4371275b | 325 | return 1; |
9db2e455 MG |
326 | } |
327 | ||
e0a7146e | 328 | static int hmlan_parse_in(int fd, void *data) |
9db2e455 MG |
329 | { |
330 | struct hmcfgusb_dev *dev = data; | |
627e3f33 | 331 | uint8_t buf[1025]; |
e75295bb MG |
332 | uint8_t out[0x40]; //FIXME!!! |
333 | uint8_t *outpos; | |
334 | uint8_t *inpos; | |
9db2e455 | 335 | int i; |
627e3f33 | 336 | int last; |
9db2e455 MG |
337 | int r; |
338 | ||
627e3f33 MG |
339 | memset(buf, 0, sizeof(buf)); |
340 | ||
341 | r = read(fd, buf, sizeof(buf)-1); | |
9db2e455 | 342 | if (r > 0) { |
627e3f33 MG |
343 | uint8_t *inend = buf + r; |
344 | ||
345 | inpos = buf; | |
346 | ||
627e3f33 MG |
347 | while (inpos < inend) { |
348 | uint8_t *instart = inpos; | |
349 | ||
350 | if ((*inpos == '\r') || (*inpos == '\n')) { | |
351 | inpos++; | |
352 | continue; | |
9db2e455 | 353 | } |
627e3f33 MG |
354 | |
355 | outpos = out; | |
9db2e455 | 356 | |
627e3f33 MG |
357 | last = inend - inpos; |
358 | ||
359 | for (i = 0; i < last; i++) { | |
360 | if ((inpos[i] == '\r') || (inpos[i] == '\n')) { | |
361 | last = i; | |
362 | break; | |
363 | } | |
364 | } | |
365 | ||
366 | if (last == 0) | |
367 | continue; | |
368 | ||
62f60cc1 MG |
369 | if (verbose) { |
370 | printf("LAN > "); | |
371 | for (i = 0; i < last; i++) | |
372 | printf("%c", instart[i]); | |
373 | printf("\n"); | |
374 | } | |
375 | ||
627e3f33 MG |
376 | memset(out, 0, sizeof(out)); |
377 | *outpos++ = *inpos++; | |
378 | ||
03ca736e | 379 | switch(*instart) { |
627e3f33 MG |
380 | case 'S': |
381 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
382 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
383 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
384 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
385 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
386 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE); | |
387 | break; | |
d419ace3 MG |
388 | case 'Y': |
389 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
390 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
391 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE); | |
d26506bf | 392 | break; |
627e3f33 MG |
393 | default: |
394 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS); | |
395 | break; | |
396 | } | |
e75295bb | 397 | |
33df13be | 398 | hmcfgusb_send(dev, out, sizeof(out), 1); |
627e3f33 | 399 | } |
9db2e455 MG |
400 | } else if (r < 0) { |
401 | perror("read"); | |
e0a7146e MG |
402 | return r; |
403 | } else { | |
404 | return 0; | |
9db2e455 | 405 | } |
e0a7146e MG |
406 | |
407 | return 1; | |
9db2e455 MG |
408 | } |
409 | ||
23d96b59 | 410 | static int comm(int fd_in, int fd_out, int master_socket, int flags) |
9db2e455 MG |
411 | { |
412 | struct hmcfgusb_dev *dev; | |
23d96b59 | 413 | uint8_t out[0x40]; //FIXME!!! |
9db2e455 MG |
414 | int quit = 0; |
415 | ||
627e3f33 MG |
416 | hmcfgusb_set_debug(debug); |
417 | ||
e0a7146e | 418 | dev = hmcfgusb_init(hmlan_format_out, &fd_out); |
9db2e455 MG |
419 | if (!dev) { |
420 | fprintf(stderr, "Can't initialize HM-CFG-USB!\n"); | |
e0a7146e MG |
421 | return 0; |
422 | } | |
423 | ||
424 | if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) { | |
425 | fprintf(stderr, "Can't add client to pollfd!\n"); | |
426 | hmcfgusb_close(dev); | |
427 | return 0; | |
9db2e455 MG |
428 | } |
429 | ||
e0a7146e MG |
430 | if (master_socket >= 0) { |
431 | if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) { | |
432 | fprintf(stderr, "Can't add master_socket to pollfd!\n"); | |
433 | hmcfgusb_close(dev); | |
434 | return 0; | |
435 | } | |
9db2e455 MG |
436 | } |
437 | ||
23d96b59 MG |
438 | memset(out, 0, sizeof(out)); |
439 | out[0] = 'K'; | |
d84111c4 | 440 | wait_for_h = 1; |
e09806b6 | 441 | hmcfgusb_send_null_frame(dev); |
23d96b59 | 442 | hmcfgusb_send(dev, out, sizeof(out), 1); |
9db2e455 MG |
443 | |
444 | while(!quit) { | |
445 | int fd; | |
446 | ||
b55c340d | 447 | fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */ |
9db2e455 | 448 | if (fd >= 0) { |
e0a7146e MG |
449 | if (fd == master_socket) { |
450 | int client; | |
451 | ||
452 | client = accept(master_socket, NULL, 0); | |
453 | if (client >= 0) { | |
454 | shutdown(client, SHUT_RDWR); | |
455 | close(client); | |
456 | } | |
457 | } else { | |
458 | if (hmlan_parse_in(fd, dev) <= 0) { | |
459 | quit = 1; | |
460 | } | |
461 | } | |
9db2e455 MG |
462 | } else if (fd == -1) { |
463 | if (errno) { | |
464 | perror("hmcfgusb_poll"); | |
465 | quit = 1; | |
23d96b59 MG |
466 | } else { |
467 | /* periodically wakeup the device */ | |
468 | hmcfgusb_send_null_frame(dev); | |
9db2e455 MG |
469 | } |
470 | } | |
471 | } | |
472 | ||
813afd12 | 473 | hmcfgusb_close(dev); |
e0a7146e MG |
474 | return 1; |
475 | } | |
476 | ||
b0bf6ad2 MG |
477 | void sigterm_handler(int sig) |
478 | { | |
479 | if (unlink(PID_FILE) == -1) | |
480 | perror("Can't remove PID file"); | |
481 | ||
482 | exit(EXIT_SUCCESS); | |
483 | } | |
484 | ||
485 | #define FLAG_DAEMON (1 << 0) | |
486 | #define FLAG_PID_FILE (1 << 1) | |
487 | ||
488 | static int socket_server(char *iface, int port, int flags) | |
e0a7146e | 489 | { |
4371275b | 490 | struct sigaction sact; |
e0a7146e MG |
491 | struct sockaddr_in sin; |
492 | int sock; | |
493 | int n; | |
627e3f33 MG |
494 | pid_t pid; |
495 | ||
b0bf6ad2 MG |
496 | if (flags & FLAG_DAEMON) { |
497 | FILE *pidfile = NULL; | |
498 | ||
499 | if (flags & FLAG_PID_FILE) { | |
e6ab4631 MG |
500 | int fd; |
501 | ||
502 | fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
503 | if (fd == -1) { | |
504 | if (errno == EEXIST) { | |
505 | pid_t old_pid; | |
506 | pidfile = fopen(PID_FILE, "r"); | |
507 | if (!pidfile) { | |
508 | perror("PID file " PID_FILE " already exists, already running?"); | |
509 | exit(EXIT_FAILURE); | |
510 | } | |
511 | ||
512 | if (fscanf(pidfile, "%u", &old_pid) != 1) { | |
513 | fclose(pidfile); | |
514 | fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n"); | |
515 | exit(EXIT_FAILURE); | |
516 | } | |
517 | ||
518 | fclose(pidfile); | |
519 | ||
520 | fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid); | |
521 | exit(EXIT_FAILURE); | |
522 | } | |
523 | perror("Can't create PID file " PID_FILE); | |
524 | exit(EXIT_FAILURE); | |
525 | } | |
b0bf6ad2 | 526 | |
e6ab4631 | 527 | pidfile = fdopen(fd, "w"); |
b0bf6ad2 | 528 | if (!pidfile) { |
e6ab4631 | 529 | perror("Can't reopen PID file fd"); |
b0bf6ad2 MG |
530 | exit(EXIT_FAILURE); |
531 | } | |
532 | ||
533 | memset(&sact, 0, sizeof(sact)); | |
534 | sact.sa_handler = sigterm_handler; | |
535 | ||
536 | if (sigaction(SIGTERM, &sact, NULL) == -1) { | |
537 | perror("sigaction(SIGTERM)"); | |
538 | exit(EXIT_FAILURE); | |
539 | } | |
540 | } | |
541 | ||
627e3f33 MG |
542 | pid = fork(); |
543 | if (pid > 0) { | |
b0bf6ad2 MG |
544 | if (pidfile) { |
545 | fprintf(pidfile, "%u\n", pid); | |
546 | fclose(pidfile); | |
547 | } | |
548 | ||
627e3f33 MG |
549 | printf("Daemon with PID %u started!\n", pid); |
550 | exit(EXIT_SUCCESS); | |
551 | } else if (pid < 0) { | |
552 | perror("fork"); | |
553 | exit(EXIT_FAILURE); | |
554 | } | |
b0bf6ad2 MG |
555 | |
556 | if (pidfile) | |
557 | fclose(pidfile); | |
627e3f33 | 558 | } |
e0a7146e | 559 | |
4371275b MG |
560 | memset(&sact, 0, sizeof(sact)); |
561 | sact.sa_handler = SIG_IGN; | |
562 | ||
563 | if (sigaction(SIGPIPE, &sact, NULL) == -1) { | |
b0bf6ad2 | 564 | perror("sigaction(SIGPIPE)"); |
27f4063e | 565 | exit(EXIT_FAILURE); |
4371275b MG |
566 | } |
567 | ||
e0a7146e MG |
568 | impersonate_hmlanif = 1; |
569 | ||
570 | sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
571 | if (sock == -1) { | |
572 | perror("Can't open socket"); | |
573 | return EXIT_FAILURE; | |
574 | } | |
575 | ||
576 | n = 1; | |
577 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) { | |
578 | perror("Can't set socket options"); | |
579 | return EXIT_FAILURE; | |
580 | } | |
581 | ||
582 | memset(&sin, 0, sizeof(sin)); | |
583 | sin.sin_family = AF_INET; | |
584 | sin.sin_port = htons(port); | |
4a8a2269 MG |
585 | if (!iface) { |
586 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
587 | } else { | |
588 | if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) { | |
589 | perror("inet_ntop"); | |
590 | return EXIT_FAILURE; | |
591 | } | |
592 | } | |
e0a7146e MG |
593 | |
594 | if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | |
595 | perror("Can't bind socket"); | |
596 | return EXIT_FAILURE; | |
597 | } | |
598 | ||
599 | if (listen(sock, 1) == -1) { | |
600 | perror("Can't listen on socket"); | |
601 | return EXIT_FAILURE; | |
602 | } | |
603 | ||
604 | while(1) { | |
605 | struct sockaddr_in csin; | |
606 | socklen_t csinlen; | |
607 | int client; | |
627e3f33 | 608 | in_addr_t client_addr; |
e0a7146e MG |
609 | |
610 | memset(&csin, 0, sizeof(csin)); | |
611 | csinlen = sizeof(csin); | |
612 | client = accept(sock, (struct sockaddr*)&csin, &csinlen); | |
613 | if (client == -1) { | |
614 | perror("Couldn't accept client"); | |
615 | continue; | |
616 | } | |
617 | ||
627e3f33 MG |
618 | /* FIXME: getnameinfo... */ |
619 | client_addr = ntohl(csin.sin_addr.s_addr); | |
620 | ||
621 | if (verbose) { | |
622 | printf("Client %d.%d.%d.%d connected!\n", | |
623 | (client_addr & 0xff000000) >> 24, | |
624 | (client_addr & 0x00ff0000) >> 16, | |
625 | (client_addr & 0x0000ff00) >> 8, | |
626 | (client_addr & 0x000000ff)); | |
627 | } | |
e0a7146e | 628 | |
23d96b59 | 629 | comm(client, client, sock, flags); |
e0a7146e MG |
630 | |
631 | shutdown(client, SHUT_RDWR); | |
632 | close(client); | |
633 | ||
627e3f33 MG |
634 | if (verbose) { |
635 | printf("Connection to %d.%d.%d.%d closed!\n", | |
636 | (client_addr & 0xff000000) >> 24, | |
637 | (client_addr & 0x00ff0000) >> 16, | |
638 | (client_addr & 0x0000ff00) >> 8, | |
639 | (client_addr & 0x000000ff)); | |
640 | } | |
e09806b6 | 641 | sleep(1); |
e0a7146e MG |
642 | } |
643 | ||
644 | return EXIT_SUCCESS; | |
645 | } | |
646 | ||
23d96b59 | 647 | static int interactive_server(int flags) |
e0a7146e | 648 | { |
23d96b59 | 649 | if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags)) |
e0a7146e | 650 | return EXIT_FAILURE; |
9db2e455 MG |
651 | |
652 | return EXIT_SUCCESS; | |
653 | } | |
e0a7146e | 654 | |
627e3f33 MG |
655 | void hmlan_syntax(char *prog) |
656 | { | |
657 | fprintf(stderr, "Syntax: %s options\n\n", prog); | |
658 | fprintf(stderr, "Possible options:\n"); | |
659 | fprintf(stderr, "\t-D\tdebug mode\n"); | |
660 | fprintf(stderr, "\t-d\tdaemon mode\n"); | |
661 | fprintf(stderr, "\t-h\tthis help\n"); | |
662 | fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n"); | |
4a8a2269 | 663 | fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n"); |
b0bf6ad2 | 664 | fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n"); |
627e3f33 MG |
665 | fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n"); |
666 | fprintf(stderr, "\t-v\tverbose mode\n"); | |
667 | ||
668 | } | |
669 | ||
e0a7146e MG |
670 | int main(int argc, char **argv) |
671 | { | |
627e3f33 | 672 | int port = 1000; |
4a8a2269 | 673 | char *iface = NULL; |
627e3f33 | 674 | int interactive = 0; |
b0bf6ad2 | 675 | int flags = 0; |
627e3f33 MG |
676 | char *ep; |
677 | int opt; | |
678 | ||
23d96b59 | 679 | while((opt = getopt(argc, argv, "DdhiPp:Rl:v")) != -1) { |
627e3f33 MG |
680 | switch (opt) { |
681 | case 'D': | |
682 | debug = 1; | |
683 | verbose = 1; | |
684 | break; | |
685 | case 'd': | |
b0bf6ad2 | 686 | flags |= FLAG_DAEMON; |
627e3f33 MG |
687 | break; |
688 | case 'i': | |
689 | interactive = 1; | |
690 | break; | |
b0bf6ad2 MG |
691 | case 'P': |
692 | flags |= FLAG_PID_FILE; | |
693 | break; | |
627e3f33 MG |
694 | case 'p': |
695 | port = strtoul(optarg, &ep, 10); | |
696 | if (*ep != '\0') { | |
697 | fprintf(stderr, "Can't parse port!\n"); | |
698 | exit(EXIT_FAILURE); | |
699 | } | |
700 | break; | |
23d96b59 | 701 | case 'R': |
b55c340d | 702 | fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n"); |
23d96b59 | 703 | break; |
4a8a2269 MG |
704 | case 'l': |
705 | iface = optarg; | |
706 | break; | |
627e3f33 MG |
707 | case 'v': |
708 | verbose = 1; | |
709 | break; | |
710 | case 'h': | |
711 | case ':': | |
712 | case '?': | |
713 | default: | |
714 | hmlan_syntax(argv[0]); | |
715 | exit(EXIT_FAILURE); | |
716 | break; | |
717 | } | |
718 | } | |
719 | ||
720 | if (interactive) { | |
23d96b59 | 721 | return interactive_server(flags); |
627e3f33 | 722 | } else { |
b0bf6ad2 | 723 | return socket_server(iface, port, flags); |
627e3f33 | 724 | } |
e0a7146e | 725 | } |