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