+/*
+ * Return user specified netmask, modified by any mask we might determine
+ * for address `addr' (in network byte order).
+ * Here we scan through the system's list of interfaces, looking for
+ * any non-point-to-point interfaces which might appear to be on the same
+ * network as `addr'. If we find any, we OR in their netmask to the
+ * user-specified netmask.
+ */
+u_int32_t
+GetMask(addr)
+ u_int32_t addr;
+{
+ u_int32_t mask, nmask, ina;
+ struct ifreq *ifr, *ifend, ifreq;
+ struct ifconf ifc;
+
+ addr = ntohl(addr);
+ if (IN_CLASSA(addr)) /* determine network mask for address class */
+ nmask = IN_CLASSA_NET;
+ else if (IN_CLASSB(addr))
+ nmask = IN_CLASSB_NET;
+ else
+ nmask = IN_CLASSC_NET;
+ /* class D nets are disallowed by bad_ip_adrs */
+ mask = netmask | htonl(nmask);
+
+ /*
+ * Scan through the system's network interfaces.
+ */
+ ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
+ ifc.ifc_req = (struct ifreq *)alloca(ifc.ifc_len);
+ if (ifc.ifc_req == 0)
+ return mask;
+ if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
+ warn("Couldn't get system interface list: %m");
+ return mask;
+ }
+ ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
+ for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
+ /*
+ * Check the interface's internet address.
+ */
+ if (ifr->ifr_addr.sa_family == AF_INET) {
+ ina = INET_ADDR(ifr->ifr_addr);
+ if ((ntohl(ina) & nmask) != (addr & nmask))
+ continue;
+ /*
+ * Check that the interface is up, and not point-to-point or loopback.
+ */
+ strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+ if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
+ continue;
+ if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
+ != IFF_UP)
+ continue;
+ /*
+ * Get its netmask and OR it into our mask.
+ */
+ if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
+ continue;
+ mask |= INET_ADDR(ifreq.ifr_addr);
+ break;
+ } else {
+ if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
+ ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
+ }
+ }
+
+ return mask;
+}
+
+/*
+ * have_route_to - determine if the system has any route to
+ * a given IP address. `addr' is in network byte order.
+ * For demand mode to work properly, we have to ignore routes
+ * through our own interface.
+ */
+int have_route_to(u_int32_t addr)
+{
+ char buf[sizeof(struct rt_msghdr) + (sizeof(struct sockaddr_in))];
+ int status;
+ int s, n;
+ struct rt_msghdr *rtm;
+ struct sockaddr_in *sin;
+ int msglen = sizeof(*rtm) + (sizeof(*sin));
+ char *cp;
+ char msg[2048];
+
+ rtm = (struct rt_msghdr *)buf;
+ memset(rtm, 0, msglen);
+ rtm->rtm_msglen = msglen;
+ rtm->rtm_version = RTM_VERSION;
+ rtm->rtm_type = RTM_GET;
+ rtm->rtm_addrs = RTA_DST;
+ /* rtm->rtm_addrs, rtm_flags should be set on output */
+
+ sin = (struct sockaddr_in *)((u_char *)rtm + sizeof(*rtm));
+ sin->sin_len = sizeof(*sin);
+ sin->sin_family = AF_INET;
+ sin->sin_addr.s_addr = addr;
+
+ status = 0;
+
+ if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
+ return -1;
+ if (write(s, (char *)rtm, msglen) != msglen) {
+ close(s);
+ return status == ESRCH? 0: -1;
+ }
+
+ n = read(s, msg, 2048);
+ close(s);
+ if (n <= 0)
+ return -1;
+
+ rtm = (struct rt_msghdr *) msg;
+ if (rtm->rtm_version != RTM_VERSION)
+ return -1;
+
+ /* here we try to work out if the route is through our own interface */
+ cp = (char *)(rtm + 1);
+ if (rtm->rtm_addrs & RTA_DST) {
+ struct sockaddr *sa = (struct sockaddr *) cp;
+ cp = (char *)(((unsigned long)cp + sa->sa_len
+ + sizeof(long) - 1) & ~(sizeof(long) - 1));
+ }
+ if (rtm->rtm_addrs & RTA_GATEWAY) {
+ sin = (struct sockaddr_in *) cp;
+ if (sin->sin_addr.s_addr == ifaddrs[0]
+ || sin->sin_addr.s_addr == ifaddrs[1])
+ return 0; /* route is through our interface */
+ }
+
+ return 1;
+}
+
+static int
+strioctl(fd, cmd, ptr, ilen, olen)
+ int fd, cmd, ilen, olen;
+ void *ptr;
+{
+ struct strioctl str;
+
+ str.ic_cmd = cmd;
+ str.ic_timout = 0;
+ str.ic_len = ilen;
+ str.ic_dp = ptr;
+ if (ioctl(fd, I_STR, &str) == -1)
+ return -1;
+ if (str.ic_len != olen)
+ dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
+ olen, str.ic_len, cmd);
+ return 0;
+}
+
+/*
+ * Use the hostid as part of the random number seed.
+ */
+int
+get_host_seed()
+{
+ return gethostid();
+}
+
+/*
+ * get_pty - get a pty master/slave pair and chown the slave side
+ * to the uid given. Assumes slave_name points to >= 12 bytes of space.
+ */
+int
+get_pty(master_fdp, slave_fdp, slave_name, uid)
+ int *master_fdp;
+ int *slave_fdp;
+ char *slave_name;
+ int uid;
+{
+ int i, mfd, sfd;
+ char pty_name[12];
+ struct termios tios;
+
+ sfd = -1;
+ for (i = 0; i < 64; ++i) {
+ slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
+ 'p' + i / 16, i % 16);
+ mfd = open(pty_name, O_RDWR, 0);
+ if (mfd >= 0) {
+ pty_name[5] = 't';
+ sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
+ if (sfd >= 0)
+ break;
+ close(mfd);
+ }
+ }
+ if (sfd < 0)
+ return 0;
+
+ strlcpy(slave_name, pty_name, 12);
+ *master_fdp = mfd;
+ *slave_fdp = sfd;
+ fchown(sfd, uid, -1);
+ fchmod(sfd, S_IRUSR | S_IWUSR);
+ if (tcgetattr(sfd, &tios) == 0) {
+ tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
+ tios.c_cflag |= CS8 | CREAD;
+ tios.c_iflag = IGNPAR | CLOCAL;
+ tios.c_oflag = 0;
+ tios.c_lflag = 0;
+ if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
+ warn("couldn't set attributes on pty: %m");
+ } else
+ warn("couldn't get attributes on pty: %m");
+
+ return 1;
+}
+
+#if 0