以编程方式获取链接速度? [英] Get link speed programmatically?
问题描述
我正在编写一个报告本地计算机上网络设备属性的应用程序.我需要mac地址,mtu,链接速度和其他一些信息.我为此使用udev.我已经弄清楚了如何获取mac地址和mtu,但还没有找到如何提高链接速度.我可以从终端上使用ethtool来获取它,但是我需要一种以编程方式获取它的方法.
I am writing an application that reports attributes of network devices on the local machine. I need the mac address, mtu, link speed and a few others. I'm using udev for this. I've already figured out how to get the mac address and mtu, but not how to get the link speed. I can get it with ethtool from the terminal, but I need a way to get it programmatically.
有人知道我如何使用udev或其他库获取链接速度属性吗?
Does anyone know how I can get the link speed attribute with udev or another library?
推荐答案
您需要使用SIOCETHTOOL
ioctl()调用.有一个不错的 ioctl/SIOCETHTOOL的介绍在LinuxJournal上调用,下面的代码(并非旨在作为C良好实践的例子!)应向您展示如何使用它来提高速度.
You need to use the SIOCETHTOOL
ioctl() call. There's a nice introduction to ioctl/SIOCETHTOOL call on LinuxJournal, and the code below (which is not intended to be an example of good C practices!) should show you how to use it to get the speed.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/ethtool.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
int sock;
struct ifreq ifr;
struct ethtool_cmd edata;
int rc;
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0) {
perror("socket");
exit(1);
}
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
ifr.ifr_data = &edata;
edata.cmd = ETHTOOL_GSET;
rc = ioctl(sock, SIOCETHTOOL, &ifr);
if (rc < 0) {
perror("ioctl");
exit(1);
}
switch (ethtool_cmd_speed(&edata)) {
case SPEED_10: printf("10Mbps\n"); break;
case SPEED_100: printf("100Mbps\n"); break;
case SPEED_1000: printf("1Gbps\n"); break;
case SPEED_2500: printf("2.5Gbps\n"); break;
case SPEED_10000: printf("10Gbps\n"); break;
default: printf("Speed returned is %d\n", edata.speed);
}
return (0);
}
这篇关于以编程方式获取链接速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!