2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: sys-ultrix.c,v 1.12 1995/06/30 00:40:06 paulus Exp $";
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
40 #include <sys/errno.h>
44 #include <net/ppp_defs.h>
45 #include <net/if_ppp.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
51 static int initdisc = -1; /* Initial TTY discipline */
53 static int restore_term; /* 1 => we've munged the terminal */
54 static struct termios inittermios; /* Initial TTY termios */
56 static char *lock_file;
58 int sockfd; /* socket for doing interface ioctls */
61 * sys_init - System-dependent initialization.
66 openlog("pppd", LOG_PID);
68 /* Get an internet socket for doing socket ioctl's on. */
69 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
70 syslog(LOG_ERR, "Couldn't create IP socket: %m");
76 * note_debug_level - note a change in the debug level.
85 * daemon - Detach us from the terminal session.
88 daemon(nochdir, noclose)
93 if ((pid = fork()) < 0)
96 exit(0); /* parent dies */
101 fclose(stdin); /* don't need stdin, stdout, stderr */
110 * ppp_available - check whether the system has any ppp interfaces
111 * (in fact we check whether we can do an ioctl on ppp0).
120 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
121 return 1; /* can't tell - maybe we're not root */
123 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
124 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
132 * establish_ppp - Turn the serial port into a ppp interface.
137 int pppdisc = PPPDISC;
140 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
141 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
144 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
145 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
150 * Find out which interface we were given.
152 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
153 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
158 * Enable debug in the driver if requested.
161 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
162 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
164 x |= (kdebugflag & 0xFF) * SC_DEBUG;
165 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
166 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
173 * disestablish_ppp - Restore the serial port to normal operation.
174 * This shouldn't call die() because it's called from die().
184 * Check whether the link seems not to be 8-bit clean.
186 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
188 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
190 s = "bit 7 set to 1";
193 s = "bit 7 set to 0";
203 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
204 syslog(LOG_WARNING, "All received characters had %s", s);
207 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
208 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
214 * List of valid speeds.
217 int speed_int, speed_val;
289 * Translate from bits/second to a speed_t.
295 struct speed *speedp;
299 for (speedp = speeds; speedp->speed_int; speedp++)
300 if (bps == speedp->speed_int)
301 return speedp->speed_val;
302 syslog(LOG_WARNING, "speed %d not supported", bps);
307 * Translate from a speed_t to bits/second.
313 struct speed *speedp;
317 for (speedp = speeds; speedp->speed_int; speedp++)
318 if (speed == speedp->speed_val)
319 return speedp->speed_int;
324 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
325 * at the requested speed, etc. If `local' is true, set CLOCAL
326 * regardless of whether the modem option was specified.
328 set_up_tty(fd, local)
334 if (tcgetattr(fd, &tios) < 0) {
335 syslog(LOG_ERR, "tcgetattr: %m");
342 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
345 tios.c_cflag |= CRTSCTS;
346 else if (crtscts < 0)
347 tios.c_cflag &= ~CRTSCTS;
350 tios.c_cflag |= CS8 | CREAD | HUPCL;
352 tios.c_cflag |= CLOCAL;
353 tios.c_iflag = IGNBRK | IGNPAR;
357 tios.c_cc[VTIME] = 0;
360 tios.c_iflag |= IXOFF;
361 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
362 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
365 speed = translate_speed(inspeed);
367 cfsetospeed(&tios, speed);
368 cfsetispeed(&tios, speed);
370 speed = cfgetospeed(&tios);
372 * We can't proceed if the serial port speed is B0,
373 * since that implies that the serial port is disabled.
376 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
382 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
383 syslog(LOG_ERR, "tcsetattr: %m");
388 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
389 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
390 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
391 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
393 baud_rate = inspeed = baud_rate_of(speed);
398 * restore_tty - restore the terminal to the saved settings.
404 if (!default_device) {
406 * Turn off echoing, because otherwise we can get into
407 * a loop with the tty and the modem echoing to each other.
408 * We presume we are the sole user of this tty device, so
409 * when we close it, it will revert to its defaults anyway.
411 inittermios.c_lflag &= ~(ECHO | ECHONL);
413 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
415 syslog(LOG_WARNING, "tcsetattr: %m");
416 restore_term = FALSE;
421 * setdtr - control the DTR line on the serial port.
422 * This is called from die(), so it shouldn't call die().
427 int modembits = TIOCM_DTR;
429 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
434 * output - Output PPP packet.
443 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
445 log_packet(p, len, "sent ");
447 if (write(fd, p, len) < 0) {
448 syslog(LOG_ERR, "write: %m");
455 * wait_input - wait until there is data available on fd,
456 * for the length of time specified by *timo (indefinite
460 struct timeval *timo;
467 n = select(fd+1, &ready, NULL, &ready, timo);
468 if (n < 0 && errno != EINTR) {
469 syslog(LOG_ERR, "select: %m");
476 * read_packet - get a PPP packet from the serial device.
484 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
485 if (errno == EWOULDBLOCK || errno == EINTR) {
486 MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
489 syslog(LOG_ERR, "read(fd): %m");
497 * ppp_send_config - configure the transmit characteristics of
501 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
508 if (ioctl(fd, PPPIOCSMTU, &mtu) < 0) {
509 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
513 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
514 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
518 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
519 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
522 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
523 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
524 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
525 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
532 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
535 ppp_set_xaccm(unit, accm)
539 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
540 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
545 * ppp_recv_config - configure the receive-side characteristics of
549 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
556 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
557 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
560 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
561 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
564 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
565 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
568 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
569 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
570 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
576 * ccp_test - ask kernel whether a given compression method
577 * is acceptable for use.
579 ccp_test(unit, opt_ptr, opt_len, for_transmit)
580 int unit, opt_len, for_transmit;
583 struct ppp_option_data data;
586 data.length = opt_len;
587 data.transmit = for_transmit;
588 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
592 * ccp_flags_set - inform kernel about the current state of CCP.
595 ccp_flags_set(unit, isopen, isup)
596 int unit, isopen, isup;
600 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
601 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
604 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
605 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
606 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
607 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
611 * ccp_fatal_error - returns 1 if decompression was disabled as a
612 * result of an error detected after decompression of a packet,
613 * 0 otherwise. This is necessary because of patent nonsense.
616 ccp_fatal_error(unit)
621 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
622 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
625 return x & SC_DC_FERROR;
629 * sifvjcomp - config tcp header compression
632 sifvjcomp(u, vjcomp, cidcomp, maxcid)
633 int u, vjcomp, cidcomp, maxcid;
637 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
638 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
641 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
642 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
643 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
644 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
647 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
648 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
655 * sifup - Config the interface up and enable IP packets to pass.
658 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
669 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
670 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
671 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
674 ifr.ifr_flags |= IFF_UP;
675 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
676 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
679 npi.protocol = PPP_IP;
680 npi.mode = NPMODE_PASS;
681 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
682 if (errno != ENOTTY) {
683 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
686 /* for backwards compatibility */
687 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
688 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
692 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
693 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
701 * sifdown - Config the interface down and disable IP.
713 npi.protocol = PPP_IP;
714 npi.mode = NPMODE_ERROR;
715 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
716 if (errno != ENOTTY) {
717 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
720 /* backwards compatibility */
721 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
722 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
726 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
727 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
734 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
735 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
736 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
739 ifr.ifr_flags &= ~IFF_UP;
740 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
741 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
749 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
752 #define SET_SA_FAMILY(addr, family) \
753 BZERO((char *) &(addr), sizeof(addr)); \
754 addr.sa_family = (family);
757 * sifaddr - Config the interface IP addresses and netmask.
768 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
769 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
770 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
771 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
772 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
775 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
776 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
777 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
781 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
782 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
783 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
784 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
792 * cifaddr - Clear the interface IP addresses, and delete routes
793 * through the interface if possible.
802 SET_SA_FAMILY(rt.rt_dst, AF_INET);
803 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
804 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
805 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
806 rt.rt_flags = RTF_HOST;
807 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
808 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
815 * sifdefaultroute - assign a default route through the address given.
818 sifdefaultroute(u, g)
822 SET_SA_FAMILY(rt.rt_dst, AF_INET);
823 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
824 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
825 rt.rt_flags = RTF_GATEWAY;
826 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
827 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
834 * cifdefaultroute - delete a default route through the address given.
837 cifdefaultroute(u, g)
841 SET_SA_FAMILY(rt.rt_dst, AF_INET);
842 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
843 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
844 rt.rt_flags = RTF_GATEWAY;
845 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
846 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
850 * sifproxyarp - Make a proxy ARP entry for the peer.
853 sifproxyarp(unit, hisaddr)
857 struct arpreq arpreq;
859 BZERO(&arpreq, sizeof(arpreq));
862 * Get the hardware address of an interface on the same subnet
863 * as our local address.
865 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
866 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
870 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
871 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
872 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
873 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
874 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
882 * cifproxyarp - Delete the proxy ARP entry for the peer.
885 cifproxyarp(unit, hisaddr)
889 struct arpreq arpreq;
891 BZERO(&arpreq, sizeof(arpreq));
892 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
893 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
894 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
895 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
902 * get_ether_addr - get the hardware address of an interface on the
903 * the same subnet as ipaddr.
908 get_ether_addr(ipaddr, hwaddr)
910 struct sockaddr *hwaddr;
912 struct ifreq *ifr, *ifend, *ifp;
914 struct sockaddr_dl *dla;
917 struct ifreq ifs[MAX_IFS];
918 struct ifdevea ifdevea;
920 ifc.ifc_len = sizeof(ifs);
922 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
923 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
928 * Scan through looking for an interface with an Internet
929 * address on the same subnet as `ipaddr'.
931 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
932 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
933 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
934 if (ifr->ifr_addr.sa_family == AF_INET) {
935 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
936 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
938 * Check that the interface is up, and not point-to-point
941 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
943 if ((ifreq.ifr_flags &
944 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
945 != (IFF_UP|IFF_BROADCAST))
948 * Get its netmask and check that it's on the right subnet.
950 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
952 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
953 if ((ipaddr & mask) != (ina & mask))
962 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
965 * Grab the physical address for this interface.
967 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
968 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
969 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
973 hwaddr->sa_family = AF_UNSPEC;
974 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
979 * Return user specified netmask, modified by any mask we might determine
980 * for address `addr' (in network byte order).
981 * Here we scan through the system's list of interfaces, looking for
982 * any non-point-to-point interfaces which might appear to be on the same
983 * network as `addr'. If we find any, we OR in their netmask to the
984 * user-specified netmask.
990 u_int32_t mask, nmask, ina;
991 struct ifreq *ifr, *ifend, ifreq;
993 struct ifreq ifs[MAX_IFS];
996 if (IN_CLASSA(addr)) /* determine network mask for address class */
997 nmask = IN_CLASSA_NET;
998 else if (IN_CLASSB(addr))
999 nmask = IN_CLASSB_NET;
1001 nmask = IN_CLASSC_NET;
1002 /* class D nets are disallowed by bad_ip_adrs */
1003 mask = netmask | htonl(nmask);
1006 * Scan through the system's network interfaces.
1008 ifc.ifc_len = sizeof(ifs);
1010 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1011 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1014 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1015 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1016 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1018 * Check the interface's internet address.
1020 if (ifr->ifr_addr.sa_family != AF_INET)
1022 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1023 if ((ntohl(ina) & nmask) != (addr & nmask))
1026 * Check that the interface is up, and not point-to-point or loopback.
1028 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1029 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1031 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1035 * Get its netmask and OR it into our mask.
1037 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1039 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1047 Seems like strdup() is not part of string package in Ultrix.
1048 If I understood the man-page on the sun this should work.
1053 char *strdup( in ) char *in;
1056 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1057 (void) strcpy( dup, in );
1062 * This logwtmp() implementation is subject to the following copyright:
1064 * Copyright (c) 1988 The Regents of the University of California.
1065 * All rights reserved.
1067 * Redistribution and use in source and binary forms are permitted
1068 * provided that the above copyright notice and this paragraph are
1069 * duplicated in all such forms and that any documentation,
1070 * advertising materials, and other materials related to such
1071 * distribution and use acknowledge that the software was developed
1072 * by the University of California, Berkeley. The name of the
1073 * University may not be used to endorse or promote products derived
1074 * from this software without specific prior written permission.
1075 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1076 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1077 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1080 #define WTMPFILE "/usr/adm/wtmp"
1083 logwtmp(line, name, host)
1084 char *line, *name, *host;
1090 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1092 if (!fstat(fd, &buf)) {
1093 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1094 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1095 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1096 (void)time(&ut.ut_time);
1097 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1098 (void)ftruncate(fd, buf.st_size);
1104 * Routines for locking and unlocking the serial device, moved here
1108 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1111 * lock - create a lock file for the named device
1120 if ((p = strrchr(dev, '/')) != NULL)
1122 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1123 if (lock_file == NULL)
1124 novm("lock file name");
1125 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1127 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1129 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1130 /* Read the lock file to find out who has the device locked */
1131 n = read(fd, &pid, sizeof(pid));
1133 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1136 if (kill(pid, 0) == -1 && errno == ESRCH) {
1137 /* pid no longer exists - remove the lock file */
1138 if (unlink(lock_file) == 0) {
1140 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1144 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1147 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1152 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1159 write(fd, &pid, sizeof pid);
1166 * unlock - remove our lockfile