用pcap实例编程

用pcap实例编程,c,endianness,pcap,libpcap,C,Endianness,Pcap,Libpcap,我正在从中学习libpcap,我遇到了以下代码的问题: struct sniff_ethernet { u_char ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */ u_char ether_shost[ETHER_ADDR_LEN]; /* Source host address */ u_short ether_type; /* IP? ARP? RARP? etc */ }; 我得到了交换字

我正在从中学习libpcap,我遇到了以下代码的问题:

struct sniff_ethernet {
    u_char ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */
    u_char ether_shost[ETHER_ADDR_LEN]; /* Source host address */
    u_short ether_type; /* IP? ARP? RARP? etc */
};

我得到了交换字节的以太类型值。我认为原因是我使用的是x86_64 little endian机器,其中LSB位于最低地址,在数据包字节流中,ether_类型的MSB位于LSB之前。问题是:示例代码是仅在big-endian机器上工作还是我遗漏了什么?

处的示例代码没有查看以太网类型,因此它可以在运行它的机器的字节顺序上工作。它依靠捕获筛选器(“端口23”)不捕获非IPv4流量

例如,当您在代码中使用其值时,必须在
ether\u type
字段上使用
ntohs()

我不知道
ntohs()
,它解决了我的问题,检测802.1Q标记帧,谢谢!
const struct sniff_ethernet *ethernet; /* The ethernet header */
ethernet = (struct sniff_ethernet*)(packet);