gracefully handle errors on socket-write
[hmcfgusb] / hmland.c
CommitLineData
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
42extern char *optarg;
43
e0a7146e 44static int impersonate_hmlanif = 0;
627e3f33
MG
45static int debug = 0;
46static 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
58static 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
113static 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
128static 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 176static 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 250static 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 323static 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 382static 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");
406 }
407
e0a7146e
MG
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;
627e3f33 441 in_addr_t client_addr;
e0a7146e
MG
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
627e3f33
MG
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 }
e0a7146e
MG
461
462 comm(client, client, sock);
463
464 shutdown(client, SHUT_RDWR);
465 close(client);
466
627e3f33
MG
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 }
e0a7146e
MG
474
475 }
476
477 return EXIT_SUCCESS;
478}
479
480static int interactive_server(void)
481{
482 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1))
483 return EXIT_FAILURE;
9db2e455
MG
484
485 return EXIT_SUCCESS;
486}
e0a7146e 487
627e3f33
MG
488void 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
e0a7146e
MG
501int main(int argc, char **argv)
502{
627e3f33
MG
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 }
e0a7146e 546}
Impressum, Datenschutz