2017-10-05 97 views
0

我使用pcap_open_offline來解析數據包。我該如何檢查以太網報頭是否輸入IEEE 802.1Q。我知道我需要檢查802.1Q標記中的前16位是否等於8100,但我不知道該怎麼做。或者如果你知道另一種方式,我可以嘗試它。如何檢查以太網報頭是否輸入IEEE 802.1Q?

+0

閱讀[問],提供[MCVE]和所有必需的信息。 – Olaf

+0

取決於您使用的語言。假設它是C,值0x8100是以字節12,13的數據包(從0開始)。字節[12]是0x81,字節[13]是0 - 因爲這些東西通常以大端格式描述。 – ddbug

回答

0

假設你想在C解決方案,下面是一個簡單的實現:

struct ether_header { 
    /* destination MAC */ 
    uint8_t dst_mac[6]; 
    /* source MAC */ 
    uint8_t src_mac[6]; 
    /* EtherType */ 
    uint16_t ether_type; 
}; 

#define ETHERTYPE_VLAN 0x8100 

/* this method gets the packet data read from pcap file and returns 1 if ether type is 802.1Q, 0 otherwise */ 
int is_IEEE_802_1Q(const uint8_t* packet_data) { 
    /* cast ethernet header */ 
    ether_header* eth_header = (ether_header*)packet_data; 

    /* assuming big endian as most pcap files are in big endian */ 
    if (eth_header->ether_type == ETHERTYPE_VLAN) { 
     return 1; 
    } 

    return 0; 
}