Debian Codegraph @

ax25ipd/io.c

raw ◀ ▶
2 Debian patches touch this file
1 /* io.c All base I/O routines live here
2 *
3 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
4 * This software may be freely used, distributed, or modified, providing
5 * this header is not removed.
6 *
7 * This is the only module that knows about base level UNIX/SunOS I/O
8 * This is also the key dispatching module, so it knows about a lot more
9 * than just I/O stuff.
10 */
11 #define _XOPEN_SOURCE
12 #define _XOPEN_SOURCE_EXTENDED
13
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <memory.h>
17 #include <netdb.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <syslog.h>
•22 #include <termios.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/socket.h>
32
33 #include "ax25ipd.h"
34
•35 static struct termios nterm;
36
37 int ttyfd = -1;
38 static int udpsock = -1;
39 static int sock = -1;
40 static struct sockaddr_in udpbind;
41 static struct sockaddr_in to;
42 static struct sockaddr_in from;
43 static socklen_t fromlen;
44
45 static time_t last_bc_time;
46
47 int ttyfd_bpq = 0;
48
49 /*
50 * I/O modes for the io_error routine
51 */
52 #define READ_MSG 0x00
53 #define SEND_MSG 0x01
54
55 #define IP_MODE 0x10
56 #define UDP_MODE 0x20
57 #define TTY_MODE 0x30
58
59 #ifndef FNDELAY
60 #define FNDELAY O_NDELAY
61 #endif
62
63 /*
64 * process an I/O error; return true if a retry is needed
65 *
66 * oops - the error flag; < 0 indicates a problem
67 * buf - the data in question
68 * bufsize - the size of the data buffer
69 * dir - the direction; input or output
70 * mode - the fd on which we got the error
71 * where - line in the code where this function was called
72 */
73 static int io_error( int oops, unsigned char *buf, int bufsize, int dir,
74 int mode, int where)
75 {
76
77 /* if (oops >= 0)
78 return 0; */ /* do we have an error ? */
79 /* dl9sau: nobody has set fd's to O_NONBLOCK.
80 * thus EAGAIN (below) or EWOULDBLOCK are never be set.
81 * Has someone removed this behaviour previously?
82 * Anyway, in the current implementation, with blocking
83 * read/writes, a read or write of 0 bytes means EOF,
84 * for e.g. if the attached tty is closed.
85 * We have to exit then. We've currentlsy no mechanism
86 * for regulary reconnects.
87 */
88 if (oops > 0)
89 return 0; /* do we have an error ? */
90
91 if (oops == 0) {
92 if (dir == READ_MSG && oops != TTY_MODE /* && != TCP_MODE, if we'd implement this */ )
93 return 0;
•94 fprintf(stderr, "Close event on mode 0x%2.2x (during %s). LINE %d. Terminating normally.\n", mode, (dir == READ_MSG ? "READ" : "WRITE"), where);
95 exit(1);
96 }
97
98 #ifdef EAGAIN
99 if (errno == EAGAIN) {
100 #ifdef notdef
101 /* select() said that data is available, but recvfrom sais
102 * EAGAIN - i really do not know what's the sense in this.. */
103 if (dir == READ_MSG && oops != TTY_MODE /* && != TCP_MODE, if we'd implement this */ )
104 return 0;
105 perror("System 5 I/O error!");
106 fprintf(stderr, "A System 5 style I/O error was detected. This rogram requires BSD 4.2\n");
107 fprintf(stderr, "behaviour. This is probably a result of compile-time environment.\n");
108 fprintf(stderr, "Mode 0x%2.2x, LINE: %d. During %s\n", mode, where, (dir == READ_MSG ? "READ" : "WRITE"));
109 exit(3);
110 #else
111 int ret = 0;
112 if (dir == READ_MSG) {
113 LOGL4("read / recv returned -1 EAGAIN\n");
114 ret = 0;
115 } else if (dir == SEND_MSG) {
116 LOGL4("write / send returned -1 EAGAIN, sleeping and retrying!\n");
117 usleep(100000);
118 ret = 1;
119 }
120 return ret;
121 #endif
122 }
123 #endif
124
125 if (dir == READ_MSG) {
126 if (errno == EINTR)
127 return 0; /* never retry read */
128 if (errno == EWOULDBLOCK) {
129 LOGL4("READ would block (?!), sleeping and retrying!\n");
130 usleep(100000); /* sleep a bit */
131 return 1; /* and retry */
132 }
133 if (mode == IP_MODE) {
134 perror("reading from raw ip socket");
135 exit(2);
136 } else if (mode == UDP_MODE) {
137 perror("reading from udp socket");
138 exit(2);
139 } else if (mode == TTY_MODE) {
140 perror("reading from tty device");
141 exit(2);
142 } else {
143 perror("reading from unknown I/O");
144 exit(2);
145 }
146 } else if (dir == SEND_MSG) {
147 if (errno == EINTR)
148 return 1; /* always retry on writes */
149 if (mode == IP_MODE) {
150 if (errno == EMSGSIZE) { /* msg too big, drop it */
151 perror("message dropped on raw ip socket");
152 fprintf (stderr, "message was %d bytes long.\n", bufsize);
153 return 0;
154 }
155 if (errno == ENETDOWN || errno == ENETRESET ||
156 errno == ENETUNREACH || errno == EHOSTDOWN ||
157 errno == EHOSTUNREACH || errno == ENONET ||
158 errno == EPERM) {
159 /* host closed his axip receiver or dropped the line */
160 perror("error after sending on to axip partner. ignoring.");
161 LOGL4("error after sending on to axip partner: %s; ignoring!\n", strerror(errno));
162
163 return 0;
164 }
165 if (errno == ENOBUFS) { /* congestion; sleep + retry */
166 LOGL4("send congestion on raw ip, sleeping and retrying!\n");
167 usleep(100000);
168 return 1;
169 }
170 if (errno == EWOULDBLOCK) {
171 LOGL4("send on raw ip would block, sleeping and retrying!\n");
172 usleep(100000); /* sleep a bit */
173 return 1; /* and retry */
174 }
175 perror("writing to raw ip socket");
176 exit(2);
177 } else if (mode == UDP_MODE) {
178 if (errno == EMSGSIZE) { /* msg too big, drop it */
179 perror("message dropped on udp socket");
180 fprintf(stderr, "message was %d bytes long.\n", bufsize);
181 return 0;
182 }
183 if (errno == ENETDOWN || errno == ENETRESET ||
184 errno == ENETUNREACH || errno == EHOSTDOWN ||
185 errno == EHOSTUNREACH || errno == ENONET ||
186 errno == EPERM) {
187 /* host closed his axudp receiver or dropped the line */
188 perror("error after sending to axudp partner. ignoring.");
189 LOGL4("error after sending to axudp partner: %s; ignoring!\n", strerror(errno));
190
191 return 0;
192 }
193 if (errno == ENOBUFS) { /* congestion; sleep + retry */
194 LOGL4("send congestion on udp, sleeping and retrying!\n");
195 usleep(100000);
196 return 1;
197 }
198 if (errno == EWOULDBLOCK) {
199 LOGL4("send on udp would block, sleeping and retrying!\n");
200 usleep(100000); /* sleep a bit */
201 return 1; /* and retry */
202 }
203 perror("writing to udp socket");
204 exit(2);
205 } else if (mode == TTY_MODE) {
206 if (errno == EWOULDBLOCK) {
207 LOGL4("write to tty would block, sleeping and retrying!\n");
208 usleep(100000); /* sleep a bit */
209 return 1; /* and retry */
210 }
211 perror("writing to tty device");
212 exit(2);
213 } else {
214 perror("writing to unknown I/O");
215 exit(2);
216 }
217 } else {
218 perror("Unknown direction and I/O");
219 exit(2);
220 }
221 return 0;
222 }
223
224 /*
225 * Initialize the io variables
226 */
227
228 void io_init(void)
229 {
230
231 /*
232 * Close the file descriptors if they are open. The idea is that we
233 * will be able to support a re-initialization if sent a SIGHUP.
234 */
235
236 if (ttyfd >= 0) {
237 close(ttyfd);
238 ttyfd = -1;
239 }
240
241 if (sock >= 0) {
242 close(sock);
243 sock = -1;
244 }
245
246 if (udpsock >= 0) {
247 close(udpsock);
248 udpsock = -1;
249 }
250
251 /*
252 * The memset is not strictly required - it simply zeros out the
253 * address structure. Since both to and from are static, they are
254 * already clear.
255 */
256 memset(&to, 0, sizeof(struct sockaddr));
257 to.sin_family = AF_INET;
258
259 memset(&from, 0, sizeof(struct sockaddr));
260 from.sin_family = AF_INET;
261
262 memset(&udpbind, 0, sizeof(struct sockaddr));
263 udpbind.sin_family = AF_INET;
264 }
265
266 /*
267 * open and initialize the IO interfaces
268 */
269
270 void io_open(void)
271 {
272 int baudrate;
273 int i_am_unix98_pty_master = 0; /* unix98 ptmx support */
274 char *namepts = NULL; /* name of the unix98 pts slave, which
275 * the client has to use */
276
277 if (ip_mode) {
278 sock = socket(AF_INET, SOCK_RAW, IPPROTO_AX25);
279 if (sock < 0) {
280 perror("opening raw socket");
281 exit(1);
282 }
283 if (fcntl(sock, F_SETFL, FNDELAY) < 0) {
284 perror("setting non-blocking I/O on raw socket");
285 exit(1);
286 }
287 }
288
289 if (udp_mode) {
290 udpsock = socket(AF_INET, SOCK_DGRAM, 0);
291 if (udpsock < 0) {
292 perror("opening udp socket");
293 exit(1);
294 }
295 if (fcntl(udpsock, F_SETFL, FNDELAY) < 0) {
296 perror("setting non-blocking I/O on UDP socket");
297 exit(1);
298 }
299 /*
300 * Ok, the udp socket is open. Now express our interest in receiving
301 * data destined for a particular socket.
302 */
303 udpbind.sin_addr.s_addr = INADDR_ANY;
304 udpbind.sin_port = my_udp;
305 if (bind(udpsock, (struct sockaddr *) &udpbind, sizeof udpbind) < 0) {
306 perror("binding udp socket");
307 exit(1);
308 }
309 }
310
311 if (!strcmp("/dev/ptmx", ttydevice))
312 i_am_unix98_pty_master = 1;
313
314 ttyfd = ((ttyfd_bpq = (strchr(ttydevice, '/') ? 0 : 1)) ? open_ethertap(ttydevice) : open(ttydevice, O_RDWR, 0));
315 if (ttyfd < 0) {
316 perror("opening tty device");
317 exit(1);
318 }
319 if (fcntl(ttyfd, F_SETFL, FNDELAY) < 0) {
320 perror("setting non-blocking I/O on tty device");
321 exit(1);
322 }
323
324 if (i_am_unix98_pty_master) {
325 /* get name of pts-device */
326 namepts = ptsname(ttyfd);
327 if (namepts == NULL) {
328 perror("Cannot get name of pts-device.");
329 exit(1);
330 }
331 /* unlock pts-device */
332 if (unlockpt(ttyfd) == -1) {
333 perror("Cannot unlock pts-device.");
334 exit(1);
335 }
336 }
337
338 if (ttyfd_bpq) {
339 set_bpq_dev_call_and_up(ttydevice);
340 goto behind_normal_tty;
341 }
•342 if (tcgetattr(ttyfd, &nterm) < 0) {
343 perror("fetching tty device parameters");
344 exit(1);
345 }
346
347 if (ttyspeed == 50)
348 baudrate = B50;
349 else if (ttyspeed == 50)
350 baudrate = B50;
351 else if (ttyspeed == 75)
352 baudrate = B75;
353 else if (ttyspeed == 110)
354 baudrate = B110;
355 else if (ttyspeed == 134)
356 baudrate = B134;
357 else if (ttyspeed == 150)
358 baudrate = B150;
359 else if (ttyspeed == 200)
360 baudrate = B200;
361 else if (ttyspeed == 300)
362 baudrate = B300;
363 else if (ttyspeed == 600)
364 baudrate = B600;
365 else if (ttyspeed == 1200)
366 baudrate = B1200;
367 else if (ttyspeed == 1800)
368 baudrate = B1800;
369 else if (ttyspeed == 2400)
370 baudrate = B2400;
371 else if (ttyspeed == 4800)
372 baudrate = B4800;
373 else if (ttyspeed == 9600)
374 baudrate = B9600;
375 #ifdef B19200
376 else if (ttyspeed == 19200)
377 baudrate = B19200;
378 #else
379 #ifdef EXTA
380 else if (ttyspeed == 19200)
381 baudrate = EXTA;
382 #endif /* EXTA */
383 #endif /* B19200 */
384 #ifdef B38400
385 else if (ttyspeed == 38400)
386 baudrate = B38400;
387 #else
388 #ifdef EXTB
389 else if (ttyspeed == 38400)
390 baudrate = EXTB;
391 #endif /* EXTB */
392 #endif /* B38400 */
393 #ifdef B57600
394 else if (ttyspeed == 57600)
395 baudrate = B57600;
396 #endif /* B57600 */
397 #ifdef B76800 /* SPARC-specific */
398 else if (ttyspeed == 76800)
399 baudrate = B76800;
400 #endif /* B76800 */
401 #ifdef B115200
402 else if (ttyspeed == 115200)
403 baudrate = B115200;
404 #endif /* B115200 */
405 #ifdef B153600 /* SPARC-specific */
406 else if (ttyspeed == 153600)
407 baudrate = B153600;
408 #endif /* B153600 */
409 #ifdef B230400
410 else if (ttyspeed == 230400)
411 baudrate = B230400;
412 #endif /* B230400 */
413 #ifdef B307200 /* SPARC-specific */
414 else if (ttyspeed == 307200)
415 baudrate = B307200;
416 #endif /* B307200 */
417 #ifdef B460800
418 else if (ttyspeed == 460800)
419 baudrate = B460800;
420 #endif /* B460800 */
421 #ifdef B500000
422 else if (ttyspeed == 500000)
423 baudrate = B500000;
424 #endif /* B500000 */
425 #ifdef B576000
426 else if (ttyspeed == 576000)
427 baudrate = B576000;
428 #endif /* B576000 */
429 #ifdef B614400 /* SPARC-specific */
430 else if (ttyspeed == 614400)
431 baudrate = B614400;
432 #endif /* B614400 */
433 #ifdef B921600 /* SPARC-specific */
434 else if (ttyspeed == 921600)
435 baudrate = B921600;
436 #endif /* B921600 */
437 #ifdef B1000000
438 else if (ttyspeed == 1000000)
439 baudrate = B1000000;
440 #endif /* B1000000 */
441 #ifdef B1152000
442 else if (ttyspeed == 1152000)
443 baudrate = B1152000;
444 #endif /* B1152000 */
445 #ifdef B1500000
446 else if (ttyspeed == 1500000)
447 baudrate = B1500000;
448 #endif /* B1500000 */
449 #ifdef B2000000
450 else if (ttyspeed == 2000000)
451 baudrate = B2000000;
452 #endif /* B2000000 */
453 #ifdef B2500000
454 else if (ttyspeed == 2500000)
455 baudrate = B2500000;
456 #endif /* B2500000 */
457 #ifdef B3000000
458 else if (ttyspeed == 3000000)
459 baudrate = B3000000;
460 #endif /* B3000000 */
461 #ifdef B3500000
462 else if (ttyspeed == 3500000)
463 baudrate = B3500000;
464 #endif /* B3500000 */
465 #ifdef B4000000
466 else if (ttyspeed == 4000000)
467 baudrate = B4000000;
468 #endif /* B4000000 */
469 else
470 baudrate = B9600;
471
472 nterm.c_iflag = 0;
473 nterm.c_oflag = 0;
474 nterm.c_cflag = baudrate | CS8 | CREAD | CLOCAL;
475 nterm.c_lflag = 0;
476 nterm.c_cc[VMIN] = 0;
477 nterm.c_cc[VTIME] = 0;
478
•479 if (tcsetattr(ttyfd, TCSADRAIN, &nterm) < 0) {
480 perror("setting tty device parameters");
481 exit(1);
482 }
483
484 if (digi)
485 send_params();
486
487 if (i_am_unix98_pty_master) {
488 /* Users await the slave pty to be referenced in the last line */
489 printf("Awaiting client connects on\n%s\n", namepts);
490 syslog(LOG_INFO, "Bound to master pty /dev/ptmx with slave pty %s\n", namepts);
491 }
492
493 behind_normal_tty:
494
495 last_bc_time = 0; /* force immediate id */
496 }
497
498 /*
499 * Start up and run the I/O mechanisms.
500 * run in a loop, using the select call to handle input.
501 */
502
503 void io_start(void) {
504 int n, nb, hdr_len;
505 fd_set readfds;
506 unsigned char buf[MAX_FRAME];
507 struct timeval wait;
508 struct iphdr *ipptr;
509 time_t now;
510
511 for (;;) {
512
513 if ((bc_interval > 0) && digi) {
514 now = time(NULL);
515 if (last_bc_time + bc_interval < now) {
516 last_bc_time = now;
517 LOGL4("iostart: BEACON\n");
518 do_beacon();
519 }
520 }
521
522 wait.tv_sec = 10; /* lets us keep the beacon going */
523 wait.tv_usec = 0;
524
525 FD_ZERO(&readfds);
526
527 FD_SET(ttyfd, &readfds);
528
529 if (ip_mode) {
530 FD_SET(sock, &readfds);
531 }
532
533 if (udp_mode) {
534 FD_SET(udpsock, &readfds);
535 }
536
537 nb = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &wait);
538
539 if (nb < 0) {
540 if (errno == EINTR)
541 continue; /* Ignore */
542 perror("select");
543 exit(1);
544 }
545
546 if (nb == 0) {
547 fflush(stdout);
548 fflush(stderr);
549 /* just so we go back to the top of the loop! */
550 continue;
551 }
552
553 if (FD_ISSET(ttyfd, &readfds)) {
554 do {
555 n = read(ttyfd, buf, MAX_FRAME);
556 }
557 while (io_error(n, buf, n, READ_MSG, TTY_MODE, __LINE__));
558 LOGL4("ttydata l=%d\n", n);
559 if (n > 0) {
560 if (!ttyfd_bpq) {
561 assemble_kiss(buf, n);
562 } else {
563 /* no crc but MAC header on bpqether */
564 if (receive_bpq(buf, n) < 0) {
565 goto out_ttyfd;
566 }
567 }
568 }
569
570 /*
571 * If we are in "beacon after" mode, reset the "last_bc_time" each time
572 * we hear something on the channel.
573 */
574 if (!bc_every)
575 last_bc_time = time(NULL);
576 }
577 out_ttyfd:
578
579 if (udp_mode) {
580 if (FD_ISSET(udpsock, &readfds)) {
581 do {
582 fromlen = sizeof from;
583 n = recvfrom(udpsock, buf, MAX_FRAME, 0, (struct sockaddr *) &from, &fromlen);
584 }
585 while (io_error(n, buf, n, READ_MSG, UDP_MODE, __LINE__));
586 LOGL4("udpdata from=%s port=%d l=%d\n",
587 inet_ntoa(from. sin_addr),
588 ntohs(from. sin_port), n);
589 stats.udp_in++;
590 if (n > 0)
591 from_ip(buf, n);
592 }
593 }
594 /* if udp_mode */
595 if (ip_mode) {
596 if (FD_ISSET(sock, &readfds)) {
597 do {
598 fromlen = sizeof from;
599 n = recvfrom(sock, buf, MAX_FRAME, 0, (struct sockaddr *) &from, &fromlen);
600 }
601 while (io_error(n, buf, n, READ_MSG, IP_MODE, __LINE__));
602 ipptr = (struct iphdr *) buf;
603 hdr_len = 4 * ipptr-> ihl;
604 LOGL4("ipdata from=%s l=%d, hl=%d\n",
605 inet_ntoa(from. sin_addr), n, hdr_len);
606 stats.ip_in++;
607 if (n > hdr_len)
608 from_ip(buf + hdr_len, n - hdr_len);
609 }
610 }
611 /* if ip_mode */
612 } /* for forever */
613 }
614
615 /* Send an IP frame */
616
617 void send_ip(unsigned char *buf, int l, unsigned char *targetip)
618 {
619 int n;
620
621 if (l <= 0)
622 return;
623 memcpy(&to.sin_addr, targetip, 4);
624 memcpy(&to.sin_port, &targetip[4], 2);
625 LOGL4("sendipdata to=%s %s %d l=%d\n", inet_ntoa(to. sin_addr),
626 to.sin_port ? "udp" : "ip", ntohs(to.sin_port), l);
627 if (to.sin_port) {
628 if (udp_mode) {
629 stats.udp_out++;
630 do {
631 n = sendto(udpsock, buf, l, 0, (struct sockaddr *) &to, sizeof to);
632 }
633 while (io_error(n, buf, l, SEND_MSG, UDP_MODE, __LINE__));
634 }
635 } else {
636 if (ip_mode) {
637 stats.ip_out++;
638 do {
639 n = sendto(sock, buf, l, 0, (struct sockaddr *) &to, sizeof to);
640 }
641 while (io_error(n, buf, l, SEND_MSG, IP_MODE, __LINE__));
642 }
643 }
644 }
645
646 /* Send a kiss frame */
647
648 void send_tty(unsigned char *buf, int l)
649 {
650 int n;
651 unsigned char *p;
652 int nc;
653
654 if (l <= 0)
655 return;
656 LOGL4("sendttydata l=%d\tsent: ", l);
657 stats.kiss_out++;
658
659 p = buf;
660 nc = l;
661 n = 0;
662
663 /*
664 * we have to loop around here because each call to write may write a few
665 * characters. So we simply increment the buffer each time around. If
666 * we ever write no characters, we should get an error code, and io_error
667 * will sleep for a fraction of a second. Note that we are keyed to
668 * the BSD 4.2 behaviour... the Sys 5 non-blocking I/O may or may not work
669 * in this loop. We may detect system 5 behaviour (this would result from
670 * compile-time options) by having io_error barf when it detects an EAGAIN
671 * error code.
672 */
673 do {
674 if ((n > 0) && (n < nc)) { /* did we put only write a bit? */
675 p += n; /* point to the new data */
676 nc -= n; /* drop the length */
677 }
678 n = write(ttyfd, p, nc);
679 if (n > 0) {
680 if (n != nc) {
681 LOGL4("%d ", n); /* no-one said loglevel 4 */
682 } else {
683 LOGL4("%d\n", n); /* was efficient!!! */
684 }
685 }
686 }
687 while (((n > 0) && (n < nc)) || (io_error(n, p, nc, SEND_MSG, TTY_MODE, __LINE__)));
688 }