2017-10-05 118 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; 
}