要获取链接类型(以太网、802.11 等),您可以使用SIOCGIFHWADDR
ioctl。ioctl 返回 中的值之一(在中ARPHRD_
定义net/if_arp.h
)。有关详细信息,请参阅man netdevice(7)。sa_family
struct sockaddr
看一个例子(未测试):
/**
* Get network interface link type
* @param name the network interface name
* @return <0 error code on error, the ARPHRD_ link type otherwise
*/
int get_link_type(const char* name)
{
int rv;
int fd;
struct ifreq ifr;
if (strlen(name) >= IFNAMSIZ) {
fprintf(stderr, "Name '%s' is too long\n", name);
return -ENAMETOOLONG;
}
strncpy(ifr.ifr_name, name, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
return fd;
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
if (rv < 0)
return rv;
{
char *type = "Unknown";
switch (ifr.ifr_hwaddr.sa_family)
{
case ARPHRD_ETHER: type = "Ethernet"; break;
case ARPHRD_IEEE80211: type = "802.11"; break;
/* add more cases here */
}
printf("Link type is: %s\n", type);
}
return ifr.ifr_hwaddr.sa_family;
}
要获得链接速度,您需要SIOCETHTOOL
ioctl,例如此处讨论的内容。