+ }
+
+/*
+ * 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 (u_int32_t addr)
+ {
+ u_int32_t mask, nmask, ina;
+ struct ifreq *ifr, *ifend, ifreq;
+ struct ifconf ifc;
+ struct ifreq ifs[MAX_IFS];
+
+ 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 = sizeof(ifs);
+ ifc.ifc_req = ifs;
+ if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
+ {
+ syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %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)
+ {
+ continue;
+ }
+ ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
+ if (((ntohl(ina) ^ addr) & nmask) != 0)
+ {
+ continue;
+ }
+/*
+ * Check that the interface is up, and not point-to-point nor loopback.
+ */
+ strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+ if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
+ {
+ continue;
+ }
+
+ if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
+ {
+ continue;
+ }
+/*
+ * Get its netmask and OR it into our mask.
+ */
+ if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
+ {
+ continue;
+ }
+ mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
+ break;
+ }
+ return mask;
+ }
+
+/*
+ * Internal routine to decode the version.modification.patch level
+ */
+
+static void decode_version (char *buf, int *version,
+ int *modification, int *patch)
+ {
+ *version = (int) strtoul (buf, &buf, 10);
+ *modification = 0;
+ *patch = 0;
+
+ if (*buf == '.')
+ {
+ ++buf;
+ *modification = (int) strtoul (buf, &buf, 10);
+ if (*buf == '.')
+ {
+ ++buf;
+ *patch = (int) strtoul (buf, &buf, 10);
+ }
+ }
+
+ if (*buf != '\0')
+ {
+ *version =
+ *modification =
+ *patch = 0;
+ }
+ }
+
+/*
+ * Procedure to determine if the PPP line dicipline is registered.
+ */
+
+int
+ppp_registered(void)
+ {
+ int local_fd;
+ int ppp_disc = N_PPP;
+ int init_disc = -1;
+ int initfdflags;
+
+ local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
+ if (local_fd < 0)
+ {
+ syslog(LOG_ERR, "Failed to open %s: %m", devnam);
+ return 0;
+ }
+
+ initfdflags = fcntl(local_fd, F_GETFL);
+ if (initfdflags == -1)
+ {
+ syslog(LOG_ERR, "Couldn't get device fd flags: %m");
+ close (local_fd);
+ return 0;
+ }
+
+ initfdflags &= ~O_NONBLOCK;
+ fcntl(local_fd, F_SETFL, initfdflags);
+/*
+ * Read the initial line dicipline and try to put the device into the
+ * PPP dicipline.
+ */
+ if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
+ {
+ syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
+ close (local_fd);
+ return 0;
+ }
+
+ if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
+ {
+ syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
+ close (local_fd);
+ return 0;
+ }
+
+ if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
+ {
+ syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
+ close (local_fd);
+ return 0;
+ }
+
+ close (local_fd);
+ return 1;
+ }