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.14 1995/08/11 02:36:26 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 */
55 static struct winsize wsinfo; /* Initial window size info */
57 static char *lock_file;
59 int sockfd; /* socket for doing interface ioctls */
62 * sys_init - System-dependent initialization.
67 openlog("pppd", LOG_PID);
69 /* Get an internet socket for doing socket ioctl's on. */
70 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
71 syslog(LOG_ERR, "Couldn't create IP socket: %m");
77 * note_debug_level - note a change in the debug level.
86 * daemon - Detach us from the terminal session.
89 daemon(nochdir, noclose)
94 if ((pid = fork()) < 0)
97 exit(0); /* parent dies */
102 fclose(stdin); /* don't need stdin, stdout, stderr */
111 * ppp_available - check whether the system has any ppp interfaces
112 * (in fact we check whether we can do an ioctl on ppp0).
121 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
122 return 1; /* can't tell - maybe we're not root */
124 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
125 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
133 * establish_ppp - Turn the serial port into a ppp interface.
138 int pppdisc = PPPDISC;
141 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
142 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
145 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
146 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
151 * Find out which interface we were given.
153 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
154 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
159 * Enable debug in the driver if requested.
162 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
163 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
165 x |= (kdebugflag & 0xFF) * SC_DEBUG;
166 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
167 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
174 * disestablish_ppp - Restore the serial port to normal operation.
175 * This shouldn't call die() because it's called from die().
185 * Check whether the link seems not to be 8-bit clean.
187 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
189 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
191 s = "bit 7 set to 1";
194 s = "bit 7 set to 0";
204 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
205 syslog(LOG_WARNING, "All received characters had %s", s);
208 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
209 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
215 * List of valid speeds.
218 int speed_int, speed_val;
290 * Translate from bits/second to a speed_t.
296 struct speed *speedp;
300 for (speedp = speeds; speedp->speed_int; speedp++)
301 if (bps == speedp->speed_int)
302 return speedp->speed_val;
303 syslog(LOG_WARNING, "speed %d not supported", bps);
308 * Translate from a speed_t to bits/second.
314 struct speed *speedp;
318 for (speedp = speeds; speedp->speed_int; speedp++)
319 if (speed == speedp->speed_val)
320 return speedp->speed_int;
325 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
326 * at the requested speed, etc. If `local' is true, set CLOCAL
327 * regardless of whether the modem option was specified.
329 set_up_tty(fd, local)
335 if (tcgetattr(fd, &tios) < 0) {
336 syslog(LOG_ERR, "tcgetattr: %m");
342 ioctl(fd, TIOCGWINSZ, &wsinfo);
345 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
348 tios.c_cflag |= CRTSCTS;
349 else if (crtscts < 0)
350 tios.c_cflag &= ~CRTSCTS;
353 tios.c_cflag |= CS8 | CREAD | HUPCL;
355 tios.c_cflag |= CLOCAL;
356 tios.c_iflag = IGNBRK | IGNPAR;
360 tios.c_cc[VTIME] = 0;
363 tios.c_iflag |= IXOFF;
364 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
365 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
368 speed = translate_speed(inspeed);
370 cfsetospeed(&tios, speed);
371 cfsetispeed(&tios, speed);
373 speed = cfgetospeed(&tios);
375 * We can't proceed if the serial port speed is B0,
376 * since that implies that the serial port is disabled.
379 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
385 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
386 syslog(LOG_ERR, "tcsetattr: %m");
391 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
392 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
393 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
394 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
396 baud_rate = inspeed = baud_rate_of(speed);
401 * restore_tty - restore the terminal to the saved settings.
407 if (!default_device) {
409 * Turn off echoing, because otherwise we can get into
410 * a loop with the tty and the modem echoing to each other.
411 * We presume we are the sole user of this tty device, so
412 * when we close it, it will revert to its defaults anyway.
414 inittermios.c_lflag &= ~(ECHO | ECHONL);
416 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
418 syslog(LOG_WARNING, "tcsetattr: %m");
419 ioctl(fd, TIOCSWINSZ, &wsinfo);
420 restore_term = FALSE;
425 * setdtr - control the DTR line on the serial port.
426 * This is called from die(), so it shouldn't call die().
431 int modembits = TIOCM_DTR;
433 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
438 * output - Output PPP packet.
447 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
449 log_packet(p, len, "sent ");
451 if (write(fd, p, len) < 0) {
452 syslog(LOG_ERR, "write: %m");
459 * wait_input - wait until there is data available on fd,
460 * for the length of time specified by *timo (indefinite
464 struct timeval *timo;
471 n = select(fd+1, &ready, NULL, &ready, timo);
472 if (n < 0 && errno != EINTR) {
473 syslog(LOG_ERR, "select: %m");
480 * read_packet - get a PPP packet from the serial device.
488 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
489 if (errno == EWOULDBLOCK || errno == EINTR) {
490 MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
493 syslog(LOG_ERR, "read(fd): %m");
501 * ppp_send_config - configure the transmit characteristics of
505 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
512 if (ioctl(fd, PPPIOCSMTU, &mtu) < 0) {
513 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
517 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
518 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
522 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
523 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
526 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
527 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
528 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
529 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
536 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
539 ppp_set_xaccm(unit, accm)
543 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
544 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
549 * ppp_recv_config - configure the receive-side characteristics of
553 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
560 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
561 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
564 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
565 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
568 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
569 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
572 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
573 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
574 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
580 * ccp_test - ask kernel whether a given compression method
581 * is acceptable for use.
583 ccp_test(unit, opt_ptr, opt_len, for_transmit)
584 int unit, opt_len, for_transmit;
587 struct ppp_option_data data;
590 data.length = opt_len;
591 data.transmit = for_transmit;
592 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
596 * ccp_flags_set - inform kernel about the current state of CCP.
599 ccp_flags_set(unit, isopen, isup)
600 int unit, isopen, isup;
604 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
605 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
608 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
609 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
610 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
611 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
615 * ccp_fatal_error - returns 1 if decompression was disabled as a
616 * result of an error detected after decompression of a packet,
617 * 0 otherwise. This is necessary because of patent nonsense.
620 ccp_fatal_error(unit)
625 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
626 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
629 return x & SC_DC_FERROR;
633 * sifvjcomp - config tcp header compression
636 sifvjcomp(u, vjcomp, cidcomp, maxcid)
637 int u, vjcomp, cidcomp, maxcid;
641 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
642 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
645 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
646 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
647 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
648 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
651 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
652 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
659 * sifup - Config the interface up and enable IP packets to pass.
662 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
673 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
674 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
675 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
678 ifr.ifr_flags |= IFF_UP;
679 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
680 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
683 npi.protocol = PPP_IP;
684 npi.mode = NPMODE_PASS;
685 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
686 if (errno != ENOTTY) {
687 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
690 /* for backwards compatibility */
691 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
692 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
696 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
697 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
705 * sifdown - Config the interface down and disable IP.
717 npi.protocol = PPP_IP;
718 npi.mode = NPMODE_ERROR;
719 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
720 if (errno != ENOTTY) {
721 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
724 /* backwards compatibility */
725 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
726 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
730 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
731 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
738 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
739 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
740 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
743 ifr.ifr_flags &= ~IFF_UP;
744 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
745 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
753 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
756 #define SET_SA_FAMILY(addr, family) \
757 BZERO((char *) &(addr), sizeof(addr)); \
758 addr.sa_family = (family);
761 * sifaddr - Config the interface IP addresses and netmask.
772 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
773 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
774 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
775 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
776 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
779 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
780 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
781 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
785 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
786 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
787 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
788 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
796 * cifaddr - Clear the interface IP addresses, and delete routes
797 * through the interface if possible.
806 SET_SA_FAMILY(rt.rt_dst, AF_INET);
807 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
808 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
809 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
810 rt.rt_flags = RTF_HOST;
811 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
812 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
819 * sifdefaultroute - assign a default route through the address given.
822 sifdefaultroute(u, g)
826 SET_SA_FAMILY(rt.rt_dst, AF_INET);
827 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
828 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
829 rt.rt_flags = RTF_GATEWAY;
830 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
831 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
838 * cifdefaultroute - delete a default route through the address given.
841 cifdefaultroute(u, g)
845 SET_SA_FAMILY(rt.rt_dst, AF_INET);
846 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
847 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
848 rt.rt_flags = RTF_GATEWAY;
849 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
850 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
854 * sifproxyarp - Make a proxy ARP entry for the peer.
857 sifproxyarp(unit, hisaddr)
861 struct arpreq arpreq;
863 BZERO(&arpreq, sizeof(arpreq));
866 * Get the hardware address of an interface on the same subnet
867 * as our local address.
869 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
870 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
874 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
875 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
876 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
877 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
878 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
886 * cifproxyarp - Delete the proxy ARP entry for the peer.
889 cifproxyarp(unit, hisaddr)
893 struct arpreq arpreq;
895 BZERO(&arpreq, sizeof(arpreq));
896 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
897 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
898 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
899 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
906 * get_ether_addr - get the hardware address of an interface on the
907 * the same subnet as ipaddr.
912 get_ether_addr(ipaddr, hwaddr)
914 struct sockaddr *hwaddr;
916 struct ifreq *ifr, *ifend;
920 struct ifreq ifs[MAX_IFS];
921 struct ifdevea ifdevea;
923 ifc.ifc_len = sizeof(ifs);
925 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
926 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
931 * Scan through looking for an interface with an Internet
932 * address on the same subnet as `ipaddr'.
934 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
935 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
936 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
937 if (ifr->ifr_addr.sa_family == AF_INET) {
938 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
939 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
941 * Check that the interface is up, and not point-to-point
944 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
946 if ((ifreq.ifr_flags &
947 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
948 != (IFF_UP|IFF_BROADCAST))
951 * Get its netmask and check that it's on the right subnet.
953 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
955 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
956 if ((ipaddr & mask) != (ina & mask))
965 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
968 * Grab the physical address for this interface.
970 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
971 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
972 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
976 hwaddr->sa_family = AF_UNSPEC;
977 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
982 * Return user specified netmask, modified by any mask we might determine
983 * for address `addr' (in network byte order).
984 * Here we scan through the system's list of interfaces, looking for
985 * any non-point-to-point interfaces which might appear to be on the same
986 * network as `addr'. If we find any, we OR in their netmask to the
987 * user-specified netmask.
993 u_int32_t mask, nmask, ina;
994 struct ifreq *ifr, *ifend, ifreq;
996 struct ifreq ifs[MAX_IFS];
999 if (IN_CLASSA(addr)) /* determine network mask for address class */
1000 nmask = IN_CLASSA_NET;
1001 else if (IN_CLASSB(addr))
1002 nmask = IN_CLASSB_NET;
1004 nmask = IN_CLASSC_NET;
1005 /* class D nets are disallowed by bad_ip_adrs */
1006 mask = netmask | htonl(nmask);
1009 * Scan through the system's network interfaces.
1011 ifc.ifc_len = sizeof(ifs);
1013 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1014 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1017 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1018 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1019 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1021 * Check the interface's internet address.
1023 if (ifr->ifr_addr.sa_family != AF_INET)
1025 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1026 if ((ntohl(ina) & nmask) != (addr & nmask))
1029 * Check that the interface is up, and not point-to-point or loopback.
1031 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1032 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1034 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1038 * Get its netmask and OR it into our mask.
1040 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1042 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1050 Seems like strdup() is not part of string package in Ultrix.
1051 If I understood the man-page on the sun this should work.
1056 char *strdup( in ) char *in;
1059 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1060 (void) strcpy( dup, in );
1065 * This logwtmp() implementation is subject to the following copyright:
1067 * Copyright (c) 1988 The Regents of the University of California.
1068 * All rights reserved.
1070 * Redistribution and use in source and binary forms are permitted
1071 * provided that the above copyright notice and this paragraph are
1072 * duplicated in all such forms and that any documentation,
1073 * advertising materials, and other materials related to such
1074 * distribution and use acknowledge that the software was developed
1075 * by the University of California, Berkeley. The name of the
1076 * University may not be used to endorse or promote products derived
1077 * from this software without specific prior written permission.
1078 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1079 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1080 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1083 #define WTMPFILE "/usr/adm/wtmp"
1086 logwtmp(line, name, host)
1087 char *line, *name, *host;
1093 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1095 if (!fstat(fd, &buf)) {
1096 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1097 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1098 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1099 (void)time(&ut.ut_time);
1100 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1101 (void)ftruncate(fd, buf.st_size);
1107 * Routines for locking and unlocking the serial device, moved here
1111 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1114 * lock - create a lock file for the named device
1123 if ((p = strrchr(dev, '/')) != NULL)
1125 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1126 if (lock_file == NULL)
1127 novm("lock file name");
1128 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1130 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1132 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1133 /* Read the lock file to find out who has the device locked */
1134 n = read(fd, &pid, sizeof(pid));
1136 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1139 if (kill(pid, 0) == -1 && errno == ESRCH) {
1140 /* pid no longer exists - remove the lock file */
1141 if (unlink(lock_file) == 0) {
1143 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1147 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1150 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1155 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1162 write(fd, &pid, sizeof pid);
1169 * unlock - remove our lockfile