hoz*_*hoz 5 c++ linux networking can-bus
我正在使用getifaddrsLinux 函数来获取有关运行 Debian Jessie 内核 3.16.0 的 Linux 计算机上的网络接口的信息。
我想知道的信息之一是网络统计信息(丢弃的数据包、发送的数据包等),正如 getifaddrs 的手册页所述,它包含在when设置rtnl_link_stats为 所指向的结构中。ifaddrs::ifa_addr::ifa_dataifaddrs::if_addr::sa_familyAF_PACKET
这适用于以太网接口,但不适用于机器上的 CAN 接口,因为ifaddrs::ifa_addr为 NULL,因此AF_PACKET永远不会为任何 CAN 接口返回。
以下 C++ 代码...
#include <ifaddrs.h>
#include <linux/if_link.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <cstdio>
#include <cstdlib>
int main() {
ifaddrs *addrs;
if (getifaddrs(&addrs) == -1) {
perror("getifaddrs");
return EXIT_FAILURE;
}
printf("AF_PACKET: %d\n\n", (int)AF_PACKET);
for (auto addr = addrs; addr != NULL; addr = addr->ifa_next) {
if (addr->ifa_addr != nullptr) {
printf("%s: family: %d\n", addr->ifa_name, (int)addr->ifa_addr->sa_family);
} else {
printf("%s: family: none\n", addr->ifa_name);
}
}
freeifaddrs(addrs);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
...打印出来
AF_PACKET: 17
lo: family: 17
eth0: family: 17
eth1: family: 17
can0: family: none
can1: family: none
lo: family: 2
eth1: family: 2
lo: family: 10
eth1: family: 10
Run Code Online (Sandbox Code Playgroud)
这意味着这两个 CAN 接口都没有任何系列集。
不过,该ifconfig命令在显示信息方面没有问题:
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:28481588 errors:0 dropped:8729866 overruns:0 frame:0
TX packets:8168599 errors:2292404 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:211108099 (201.3 MiB) TX bytes:64828340 (61.8 MiB)
Interrupt:17
can1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:17
Run Code Online (Sandbox Code Playgroud)
(请注意,我仅包含 CAN 接口的输出,未包含其余部分)
我究竟做错了什么?检索 CAN 接口数据包统计信息的正确方法是什么?
小智 0
使用“ifconfig”命令中的“address_family”选项;检查你的“man ifconfig”页面,这是我的相关文本(我的系统上没有 CAN intfs):
...
address_family
Specify the address family which affects interpretation of the remaining parameters. Since an interface
can receive transmissions in differing protocols with different naming schemes, specifying the address
family is recommended. The address or protocol families currently supported are ``inet'', ``inet6'', and
``link''. The default is ``inet''. ``ether'' and ``lladdr'' are synonyms for ``link''.
Run Code Online (Sandbox Code Playgroud)