2013-12-14 27 views
1

我与libpcap的学习从这个http://www.tcpdump.org/pcap.html,我已经用这个代码碰到的问题:编程使用PCAP例如

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之前。问题是:是仅在大端机器上工作的示例代码,还是我错过了一些东西?

回答

0

http://www.tcpdump.org/pcap.html上的示例代码没有考虑以太网类型,所以它的工作原理与它所运行的机器的字节顺序无关。它依靠捕获过滤器(“端口23”)不捕获非IPv4流量。

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

+0

我不知道'ntohs()'它解决了我的问题,检测802.1Q标记的帧,所以谢谢! –