我对ioctl有意见(我想)。
该软件是一个debian软件包,安装在计算机的启动过程中,然后立即启动。该软件通过使用/etc/网络/接口建立网络。IP和Net掩码被写入接口配置文件,路由通过接口配置文件中的up命令添加。
# CONFIG_MARKER eth0
auto eth0
iface eth0 inet static
address 192.168.0.237
netmask 255.255.255.0
up route add -net 192.168.0.0 netmask 255.255.255.0 gateway 192.168.0.126 dev eth0
down route del -net 192.168.0.0 netmask 255.255.255.0 gateway 192.168.0.126 dev eth0
up route add default gateway 192.168.0.126 dev eth0
down route del default gateway 192.168.0.126 dev eth0
# CONFIG_MARKER eth0
# CONFIG_MARKER prp1
auto prp1
iface prp1 inet static
address 192.168.20.237
netmask 255.255.255.0
up route add -net 192.168.20.0 netmask 255.255.255.0 gateway 192.168.20.1 dev prp1
down route del -net 192.168.20.0 netmask 255.255.255.0 gateway 192.168.20.1 dev prp1在创建配置文件之后,软件使用ifdown和ifup启动设置的接口。(因此,每个接口首先在接口configt文件中设置,然后通过ifup启动,因此一个接口被配置,然后启动,然后下一个接口被配置并启动.)
现在,当我使用使用ioctl读取接口信息的函数(命令行)时,问题就出现了,在调用之后,路由表将为空(或者更确切地说,接口配置文件中添加的手动通道up命令将消失)。
该函数只从查询接口的套接字上读取打开的文件描述符中的数据,然后不设置任何数据。
完全出乎意料的是,在删除路由之后,我手动(从命令行)调用接口上的ifdown和ifup,对带有ioctl的函数的调用(在启动时将删除配置的路由)将不再删除路由。
有人能告诉我这里有什么问题吗?(例如,接口eth0是一个服务接口,其中包含对网站的调用。接口prp1是一个虚拟接口,它封装了两个真实的接口,eth1和eth2在PRP上没有配置。还有一些SCL设置正在进行,内部也使用ioctl进行接口查询。我确信,所描述的行为不应归咎于IEC61850通信设置。)
编辑
根据请求,导致问题的函数的代码:
NetworkInterface::NicParameter NetworkHelper::getNicParameter(
const std::string& ifaceName) {
NetworkInterface::NicParameter nicParam;
nicParam.ifaceName = ifaceName;
nicParam.opState = getOperationalState(ifaceName);
nicParam.opMode = getOperationalMode(ifaceName);
nicParam.linkStatus = getLinkStatus(ifaceName);
nicParam.speed = getSpeed(ifaceName);
// Get the IP address of the current interface.
int fd = socket(AF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
// Copy the interface name in the ifreq structure.
strncpy(ifr.ifr_name, ifaceName.c_str(), IFNAMSIZ - 1);
// get the ip address.
ioctl(fd, SIOCGIFADDR, &ifr);
nicParam.ipAddress
= inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
// Get the netmask of the current interface.
ioctl(fd, SIOCGIFNETMASK, &ifr);
nicParam.netmask
= inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
// Get the gateway address of the current interface.
ioctl(fd, SIOCGIFDSTADDR, &ifr);
nicParam.gateway = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
// Get the broadcast address of the current interface.
ioctl(fd, SIOCSIFBRDADDR, &ifr);
nicParam.broadcastAddress
= inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
// Get the broadcast address of the current interface.
struct ifreq req;
strcpy(req.ifr_name, ifaceName.c_str());
char macAddress[18]; // 17 characters + null terminator
// example: 01:02:03:04:05:06
if (ioctl(fd, SIOCGIFHWADDR, &req) != -1) {
uint8_t *mac = reinterpret_cast<uint8_t *>(
req.ifr_ifru.ifru_hwaddr.sa_data);
sprintf(macAddress, "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
nicParam.macAddress = std::string(macAddress);
close(fd);
// Get the total number of received bytes.
int returnValue;
std::string procNetDev = "cat /proc/net/dev | grep " + ifaceName;
nicParam.receivedPackets.numBytes = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $2}'", returnValue);
// Get the total number of received packets.
// Include the number of faulty and dropped packets.
nicParam.receivedPackets.numPackets = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $3}'", returnValue);
nicParam.receivedPackets.numErrors = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $4}'", returnValue);
nicParam.receivedPackets.numDroppedPackets = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $5}'", returnValue);
// Get the total number of transmitted bytes.
nicParam.transmittedPackets.numBytes = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $10}'", returnValue);
// Get the total number of transmitted packets.
// Include the number of faulty and dropped packets.
nicParam.transmittedPackets.numPackets = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $11}'", returnValue);
nicParam.transmittedPackets.numErrors = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $12}'", returnValue);
nicParam.transmittedPackets.numDroppedPackets = OsHelper::executeCommandInt(
procNetDev + " | awk '{print $13}'", returnValue);
// Check if DHCP is enabled or not.
returnValue = 0;
OsHelper::executeCommand("cat /etc/network/interfaces | grep -v '#' | grep "
+ ifaceName + " | grep dhcp", returnValue);
if (returnValue == 0)
nicParam.dhcpState = "enabled";
else
nicParam.dhcpState = "disabled";
return nicParam;
}广播没有被正确的检索,它的其他一些函数调用真的.别介意!
发布于 2016-05-23 10:10:28
所以我发现我的问题..。这是个有点尴尬的错误。
在这个函数中,您会发现"ioctl(fd,SIOCSIFBRDADDR,&ifr)“,当在一行中看到时,如果没有所有其他调用,就会清楚地”设置“一个参数,而不是检索它(广播地址siocS.而不是siocG)。并且在接口线上设置一些东西会导致路由明显被删除,因为NIC改变了,这些路由将是错误的。
阅读的时候。
https://stackoverflow.com/questions/37309558
复制相似问题