2 * sys-str.c - System-dependent procedures for setting up
3 * PPP interfaces on systems which use the STREAMS ppp interface.
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-str.c,v 1.22 1995/07/04 12:35:56 paulus Exp $";
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
46 #include <arpa/inet.h>
48 #include <net/ppp_defs.h>
49 #include <net/ppp_str.h>
50 #include <net/route.h>
51 #include <net/if_arp.h>
52 #include <net/nit_if.h>
53 #include <netinet/in.h>
58 #define ifr_mtu ifr_metric
61 #define MAXMODULES 10 /* max number of module names to save */
62 static struct modlist {
63 char modname[FMNAMESZ+1];
64 } str_modules[MAXMODULES];
65 static int str_module_count = 0;
66 static int pushed_ppp;
67 static int closed_stdio;
69 static int restore_term; /* 1 => we've munged the terminal */
70 static struct termios inittermios; /* Initial TTY termios */
72 int sockfd; /* socket for doing interface ioctls */
73 static char *lock_file; /* lock file created for serial port */
76 * sys_init - System-dependent initialization.
81 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
82 setlogmask(LOG_UPTO(LOG_INFO));
84 setlogmask(LOG_UPTO(LOG_DEBUG));
86 /* Get an internet socket for doing socket ioctl's on. */
87 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
88 syslog(LOG_ERR, "Couldn't create IP socket: %m");
94 * note_debug_level - note a change in the debug level.
100 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
101 setlogmask(LOG_UPTO(LOG_DEBUG));
103 setlogmask(LOG_UPTO(LOG_WARNING));
108 * daemon - Detach us from the terminal session.
111 daemon(nochdir, noclose)
112 int nochdir, noclose;
116 if ((pid = fork()) < 0)
119 exit(0); /* parent dies */
124 fclose(stdin); /* don't need stdin, stdout, stderr */
133 * ppp_available - check if this kernel supports PPP.
140 fd = open("/dev/tty", O_RDONLY, 0);
142 return 1; /* can't find out - assume we have ppp */
143 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
150 * establish_ppp - Turn the serial port into a ppp interface.
155 /* go through and save the name of all the modules, then pop em */
157 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
158 ioctl(fd, I_POP, 0) < 0)
160 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
161 str_modules[str_module_count].modname));
165 /* now push the async/fcs module */
166 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
167 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
170 /* push the compress module */
171 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
172 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
174 /* finally, push the ppp_if module that actually handles the */
175 /* network interface */
176 if (ioctl(fd, I_PUSH, "pppif") < 0) {
177 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
181 /* read mode, message non-discard mode */
182 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
183 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
187 * Find out which interface we were given.
188 * (ppp_if handles this ioctl)
190 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
191 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
195 /* Set debug flags in driver */
196 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
197 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
200 /* close stdin, stdout, stderr if they might refer to the device */
201 if (default_device && !closed_stdio) {
204 for (i = 0; i <= 2; ++i)
205 if (i != fd && i != sockfd)
212 * disestablish_ppp - Restore the serial port to normal operation.
213 * It attempts to reconstruct the stream with the previously popped
214 * modules. This shouldn't call die() because it's called from die().
223 /* we can't push or pop modules after the stream has hung up */
224 str_module_count = 0;
225 restore_term = 0; /* nor can we fix up terminal settings */
231 * Check whether the link seems not to be 8-bit clean.
233 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
235 switch (~flags & PAI_FLAGS_HIBITS) {
237 s = "bit 7 set to 1";
240 s = "bit 7 set to 0";
242 case PAI_FLAGS_PAR_EVEN:
245 case PAI_FLAGS_PAR_ODD:
250 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
251 syslog(LOG_WARNING, "All received characters had %s", s);
255 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
258 for (; str_module_count > 0; str_module_count--) {
259 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
261 syslog(LOG_WARNING, "Couldn't restore module %s: %m",
262 str_modules[str_module_count-1].modname);
264 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
265 str_modules[str_module_count-1].modname));
274 * List of valid speeds.
277 int speed_int, speed_val;
349 * Translate from bits/second to a speed_t.
355 struct speed *speedp;
359 for (speedp = speeds; speedp->speed_int; speedp++)
360 if (bps == speedp->speed_int)
361 return speedp->speed_val;
362 syslog(LOG_WARNING, "speed %d not supported", bps);
367 * Translate from a speed_t to bits/second.
373 struct speed *speedp;
377 for (speedp = speeds; speedp->speed_int; speedp++)
378 if (speed == speedp->speed_val)
379 return speedp->speed_int;
384 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
385 * at the requested speed, etc. If `local' is true, set CLOCAL
386 * regardless of whether the modem option was specified.
388 set_up_tty(fd, local)
394 if (tcgetattr(fd, &tios) < 0) {
395 syslog(LOG_ERR, "tcgetattr: %m");
402 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
404 tios.c_cflag |= CRTSCTS;
405 else if (crtscts < 0)
406 tios.c_cflag &= ~CRTSCTS;
408 tios.c_cflag |= CS8 | CREAD | HUPCL;
410 tios.c_cflag |= CLOCAL;
411 tios.c_iflag = IGNBRK | IGNPAR;
415 tios.c_cc[VTIME] = 0;
418 tios.c_iflag |= IXOFF;
419 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
420 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
423 speed = translate_speed(inspeed);
425 cfsetospeed(&tios, speed);
426 cfsetispeed(&tios, speed);
428 speed = cfgetospeed(&tios);
430 * We can't proceed if the serial port speed is B0,
431 * since that implies that the serial port is disabled.
434 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
440 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
441 syslog(LOG_ERR, "tcsetattr: %m");
445 baud_rate = inspeed = baud_rate_of(speed);
450 * restore_tty - restore the terminal to the saved settings.
456 if (!default_device) {
458 * Turn off echoing, because otherwise we can get into
459 * a loop with the tty and the modem echoing to each other.
460 * We presume we are the sole user of this tty device, so
461 * when we close it, it will revert to its defaults anyway.
463 inittermios.c_lflag &= ~(ECHO | ECHONL);
465 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
467 syslog(LOG_WARNING, "tcsetattr: %m");
473 * setdtr - control the DTR line on the serial port.
474 * This is called from die(), so it shouldn't call die().
479 int modembits = TIOCM_DTR;
481 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
486 * output - Output PPP packet.
497 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
499 log_packet(p, len, "sent ");
502 str.buf = (caddr_t) p;
503 if (putmsg(fd, NULL, &str, 0) < 0) {
504 if (errno != ENXIO) {
505 syslog(LOG_ERR, "putmsg: %m");
512 * wait_input - wait for input, for a length of time specified in *timo.
515 struct timeval *timo;
520 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
522 pfd.events = POLLIN | POLLPRI | POLLHUP;
523 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
524 syslog(LOG_ERR, "poll: %m");
530 * read_packet - get a PPP packet from the serial device.
536 struct strbuf str, ctl;
538 unsigned char ctlbuf[16];
540 str.maxlen = PPP_MTU + PPP_HDRLEN;
541 str.buf = (caddr_t) buf;
542 ctl.maxlen = sizeof(ctlbuf);
543 ctl.buf = (caddr_t) ctlbuf;
545 len = getmsg(fd, &ctl, &str, &i);
547 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
550 syslog(LOG_ERR, "getmsg(fd) %m");
554 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
556 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
557 ctlbuf[0], ctlbuf[1]);
560 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
569 * ppp_send_config - configure the transmit characteristics of
573 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
581 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
583 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
584 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
588 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
589 if (hungup && errno == ENXIO)
591 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
596 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
597 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
602 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
603 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
610 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
613 ppp_set_xaccm(unit, accm)
617 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
618 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
623 * ppp_recv_config - configure the receive-side characteristics of
627 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
634 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
635 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
638 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
639 if (hungup && errno == ENXIO)
641 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
644 c = 2 + (pcomp? 1: 0);
645 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
646 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
649 c = 2 + (accomp? 1: 0);
650 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
651 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
656 * ccp_test - ask kernel whether a given compression method
657 * is acceptable for use.
659 ccp_test(unit, opt_ptr, opt_len, for_transmit)
660 int unit, opt_len, for_transmit;
663 struct ppp_option_data data;
665 if ((unsigned) opt_len > MAX_PPP_OPTION)
666 opt_len = MAX_PPP_OPTION;
667 data.length = opt_len;
668 data.transmit = for_transmit;
669 BCOPY(opt_ptr, data.opt_data, opt_len);
670 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
674 * ccp_flags_set - inform kernel about the current state of CCP.
677 ccp_flags_set(unit, isopen, isup)
678 int unit, isopen, isup;
682 x = (isopen? 1: 0) + (isup? 2: 0);
683 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY) {
684 if (hungup && errno == ENXIO)
686 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
691 * ccp_fatal_error - returns 1 if decompression was disabled as a
692 * result of an error detected after decompression of a packet,
693 * 0 otherwise. This is necessary because of patent nonsense.
696 ccp_fatal_error(unit)
701 if (ioctl(fd, SIOCGIFCOMP, (caddr_t) &x) < 0) {
702 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
705 return x & CCP_FATALERROR;
709 * sifvjcomp - config tcp header compression
712 sifvjcomp(u, vjcomp, cidcomp, maxcid)
713 int u, vjcomp, cidcomp, maxcid;
717 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
718 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
719 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
726 * sifup - Config the interface up and enable IP packets to pass.
735 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
736 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
737 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
740 ifr.ifr_flags |= IFF_UP;
741 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
742 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
745 npi.protocol = PPP_IP;
746 npi.mode = NPMODE_PASS;
747 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
748 if (errno != ENOTTY) {
749 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
757 * sifdown - Config the interface down.
768 npi.protocol = PPP_IP;
769 npi.mode = NPMODE_ERROR;
770 if (ioctl(fd, SIOCSETNPMODE, (caddr_t) &npi) < 0) {
771 if (errno != ENOTTY && errno != ENXIO) {
772 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
777 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
778 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
779 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
782 if ((ifr.ifr_flags & IFF_UP) != 0) {
783 ifr.ifr_flags &= ~IFF_UP;
784 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
785 syslog(LOG_ERR, "Couldn't mark interface down: %m");
794 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
796 #define SET_SA_FAMILY(addr, family) \
797 BZERO((char *) &(addr), sizeof(addr)); \
798 addr.sa_family = (family);
801 * sifaddr - Config the interface IP addresses and netmask.
812 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
813 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
814 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
815 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
816 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
819 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
820 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
821 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
825 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
826 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
827 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
828 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
836 * cifaddr - Clear the interface IP addresses, and delete routes
837 * through the interface if possible.
846 SET_SA_FAMILY(rt.rt_dst, AF_INET);
847 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
848 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
849 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
850 rt.rt_flags = RTF_HOST;
851 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
852 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
859 * sifdefaultroute - assign a default route through the address given.
862 sifdefaultroute(u, g)
868 SET_SA_FAMILY(rt.rt_dst, AF_INET);
869 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
870 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
871 rt.rt_flags = RTF_GATEWAY;
872 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
873 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
880 * cifdefaultroute - delete a default route through the address given.
883 cifdefaultroute(u, g)
889 SET_SA_FAMILY(rt.rt_dst, AF_INET);
890 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
891 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
892 rt.rt_flags = RTF_GATEWAY;
893 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
894 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
901 * sifproxyarp - Make a proxy ARP entry for the peer.
904 sifproxyarp(unit, hisaddr)
908 struct arpreq arpreq;
910 BZERO(&arpreq, sizeof(arpreq));
913 * Get the hardware address of an interface on the same subnet
914 * as our local address.
916 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
917 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
921 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
922 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
923 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
924 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
925 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
933 * cifproxyarp - Delete the proxy ARP entry for the peer.
936 cifproxyarp(unit, hisaddr)
940 struct arpreq arpreq;
942 BZERO(&arpreq, sizeof(arpreq));
943 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
944 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
945 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
946 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
953 * get_ether_addr - get the hardware address of an interface on the
954 * the same subnet as ipaddr.
959 get_ether_addr(ipaddr, hwaddr)
961 struct sockaddr *hwaddr;
963 struct ifreq *ifr, *ifend;
967 struct ifreq ifs[MAX_IFS];
970 ifc.ifc_len = sizeof(ifs);
972 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
973 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
978 * Scan through looking for an interface with an Internet
979 * address on the same subnet as `ipaddr'.
981 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
982 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
983 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
984 if (ifr->ifr_addr.sa_family == AF_INET) {
987 * Check that the interface is up, and not point-to-point
990 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
991 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
993 if ((ifreq.ifr_flags &
994 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
995 != (IFF_UP|IFF_BROADCAST))
999 * Get its netmask and check that it's on the right subnet.
1001 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1003 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1004 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1005 if ((ipaddr & mask) != (ina & mask))
1014 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1017 * Grab the physical address for this interface.
1019 if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1020 syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1023 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1024 if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1025 || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1026 syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1032 hwaddr->sa_family = AF_UNSPEC;
1033 memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1038 #define WTMPFILE "/usr/adm/wtmp"
1041 logwtmp(line, name, host)
1042 char *line, *name, *host;
1048 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1050 if (!fstat(fd, &buf)) {
1051 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1052 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1053 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1054 (void)time(&ut.ut_time);
1055 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1056 (void)ftruncate(fd, buf.st_size);
1062 * Return user specified netmask, modified by any mask we might determine
1063 * for address `addr' (in network byte order).
1064 * Here we scan through the system's list of interfaces, looking for
1065 * any non-point-to-point interfaces which might appear to be on the same
1066 * network as `addr'. If we find any, we OR in their netmask to the
1067 * user-specified netmask.
1075 u_int32_t mask, nmask, ina;
1076 struct ifreq *ifr, *ifend, ifreq;
1078 struct ifreq ifs[MAX_IFS];
1081 if (IN_CLASSA(addr)) /* determine network mask for address class */
1082 nmask = IN_CLASSA_NET;
1083 else if (IN_CLASSB(addr))
1084 nmask = IN_CLASSB_NET;
1086 nmask = IN_CLASSC_NET;
1087 /* class D nets are disallowed by bad_ip_adrs */
1088 mask = netmask | htonl(nmask);
1091 * Scan through the system's network interfaces.
1093 ifc.ifc_len = sizeof(ifs);
1095 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1096 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1099 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1100 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1102 * Check the interface's internet address.
1104 if (ifr->ifr_addr.sa_family != AF_INET)
1106 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1107 if ((ntohl(ina) & nmask) != (addr & nmask))
1110 * Check that the interface is up, and not point-to-point or loopback.
1112 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1113 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1115 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1119 * Get its netmask and OR it into our mask.
1121 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1123 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1130 * Code for locking/unlocking the serial device.
1131 * This code is derived from chat.c.
1134 #if !defined(HDB) && !defined(SUNOS3)
1135 #define HDB 1 /* ascii lock files are the default */
1141 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1143 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1145 #endif /* LOCK_DIR */
1148 * lock - create a lock file for the named device.
1154 char hdb_lock_buffer[12];
1158 if ((p = strrchr(dev, '/')) != NULL)
1160 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1161 if (lock_file == NULL)
1162 novm("lock file name");
1163 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1165 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1167 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1168 /* Read the lock file to find out who has the device locked */
1170 n = read(fd, hdb_lock_buffer, 11);
1172 hdb_lock_buffer[n] = 0;
1173 pid = atoi(hdb_lock_buffer);
1176 n = read(fd, &pid, sizeof(pid));
1179 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1182 if (kill(pid, 0) == -1 && errno == ESRCH) {
1183 /* pid no longer exists - remove the lock file */
1184 if (unlink(lock_file) == 0) {
1186 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1190 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1193 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1198 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1205 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1206 write(fd, hdb_lock_buffer, 11);
1209 write(fd, &pid, sizeof pid);
1217 * unlock - remove our lockfile
1230 * SunOS doesn't have strtoul :-(
1233 strtoul(str, ptr, base)
1237 return (unsigned long) strtol(str, ptr, base);