]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
Make a consistency in pppoe options
[ppp.git] / pppd / sys-linux.c
index f38210a4720556cf53784008850dcc4858e4fb67..587242bb1ed7a2cb0107061a2109e17ccdde1dd1 100644 (file)
@@ -467,6 +467,13 @@ int generic_establish_ppp (int fd)
     if (new_style_driver) {
        int flags;
 
+       /* If a ppp_fd is already open, close it first */
+       if (ppp_fd >= 0) {
+           close(ppp_fd);
+           remove_fd(ppp_fd);
+           ppp_fd = -1;
+       }
+
        /* Open an instance of /dev/ppp and connect the channel to it */
        if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
            error("Couldn't get channel number: %m");
@@ -625,7 +632,7 @@ void generic_disestablish_ppp(int dev_fd)
  * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
  * Assumes new_style_driver.
  */
-static int make_ppp_unit()
+static int make_ppp_unit(void)
 {
        int x, flags;
 
@@ -1377,9 +1384,7 @@ int set_filters(struct bpf_program *pass, struct bpf_program *active)
  * get_idle_time - return how long the link has been idle.
  */
 int
-get_idle_time(u, ip)
-    int u;
-    struct ppp_idle *ip;
+get_idle_time(int u, struct ppp_idle *ip)
 {
     return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
 }
@@ -1389,9 +1394,7 @@ get_idle_time(u, ip)
  * get_ppp_stats - return statistics for the link.
  */
 int
-get_ppp_stats(u, stats)
-    int u;
-    struct pppd_stats *stats;
+get_ppp_stats(int u, struct pppd_stats *stats)
 {
     struct ifpppstatsreq req;
 
@@ -2091,7 +2094,7 @@ get_if_hwaddr(u_char *addr, char *name)
 
        sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (sock_fd < 0)
-               return 0;
+               return -1;
        memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
        strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
        ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
@@ -2105,10 +2108,43 @@ get_if_hwaddr(u_char *addr, char *name)
  * get_first_ethernet - return the name of the first ethernet-style
  * interface on this system.
  */
+static char first_ether_name[IF_NAMESIZE];
 char *
-get_first_ethernet()
+get_first_ethernet(void)
 {
-       return "eth0";
+       struct if_nameindex *if_ni, *i;
+       struct ifreq ifreq;
+       int ret, sock_fd;
+
+       sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (sock_fd < 0)
+               return NULL;
+
+       if_ni = if_nameindex();
+       if (!if_ni) {
+               close(sock_fd);
+               return NULL;
+       }
+
+       first_ether_name[0] = 0;
+
+       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));
+                       break;
+               }
+       }
+
+       if_freenameindex(if_ni);
+       close(sock_fd);
+
+       if (!first_ether_name[0])
+               return NULL;
+
+       return first_ether_name;
 }
 
 /********************************************************************
@@ -2826,11 +2862,7 @@ int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
  * to the uid given.  Assumes slave_name points to >= 16 bytes of space.
  */
 int
-get_pty(master_fdp, slave_fdp, slave_name, uid)
-    int *master_fdp;
-    int *slave_fdp;
-    char *slave_name;
-    int uid;
+get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid)
 {
     int i, mfd, sfd = -1;
     char pty_name[16];
@@ -2956,10 +2988,7 @@ open_ppp_loopback(void)
  */
 
 int
-sifnpmode(u, proto, mode)
-    int u;
-    int proto;
-    enum NPmode mode;
+sifnpmode(int u, int proto, enum NPmode mode)
 {
     struct npioctl npi;
 
@@ -3070,7 +3099,7 @@ int cipxfaddr (int unit)
  * Use the hostname as part of the random number seed.
  */
 int
-get_host_seed()
+get_host_seed(void)
 {
     int h;
     char *p = hostname;