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