C-如何检查以太网标头是否键入IEEE 802.1q



我将pcap_open_offline用于解析数据包。我该检查以太网标头是否是IEEE 802.1q。我知道我需要检查802.1q标签中的第一个16位等于8100,但我不知道该怎么做。或者,如果您知道另一种方法,我可以尝试。

假设您想要解决方案,这是一个简单的实现:

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;
}

最新更新