我正在尝试读取我的设备上的以太网的速度,虽然我正在连接100mbps电缆,但它的读取值是相同的恒定值57872或一些垃圾值或0总是。。。。
我做的程序逻辑如下,它不会包含所有的代码,只是它的一部分:
void get_link_config_res(void)
{
int i=0;
FILE *fp;
int ret_val;
char in_buf[200];
char in_buf1[64];
unsigned char interface_name[10];
int sock_id;
struct ifreq ir;
struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
struct sockaddr_in *nmsk = (void *) &ir.ifr_ifru.ifru_netmask;
//struct sockaddr_in *brdcast = (void *) &ir.ifr_ifru.ifru_broadaddr;
static int_net_cfg_t int_eth,int_wifi;
int_netcfg_array_t temp_netcfg,wifi_netcfg;
static __u8 mac_address[6];
char *temp_buf;
struct ethtool_cmd edata;
if(verify_host_link_status(ETH_0) == SET_FLAG)
{
/* get Ethernet link status */
memset(&ir, 0, sizeof(ir));
memset(in_buf, 0, sizeof(in_buf));
memset(in_buf1, 0, sizeof(in_buf1));
memset(mac_address, 0, sizeof(mac_address));
memset(interface_name, 0, sizeof(interface_name));
strcpy(ir.ifr_name, ETHERNET);
/* socket creation (IPV4) */
sock_id = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_id < 0)
{
SPX_DEBUG_3("cannot open socket \n");
}
strcpy(ir.ifr_ifrn.ifrn_name, ir.ifr_name);
ir.ifr_data = (char *)&edata;
//printf("Ethernet speed 1=",ir.ifr_data);
ret_val = ioctl(sock_id, SIOCGIFHWADDR, &ir);
//printf("retval of ioctl is = %d\n",ret_val);
memcpy(mac_address, &ir.ifr_ifru.ifru_hwaddr.sa_data, 6);
for(i=0; i<6; i++)
{
temp_netcfg.hwaddr[i] = mac_address[i];
}
mac2string(temp_netcfg.hwaddr, &in_buf1);
sprintf(in_buf,"echo mac_address=%s > %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
edata.cmd = ETHTOOL_GSET;
/*eth speed*/
ret_val = ioctl(sock_id, SIOCETHTOOL, &ir);
memset(in_buf, 0, sizeof(in_buf));
//printf("Ethernet speed=%d",edata.speed);
switch (edata.speed)
{
case SPEED_10:
sprintf(in_buf,"echo speed=10Mbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
//printf("10Mbps\n");
break;
case SPEED_100:
//printf("100Mbps\n");
sprintf(in_buf,"echo speed=100Mbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
case SPEED_1000:
//printf("1Gbps\n");
sprintf(in_buf,"echo speed=1Gbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
case SPEED_10000:
//printf("10Gbps\n");
sprintf(in_buf,"echo speed=10Gbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
default:
//printf("Speed returned is ____%d\n", edata.speed);
sprintf(in_buf,"echo speed=%dMbps >> %s",edata.speed, ETHERNET_PARAMS_FILE);
system(in_buf);
break;
}
memset(in_buf, 0, sizeof(in_buf));
/* get the IP address */
ret_val = ioctl(sock_id, SIOCGIFADDR, &ir);
int_eth.ipaddr.s_addr = sin->sin_addr.s_addr;
/*To get Ip address in ASCII format*/
temp_buf = inet_ntoa(int_eth.ipaddr);
strcpy (in_buf, temp_buf);
sprintf(in_buf,"echo ip_address=%s >> %s",temp_buf, ETHERNET_PARAMS_FILE);
system(in_buf);
memset(in_buf, 0, sizeof(in_buf));
/*subnet mask*/
ret_val = ioctl(sock_id, SIOCGIFNETMASK, &ir);
int_eth.netmask.s_addr = nmsk->sin_addr.s_addr;
/*To get subnet mask in ASCII format*/
temp_buf = inet_ntoa(int_eth.netmask);
strcpy (in_buf, temp_buf);
sprintf(in_buf,"echo subnet_mask=%s >> %s",temp_buf, ETHERNET_PARAMS_FILE);
system(in_buf);
close(sock_id);
}
else
{
/*Host is not connected, to the Ethernet interface.*/
//printf("Ethernet Interface is not connected...\n");
memset(in_buf1, 0, sizeof(in_buf1));
strcpy(in_buf1, "NONE");
sprintf(in_buf,"echo mac_address=%s > %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo speed=%s >> %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo ip_address=%s >> %s",in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo subnet_mask=%s >> %s",in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
//status = CLEAR_FLAG;
}
}
请帮我解决这个。。。。。提前谢谢。。。。。
最佳答案
ret_val = ioctl(sock_id, SIOCETHTOOL, &ir);
你至少已经走到一半了。您似乎认识到了这样一个事实,即
ioctl
可能会失败,但您没有执行并实际检查返回值。鉴于您的文本指示值总是错误的(“57872或一些垃圾值或0总是”),我敢说您一定会得到一个一致的错误代码,如
EOPNOTSUPP
。修改代码以先检查,然后再次运行。
关于c - 我试图通过ioctl从驱动程序获得以太网速度恒定为57872Mbps,为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7190796/