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