2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * Copyright (c) 1995 The Australian National University.
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 and The Australian National University.
15 * The names of the Universities may not be used to endorse or promote
16 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 static char rcsid[] = "$Id: sys-ultrix.c,v 1.30 1999/03/22 05:55:40 paulus Exp $";
39 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
44 #include <sys/errno.h>
48 #include <net/ppp_defs.h>
49 #include <net/if_ppp.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
55 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
56 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
57 static int ppp_fd = -1; /* fd which is set to PPP discipline */
59 static int restore_term; /* 1 => we've munged the terminal */
60 static struct termios inittermios; /* Initial TTY termios */
61 static struct winsize wsinfo; /* Initial window size info */
63 static char *lock_file; /* name of lock file created */
65 static int loop_slave = -1;
66 static int loop_master;
67 static char loop_name[20];
69 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
71 static int sockfd; /* socket for doing interface ioctls */
73 static fd_set in_fds; /* set of fds that wait_input waits for */
74 static int max_in_fd; /* highest fd set in in_fds */
76 static int if_is_up; /* the interface is currently up */
77 static u_int32_t ifaddrs[2]; /* local and remote addresses */
78 static u_int32_t default_route_gateway; /* gateway addr for default route */
79 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
81 /* Prototypes for procedures local to this file. */
82 static int translate_speed __P((int));
83 static int baud_rate_of __P((int));
84 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
88 * sys_init - System-dependent initialization.
93 /* Get an internet socket for doing socket ioctl's on. */
94 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
95 fatal("Couldn't create IP socket: %m");
102 * sys_cleanup - restore any system state we modified before exiting:
103 * mark the interface down, delete default route and/or proxy arp entry.
104 * This shouldn't call die() because it's called from die().
112 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
113 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
114 && ((ifr.ifr_flags & IFF_UP) != 0)) {
115 ifr.ifr_flags &= ~IFF_UP;
116 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
120 cifaddr(0, ifaddrs[0], ifaddrs[1]);
121 if (default_route_gateway)
122 cifdefaultroute(0, 0, default_route_gateway);
124 cifproxyarp(0, proxy_arp_addr);
128 * sys_close - Clean up in a child process before execing.
134 if (loop_slave >= 0) {
141 * sys_check_options - check the options that the user specified
147 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
155 * daemon - Detach us from the terminal session.
158 daemon(nochdir, noclose)
159 int nochdir, noclose;
163 if ((pid = fork()) < 0)
166 exit(0); /* parent dies */
171 fclose(stdin); /* don't need stdin, stdout, stderr */
179 * ppp_available - check whether the system has any ppp interfaces
180 * (in fact we check whether we can do an ioctl on ppp0).
187 extern char *no_ppp_msg;
189 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
190 return 1; /* can't tell */
192 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
193 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
197 This system lacks kernel support for PPP. To include PPP support\n\
198 in the kernel, please follow the steps detailed in the README.ultrix\n\
199 file in the ppp-2.2 distribution.\n";
204 * establish_ppp - Turn the serial port into a ppp interface.
210 int pppdisc = PPPDISC;
214 * Save the old line discipline of fd, and set it to PPP.
216 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
217 fatal("ioctl(TIOCGETD): %m");
218 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
219 fatal("ioctl(TIOCSETD): %m");
222 * Find out which interface we were given.
224 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
225 fatal("ioctl(PPPIOCGUNIT): %m");
230 * Enable debug in the driver if requested.
233 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
234 warn("ioctl (PPPIOCGFLAGS): %m");
236 x |= (kdebugflag & 0xFF) * SC_DEBUG;
237 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
238 warn("ioctl(PPPIOCSFLAGS): %m");
243 * Set device for non-blocking reads.
245 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
246 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
247 warn("Couldn't set device to non-blocking mode: %m");
254 * restore_loop - reattach the ppp unit to the loopback.
262 * disestablish_ppp - Restore the serial port to normal operation.
263 * This shouldn't call die() because it's called from die().
269 /* Reset non-blocking mode on fd. */
270 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
271 warn("Couldn't restore device fd flags: %m");
274 /* Restore old line discipline. */
275 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
276 error("ioctl(TIOCSETD): %m");
284 * Check whether the link seems not to be 8-bit clean.
292 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
294 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
296 s = "bit 7 set to 1";
299 s = "bit 7 set to 0";
309 warn("Serial link is not 8-bit clean:");
310 warn("All received characters had %s", s);
317 * List of valid speeds.
320 int speed_int, speed_val;
392 * Translate from bits/second to a speed_t.
398 struct speed *speedp;
402 for (speedp = speeds; speedp->speed_int; speedp++)
403 if (bps == speedp->speed_int)
404 return speedp->speed_val;
405 warn("speed %d not supported", bps);
410 * Translate from a speed_t to bits/second.
416 struct speed *speedp;
420 for (speedp = speeds; speedp->speed_int; speedp++)
421 if (speed == speedp->speed_val)
422 return speedp->speed_int;
427 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
428 * at the requested speed, etc. If `local' is true, set CLOCAL
429 * regardless of whether the modem option was specified.
432 set_up_tty(fd, local)
438 if (tcgetattr(fd, &tios) < 0)
439 fatal("tcgetattr: %m");
443 ioctl(fd, TIOCGWINSZ, &wsinfo);
446 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
448 if (crtscts > 0 && !local)
449 tios.c_cflag |= CRTSCTS;
450 else if (crtscts < 0)
451 tios.c_cflag &= ~CRTSCTS;
454 tios.c_cflag |= CS8 | CREAD | HUPCL;
456 tios.c_cflag |= CLOCAL;
457 tios.c_iflag = IGNBRK | IGNPAR;
461 tios.c_cc[VTIME] = 0;
464 tios.c_iflag |= IXON | IXOFF;
465 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
466 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
469 speed = translate_speed(inspeed);
471 cfsetospeed(&tios, speed);
472 cfsetispeed(&tios, speed);
474 speed = cfgetospeed(&tios);
476 * We can't proceed if the serial port speed is B0,
477 * since that implies that the serial port is disabled.
480 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
483 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
484 fatal("tcsetattr: %m");
487 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
488 warn("TIOC(N)MODEM: %m");
489 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
490 warn("TIOC(N)CAR: %m");
492 baud_rate = inspeed = baud_rate_of(speed);
497 * restore_tty - restore the terminal to the saved settings.
504 if (!default_device) {
506 * Turn off echoing, because otherwise we can get into
507 * a loop with the tty and the modem echoing to each other.
508 * We presume we are the sole user of this tty device, so
509 * when we close it, it will revert to its defaults anyway.
511 inittermios.c_lflag &= ~(ECHO | ECHONL);
513 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
515 warn("tcsetattr: %m");
516 ioctl(fd, TIOCSWINSZ, &wsinfo);
522 * setdtr - control the DTR line on the serial port.
523 * This is called from die(), so it shouldn't call die().
529 int modembits = TIOCM_DTR;
531 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
536 * open_ppp_loopback - open the device we use for getting
537 * packets in demand mode, and connect it to a ppp interface.
538 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
543 fatal("open_ppp_loopback called!");
544 /* return loop_master; */
549 * output - Output PPP packet.
558 dbglog("sent %P", p, len);
560 if (write(ttyfd, p, len) < 0) {
568 * wait_input - wait until there is data available,
569 * for the length of time specified by *timo (indefinite
574 struct timeval *timo;
580 n = select(ttyfd+1, &ready, NULL, &ready, timo);
581 if (n < 0 && errno != EINTR)
587 * add_fd - add an fd to the set that wait_input waits for.
598 * remove_fd - remove an fd from the set that wait_input waits for.
608 * wait_loop_output - wait until there is data available on the
609 * loopback, for the length of time specified by *timo (indefinite
613 wait_loop_output(timo)
614 struct timeval *timo;
621 * wait_time - wait for a given length of time or until a
622 * signal is received.
626 struct timeval *timo;
630 n = select(0, NULL, NULL, NULL, timo);
631 if (n < 0 && errno != EINTR)
637 * read_packet - get a PPP packet from the serial device.
645 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
646 if (errno == EWOULDBLOCK || errno == EINTR)
648 fatal("read(fd): %m");
655 * get_loop_output - read characters from the loopback, form them
656 * into frames, and detect when we want to bring the real link up.
657 * Return value is 1 if we need to bring up the link, 0 otherwise.
667 * ppp_send_config - configure the transmit characteristics of
671 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
678 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0)
679 fatal("ioctl(PPPIOCSMTU): %m");
681 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
682 fatal("ioctl(PPPIOCSASYNCMAP): %m");
684 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
685 fatal("ioctl (PPPIOCGFLAGS): %m");
686 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
687 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
688 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
689 fatal("ioctl(PPPIOCSFLAGS): %m");
694 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
697 ppp_set_xaccm(unit, accm)
701 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
702 warn("ioctl(set extended ACCM): %m");
707 * ppp_recv_config - configure the receive-side characteristics of
711 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
718 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
719 fatal("ioctl(PPPIOCSMRU): %m");
720 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
721 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
722 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
723 fatal("ioctl (PPPIOCGFLAGS): %m");
724 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
725 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
726 fatal("ioctl(PPPIOCSFLAGS): %m");
730 * ccp_test - ask kernel whether a given compression method
731 * is acceptable for use. Returns 1 if the method and parameters
732 * are OK, 0 if the method is known but the parameters are not OK
733 * (e.g. code size should be reduced), or -1 if the method is unknown.
736 ccp_test(unit, opt_ptr, opt_len, for_transmit)
737 int unit, opt_len, for_transmit;
740 struct ppp_option_data data;
743 data.length = opt_len;
744 data.transmit = for_transmit;
745 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
747 return (errno == ENOBUFS)? 0: -1;
751 * ccp_flags_set - inform kernel about the current state of CCP.
754 ccp_flags_set(unit, isopen, isup)
755 int unit, isopen, isup;
759 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
760 error("ioctl (PPPIOCGFLAGS): %m");
763 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
764 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
765 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
766 error("ioctl(PPPIOCSFLAGS): %m");
770 * ccp_fatal_error - returns 1 if decompression was disabled as a
771 * result of an error detected after decompression of a packet,
772 * 0 otherwise. This is necessary because of patent nonsense.
775 ccp_fatal_error(unit)
780 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
781 error("ioctl(PPPIOCGFLAGS): %m");
784 return x & SC_DC_FERROR;
788 * get_idle_time - return how long the link has been idle.
795 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
799 * get_ppp_stats - return statistics for the link.
802 get_ppp_stats(u, stats)
804 struct pppd_stats *stats;
806 struct ifpppstatsreq req;
808 memset (&req, 0, sizeof (req));
809 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
810 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
811 error("Couldn't get PPP statistics: %m");
814 stats->bytes_in = req.stats.p.ppp_ibytes;
815 stats->bytes_out = req.stats.p.ppp_obytes;
821 * sifvjcomp - config tcp header compression
824 sifvjcomp(u, vjcomp, cidcomp, maxcid)
825 int u, vjcomp, cidcomp, maxcid;
829 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
830 error("ioctl (PPPIOCGFLAGS): %m");
833 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
834 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
835 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
836 error("ioctl(PPPIOCSFLAGS): %m");
839 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
840 error("ioctl(PPPIOCSFLAGS): %m");
847 * sifup - Config the interface up and enable IP packets to pass.
855 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
856 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
857 error("ioctl (SIOCGIFFLAGS): %m");
860 ifr.ifr_flags |= IFF_UP;
861 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
862 error("ioctl(SIOCSIFFLAGS): %m");
870 * sifnpmode - Set the mode for handling packets for a given NP.
873 sifnpmode(u, proto, mode)
880 npi.protocol = proto;
882 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
883 error("ioctl(set NP %d mode to %d): %m", proto, mode);
890 * sifdown - Config the interface down and disable IP.
901 npi.protocol = PPP_IP;
902 npi.mode = NPMODE_ERROR;
903 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
904 /* ignore errors, because ppp_fd might have been closed by now. */
906 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
907 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
908 error("ioctl (SIOCGIFFLAGS): %m");
911 ifr.ifr_flags &= ~IFF_UP;
912 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
913 error("ioctl(SIOCSIFFLAGS): %m");
922 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
925 #define SET_SA_FAMILY(addr, family) \
926 BZERO((char *) &(addr), sizeof(addr)); \
927 addr.sa_family = (family);
930 * sifaddr - Config the interface IP addresses and netmask.
941 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
942 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
944 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
945 info("Setting interface mask to %s\n", ip_ntoa(m));
946 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
947 error("ioctl(SIOCSIFNETMASK): %m");
951 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
952 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
953 error("ioctl(SIOCSIFADDR): %m");
956 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
957 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
958 error("ioctl(SIOCSIFDSTADDR): %m");
967 * cifaddr - Clear the interface IP addresses, and delete routes
968 * through the interface if possible.
978 BZERO(&rt, sizeof(rt));
979 SET_SA_FAMILY(rt.rt_dst, AF_INET);
980 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
981 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
982 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
983 rt.rt_flags = RTF_HOST;
984 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
985 error("ioctl(SIOCDELRT): %m");
992 * sifdefaultroute - assign a default route through the address given.
995 sifdefaultroute(u, l, g)
1001 BZERO(&rt, sizeof(rt));
1002 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1003 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1004 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1005 rt.rt_flags = RTF_GATEWAY;
1006 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1007 error("default route ioctl(SIOCADDRT): %m");
1010 default_route_gateway = g;
1015 * cifdefaultroute - delete a default route through the address given.
1018 cifdefaultroute(u, l, g)
1024 BZERO(&rt, sizeof(rt));
1025 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1026 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1027 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1028 rt.rt_flags = RTF_GATEWAY;
1029 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1030 warn("default route ioctl(SIOCDELRT): %m");
1031 default_route_gateway = 0;
1036 * sifproxyarp - Make a proxy ARP entry for the peer.
1039 sifproxyarp(unit, hisaddr)
1043 struct arpreq arpreq;
1045 BZERO(&arpreq, sizeof(arpreq));
1048 * Get the hardware address of an interface on the same subnet
1049 * as our local address.
1051 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1052 error("Cannot determine ethernet address for proxy ARP");
1056 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1057 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1058 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1059 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1060 error("Couldn't add proxy arp entry: %m");
1064 proxy_arp_addr = hisaddr;
1069 * cifproxyarp - Delete the proxy ARP entry for the peer.
1072 cifproxyarp(unit, hisaddr)
1076 struct arpreq arpreq;
1078 BZERO(&arpreq, sizeof(arpreq));
1079 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1080 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1081 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1082 warn("Couldn't delete proxy arp entry: %m");
1090 * get_ether_addr - get the hardware address of an interface on the
1091 * the same subnet as ipaddr.
1096 get_ether_addr(ipaddr, hwaddr)
1098 struct sockaddr *hwaddr;
1100 struct ifreq *ifr, *ifend;
1101 u_int32_t ina, mask;
1104 struct ifreq ifs[MAX_IFS];
1105 struct ifdevea ifdevea;
1107 ifc.ifc_len = sizeof(ifs);
1109 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1110 error("ioctl(SIOCGIFCONF): %m");
1115 * Scan through looking for an interface with an Internet
1116 * address on the same subnet as `ipaddr'.
1118 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1119 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1120 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1121 if (ifr->ifr_addr.sa_family == AF_INET) {
1122 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1123 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1125 * Check that the interface is up, and not point-to-point
1128 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1130 if ((ifreq.ifr_flags &
1131 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1132 != (IFF_UP|IFF_BROADCAST))
1135 * Get its netmask and check that it's on the right subnet.
1137 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1139 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1140 if ((ipaddr & mask) != (ina & mask))
1149 info("found interface %s for proxy arp", ifr->ifr_name);
1152 * Grab the physical address for this interface.
1154 strlcpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1155 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1156 error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1160 hwaddr->sa_family = AF_UNSPEC;
1161 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1166 * Return user specified netmask, modified by any mask we might determine
1167 * for address `addr' (in network byte order).
1168 * Here we scan through the system's list of interfaces, looking for
1169 * any non-point-to-point interfaces which might appear to be on the same
1170 * network as `addr'. If we find any, we OR in their netmask to the
1171 * user-specified netmask.
1177 u_int32_t mask, nmask, ina;
1178 struct ifreq *ifr, *ifend, ifreq;
1180 struct ifreq ifs[MAX_IFS];
1183 if (IN_CLASSA(addr)) /* determine network mask for address class */
1184 nmask = IN_CLASSA_NET;
1185 else if (IN_CLASSB(addr))
1186 nmask = IN_CLASSB_NET;
1188 nmask = IN_CLASSC_NET;
1189 /* class D nets are disallowed by bad_ip_adrs */
1190 mask = netmask | htonl(nmask);
1193 * Scan through the system's network interfaces.
1195 ifc.ifc_len = sizeof(ifs);
1197 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1198 warn("ioctl(SIOCGIFCONF): %m");
1201 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1202 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1203 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1205 * Check the interface's internet address.
1207 if (ifr->ifr_addr.sa_family != AF_INET)
1209 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1210 if ((ntohl(ina) & nmask) != (addr & nmask))
1213 * Check that the interface is up, and not point-to-point or loopback.
1215 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1216 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1218 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1222 * Get its netmask and OR it into our mask.
1224 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1226 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1233 * have_route_to - determine if the system has any route to
1234 * a given IP address.
1235 * For demand mode to work properly, we have to ignore routes
1236 * through our own interface.
1238 int have_route_to(u_int32_t addr)
1245 * Use the hostid as part of the random number seed.
1255 Seems like strdup() is not part of string package in Ultrix.
1256 If I understood the man-page on the sun this should work.
1261 char *strdup( in ) char *in;
1264 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1265 (void) strcpy( dup, in );
1270 * This logwtmp() implementation is subject to the following copyright:
1272 * Copyright (c) 1988 The Regents of the University of California.
1273 * All rights reserved.
1275 * Redistribution and use in source and binary forms are permitted
1276 * provided that the above copyright notice and this paragraph are
1277 * duplicated in all such forms and that any documentation,
1278 * advertising materials, and other materials related to such
1279 * distribution and use acknowledge that the software was developed
1280 * by the University of California, Berkeley. The name of the
1281 * University may not be used to endorse or promote products derived
1282 * from this software without specific prior written permission.
1283 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1284 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1285 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1288 #define WTMPFILE "/usr/adm/wtmp"
1291 logwtmp(line, name, host)
1292 const char *line, *name, *host;
1298 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1300 if (!fstat(fd, &buf)) {
1301 strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1302 strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
1303 strlcpy(ut.ut_host, host, sizeof(ut.ut_host));
1304 (void)time(&ut.ut_time);
1305 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1306 (void)ftruncate(fd, buf.st_size);
1312 * Routines for locking and unlocking the serial device, moved here
1316 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1319 * lock - create a lock file for the named device
1329 if ((p = strrchr(dev, '/')) != NULL)
1331 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1332 lock_file = malloc(l);
1333 if (lock_file == NULL)
1334 novm("lock file name");
1335 slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1337 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1339 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1340 /* Read the lock file to find out who has the device locked */
1341 n = read(fd, &pid, sizeof(pid));
1343 error("Can't read pid from lock file %s", lock_file);
1346 if (kill(pid, 0) == -1 && errno == ESRCH) {
1347 /* pid no longer exists - remove the lock file */
1348 if (unlink(lock_file) == 0) {
1350 notice("Removed stale lock on %s (pid %d)",
1354 warn("Couldn't remove stale lock on %s",
1357 notice("Device %s is locked by pid %d",
1362 error("Can't create lock file %s: %m", lock_file);
1369 write(fd, &pid, sizeof pid);
1376 * unlock - remove our lockfile