123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package layers
- import (
- "encoding/binary"
- "errors"
- "github.com/google/gopacket"
- )
- type MPLS struct {
- BaseLayer
- Label uint32
- TrafficClass uint8
- StackBottom bool
- TTL uint8
- }
- func (m *MPLS) LayerType() gopacket.LayerType { return LayerTypeMPLS }
- type ProtocolGuessingDecoder struct{}
- func (ProtocolGuessingDecoder) Decode(data []byte, p gopacket.PacketBuilder) error {
- switch data[0] {
-
- case 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f:
- return decodeIPv4(data, p)
-
- case 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f:
- return decodeIPv6(data, p)
- }
- return errors.New("Unable to guess protocol of packet data")
- }
- var MPLSPayloadDecoder gopacket.Decoder = ProtocolGuessingDecoder{}
- func decodeMPLS(data []byte, p gopacket.PacketBuilder) error {
- decoded := binary.BigEndian.Uint32(data[:4])
- mpls := &MPLS{
- Label: decoded >> 12,
- TrafficClass: uint8(decoded>>9) & 0x7,
- StackBottom: decoded&0x100 != 0,
- TTL: uint8(decoded),
- BaseLayer: BaseLayer{data[:4], data[4:]},
- }
- p.AddLayer(mpls)
- if mpls.StackBottom {
- return p.NextDecoder(MPLSPayloadDecoder)
- }
- return p.NextDecoder(gopacket.DecodeFunc(decodeMPLS))
- }
- func (m *MPLS) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
- bytes, err := b.PrependBytes(4)
- if err != nil {
- return err
- }
- encoded := m.Label << 12
- encoded |= uint32(m.TrafficClass) << 9
- encoded |= uint32(m.TTL)
- if m.StackBottom {
- encoded |= 0x100
- }
- binary.BigEndian.PutUint32(bytes, encoded)
- return nil
- }
|