]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-sunos4.c
remove a couple of ansi-C-isms.
[ppp.git] / pppd / sys-sunos4.c
1 /*
2  * System-dependent procedures for pppd under SunOS 4.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: sys-sunos4.c,v 1.17 1999/03/19 04:23:50 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <malloc.h>
43 #include <utmp.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include <sys/poll.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/nit_if.h>
56 #include <net/route.h>
57 #include <net/ppp_defs.h>
58 #include <net/pppio.h>
59 #include <netinet/in.h>
60
61 #include "pppd.h"
62
63 #if defined(sun) && defined(sparc)
64 #include <alloca.h>
65 #ifndef __GNUC__
66 extern void *alloca();
67 #endif
68 #endif /*sparc*/
69
70 static int      pppfd;
71 static int      fdmuxid = -1;
72 static int      iffd;
73 static int      sockfd;
74
75 static int      restore_term;
76 static struct termios inittermios;
77 static struct winsize wsinfo;   /* Initial window size info */
78 static pid_t    parent_pid;     /* PID of our parent */
79
80 extern u_char   inpacket_buf[]; /* borrowed from main.c */
81
82 #define MAX_POLLFDS     32
83 static struct pollfd pollfds[MAX_POLLFDS];
84 static int n_pollfds;
85
86 static int      link_mtu, link_mru;
87
88 #define NMODULES        32
89 static int      tty_nmodules;
90 static char     tty_modules[NMODULES][FMNAMESZ+1];
91
92 static int      if_is_up;       /* Interface has been marked up */
93 static u_int32_t ifaddrs[2];    /* local and remote addresses */
94 static u_int32_t default_route_gateway; /* Gateway for default route added */
95 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
96
97 /* Prototypes for procedures local to this file. */
98 static int translate_speed __P((int));
99 static int baud_rate_of __P((int));
100 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
101 static int strioctl __P((int, int, void *, int, int));
102
103
104 /*
105  * sys_init - System-dependent initialization.
106  */
107 void
108 sys_init()
109 {
110     int x;
111
112     /* Get an internet socket for doing socket ioctl's on. */
113     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
114         fatal("Couldn't create IP socket: %m");
115
116     /*
117      * We may want to send a SIGHUP to the session leader associated
118      * with our controlling terminal later.  Because SunOS doesn't
119      * have getsid(), we make do with sending the signal to our
120      * parent process.
121      */
122     parent_pid = getppid();
123
124     /*
125      * Open the ppp device.
126      */
127     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
128     if (pppfd < 0)
129         fatal("Can't open /dev/ppp: %m");
130     if (kdebugflag) {
131         x = PPPDBG_LOG + PPPDBG_DRIVER;
132         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
133     }
134
135     /* Assign a new PPA and get its unit number. */
136     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
137         fatal("Can't create new PPP interface: %m");
138
139     /*
140      * Open the ppp device again and push the if_ppp module on it.
141      */
142     iffd = open("/dev/ppp", O_RDWR, 0);
143     if (iffd < 0)
144         fatal("Can't open /dev/ppp (2): %m");
145     if (kdebugflag) {
146         x = PPPDBG_LOG + PPPDBG_DRIVER;
147         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
148     }
149     if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0)
150         fatal("Couldn't attach ppp interface to device: %m");
151     if (ioctl(iffd, I_PUSH, "if_ppp") < 0)
152         fatal("Can't push ppp interface module: %m");
153     if (kdebugflag) {
154         x = PPPDBG_LOG + PPPDBG_IF;
155         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
156     }
157     if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0)
158         fatal("Couldn't create ppp interface unit: %m");
159     x = PPP_IP;
160     if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0)
161         fatal("Couldn't bind ppp interface to IP SAP: %m");
162
163     n_pollfds = 0;
164 }
165
166 /*
167  * sys_cleanup - restore any system state we modified before exiting:
168  * mark the interface down, delete default route and/or proxy arp entry.
169  * This shouldn't call die() because it's called from die().
170  */
171 void
172 sys_cleanup()
173 {
174     if (if_is_up)
175         sifdown(0);
176     if (ifaddrs[0])
177         cifaddr(0, ifaddrs[0], ifaddrs[1]);
178     if (default_route_gateway)
179         cifdefaultroute(0, 0, default_route_gateway);
180     if (proxy_arp_addr)
181         cifproxyarp(0, proxy_arp_addr);
182 }
183
184 /*
185  * sys_close - Clean up in a child process before execing.
186  */
187 void
188 sys_close()
189 {
190     close(iffd);
191     close(pppfd);
192     close(sockfd);
193 }
194
195 /*
196  * sys_check_options - check the options that the user specified
197  */
198 int
199 sys_check_options()
200 {
201     return 1;
202 }
203
204
205 /*
206  * daemon - Detach us from controlling terminal session.
207  */
208 int
209 daemon(nochdir, noclose)
210     int nochdir, noclose;
211 {
212     int pid;
213
214     if ((pid = fork()) < 0)
215         return -1;
216     if (pid != 0)
217         exit(0);                /* parent dies */
218     setsid();
219     if (!nochdir)
220         chdir("/");
221     if (!noclose) {
222         fclose(stdin);          /* don't need stdin, stdout, stderr */
223         fclose(stdout);
224         fclose(stderr);
225     }
226     return 0;
227 }
228
229 /*
230  * ppp_available - check whether the system has any ppp interfaces
231  */
232 int
233 ppp_available()
234 {
235     struct stat buf;
236
237     return stat("/dev/ppp", &buf) >= 0;
238 }
239
240 /*
241  * establish_ppp - Turn the serial port into a ppp interface.
242  */
243 int
244 establish_ppp(fd)
245     int fd;
246 {
247     int i;
248
249     /* Pop any existing modules off the tty stream. */
250     for (i = 0;; ++i)
251         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
252             || ioctl(fd, I_POP, 0) < 0)
253             break;
254     tty_nmodules = i;
255
256     /* Push the async hdlc module and the compressor module. */
257     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0)
258         fatal("Couldn't push PPP Async HDLC module: %m");
259     if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
260         error("Couldn't push PPP compression module: %m");
261
262     /* Link the serial port under the PPP multiplexor. */
263     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0)
264         fatal("Can't link tty to PPP mux: %m");
265
266     return pppfd;
267 }
268
269 /*
270  * restore_loop - reattach the ppp unit to the loopback.
271  * This doesn't need to do anything because disestablish_ppp does it.
272  */
273 void
274 restore_loop()
275 {
276 }
277
278 /*
279  * disestablish_ppp - Restore the serial port to normal operation.
280  * It attempts to reconstruct the stream with the previously popped
281  * modules.  This shouldn't call die() because it's called from die().
282  */
283 void
284 disestablish_ppp(fd)
285     int fd;
286 {
287     int i;
288
289     if (fdmuxid >= 0) {
290         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
291             if (!hungup)
292                 error("Can't unlink tty from PPP mux: %m");
293         }
294         fdmuxid = -1;
295
296         if (!hungup) {
297             while (ioctl(fd, I_POP, 0) >= 0)
298                 ;
299             for (i = tty_nmodules - 1; i >= 0; --i)
300                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
301                     error("Couldn't restore tty module %s: %m",
302                            tty_modules[i]);
303         }
304         if (hungup && default_device && parent_pid > 0) {
305             /*
306              * If we have received a hangup, we need to send a SIGHUP
307              * to the terminal's controlling process.  The reason is
308              * that the original stream head for the terminal hasn't
309              * seen the M_HANGUP message (it went up through the ppp
310              * driver to the stream head for our fd to /dev/ppp).
311              * Actually we send the signal to the process that invoked
312              * pppd, since SunOS doesn't have getsid().
313              */
314             kill(parent_pid, SIGHUP);
315         }
316     }
317 }
318
319 /*
320  * Check whether the link seems not to be 8-bit clean.
321  */
322 void
323 clean_check()
324 {
325     int x;
326     char *s;
327
328     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
329         return;
330     s = NULL;
331     switch (~x) {
332     case RCV_B7_0:
333         s = "bit 7 set to 1";
334         break;
335     case RCV_B7_1:
336         s = "bit 7 set to 0";
337         break;
338     case RCV_EVNP:
339         s = "odd parity";
340         break;
341     case RCV_ODDP:
342         s = "even parity";
343         break;
344     }
345     if (s != NULL) {
346         warn("Serial link is not 8-bit clean:");
347         warn("All received characters had %s", s);
348     }
349 }
350
351 /*
352  * List of valid speeds.
353  */
354 struct speed {
355     int speed_int, speed_val;
356 } speeds[] = {
357 #ifdef B50
358     { 50, B50 },
359 #endif
360 #ifdef B75
361     { 75, B75 },
362 #endif
363 #ifdef B110
364     { 110, B110 },
365 #endif
366 #ifdef B134
367     { 134, B134 },
368 #endif
369 #ifdef B150
370     { 150, B150 },
371 #endif
372 #ifdef B200
373     { 200, B200 },
374 #endif
375 #ifdef B300
376     { 300, B300 },
377 #endif
378 #ifdef B600
379     { 600, B600 },
380 #endif
381 #ifdef B1200
382     { 1200, B1200 },
383 #endif
384 #ifdef B1800
385     { 1800, B1800 },
386 #endif
387 #ifdef B2000
388     { 2000, B2000 },
389 #endif
390 #ifdef B2400
391     { 2400, B2400 },
392 #endif
393 #ifdef B3600
394     { 3600, B3600 },
395 #endif
396 #ifdef B4800
397     { 4800, B4800 },
398 #endif
399 #ifdef B7200
400     { 7200, B7200 },
401 #endif
402 #ifdef B9600
403     { 9600, B9600 },
404 #endif
405 #ifdef B19200
406     { 19200, B19200 },
407 #endif
408 #ifdef B38400
409     { 38400, B38400 },
410 #endif
411 #ifdef EXTA
412     { 19200, EXTA },
413 #endif
414 #ifdef EXTB
415     { 38400, EXTB },
416 #endif
417 #ifdef B57600
418     { 57600, B57600 },
419 #endif
420 #ifdef B115200
421     { 115200, B115200 },
422 #endif
423     { 0, 0 }
424 };
425
426 /*
427  * Translate from bits/second to a speed_t.
428  */
429 static int
430 translate_speed(bps)
431     int bps;
432 {
433     struct speed *speedp;
434
435     if (bps == 0)
436         return 0;
437     for (speedp = speeds; speedp->speed_int; speedp++)
438         if (bps == speedp->speed_int)
439             return speedp->speed_val;
440     warn("speed %d not supported", bps);
441     return 0;
442 }
443
444 /*
445  * Translate from a speed_t to bits/second.
446  */
447 static int
448 baud_rate_of(speed)
449     int speed;
450 {
451     struct speed *speedp;
452
453     if (speed == 0)
454         return 0;
455     for (speedp = speeds; speedp->speed_int; speedp++)
456         if (speed == speedp->speed_val)
457             return speedp->speed_int;
458     return 0;
459 }
460
461 /*
462  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
463  * at the requested speed, etc.  If `local' is true, set CLOCAL
464  * regardless of whether the modem option was specified.
465  */
466 void
467 set_up_tty(fd, local)
468     int fd, local;
469 {
470     int speed;
471     struct termios tios;
472
473     if (tcgetattr(fd, &tios) < 0)
474         fatal("tcgetattr: %m");
475
476     if (!restore_term) {
477         inittermios = tios;
478         ioctl(fd, TIOCGWINSZ, &wsinfo);
479     }
480
481     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
482     if (crtscts > 0)
483         tios.c_cflag |= CRTSCTS;
484     else if (crtscts < 0)
485         tios.c_cflag &= ~CRTSCTS;
486
487     tios.c_cflag |= CS8 | CREAD | HUPCL;
488     if (local || !modem)
489         tios.c_cflag |= CLOCAL;
490     tios.c_iflag = IGNBRK | IGNPAR;
491     tios.c_oflag = 0;
492     tios.c_lflag = 0;
493     tios.c_cc[VMIN] = 1;
494     tios.c_cc[VTIME] = 0;
495
496     if (crtscts == -2) {
497         tios.c_iflag |= IXON | IXOFF;
498         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
499         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
500     }
501
502     speed = translate_speed(inspeed);
503     if (speed) {
504         cfsetospeed(&tios, speed);
505         cfsetispeed(&tios, speed);
506     } else {
507         speed = cfgetospeed(&tios);
508         /*
509          * We can't proceed if the serial port speed is 0,
510          * since that implies that the serial port is disabled.
511          */
512         if (speed == B0)
513             fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
514     }
515
516     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
517         fatal("tcsetattr: %m");
518
519     baud_rate = inspeed = baud_rate_of(speed);
520     restore_term = 1;
521 }
522
523 /*
524  * restore_tty - restore the terminal to the saved settings.
525  */
526 void
527 restore_tty(fd)
528     int fd;
529 {
530     if (restore_term) {
531         if (!default_device) {
532             /*
533              * Turn off echoing, because otherwise we can get into
534              * a loop with the tty and the modem echoing to each other.
535              * We presume we are the sole user of this tty device, so
536              * when we close it, it will revert to its defaults anyway.
537              */
538             inittermios.c_lflag &= ~(ECHO | ECHONL);
539         }
540         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
541             if (!hungup && errno != ENXIO)
542                 warn("tcsetattr: %m");
543         ioctl(fd, TIOCSWINSZ, &wsinfo);
544         restore_term = 0;
545     }
546 }
547
548 /*
549  * setdtr - control the DTR line on the serial port.
550  * This is called from die(), so it shouldn't call die().
551  */
552 void
553 setdtr(fd, on)
554 int fd, on;
555 {
556     int modembits = TIOCM_DTR;
557
558     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
559 }
560
561 /*
562  * open_loopback - open the device we use for getting packets
563  * in demand mode.  Under SunOS, we use our existing fd
564  * to the ppp driver.
565  */
566 int
567 open_ppp_loopback()
568 {
569     return pppfd;
570 }
571
572 /*
573  * output - Output PPP packet.
574  */
575 void
576 output(unit, p, len)
577     int unit;
578     u_char *p;
579     int len;
580 {
581     struct strbuf data;
582     int retries;
583     struct pollfd pfd;
584
585     if (debug)
586         dbglog("sent %P", p, len);
587
588     data.len = len;
589     data.buf = (caddr_t) p;
590     retries = 4;
591     while (putmsg(pppfd, NULL, &data, 0) < 0) {
592         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
593             if (errno != ENXIO)
594                 error("Couldn't send packet: %m");
595             break;
596         }
597         pfd.fd = pppfd;
598         pfd.events = POLLOUT;
599         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
600     }
601 }
602
603
604 /*
605  * wait_input - wait until there is data available,
606  * for the length of time specified by *timo (indefinite
607  * if timo is NULL).
608  */
609 void
610 wait_input(timo)
611     struct timeval *timo;
612 {
613     int t;
614
615     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
616     if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
617         fatal("poll: %m");
618 }
619
620 /*
621  * add_fd - add an fd to the set that wait_input waits for.
622  */
623 void add_fd(fd)
624     int fd;
625 {
626     int n;
627
628     for (n = 0; n < n_pollfds; ++n)
629         if (pollfds[n].fd == fd)
630             return;
631     if (n_pollfds < MAX_POLLFDS) {
632         pollfds[n_pollfds].fd = fd;
633         pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
634         ++n_pollfds;
635     } else
636         error("Too many inputs!");
637 }
638
639 /*
640  * remove_fd - remove an fd from the set that wait_input waits for.
641  */
642 void remove_fd(fd)
643     int fd;
644 {
645     int n;
646
647     for (n = 0; n < n_pollfds; ++n) {
648         if (pollfds[n].fd == fd) {
649             while (++n < n_pollfds)
650                 pollfds[n-1] = pollfds[n];
651             --n_pollfds;
652             break;
653         }
654     }
655 }
656
657 #if 0
658 /*
659  * wait_loop_output - wait until there is data available on the
660  * loopback, for the length of time specified by *timo (indefinite
661  * if timo is NULL).
662  */
663 void
664 wait_loop_output(timo)
665     struct timeval *timo;
666 {
667     wait_input(timo);
668 }
669
670 /*
671  * wait_time - wait for a given length of time or until a
672  * signal is received.
673  */
674 void
675 wait_time(timo)
676     struct timeval *timo;
677 {
678     int n;
679
680     n = select(0, NULL, NULL, NULL, timo);
681     if (n < 0 && errno != EINTR)
682         fatal("select: %m");
683 }
684 #endif
685
686 /*
687  * read_packet - get a PPP packet from the serial device.
688  */
689 int
690 read_packet(buf)
691     u_char *buf;
692 {
693     struct strbuf ctrl, data;
694     int flags, len;
695     unsigned char ctrlbuf[64];
696
697     for (;;) {
698         data.maxlen = PPP_MRU + PPP_HDRLEN;
699         data.buf = (caddr_t) buf;
700         ctrl.maxlen = sizeof(ctrlbuf);
701         ctrl.buf = (caddr_t) ctrlbuf;
702         flags = 0;
703         len = getmsg(pppfd, &ctrl, &data, &flags);
704         if (len < 0) {
705             if (errno = EAGAIN || errno == EINTR)
706                 return -1;
707             fatal("Error reading packet: %m");
708         }
709
710         if (ctrl.len <= 0)
711             return data.len;
712
713         /*
714          * Got a M_PROTO or M_PCPROTO message.  Huh?
715          */
716         if (debug)
717             dbglog("got ctrl msg len=%d", ctrl.len);
718
719     }
720 }
721
722 /*
723  * get_loop_output - get outgoing packets from the ppp device,
724  * and detect when we want to bring the real link up.
725  * Return value is 1 if we need to bring up the link, 0 otherwise.
726  */
727 int
728 get_loop_output()
729 {
730     int len;
731     int rv = 0;
732
733     while ((len = read_packet(inpacket_buf)) > 0) {
734         if (loop_frame(inpacket_buf, len))
735             rv = 1;
736     }
737     return rv;
738 }
739
740 /*
741  * ppp_send_config - configure the transmit characteristics of
742  * the ppp interface.
743  */
744 void
745 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
746     int unit, mtu;
747     u_int32_t asyncmap;
748     int pcomp, accomp;
749 {
750     int cf[2];
751     struct ifreq ifr;
752
753     link_mtu = mtu;
754     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
755         if (hungup && errno == ENXIO)
756             return;
757         error("Couldn't set MTU: %m");
758     }
759     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
760         error("Couldn't set transmit ACCM: %m");
761     }
762     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
763     cf[1] = COMP_PROT | COMP_AC;
764     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
765         error("Couldn't set prot/AC compression: %m");
766     }
767
768     /* set mtu for ip as well */
769     memset(&ifr, 0, sizeof(ifr));
770     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
771     ifr.ifr_metric = link_mtu;
772     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
773         error("Couldn't set IP MTU: %m");
774     }
775 }
776
777 /*
778  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
779  */
780 void
781 ppp_set_xaccm(unit, accm)
782     int unit;
783     ext_accm accm;
784 {
785     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
786         if (!hungup || errno != ENXIO)
787             warn("Couldn't set extended ACCM: %m");
788     }
789 }
790
791 /*
792  * ppp_recv_config - configure the receive-side characteristics of
793  * the ppp interface.
794  */
795 void
796 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
797     int unit, mru;
798     u_int32_t asyncmap;
799     int pcomp, accomp;
800 {
801     int cf[2];
802
803     link_mru = mru;
804     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
805         if (hungup && errno == ENXIO)
806             return;
807         error("Couldn't set MRU: %m");
808     }
809     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
810         error("Couldn't set receive ACCM: %m");
811     }
812     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
813     cf[1] = DECOMP_PROT | DECOMP_AC;
814     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
815         error("Couldn't set prot/AC decompression: %m");
816     }
817 }
818
819 /*
820  * ccp_test - ask kernel whether a given compression method
821  * is acceptable for use.
822  */
823 int
824 ccp_test(unit, opt_ptr, opt_len, for_transmit)
825     int unit, opt_len, for_transmit;
826     u_char *opt_ptr;
827 {
828     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
829                  opt_ptr, opt_len, 0) >= 0)
830         return 1;
831     return (errno == ENOSR)? 0: -1;
832 }
833
834 /*
835  * ccp_flags_set - inform kernel about the current state of CCP.
836  */
837 void
838 ccp_flags_set(unit, isopen, isup)
839     int unit, isopen, isup;
840 {
841     int cf[2];
842
843     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
844     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
845     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
846         if (!hungup || errno != ENXIO)
847             error("Couldn't set kernel CCP state: %m");
848     }
849 }
850
851 /*
852  * get_idle_time - return how long the link has been idle.
853  */
854 int
855 get_idle_time(u, ip)
856     int u;
857     struct ppp_idle *ip;
858 {
859     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
860 }
861
862 /*
863  * get_ppp_stats - return statistics for the link.
864  */
865 int
866 get_ppp_stats(u, stats)
867     int u;
868     struct ppp_stats *stats;
869 {
870     if (strioctl(pppfd, PPPIO_GETSTAT, stats, 0, sizeof(*stats)) < 0) {
871         error("Couldn't get link statistics: %m");
872         return 0;
873     }
874     return 1;
875 }
876
877
878 /*
879  * ccp_fatal_error - returns 1 if decompression was disabled as a
880  * result of an error detected after decompression of a packet,
881  * 0 otherwise.  This is necessary because of patent nonsense.
882  */
883 int
884 ccp_fatal_error(unit)
885     int unit;
886 {
887     int cf[2];
888
889     cf[0] = cf[1] = 0;
890     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
891         if (errno != ENXIO && errno != EINVAL)
892             error("Couldn't get compression flags: %m");
893         return 0;
894     }
895     return cf[0] & CCP_FATALERROR;
896 }
897
898 /*
899  * sifvjcomp - config tcp header compression
900  */
901 int
902 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
903     int u, vjcomp, xcidcomp, xmaxcid;
904 {
905     int cf[2];
906     char maxcid[2];
907
908     if (vjcomp) {
909         maxcid[0] = xcidcomp;
910         maxcid[1] = 15;         /* XXX should be rmaxcid */
911         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
912             error("Couldn't initialize VJ compression: %m");
913         }
914     }
915
916     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
917         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
918     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
919     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
920         if (vjcomp)
921             error("Couldn't enable VJ compression: %m");
922     }
923
924     return 1;
925 }
926
927 /*
928  * sifup - Config the interface up and enable IP packets to pass.
929  */
930 int
931 sifup(u)
932     int u;
933 {
934     struct ifreq ifr;
935
936     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
937     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
938         error("Couldn't mark interface up (get): %m");
939         return 0;
940     }
941     ifr.ifr_flags |= IFF_UP;
942     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
943         error("Couldn't mark interface up (set): %m");
944         return 0;
945     }
946     if_is_up = 1;
947     return 1;
948 }
949
950 /*
951  * sifdown - Config the interface down and disable IP.
952  */
953 int
954 sifdown(u)
955     int u;
956 {
957     struct ifreq ifr;
958
959     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
960     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
961         error("Couldn't mark interface down (get): %m");
962         return 0;
963     }
964     if ((ifr.ifr_flags & IFF_UP) != 0) {
965         ifr.ifr_flags &= ~IFF_UP;
966         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
967             error("Couldn't mark interface down (set): %m");
968             return 0;
969         }
970     }
971     if_is_up = 0;
972     return 1;
973 }
974
975 /*
976  * sifnpmode - Set the mode for handling packets for a given NP.
977  */
978 int
979 sifnpmode(u, proto, mode)
980     int u;
981     int proto;
982     enum NPmode mode;
983 {
984     int npi[2];
985
986     npi[0] = proto;
987     npi[1] = (int) mode;
988     if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
989         error("ioctl(set NP %d mode to %d): %m", proto, mode);
990         return 0;
991     }
992     return 1;
993 }
994
995 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
996
997 /*
998  * sifaddr - Config the interface IP addresses and netmask.
999  */
1000 int
1001 sifaddr(u, o, h, m)
1002     int u;
1003     u_int32_t o, h, m;
1004 {
1005     struct ifreq ifr;
1006
1007     memset(&ifr, 0, sizeof(ifr));
1008     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1009     ifr.ifr_addr.sa_family = AF_INET;
1010     INET_ADDR(ifr.ifr_addr) = m;
1011     if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
1012         error("Couldn't set IP netmask: %m");
1013     }
1014     ifr.ifr_addr.sa_family = AF_INET;
1015     INET_ADDR(ifr.ifr_addr) = o;
1016     if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
1017         error("Couldn't set local IP address: %m");
1018     }
1019     ifr.ifr_dstaddr.sa_family = AF_INET;
1020     INET_ADDR(ifr.ifr_dstaddr) = h;
1021     if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
1022         error("Couldn't set remote IP address: %m");
1023     }
1024 #if 0   /* now done in ppp_send_config */
1025     ifr.ifr_metric = link_mtu;
1026     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
1027         error("Couldn't set IP MTU: %m");
1028     }
1029 #endif
1030     ifaddrs[0] = o;
1031     ifaddrs[1] = h;
1032
1033     return 1;
1034 }
1035
1036 /*
1037  * cifaddr - Clear the interface IP addresses, and delete routes
1038  * through the interface if possible.
1039  */
1040 int
1041 cifaddr(u, o, h)
1042     int u;
1043     u_int32_t o, h;
1044 {
1045     struct rtentry rt;
1046
1047     bzero(&rt, sizeof(rt));
1048     rt.rt_dst.sa_family = AF_INET;
1049     INET_ADDR(rt.rt_dst) = h;
1050     rt.rt_gateway.sa_family = AF_INET;
1051     INET_ADDR(rt.rt_gateway) = o;
1052     rt.rt_flags = RTF_HOST;
1053     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1054         error("Couldn't delete route through interface: %m");
1055     ifaddrs[0] = 0;
1056     return 1;
1057 }
1058
1059 /*
1060  * sifdefaultroute - assign a default route through the address given.
1061  */
1062 int
1063 sifdefaultroute(u, l, g)
1064     int u;
1065     u_int32_t l, g;
1066 {
1067     struct rtentry rt;
1068
1069     bzero(&rt, sizeof(rt));
1070     rt.rt_dst.sa_family = AF_INET;
1071     INET_ADDR(rt.rt_dst) = 0;
1072     rt.rt_gateway.sa_family = AF_INET;
1073     INET_ADDR(rt.rt_gateway) = g;
1074     rt.rt_flags = RTF_GATEWAY;
1075
1076     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1077         error("Can't add default route: %m");
1078         return 0;
1079     }
1080
1081     default_route_gateway = g;
1082     return 1;
1083 }
1084
1085 /*
1086  * cifdefaultroute - delete a default route through the address given.
1087  */
1088 int
1089 cifdefaultroute(u, l, g)
1090     int u;
1091     u_int32_t l, g;
1092 {
1093     struct rtentry rt;
1094
1095     bzero(&rt, sizeof(rt));
1096     rt.rt_dst.sa_family = AF_INET;
1097     INET_ADDR(rt.rt_dst) = 0;
1098     rt.rt_gateway.sa_family = AF_INET;
1099     INET_ADDR(rt.rt_gateway) = g;
1100     rt.rt_flags = RTF_GATEWAY;
1101
1102     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1103         error("Can't delete default route: %m");
1104         return 0;
1105     }
1106
1107     default_route_gateway = 0;
1108     return 1;
1109 }
1110
1111 /*
1112  * sifproxyarp - Make a proxy ARP entry for the peer.
1113  */
1114 int
1115 sifproxyarp(unit, hisaddr)
1116     int unit;
1117     u_int32_t hisaddr;
1118 {
1119     struct arpreq arpreq;
1120
1121     bzero(&arpreq, sizeof(arpreq));
1122     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1123         return 0;
1124
1125     arpreq.arp_pa.sa_family = AF_INET;
1126     INET_ADDR(arpreq.arp_pa) = hisaddr;
1127     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1128     if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1129         error("Couldn't set proxy ARP entry: %m");
1130         return 0;
1131     }
1132
1133     proxy_arp_addr = hisaddr;
1134     return 1;
1135 }
1136
1137 /*
1138  * cifproxyarp - Delete the proxy ARP entry for the peer.
1139  */
1140 int
1141 cifproxyarp(unit, hisaddr)
1142     int unit;
1143     u_int32_t hisaddr;
1144 {
1145     struct arpreq arpreq;
1146
1147     bzero(&arpreq, sizeof(arpreq));
1148     arpreq.arp_pa.sa_family = AF_INET;
1149     INET_ADDR(arpreq.arp_pa) = hisaddr;
1150     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1151         error("Couldn't delete proxy ARP entry: %m");
1152         return 0;
1153     }
1154
1155     proxy_arp_addr = 0;
1156     return 1;
1157 }
1158
1159 /*
1160  * get_ether_addr - get the hardware address of an interface on the
1161  * the same subnet as ipaddr.
1162  */
1163 #define MAX_IFS         32
1164
1165 static int
1166 get_ether_addr(ipaddr, hwaddr)
1167     u_int32_t ipaddr;
1168     struct sockaddr *hwaddr;
1169 {
1170     struct ifreq *ifr, *ifend;
1171     u_int32_t ina, mask;
1172     struct ifreq ifreq;
1173     struct ifconf ifc;
1174     struct ifreq ifs[MAX_IFS];
1175     int nit_fd;
1176
1177     ifc.ifc_len = sizeof(ifs);
1178     ifc.ifc_req = ifs;
1179     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1180         error("ioctl(SIOCGIFCONF): %m");
1181         return 0;
1182     }
1183
1184     /*
1185      * Scan through looking for an interface with an Internet
1186      * address on the same subnet as `ipaddr'.
1187      */
1188     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1189     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1190             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1191         if (ifr->ifr_addr.sa_family == AF_INET) {
1192
1193             /*
1194              * Check that the interface is up, and not point-to-point
1195              * or loopback.
1196              */
1197             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1198             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1199                 continue;
1200             if ((ifreq.ifr_flags &
1201                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1202                  != (IFF_UP|IFF_BROADCAST))
1203                 continue;
1204
1205             /*
1206              * Get its netmask and check that it's on the right subnet.
1207              */
1208             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1209                 continue;
1210             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1211             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1212             if ((ipaddr & mask) != (ina & mask))
1213                 continue;
1214
1215             break;
1216         }
1217     }
1218
1219     if (ifr >= ifend)
1220         return 0;
1221     info("found interface %s for proxy arp", ifr->ifr_name);
1222
1223     /*
1224      * Grab the physical address for this interface.
1225      */
1226     if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1227         error("Couldn't open /dev/nit: %m");
1228         return 0;
1229     }
1230     strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1231     if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1232         || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1233         error("Couldn't get hardware address for %s: %m",
1234                ifreq.ifr_name);
1235         close(nit_fd);
1236         return 0;
1237     }
1238
1239     hwaddr->sa_family = AF_UNSPEC;
1240     memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1241     close(nit_fd);
1242     return 1;
1243 }
1244
1245 /*
1246  * have_route_to - determine if the system has any route to
1247  * a given IP address.
1248  * For demand mode to work properly, we have to ignore routes
1249  * through our own interface.
1250  */
1251 int have_route_to(addr)
1252     u_int32_t addr;
1253 {
1254     return -1;
1255 }
1256
1257 #define WTMPFILE        "/usr/adm/wtmp"
1258
1259 void
1260 logwtmp(line, name, host)
1261     const char *line, *name, *host;
1262 {
1263     int fd;
1264     struct stat buf;
1265     struct utmp ut;
1266
1267     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1268         return;
1269     if (!fstat(fd, &buf)) {
1270         strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1271         strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
1272         strlcpy(ut.ut_host, host, sizeof(ut.ut_host));
1273         (void)time(&ut.ut_time);
1274         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1275             (void)ftruncate(fd, buf.st_size);
1276     }
1277     close(fd);
1278 }
1279
1280 /*
1281  * Return user specified netmask, modified by any mask we might determine
1282  * for address `addr' (in network byte order).
1283  * Here we scan through the system's list of interfaces, looking for
1284  * any non-point-to-point interfaces which might appear to be on the same
1285  * network as `addr'.  If we find any, we OR in their netmask to the
1286  * user-specified netmask.
1287  */
1288 u_int32_t
1289 GetMask(addr)
1290     u_int32_t addr;
1291 {
1292     u_int32_t mask, nmask, ina;
1293     struct ifreq *ifr, *ifend, ifreq;
1294     struct ifconf ifc;
1295
1296     addr = ntohl(addr);
1297     if (IN_CLASSA(addr))        /* determine network mask for address class */
1298         nmask = IN_CLASSA_NET;
1299     else if (IN_CLASSB(addr))
1300         nmask = IN_CLASSB_NET;
1301     else
1302         nmask = IN_CLASSC_NET;
1303     /* class D nets are disallowed by bad_ip_adrs */
1304     mask = netmask | htonl(nmask);
1305
1306     /*
1307      * Scan through the system's network interfaces.
1308      */
1309     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1310     ifc.ifc_req = alloca(ifc.ifc_len);
1311     if (ifc.ifc_req == 0)
1312         return mask;
1313     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1314         warn("Couldn't get system interface list: %m");
1315         return mask;
1316     }
1317     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1318     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1319         /*
1320          * Check the interface's internet address.
1321          */
1322         if (ifr->ifr_addr.sa_family != AF_INET)
1323             continue;
1324         ina = INET_ADDR(ifr->ifr_addr);
1325         if ((ntohl(ina) & nmask) != (addr & nmask))
1326             continue;
1327         /*
1328          * Check that the interface is up, and not point-to-point or loopback.
1329          */
1330         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1331         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1332             continue;
1333         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1334             != IFF_UP)
1335             continue;
1336         /*
1337          * Get its netmask and OR it into our mask.
1338          */
1339         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1340             continue;
1341         mask |= INET_ADDR(ifreq.ifr_addr);
1342     }
1343
1344     return mask;
1345 }
1346
1347 static int
1348 strioctl(fd, cmd, ptr, ilen, olen)
1349     int fd, cmd, ilen, olen;
1350     void *ptr;
1351 {
1352     struct strioctl str;
1353
1354     str.ic_cmd = cmd;
1355     str.ic_timout = 0;
1356     str.ic_len = ilen;
1357     str.ic_dp = ptr;
1358     if (ioctl(fd, I_STR, &str) == -1)
1359         return -1;
1360     if (str.ic_len != olen)
1361         dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1362                olen, str.ic_len, cmd);
1363     return 0;
1364 }
1365
1366 /*
1367  * Use the hostid as part of the random number seed.
1368  */
1369 int
1370 get_host_seed()
1371 {
1372     return gethostid();
1373 }
1374
1375 /*
1376  * Code for locking/unlocking the serial device.
1377  * This code is derived from chat.c.
1378  */
1379
1380 #if !defined(HDB) && !defined(SUNOS3)
1381 #define HDB     1               /* ascii lock files are the default */
1382 #endif
1383
1384 #ifndef LOCK_DIR
1385 # if HDB
1386 #  define       PIDSTRING
1387 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1388 # else /* HDB */
1389 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1390 # endif /* HDB */
1391 #endif /* LOCK_DIR */
1392
1393 static char *lock_file;         /* name of lock file created */
1394
1395 /*
1396  * lock - create a lock file for the named device.
1397  */
1398 int
1399 lock(dev)
1400     char *dev;
1401 {
1402     char hdb_lock_buffer[12];
1403     int fd, pid, n;
1404     char *p;
1405     size_t l;
1406
1407     if ((p = strrchr(dev, '/')) != NULL)
1408         dev = p + 1;
1409     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1410     lock_file = malloc(l);
1411     if (lock_file == NULL)
1412         novm("lock file name");
1413     slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1414
1415     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1416         if (errno == EEXIST
1417             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1418             /* Read the lock file to find out who has the device locked */
1419 #ifdef PIDSTRING
1420             n = read(fd, hdb_lock_buffer, 11);
1421             if (n > 0) {
1422                 hdb_lock_buffer[n] = 0;
1423                 pid = atoi(hdb_lock_buffer);
1424             }
1425 #else
1426             n = read(fd, &pid, sizeof(pid));
1427 #endif
1428             if (n <= 0) {
1429                 error("Can't read pid from lock file %s", lock_file);
1430                 close(fd);
1431             } else {
1432                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1433                     /* pid no longer exists - remove the lock file */
1434                     if (unlink(lock_file) == 0) {
1435                         close(fd);
1436                         notice("Removed stale lock on %s (pid %d)",
1437                                dev, pid);
1438                         continue;
1439                     } else
1440                         warn("Couldn't remove stale lock on %s",
1441                                dev);
1442                 } else
1443                     notice("Device %s is locked by pid %d",
1444                            dev, pid);
1445             }
1446             close(fd);
1447         } else
1448             error("Can't create lock file %s: %m", lock_file);
1449         free(lock_file);
1450         lock_file = NULL;
1451         return -1;
1452     }
1453
1454 #ifdef PIDSTRING
1455     slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid());
1456     write(fd, hdb_lock_buffer, 11);
1457 #else
1458     pid = getpid();
1459     write(fd, &pid, sizeof pid);
1460 #endif
1461
1462     close(fd);
1463     return 0;
1464 }
1465
1466 /*
1467  * unlock - remove our lockfile
1468  */
1469 void
1470 unlock()
1471 {
1472     if (lock_file) {
1473         unlink(lock_file);
1474         free(lock_file);
1475         lock_file = NULL;
1476     }
1477 }
1478
1479 /*
1480  * SunOS doesn't have strtoul :-(
1481  */
1482 unsigned long
1483 strtoul(str, ptr, base)
1484     char *str, **ptr;
1485     int base;
1486 {
1487     return (unsigned long) strtol(str, ptr, base);
1488 }
1489
1490 /*
1491  * Or strerror :-(
1492  */
1493 extern char *sys_errlist[];
1494 extern int sys_nerr;
1495
1496 char *
1497 strerror(n)
1498     int n;
1499 {
1500     static char unknown[32];
1501
1502     if (n > 0 && n < sys_nerr)
1503         return sys_errlist[n];
1504     slprintf(unknown, sizeof(unknown), "Error %d", n);
1505     return unknown;
1506 }