代码之家  ›  专栏  ›  技术社区  ›  Spook

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

  •  -1
  • Spook  · 技术社区  · 7 年前

    我使用 pcap_open_offline

    1 回复  |  直到 7 年前
        1
  •  0
  •   seladb    7 年前

    假设您需要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;
    }