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