gpt4 book ai didi

c - 我试图通过 ioctl 从驱动程序获得的以太网速度是恒定的,比如 57872Mbps,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 04:56:27 24 4
gpt4 key购买 nike

我正在尝试读取我设备上的以太网速度,尽管我正在连接 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com