github.com/labulakalia/water@v0.0.5-0.20231118024244-f351ca6784b6/waterutil/tap.go (about)

     1  package waterutil
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  type Tagging int
     8  
     9  // Indicating whether/how a MAC frame is tagged. The value is number of bytes taken by tagging.
    10  const (
    11  	NotTagged    Tagging = 0
    12  	Tagged       Tagging = 4
    13  	DoubleTagged Tagging = 8
    14  )
    15  
    16  func MACDestination(macFrame []byte) net.HardwareAddr {
    17  	return net.HardwareAddr(macFrame[:6])
    18  }
    19  
    20  func MACSource(macFrame []byte) net.HardwareAddr {
    21  	return net.HardwareAddr(macFrame[6:12])
    22  }
    23  
    24  func MACTagging(macFrame []byte) Tagging {
    25  	if macFrame[12] == 0x81 && macFrame[13] == 0x00 {
    26  		return Tagged
    27  	} else if macFrame[12] == 0x88 && macFrame[13] == 0xa8 {
    28  		return DoubleTagged
    29  	}
    30  	return NotTagged
    31  }
    32  
    33  func MACEthertype(macFrame []byte) Ethertype {
    34  	ethertypePos := 12 + MACTagging(macFrame)
    35  	return Ethertype{macFrame[ethertypePos], macFrame[ethertypePos+1]}
    36  }
    37  
    38  func MACPayload(macFrame []byte) []byte {
    39  	return macFrame[12+MACTagging(macFrame)+2:]
    40  }
    41  
    42  func IsBroadcast(addr net.HardwareAddr) bool {
    43  	return addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff && addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff
    44  }
    45  
    46  func IsIPv4Multicast(addr net.HardwareAddr) bool {
    47  	return addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e
    48  }