]>
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> | |
35 | #include <netinet/in.h> | |
36 | #include <arpa/inet.h> | |
9db2e455 MG |
37 | #include <libusb-1.0/libusb.h> |
38 | ||
39 | #include "hexdump.h" | |
40 | #include "hmcfgusb.h" | |
41 | ||
627e3f33 MG |
42 | extern char *optarg; |
43 | ||
e0a7146e | 44 | static int impersonate_hmlanif = 0; |
627e3f33 MG |
45 | static int debug = 0; |
46 | static int verbose = 0; | |
e0a7146e | 47 | |
e75295bb MG |
48 | #define FLAG_LENGTH_BYTE (1<<0) |
49 | #define FLAG_FORMAT_HEX (1<<1) | |
50 | #define FLAG_COMMA_BEFORE (1<<2) | |
51 | #define FLAG_COMMA_AFTER (1<<3) | |
52 | #define FLAG_NL (1<<4) | |
53 | #define FLAG_IGNORE_COMMAS (1<<5) | |
54 | ||
55 | #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; } | |
56 | #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; } | |
57 | ||
58 | static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags) | |
59 | { | |
51d4ece6 MG |
60 | const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
61 | 'A', 'B', 'C', 'D', 'E', 'F'}; | |
e75295bb MG |
62 | uint8_t *buf_out = *outpos; |
63 | uint8_t *outend = *outpos + outlen; | |
64 | uint8_t *inend = *inpos + inlen; | |
65 | int i; | |
66 | ||
67 | if (flags & FLAG_COMMA_BEFORE) { | |
68 | CHECK_SPACE(1); | |
69 | **outpos=','; | |
70 | *outpos += 1; | |
71 | } | |
72 | ||
73 | if (flags & FLAG_LENGTH_BYTE) { | |
74 | CHECK_AVAIL(1); | |
75 | len = **inpos; | |
76 | *inpos += 1; | |
77 | } | |
78 | ||
79 | if (flags & FLAG_FORMAT_HEX) { | |
e75295bb MG |
80 | CHECK_AVAIL(len); |
81 | CHECK_SPACE(len*2); | |
82 | for (i = 0; i < len; i++) { | |
51d4ece6 MG |
83 | **outpos = nibble[((**inpos) & 0xf0) >> 4]; |
84 | *outpos += 1; | |
85 | **outpos = nibble[((**inpos) & 0xf)]; | |
86 | *inpos += 1; *outpos += 1; | |
e75295bb MG |
87 | } |
88 | } else { | |
89 | CHECK_AVAIL(len); | |
90 | CHECK_SPACE(len); | |
91 | memcpy(*outpos, *inpos, len); | |
92 | *outpos += len; | |
93 | *inpos += len; | |
94 | } | |
95 | ||
96 | if (flags & FLAG_COMMA_AFTER) { | |
97 | CHECK_SPACE(1); | |
98 | **outpos=','; | |
99 | *outpos += 1; | |
100 | } | |
101 | ||
102 | if (flags & FLAG_NL) { | |
103 | CHECK_SPACE(2); | |
104 | **outpos='\r'; | |
105 | *outpos += 1; | |
106 | **outpos='\n'; | |
107 | *outpos += 1; | |
108 | } | |
109 | ||
110 | return *outpos - buf_out; | |
111 | } | |
112 | ||
51d4ece6 MG |
113 | static uint8_t ascii_to_nibble(uint8_t a) |
114 | { | |
115 | uint8_t c = 0x00; | |
116 | ||
117 | if ((a >= '0') && (a <= '9')) { | |
118 | c = a - '0'; | |
119 | } else if ((a >= 'A') && (a <= 'F')) { | |
120 | c = (a - 'A') + 10; | |
121 | } else if ((a >= 'a') && (a <= 'f')) { | |
122 | c = (a - 'a') + 10; | |
123 | } | |
124 | ||
125 | return c; | |
126 | } | |
127 | ||
e75295bb MG |
128 | static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags) |
129 | { | |
130 | uint8_t *buf_out = *outpos; | |
131 | uint8_t *outend = *outpos + outlen; | |
132 | uint8_t *inend = *inpos + inlen; | |
e75295bb MG |
133 | |
134 | if (flags & FLAG_LENGTH_BYTE) { | |
135 | int len = 0; | |
136 | uint8_t *ip; | |
137 | ||
138 | ip = *inpos; | |
139 | while(ip < inend) { | |
140 | if (*ip == ',') { | |
141 | ip++; | |
142 | if (!(flags & FLAG_IGNORE_COMMAS)) | |
143 | break; | |
144 | ||
145 | continue; | |
146 | } | |
147 | len++; | |
148 | ip++; | |
149 | } | |
150 | CHECK_SPACE(1); | |
151 | **outpos = (len / 2); | |
152 | *outpos += 1; | |
153 | } | |
154 | ||
155 | while(*inpos < inend) { | |
156 | if (**inpos == ',') { | |
157 | *inpos += 1; | |
158 | if (!(flags & FLAG_IGNORE_COMMAS)) | |
159 | break; | |
160 | ||
161 | continue; | |
162 | } | |
163 | ||
164 | CHECK_SPACE(1); | |
165 | CHECK_AVAIL(2); | |
e75295bb | 166 | |
51d4ece6 MG |
167 | **outpos = ascii_to_nibble(**inpos) << 4; |
168 | *inpos += 1; | |
169 | **outpos |= ascii_to_nibble(**inpos); | |
170 | *inpos += 1; *outpos += 1; | |
e75295bb MG |
171 | } |
172 | ||
173 | return *outpos - buf_out; | |
174 | } | |
175 | ||
4371275b | 176 | static int hmlan_format_out(uint8_t *buf, int buf_len, void *data) |
9db2e455 | 177 | { |
e75295bb MG |
178 | uint8_t out[1024]; |
179 | uint8_t *outpos; | |
180 | uint8_t *inpos; | |
e0a7146e | 181 | int fd = *((int*)data); |
4371275b | 182 | int w; |
9db2e455 MG |
183 | |
184 | if (buf_len < 1) | |
4371275b | 185 | return 1; |
9db2e455 | 186 | |
e0a7146e | 187 | memset(out, 0, sizeof(out)); |
e75295bb MG |
188 | outpos = out; |
189 | inpos = buf; | |
e0a7146e | 190 | |
e75295bb | 191 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0); |
9db2e455 MG |
192 | switch(buf[0]) { |
193 | case 'H': | |
e0a7146e MG |
194 | if (impersonate_hmlanif) { |
195 | buf[5] = 'L'; | |
196 | buf[6] = 'A'; | |
197 | buf[7] = 'N'; | |
198 | } | |
e75295bb MG |
199 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE); |
200 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
201 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE); | |
202 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
203 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
204 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
205 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL); | |
9db2e455 MG |
206 | |
207 | break; | |
208 | case 'E': | |
e75295bb MG |
209 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX); |
210 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
211 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
212 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
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_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL); | |
215 | ||
9db2e455 MG |
216 | break; |
217 | case 'R': | |
e75295bb MG |
218 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX); |
219 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
220 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
221 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
222 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
223 | 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); | |
224 | ||
9db2e455 MG |
225 | break; |
226 | case 'I': | |
e75295bb MG |
227 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX); |
228 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
229 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE); | |
230 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL); | |
231 | ||
232 | break; | |
9db2e455 | 233 | default: |
e75295bb | 234 | format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL); |
9db2e455 MG |
235 | hexdump(buf, buf_len, "Unknown> "); |
236 | break; | |
237 | } | |
627e3f33 MG |
238 | if (debug) |
239 | fprintf(stderr, "LAN < %s\n", out); | |
4371275b MG |
240 | |
241 | w = write(fd, out, outpos-out); | |
242 | if (w <= 0) { | |
243 | perror("write"); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | return 1; | |
9db2e455 MG |
248 | } |
249 | ||
e0a7146e | 250 | static int hmlan_parse_in(int fd, void *data) |
9db2e455 MG |
251 | { |
252 | struct hmcfgusb_dev *dev = data; | |
627e3f33 | 253 | uint8_t buf[1025]; |
e75295bb MG |
254 | uint8_t out[0x40]; //FIXME!!! |
255 | uint8_t *outpos; | |
256 | uint8_t *inpos; | |
9db2e455 | 257 | int i; |
627e3f33 | 258 | int last; |
9db2e455 MG |
259 | int r; |
260 | ||
627e3f33 MG |
261 | memset(buf, 0, sizeof(buf)); |
262 | ||
263 | r = read(fd, buf, sizeof(buf)-1); | |
9db2e455 | 264 | if (r > 0) { |
627e3f33 MG |
265 | uint8_t *inend = buf + r; |
266 | ||
267 | inpos = buf; | |
268 | ||
269 | if (debug) | |
e2572b06 | 270 | fprintf(stderr, "\nLAN > %s", buf); |
627e3f33 MG |
271 | |
272 | while (inpos < inend) { | |
273 | uint8_t *instart = inpos; | |
274 | ||
275 | if ((*inpos == '\r') || (*inpos == '\n')) { | |
276 | inpos++; | |
277 | continue; | |
9db2e455 | 278 | } |
627e3f33 MG |
279 | |
280 | outpos = out; | |
9db2e455 | 281 | |
627e3f33 MG |
282 | last = inend - inpos; |
283 | ||
284 | for (i = 0; i < last; i++) { | |
285 | if ((inpos[i] == '\r') || (inpos[i] == '\n')) { | |
286 | last = i; | |
287 | break; | |
288 | } | |
289 | } | |
290 | ||
291 | if (last == 0) | |
292 | continue; | |
293 | ||
294 | memset(out, 0, sizeof(out)); | |
295 | *outpos++ = *inpos++; | |
296 | ||
297 | switch(buf[0]) { | |
298 | case 'S': | |
299 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
300 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
301 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
302 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
303 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); | |
304 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE); | |
305 | break; | |
306 | default: | |
307 | parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS); | |
308 | break; | |
309 | } | |
e75295bb | 310 | |
33df13be | 311 | hmcfgusb_send(dev, out, sizeof(out), 1); |
627e3f33 | 312 | } |
9db2e455 MG |
313 | } else if (r < 0) { |
314 | perror("read"); | |
e0a7146e MG |
315 | return r; |
316 | } else { | |
317 | return 0; | |
9db2e455 | 318 | } |
e0a7146e MG |
319 | |
320 | return 1; | |
9db2e455 MG |
321 | } |
322 | ||
e0a7146e | 323 | static int comm(int fd_in, int fd_out, int master_socket) |
9db2e455 MG |
324 | { |
325 | struct hmcfgusb_dev *dev; | |
326 | int quit = 0; | |
327 | ||
627e3f33 MG |
328 | hmcfgusb_set_debug(debug); |
329 | ||
e0a7146e | 330 | dev = hmcfgusb_init(hmlan_format_out, &fd_out); |
9db2e455 MG |
331 | if (!dev) { |
332 | fprintf(stderr, "Can't initialize HM-CFG-USB!\n"); | |
e0a7146e MG |
333 | return 0; |
334 | } | |
335 | ||
336 | if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) { | |
337 | fprintf(stderr, "Can't add client to pollfd!\n"); | |
338 | hmcfgusb_close(dev); | |
339 | return 0; | |
9db2e455 MG |
340 | } |
341 | ||
e0a7146e MG |
342 | if (master_socket >= 0) { |
343 | if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) { | |
344 | fprintf(stderr, "Can't add master_socket to pollfd!\n"); | |
345 | hmcfgusb_close(dev); | |
346 | return 0; | |
347 | } | |
9db2e455 MG |
348 | } |
349 | ||
350 | hmcfgusb_send(dev, (unsigned char*)"K", 1, 1); | |
351 | ||
352 | while(!quit) { | |
353 | int fd; | |
354 | ||
355 | fd = hmcfgusb_poll(dev, 3600); | |
356 | if (fd >= 0) { | |
e0a7146e MG |
357 | if (fd == master_socket) { |
358 | int client; | |
359 | ||
360 | client = accept(master_socket, NULL, 0); | |
361 | if (client >= 0) { | |
362 | shutdown(client, SHUT_RDWR); | |
363 | close(client); | |
364 | } | |
365 | } else { | |
366 | if (hmlan_parse_in(fd, dev) <= 0) { | |
367 | quit = 1; | |
368 | } | |
369 | } | |
9db2e455 MG |
370 | } else if (fd == -1) { |
371 | if (errno) { | |
372 | perror("hmcfgusb_poll"); | |
373 | quit = 1; | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
813afd12 | 378 | hmcfgusb_close(dev); |
e0a7146e MG |
379 | return 1; |
380 | } | |
381 | ||
627e3f33 | 382 | static int socket_server(int port, int daemon) |
e0a7146e | 383 | { |
4371275b | 384 | struct sigaction sact; |
e0a7146e MG |
385 | struct sockaddr_in sin; |
386 | int sock; | |
387 | int n; | |
627e3f33 MG |
388 | pid_t pid; |
389 | ||
390 | if (daemon) { | |
391 | pid = fork(); | |
392 | if (pid > 0) { | |
393 | printf("Daemon with PID %u started!\n", pid); | |
394 | exit(EXIT_SUCCESS); | |
395 | } else if (pid < 0) { | |
396 | perror("fork"); | |
397 | exit(EXIT_FAILURE); | |
398 | } | |
399 | } | |
e0a7146e | 400 | |
4371275b MG |
401 | memset(&sact, 0, sizeof(sact)); |
402 | sact.sa_handler = SIG_IGN; | |
403 | ||
404 | if (sigaction(SIGPIPE, &sact, NULL) == -1) { | |
405 | perror("sigaction"); | |
27f4063e | 406 | exit(EXIT_FAILURE); |
4371275b MG |
407 | } |
408 | ||
e0a7146e MG |
409 | impersonate_hmlanif = 1; |
410 | ||
411 | sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
412 | if (sock == -1) { | |
413 | perror("Can't open socket"); | |
414 | return EXIT_FAILURE; | |
415 | } | |
416 | ||
417 | n = 1; | |
418 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) { | |
419 | perror("Can't set socket options"); | |
420 | return EXIT_FAILURE; | |
421 | } | |
422 | ||
423 | memset(&sin, 0, sizeof(sin)); | |
424 | sin.sin_family = AF_INET; | |
425 | sin.sin_port = htons(port); | |
426 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
427 | ||
428 | if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | |
429 | perror("Can't bind socket"); | |
430 | return EXIT_FAILURE; | |
431 | } | |
432 | ||
433 | if (listen(sock, 1) == -1) { | |
434 | perror("Can't listen on socket"); | |
435 | return EXIT_FAILURE; | |
436 | } | |
437 | ||
438 | while(1) { | |
439 | struct sockaddr_in csin; | |
440 | socklen_t csinlen; | |
441 | int client; | |
627e3f33 | 442 | in_addr_t client_addr; |
e0a7146e MG |
443 | |
444 | memset(&csin, 0, sizeof(csin)); | |
445 | csinlen = sizeof(csin); | |
446 | client = accept(sock, (struct sockaddr*)&csin, &csinlen); | |
447 | if (client == -1) { | |
448 | perror("Couldn't accept client"); | |
449 | continue; | |
450 | } | |
451 | ||
627e3f33 MG |
452 | /* FIXME: getnameinfo... */ |
453 | client_addr = ntohl(csin.sin_addr.s_addr); | |
454 | ||
455 | if (verbose) { | |
456 | printf("Client %d.%d.%d.%d connected!\n", | |
457 | (client_addr & 0xff000000) >> 24, | |
458 | (client_addr & 0x00ff0000) >> 16, | |
459 | (client_addr & 0x0000ff00) >> 8, | |
460 | (client_addr & 0x000000ff)); | |
461 | } | |
e0a7146e MG |
462 | |
463 | comm(client, client, sock); | |
464 | ||
465 | shutdown(client, SHUT_RDWR); | |
466 | close(client); | |
467 | ||
627e3f33 MG |
468 | if (verbose) { |
469 | printf("Connection to %d.%d.%d.%d closed!\n", | |
470 | (client_addr & 0xff000000) >> 24, | |
471 | (client_addr & 0x00ff0000) >> 16, | |
472 | (client_addr & 0x0000ff00) >> 8, | |
473 | (client_addr & 0x000000ff)); | |
474 | } | |
e0a7146e MG |
475 | |
476 | } | |
477 | ||
478 | return EXIT_SUCCESS; | |
479 | } | |
480 | ||
481 | static int interactive_server(void) | |
482 | { | |
483 | if (!comm(STDIN_FILENO, STDOUT_FILENO, -1)) | |
484 | return EXIT_FAILURE; | |
9db2e455 MG |
485 | |
486 | return EXIT_SUCCESS; | |
487 | } | |
e0a7146e | 488 | |
627e3f33 MG |
489 | void hmlan_syntax(char *prog) |
490 | { | |
491 | fprintf(stderr, "Syntax: %s options\n\n", prog); | |
492 | fprintf(stderr, "Possible options:\n"); | |
493 | fprintf(stderr, "\t-D\tdebug mode\n"); | |
494 | fprintf(stderr, "\t-d\tdaemon mode\n"); | |
495 | fprintf(stderr, "\t-h\tthis help\n"); | |
496 | fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n"); | |
497 | fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n"); | |
498 | fprintf(stderr, "\t-v\tverbose mode\n"); | |
499 | ||
500 | } | |
501 | ||
e0a7146e MG |
502 | int main(int argc, char **argv) |
503 | { | |
627e3f33 MG |
504 | int port = 1000; |
505 | int interactive = 0; | |
506 | int daemon = 0; | |
507 | char *ep; | |
508 | int opt; | |
509 | ||
510 | while((opt = getopt(argc, argv, "Ddhip:v")) != -1) { | |
511 | switch (opt) { | |
512 | case 'D': | |
513 | debug = 1; | |
514 | verbose = 1; | |
515 | break; | |
516 | case 'd': | |
517 | daemon = 1; | |
518 | break; | |
519 | case 'i': | |
520 | interactive = 1; | |
521 | break; | |
522 | case 'p': | |
523 | port = strtoul(optarg, &ep, 10); | |
524 | if (*ep != '\0') { | |
525 | fprintf(stderr, "Can't parse port!\n"); | |
526 | exit(EXIT_FAILURE); | |
527 | } | |
528 | break; | |
529 | case 'v': | |
530 | verbose = 1; | |
531 | break; | |
532 | case 'h': | |
533 | case ':': | |
534 | case '?': | |
535 | default: | |
536 | hmlan_syntax(argv[0]); | |
537 | exit(EXIT_FAILURE); | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
542 | if (interactive) { | |
543 | return interactive_server(); | |
544 | } else { | |
545 | return socket_server(port, daemon); | |
546 | } | |
e0a7146e | 547 | } |