]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-bsd.c
use strlcpy, strlcat, slprintf everywhere
[ppp.git] / pppd / sys-bsd.c
index 46962dec0ff63af5f04993646f8d5349b4875d00..c2475a18d0b28cee72aba20a06ea0786a6f08a3c 100644 (file)
@@ -21,8 +21,9 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: sys-bsd.c,v 1.29 1997/11/27 06:10:04 paulus Exp $";
+static char rcsid[] = "$Id: sys-bsd.c,v 1.36 1999/03/12 06:07:21 paulus Exp $";
 /*     $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */
 /*     $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */
+#endif
 
 /*
  * TODO:
 
 /*
  * TODO:
@@ -62,9 +63,13 @@ static char rcsid[] = "$Id: sys-bsd.c,v 1.29 1997/11/27 06:10:04 paulus Exp $";
 #if defined(NetBSD) && (NetBSD >= 199703)
 #include <netinet/if_inarp.h>
 #else  /* NetBSD 1.2D or later */
 #if defined(NetBSD) && (NetBSD >= 199703)
 #include <netinet/if_inarp.h>
 #else  /* NetBSD 1.2D or later */
+#ifdef __FreeBSD__
+#include <netinet/if_ether.h>
+#else
 #include <net/if_ether.h>
 #endif
 #endif
 #include <net/if_ether.h>
 #endif
 #endif
+#endif
 
 #include "pppd.h"
 #include "fsm.h"
 
 #include "pppd.h"
 #include "fsm.h"
@@ -123,7 +128,7 @@ sys_cleanup()
     struct ifreq ifr;
 
     if (if_is_up) {
     struct ifreq ifr;
 
     if (if_is_up) {
-       strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+       strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
            && ((ifr.ifr_flags & IFF_UP) != 0)) {
            ifr.ifr_flags &= ~IFF_UP;
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
            && ((ifr.ifr_flags & IFF_UP) != 0)) {
            ifr.ifr_flags &= ~IFF_UP;
@@ -154,9 +159,16 @@ sys_close()
 /*
  * sys_check_options - check the options that the user specified
  */
 /*
  * sys_check_options - check the options that the user specified
  */
-void
+int
 sys_check_options()
 {
 sys_check_options()
 {
+#ifndef CDTRCTS
+    if (crtscts == 2) {
+       syslog(LOG_WARNING, "DTR/CTS flow control is not supported on this system");
+       return 0;
+    }
+#endif
+    return 1;
 }
 
 /*
 }
 
 /*
@@ -173,7 +185,7 @@ ppp_available()
     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        return 1;               /* can't tell */
 
     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        return 1;               /* can't tell */
 
-    strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), "ppp0");
     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
     close(s);
 
     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
     close(s);
 
@@ -380,10 +392,19 @@ set_up_tty(fd, local)
     }
 
     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
     }
 
     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
-    if (crtscts > 0 && !local)
-       tios.c_cflag |= CRTSCTS;
-    else if (crtscts < 0)
+    if (crtscts > 0 && !local) {
+        if (crtscts == 2) {
+#ifdef CDTRCTS
+            tios.c_cflag |= CDTRCTS;
+#endif
+       } else
+           tios.c_cflag |= CRTSCTS;
+    } else if (crtscts < 0) {
        tios.c_cflag &= ~CRTSCTS;
        tios.c_cflag &= ~CRTSCTS;
+#ifdef CDTRCTS
+       tios.c_cflag &= ~CDTRCTS;
+#endif
+    }
 
     tios.c_cflag |= CS8 | CREAD | HUPCL;
     if (local || !modem)
 
     tios.c_cflag |= CS8 | CREAD | HUPCL;
     if (local || !modem)
@@ -667,7 +688,7 @@ ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
     u_int x;
     struct ifreq ifr;
 
     u_int x;
     struct ifreq ifr;
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     ifr.ifr_mtu = mtu;
     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
     ifr.ifr_mtu = mtu;
     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
@@ -867,7 +888,7 @@ sifup(u)
 {
     struct ifreq ifr;
 
 {
     struct ifreq ifr;
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        return 0;
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        return 0;
@@ -918,7 +939,7 @@ sifdown(u)
     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
     /* ignore errors, because ppp_fd might have been closed by now. */
 
     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
     /* ignore errors, because ppp_fd might have been closed by now. */
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        rv = 0;
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        rv = 0;
@@ -953,7 +974,7 @@ sifaddr(u, o, h, m)
     struct ifaliasreq ifra;
     struct ifreq ifr;
 
     struct ifaliasreq ifra;
     struct ifreq ifr;
 
-    strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
+    strlcpy(ifra.ifra_name, sizeof(ifra.ifra_name), ifname);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
@@ -964,7 +985,7 @@ sifaddr(u, o, h, m)
     } else
        BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
     BZERO(&ifr, sizeof(ifr));
     } else
        BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
     BZERO(&ifr, sizeof(ifr));
-    strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
        if (errno != EADDRNOTAVAIL)
            syslog(LOG_WARNING, "Couldn't remove interface address: %m");
     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
        if (errno != EADDRNOTAVAIL)
            syslog(LOG_WARNING, "Couldn't remove interface address: %m");
@@ -995,7 +1016,7 @@ cifaddr(u, o, h)
     struct ifaliasreq ifra;
 
     ifaddrs[0] = 0;
     struct ifaliasreq ifra;
 
     ifaddrs[0] = 0;
-    strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
+    strlcpy(ifra.ifra_name, sizeof(ifra.ifra_name), ifname);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
@@ -1055,7 +1076,7 @@ dodefaultroute(g, cmd)
 
     memset(&rtmsg, 0, sizeof(rtmsg));
     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
 
     memset(&rtmsg, 0, sizeof(rtmsg));
     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
-    rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
+    rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
     rtmsg.hdr.rtm_version = RTM_VERSION;
     rtmsg.hdr.rtm_seq = ++rtm_seq;
     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
     rtmsg.hdr.rtm_version = RTM_VERSION;
     rtmsg.hdr.rtm_seq = ++rtm_seq;
     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
@@ -1273,7 +1294,7 @@ get_ether_addr(ipaddr, hwaddr)
                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
        if (ifr->ifr_addr.sa_family == AF_INET) {
            ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
        if (ifr->ifr_addr.sa_family == AF_INET) {
            ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
-           strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+           strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
            /*
             * Check that the interface is up, and not point-to-point
             * or loopback.
            /*
             * Check that the interface is up, and not point-to-point
             * or loopback.
@@ -1372,7 +1393,7 @@ GetMask(addr)
        /*
         * Check that the interface is up, and not point-to-point or loopback.
         */
        /*
         * Check that the interface is up, and not point-to-point or loopback.
         */
-       strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+       strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
            continue;
        if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
            continue;
        if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
@@ -1389,6 +1410,26 @@ GetMask(addr)
     return mask;
 }
 
     return mask;
 }
 
+/*
+ * have_route_to - determine if the system has any route to
+ * a given IP address.
+ * For demand mode to work properly, we have to ignore routes
+ * through our own interface.
+ */
+int have_route_to(u_int32_t addr)
+{
+    return -1;
+}
+
+/*
+ * Use the hostid as part of the random number seed.
+ */
+int
+get_host_seed()
+{
+    return gethostid();
+}
+
 /*
  * lock - create a lock file for the named lock device
  */
 /*
  * lock - create a lock file for the named lock device
  */
@@ -1401,13 +1442,15 @@ lock(dev)
     char hdb_lock_buffer[12];
     int fd, pid, n;
     char *p;
     char hdb_lock_buffer[12];
     int fd, pid, n;
     char *p;
+    size_t l;
 
     if ((p = strrchr(dev, '/')) != NULL)
        dev = p + 1;
 
     if ((p = strrchr(dev, '/')) != NULL)
        dev = p + 1;
-    lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
+    l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
+    lock_file = malloc(l);
     if (lock_file == NULL)
        novm("lock file name");
     if (lock_file == NULL)
        novm("lock file name");
-    strcat(strcpy(lock_file, LOCK_PREFIX), dev);
+    slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
 
     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
        if (errno == EEXIST
 
     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
        if (errno == EEXIST