]> git.ozlabs.org Git - ppp.git/commitdiff
pppd: Combine get_first_ethernet() and get_if_hwaddr() into one function (#207)
authorpali <7141871+pali@users.noreply.github.com>
Sat, 2 Jan 2021 03:27:13 +0000 (04:27 +0100)
committerGitHub <noreply@github.com>
Sat, 2 Jan 2021 03:27:13 +0000 (14:27 +1100)
On all places is just needed hardware address for the first ethernet-style
interface. So provide it by new get_first_ether_hwaddr() function.

Signed-off-by: Pali Rohár <pali@kernel.org>
pppd/ipv6cp.c
pppd/multilink.c
pppd/pppd.h
pppd/sys-linux.c
pppd/sys-solaris.c

index b9c96d4f0e2c2108cdb46858d1f0e0d95f01f5a6..431cb62211bff1d28de4ca8151c070199a615877 100644 (file)
@@ -1068,18 +1068,10 @@ static int
 ether_to_eui64(eui64_t *p_eui64)
 {
     u_char addr[6];
-    char *if_name;
-
-    if (get_if_hwaddr(addr, devnam) < 0) {
-        if ((if_name = get_first_ethernet()) == NULL) {
-            error("no persistent id can be found");
-            return 0;
-        }
-
-        if (get_if_hwaddr(addr, if_name) < 0) {
-            error("could not obtain hardware address for %s", if_name);
-            return 0;
-        }
+
+    if (get_if_hwaddr(addr, devnam) < 0 || get_first_ether_hwaddr(addr) < 0) {
+        error("ipv6cp: no persistent id can be found");
+        return 0;
     }
 
     /*
index 07bd6d0c62b2ca769299978c6ce5cdf17bc3bbe6..ddd848c3270bcba9c8dbeda4c6324fbc2151989d 100644 (file)
@@ -430,13 +430,11 @@ owns_unit(TDB_DATA key, int unit)
 static int
 get_default_epdisc(struct epdisc *ep)
 {
-       char *p;
        struct hostent *hp;
        u_int32_t addr;
 
        /* First try for an ethernet MAC address */
-       p = get_first_ethernet();
-       if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
+       if (get_first_ether_hwaddr(ep->value) >= 0) {
                ep->class = EPD_MAC;
                ep->length = 6;
                return 1;
index 6ac0ab83e51754d9df69bdc5c69a6b303ec4138b..612902f55d0d6ceef0646980d7941b7571526e14 100644 (file)
@@ -710,7 +710,7 @@ int  sipxfaddr(int, unsigned long, unsigned char *);
 int  cipxfaddr(int);
 #endif
 int  get_if_hwaddr(u_char *addr, char *name);
-char *get_first_ethernet(void);
+int  get_first_ether_hwaddr(u_char *addr);
 int get_time(struct timeval *);
                                /* Get current time, monotonic if possible. */
 
index 8b538f0e4ac4431688ac421b7c622032d3f7589f..b7972b908e1c972797f0c66fd4685cf9070ee45d 100644 (file)
@@ -2159,12 +2159,11 @@ get_if_hwaddr(u_char *addr, char *name)
 }
 
 /*
- * get_first_ethernet - return the name of the first ethernet-style
- * interface on this system.
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
  */
-static char first_ether_name[IF_NAMESIZE];
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
 {
        struct if_nameindex *if_ni, *i;
        struct ifreq ifreq;
@@ -2172,33 +2171,31 @@ get_first_ethernet(void)
 
        sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (sock_fd < 0)
-               return NULL;
+               return -1;
 
        if_ni = if_nameindex();
        if (!if_ni) {
                close(sock_fd);
-               return NULL;
+               return -1;
        }
 
-       first_ether_name[0] = 0;
+       ret = -1;
 
        for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
                memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
                strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
                ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
                if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
-                       strlcpy(first_ether_name, i->if_name, sizeof(first_ether_name));
+                       memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
                        break;
                }
+               ret = -1;
        }
 
        if_freenameindex(if_ni);
        close(sock_fd);
 
-       if (!first_ether_name[0])
-               return NULL;
-
-       return first_ether_name;
+       return ret;
 }
 
 /********************************************************************
index 24c3776eb111eaaaf5e69fecf45e4c5a8af12079..e0cd0be32e69dbb458b4cc2e67be89f3e6f75df9 100644 (file)
@@ -218,10 +218,7 @@ static int if6_is_up = 0;  /* IPv6 interface has been marked up */
 
 #endif /* defined(INET6) && defined(SOL2) */
 
-#if defined(INET6) && defined(SOL2)
-static char    first_ether_name[LIFNAMSIZ];    /* Solaris 8 and above */
-#else
-static char    first_ether_name[IFNAMSIZ];     /* Before Solaris 8 */
+#if !defined(INET6) || !defined(SOL2)
 #define MAXIFS         256                     /* Max # of interfaces */
 #endif /* defined(INET6) && defined(SOL2) */
 
@@ -294,13 +291,13 @@ sifppa(fd, ppa)
 
 #if defined(SOL2) && defined(INET6)
 /*
- * get_first_ethernet - returns the first Ethernet interface name found in 
- * the system, or NULL if none is found
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
  *
  * NOTE: This is the lifreq version (Solaris 8 and above)
  */
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
 {
     struct lifnum lifn;
     struct lifconf lifc;
@@ -312,7 +309,7 @@ get_first_ethernet(void)
 
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     if (fd < 0) {
-       return 0;
+       return -1;
     }
 
     /*
@@ -323,7 +320,7 @@ get_first_ethernet(void)
     if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) {
        close(fd);
        error("could not determine number of interfaces: %m");
-       return 0;
+       return -1;
     }
 
     num_ifs = lifn.lifn_count;
@@ -332,7 +329,7 @@ get_first_ethernet(void)
     if (req == NULL) {
        close(fd);
        error("out of memory");
-       return 0;
+       return -1;
     }
 
     /*
@@ -346,7 +343,7 @@ get_first_ethernet(void)
        close(fd);
        free(req);
        error("SIOCGLIFCONF: %m");
-       return 0;
+       return -1;
     }
 
     /*
@@ -363,10 +360,8 @@ get_first_ethernet(void)
        memset(&lifr, 0, sizeof(lifr));
        strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name));
        if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
-           close(fd);
-           free(req);
            error("SIOCGLIFFLAGS: %m");
-           return 0;
+           break;
        }
        fl = lifr.lifr_flags;
 
@@ -374,27 +369,29 @@ get_first_ethernet(void)
                != (IFF_UP | IFF_BROADCAST))
            continue;
 
+       if (get_if_hwaddr(addr, lifr.lifr_name) < 0)
+           continue;
+
        found = 1;
        break;
     }
     free(req);
     close(fd);
 
-    if (found) {
-       strncpy(first_ether_name, lifr.lifr_name, sizeof(first_ether_name));
-       return (char *)first_ether_name;
-    } else
-       return NULL;
+    if (found)
+       return 0;
+    else
+       return -1;
 }
 #else
 /*
- * get_first_ethernet - returns the first Ethernet interface name found in 
- * the system, or NULL if none is found
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
  *
  * NOTE: This is the ifreq version (before Solaris 8). 
  */
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
 {
     struct ifconf ifc;
     struct ifreq *pifreq;
@@ -405,7 +402,7 @@ get_first_ethernet(void)
 
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     if (fd < 0) {
-       return 0;
+       return -1;
     }
 
     /*
@@ -420,7 +417,7 @@ get_first_ethernet(void)
     if (req == NULL) {
        close(fd);
        error("out of memory");
-       return 0;
+       return -1;
     }
 
     /*
@@ -432,7 +429,7 @@ get_first_ethernet(void)
        close(fd);
        free(req);
        error("SIOCGIFCONF: %m");
-       return 0;
+       return -1;
     }
 
     /*
@@ -449,10 +446,8 @@ get_first_ethernet(void)
        memset(&ifr, 0, sizeof(ifr));
        strncpy(ifr.ifr_name, pifreq->ifr_name, sizeof(ifr.ifr_name));
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
-           close(fd);
-           free(req);
            error("SIOCGIFFLAGS: %m");
-           return 0;
+           break;
        }
        fl = ifr.ifr_flags;
 
@@ -460,17 +455,19 @@ get_first_ethernet(void)
                != (IFF_UP | IFF_BROADCAST))
            continue;
 
+       if (get_if_hwaddr(addr, ifr.ifr_name) < 0)
+           continue;
+
        found = 1;
        break;
     }
     free(req);
     close(fd);
 
-    if (found) {
-       strncpy(first_ether_name, ifr.ifr_name, sizeof(first_ether_name));
-       return (char *)first_ether_name;
-    } else
-       return NULL;
+    if (found)
+       return 0;
+    else
+       return -1;
 }
 #endif /* defined(SOL2) && defined(INET6) */