Debian Codegraph @

kiss/net2kiss.c

raw symbols SCIP
1 /*****************************************************************************/
2
3 /*
4 * net2kiss.c - convert a network interface to KISS data stream
5 *
6 * Copyright (C) 1996 Thomas Sailer ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 * Please note that the GPL allows you to use the driver, NOT the radio.
24 * In order to use the radio, you need a license from the communications
25 * authority of your country.
26 *
27 *
28 * History:
29 * 0.1 18.09.96 Started
30 */
31
32 /*****************************************************************************/
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <errno.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42 #include <endian.h>
43 #include <netinet/in.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <grp.h>
48 #include <pty.h>
49 #include <string.h>
50 #include <termios.h>
51 #include <limits.h>
52
53 #include <sys/socket.h>
54 #include <net/if.h>
55
56 #include <net/ethernet.h>
57
58
59 /* --------------------------------------------------------------------- */
60
61 static int fdif, fdpty;
62 static struct ifreq ifr;
63 static char *progname;
64 static int verbose;
65 static int i_am_unix98_pty_master; /* unix98 ptmx support */
66 static char *namepts; /* name of the unix98 pts slave, which
67 * the client has to use */
68
69 /* --------------------------------------------------------------------- */
70
71 static void die(char *func)
72 {
73 fprintf(stderr, "%s: %s (%i)%s%s\n", progname, strerror(errno),
74 errno, func ? " in " : "", func ? func : "");
75 syslog(LOG_WARNING, "%s (%i)%s%s\n", strerror(errno),
76 errno, func ? " in " : "", func ? func : "");
77 exit(-1);
78 }
79
80 /* --------------------------------------------------------------------- */
81
82 static void display_packet(unsigned char *bp, unsigned int len)
83 {
84 unsigned char v1=1,cmd=0;
85 unsigned char i,j;
86
87 if (!bp || !len)
88 return;
89 if (len < 8)
90 return;
91 if (bp[1] & 1) {
92 /*
93 * FlexNet Header Compression
94 */
95 v1 = 0;
96 cmd = (bp[1] & 2) != 0;
97 printf("fm ? to ");
98 i = (bp[2] >> 2) & 0x3f;
99 if (i)
100 printf("%c",i+0x20);
101 i = ((bp[2] << 4) | ((bp[3] >> 4) & 0xf)) & 0x3f;
102 if (i)
103 printf("%c",i+0x20);
104 i = ((bp[3] << 2) | ((bp[4] >> 6) & 3)) & 0x3f;
105 if (i)
106 printf("%c",i+0x20);
107 i = bp[4] & 0x3f;
108 if (i)
109 printf("%c",i+0x20);
110 i = (bp[5] >> 2) & 0x3f;
111 if (i)
112 printf("%c",i+0x20);
113 i = ((bp[5] << 4) | ((bp[6] >> 4) & 0xf)) & 0x3f;
114 if (i)
115 printf("%c",i+0x20);
116 printf("-%u QSO Nr %u", bp[6] & 0xf, (bp[0] << 6) |
117 (bp[1] >> 2));
118 bp += 7;
119 len -= 7;
120 } else {
121 /*
122 * normal header
123 */
124 if (len < 15)
125 return;
126 if ((bp[6] & 0x80) != (bp[13] & 0x80)) {
127 v1 = 0;
128 cmd = (bp[6] & 0x80);
129 }
130 printf("fm ");
131 for (i = 7; i < 13; i++)
132 if ((bp[i] &0xfe) != 0x40)
133 printf("%c",bp[i] >> 1);
134 printf("-%u to ",(bp[13] >> 1) & 0xf);
135 for (i = 0; i < 6; i++)
136 if ((bp[i] &0xfe) != 0x40)
137 printf("%c",bp[i] >> 1);
138 printf("-%u",(bp[6] >> 1) & 0xf);
139 bp += 14;
140 len -= 14;
141 if ((!(bp[-1] & 1)) && (len >= 7)) printf(" via ");
142 while ((!(bp[-1] & 1)) && (len >= 7)) {
143 for (i = 0; i < 6; i++)
144 if ((bp[i] &0xfe) != 0x40)
145 printf("%c",bp[i] >> 1);
146 printf("-%u",(bp[6] >> 1) & 0xf);
147 bp += 7;
148 len -= 7;
149 if ((!(bp[-1] & 1)) && (len >= 7))
150 printf(",");
151 }
152 }
153 if (!len)
154 return;
155 i = *bp++;
156 len--;
157 j = v1 ? ((i & 0x10) ? '!' : ' ') :
158 ((i & 0x10) ? (cmd ? '+' : '-') : (cmd ? '^' : 'v'));
159 if (!(i & 1)) {
160 /*
161 * Info frame
162 */
163 printf(" I%u%u%c",(i >> 5) & 7,(i >> 1) & 7,j);
164 } else if (i & 2) {
165 /*
166 * U frame
167 */
168 switch (i & (~0x10)) {
169 case 0x03:
170 printf(" UI%c",j);
171 break;
172 case 0x2f:
173 printf(" SABM%c",j);
174 break;
175 case 0x43:
176 printf(" DISC%c",j);
177 break;
178 case 0x0f:
179 printf(" DM%c",j);
180 break;
181 case 0x63:
182 printf(" UA%c",j);
183 break;
184 case 0x87:
185 printf(" FRMR%c",j);
186 break;
187 default:
188 printf(" unknown U (0x%x)%c",i & (~0x10),j);
189 break;
190 }
191 } else {
192 /*
193 * supervisory
194 */
195 switch (i & 0xf) {
196 case 0x1:
197 printf(" RR%u%c",(i >> 5) & 7,j);
198 break;
199 case 0x5:
200 printf(" RNR%u%c",(i >> 5) & 7,j);
201 break;
202 case 0x9:
203 printf(" REJ%u%c",(i >> 5) & 7,j);
204 break;
205 default:
206 printf(" unknown S (0x%x)%u%c", i & 0xf,
207 (i >> 5) & 7, j);
208 break;
209 }
210 }
211 if (!len) {
212 printf("\n");
213 return;
214 }
215 printf(" pid=%02X\n", *bp++);
216 len--;
217 j = 0;
218 while (len) {
219 i = *bp++;
220 if ((i >= 32) && (i < 128))
221 printf("%c",i);
222 else if (i == 13) {
223 if (j)
224 printf("\n");
225 j = 0;
226 } else
227 printf(".");
228 if (i >= 32)
229 j = 1;
230 len--;
231 }
232 if (j)
233 printf("\n");
234 }
235
236 /* ---------------------------------------------------------------------- */
237
238 static void restore_ifflags(int signum)
239 {
240 if (ioctl(fdif, SIOCSIFFLAGS, &ifr) < 0)
241 die("ioctl SIOCSIFFLAGS");
242 close(fdif);
243 close(fdpty);
244 exit(0);
245 }
246
247 /* --------------------------------------------------------------------- */
248
249 #define KISS_FEND ((unsigned char)0300)
250 #define KISS_FESC ((unsigned char)0333)
251 #define KISS_TFEND ((unsigned char)0334)
252 #define KISS_TFESC ((unsigned char)0335)
253
254 #define KISS_CMD_DATA 0
255 #define KISS_CMD_TXDELAY 1
256 #define KISS_CMD_PPERSIST 2
257 #define KISS_CMD_SLOTTIME 3
258 #define KISS_CMD_TXTAIL 4
259 #define KISS_CMD_FULLDUP 5
260
261 #define KISS_HUNT 0
262 #define KISS_RX 1
263 #define KISS_ESCAPED 2
264
265 /* --------------------------------------------------------------------- */
266
267 static void kiss_overflow(void)
268 {
269 if (verbose)
270 printf("KISS: packet overflow\n");
271 }
272
273 static void kiss_bad_escape(void)
274 {
275 if (verbose)
276 printf("KISS: bad escape sequence\n");
277 }
278
279 static void display_kiss_packet(char *pfx, unsigned char *pkt,
280 unsigned int pktlen)
281 {
282 if (!verbose)
283 return;
284 switch (*pkt) {
285 case KISS_CMD_DATA:
286 printf("%s: ", pfx);
287 display_packet(pkt+1, pktlen-1);
288 break;
289
290 case KISS_CMD_TXDELAY:
291 printf("%s: txdelay = %dms\n", pfx, (int)pkt[1] * 10);
292 break;
293
294 case KISS_CMD_PPERSIST:
295 printf("%s: p persistence = %d\n", pfx, pkt[1]);
296 break;
297
298 case KISS_CMD_SLOTTIME:
299 printf("%s: slottime = %dms\n", pfx, (int)pkt[1] * 10);
300 break;
301
302 case KISS_CMD_TXTAIL:
303 printf("%s: txtail = %dms\n", pfx, (int)pkt[1] * 10);
304 break;
305
306 case KISS_CMD_FULLDUP:
307 printf("%s: %sduplex\n", pfx, pkt[1] ? "full" : "half");
308 break;
309
310 default:
311 printf("%s: unknown frame type 0x%02x, length %d\n", pfx,
312 *pkt, pktlen);
313 }
314 }
315
316 static void kiss_packet(int fdif, char *addr,
317 unsigned char *pkt, unsigned int pktlen)
318 {
319 struct sockaddr to;
320 int i;
321
322 if (pktlen < 2)
323 return;
324 display_kiss_packet("KISS", pkt, pktlen);
325 strncpy(to.sa_data, addr, sizeof(to.sa_data));
326 i = sendto(fdif, pkt, pktlen, 0, &to, sizeof(to));
327 if (i >= 0)
328 return;
329 if (errno == EMSGSIZE) {
330 if (verbose)
331 printf("sendto: %s: packet (size %d) too "
332 "long\n", addr, pktlen-1);
333 return;
334 }
335 if (errno == EWOULDBLOCK) {
336 if (verbose)
337 printf("sendto: %s: busy\n", addr);
338 return;
339 }
340 die("sendto");
341 }
342
343 /* --------------------------------------------------------------------- */
344
345 static int doio(int fdif, int fdpty, char *ifaddr)
346 {
347 unsigned char ibuf[2048];
348 unsigned char *bp;
349 unsigned char pktbuf[2048];
350 unsigned char *pktptr = pktbuf;
351 unsigned char pktstate = KISS_HUNT;
352 unsigned char obuf[16384];
353 unsigned int ob_wp = 0, ob_rp = 0, ob_wpx;
354 int i;
355 fd_set rmask, wmask;
356 struct sockaddr from;
357 socklen_t from_len;
358
359 #define ADD_CHAR(c) \
360 obuf[ob_wpx] = c; \
361 ob_wpx = (ob_wpx + 1) % sizeof(obuf); \
362 if (ob_wpx == ob_rp) goto kissencerr;
363
364 #define ADD_KISSCHAR(c) \
365 if (((c) & 0xff) == KISS_FEND) \
366 { ADD_CHAR(KISS_FESC); ADD_CHAR(KISS_TFEND); } \
367 else if (((c) & 0xff) == KISS_FESC) \
368 { ADD_CHAR(KISS_FESC); ADD_CHAR(KISS_TFESC); } \
369 else { ADD_CHAR(c); }
370
371 for (;;) {
372 FD_ZERO(&rmask);
373 FD_ZERO(&wmask);
374 FD_SET(fdif, &rmask);
375 FD_SET(fdpty, &rmask);
376 if (ob_rp != ob_wp)
377 FD_SET(fdpty, &wmask);
378 i = select((fdif > fdpty) ? fdif+1 : fdpty+1, &rmask, &wmask,
379 NULL, NULL);
380 if (i < 0)
381 die("select");
382 if (FD_ISSET(fdpty, &wmask)) {
383 if (ob_rp > ob_wp)
384 i = write(fdpty, obuf+ob_rp,
385 sizeof(obuf)-ob_rp);
386 else
387 i = write(fdpty, obuf+ob_rp, ob_wp - ob_rp);
388 if (i < 0)
389 die("write");
390 ob_rp = (ob_rp + i) % sizeof(obuf);
391 }
392 if (FD_ISSET(fdpty, &rmask)) {
393 i = read(fdpty, bp = ibuf, sizeof(ibuf));
394 if (i < 0) {
395 if (errno != EIO)
396 die("read");
397 return 0;
398 }
399 for (; i > 0; i--, bp++) {
400 switch (pktstate) {
401 default:
402 case KISS_HUNT:
403 if (*bp != KISS_FEND)
404 break;
405 pktptr = pktbuf;
406 pktstate = KISS_RX;
407 break;
408
409 case KISS_RX:
410 if (*bp == KISS_FESC) {
411 pktstate = KISS_ESCAPED;
412 break;
413 }
414 if (*bp == KISS_FEND) {
415 kiss_packet(fdif, ifaddr,
416 pktbuf,
417 pktptr - pktbuf);
418 pktptr = pktbuf;
419 break;
420 }
421 if (pktptr >= pktbuf+sizeof(pktbuf)) {
422 kiss_overflow();
423 pktstate = KISS_HUNT;
424 break;
425 }
426 *pktptr++ = *bp;
427 break;
428
429 case KISS_ESCAPED:
430 if (pktptr >= pktbuf+sizeof(pktbuf)) {
431 kiss_overflow();
432 pktstate = KISS_HUNT;
433 break;
434 }
435 if (*bp == KISS_TFESC)
436 *pktptr++ = KISS_FESC;
437 else if (*bp == KISS_TFEND)
438 *pktptr++ = KISS_FEND;
439 else {
440 kiss_bad_escape();
441 pktstate = KISS_HUNT;
442 break;
443 }
444 pktstate = KISS_RX;
445 break;
446 }
447 }
448 }
449 if (FD_ISSET(fdif, &rmask)) {
450 from_len = sizeof(from);
451 i = recvfrom(fdif, bp = ibuf, sizeof(ibuf), 0, &from,
452 &from_len);
453 if (i < 0) {
454 if (errno == EWOULDBLOCK)
455 continue;
456 die("recvfrom");
457 }
458 if (verbose)
459 display_kiss_packet(from.sa_data, ibuf, i);
460 ob_wpx = ob_wp;
461 ADD_CHAR(KISS_FEND);
462 for (; i > 0; i--, bp++) {
463 ADD_KISSCHAR(*bp);
464 }
465 ADD_CHAR(KISS_FEND);
466 ob_wp = ob_wpx;
467 }
468 continue;
469 kissencerr:
470 if (verbose)
471 printf("KISS: Encoder out of memory\n");
472 }
473 #undef ADD_CHAR
474 #undef ADD_KISSCHAR
475 }
476
477 /* --------------------------------------------------------------------- */
478
479 int main(int argc, char *argv[])
480 {
481 struct ifreq ifr_new;
482 struct sockaddr sa;
483 char *name_iface = "bc0";
484 char *name_pname = NULL;
485 char slavename[PATH_MAX];
486 char *master_name;
487 struct termios termios;
488 int c;
489 int errflg = 0;
490 int symlnk = 0;
491 int symlnkforce = 0;
492 short if_newflags = 0;
493 int proto = htons(ETH_P_AX25);
494
495 progname = argv[0];
496 while ((c = getopt(argc, argv, "sfzvai:")) != EOF) {
497 switch (c) {
498 case 's':
499 symlnk = 1;
500 break;
501 case 'f':
502 symlnkforce = 1;
503 break;
504 case 'i':
505 name_iface = optarg;
506 break;
507 case 'z':
508 if_newflags |= IFF_PROMISC;
509 break;
510 case 'v':
511 verbose++;
512 break;
513 case 'a':
514 proto = htons(ETH_P_ALL);
515 break;
516 default:
517 errflg++;
518 break;
519 }
520 }
521 if (argc > optind)
522 name_pname = argv[optind];
523 else
524 errflg++;
525 if (errflg) {
526 fprintf(stderr, "usage: %s [-s] [-f] [-i iface] "
527 "[-z] [-v] ptyname\n", progname);
528 exit(1);
529 }
530 openlog(progname, LOG_PID, LOG_DAEMON);
531 if (symlnk) {
532 int fdtty;
533
534 if (openpty(&fdpty, &fdtty, slavename, NULL, NULL)) {
535 fprintf(stderr, "%s: out of pseudoterminals\n",
536 progname);
537 exit(1);
538 }
539 close(fdtty);
540 fcntl(fdpty, F_SETFL, fcntl(fdpty, F_GETFL, 0) | O_NONBLOCK);
541 if (symlnkforce)
542 unlink(name_pname);
543 if (symlink(slavename, name_pname))
544 perror("symlink");
545 /* Users await the slave pty to be referenced in the 2nd line */
546 printf("Awaiting client connects on\n%s\n", slavename);
547 slavename[5] = 'p';
548 master_name = slavename;
549 } else {
550 fdpty = open(name_pname, O_RDWR | O_NOCTTY | O_NONBLOCK);
551 if (fdpty < 0) {
552 fprintf(stderr, "%s: cannot open \"%s\"\n", progname,
553 name_pname);
554 exit(1);
555 }
556 if (!strcmp("/dev/ptmx", name_pname))
557 i_am_unix98_pty_master = 1;
558 master_name = name_pname;
559 }
560 fdif = socket(PF_INET, SOCK_PACKET, proto);
561 if (fdif < 0)
562 die("socket");
563 memset(&sa, 0, sizeof(struct sockaddr));
564 memcpy(sa.sa_data, name_iface, sizeof(sa.sa_data));
565 sa.sa_family = AF_INET;
566 if (bind(fdif, &sa, sizeof(struct sockaddr)) < 0)
567 die("bind");
568 memcpy(ifr.ifr_name, name_iface, IFNAMSIZ);
569 if (ioctl(fdif, SIOCGIFFLAGS, &ifr) < 0)
570 die("ioctl SIOCGIFFLAGS");
571 ifr_new = ifr;
572 ifr_new.ifr_flags |= if_newflags;
573 if (ioctl(fdif, SIOCSIFFLAGS, &ifr_new) < 0)
574 die("ioctl SIOCSIFFLAGS");
575 if (i_am_unix98_pty_master) {
576 /* get name of pts-device */
577 namepts = ptsname(fdpty);
578 if (namepts == NULL) {
579 fprintf(stderr, "%s: Cannot get name of pts-device.\n", progname);
580 exit (1);
581 }
582 /* unlock pts-device */
583 if (unlockpt(fdpty) == -1) {
584 fprintf(stderr, "%s: Cannot unlock pts-device %s\n", progname, namepts);
585 exit (1);
586 }
587 /* Users await the slave pty to be referenced in the 2nd line */
588 printf("Awaiting client connects on\n%s\n", namepts);
589 if (!verbose) {
590 fflush(stdout);
591 fflush(stderr);
592 close(0);
593 close(1);
594 close(2);
595 }
596 }
597 signal(SIGHUP, restore_ifflags);
598 signal(SIGINT, restore_ifflags);
599 signal(SIGTERM, restore_ifflags);
600 signal(SIGQUIT, restore_ifflags);
601 signal(SIGUSR1, restore_ifflags);
602 signal(SIGUSR2, restore_ifflags);
603
604 for (;;) {
605 if (tcgetattr(fdpty, &termios))
606 die("tcgetattr");
607 termios.c_iflag = IGNBRK;
608 termios.c_oflag = 0;
609 termios.c_lflag = 0;
610 termios.c_cflag &= ~(CSIZE|CSTOPB|PARENB|HUPCL|CRTSCTS);
611 termios.c_cflag |= CS8|CREAD|CLOCAL;
612 if (tcsetattr(fdpty, TCSANOW, &termios))
613 die("tsgetattr");
614 if (doio(fdif, fdpty, name_iface))
615 break;
616 if (i_am_unix98_pty_master) {
617 if (verbose)
618 printf("%s: Trying to poll port ptmx (slave %s).\nWaiting 30s...\n", progname, namepts);
619 syslog(LOG_WARNING, "Trying to poll port ptmx (slave %s). Waiting 30s...\n", namepts);
620 sleep(30);
621 } else {
622 /*
623 * try to reopen master
624 */
625 if (verbose)
626 printf("reopening master tty: %s\n", master_name);
627 close(fdpty);
628
629 fdpty = open(master_name,
630 O_RDWR | O_NOCTTY | O_NONBLOCK);
631 if (fdpty < 0) {
632 fprintf(stderr, "%s: cannot reopen \"%s\"\n", progname,
633 master_name);
634 exit(1);
635 }
636 }
637 }
638
639 restore_ifflags(0);
640 exit(0);
641 }
642
643 /* --------------------------------------------------------------------- */