2013-12-14 24 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標記的幀,所以謝謝! –