int if_mii_probe(const char *ifname) { uint16_t *data = (uint16_t *) (&ifr.ifr_data); int phy_id; int fd = socket(AF_INET, SOCK_DGRAM, 0); int status = 0; if (fd < 0) return -1; memset(&ifr, 0, sizeof (struct ifreq)); strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(fd, SIOCGMIIPHY, &ifr) < 0) { close(fd); return -1; } /* check if the driver reports BMSR using the MII interface, as we * will need this and we already know that some don't support it. */ phy_id = data[0]; /* save it in case it is overwritten */ data[1] = 1; if (ioctl(fd, SIOCGMIIREG, &ifr) < 0) { close(fd); return -1; } data[0] = phy_id; /* Dump the MII transceiver */ status = if_mii_status(fd); close(fd); return status; }
static int if_mii_probe(const char *ifname) { uint16_t *data = (uint16_t *) (&ifr.ifr_data); int phy_id; int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); int status = 0; if (fd < 0) return -1; #ifndef _HAVE_SOCK_CLOEXEC_ if (set_sock_flags(fd, F_SETFD, FD_CLOEXEC)) log_message(LOG_INFO, "Unable to set CLOEXEC on mii_probe socket - %s (%d)", strerror(errno), errno); #endif memset(&ifr, 0, sizeof (struct ifreq)); strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(fd, SIOCGMIIPHY, &ifr) < 0) { close(fd); return -1; } /* check if the driver reports BMSR using the MII interface, as we * will need this and we already know that some don't support it. */ phy_id = data[0]; /* save it in case it is overwritten */ data[1] = 1; if (ioctl(fd, SIOCGMIIREG, &ifr) < 0) { close(fd); return -1; } data[0] = phy_id; /* Dump the MII transceiver */ status = if_mii_status(fd); close(fd); return status; }