]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-NeXT.c
changed order of args to strlcpy/strlcat.
[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.15 1999/03/19 01:29:40 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(int fd)
545 {
546     FD_SET(fd, &in_fds);
547     if (fd > max_in_fd)
548         max_in_fd = fd;
549 }
550
551 /*
552  * remove_fd - remove an fd from the set that wait_input waits for.
553  */
554 void remove_fd(int fd)
555 {
556     FD_CLR(fd, &in_fds);
557 }
558
559 /*
560  * read_packet - get a PPP packet from the serial device.
561  */
562 int
563 read_packet(buf)
564     u_char *buf;
565 {
566     int len;
567
568     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
569         if (errno == EWOULDBLOCK || errno == EINTR) {
570             SYSDEBUG(("read: %m"));
571             return -1;
572         }
573         fatal("read: %m");
574     }
575     return len;
576 }
577
578
579 /*
580  * ppp_send_config - configure the transmit characteristics of
581  * the ppp interface.
582  */
583 void
584 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
585     int unit, mtu;
586     u_int32_t asyncmap;
587     int pcomp, accomp;
588 {
589     u_int x;
590     struct ifreq ifr;
591
592     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
593     ifr.ifr_mtu = mtu;
594     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
595         fatal("ioctl(SIOCSIFMTU): %m");
596
597     if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
598         fatal("ioctl(PPPIOCSASYNCMAP): %m");
599
600     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
601         fatal("ioctl(PPPIOCGFLAGS): %m");
602
603     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
604     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
605     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
606         fatal("ioctl(PPPIOCSFLAGS): %m");
607 }
608
609
610 /*
611  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
612  */
613 void
614 ppp_set_xaccm(unit, accm)
615     int unit;
616     ext_accm accm;
617 {
618     if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
619         warn("ioctl(PPPIOCSXASYNCMAP): %m");
620 }
621
622
623 /*
624  * ppp_recv_config - configure the receive-side characteristics of
625  * the ppp interface.
626  */
627 void
628 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
629     int unit, mru;
630     u_int32_t asyncmap;
631     int pcomp, accomp;
632 {
633     int x;
634
635     if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0)
636         fatal("ioctl(PPPIOCSMRU): %m");
637     if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
638         fatal("ioctl(PPPIOCSRASYNCMAP): %m");
639     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
640         fatal("ioctl(PPPIOCGFLAGS): %m");
641     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
642     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
643         fatal("ioctl(PPPIOCSFLAGS): %m");
644 }
645
646 /*
647  * ccp_test - ask kernel whether a given compression method
648  * is acceptable for use.
649  */
650 int
651 ccp_test(unit, opt_ptr, opt_len, for_transmit)
652     int unit, opt_len, for_transmit;
653     u_char *opt_ptr;
654 {
655     struct ppp_option_data data;
656
657     data.ptr = opt_ptr;
658     data.length = opt_len;
659     data.transmit = for_transmit;
660     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
661         return 1;
662     return (errno == ENOBUFS)? 0: -1;
663 }
664
665 /*
666  * ccp_flags_set - inform kernel about the current state of CCP.
667  */
668 void
669 ccp_flags_set(unit, isopen, isup)
670     int unit, isopen, isup;
671 {
672     int x;
673
674     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
675         error("ioctl(PPPIOCGFLAGS): %m");
676         return;
677     }
678     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
679     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
680     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
681         error("ioctl(PPPIOCSFLAGS): %m");
682 }
683
684 /*
685  * ccp_fatal_error - returns 1 if decompression was disabled as a
686  * result of an error detected after decompression of a packet,
687  * 0 otherwise.  This is necessary because of patent nonsense.
688  */
689 int
690 ccp_fatal_error(unit)
691     int unit;
692 {
693     int x;
694
695     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
696         error("ioctl(PPPIOCGFLAGS): %m");
697         return 0;
698     }
699     return x & SC_DC_FERROR;
700 }
701
702 /*
703  * sifvjcomp - config tcp header compression
704  */
705 int
706 sifvjcomp(u, vjcomp, cidcomp, maxcid)
707     int u, vjcomp, cidcomp, maxcid;
708 {
709     u_int x;
710
711     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
712         error("ioctl(PPIOCGFLAGS): %m");
713         return 0;
714     }
715     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
716     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
717     if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
718         error("ioctl(PPPIOCSFLAGS): %m");
719         return 0;
720     }
721     if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
722         error("ioctl(PPPIOCSFLAGS): %m");
723         return 0;
724     }
725     return 1;
726 }
727
728 /*
729  * sifup - Config the interface up and enable IP packets to pass.
730  */
731 #ifndef SC_ENABLE_IP
732 #define SC_ENABLE_IP    0x100   /* compat for old versions of kernel code */
733 #endif
734
735 int
736 sifup(u)
737     int u;
738 {
739     struct ifreq ifr;
740     u_int x;
741     struct npioctl npi;
742
743     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
744     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
745         error("ioctl (SIOCGIFFLAGS): %m");
746         return 0;
747     }
748     ifr.ifr_flags |= IFF_UP;
749     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
750         error("ioctl(SIOCSIFFLAGS): %m");
751         return 0;
752     }
753     if_is_up = 1;
754     npi.protocol = PPP_IP;
755     npi.mode = NPMODE_PASS;
756     if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
757         if (errno != ENOTTY) {
758             error("ioctl(PPPIOCSNPMODE): %m");
759             return 0;
760         }
761         /* for backwards compatibility */
762         if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
763             error("ioctl (PPPIOCGFLAGS): %m");
764             return 0;
765         }
766         x |= SC_ENABLE_IP;
767         if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
768             error("ioctl(PPPIOCSFLAGS): %m");
769             return 0;
770         }
771     }
772     return 1;
773 }
774
775 /*
776  * sifdown - Config the interface down and disable IP.
777  */
778 int
779 sifdown(u)
780     int u;
781 {
782     struct ifreq ifr;
783     u_int x;
784     int rv;
785     struct npioctl npi;
786
787     rv = 1;
788     npi.protocol = PPP_IP;
789     npi.mode = NPMODE_ERROR;
790     ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi);
791     /* ignore errors, because ttyfd might have been closed by now. */
792
793
794     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
795     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
796         error("ioctl (SIOCGIFFLAGS): %m");
797         rv = 0;
798     } else {
799         ifr.ifr_flags &= ~IFF_UP;
800         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
801             error("ioctl(SIOCSIFFLAGS): %m");
802             rv = 0;
803         } else
804             if_is_up = 0;
805     }
806     return rv;
807 }
808
809 /*
810  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
811  * if it exists.
812  */
813 #define SET_SA_FAMILY(addr, family)             \
814     BZERO((char *) &(addr), sizeof(addr));      \
815     addr.sa_family = (family); 
816
817 /*
818  * sifaddr - Config the interface IP addresses and netmask.
819  */
820 int
821 sifaddr(u, o, h, m)
822     int u;
823     u_int32_t o, h, m;
824 {
825     int ret;
826     struct ifreq ifr;
827
828     ret = 1;
829     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
830     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
831     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
832     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
833         error("ioctl(SIOCAIFADDR): %m");
834         ret = 0;
835     }
836     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
837     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
838         error("ioctl(SIOCSIFDSTADDR): %m");
839         ret = 0;
840     }
841     if (m != 0) {
842         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
843         info("Setting interface mask to %s\n", ip_ntoa(m));
844         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
845             error("ioctl(SIOCSIFNETMASK): %m");
846             ret = 0;
847         }
848     }
849     return ret;
850 }
851
852 /*
853  * cifaddr - Clear the interface IP addresses, and delete routes
854  * through the interface if possible.
855  *
856  * N.B.: under NextStep, you can't *delete* an address on an interface,
857  * so we change it to 0.0.0.0...  A real hack.  But it simplifies
858  * reconnection on the server side.
859  */
860 int
861 cifaddr(u, o, h)
862     int u;
863     u_int32_t o, h;
864 {
865     struct rtentry rt;
866
867 #if 1
868     h = o = 0L;
869     (void) sifaddr(u, o, h, 0L);
870 #endif
871     SET_SA_FAMILY(rt.rt_dst, AF_INET);
872     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
873     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
874     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
875     rt.rt_flags = RTF_HOST;
876     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
877         error("ioctl(SIOCDELRT): %m");
878         return 0;
879     }
880     return 1;
881 }
882
883 /*
884  * sifdefaultroute - assign a default route through the address given.
885  */
886 int
887 sifdefaultroute(u, l, g)
888     int u;
889     u_int32_t l, g;
890 {
891     return dodefaultroute(g, 's');
892 }
893
894 /*
895  * cifdefaultroute - delete a default route through the address given.
896  */
897 int
898 cifdefaultroute(u, l, g)
899     int u;
900     u_int32_t l, g;
901 {
902     return dodefaultroute(g, 'c');
903 }
904
905 /*
906  * dodefaultroute - talk to a routing socket to add/delete a default route.
907  */
908 int
909 dodefaultroute(g, cmd)
910     u_int32_t g;
911     int cmd;
912 {
913     struct rtentry rt;
914
915     SET_SA_FAMILY(rt.rt_dst, AF_INET);
916     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
917     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
918     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
919     rt.rt_flags = RTF_GATEWAY;
920     if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
921         error("%cifdefaultroute: ioctl(%s): %m", cmd,
922                (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
923         return 0;
924     }
925     default_route_gateway = (cmd == 's')? g: 0;
926     return 1;
927 }
928
929 /*
930  * sifproxyarp - Make a proxy ARP entry for the peer.
931  */
932 int
933 sifproxyarp(unit, hisaddr)
934     int unit;
935     u_int32_t hisaddr;
936 {
937     struct arpreq arpreq;
938
939     BZERO(&arpreq, sizeof(arpreq));
940
941     /*
942      * Get the hardware address of an interface on the same subnet
943      * as our local address.
944      */
945     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
946         error("Cannot determine ethernet address for proxy ARP");
947         return 0;
948     }
949
950     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
951     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
952     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
953     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
954         error("ioctl(SIOCSARP): %m");
955         return 0;
956     }
957
958     proxy_arp_addr = hisaddr;
959     return 1;
960 }
961
962 /*
963  * cifproxyarp - Delete the proxy ARP entry for the peer.
964  */
965 int
966 cifproxyarp(unit, hisaddr)
967     int unit;
968     u_int32_t hisaddr;
969 {
970     struct arpreq arpreq;
971
972     BZERO(&arpreq, sizeof(arpreq));
973     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
974     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
975     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
976         warn("ioctl(SIOCDARP): %m");
977         return 0;
978     }
979     proxy_arp_addr = 0;
980     return 1;
981 }
982
983 /*
984  * get_ether_addr - get the hardware address of an interface on the
985  * the same subnet as ipaddr.
986  */
987 #define MAX_IFS         32
988
989 int
990 get_ether_addr(ipaddr, hwaddr)
991     u_int32_t ipaddr;
992     struct sockaddr *hwaddr;
993 {
994     struct ifreq *ifr, *ifend, *ifp;
995     u_int32_t ina, mask;
996     struct ether_addr dla;
997     struct ifreq ifreq;
998     struct ifconf ifc;
999     struct ifreq ifs[MAX_IFS];
1000     struct hostent *hostent;
1001
1002     ifc.ifc_len = sizeof(ifs);
1003     ifc.ifc_req = ifs;
1004     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1005         error("ioctl(SIOCGIFCONF): %m");
1006         return 0;
1007     }
1008
1009     /*
1010      * Scan through looking for an interface with an Internet
1011      * address on the same subnet as `ipaddr'.
1012      */
1013     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1014     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1015                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1016         if (ifr->ifr_addr.sa_family == AF_INET) {
1017             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1018             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1019             /*
1020              * Check that the interface is up, and not point-to-point
1021              * or loopback.
1022              */
1023             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1024                 continue;
1025             if ((ifreq.ifr_flags &
1026                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1027                  != (IFF_UP|IFF_BROADCAST))
1028                 continue;
1029             /*
1030              * Get its netmask and check that it's on the right subnet.
1031              */
1032             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1033                 continue;
1034             mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1035             if ((ipaddr & mask) != (ina & mask))
1036                 continue;
1037
1038             break;
1039         }
1040     }
1041
1042     if (ifr >= ifend)
1043         return 0;
1044     info("found interface %s for proxy arp", ifr->ifr_name);
1045
1046     /*
1047      * Get the hostname and look for an entry using the ethers database.
1048      * Under NeXTStep this is the best we can do for now.
1049      */
1050     if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1051         return 0;
1052
1053     if (ether_by_host(hostent->h_name, &dla)) {
1054         info("Add entry for %s in /etc/ethers", hostent->h_name);
1055         return 0;       /* it's not there */
1056     }
1057     hwaddr->sa_family = AF_UNSPEC;
1058     BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1059     return 1;
1060 }
1061
1062 static int
1063 ether_by_host(hostname, etherptr)
1064     char *hostname;
1065     struct ether_addr *etherptr;
1066 {
1067     struct ether_addr *thisptr;
1068     void *conn;
1069     ni_id root;
1070     ni_namelist val;
1071     char path[256];
1072
1073     if (!ether_hostton(hostname, etherptr))
1074         return 0;
1075     /*
1076      * We shall now try and
1077      * find the address in the
1078      * top domain of netinfo.
1079      */
1080     slprintf(path, sizeof(path), "/machines/%s", hostname);
1081
1082     if (ni_open((void *)0, "/", &conn)
1083      || ni_root(conn, &root)
1084      || ni_pathsearch(conn, &root, path)
1085      || ni_lookupprop(conn, &root, "en_address", &val))
1086         return 1;
1087
1088     /*
1089      * Now we can convert the returned string into an ethernet address.
1090      */
1091     strlcpy(path, val.ni_namelist_val[0], sizeof(path));
1092     ni_free(conn);
1093     if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1094         return 1;
1095     BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1096     return 0;
1097 }
1098
1099
1100
1101 /*
1102  * Return user specified netmask, modified by any mask we might determine
1103  * for address `addr' (in network byte order).
1104  * Here we scan through the system's list of interfaces, looking for
1105  * any non-point-to-point interfaces which might appear to be on the same
1106  * network as `addr'.  If we find any, we OR in their netmask to the
1107  * user-specified netmask.
1108  */
1109 u_int32_t
1110 GetMask(addr)
1111     u_int32_t addr;
1112 {
1113     u_int32_t mask, nmask, ina;
1114     struct ifreq *ifr, *ifend, ifreq;
1115     struct ifconf ifc;
1116     struct ifreq ifs[MAX_IFS];
1117
1118     addr = ntohl(addr);
1119     if (IN_CLASSA(addr))        /* determine network mask for address class */
1120         nmask = IN_CLASSA_NET;
1121     else if (IN_CLASSB(addr))
1122         nmask = IN_CLASSB_NET;
1123     else
1124         nmask = IN_CLASSC_NET;
1125     /* class D nets are disallowed by bad_ip_adrs */
1126     mask = netmask | htonl(nmask);
1127
1128     /*
1129      * Scan through the system's network interfaces.
1130      */
1131     ifc.ifc_len = sizeof(ifs);
1132     ifc.ifc_req = ifs;
1133     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1134         warn("ioctl(SIOCGIFCONF): %m");
1135         return mask;
1136     }
1137     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1138     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1139                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1140         /*
1141          * Check the interface's internet address.
1142          */
1143         if (ifr->ifr_addr.sa_family != AF_INET)
1144             continue;
1145         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1146         if ((ntohl(ina) & nmask) != (addr & nmask))
1147             continue;
1148         /*
1149          * Check that the interface is up, and not point-to-point or loopback.
1150          */
1151         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1152         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1153             continue;
1154         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1155             != IFF_UP)
1156             continue;
1157         /*
1158          * Get its netmask and OR it into our mask.
1159          */
1160         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1161             continue;
1162         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1163     }
1164
1165     return mask;
1166 }
1167
1168 /*
1169  * have_route_to - determine if the system has any route to
1170  * a given IP address.
1171  * For demand mode to work properly, we have to ignore routes
1172  * through our own interface.
1173  */
1174 int have_route_to(u_int32_t addr)
1175 {
1176     return -1;
1177 }
1178
1179
1180 /*
1181  * daemon - Detach us from the terminal session.
1182  */
1183 int
1184 daemon(nochdir, noclose)
1185     int nochdir, noclose;
1186 {
1187     int pid;
1188
1189     if ((pid = fork()) < 0)
1190         return -1;
1191     if (pid != 0)
1192         exit(0);                /* parent dies */
1193     (void)setsid();
1194     if (!nochdir)
1195         chdir("/");
1196     if (!noclose) {
1197         fclose(stdin);          /* don't need stdin, stdout, stderr */
1198         fclose(stdout);
1199         fclose(stderr);
1200     }
1201     return 0;
1202 }
1203
1204
1205 char *
1206 strdup(s)
1207     const char *s;
1208 {
1209     char *d = malloc(strlen(s) + 1);
1210
1211     if (d) strcpy(d, s);
1212     return d;
1213 }
1214
1215 /*
1216  * This logwtmp() implementation is subject to the following copyright:
1217  *
1218  * Copyright (c) 1988 The Regents of the University of California.
1219  * All rights reserved.
1220  *
1221  * Redistribution and use in source and binary forms are permitted
1222  * provided that the above copyright notice and this paragraph are
1223  * duplicated in all such forms and that any documentation,
1224  * advertising materials, and other materials related to such
1225  * distribution and use acknowledge that the software was developed
1226  * by the University of California, Berkeley.  The name of the
1227  * University may not be used to endorse or promote products derived
1228  * from this software without specific prior written permission.
1229  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1230  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1231  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1232  */
1233
1234 #define WTMPFILE        "/usr/adm/wtmp"
1235
1236 void
1237 logwtmp(line, name, host)
1238     const char *line, *name, *host;
1239 {
1240     int fd;
1241     struct stat buf;
1242     struct utmp ut;
1243
1244     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1245         return;
1246     if (!fstat(fd, &buf)) {
1247         strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1248         strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
1249         strlcpy(ut.ut_host, host, sizeof(ut.ut_host));
1250         (void)time(&ut.ut_time);
1251         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1252             (void)ftruncate(fd, buf.st_size);
1253     }
1254     close(fd);
1255 }
1256
1257 /*
1258  * Routines for locking and unlocking the serial device, moved here
1259  * from chat.c.
1260  */
1261
1262 #define LOCK_PREFIX     "/usr/spool/uucp/LCK/LCK.."
1263
1264 /*
1265  * lock - create a lock file for the named device
1266  */
1267 int
1268 lock(dev)
1269     char *dev;
1270 {
1271     int fd, pid, n;
1272     char *p;
1273     size_t l;
1274
1275     if ((p = strrchr(dev, '/')) != NULL)
1276         dev = p + 1;
1277     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1278     lock_file = malloc(l);
1279     if (lock_file == NULL)
1280         novm("lock file name");
1281     slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1282
1283     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1284         if (errno == EEXIST
1285             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1286             /* Read the lock file to find out who has the device locked */
1287             n = read(fd, &pid, sizeof(pid));
1288             if (n <= 0) {
1289                 error("Can't read pid from lock file %s", lock_file);
1290                 close(fd);
1291             } else {
1292                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1293                     /* pid no longer exists - remove the lock file */
1294                     if (unlink(lock_file) == 0) {
1295                         close(fd);
1296                         notice("Removed stale lock on %s (pid %d)",
1297                                dev, pid);
1298                         continue;
1299                     } else
1300                         warn("Couldn't remove stale lock on %s", dev);
1301                 } else
1302                     notice("Device %s is locked by pid %d",
1303                            dev, pid);
1304             }
1305             close(fd);
1306         } else
1307             error("Can't create lock file %s: %m", lock_file);
1308         free(lock_file);
1309         lock_file = NULL;
1310         return -1;
1311     }
1312
1313     pid = getpid();
1314     write(fd, &pid, sizeof pid);
1315
1316     close(fd);
1317     return 0;
1318 }
1319
1320 /*
1321  * unlock - remove our lockfile
1322  */
1323 void
1324 unlock()
1325 {
1326     if (lock_file) {
1327         unlink(lock_file);
1328         free(lock_file);
1329         lock_file = NULL;
1330     }
1331 }
1332
1333 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1334 int
1335 ioctl(fd, cmd, c)
1336     int fd, cmd;
1337     caddr_t c;
1338 {
1339 #undef  ioctl
1340     int ret;
1341
1342 #ifdef DEBUGIOCTL
1343     int serrno;
1344     u_char let, code, size;
1345
1346     size = (cmd >> 16) & IOCPARM_MASK;
1347     let = (cmd >> 8);
1348     code = cmd;
1349
1350     if (let == 't' && (75 <= code && code <= 90))
1351     info("ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1352            let, code, size, c);
1353 #endif
1354
1355     ret = ioctl(fd, cmd, c);
1356
1357 #ifdef DEBUGIOCTL
1358     serrno = errno;
1359     if (ret == -1)
1360         info("ioctl('%c', %d, %d) errno = %d (%m)\n",
1361                 let, code, size, errno);
1362     if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1363         int i, len = ((cmd >> 16) & IOCPARM_MASK);
1364         for (i = 0; i < len / 4; ++i)
1365                 info("word[%d] @ 0x%06x = 0x%x\n",
1366                        i, &((int *) c)[i],((int *)c)[i]);
1367     }
1368     errno = serrno;
1369 #endif
1370
1371     if (ret == -1 && errno == EPERM)
1372         errno = ret = 0;
1373     return ret;
1374 }
1375 #endif  /* HAS_BROKEN_IOCTL */
1376
1377
1378 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1379
1380 /*
1381  * These redefinitions of Posix functions are necessary
1382  * because HPPA systems have an OS bug that causes 
1383  * sigaction to core dump:
1384  *
1385  * AlainF 9-Nov-1994    HACK FOR HP-PA/NEXTSTEP
1386  *                      sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1387  *                      Posix lib. This causes pppd to SIGBUS at the expiration
1388  *                      of the first timeout (_sigtramp seems to invoke
1389  *                      the SIGALRM handler at an unreasonably low address).
1390  *                      All calls so sigaction(3) have been changed to calls
1391  *                      to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1392  *                      sigblock(2).
1393  *                      This is kind of a hack, especially since there are
1394  *                      other routines of the Posix lib still used, but
1395  *                      it worked for me.
1396  *
1397  * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1398  * have the same bug.  Thus this fix has been enabled for SPARC also.
1399  *
1400  *
1401  */
1402
1403 int sigemptyset(sigset_t *mask)
1404 {
1405   *mask = 0;
1406 }
1407
1408 sigaddset(sigset_t *mask, int which_sig)
1409 {
1410   *mask |= sigmask(which_sig);
1411 }
1412
1413
1414 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1415 {
1416    struct sigvec sv;
1417    static int in = 0;
1418
1419    sv.sv_handler = act->sa_handler;
1420    sv.sv_mask = act->sa_mask;
1421    sv.sv_flags = 0;
1422
1423    if (!in)
1424      {
1425        in = 1;
1426        warn("PPPD: Inside modified HP and SPARC sigaction\n");
1427      }
1428
1429    return sigvec(sig, &sv, NULL);
1430 }
1431
1432 #endif
1433
1434
1435 /*
1436  * Code following is added for 2.3 compatibility
1437  */
1438
1439 /*
1440  * get_idle_time - return how long the link has been idle.
1441  */
1442 int
1443 get_idle_time(u, ip)
1444     int u;
1445     struct ppp_idle *ip;
1446 {
1447   return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0); 
1448 }
1449
1450
1451 /*
1452  * get_loop_output - read characters from the loopback, form them
1453  * into frames, and detect when we want to bring the real link up.
1454  * Return value is 1 if we need to bring up the link, 0 otherwise.
1455  */
1456 int
1457 get_loop_output()
1458 {
1459
1460 #if 0
1461     int rv = 0;
1462     int n;
1463
1464     while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1465         if (loop_chars(inbuf, n))
1466             rv = 1;
1467     }
1468
1469     if (n == 0)
1470         fatal("eof on loopback");
1471     if (errno != EWOULDBLOCK)
1472         fatal("read from loopback: %m");
1473
1474     return rv;
1475 #endif
1476
1477     return 0;
1478 }
1479
1480 /*
1481  * sifnpmode - Set the mode for handling packets for a given NP.
1482  */
1483 int
1484 sifnpmode(u, proto, mode)
1485     int u;
1486     int proto;
1487     enum NPmode mode;
1488 {
1489     struct npioctl npi;
1490
1491     npi.protocol = proto;
1492     npi.mode = mode;
1493     if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1494         error("ioctl(set NP %d mode to %d): %m", proto, mode);
1495         return 0;
1496     }
1497     return 1;
1498 }
1499
1500
1501 /*
1502  * open_ppp_loopback - open the device we use for getting
1503  * packets in demand mode, and connect it to a ppp interface.
1504  * Here we use a pty.
1505  */
1506 int
1507 open_ppp_loopback()
1508 {
1509
1510 #if 0
1511     int flags;
1512     struct termios tios;
1513     int pppdisc = PPPDISC;
1514
1515     fatal("open_ppp_loopback called!");
1516
1517     if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0)
1518         fatal("No free pty for loopback");
1519     SYSDEBUG(("using %s for loopback", loop_name));
1520
1521     if (tcgetattr(loop_slave, &tios) == 0) {
1522         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1523         tios.c_cflag |= CS8 | CREAD;
1524         tios.c_iflag = IGNPAR;
1525         tios.c_oflag = 0;
1526         tios.c_lflag = 0;
1527         if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1528             warn("couldn't set attributes on loopback: %m");
1529     }
1530
1531     if ((flags = fcntl(loop_master, F_GETFL)) != -1) 
1532         if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1533             warn("couldn't set loopback to nonblock: %m");
1534
1535     ttyfd = loop_slave;
1536     if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0)
1537         fatal("ioctl(TIOCSETD): %m");
1538
1539     /*
1540      * Find out which interface we were given.
1541      */
1542     if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0)
1543         fatal("ioctl(PPPIOCGUNIT): %m");
1544
1545     /*
1546      * Enable debug in the driver if requested.
1547      */
1548     if (kdebugflag) {
1549         if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1550             warn("ioctl (PPPIOCGFLAGS): %m");
1551         } else {
1552             flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1553             if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1554                 warn("ioctl(PPPIOCSFLAGS): %m");
1555         }
1556     }
1557
1558     return loop_master;
1559 #endif
1560
1561 }
1562
1563 /*
1564  * restore_loop - reattach the ppp unit to the loopback.
1565  */
1566 void
1567 restore_loop()
1568 {
1569     int x;
1570
1571     /*
1572      * Transfer the ppp interface back to the loopback.
1573      */
1574     if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0)
1575         fatal("ioctl(transfer ppp unit): %m");
1576     x = PPPDISC;
1577     if (ioctl(loop_slave, TIOCSETD, &x) < 0)
1578         fatal("ioctl(TIOCSETD): %m");
1579
1580     /*
1581      * Check that we got the same unit again.
1582      */
1583     if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
1584         fatal("ioctl(PPPIOCGUNIT): %m");
1585     if (x != ifunit)
1586         fatal("transfer_ppp failed: wanted unit %d, got %d",
1587               ifunit, x);
1588     ttyfd = loop_slave;
1589 }
1590
1591
1592 /*
1593  * Use the hostid as part of the random number seed.
1594  */
1595 int
1596 get_host_seed()
1597 {
1598     return gethostid();
1599 }
1600
1601
1602 /*
1603  * sys_check_options - check the options that the user specified
1604  */
1605 int
1606 sys_check_options()
1607 {
1608   /*
1609    * We don't support demand dialing yet.
1610    */
1611   if (demand)
1612     {
1613       option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1614       return 0;
1615     }
1616   return 1;
1617 }
1618
1619
1620 /*
1621  * sys_close - Clean up in a child process before execing.
1622  */
1623 void
1624 sys_close()
1625 {
1626     close(sockfd);
1627     if (loop_slave >= 0) {
1628         close(loop_slave);
1629         close(loop_master);
1630     }
1631     closelog();
1632 }
1633
1634 #if 0
1635 /*
1636  * wait_loop_output - wait until there is data available on the
1637  * loopback, for the length of time specified by *timo (indefinite
1638  * if timo is NULL).
1639  */
1640 void wait_loop_output(timo)
1641     struct timeval *timo;
1642 {
1643     fd_set ready;
1644     int n;
1645
1646     FD_ZERO(&ready);
1647     FD_SET(loop_master, &ready);
1648     n = select(loop_master + 1, &ready, NULL, &ready, timo);
1649     if (n < 0 && errno != EINTR)
1650         fatal("select: %m");
1651 }
1652
1653 /*
1654  * wait_time - wait for a given length of time or until a
1655  * signal is received.
1656  */
1657 void wait_time(timo)
1658     struct timeval *timo;
1659 {
1660     int n;
1661
1662     n = select(0, NULL, NULL, NULL, timo);
1663     if (n < 0 && errno != EINTR)
1664         fatal("select: %m");
1665 }
1666 #endif