ioctl 使路由消失

ioctl make routes disappear

我对 ioctl 有疑问(我认为)。

该软件是一个 debian 包,它在机器的引导过程中安装,然后立即启动。该软件通过使用 /etc/network/interfaces 设置网络。 IP 和网络掩码被写入接口配置文件,路由通过接口配置文件中的 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 启动设置的界面。 (所以每个接口首先在接口配置文件中设置,然后通过ifup启动,所以一个接口被配置然后启动,然后下一个接口被配置并启动...)

现在问题出现了,当我使用一个使用 ioctl 来读取接口信息的函数(命令行)时,调用之后,路由 table 将是空的(或者更确切地说,手动 [via up 命令在界面配置文件中]添加的路由将消失)。

该函数只从被查询接口套接字上打开的文件描述符中读取数据,不会在那里设置数据。

完全出乎意料的是在删除路由后,我手动(从命令行)在接口上调用 ifdown 和 ifup 并使用 ioctl 调用函数(在启动时会删除配置的路由)不会再删除路由了。

任何人都可以指出这里可能存在的问题吗? (接口 eth0 是一个服务接口,例如访问网站的接口。接口 prp1 是一个虚拟接口,它通过 PRP 包装了 2 个真实接口 [eth1 和 eth2 未配置]。还有一些 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 }'", returnValue);

    // Get the total number of received packets.
    // Include the number of faulty and dropped packets.
    nicParam.receivedPackets.numPackets = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);
    nicParam.receivedPackets.numErrors = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);
    nicParam.receivedPackets.numDroppedPackets = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);

    // Get the total number of transmitted bytes.
    nicParam.transmittedPackets.numBytes = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);

    // Get the total number of transmitted packets.
    // Include the number of faulty and dropped packets.
    nicParam.transmittedPackets.numPackets = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);
    nicParam.transmittedPackets.numErrors = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", returnValue);
    nicParam.transmittedPackets.numDroppedPackets = OsHelper::executeCommandInt(
        procNetDev + " | awk '{print }'", 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;
}

未正确检索到广播,它确实调用了一些其他函数...别介意!

所以我发现了我的问题... 这是一个有点尴尬的错误。

在函数中你会发现 "ioctl(fd, SIOCSIFBRDADDR, &ifr);" ,当在一行中看到没有所有其他调用时,显然 "Sets" 一个参数而不是检索它(广播地址 siocS ...而不是 siocG...)。而且在接口上设置一些东西,显然会导致路由被删除,因为换了网卡,路由就会出错

感谢阅读。