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.9 1995/04/27 00:45:24 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 (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
406 syslog(LOG_WARNING, "tcsetattr: %m");
407 restore_term = FALSE;
412 * setdtr - control the DTR line on the serial port.
413 * This is called from die(), so it shouldn't call die().
418 int modembits = TIOCM_DTR;
420 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
425 * output - Output PPP packet.
434 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
436 log_packet(p, len, "sent ");
438 if (write(fd, p, len) < 0) {
439 syslog(LOG_ERR, "write: %m");
446 * wait_input - wait until there is data available on fd,
447 * for the length of time specified by *timo (indefinite
451 struct timeval *timo;
458 n = select(fd+1, &ready, NULL, &ready, timo);
459 if (n < 0 && errno != EINTR) {
460 syslog(LOG_ERR, "select: %m");
467 * read_packet - get a PPP packet from the serial device.
475 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
476 if (errno == EWOULDBLOCK || errno == EINTR) {
477 MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
480 syslog(LOG_ERR, "read(fd): %m");
488 * ppp_send_config - configure the transmit characteristics of
492 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
499 if (ioctl(fd, PPPIOCSMTU, &mtu) < 0) {
500 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
504 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
505 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
509 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
510 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
513 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
514 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
515 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
516 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
523 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
526 ppp_set_xaccm(unit, accm)
530 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
531 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
536 * ppp_recv_config - configure the receive-side characteristics of
540 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
547 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
548 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
551 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
552 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
555 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
556 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
559 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
560 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
561 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
567 * ccp_test - ask kernel whether a given compression method
568 * is acceptable for use.
570 ccp_test(unit, opt_ptr, opt_len, for_transmit)
571 int unit, opt_len, for_transmit;
574 struct ppp_option_data data;
577 data.length = opt_len;
578 data.transmit = for_transmit;
579 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
583 * ccp_flags_set - inform kernel about the current state of CCP.
586 ccp_flags_set(unit, isopen, isup)
587 int unit, isopen, isup;
591 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
592 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
595 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
596 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
597 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
598 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
602 * ccp_fatal_error - returns 1 if decompression was disabled as a
603 * result of an error detected after decompression of a packet,
604 * 0 otherwise. This is necessary because of patent nonsense.
607 ccp_fatal_error(unit)
612 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
613 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
616 return x & SC_DC_FERROR;
620 * sifvjcomp - config tcp header compression
623 sifvjcomp(u, vjcomp, cidcomp, maxcid)
624 int u, vjcomp, cidcomp, maxcid;
628 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
629 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
632 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
633 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
634 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
635 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
638 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
639 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
646 * sifup - Config the interface up and enable IP packets to pass.
649 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
660 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
661 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
662 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
665 ifr.ifr_flags |= IFF_UP;
666 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
667 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
670 npi.protocol = PPP_IP;
671 npi.mode = NPMODE_PASS;
672 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
673 if (errno != ENOTTY) {
674 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
677 /* for backwards compatibility */
678 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
679 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
683 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
684 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
692 * sifdown - Config the interface down and disable IP.
704 npi.protocol = PPP_IP;
705 npi.mode = NPMODE_ERROR;
706 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
707 if (errno != ENOTTY) {
708 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
711 /* backwards compatibility */
712 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
713 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
717 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
718 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
725 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
726 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
727 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
730 ifr.ifr_flags &= ~IFF_UP;
731 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
732 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
740 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
743 #define SET_SA_FAMILY(addr, family) \
744 BZERO((char *) &(addr), sizeof(addr)); \
745 addr.sa_family = (family);
748 * sifaddr - Config the interface IP addresses and netmask.
759 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
760 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
761 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
762 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
763 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
766 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
767 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
768 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
772 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
773 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
774 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
775 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
783 * cifaddr - Clear the interface IP addresses, and delete routes
784 * through the interface if possible.
793 SET_SA_FAMILY(rt.rt_dst, AF_INET);
794 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
795 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
796 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
797 rt.rt_flags = RTF_HOST;
798 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
799 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
806 * sifdefaultroute - assign a default route through the address given.
809 sifdefaultroute(u, g)
813 SET_SA_FAMILY(rt.rt_dst, AF_INET);
814 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
815 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
816 rt.rt_flags = RTF_GATEWAY;
817 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
818 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
825 * cifdefaultroute - delete a default route through the address given.
828 cifdefaultroute(u, g)
832 SET_SA_FAMILY(rt.rt_dst, AF_INET);
833 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
834 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
835 rt.rt_flags = RTF_GATEWAY;
836 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
837 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
841 * sifproxyarp - Make a proxy ARP entry for the peer.
844 sifproxyarp(unit, hisaddr)
848 struct arpreq arpreq;
850 BZERO(&arpreq, sizeof(arpreq));
853 * Get the hardware address of an interface on the same subnet
854 * as our local address.
856 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
857 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
861 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
862 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
863 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
864 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
865 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
873 * cifproxyarp - Delete the proxy ARP entry for the peer.
876 cifproxyarp(unit, hisaddr)
880 struct arpreq arpreq;
882 BZERO(&arpreq, sizeof(arpreq));
883 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
884 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
885 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
886 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
893 * get_ether_addr - get the hardware address of an interface on the
894 * the same subnet as ipaddr.
899 get_ether_addr(ipaddr, hwaddr)
901 struct sockaddr *hwaddr;
903 struct ifreq *ifr, *ifend, *ifp;
905 struct sockaddr_dl *dla;
908 struct ifreq ifs[MAX_IFS];
910 ifc.ifc_len = sizeof(ifs);
912 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
913 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
918 * Scan through looking for an interface with an Internet
919 * address on the same subnet as `ipaddr'.
921 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
922 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
923 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
924 if (ifr->ifr_addr.sa_family == AF_INET) {
925 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
926 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
928 * Check that the interface is up, and not point-to-point
931 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
933 if ((ifreq.ifr_flags &
934 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
935 != (IFF_UP|IFF_BROADCAST))
938 * Get its netmask and check that it's on the right subnet.
940 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
942 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
943 if ((ipaddr & mask) != (ina & mask))
952 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
955 * Now scan through again looking for a link-level address
956 * for this interface.
959 for (ifr = ifc.ifc_req; ifr < ifend; ) {
960 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
961 && ifr->ifr_addr.sa_family == AF_DLI) {
963 * Found the link-level address - copy it out
965 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
966 hwaddr->sa_family = AF_UNSPEC;
967 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
970 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
978 * Return user specified netmask, modified by any mask we might determine
979 * for address `addr' (in network byte order).
980 * Here we scan through the system's list of interfaces, looking for
981 * any non-point-to-point interfaces which might appear to be on the same
982 * network as `addr'. If we find any, we OR in their netmask to the
983 * user-specified netmask.
989 u_int32_t mask, nmask, ina;
990 struct ifreq *ifr, *ifend, ifreq;
992 struct ifreq ifs[MAX_IFS];
995 if (IN_CLASSA(addr)) /* determine network mask for address class */
996 nmask = IN_CLASSA_NET;
997 else if (IN_CLASSB(addr))
998 nmask = IN_CLASSB_NET;
1000 nmask = IN_CLASSC_NET;
1001 /* class D nets are disallowed by bad_ip_adrs */
1002 mask = netmask | htonl(nmask);
1005 * Scan through the system's network interfaces.
1007 ifc.ifc_len = sizeof(ifs);
1009 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1010 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1013 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1014 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1015 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1017 * Check the interface's internet address.
1019 if (ifr->ifr_addr.sa_family != AF_INET)
1021 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1022 if ((ntohl(ina) & nmask) != (addr & nmask))
1025 * Check that the interface is up, and not point-to-point or loopback.
1027 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1028 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1030 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1034 * Get its netmask and OR it into our mask.
1036 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1038 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1046 Seems like strdup() is not part of string package in Ultrix.
1047 If I understood the man-page on the sun this should work.
1052 char *strdup( in ) char *in;
1055 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1056 (void) strcpy( dup, in );
1061 * This logwtmp() implementation is subject to the following copyright:
1063 * Copyright (c) 1988 The Regents of the University of California.
1064 * All rights reserved.
1066 * Redistribution and use in source and binary forms are permitted
1067 * provided that the above copyright notice and this paragraph are
1068 * duplicated in all such forms and that any documentation,
1069 * advertising materials, and other materials related to such
1070 * distribution and use acknowledge that the software was developed
1071 * by the University of California, Berkeley. The name of the
1072 * University may not be used to endorse or promote products derived
1073 * from this software without specific prior written permission.
1074 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1075 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1076 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1079 #define WTMPFILE "/usr/adm/wtmp"
1082 logwtmp(line, name, host)
1083 char *line, *name, *host;
1089 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1091 if (!fstat(fd, &buf)) {
1092 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1093 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1094 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1095 (void)time(&ut.ut_time);
1096 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1097 (void)ftruncate(fd, buf.st_size);
1103 * Routines for locking and unlocking the serial device, moved here
1107 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1110 * lock - create a lock file for the named device
1119 if ((p = strrchr(dev, '/')) != NULL)
1121 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1122 if (lock_file == NULL)
1123 novm("lock file name");
1124 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1126 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1128 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1129 /* Read the lock file to find out who has the device locked */
1130 n = read(fd, &pid, sizeof(pid));
1132 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1135 if (kill(pid, 0) == -1 && errno == ESRCH) {
1136 /* pid no longer exists - remove the lock file */
1137 if (unlink(lock_file) == 0) {
1139 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1143 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1146 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1151 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1158 write(fd, &pid, sizeof pid);
1165 * unlock - remove our lockfile