简体   繁体   English

如何检查以太网标头是否为 IEEE 802.1Q 类型?

[英]How to check if ethernet header is type IEEE 802.1Q?

I use pcap_open_offline for parsing packets.我使用pcap_open_offline来解析数据包。 I what to check if ethernet header is type IEEE 802.1Q.我要检查以太网标头是否为 IEEE 802.1Q 类型。 I know I need to check if first 16 bits in 802.1Q tag are equal to 8100 but I do not know how to do it.我知道我需要检查 802.1Q 标签中的前 16 位是否等于 8100,但我不知道该怎么做。 Or if you know another way I can try it.或者如果你知道另一种方法,我可以试试。

Assume you want a solution in C, here is a simple implementation:假设你想要一个 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM