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