Re: vlans in socket progrmaming

[email protected]
Newsgroups gmane.linux.drivers.vlan
Message-ID <[email protected]>
> i'm searching for a code-snippet/faq/tutorial showing basic vlan 
> thingsin socket programming, especially how to set and get the vlan-
> id in network packets.

I extract VLAN IDs like this:

    struct ip* ip;

    struct sockaddr_ll sll;
    int from_len;
    tpkt_t pkt;

    uint8_t* pt;
    size_t len;

    vid_t vid;
    int pr;
    
    from_len = sizeof(sll);
    pkt.len = recvfrom(sd, pkt.data, sizeof(pkt.data), 0,
                       (struct sockaddr*)&sll, &from_len);
    if (pkt.len < 0) {
        return -1;
    }
    pt = pkt.data;
    len = pkt.len;

    // Extract VLAN ID if frame is tagged.
    switch (htons(((struct ethhdr*)pt)->h_proto)) {
    case ETHERTYPE_IP: // No tag
        pt += ETHER_HDR_LEN;  // Skip over Ethernet header
        len -= ETHER_HDR_LEN;
        vid = 0;
        break;
            
    case 0x8100: // VLAN tagged data
        pt += ETHER_HDR_LEN;  // Skip over Ethernet header
        len -= ETHER_HDR_LEN;
           
        vid = *((uint16_t*)pt) & 0x0fff;
        pr = *((uint16_t*)pt) >> 13;
        igmp_log(LOG_INFO,"Found VLAN tag. ID:%d, pr:%d\n", vid, pr);
            
        pt += 4;  // Skip TCI and real length/type field
        len -= 4;
        break;
            
    default:  // Not IP (w/ or w/o tag)
        return 0;
    }

HTH.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.