]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-NeXT.c
Add extra baud rates >= 1Mbaud.
[ppp.git] / pppd / sys-NeXT.c
1 /*
2  * sys-next.c - System-dependent procedures for setting up
3  * PPP interfaces on NeXT 3.2/3.3  systems
4  *
5  * Copyright (c) 1994 Philippe-Andre Prindeville.
6  * All rights reserved.
7  *
8  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  *
22  * 3. The name "Carnegie Mellon University" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For permission or any legal
25  *    details, please contact
26  *      Office of Technology Transfer
27  *      Carnegie Mellon University
28  *      5000 Forbes Avenue
29  *      Pittsburgh, PA  15213-3890
30  *      (412) 268-4387, fax: (412) 268-7395
31  *      tech-transfer@andrew.cmu.edu
32  *
33  * 4. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by Computing Services
36  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
37  *
38  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
39  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
40  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
41  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
42  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
43  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
44  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45  */
46
47 #define RCSID   "$Id: sys-NeXT.c,v 1.21 2002/12/04 23:03:32 paulus Exp $"
48
49 #include <stdio.h>
50 #include <termios.h>
51 #include <utmp.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <libc.h>
55 #include <strings.h>
56 #include <sys/types.h>
57 #include <sys/file.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/time.h>
61 #include <sys/errno.h>
62 #include <sys/stat.h>
63 #include <sys/fcntl.h>
64
65 #include <net/if.h>
66 #include <net/ppp_defs.h>
67 #include <net/if_ppp.h>
68 #include <netdb.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_var.h>
72 #if !(NS_TARGET >= 40)
73 /* XXX get an error "duplicate member ip_v under 4.1 GAMMA */
74 #include <netinet/ip.h>
75 #endif /* NS_TARGET */
76 #include <netinet/if_ether.h>
77 #include <net/route.h>
78 #include <netinet/in.h>
79
80 #include <netinfo/ni.h>
81
82 #include "pppd.h"
83
84 static const char rcsid[] = RCSID;
85
86 static int initdisc = -1;       /* Initial TTY discipline */
87 static int initfdflags = -1;    /* Initial file descriptor flags for fd */
88 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
89 static int loop_slave = -1;
90 static int loop_master;
91 static char loop_name[20];
92
93 static fd_set in_fds;           /* set of fds that wait_input waits for */
94 static int max_in_fd;           /* highest fd set in in_fds */
95
96 extern int errno;
97
98 static int      restore_term;   /* 1 => we've munged the terminal */
99 static struct termios inittermios; /* Initial TTY termios */
100
101 static int sockfd;              /* socket for doing interface ioctls */
102 static int pppdev;  /* +++ */
103
104 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
105 #define ioctl   myioctl
106 #endif
107
108 static int if_is_up;            /* the interface is currently up */
109 static u_int32_t default_route_gateway; /* gateway addr for default route */
110 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
111
112 /* Prototypes for procedures local to this file. */
113 static int translate_speed __P((int));
114 static int baud_rate_of __P((int));
115 static int dodefaultroute __P((u_int32_t, int));
116 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
117 static int ether_by_host __P((char *, struct ether_addr *));
118
119
120 /*
121  * sys_init - System-dependent initialization.
122  */
123 void
124 sys_init()
125 {
126     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
127     setlogmask(LOG_UPTO(LOG_INFO));
128
129     /* Get an internet socket for doing socket ioctl's on. */
130     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
131         fatal("Couldn't create IP socket: %m");
132
133     if((pppdev = open("/dev/ppp0", O_RDWR, O_NONBLOCK)) == NULL)
134         fatal("Couldn't open /dev/ppp0: %m");
135       
136     FD_ZERO(&in_fds);
137     max_in_fd = 0;
138 }
139
140 /*
141  * sys_cleanup - restore any system state we modified before exiting:
142  * mark the interface down, delete default route and/or proxy arp entry.
143  * This should call die() because it's called from die().
144  */
145 void
146 sys_cleanup()
147 {
148     struct ifreq ifr;
149
150     if (if_is_up) {
151         strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
152         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
153             && ((ifr.ifr_flags & IFF_UP) != 0)) {
154             ifr.ifr_flags &= ~IFF_UP;
155             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
156         }
157     }
158
159     if (default_route_gateway)
160         cifdefaultroute(0, 0, default_route_gateway);
161     if (proxy_arp_addr)
162         cifproxyarp(0, proxy_arp_addr);
163
164     close(pppdev);
165 }
166
167 /*
168  * ppp_available - check whether the system has any ppp interfaces
169  * (in fact we check whether we can do an ioctl on ppp0).
170  */
171 int
172 ppp_available()
173 {
174     int s, ok;
175     struct ifreq ifr;
176     extern char *no_ppp_msg;
177
178     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
179         return 1;               /* can't tell - maybe we're not root */
180
181     strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
182     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
183     close(s);
184
185     no_ppp_msg = "\
186 This system lacks kernel support for PPP.  To include PPP support\n\
187 in the kernel, please follow the steps detailed in the README.NeXT\n\
188 file in the ppp-2.2 distribution.\n";
189
190     return ok;
191 }
192
193 /*
194  * establish_ppp - Turn the serial port into a ppp interface.
195  */
196 int
197 establish_ppp(fd)
198     int fd;
199 {
200     int pppdisc = PPPDISC;
201     int x;
202
203     if (ioctl(fd, TIOCGETD, &initdisc) < 0)
204         fatal("ioctl(TIOCGETD): %m");
205     if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
206         fatal("ioctl(establish TIOCSETD): %m");
207
208     /*
209      * Find out which interface we were given.
210      */
211     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
212         fatal("ioctl(PPPIOCGUNIT): %m");
213
214     /*
215      * Enable debug in the driver if requested.
216      */
217     if (kdebugflag) {
218         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
219             warn("ioctl(PPPIOCGFLAGS): %m");
220         } else {
221             x |= (kdebugflag & 0xFF) * SC_DEBUG;
222             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
223                 warn("ioctl(PPPIOCSFLAGS): %m");
224         }
225     }
226
227     /*
228      * Set device for non-blocking reads so PPPD can poll for
229      * input from the kernel.
230      */
231     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
232         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
233         warn("Couldn't set device to non-blocking mode: %m");
234     }
235
236     return fd;
237 }
238
239
240 /*
241  * disestablish_ppp - Restore the serial port to normal operation.
242  * This shouldn't call die() because it's called from die().
243  */
244 void
245 disestablish_ppp(fd)
246     int fd;
247 {
248     /* Reset non-blocking mode on fd. */
249     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
250         warn("Couldn't restore device fd flags: %m");
251     initfdflags = -1;
252
253     /* Restore old line discipline. */
254     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
255         error("ioctl(TIOCSETD): %m");
256     initdisc = -1;
257 }
258
259 /*
260  * Check whether the link seems not to be 8-bit clean.
261  */
262 void
263 clean_check()
264 {
265     int x;
266     char *s;
267
268     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
269         s = NULL;
270         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
271         case SC_RCV_B7_0:
272             s = "bit 7 set to 1";
273             break;
274         case SC_RCV_B7_1:
275             s = "bit 7 set to 0";
276             break;
277         case SC_RCV_EVNP:
278             s = "odd parity";
279             break;
280         case SC_RCV_ODDP:
281             s = "even parity";
282             break;
283         }
284         if (s != NULL) {
285             warn("Serial link is not 8-bit clean:");
286             warn("All received characters had %s", s);
287         }
288     }
289 }
290
291 /*
292  * List of valid speeds.
293  */
294 struct speed {
295     int speed_int, speed_val;
296 } speeds[] = {
297 #ifdef B50
298     { 50, B50 },
299 #endif
300 #ifdef B75
301     { 75, B75 },
302 #endif
303 #ifdef B110
304     { 110, B110 },
305 #endif
306 #ifdef B134
307     { 134, B134 },
308 #endif
309 #ifdef B150
310     { 150, B150 },
311 #endif
312 #ifdef B200
313     { 200, B200 },
314 #endif
315 #ifdef B300
316     { 300, B300 },
317 #endif
318 #ifdef B600
319     { 600, B600 },
320 #endif
321 #ifdef B1200
322     { 1200, B1200 },
323 #endif
324 #ifdef B1800
325     { 1800, B1800 },
326 #endif
327 #ifdef B2000
328     { 2000, B2000 },
329 #endif
330 #ifdef B2400
331     { 2400, B2400 },
332 #endif
333 #ifdef B3600
334     { 3600, B3600 },
335 #endif
336 #ifdef B4800
337     { 4800, B4800 },
338 #endif
339 #ifdef B7200
340     { 7200, B7200 },
341 #endif
342 #ifdef B9600
343     { 9600, B9600 },
344 #endif
345 #ifdef B19200
346     { 19200, B19200 },
347 #endif
348 #ifdef B38400
349     { 38400, B38400 },
350 #endif
351 #ifdef EXTA
352     { 19200, EXTA },
353 #endif
354 #ifdef EXTB
355     { 38400, EXTB },
356 #endif
357 #ifdef B14400
358     { 14400, B14400 },
359 #endif
360 #ifdef B28800
361     { 28800, B28800 },
362 #endif
363 #ifdef B43200
364     { 43200, B43200 },
365 #endif
366 #ifdef B57600
367     { 57600, B57600 },
368 #endif
369 /*
370 #ifndef B115200
371 #warning Defining B115200
372 #define B115200 20
373 #endif
374 */
375 #ifdef B115200
376     { 115200, B115200 },
377 #endif
378     { 0, 0 }
379 };
380
381 /*
382  * Translate from bits/second to a speed_t.
383  */
384 int
385 translate_speed(bps)
386     int bps;
387 {
388     struct speed *speedp;
389
390     if (bps == 0)
391         return 0;
392     for (speedp = speeds; speedp->speed_int; speedp++)
393         if (bps == speedp->speed_int)
394             return speedp->speed_val;
395     warn("speed %d not supported", bps);
396     return 0;
397 }
398
399 /*
400  * Translate from a speed_t to bits/second.
401  */
402 static int
403 baud_rate_of(speed)
404     int speed;
405 {
406     struct speed *speedp;
407
408     if (speed == 0)
409         return 0;
410     for (speedp = speeds; speedp->speed_int; speedp++)
411         if (speed == speedp->speed_val)
412             return speedp->speed_int;
413     return 0;
414 }
415
416
417 /*
418  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
419  * at the requested speed, etc.  If `local' is true, set CLOCAL
420  * regardless of whether the modem option was specified.
421  */
422 void
423 set_up_tty(fd, local)
424     int fd, local;
425 {
426     int speed, x, modembits;
427     struct termios tios;
428
429     if (tcgetattr(fd, &tios) < 0)
430         fatal("tcgetattr: %m");
431
432     if (!restore_term)
433         inittermios = tios;
434
435     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
436
437     tios.c_cflag |= CS8 | CREAD | HUPCL;
438     if (local || !modem)
439         tios.c_cflag |= CLOCAL;
440
441     tios.c_iflag = IGNBRK | IGNPAR;
442     tios.c_oflag = 0;
443     tios.c_lflag = 0;
444     tios.c_cc[VMIN] = 1;
445     tios.c_cc[VTIME] = 0;
446
447     if (crtscts == -2) {
448         tios.c_iflag |= IXON | IXOFF;
449         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
450         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
451     }
452
453     speed = translate_speed(inspeed);
454     if (speed) {
455         cfsetospeed(&tios, speed);
456         cfsetispeed(&tios, speed);
457     } else {
458         speed = cfgetospeed(&tios);
459         /*
460          * We can't proceed if the serial port speed is B0,
461          * since that implies that the serial port is disabled.
462          */
463         if (speed == B0)
464             fatal("Baud rate for %s is 0; need explicit baud rate",
465                   devnam);
466     }
467
468     if (modem) {
469       modembits = TIOCM_RTS | TIOCM_CTS;
470       if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
471         error("ioctl: TIOCMBIS/BIC: %m");
472     }
473
474     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
475         fatal("tcsetattr: %m");
476
477    baud_rate = inspeed = baud_rate_of(speed);
478    restore_term = 1;
479 }
480
481 /*
482  * restore_tty - restore the terminal to the saved settings.
483  */
484 void
485 restore_tty(fd)
486     int fd;
487 {
488     if (restore_term) {
489         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
490             if (errno != ENXIO)
491                 warn("tcsetattr: %m");
492         restore_term = 0;
493     }
494 }
495
496 /*
497  * setdtr - control the DTR line on the serial port.
498  * This is called from die(), so it shouldn't call die().
499  *
500  * The write hack is to get NXFax to recognize that there is
501  * activity on the port.  Not using the write nukes
502  * NXFax's capability to determine port usage.
503  *
504  */
505 void
506 setdtr(fd, on)
507 int fd, on;
508 {
509     int modembits = TIOCM_DTR;
510
511     if (!on)
512       {
513         write(fd, " ", 1);
514         sleep(1);
515       }
516
517 /*    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
518     ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
519 }
520
521
522 /*
523  * output - Output PPP packet.
524  */
525 void
526 output(unit, p, len)
527     int unit;
528     u_char *p;
529     int len;
530 {
531     if (debug)
532         dbglog("sent %P", p, len);
533
534     if (write(ttyfd, p, len) < 0) {
535         if (errno == EWOULDBLOCK || errno == ENOBUFS
536             || errno == ENXIO || errno == EIO) {
537             warn("write: warning: %m");
538         } else {
539             fatal("write: %m");
540         }
541     }
542 }
543
544
545 /*
546  * wait_input - wait until there is data available,
547  * for the length of time specified by *timo (indefinite
548  * if timo is NULL).
549  */
550 void
551 wait_input(timo)
552     struct timeval *timo;
553 {
554     fd_set ready;
555     int n;
556
557     ready = in_fds;
558     n = select(max_in_fd + 1, &ready, NULL, &ready, timo);
559     if (n < 0 && errno != EINTR)
560         fatal("select: %m");
561 }
562
563
564 /*
565  * add_fd - add an fd to the set that wait_input waits for.
566  */
567 void add_fd(fd)
568     int fd;
569 {
570     FD_SET(fd, &in_fds);
571     if (fd > max_in_fd)
572         max_in_fd = fd;
573 }
574
575 /*
576  * remove_fd - remove an fd from the set that wait_input waits for.
577  */
578 void remove_fd(fd)
579     int fd;
580 {
581     FD_CLR(fd, &in_fds);
582 }
583
584 /*
585  * read_packet - get a PPP packet from the serial device.
586  */
587 int
588 read_packet(buf)
589     u_char *buf;
590 {
591     int len;
592
593     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
594         if (errno == EWOULDBLOCK || errno == EINTR) {
595             SYSDEBUG(("read: %m"));
596             return -1;
597         }
598         fatal("read: %m");
599     }
600     return len;
601 }
602
603
604 /*
605  * ppp_send_config - configure the transmit characteristics of
606  * the ppp interface.
607  */
608 void
609 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
610     int unit, mtu;
611     u_int32_t asyncmap;
612     int pcomp, accomp;
613 {
614     u_int x;
615     struct ifreq ifr;
616
617     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
618     ifr.ifr_mtu = mtu;
619     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
620         fatal("ioctl(SIOCSIFMTU): %m");
621
622     if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
623         fatal("ioctl(PPPIOCSASYNCMAP): %m");
624
625     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
626         fatal("ioctl(PPPIOCGFLAGS): %m");
627
628     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
629     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
630     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
631         fatal("ioctl(PPPIOCSFLAGS): %m");
632 }
633
634
635 /*
636  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
637  */
638 void
639 ppp_set_xaccm(unit, accm)
640     int unit;
641     ext_accm accm;
642 {
643     if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
644         warn("ioctl(PPPIOCSXASYNCMAP): %m");
645 }
646
647
648 /*
649  * ppp_recv_config - configure the receive-side characteristics of
650  * the ppp interface.
651  */
652 void
653 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
654     int unit, mru;
655     u_int32_t asyncmap;
656     int pcomp, accomp;
657 {
658     int x;
659
660     if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0)
661         fatal("ioctl(PPPIOCSMRU): %m");
662     if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
663         fatal("ioctl(PPPIOCSRASYNCMAP): %m");
664     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
665         fatal("ioctl(PPPIOCGFLAGS): %m");
666     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
667     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
668         fatal("ioctl(PPPIOCSFLAGS): %m");
669 }
670
671 /*
672  * ccp_test - ask kernel whether a given compression method
673  * is acceptable for use.
674  */
675 int
676 ccp_test(unit, opt_ptr, opt_len, for_transmit)
677     int unit, opt_len, for_transmit;
678     u_char *opt_ptr;
679 {
680     struct ppp_option_data data;
681
682     data.ptr = opt_ptr;
683     data.length = opt_len;
684     data.transmit = for_transmit;
685     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
686         return 1;
687     return (errno == ENOBUFS)? 0: -1;
688 }
689
690 /*
691  * ccp_flags_set - inform kernel about the current state of CCP.
692  */
693 void
694 ccp_flags_set(unit, isopen, isup)
695     int unit, isopen, isup;
696 {
697     int x;
698
699     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
700         error("ioctl(PPPIOCGFLAGS): %m");
701         return;
702     }
703     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
704     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
705     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
706         error("ioctl(PPPIOCSFLAGS): %m");
707 }
708
709 /*
710  * ccp_fatal_error - returns 1 if decompression was disabled as a
711  * result of an error detected after decompression of a packet,
712  * 0 otherwise.  This is necessary because of patent nonsense.
713  */
714 int
715 ccp_fatal_error(unit)
716     int unit;
717 {
718     int x;
719
720     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
721         error("ioctl(PPPIOCGFLAGS): %m");
722         return 0;
723     }
724     return x & SC_DC_FERROR;
725 }
726
727 /*
728  * sifvjcomp - config tcp header compression
729  */
730 int
731 sifvjcomp(u, vjcomp, cidcomp, maxcid)
732     int u, vjcomp, cidcomp, maxcid;
733 {
734     u_int x;
735
736     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
737         error("ioctl(PPIOCGFLAGS): %m");
738         return 0;
739     }
740     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
741     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
742     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
743         error("ioctl(PPPIOCSFLAGS): %m");
744         return 0;
745     }
746     if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
747         error("ioctl(PPPIOCSFLAGS): %m");
748         return 0;
749     }
750     return 1;
751 }
752
753 /*
754  * sifup - Config the interface up and enable IP packets to pass.
755  */
756 #ifndef SC_ENABLE_IP
757 #define SC_ENABLE_IP    0x100   /* compat for old versions of kernel code */
758 #endif
759
760 int
761 sifup(u)
762     int u;
763 {
764     struct ifreq ifr;
765     u_int x;
766     struct npioctl npi;
767
768     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
769     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
770         error("ioctl (SIOCGIFFLAGS): %m");
771         return 0;
772     }
773     ifr.ifr_flags |= IFF_UP;
774     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
775         error("ioctl(SIOCSIFFLAGS): %m");
776         return 0;
777     }
778     if_is_up = 1;
779     npi.protocol = PPP_IP;
780     npi.mode = NPMODE_PASS;
781     if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
782         if (errno != ENOTTY) {
783             error("ioctl(PPPIOCSNPMODE): %m");
784             return 0;
785         }
786         /* for backwards compatibility */
787         if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
788             error("ioctl (PPPIOCGFLAGS): %m");
789             return 0;
790         }
791         x |= SC_ENABLE_IP;
792         if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
793             error("ioctl(PPPIOCSFLAGS): %m");
794             return 0;
795         }
796     }
797     return 1;
798 }
799
800 /*
801  * sifdown - Config the interface down and disable IP.
802  */
803 int
804 sifdown(u)
805     int u;
806 {
807     struct ifreq ifr;
808     u_int x;
809     int rv;
810     struct npioctl npi;
811
812     rv = 1;
813     npi.protocol = PPP_IP;
814     npi.mode = NPMODE_ERROR;
815     ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi);
816     /* ignore errors, because ttyfd might have been closed by now. */
817
818
819     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
820     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
821         error("ioctl (SIOCGIFFLAGS): %m");
822         rv = 0;
823     } else {
824         ifr.ifr_flags &= ~IFF_UP;
825         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
826             error("ioctl(SIOCSIFFLAGS): %m");
827             rv = 0;
828         } else
829             if_is_up = 0;
830     }
831     return rv;
832 }
833
834 /*
835  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
836  * if it exists.
837  */
838 #define SET_SA_FAMILY(addr, family)             \
839     BZERO((char *) &(addr), sizeof(addr));      \
840     addr.sa_family = (family); 
841
842 /*
843  * sifaddr - Config the interface IP addresses and netmask.
844  */
845 int
846 sifaddr(u, o, h, m)
847     int u;
848     u_int32_t o, h, m;
849 {
850     int ret;
851     struct ifreq ifr;
852
853     ret = 1;
854     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
855     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
856     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
857     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
858         error("ioctl(SIOCAIFADDR): %m");
859         ret = 0;
860     }
861     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
862     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
863         error("ioctl(SIOCSIFDSTADDR): %m");
864         ret = 0;
865     }
866     if (m != 0) {
867         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
868         info("Setting interface mask to %s\n", ip_ntoa(m));
869         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
870             error("ioctl(SIOCSIFNETMASK): %m");
871             ret = 0;
872         }
873     }
874     return ret;
875 }
876
877 /*
878  * cifaddr - Clear the interface IP addresses, and delete routes
879  * through the interface if possible.
880  *
881  * N.B.: under NextStep, you can't *delete* an address on an interface,
882  * so we change it to 0.0.0.0...  A real hack.  But it simplifies
883  * reconnection on the server side.
884  */
885 int
886 cifaddr(u, o, h)
887     int u;
888     u_int32_t o, h;
889 {
890     struct rtentry rt;
891
892 #if 1
893     h = o = 0L;
894     (void) sifaddr(u, o, h, 0L);
895 #endif
896     SET_SA_FAMILY(rt.rt_dst, AF_INET);
897     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
898     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
899     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
900     rt.rt_flags = RTF_HOST;
901     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
902         error("ioctl(SIOCDELRT): %m");
903         return 0;
904     }
905     return 1;
906 }
907
908 /*
909  * sifdefaultroute - assign a default route through the address given.
910  */
911 int
912 sifdefaultroute(u, l, g)
913     int u;
914     u_int32_t l, g;
915 {
916     return dodefaultroute(g, 's');
917 }
918
919 /*
920  * cifdefaultroute - delete a default route through the address given.
921  */
922 int
923 cifdefaultroute(u, l, g)
924     int u;
925     u_int32_t l, g;
926 {
927     return dodefaultroute(g, 'c');
928 }
929
930 /*
931  * dodefaultroute - talk to a routing socket to add/delete a default route.
932  */
933 int
934 dodefaultroute(g, cmd)
935     u_int32_t g;
936     int cmd;
937 {
938     struct rtentry rt;
939
940     SET_SA_FAMILY(rt.rt_dst, AF_INET);
941     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
942     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
943     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
944     rt.rt_flags = RTF_GATEWAY;
945     if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
946         error("%cifdefaultroute: ioctl(%s): %m", cmd,
947                (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
948         return 0;
949     }
950     default_route_gateway = (cmd == 's')? g: 0;
951     return 1;
952 }
953
954 /*
955  * sifproxyarp - Make a proxy ARP entry for the peer.
956  */
957 int
958 sifproxyarp(unit, hisaddr)
959     int unit;
960     u_int32_t hisaddr;
961 {
962     struct arpreq arpreq;
963
964     BZERO(&arpreq, sizeof(arpreq));
965
966     /*
967      * Get the hardware address of an interface on the same subnet
968      * as our local address.
969      */
970     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
971         error("Cannot determine ethernet address for proxy ARP");
972         return 0;
973     }
974
975     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
976     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
977     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
978     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
979         error("ioctl(SIOCSARP): %m");
980         return 0;
981     }
982
983     proxy_arp_addr = hisaddr;
984     return 1;
985 }
986
987 /*
988  * cifproxyarp - Delete the proxy ARP entry for the peer.
989  */
990 int
991 cifproxyarp(unit, hisaddr)
992     int unit;
993     u_int32_t hisaddr;
994 {
995     struct arpreq arpreq;
996
997     BZERO(&arpreq, sizeof(arpreq));
998     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
999     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1000     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1001         warn("ioctl(SIOCDARP): %m");
1002         return 0;
1003     }
1004     proxy_arp_addr = 0;
1005     return 1;
1006 }
1007
1008 /*
1009  * get_ether_addr - get the hardware address of an interface on the
1010  * the same subnet as ipaddr.
1011  */
1012 #define MAX_IFS         32
1013
1014 int
1015 get_ether_addr(ipaddr, hwaddr)
1016     u_int32_t ipaddr;
1017     struct sockaddr *hwaddr;
1018 {
1019     struct ifreq *ifr, *ifend, *ifp;
1020     u_int32_t ina, mask;
1021     struct ether_addr dla;
1022     struct ifreq ifreq;
1023     struct ifconf ifc;
1024     struct ifreq ifs[MAX_IFS];
1025     struct hostent *hostent;
1026
1027     ifc.ifc_len = sizeof(ifs);
1028     ifc.ifc_req = ifs;
1029     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1030         error("ioctl(SIOCGIFCONF): %m");
1031         return 0;
1032     }
1033
1034     /*
1035      * Scan through looking for an interface with an Internet
1036      * address on the same subnet as `ipaddr'.
1037      */
1038     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1039     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1040                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1041         if (ifr->ifr_addr.sa_family == AF_INET) {
1042             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1043             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1044             /*
1045              * Check that the interface is up, and not point-to-point
1046              * or loopback.
1047              */
1048             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1049                 continue;
1050             if ((ifreq.ifr_flags &
1051                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1052                  != (IFF_UP|IFF_BROADCAST))
1053                 continue;
1054             /*
1055              * Get its netmask and check that it's on the right subnet.
1056              */
1057             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1058                 continue;
1059             mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1060             if ((ipaddr & mask) != (ina & mask))
1061                 continue;
1062
1063             break;
1064         }
1065     }
1066
1067     if (ifr >= ifend)
1068         return 0;
1069     info("found interface %s for proxy arp", ifr->ifr_name);
1070
1071     /*
1072      * Get the hostname and look for an entry using the ethers database.
1073      * Under NeXTStep this is the best we can do for now.
1074      */
1075     if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1076         return 0;
1077
1078     if (ether_by_host(hostent->h_name, &dla)) {
1079         info("Add entry for %s in /etc/ethers", hostent->h_name);
1080         return 0;       /* it's not there */
1081     }
1082     hwaddr->sa_family = AF_UNSPEC;
1083     BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1084     return 1;
1085 }
1086
1087 static int
1088 ether_by_host(hostname, etherptr)
1089     char *hostname;
1090     struct ether_addr *etherptr;
1091 {
1092     struct ether_addr *thisptr;
1093     void *conn;
1094     ni_id root;
1095     ni_namelist val;
1096     char path[256];
1097
1098     if (!ether_hostton(hostname, etherptr))
1099         return 0;
1100     /*
1101      * We shall now try and
1102      * find the address in the
1103      * top domain of netinfo.
1104      */
1105     slprintf(path, sizeof(path), "/machines/%s", hostname);
1106
1107     if (ni_open((void *)0, "/", &conn)
1108      || ni_root(conn, &root)
1109      || ni_pathsearch(conn, &root, path)
1110      || ni_lookupprop(conn, &root, "en_address", &val))
1111         return 1;
1112
1113     /*
1114      * Now we can convert the returned string into an ethernet address.
1115      */
1116     strlcpy(path, val.ni_namelist_val[0], sizeof(path));
1117     ni_free(conn);
1118     if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1119         return 1;
1120     BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1121     return 0;
1122 }
1123
1124
1125
1126 /*
1127  * Return user specified netmask, modified by any mask we might determine
1128  * for address `addr' (in network byte order).
1129  * Here we scan through the system's list of interfaces, looking for
1130  * any non-point-to-point interfaces which might appear to be on the same
1131  * network as `addr'.  If we find any, we OR in their netmask to the
1132  * user-specified netmask.
1133  */
1134 u_int32_t
1135 GetMask(addr)
1136     u_int32_t addr;
1137 {
1138     u_int32_t mask, nmask, ina;
1139     struct ifreq *ifr, *ifend, ifreq;
1140     struct ifconf ifc;
1141     struct ifreq ifs[MAX_IFS];
1142
1143     addr = ntohl(addr);
1144     if (IN_CLASSA(addr))        /* determine network mask for address class */
1145         nmask = IN_CLASSA_NET;
1146     else if (IN_CLASSB(addr))
1147         nmask = IN_CLASSB_NET;
1148     else
1149         nmask = IN_CLASSC_NET;
1150     /* class D nets are disallowed by bad_ip_adrs */
1151     mask = netmask | htonl(nmask);
1152
1153     /*
1154      * Scan through the system's network interfaces.
1155      */
1156     ifc.ifc_len = sizeof(ifs);
1157     ifc.ifc_req = ifs;
1158     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1159         warn("ioctl(SIOCGIFCONF): %m");
1160         return mask;
1161     }
1162     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1163     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1164                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1165         /*
1166          * Check the interface's internet address.
1167          */
1168         if (ifr->ifr_addr.sa_family != AF_INET)
1169             continue;
1170         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1171         if ((ntohl(ina) & nmask) != (addr & nmask))
1172             continue;
1173         /*
1174          * Check that the interface is up, and not point-to-point or loopback.
1175          */
1176         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1177         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1178             continue;
1179         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1180             != IFF_UP)
1181             continue;
1182         /*
1183          * Get its netmask and OR it into our mask.
1184          */
1185         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1186             continue;
1187         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1188     }
1189
1190     return mask;
1191 }
1192
1193 /*
1194  * have_route_to - determine if the system has any route to
1195  * a given IP address.
1196  * For demand mode to work properly, we have to ignore routes
1197  * through our own interface.
1198  */
1199 int have_route_to(u_int32_t addr)
1200 {
1201     return -1;
1202 }
1203
1204 #if 0
1205 /*
1206  * daemon - Detach us from the terminal session.
1207  */
1208 int
1209 daemon(nochdir, noclose)
1210     int nochdir, noclose;
1211 {
1212     int pid;
1213
1214     if ((pid = fork()) < 0)
1215         return -1;
1216     if (pid != 0)
1217         exit(0);                /* parent dies */
1218     (void)setsid();
1219     if (!nochdir)
1220         chdir("/");
1221     if (!noclose) {
1222         fclose(stdin);          /* don't need stdin, stdout, stderr */
1223         fclose(stdout);
1224         fclose(stderr);
1225     }
1226     return 0;
1227 }
1228 #endif
1229
1230 char *
1231 strdup(s)
1232     const char *s;
1233 {
1234     char *d = malloc(strlen(s) + 1);
1235
1236     if (d) strcpy(d, s);
1237     return d;
1238 }
1239
1240 /*
1241  * This logwtmp() implementation is subject to the following copyright:
1242  *
1243  * Copyright (c) 1988 The Regents of the University of California.
1244  * All rights reserved.
1245  *
1246  * Redistribution and use in source and binary forms are permitted
1247  * provided that the above copyright notice and this paragraph are
1248  * duplicated in all such forms and that any documentation,
1249  * advertising materials, and other materials related to such
1250  * distribution and use acknowledge that the software was developed
1251  * by the University of California, Berkeley.  The name of the
1252  * University may not be used to endorse or promote products derived
1253  * from this software without specific prior written permission.
1254  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1255  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1256  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1257  */
1258
1259 #define WTMPFILE        "/usr/adm/wtmp"
1260
1261 void
1262 logwtmp(line, name, host)
1263     const char *line, *name, *host;
1264 {
1265     int fd;
1266     struct stat buf;
1267     struct utmp ut;
1268
1269     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1270         return;
1271     if (!fstat(fd, &buf)) {
1272         strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1273         strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1274         strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1275         (void)time(&ut.ut_time);
1276         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1277             (void)ftruncate(fd, buf.st_size);
1278     }
1279     close(fd);
1280 }
1281
1282 #if 0
1283 /*
1284  * Routines for locking and unlocking the serial device, moved here
1285  * from chat.c.
1286  */
1287
1288 #define LOCK_PREFIX     "/usr/spool/uucp/LCK/LCK.."
1289
1290 static char *lock_file;
1291
1292 /*
1293  * lock - create a lock file for the named device
1294  */
1295 int
1296 lock(dev)
1297     char *dev;
1298 {
1299     int fd, pid, n;
1300     char *p;
1301     size_t l;
1302
1303     if ((p = strrchr(dev, '/')) != NULL)
1304         dev = p + 1;
1305     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1306     lock_file = malloc(l);
1307     if (lock_file == NULL)
1308         novm("lock file name");
1309     slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1310
1311     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1312         if (errno == EEXIST
1313             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1314             /* Read the lock file to find out who has the device locked */
1315             n = read(fd, &pid, sizeof(pid));
1316             if (n <= 0) {
1317                 error("Can't read pid from lock file %s", lock_file);
1318                 close(fd);
1319             } else {
1320                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1321                     /* pid no longer exists - remove the lock file */
1322                     if (unlink(lock_file) == 0) {
1323                         close(fd);
1324                         notice("Removed stale lock on %s (pid %d)",
1325                                dev, pid);
1326                         continue;
1327                     } else
1328                         warn("Couldn't remove stale lock on %s", dev);
1329                 } else
1330                     notice("Device %s is locked by pid %d",
1331                            dev, pid);
1332             }
1333             close(fd);
1334         } else
1335             error("Can't create lock file %s: %m", lock_file);
1336         free(lock_file);
1337         lock_file = NULL;
1338         return -1;
1339     }
1340
1341     pid = getpid();
1342     write(fd, &pid, sizeof pid);
1343
1344     close(fd);
1345     return 0;
1346 }
1347
1348 /*
1349  * unlock - remove our lockfile
1350  */
1351 void
1352 unlock()
1353 {
1354     if (lock_file) {
1355         unlink(lock_file);
1356         free(lock_file);
1357         lock_file = NULL;
1358     }
1359 }
1360 #endif
1361
1362 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1363 int
1364 ioctl(fd, cmd, c)
1365     int fd, cmd;
1366     caddr_t c;
1367 {
1368 #undef  ioctl
1369     int ret;
1370
1371 #ifdef DEBUGIOCTL
1372     int serrno;
1373     u_char let, code, size;
1374
1375     size = (cmd >> 16) & IOCPARM_MASK;
1376     let = (cmd >> 8);
1377     code = cmd;
1378
1379     if (let == 't' && (75 <= code && code <= 90))
1380     info("ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1381            let, code, size, c);
1382 #endif
1383
1384     ret = ioctl(fd, cmd, c);
1385
1386 #ifdef DEBUGIOCTL
1387     serrno = errno;
1388     if (ret == -1)
1389         info("ioctl('%c', %d, %d) errno = %d (%m)\n",
1390                 let, code, size, errno);
1391     if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1392         int i, len = ((cmd >> 16) & IOCPARM_MASK);
1393         for (i = 0; i < len / 4; ++i)
1394                 info("word[%d] @ 0x%06x = 0x%x\n",
1395                        i, &((int *) c)[i],((int *)c)[i]);
1396     }
1397     errno = serrno;
1398 #endif
1399
1400     if (ret == -1 && errno == EPERM)
1401         errno = ret = 0;
1402     return ret;
1403 }
1404 #endif  /* HAS_BROKEN_IOCTL */
1405
1406
1407 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1408
1409 /*
1410  * These redefinitions of Posix functions are necessary
1411  * because HPPA systems have an OS bug that causes 
1412  * sigaction to core dump:
1413  *
1414  * AlainF 9-Nov-1994    HACK FOR HP-PA/NEXTSTEP
1415  *                      sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1416  *                      Posix lib. This causes pppd to SIGBUS at the expiration
1417  *                      of the first timeout (_sigtramp seems to invoke
1418  *                      the SIGALRM handler at an unreasonably low address).
1419  *                      All calls so sigaction(3) have been changed to calls
1420  *                      to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1421  *                      sigblock(2).
1422  *                      This is kind of a hack, especially since there are
1423  *                      other routines of the Posix lib still used, but
1424  *                      it worked for me.
1425  *
1426  * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1427  * have the same bug.  Thus this fix has been enabled for SPARC also.
1428  *
1429  *
1430  */
1431
1432 int sigemptyset(sigset_t *mask)
1433 {
1434   *mask = 0;
1435 }
1436
1437 sigaddset(sigset_t *mask, int which_sig)
1438 {
1439   *mask |= sigmask(which_sig);
1440 }
1441
1442
1443 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1444 {
1445    struct sigvec sv;
1446    static int in = 0;
1447
1448    sv.sv_handler = act->sa_handler;
1449    sv.sv_mask = act->sa_mask;
1450    sv.sv_flags = 0;
1451
1452    if (!in)
1453      {
1454        in = 1;
1455        warn("PPPD: Inside modified HP and SPARC sigaction\n");
1456      }
1457
1458    return sigvec(sig, &sv, NULL);
1459 }
1460
1461 #endif
1462
1463
1464 /*
1465  * Code following is added for 2.3 compatibility
1466  */
1467
1468 /*
1469  * get_idle_time - return how long the link has been idle.
1470  */
1471 int
1472 get_idle_time(u, ip)
1473     int u;
1474     struct ppp_idle *ip;
1475 {
1476   return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0); 
1477 }
1478
1479 /*
1480  * get_ppp_stats - return statistics for the link.
1481  */
1482 int
1483 get_ppp_stats(u, stats)
1484     int u;
1485     struct pppd_stats *stats;
1486 {
1487     struct ifpppstatsreq req;
1488
1489     memset (&req, 0, sizeof (req));
1490     strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
1491     if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
1492         error("Couldn't get PPP statistics: %m");
1493         return 0;
1494     }
1495     stats->bytes_in = req.stats.p.ppp_ibytes;
1496     stats->bytes_out = req.stats.p.ppp_obytes;
1497     return 1;
1498 }
1499
1500
1501 /*
1502  * get_loop_output - read characters from the loopback, form them
1503  * into frames, and detect when we want to bring the real link up.
1504  * Return value is 1 if we need to bring up the link, 0 otherwise.
1505  */
1506 int
1507 get_loop_output()
1508 {
1509
1510 #if 0
1511     int rv = 0;
1512     int n;
1513
1514     while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1515         if (loop_chars(inbuf, n))
1516             rv = 1;
1517     }
1518
1519     if (n == 0)
1520         fatal("eof on loopback");
1521     if (errno != EWOULDBLOCK)
1522         fatal("read from loopback: %m");
1523
1524     return rv;
1525 #endif
1526
1527     return 0;
1528 }
1529
1530 /*
1531  * sifnpmode - Set the mode for handling packets for a given NP.
1532  */
1533 int
1534 sifnpmode(u, proto, mode)
1535     int u;
1536     int proto;
1537     enum NPmode mode;
1538 {
1539     struct npioctl npi;
1540
1541     npi.protocol = proto;
1542     npi.mode = mode;
1543     if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1544         error("ioctl(set NP %d mode to %d): %m", proto, mode);
1545         return 0;
1546     }
1547     return 1;
1548 }
1549
1550
1551 /*
1552  * open_ppp_loopback - open the device we use for getting
1553  * packets in demand mode, and connect it to a ppp interface.
1554  * Here we use a pty.
1555  */
1556 int
1557 open_ppp_loopback()
1558 {
1559
1560 #if 0
1561     int flags;
1562     struct termios tios;
1563     int pppdisc = PPPDISC;
1564
1565     fatal("open_ppp_loopback called!");
1566
1567     if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0)
1568         fatal("No free pty for loopback");
1569     SYSDEBUG(("using %s for loopback", loop_name));
1570
1571     if (tcgetattr(loop_slave, &tios) == 0) {
1572         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1573         tios.c_cflag |= CS8 | CREAD;
1574         tios.c_iflag = IGNPAR;
1575         tios.c_oflag = 0;
1576         tios.c_lflag = 0;
1577         if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1578             warn("couldn't set attributes on loopback: %m");
1579     }
1580
1581     if ((flags = fcntl(loop_master, F_GETFL)) != -1) 
1582         if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1583             warn("couldn't set loopback to nonblock: %m");
1584
1585     ttyfd = loop_slave;
1586     if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0)
1587         fatal("ioctl(TIOCSETD): %m");
1588
1589     /*
1590      * Find out which interface we were given.
1591      */
1592     if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0)
1593         fatal("ioctl(PPPIOCGUNIT): %m");
1594
1595     /*
1596      * Enable debug in the driver if requested.
1597      */
1598     if (kdebugflag) {
1599         if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1600             warn("ioctl (PPPIOCGFLAGS): %m");
1601         } else {
1602             flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1603             if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1604                 warn("ioctl(PPPIOCSFLAGS): %m");
1605         }
1606     }
1607
1608     return loop_master;
1609 #endif
1610
1611 }
1612
1613 /*
1614  * restore_loop - reattach the ppp unit to the loopback.
1615  */
1616 void
1617 restore_loop()
1618 {
1619     int x;
1620
1621     /*
1622      * Transfer the ppp interface back to the loopback.
1623      */
1624     if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0)
1625         fatal("ioctl(transfer ppp unit): %m");
1626     x = PPPDISC;
1627     if (ioctl(loop_slave, TIOCSETD, &x) < 0)
1628         fatal("ioctl(TIOCSETD): %m");
1629
1630     /*
1631      * Check that we got the same unit again.
1632      */
1633     if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
1634         fatal("ioctl(PPPIOCGUNIT): %m");
1635     if (x != ifunit)
1636         fatal("transfer_ppp failed: wanted unit %d, got %d",
1637               ifunit, x);
1638     ttyfd = loop_slave;
1639 }
1640
1641
1642 /*
1643  * Use the hostid as part of the random number seed.
1644  */
1645 int
1646 get_host_seed()
1647 {
1648     return gethostid();
1649 }
1650
1651
1652 /*
1653  * sys_check_options - check the options that the user specified
1654  */
1655 int
1656 sys_check_options()
1657 {
1658   /*
1659    * We don't support demand dialing yet.
1660    */
1661   if (demand)
1662     {
1663       option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1664       return 0;
1665     }
1666   return 1;
1667 }
1668
1669
1670 /*
1671  * sys_close - Clean up in a child process before execing.
1672  */
1673 void
1674 sys_close()
1675 {
1676     close(sockfd);
1677     if (loop_slave >= 0) {
1678         close(loop_slave);
1679         close(loop_master);
1680     }
1681     closelog();
1682 }
1683
1684 #if 0
1685 /*
1686  * wait_loop_output - wait until there is data available on the
1687  * loopback, for the length of time specified by *timo (indefinite
1688  * if timo is NULL).
1689  */
1690 void wait_loop_output(timo)
1691     struct timeval *timo;
1692 {
1693     fd_set ready;
1694     int n;
1695
1696     FD_ZERO(&ready);
1697     FD_SET(loop_master, &ready);
1698     n = select(loop_master + 1, &ready, NULL, &ready, timo);
1699     if (n < 0 && errno != EINTR)
1700         fatal("select: %m");
1701 }
1702
1703 /*
1704  * wait_time - wait for a given length of time or until a
1705  * signal is received.
1706  */
1707 void wait_time(timo)
1708     struct timeval *timo;
1709 {
1710     int n;
1711
1712     n = select(0, NULL, NULL, NULL, timo);
1713     if (n < 0 && errno != EINTR)
1714         fatal("select: %m");
1715 }
1716 #endif