Skip to content
Snippets Groups Projects
tlv_protocols_supported.go 1.71 KiB
Newer Older
  • Learn to ignore specific revisions
  • package packet
    
    import (
    	"bytes"
    
    Oliver Herms's avatar
    Oliver Herms committed
    
    	"github.com/bio-routing/bio-rd/util/decode"
    
    Julian Kornberger's avatar
    Julian Kornberger committed
    	"github.com/pkg/errors"
    
    )
    
    // ProtocolsSupportedTLVType is the type value of an protocols supported TLV
    const ProtocolsSupportedTLVType = 129
    
    // ProtocolsSupportedTLV represents a protocols supported TLV
    type ProtocolsSupportedTLV struct {
    	TLVType                 uint8
    	TLVLength               uint8
    
    Oliver Herms's avatar
    Oliver Herms committed
    	NetworkLayerProtocolIDs []uint8
    
    Oliver Herms's avatar
    Oliver Herms committed
    func readProtocolsSupportedTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ProtocolsSupportedTLV, error) {
    
    	pdu := &ProtocolsSupportedTLV{
    		TLVType:                 tlvType,
    		TLVLength:               tlvLength,
    
    Oliver Herms's avatar
    Oliver Herms committed
    		NetworkLayerProtocolIDs: make([]uint8, tlvLength),
    
    	}
    
    	protoID := uint8(0)
    	fields := []interface{}{
    		&protoID,
    	}
    
    	for i := uint8(0); i < tlvLength; i++ {
    
    Oliver Herms's avatar
    Oliver Herms committed
    		err := decode.Decode(buf, fields)
    
    Julian Kornberger's avatar
    Julian Kornberger committed
    			return nil, errors.Wrap(err, "Unable to decode fields")
    
    Oliver Herms's avatar
    Oliver Herms committed
    		pdu.NetworkLayerProtocolIDs[i] = protoID
    
    Oliver Herms's avatar
    Oliver Herms committed
    	return pdu, nil
    
    }
    
    func NewProtocolsSupportedTLV(protocols []uint8) ProtocolsSupportedTLV {
    	return ProtocolsSupportedTLV{
    		TLVType:                 ProtocolsSupportedTLVType,
    		TLVLength:               uint8(len(protocols)),
    
    Oliver Herms's avatar
    Oliver Herms committed
    		NetworkLayerProtocolIDs: protocols,
    
    	}
    }
    
    // Type gets the type of the TLV
    func (p ProtocolsSupportedTLV) Type() uint8 {
    	return p.TLVType
    }
    
    // Length gets the length of the TLV
    func (p ProtocolsSupportedTLV) Length() uint8 {
    	return p.TLVLength
    }
    
    // Value gets the TLV itself
    func (p ProtocolsSupportedTLV) Value() interface{} {
    	return p
    }
    
    // Serialize serializes a protocols supported TLV
    func (p ProtocolsSupportedTLV) Serialize(buf *bytes.Buffer) {
    	buf.WriteByte(p.TLVType)
    	buf.WriteByte(p.TLVLength)
    
    Oliver Herms's avatar
    Oliver Herms committed
    	buf.Write(p.NetworkLayerProtocolIDs)