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