使用pcap编程编程

时间:2013-12-14 14:49:10

标签: c endianness pcap libpcap

我正在使用这个http://www.tcpdump.org/pcap.html的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 */
};

...

const struct sniff_ethernet *ethernet; /* The ethernet header */
ethernet = (struct sniff_ethernet*)(packet);

我通过交换字节获取ether_type值。我认为原因是我使用的是x86_64 little-endian机器,其中LSB处于最低地址,而在数据包字节流中,ether_type MSB在LSB之前。问题是:是仅在big-endian机器上运行的示例代码还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

http://www.tcpdump.org/pcap.html处的示例代码并未查看以太网类型,因此无论运行它的机器的字节顺序如何,它都能正常工作。它依靠捕获过滤器(“端口23”)来捕获非IPv4流量。

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

相关问题