]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-NeXT.c
make establish/disestablish_ppp members of the channel struct,
[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 #define RCSID   "$Id: sys-NeXT.c,v 1.20 1999/08/13 06:46:17 paulus Exp $"
23
24 #include <stdio.h>
25 #include <termios.h>
26 #include <utmp.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <libc.h>
30 #include <strings.h>
31 #include <sys/types.h>
32 #include <sys/file.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <sys/time.h>
36 #include <sys/errno.h>
37 #include <sys/stat.h>
38 #include <sys/fcntl.h>
39
40 #include <net/if.h>
41 #include <net/ppp_defs.h>
42 #include <net/if_ppp.h>
43 #include <netdb.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in_var.h>
47 #if !(NS_TARGET >= 40)
48 /* XXX get an error "duplicate member ip_v under 4.1 GAMMA */
49 #include <netinet/ip.h>
50 #endif /* NS_TARGET */
51 #include <netinet/if_ether.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54
55 #include <netinfo/ni.h>
56
57 #include "pppd.h"
58
59 static const char rcsid[] = RCSID;
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 int sockfd;              /* socket for doing interface ioctls */
77 static int pppdev;  /* +++ */
78
79 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
80 #define ioctl   myioctl
81 #endif
82
83 static int if_is_up;            /* the interface is currently up */
84 static u_int32_t default_route_gateway; /* gateway addr for default route */
85 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
86
87 /* Prototypes for procedures local to this file. */
88 static int translate_speed __P((int));
89 static int baud_rate_of __P((int));
90 static int dodefaultroute __P((u_int32_t, int));
91 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
92 static int ether_by_host __P((char *, struct ether_addr *));
93
94
95 /*
96  * sys_init - System-dependent initialization.
97  */
98 void
99 sys_init()
100 {
101     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
102     setlogmask(LOG_UPTO(LOG_INFO));
103
104     /* Get an internet socket for doing socket ioctl's on. */
105     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
106         fatal("Couldn't create IP socket: %m");
107
108     if((pppdev = open("/dev/ppp0", O_RDWR, O_NONBLOCK)) == NULL)
109         fatal("Couldn't open /dev/ppp0: %m");
110       
111     FD_ZERO(&in_fds);
112     max_in_fd = 0;
113 }
114
115 /*
116  * sys_cleanup - restore any system state we modified before exiting:
117  * mark the interface down, delete default route and/or proxy arp entry.
118  * This should call die() because it's called from die().
119  */
120 void
121 sys_cleanup()
122 {
123     struct ifreq ifr;
124
125     if (if_is_up) {
126         strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
127         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
128             && ((ifr.ifr_flags & IFF_UP) != 0)) {
129             ifr.ifr_flags &= ~IFF_UP;
130             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
131         }
132     }
133
134     if (default_route_gateway)
135         cifdefaultroute(0, 0, default_route_gateway);
136     if (proxy_arp_addr)
137         cifproxyarp(0, proxy_arp_addr);
138
139     close(pppdev);
140 }
141
142 /*
143  * ppp_available - check whether the system has any ppp interfaces
144  * (in fact we check whether we can do an ioctl on ppp0).
145  */
146 int
147 ppp_available()
148 {
149     int s, ok;
150     struct ifreq ifr;
151     extern char *no_ppp_msg;
152
153     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
154         return 1;               /* can't tell - maybe we're not root */
155
156     strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
157     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
158     close(s);
159
160     no_ppp_msg = "\
161 This system lacks kernel support for PPP.  To include PPP support\n\
162 in the kernel, please follow the steps detailed in the README.NeXT\n\
163 file in the ppp-2.2 distribution.\n";
164
165     return ok;
166 }
167
168 /*
169  * establish_ppp - Turn the serial port into a ppp interface.
170  */
171 int
172 establish_ppp(fd)
173     int fd;
174 {
175     int pppdisc = PPPDISC;
176     int x;
177
178     if (ioctl(fd, TIOCGETD, &initdisc) < 0)
179         fatal("ioctl(TIOCGETD): %m");
180     if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
181         fatal("ioctl(establish TIOCSETD): %m");
182
183     /*
184      * Find out which interface we were given.
185      */
186     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
187         fatal("ioctl(PPPIOCGUNIT): %m");
188
189     /*
190      * Enable debug in the driver if requested.
191      */
192     if (kdebugflag) {
193         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
194             warn("ioctl(PPPIOCGFLAGS): %m");
195         } else {
196             x |= (kdebugflag & 0xFF) * SC_DEBUG;
197             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
198                 warn("ioctl(PPPIOCSFLAGS): %m");
199         }
200     }
201
202     /*
203      * Set device for non-blocking reads so PPPD can poll for
204      * input from the kernel.
205      */
206     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
207         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
208         warn("Couldn't set device to non-blocking mode: %m");
209     }
210
211     return fd;
212 }
213
214
215 /*
216  * disestablish_ppp - Restore the serial port to normal operation.
217  * This shouldn't call die() because it's called from die().
218  */
219 void
220 disestablish_ppp(fd)
221     int fd;
222 {
223     /* Reset non-blocking mode on fd. */
224     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
225         warn("Couldn't restore device fd flags: %m");
226     initfdflags = -1;
227
228     /* Restore old line discipline. */
229     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
230         error("ioctl(TIOCSETD): %m");
231     initdisc = -1;
232 }
233
234 /*
235  * Check whether the link seems not to be 8-bit clean.
236  */
237 void
238 clean_check()
239 {
240     int x;
241     char *s;
242
243     if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
244         s = NULL;
245         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
246         case SC_RCV_B7_0:
247             s = "bit 7 set to 1";
248             break;
249         case SC_RCV_B7_1:
250             s = "bit 7 set to 0";
251             break;
252         case SC_RCV_EVNP:
253             s = "odd parity";
254             break;
255         case SC_RCV_ODDP:
256             s = "even parity";
257             break;
258         }
259         if (s != NULL) {
260             warn("Serial link is not 8-bit clean:");
261             warn("All received characters had %s", s);
262         }
263     }
264 }
265
266 /*
267  * List of valid speeds.
268  */
269 struct speed {
270     int speed_int, speed_val;
271 } speeds[] = {
272 #ifdef B50
273     { 50, B50 },
274 #endif
275 #ifdef B75
276     { 75, B75 },
277 #endif
278 #ifdef B110
279     { 110, B110 },
280 #endif
281 #ifdef B134
282     { 134, B134 },
283 #endif
284 #ifdef B150
285     { 150, B150 },
286 #endif
287 #ifdef B200
288     { 200, B200 },
289 #endif
290 #ifdef B300
291     { 300, B300 },
292 #endif
293 #ifdef B600
294     { 600, B600 },
295 #endif
296 #ifdef B1200
297     { 1200, B1200 },
298 #endif
299 #ifdef B1800
300     { 1800, B1800 },
301 #endif
302 #ifdef B2000
303     { 2000, B2000 },
304 #endif
305 #ifdef B2400
306     { 2400, B2400 },
307 #endif
308 #ifdef B3600
309     { 3600, B3600 },
310 #endif
311 #ifdef B4800
312     { 4800, B4800 },
313 #endif
314 #ifdef B7200
315     { 7200, B7200 },
316 #endif
317 #ifdef B9600
318     { 9600, B9600 },
319 #endif
320 #ifdef B19200
321     { 19200, B19200 },
322 #endif
323 #ifdef B38400
324     { 38400, B38400 },
325 #endif
326 #ifdef EXTA
327     { 19200, EXTA },
328 #endif
329 #ifdef EXTB
330     { 38400, EXTB },
331 #endif
332 #ifdef B14400
333     { 14400, B14400 },
334 #endif
335 #ifdef B28800
336     { 28800, B28800 },
337 #endif
338 #ifdef B43200
339     { 43200, B43200 },
340 #endif
341 #ifdef B57600
342     { 57600, B57600 },
343 #endif
344 /*
345 #ifndef B115200
346 #warning Defining B115200
347 #define B115200 20
348 #endif
349 */
350 #ifdef B115200
351     { 115200, B115200 },
352 #endif
353     { 0, 0 }
354 };
355
356 /*
357  * Translate from bits/second to a speed_t.
358  */
359 int
360 translate_speed(bps)
361     int bps;
362 {
363     struct speed *speedp;
364
365     if (bps == 0)
366         return 0;
367     for (speedp = speeds; speedp->speed_int; speedp++)
368         if (bps == speedp->speed_int)
369             return speedp->speed_val;
370     warn("speed %d not supported", bps);
371     return 0;
372 }
373
374 /*
375  * Translate from a speed_t to bits/second.
376  */
377 static int
378 baud_rate_of(speed)
379     int speed;
380 {
381     struct speed *speedp;
382
383     if (speed == 0)
384         return 0;
385     for (speedp = speeds; speedp->speed_int; speedp++)
386         if (speed == speedp->speed_val)
387             return speedp->speed_int;
388     return 0;
389 }
390
391
392 /*
393  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
394  * at the requested speed, etc.  If `local' is true, set CLOCAL
395  * regardless of whether the modem option was specified.
396  */
397 void
398 set_up_tty(fd, local)
399     int fd, local;
400 {
401     int speed, x, modembits;
402     struct termios tios;
403
404     if (tcgetattr(fd, &tios) < 0)
405         fatal("tcgetattr: %m");
406
407     if (!restore_term)
408         inittermios = tios;
409
410     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
411
412     tios.c_cflag |= CS8 | CREAD | HUPCL;
413     if (local || !modem)
414         tios.c_cflag |= CLOCAL;
415
416     tios.c_iflag = IGNBRK | IGNPAR;
417     tios.c_oflag = 0;
418     tios.c_lflag = 0;
419     tios.c_cc[VMIN] = 1;
420     tios.c_cc[VTIME] = 0;
421
422     if (crtscts == -2) {
423         tios.c_iflag |= IXON | IXOFF;
424         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
425         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
426     }
427
428     speed = translate_speed(inspeed);
429     if (speed) {
430         cfsetospeed(&tios, speed);
431         cfsetispeed(&tios, speed);
432     } else {
433         speed = cfgetospeed(&tios);
434         /*
435          * We can't proceed if the serial port speed is B0,
436          * since that implies that the serial port is disabled.
437          */
438         if (speed == B0)
439             fatal("Baud rate for %s is 0; need explicit baud rate",
440                   devnam);
441     }
442
443     if (modem) {
444       modembits = TIOCM_RTS | TIOCM_CTS;
445       if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
446         error("ioctl: TIOCMBIS/BIC: %m");
447     }
448
449     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
450         fatal("tcsetattr: %m");
451
452    baud_rate = inspeed = baud_rate_of(speed);
453    restore_term = 1;
454 }
455
456 /*
457  * restore_tty - restore the terminal to the saved settings.
458  */
459 void
460 restore_tty(fd)
461     int fd;
462 {
463     if (restore_term) {
464         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
465             if (errno != ENXIO)
466                 warn("tcsetattr: %m");
467         restore_term = 0;
468     }
469 }
470
471 /*
472  * setdtr - control the DTR line on the serial port.
473  * This is called from die(), so it shouldn't call die().
474  *
475  * The write hack is to get NXFax to recognize that there is
476  * activity on the port.  Not using the write nukes
477  * NXFax's capability to determine port usage.
478  *
479  */
480 void
481 setdtr(fd, on)
482 int fd, on;
483 {
484     int modembits = TIOCM_DTR;
485
486     if (!on)
487       {
488         write(fd, " ", 1);
489         sleep(1);
490       }
491
492 /*    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
493     ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
494 }
495
496
497 /*
498  * output - Output PPP packet.
499  */
500 void
501 output(unit, p, len)
502     int unit;
503     u_char *p;
504     int len;
505 {
506     if (debug)
507         dbglog("sent %P", p, len);
508
509     if (write(ttyfd, p, len) < 0) {
510         if (errno == EWOULDBLOCK || errno == ENOBUFS
511             || errno == ENXIO || errno == EIO) {
512             warn("write: warning: %m");
513         } else {
514             fatal("write: %m");
515         }
516     }
517 }
518
519
520 /*
521  * wait_input - wait until there is data available,
522  * for the length of time specified by *timo (indefinite
523  * if timo is NULL).
524  */
525 void
526 wait_input(timo)
527     struct timeval *timo;
528 {
529     fd_set ready;
530     int n;
531
532     ready = in_fds;
533     n = select(max_in_fd + 1, &ready, NULL, &ready, timo);
534     if (n < 0 && errno != EINTR)
535         fatal("select: %m");
536 }
537
538
539 /*
540  * add_fd - add an fd to the set that wait_input waits for.
541  */
542 void add_fd(fd)
543     int fd;
544 {
545     FD_SET(fd, &in_fds);
546     if (fd > max_in_fd)
547         max_in_fd = fd;
548 }
549
550 /*
551  * remove_fd - remove an fd from the set that wait_input waits for.
552  */
553 void remove_fd(fd)
554     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 #if 0
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 #endif
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         strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1248         strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1249         strncpy(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 #if 0
1258 /*
1259  * Routines for locking and unlocking the serial device, moved here
1260  * from chat.c.
1261  */
1262
1263 #define LOCK_PREFIX     "/usr/spool/uucp/LCK/LCK.."
1264
1265 static char *lock_file;
1266
1267 /*
1268  * lock - create a lock file for the named device
1269  */
1270 int
1271 lock(dev)
1272     char *dev;
1273 {
1274     int fd, pid, n;
1275     char *p;
1276     size_t l;
1277
1278     if ((p = strrchr(dev, '/')) != NULL)
1279         dev = p + 1;
1280     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1281     lock_file = malloc(l);
1282     if (lock_file == NULL)
1283         novm("lock file name");
1284     slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1285
1286     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1287         if (errno == EEXIST
1288             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1289             /* Read the lock file to find out who has the device locked */
1290             n = read(fd, &pid, sizeof(pid));
1291             if (n <= 0) {
1292                 error("Can't read pid from lock file %s", lock_file);
1293                 close(fd);
1294             } else {
1295                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1296                     /* pid no longer exists - remove the lock file */
1297                     if (unlink(lock_file) == 0) {
1298                         close(fd);
1299                         notice("Removed stale lock on %s (pid %d)",
1300                                dev, pid);
1301                         continue;
1302                     } else
1303                         warn("Couldn't remove stale lock on %s", dev);
1304                 } else
1305                     notice("Device %s is locked by pid %d",
1306                            dev, pid);
1307             }
1308             close(fd);
1309         } else
1310             error("Can't create lock file %s: %m", lock_file);
1311         free(lock_file);
1312         lock_file = NULL;
1313         return -1;
1314     }
1315
1316     pid = getpid();
1317     write(fd, &pid, sizeof pid);
1318
1319     close(fd);
1320     return 0;
1321 }
1322
1323 /*
1324  * unlock - remove our lockfile
1325  */
1326 void
1327 unlock()
1328 {
1329     if (lock_file) {
1330         unlink(lock_file);
1331         free(lock_file);
1332         lock_file = NULL;
1333     }
1334 }
1335 #endif
1336
1337 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1338 int
1339 ioctl(fd, cmd, c)
1340     int fd, cmd;
1341     caddr_t c;
1342 {
1343 #undef  ioctl
1344     int ret;
1345
1346 #ifdef DEBUGIOCTL
1347     int serrno;
1348     u_char let, code, size;
1349
1350     size = (cmd >> 16) & IOCPARM_MASK;
1351     let = (cmd >> 8);
1352     code = cmd;
1353
1354     if (let == 't' && (75 <= code && code <= 90))
1355     info("ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1356            let, code, size, c);
1357 #endif
1358
1359     ret = ioctl(fd, cmd, c);
1360
1361 #ifdef DEBUGIOCTL
1362     serrno = errno;
1363     if (ret == -1)
1364         info("ioctl('%c', %d, %d) errno = %d (%m)\n",
1365                 let, code, size, errno);
1366     if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1367         int i, len = ((cmd >> 16) & IOCPARM_MASK);
1368         for (i = 0; i < len / 4; ++i)
1369                 info("word[%d] @ 0x%06x = 0x%x\n",
1370                        i, &((int *) c)[i],((int *)c)[i]);
1371     }
1372     errno = serrno;
1373 #endif
1374
1375     if (ret == -1 && errno == EPERM)
1376         errno = ret = 0;
1377     return ret;
1378 }
1379 #endif  /* HAS_BROKEN_IOCTL */
1380
1381
1382 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1383
1384 /*
1385  * These redefinitions of Posix functions are necessary
1386  * because HPPA systems have an OS bug that causes 
1387  * sigaction to core dump:
1388  *
1389  * AlainF 9-Nov-1994    HACK FOR HP-PA/NEXTSTEP
1390  *                      sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1391  *                      Posix lib. This causes pppd to SIGBUS at the expiration
1392  *                      of the first timeout (_sigtramp seems to invoke
1393  *                      the SIGALRM handler at an unreasonably low address).
1394  *                      All calls so sigaction(3) have been changed to calls
1395  *                      to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1396  *                      sigblock(2).
1397  *                      This is kind of a hack, especially since there are
1398  *                      other routines of the Posix lib still used, but
1399  *                      it worked for me.
1400  *
1401  * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1402  * have the same bug.  Thus this fix has been enabled for SPARC also.
1403  *
1404  *
1405  */
1406
1407 int sigemptyset(sigset_t *mask)
1408 {
1409   *mask = 0;
1410 }
1411
1412 sigaddset(sigset_t *mask, int which_sig)
1413 {
1414   *mask |= sigmask(which_sig);
1415 }
1416
1417
1418 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1419 {
1420    struct sigvec sv;
1421    static int in = 0;
1422
1423    sv.sv_handler = act->sa_handler;
1424    sv.sv_mask = act->sa_mask;
1425    sv.sv_flags = 0;
1426
1427    if (!in)
1428      {
1429        in = 1;
1430        warn("PPPD: Inside modified HP and SPARC sigaction\n");
1431      }
1432
1433    return sigvec(sig, &sv, NULL);
1434 }
1435
1436 #endif
1437
1438
1439 /*
1440  * Code following is added for 2.3 compatibility
1441  */
1442
1443 /*
1444  * get_idle_time - return how long the link has been idle.
1445  */
1446 int
1447 get_idle_time(u, ip)
1448     int u;
1449     struct ppp_idle *ip;
1450 {
1451   return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0); 
1452 }
1453
1454 /*
1455  * get_ppp_stats - return statistics for the link.
1456  */
1457 int
1458 get_ppp_stats(u, stats)
1459     int u;
1460     struct pppd_stats *stats;
1461 {
1462     struct ifpppstatsreq req;
1463
1464     memset (&req, 0, sizeof (req));
1465     strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
1466     if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
1467         error("Couldn't get PPP statistics: %m");
1468         return 0;
1469     }
1470     stats->bytes_in = req.stats.p.ppp_ibytes;
1471     stats->bytes_out = req.stats.p.ppp_obytes;
1472     return 1;
1473 }
1474
1475
1476 /*
1477  * get_loop_output - read characters from the loopback, form them
1478  * into frames, and detect when we want to bring the real link up.
1479  * Return value is 1 if we need to bring up the link, 0 otherwise.
1480  */
1481 int
1482 get_loop_output()
1483 {
1484
1485 #if 0
1486     int rv = 0;
1487     int n;
1488
1489     while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1490         if (loop_chars(inbuf, n))
1491             rv = 1;
1492     }
1493
1494     if (n == 0)
1495         fatal("eof on loopback");
1496     if (errno != EWOULDBLOCK)
1497         fatal("read from loopback: %m");
1498
1499     return rv;
1500 #endif
1501
1502     return 0;
1503 }
1504
1505 /*
1506  * sifnpmode - Set the mode for handling packets for a given NP.
1507  */
1508 int
1509 sifnpmode(u, proto, mode)
1510     int u;
1511     int proto;
1512     enum NPmode mode;
1513 {
1514     struct npioctl npi;
1515
1516     npi.protocol = proto;
1517     npi.mode = mode;
1518     if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1519         error("ioctl(set NP %d mode to %d): %m", proto, mode);
1520         return 0;
1521     }
1522     return 1;
1523 }
1524
1525
1526 /*
1527  * open_ppp_loopback - open the device we use for getting
1528  * packets in demand mode, and connect it to a ppp interface.
1529  * Here we use a pty.
1530  */
1531 int
1532 open_ppp_loopback()
1533 {
1534
1535 #if 0
1536     int flags;
1537     struct termios tios;
1538     int pppdisc = PPPDISC;
1539
1540     fatal("open_ppp_loopback called!");
1541
1542     if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0)
1543         fatal("No free pty for loopback");
1544     SYSDEBUG(("using %s for loopback", loop_name));
1545
1546     if (tcgetattr(loop_slave, &tios) == 0) {
1547         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1548         tios.c_cflag |= CS8 | CREAD;
1549         tios.c_iflag = IGNPAR;
1550         tios.c_oflag = 0;
1551         tios.c_lflag = 0;
1552         if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1553             warn("couldn't set attributes on loopback: %m");
1554     }
1555
1556     if ((flags = fcntl(loop_master, F_GETFL)) != -1) 
1557         if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1558             warn("couldn't set loopback to nonblock: %m");
1559
1560     ttyfd = loop_slave;
1561     if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0)
1562         fatal("ioctl(TIOCSETD): %m");
1563
1564     /*
1565      * Find out which interface we were given.
1566      */
1567     if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0)
1568         fatal("ioctl(PPPIOCGUNIT): %m");
1569
1570     /*
1571      * Enable debug in the driver if requested.
1572      */
1573     if (kdebugflag) {
1574         if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1575             warn("ioctl (PPPIOCGFLAGS): %m");
1576         } else {
1577             flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1578             if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1579                 warn("ioctl(PPPIOCSFLAGS): %m");
1580         }
1581     }
1582
1583     return loop_master;
1584 #endif
1585
1586 }
1587
1588 /*
1589  * restore_loop - reattach the ppp unit to the loopback.
1590  */
1591 void
1592 restore_loop()
1593 {
1594     int x;
1595
1596     /*
1597      * Transfer the ppp interface back to the loopback.
1598      */
1599     if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0)
1600         fatal("ioctl(transfer ppp unit): %m");
1601     x = PPPDISC;
1602     if (ioctl(loop_slave, TIOCSETD, &x) < 0)
1603         fatal("ioctl(TIOCSETD): %m");
1604
1605     /*
1606      * Check that we got the same unit again.
1607      */
1608     if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
1609         fatal("ioctl(PPPIOCGUNIT): %m");
1610     if (x != ifunit)
1611         fatal("transfer_ppp failed: wanted unit %d, got %d",
1612               ifunit, x);
1613     ttyfd = loop_slave;
1614 }
1615
1616
1617 /*
1618  * Use the hostid as part of the random number seed.
1619  */
1620 int
1621 get_host_seed()
1622 {
1623     return gethostid();
1624 }
1625
1626
1627 /*
1628  * sys_check_options - check the options that the user specified
1629  */
1630 int
1631 sys_check_options()
1632 {
1633   /*
1634    * We don't support demand dialing yet.
1635    */
1636   if (demand)
1637     {
1638       option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1639       return 0;
1640     }
1641   return 1;
1642 }
1643
1644
1645 /*
1646  * sys_close - Clean up in a child process before execing.
1647  */
1648 void
1649 sys_close()
1650 {
1651     close(sockfd);
1652     if (loop_slave >= 0) {
1653         close(loop_slave);
1654         close(loop_master);
1655     }
1656     closelog();
1657 }
1658
1659 #if 0
1660 /*
1661  * wait_loop_output - wait until there is data available on the
1662  * loopback, for the length of time specified by *timo (indefinite
1663  * if timo is NULL).
1664  */
1665 void wait_loop_output(timo)
1666     struct timeval *timo;
1667 {
1668     fd_set ready;
1669     int n;
1670
1671     FD_ZERO(&ready);
1672     FD_SET(loop_master, &ready);
1673     n = select(loop_master + 1, &ready, NULL, &ready, timo);
1674     if (n < 0 && errno != EINTR)
1675         fatal("select: %m");
1676 }
1677
1678 /*
1679  * wait_time - wait for a given length of time or until a
1680  * signal is received.
1681  */
1682 void wait_time(timo)
1683     struct timeval *timo;
1684 {
1685     int n;
1686
1687     n = select(0, NULL, NULL, NULL, timo);
1688     if (n < 0 && errno != EINTR)
1689         fatal("select: %m");
1690 }
1691 #endif