[VLAN] vlans in socket progrmaming

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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.

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux