]>
Commit | Line | Data |
---|---|---|
1 | /* HM-CFG-LAN emulation for HM-CFG-USB | |
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> | |
31 | #include <signal.h> | |
32 | #include <errno.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/socket.h> | |
35 | #include <sys/stat.h> | |
36 | #include <fcntl.h> | |
37 | #include <netinet/in.h> | |
38 | #include <arpa/inet.h> | |
39 | #include <libusb-1.0/libusb.h> | |
40 | ||
41 | #include "hexdump.h" | |
42 | #include "hmcfgusb.h" | |
43 | ||
44 | #define PID_FILE "/var/run/hmland.pid" | |
45 | ||
46 | extern char *optarg; | |
47 | ||
48 | static int impersonate_hmlanif = 0; | |
49 | static int debug = 0; | |
50 | static int verbose = 0; | |
51 | ||
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 | { | |
64 | const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
65 | 'A', 'B', 'C', 'D', 'E', 'F'}; | |
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) { | |
84 | CHECK_AVAIL(len); | |
85 | CHECK_SPACE(len*2); | |
86 | for (i = 0; i < len; i++) { | |
87 | **outpos = nibble[((**inpos) & 0xf0) >> 4]; | |
88 | *outpos += 1; | |
89 | **outpos = nibble[((**inpos) & 0xf)]; | |
90 | *inpos += 1; *outpos += 1; | |
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 | ||
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 | ||
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; | |
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); | |
170 | ||
171 | **outpos = ascii_to_nibble(**inpos) << 4; | |
172 | *inpos += 1; | |
173 | **outpos |= ascii_to_nibble(**inpos); | |
174 | *inpos += 1; *outpos += 1; | |
175 | } | |
176 | ||
177 | return *outpos - buf_out; | |
178 | } | |
179 | ||
180 | static int hmlan_format_out(uint8_t *buf, int buf_len, void *data) | |
181 | { | |
182 | uint8_t out[1024]; | |
183 | uint8_t *outpos; | |
184 | uint8_t *inpos; | |
185 | int fd = *((int*)data); | |
186 | int w; | |
187 | ||
188 | if (buf_len < 1) | |
189 | return 1; | |
190 | ||
191 | memset(out, 0, sizeof(out)); | |
192 | outpos = out; | |
193 | inpos = buf; | |
194 | ||
195 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0); | |
196 | switch(buf[0]) { | |
197 | case 'H': | |
198 | if (impersonate_hmlanif) { | |
199 | buf[5] = 'L'; | |
200 | buf[6] = 'A'; | |
201 | buf[7] = 'N'; | |
202 | } | |
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); | |
210 | ||
211 | break; | |
212 | case 'E': | |
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 | ||
220 | break; | |
221 | case 'R': | |
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 | ||
229 | break; | |
230 | case 'I': | |
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; | |
237 | default: | |
238 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL); | |
239 | hexdump(buf, buf_len, "Unknown> "); | |
240 | break; | |
241 | } | |
242 | if (verbose) { | |
243 | int i; | |
244 | ||
245 | printf("LAN < "); | |
246 | for (i = 0; i < outpos-out-2; i++) | |
247 | printf("%c", out[i]); | |
248 | printf("\n"); | |
249 | } | |
250 | ||
251 | w = write(fd, out, outpos-out); | |
252 | if (w <= 0) { | |
253 | perror("write"); | |
254 | return 0; | |
255 | } | |
256 | ||
257 | return 1; | |
258 | } | |
259 | ||
260 | static int hmlan_parse_in(int fd, void *data) | |
261 | { | |
262 | struct hmcfgusb_dev *dev = data; | |
263 | uint8_t buf[1025]; | |
264 | uint8_t out[0x40]; //FIXME!!! | |
265 | uint8_t *outpos; | |
266 | uint8_t *inpos; | |
267 | int i; | |
268 | int last; | |
269 | int r; | |
270 | ||
271 | memset(buf, 0, sizeof(buf)); | |
272 | ||
273 | r = read(fd, buf, sizeof(buf)-1); | |
274 | if (r > 0) { | |
275 | uint8_t *inend = buf + r; | |
276 | ||
277 | inpos = buf; | |
278 | ||
279 | while (inpos < inend) { | |
280 | uint8_t *instart = inpos; | |
281 | ||
282 | if ((*inpos == '\r') || (*inpos == '\n')) { | |
283 | inpos++; | |
284 | continue; | |
285 | } | |
286 | ||
287 | outpos = out; | |
288 | ||
289 | last = inend - inpos; | |
290 | ||
291 | for (i = 0; i < last; i++) { | |
292 | if ((inpos[i] == '\r') || (inpos[i] == '\n')) { | |
293 | last = i; | |
294 | break; | |
295 | } | |
296 | } | |
297 | ||
298 | if (last == 0) | |
299 | continue; | |
300 | ||
301 | if (verbose) { | |
302 | printf("LAN > "); | |
303 | for (i = 0; i < last; i++) | |
304 | printf("%c", instart[i]); | |
305 | printf("\n"); | |
306 | } | |
307 | ||
308 | memset(out, 0, sizeof(out)); | |
309 | *outpos++ = *inpos++; | |
310 | ||
311 | switch(*instart) { | |
312 | case 'S': | |
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)), 0); | |
318 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE); | |
319 | break; | |
320 | case 'Y': | |
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)), 0); | |
323 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE); | |
324 | break; | |
325 | default: | |
326 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS); | |
327 | break; | |
328 | } | |
329 | ||
330 | hmcfgusb_send(dev, out, sizeof(out), 1); | |
331 | } | |
332 | } else if (r < 0) { | |
333 | perror("read"); | |
334 | return r; | |
335 | } else { | |
336 | return 0; | |
337 | } | |
338 | ||
339 | return 1; | |
340 | } | |
341 | ||
342 | static int comm(int fd_in, int fd_out, int master_socket, int flags) | |
343 | { | |
344 | struct hmcfgusb_dev *dev; | |
345 | uint8_t out[0x40]; //FIXME!!! | |
346 | int quit = 0; | |
347 | ||
348 | hmcfgusb_set_debug(debug); | |
349 | ||
350 | dev = hmcfgusb_init(hmlan_format_out, &fd_out); | |
351 | if (!dev) { | |
352 | fprintf(stderr, "Can't initialize HM-CFG-USB!\n"); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) { | |
357 | fprintf(stderr, "Can't add client to pollfd!\n"); | |
358 | hmcfgusb_close(dev); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | if (master_socket >= 0) { | |
363 | if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) { | |
364 | fprintf(stderr, "Can't add master_socket to pollfd!\n"); | |
365 | hmcfgusb_close(dev); | |
366 | return 0; | |
367 | } | |
368 | } | |
369 | ||
370 | memset(out, 0, sizeof(out)); | |
371 | out[0] = 'K'; | |
372 | hmcfgusb_send_null_frame(dev); | |
373 | hmcfgusb_send(dev, out, sizeof(out), 1); | |
374 | ||
375 | while(!quit) { | |
376 | int fd; | |
377 | ||
378 | fd = hmcfgusb_poll(dev, 1); /* Wakeup device/bus at least once a second */ | |
379 | if (fd >= 0) { | |
380 | if (fd == master_socket) { | |
381 | int client; | |
382 | ||
383 | client = accept(master_socket, NULL, 0); | |
384 | if (client >= 0) { | |
385 | shutdown(client, SHUT_RDWR); | |
386 | close(client); | |
387 | } | |
388 | } else { | |
389 | if (hmlan_parse_in(fd, dev) <= 0) { | |
390 | quit = 1; | |
391 | } | |
392 | } | |
393 | } else if (fd == -1) { | |
394 | if (errno) { | |
395 | perror("hmcfgusb_poll"); | |
396 | quit = 1; | |
397 | } else { | |
398 | /* periodically wakeup the device */ | |
399 | hmcfgusb_send_null_frame(dev); | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | hmcfgusb_close(dev); | |
405 | return 1; | |
406 | } | |
407 | ||
408 | void sigterm_handler(int sig) | |
409 | { | |
410 | if (unlink(PID_FILE) == -1) | |
411 | perror("Can't remove PID file"); | |
412 | ||
413 | exit(EXIT_SUCCESS); | |
414 | } | |
415 | ||
416 | #define FLAG_DAEMON (1 << 0) | |
417 | #define FLAG_PID_FILE (1 << 1) | |
418 | ||
419 | static int socket_server(char *iface, int port, int flags) | |
420 | { | |
421 | struct sigaction sact; | |
422 | struct sockaddr_in sin; | |
423 | int sock; | |
424 | int n; | |
425 | pid_t pid; | |
426 | ||
427 | if (flags & FLAG_DAEMON) { | |
428 | FILE *pidfile = NULL; | |
429 | ||
430 | if (flags & FLAG_PID_FILE) { | |
431 | int fd; | |
432 | ||
433 | fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
434 | if (fd == -1) { | |
435 | if (errno == EEXIST) { | |
436 | pid_t old_pid; | |
437 | pidfile = fopen(PID_FILE, "r"); | |
438 | if (!pidfile) { | |
439 | perror("PID file " PID_FILE " already exists, already running?"); | |
440 | exit(EXIT_FAILURE); | |
441 | } | |
442 | ||
443 | if (fscanf(pidfile, "%u", &old_pid) != 1) { | |
444 | fclose(pidfile); | |
445 | fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n"); | |
446 | exit(EXIT_FAILURE); | |
447 | } | |
448 | ||
449 | fclose(pidfile); | |
450 | ||
451 | fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid); | |
452 | exit(EXIT_FAILURE); | |
453 | } | |
454 | perror("Can't create PID file " PID_FILE); | |
455 | exit(EXIT_FAILURE); | |
456 | } | |
457 | ||
458 | pidfile = fdopen(fd, "w"); | |
459 | if (!pidfile) { | |
460 | perror("Can't reopen PID file fd"); | |
461 | exit(EXIT_FAILURE); | |
462 | } | |
463 | ||
464 | memset(&sact, 0, sizeof(sact)); | |
465 | sact.sa_handler = sigterm_handler; | |
466 | ||
467 | if (sigaction(SIGTERM, &sact, NULL) == -1) { | |
468 | perror("sigaction(SIGTERM)"); | |
469 | exit(EXIT_FAILURE); | |
470 | } | |
471 | } | |
472 | ||
473 | pid = fork(); | |
474 | if (pid > 0) { | |
475 | if (pidfile) { | |
476 | fprintf(pidfile, "%u\n", pid); | |
477 | fclose(pidfile); | |
478 | } | |
479 | ||
480 | printf("Daemon with PID %u started!\n", pid); | |
481 | exit(EXIT_SUCCESS); | |
482 | } else if (pid < 0) { | |
483 | perror("fork"); | |
484 | exit(EXIT_FAILURE); | |
485 | } | |
486 | ||
487 | if (pidfile) | |
488 | fclose(pidfile); | |
489 | } | |
490 | ||
491 | memset(&sact, 0, sizeof(sact)); | |
492 | sact.sa_handler = SIG_IGN; | |
493 | ||
494 | if (sigaction(SIGPIPE, &sact, NULL) == -1) { | |
495 | perror("sigaction(SIGPIPE)"); | |
496 | exit(EXIT_FAILURE); | |
497 | } | |
498 | ||
499 | impersonate_hmlanif = 1; | |
500 | ||
501 | sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
502 | if (sock == -1) { | |
503 | perror("Can't open socket"); | |
504 | return EXIT_FAILURE; | |
505 | } | |
506 | ||
507 | n = 1; | |
508 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) { | |
509 | perror("Can't set socket options"); | |
510 | return EXIT_FAILURE; | |
511 | } | |
512 | ||
513 | memset(&sin, 0, sizeof(sin)); | |
514 | sin.sin_family = AF_INET; | |
515 | sin.sin_port = htons(port); | |
516 | if (!iface) { | |
517 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
518 | } else { | |
519 | if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) { | |
520 | perror("inet_ntop"); | |
521 | return EXIT_FAILURE; | |
522 | } | |
523 | } | |
524 | ||
525 | if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | |
526 | perror("Can't bind socket"); | |
527 | return EXIT_FAILURE; | |
528 | } | |
529 | ||
530 | if (listen(sock, 1) == -1) { | |
531 | perror("Can't listen on socket"); | |
532 | return EXIT_FAILURE; | |
533 | } | |
534 | ||
535 | while(1) { | |
536 | struct sockaddr_in csin; | |
537 | socklen_t csinlen; | |
538 | int client; | |
539 | in_addr_t client_addr; | |
540 | ||
541 | memset(&csin, 0, sizeof(csin)); | |
542 | csinlen = sizeof(csin); | |
543 | client = accept(sock, (struct sockaddr*)&csin, &csinlen); | |
544 | if (client == -1) { | |
545 | perror("Couldn't accept client"); | |
546 | continue; | |
547 | } | |
548 | ||
549 | /* FIXME: getnameinfo... */ | |
550 | client_addr = ntohl(csin.sin_addr.s_addr); | |
551 | ||
552 | if (verbose) { | |
553 | printf("Client %d.%d.%d.%d connected!\n", | |
554 | (client_addr & 0xff000000) >> 24, | |
555 | (client_addr & 0x00ff0000) >> 16, | |
556 | (client_addr & 0x0000ff00) >> 8, | |
557 | (client_addr & 0x000000ff)); | |
558 | } | |
559 | ||
560 | comm(client, client, sock, flags); | |
561 | ||
562 | shutdown(client, SHUT_RDWR); | |
563 | close(client); | |
564 | ||
565 | if (verbose) { | |
566 | printf("Connection to %d.%d.%d.%d closed!\n", | |
567 | (client_addr & 0xff000000) >> 24, | |
568 | (client_addr & 0x00ff0000) >> 16, | |
569 | (client_addr & 0x0000ff00) >> 8, | |
570 | (client_addr & 0x000000ff)); | |
571 | } | |
572 | sleep(1); | |
573 | } | |
574 | ||
575 | return EXIT_SUCCESS; | |
576 | } | |
577 | ||
578 | static int interactive_server(int flags) | |
579 | { | |
580 | if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags)) | |
581 | return EXIT_FAILURE; | |
582 | ||
583 | return EXIT_SUCCESS; | |
584 | } | |
585 | ||
586 | void hmlan_syntax(char *prog) | |
587 | { | |
588 | fprintf(stderr, "Syntax: %s options\n\n", prog); | |
589 | fprintf(stderr, "Possible options:\n"); | |
590 | fprintf(stderr, "\t-D\tdebug mode\n"); | |
591 | fprintf(stderr, "\t-d\tdaemon mode\n"); | |
592 | fprintf(stderr, "\t-h\tthis help\n"); | |
593 | fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n"); | |
594 | fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n"); | |
595 | fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n"); | |
596 | fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n"); | |
597 | fprintf(stderr, "\t-v\tverbose mode\n"); | |
598 | ||
599 | } | |
600 | ||
601 | int main(int argc, char **argv) | |
602 | { | |
603 | int port = 1000; | |
604 | char *iface = NULL; | |
605 | int interactive = 0; | |
606 | int flags = 0; | |
607 | char *ep; | |
608 | int opt; | |
609 | ||
610 | while((opt = getopt(argc, argv, "DdhiPp:Rl:v")) != -1) { | |
611 | switch (opt) { | |
612 | case 'D': | |
613 | debug = 1; | |
614 | verbose = 1; | |
615 | break; | |
616 | case 'd': | |
617 | flags |= FLAG_DAEMON; | |
618 | break; | |
619 | case 'i': | |
620 | interactive = 1; | |
621 | break; | |
622 | case 'P': | |
623 | flags |= FLAG_PID_FILE; | |
624 | break; | |
625 | case 'p': | |
626 | port = strtoul(optarg, &ep, 10); | |
627 | if (*ep != '\0') { | |
628 | fprintf(stderr, "Can't parse port!\n"); | |
629 | exit(EXIT_FAILURE); | |
630 | } | |
631 | break; | |
632 | case 'R': | |
633 | fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n"); | |
634 | break; | |
635 | case 'l': | |
636 | iface = optarg; | |
637 | break; | |
638 | case 'v': | |
639 | verbose = 1; | |
640 | break; | |
641 | case 'h': | |
642 | case ':': | |
643 | case '?': | |
644 | default: | |
645 | hmlan_syntax(argv[0]); | |
646 | exit(EXIT_FAILURE); | |
647 | break; | |
648 | } | |
649 | } | |
650 | ||
651 | if (interactive) { | |
652 | return interactive_server(flags); | |
653 | } else { | |
654 | return socket_server(iface, port, flags); | |
655 | } | |
656 | } |