]> git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
gracefully handle errors on socket-write
[hmcfgusb] / hmland.c
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 <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <libusb-1.0/libusb.h>
38
39 #include "hexdump.h"
40 #include "hmcfgusb.h"
41
42 extern char *optarg;
43
44 static int impersonate_hmlanif = 0;
45 static int debug = 0;
46 static int verbose = 0;
47
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 {
60 const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
61 'A', 'B', 'C', 'D', 'E', 'F'};
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) {
80 CHECK_AVAIL(len);
81 CHECK_SPACE(len*2);
82 for (i = 0; i < len; i++) {
83 **outpos = nibble[((**inpos) & 0xf0) >> 4];
84 *outpos += 1;
85 **outpos = nibble[((**inpos) & 0xf)];
86 *inpos += 1; *outpos += 1;
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
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
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;
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);
166
167 **outpos = ascii_to_nibble(**inpos) << 4;
168 *inpos += 1;
169 **outpos |= ascii_to_nibble(**inpos);
170 *inpos += 1; *outpos += 1;
171 }
172
173 return *outpos - buf_out;
174 }
175
176 static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
177 {
178 uint8_t out[1024];
179 uint8_t *outpos;
180 uint8_t *inpos;
181 int fd = *((int*)data);
182 int w;
183
184 if (buf_len < 1)
185 return 1;
186
187 memset(out, 0, sizeof(out));
188 outpos = out;
189 inpos = buf;
190
191 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
192 switch(buf[0]) {
193 case 'H':
194 if (impersonate_hmlanif) {
195 buf[5] = 'L';
196 buf[6] = 'A';
197 buf[7] = 'N';
198 }
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);
206
207 break;
208 case 'E':
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
216 break;
217 case 'R':
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
225 break;
226 case 'I':
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;
233 default:
234 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
235 hexdump(buf, buf_len, "Unknown> ");
236 break;
237 }
238 if (debug)
239 fprintf(stderr, "LAN < %s\n", out);
240
241 w = write(fd, out, outpos-out);
242 if (w <= 0) {
243 perror("write");
244 return 0;
245 }
246
247 return 1;
248 }
249
250 static int hmlan_parse_in(int fd, void *data)
251 {
252 struct hmcfgusb_dev *dev = data;
253 uint8_t buf[1025];
254 uint8_t out[0x40]; //FIXME!!!
255 uint8_t *outpos;
256 uint8_t *inpos;
257 int i;
258 int last;
259 int r;
260
261 memset(buf, 0, sizeof(buf));
262
263 r = read(fd, buf, sizeof(buf)-1);
264 if (r > 0) {
265 uint8_t *inend = buf + r;
266
267 inpos = buf;
268
269 if (debug)
270 fprintf(stderr, "\nLAN > %s", buf);
271
272 while (inpos < inend) {
273 uint8_t *instart = inpos;
274
275 if ((*inpos == '\r') || (*inpos == '\n')) {
276 inpos++;
277 continue;
278 }
279
280 outpos = out;
281
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 }
310
311 hmcfgusb_send(dev, out, sizeof(out), 1);
312 }
313 } else if (r < 0) {
314 perror("read");
315 return r;
316 } else {
317 return 0;
318 }
319
320 return 1;
321 }
322
323 static int comm(int fd_in, int fd_out, int master_socket)
324 {
325 struct hmcfgusb_dev *dev;
326 int quit = 0;
327
328 hmcfgusb_set_debug(debug);
329
330 dev = hmcfgusb_init(hmlan_format_out, &fd_out);
331 if (!dev) {
332 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
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;
340 }
341
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 }
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) {
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 }
370 } else if (fd == -1) {
371 if (errno) {
372 perror("hmcfgusb_poll");
373 quit = 1;
374 }
375 }
376 }
377
378 hmcfgusb_close(dev);
379 return 1;
380 }
381
382 static int socket_server(int port, int daemon)
383 {
384 struct sigaction sact;
385 struct sockaddr_in sin;
386 int sock;
387 int n;
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 }
400
401 memset(&sact, 0, sizeof(sact));
402 sact.sa_handler = SIG_IGN;
403
404 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
405 perror("sigaction");
406 }
407
408 impersonate_hmlanif = 1;
409
410 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
411 if (sock == -1) {
412 perror("Can't open socket");
413 return EXIT_FAILURE;
414 }
415
416 n = 1;
417 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
418 perror("Can't set socket options");
419 return EXIT_FAILURE;
420 }
421
422 memset(&sin, 0, sizeof(sin));
423 sin.sin_family = AF_INET;
424 sin.sin_port = htons(port);
425 sin.sin_addr.s_addr = htonl(INADDR_ANY);
426
427 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
428 perror("Can't bind socket");
429 return EXIT_FAILURE;
430 }
431
432 if (listen(sock, 1) == -1) {
433 perror("Can't listen on socket");
434 return EXIT_FAILURE;
435 }
436
437 while(1) {
438 struct sockaddr_in csin;
439 socklen_t csinlen;
440 int client;
441 in_addr_t client_addr;
442
443 memset(&csin, 0, sizeof(csin));
444 csinlen = sizeof(csin);
445 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
446 if (client == -1) {
447 perror("Couldn't accept client");
448 continue;
449 }
450
451 /* FIXME: getnameinfo... */
452 client_addr = ntohl(csin.sin_addr.s_addr);
453
454 if (verbose) {
455 printf("Client %d.%d.%d.%d connected!\n",
456 (client_addr & 0xff000000) >> 24,
457 (client_addr & 0x00ff0000) >> 16,
458 (client_addr & 0x0000ff00) >> 8,
459 (client_addr & 0x000000ff));
460 }
461
462 comm(client, client, sock);
463
464 shutdown(client, SHUT_RDWR);
465 close(client);
466
467 if (verbose) {
468 printf("Connection to %d.%d.%d.%d closed!\n",
469 (client_addr & 0xff000000) >> 24,
470 (client_addr & 0x00ff0000) >> 16,
471 (client_addr & 0x0000ff00) >> 8,
472 (client_addr & 0x000000ff));
473 }
474
475 }
476
477 return EXIT_SUCCESS;
478 }
479
480 static int interactive_server(void)
481 {
482 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1))
483 return EXIT_FAILURE;
484
485 return EXIT_SUCCESS;
486 }
487
488 void hmlan_syntax(char *prog)
489 {
490 fprintf(stderr, "Syntax: %s options\n\n", prog);
491 fprintf(stderr, "Possible options:\n");
492 fprintf(stderr, "\t-D\tdebug mode\n");
493 fprintf(stderr, "\t-d\tdaemon mode\n");
494 fprintf(stderr, "\t-h\tthis help\n");
495 fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
496 fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
497 fprintf(stderr, "\t-v\tverbose mode\n");
498
499 }
500
501 int main(int argc, char **argv)
502 {
503 int port = 1000;
504 int interactive = 0;
505 int daemon = 0;
506 char *ep;
507 int opt;
508
509 while((opt = getopt(argc, argv, "Ddhip:v")) != -1) {
510 switch (opt) {
511 case 'D':
512 debug = 1;
513 verbose = 1;
514 break;
515 case 'd':
516 daemon = 1;
517 break;
518 case 'i':
519 interactive = 1;
520 break;
521 case 'p':
522 port = strtoul(optarg, &ep, 10);
523 if (*ep != '\0') {
524 fprintf(stderr, "Can't parse port!\n");
525 exit(EXIT_FAILURE);
526 }
527 break;
528 case 'v':
529 verbose = 1;
530 break;
531 case 'h':
532 case ':':
533 case '?':
534 default:
535 hmlan_syntax(argv[0]);
536 exit(EXIT_FAILURE);
537 break;
538 }
539 }
540
541 if (interactive) {
542 return interactive_server();
543 } else {
544 return socket_server(port, daemon);
545 }
546 }
Impressum, Datenschutz