- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对 ioctl 有疑问(我认为)。
该软件是一个 debian 包,它在机器的引导过程中安装,然后立即启动。该软件通过使用/etc/network/interfaces 设置网络。 IP 和网络掩码被写入接口(interface)配置文件,路由通过接口(interface)配置文件中的 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 启动设置的界面。 (所以每个接口(interface)首先在接口(interface)配置文件中设置,然后通过 ifup 启动,所以一个接口(interface)被配置然后启动,然后下一个接口(interface)被配置并启动......)
现在,当我使用一个使用 ioctl 的函数(命令行)读取接口(interface)信息时,问题就出现了,在该调用之后,路由表将为空(或者更确切地说,手动添加 [通过接口(interface)配置文件中的 up 命令]路线将消失)。
该函数只从被查询接口(interface)的套接字上打开的文件描述符中读取数据,不会在那里设置任何数据。
完全出乎意料的是在删除路由后,我手动(从命令行)在接口(interface)上调用 ifdown 和 ifup 并且使用 ioctl 调用函数(在启动时会删除配置的路由)不会删除路由了。
任何人都可以指出这里可能存在的问题吗?(接口(interface) eth0 是一个服务接口(interface),例如访问网站的接口(interface)。接口(interface) prp1 是一个虚拟接口(interface),它通过 PRP 包装了 2 个真实接口(interface) [eth1 和 eth2 未配置]。还有一些 SCL 设置正在进行,里面也使用 ioctl 进行接口(interface)查询。我确信,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;
}
广播没有被正确检索,它是一些其他函数调用真的......别介意!
最佳答案
所以我发现了我的问题...这是一个有点尴尬的错误。
在函数中你会发现“ioctl(fd, SIOCSIFBRDADDR, &ifr);”其中,当在没有所有其他调用的情况下在一行中看到时,显然“设置”了一个参数而不是检索它(广播地址 siocS... 而不是 siocG...)。而在接口(interface)上设置一些东西,显然会导致路由被删除,因为网卡改变了,路由就会出错。
感谢阅读。
关于linux - ioctl 使路由消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309558/
在一个简单的 MFC 应用程序中,我需要有一个不断轮询 ioctl 事件的工作线程。起初,我试图在 while 循环中使用非重叠 ioctl 来实现这一点。我的想法是,如果 ioctl 没有立即完成
据我所知,ioctl 数字由驱动程序明确定义并在内核中注册。 我正在使用 python 中的一些代码来查询操纵杆状态。我已阅读this doc about joystick api , this do
您好,我收到此错误:ioctl:设备的 ioctl 不合适如下所示的 ioctl() 调用。 fd = open(mount, O_RDONLY); destid = ioctl(fd, TRACEF
我正在编写用作伪驱动程序的 Linux 内核模块 (LKM) - 我无法弄清楚如何在 LKM 之间进行 IOCTL 调用 (wait.c)和用户级程序 (user.c)。 设备驱动程序的魔数(Magi
以 resetting a serial port 为例在 Linux 中,我想翻译以下片段 fd = open(filename, O_WRONLY); ioctl(fd, USBDEVFS_RES
嘿,我在尝试从 python 调用 ioctl linux 系统调用时遇到问题。 在 C 应用程序中运行以下行,我设法获取给定 linux 命名空间文件描述符的父文件描述符。 #define NS_G
struct file_operations中的unlocked_ioctl的签名是 long (*unlocked_ioctl) (struct file *, unsigned int, unsi
if((err = ioctl(fd, IOC_CARD_LOCK, &lock)) < 0) { printf("ioctl failed and returned errno %d \n
抱歉,如果这是一个菜鸟问题,但我正在为游戏开发一个软件“附加”。我通过驱动程序执行此操作只是因为反作弊不支持环 0 检测。我还没有看到太多关于如何使用 IOCTL 的信息,我想知道您是否可以发送自定义
我正在尝试为允许“现金抽屉”附件的销售点系统编写代码。打开现金抽屉的手册中提供了代码(使用 IOCTL 在 C++ 中)。由于我在 C# .NET 中编码,是否可以在 C# 中执行类似的操作,或者我是
我编写了一个 IOCTL 驱动程序和一个相应的 ioctl 应用程序,其中包含一个包含命令的头文件。 #include #include #include #include #include
我正在使用 ioctl() 函数调用来获取管道端可用数据的大小,并根据该大小分配内存。 为此,我将此代码段编写为 if((read(mg_in, &byte, 1)) == 1)
最近我在c中遇到了ioctl函数,在探索时我不明白为什么我们要为这个特定代码传递标准输入文件描述符以及它的作用是什么?。 #include #include #include #include
我正在做一个nvme-cli的测试工具(用c写的,可以在linux上运行)。 出于 SSD 验证目的,我实际上是在寻找自定义命令(例如,I/O 命令,写入然后读取相同的内容,最后比较两个数据是否相同)
拿一个串口。串行端口可以调用带有TIOCMIWAIT 的ioctl 来等待信号变化。但是,如果串行端口以非阻塞方式打开,如何使用 select、poll 或 epoll 之类的东西来中断事件-当像CT
我有 #define IOCTL_ALLOC_MSG _IO(MAJOR_NUM, 0) #define IOCTL_DEALLOC_MSG _IO(MAJOR_NUM, 1) 在头文件中。 在我写的
我对 ioctl 有疑问(我认为)。 该软件是一个 debian 包,它在机器的引导过程中安装,然后立即启动。该软件通过使用/etc/network/interfaces 设置网络。 IP 和网络掩码
我正在 Android 应用程序中处理一些路由功能,并且需要访问 ioctl。由于使用 ioctls 的应用程序需要 root 权限才能运行,我能够调用它们的唯一方法是链接一个单独的可执行文件并使用
引用这个链接http://stackoverflow.com/questions/8922102/adding-new-ioctls-into-kernel-number-range 我开始知道编码是
我使用/proc/diskstats 来获取读取和写入的扇区数。我想将这个数字转换为字节,所以我寻找扇区大小。我用了How to find floppy\ CD sector size in Linu
我是一名优秀的程序员,十分优秀!